blob: 89ce14b35adc3a435bee5a067b775eff426acf04 [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
Ognjen Galic2801b962018-02-07 15:58:44 +010026#define TPACPI_VERSION "0.26"
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>
Eric W. Biederman7a360942017-09-26 12:45:33 -050060#include <linux/sched/signal.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020061#include <linux/kthread.h>
62#include <linux/freezer.h>
63#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090064#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020065#include <linux/nvram.h>
66#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020067#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020068#include <linux/sysfs.h>
69#include <linux/backlight.h>
Ognjen Galic2801b962018-02-07 15:58:44 +010070#include <linux/bitops.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020071#include <linux/fb.h>
72#include <linux/platform_device.h>
73#include <linux/hwmon.h>
74#include <linux/hwmon-sysfs.h>
75#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030076#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030077#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020078#include <linux/dmi.h>
79#include <linux/jiffies.h>
80#include <linux/workqueue.h>
Lv Zheng8b484632013-12-03 08:49:16 +080081#include <linux/acpi.h>
Jiaxun Yangf7db8392019-03-07 17:37:16 +080082#include <linux/pci.h>
Ognjen Galic2801b962018-02-07 15:58:44 +010083#include <linux/power_supply.h>
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020084#include <sound/core.h>
85#include <sound/control.h>
86#include <sound/initval.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080087#include <linux/uaccess.h>
Ognjen Galic2801b962018-02-07 15:58:44 +010088#include <acpi/battery.h>
Hans de Goedeb33c6ce2015-06-16 16:28:10 +020089#include <acpi/video.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020090
91/* ThinkPad CMOS commands */
92#define TP_CMOS_VOLUME_DOWN 0
93#define TP_CMOS_VOLUME_UP 1
94#define TP_CMOS_VOLUME_MUTE 2
95#define TP_CMOS_BRIGHTNESS_UP 4
96#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030097#define TP_CMOS_THINKLIGHT_ON 12
98#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020099
100/* NVRAM Addresses */
101enum tp_nvram_addr {
102 TP_NVRAM_ADDR_HK2 = 0x57,
103 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
104 TP_NVRAM_ADDR_VIDEO = 0x59,
105 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
106 TP_NVRAM_ADDR_MIXER = 0x60,
107};
108
109/* NVRAM bit masks */
110enum {
111 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
112 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
113 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
114 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
115 TP_NVRAM_MASK_THINKLIGHT = 0x10,
116 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
117 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
118 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
119 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
120 TP_NVRAM_MASK_MUTE = 0x40,
121 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
122 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
123 TP_NVRAM_POS_LEVEL_VOLUME = 0,
124};
125
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300126/* Misc NVRAM-related */
127enum {
128 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
129};
130
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200131/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400132#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
133#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Hui Wanga3c42a42016-11-08 16:13:23 +0800134#define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300135#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200136
137/* Input IDs */
138#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
139#define TPACPI_HKEY_INPUT_VERSION 0x4101
140
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200141/* ACPI \WGSV commands */
142enum {
143 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
144 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
145 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
146 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
147};
148
149/* TP_ACPI_WGSV_GET_STATE bits */
150enum {
151 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
152 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
153 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
154 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
155 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
156 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
157 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
158 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
159 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
160 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
161};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200162
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300163/* HKEY events */
164enum tpacpi_hkey_event_t {
165 /* Hotkey-related */
166 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
167 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
168 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
Hans de Goedec685e202017-02-09 16:44:13 +0100169 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300170 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
171 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
172 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
173
174 /* Reasons for waking up from S3/S4 */
175 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
176 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
177 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
178 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
179 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
180 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
181
182 /* Auto-sleep after eject request */
183 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
184 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
185
186 /* Misc bay events */
187 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -0300188 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
189 or port replicator */
190 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
191 dock or port replicator */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300192
193 /* User-interface events */
194 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
195 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
196 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
197 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
Lyudeb03f4d42016-11-11 15:15:03 -0500198 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
199 * enter/leave tablet mode
200 */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300201 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
202 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
203 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
204
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300205 /* Key-related user-interface events */
206 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
207 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
Xavier Naveira67ab6242015-02-10 08:45:18 +0100208 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300209
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300210 /* Thermal events */
211 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
212 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
213 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
214 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
Henrique de Moraes Holschuh6e6bc5f62018-04-24 16:56:03 -0300215 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
216 TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
217 * command completed. Related to
218 * AML DYTC */
219 TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
220 * changed. Related to AML GMTS */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300221
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100222 /* AC-related events */
223 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300224
David Herrmann587d8622018-01-12 12:04:45 +0100225 /* Further user-interface events */
226 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
227 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
228
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300229 /* Misc */
230 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
231};
232
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200233/****************************************************************************
234 * Main driver
235 */
236
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200237#define TPACPI_NAME "thinkpad"
238#define TPACPI_DESC "ThinkPad ACPI Extras"
239#define TPACPI_FILE TPACPI_NAME "_acpi"
240#define TPACPI_URL "http://ibm-acpi.sf.net/"
241#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200242
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200243#define TPACPI_PROC_DIR "ibm"
244#define TPACPI_ACPI_EVENT_PREFIX "ibm"
245#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300246#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200247#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200248
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300249#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300250#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300251
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200252#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200253
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000254/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200255#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000256#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200257#define TPACPI_DBG_INIT 0x0001
258#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +0000259#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000260#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000261#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000262#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200263#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200264
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200265#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
266#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
267#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200268
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200269
270/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200271 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200272 */
273
274struct ibm_struct;
275
276struct tp_acpi_drv_struct {
277 const struct acpi_device_id *hid;
278 struct acpi_driver *driver;
279
280 void (*notify) (struct ibm_struct *, u32);
281 acpi_handle *handle;
282 u32 type;
283 struct acpi_device *device;
284};
285
286struct ibm_struct {
287 char *name;
288
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200289 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200290 int (*write) (char *);
291 void (*exit) (void);
292 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200293 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200294 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200295
296 struct list_head all_drivers;
297
298 struct tp_acpi_drv_struct *acpi;
299
300 struct {
301 u8 acpi_driver_registered:1;
302 u8 acpi_notify_installed:1;
303 u8 proc_created:1;
304 u8 init_called:1;
305 u8 experimental:1;
306 } flags;
307};
308
309struct ibm_init_struct {
310 char param[32];
311
312 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400313 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200314 struct ibm_struct *data;
315};
316
317static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200318 u32 bluetooth:1;
319 u32 hotkey:1;
320 u32 hotkey_mask:1;
321 u32 hotkey_wlsw:1;
Lyudeb3180022016-11-11 15:15:02 -0500322 enum {
323 TP_HOTKEY_TABLET_NONE = 0,
324 TP_HOTKEY_TABLET_USES_MHKG,
Benjamin Bergdda3ec02017-09-15 15:20:49 +0200325 TP_HOTKEY_TABLET_USES_GMMS,
Lyudeb3180022016-11-11 15:15:02 -0500326 } hotkey_tablet;
Pali Rohárbb28f3d52015-12-30 23:27:41 +0100327 u32 kbdlight:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200328 u32 light:1;
329 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300330 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300331 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200332 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200333 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200334 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300335 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300336 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200337 u32 mixer_no_level_control:1;
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +0200338 u32 battery_force_primary:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200339 u32 input_device_registered:1;
340 u32 platform_drv_registered:1;
341 u32 platform_drv_attrs_registered:1;
342 u32 sensors_pdrv_registered:1;
343 u32 sensors_pdrv_attrs_registered:1;
344 u32 sensors_pdev_attrs_registered:1;
345 u32 hotkey_poll_active:1;
Bastien Noceraf23a5bc2015-03-02 14:45:16 +0100346 u32 has_adaptive_kbd:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200347} tp_features;
348
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300349static struct {
350 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200351 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300352} tp_warned;
353
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200354struct thinkpad_id_data {
355 unsigned int vendor; /* ThinkPad vendor:
356 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
357
358 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
359 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
360
Jouke Witteveen846a4162018-07-11 11:44:41 +0200361 u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
362 u32 ec_model;
363 u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300364 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200365
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300366 char *model_str; /* ThinkPad T43 */
367 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200368};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200369static struct thinkpad_id_data thinkpad_id;
370
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300371static enum {
372 TPACPI_LIFE_INIT = 0,
373 TPACPI_LIFE_RUNNING,
374 TPACPI_LIFE_EXITING,
375} tpacpi_lifecycle;
376
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200377static int experimental;
378static u32 dbg_level;
379
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300380static struct workqueue_struct *tpacpi_wq;
381
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000382enum led_status_t {
383 TPACPI_LED_OFF = 0,
384 TPACPI_LED_ON,
385 TPACPI_LED_BLINK,
386};
387
Hans de Goede86ec0c22017-02-09 16:44:12 +0100388/* tpacpi LED class */
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300389struct tpacpi_led_classdev {
390 struct led_classdev led_classdev;
Adam Leeedf2d772013-06-08 16:51:15 +0800391 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300392};
393
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300394/* brightness level capabilities */
395static unsigned int bright_maxlvl; /* 0 = unknown */
396
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200397#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
398static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030399static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200400static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030401static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200402static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030403static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200404static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030405static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200406#endif
407
408
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000409/*************************************************************************
410 * Debugging helpers
411 */
412
Joe Perches0978e012011-04-04 10:06:25 -0700413#define dbg_printk(a_dbg_level, format, arg...) \
414do { \
415 if (dbg_level & (a_dbg_level)) \
416 printk(KERN_DEBUG pr_fmt("%s: " format), \
417 __func__, ##arg); \
418} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000419
420#ifdef CONFIG_THINKPAD_ACPI_DEBUG
421#define vdbg_printk dbg_printk
422static const char *str_supported(int is_supported);
423#else
Joe Perches0978e012011-04-04 10:06:25 -0700424static inline const char *str_supported(int is_supported) { return ""; }
425#define vdbg_printk(a_dbg_level, format, arg...) \
Joe Perchesefd85cf2015-08-26 11:13:38 -0700426 do { if (0) no_printk(format, ##arg); } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000427#endif
428
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000429static void tpacpi_log_usertask(const char * const what)
430{
Joe Perches0978e012011-04-04 10:06:25 -0700431 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
432 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000433}
434
Joe Perches0978e012011-04-04 10:06:25 -0700435#define tpacpi_disclose_usertask(what, format, arg...) \
436do { \
437 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
438 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
439 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
440 what, task_tgid_vnr(current), ## arg); \
441 } \
442} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000443
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300444/*
445 * Quirk handling helpers
446 *
Jouke Witteveen846a4162018-07-11 11:44:41 +0200447 * ThinkPad IDs and versions seen in the field so far are
448 * two or three characters from the set [0-9A-Z], i.e. base 36.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300449 *
450 * We use values well outside that range as specials.
451 */
452
Jouke Witteveen846a4162018-07-11 11:44:41 +0200453#define TPACPI_MATCH_ANY 0xffffffffU
454#define TPACPI_MATCH_ANY_VERSION 0xffffU
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300455#define TPACPI_MATCH_UNKNOWN 0U
456
Jouke Witteveen846a4162018-07-11 11:44:41 +0200457/* TPID('1', 'Y') == 0x3159 */
458#define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
459#define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
460#define TPVER TPID
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300461
462#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
463 { .vendor = PCI_VENDOR_ID_IBM, \
464 .bios = TPID(__id1, __id2), \
465 .ec = TPACPI_MATCH_ANY, \
466 .quirks = (__quirk) }
467
468#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
469 { .vendor = PCI_VENDOR_ID_LENOVO, \
470 .bios = TPID(__id1, __id2), \
471 .ec = TPACPI_MATCH_ANY, \
472 .quirks = (__quirk) }
473
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +0200474#define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
475 { .vendor = PCI_VENDOR_ID_LENOVO, \
476 .bios = TPID3(__id1, __id2, __id3), \
477 .ec = TPACPI_MATCH_ANY, \
478 .quirks = (__quirk) }
479
Jouke Witteveen599eeff2018-12-05 12:17:52 +0100480#define TPACPI_QEC_IBM(__id1, __id2, __quirk) \
481 { .vendor = PCI_VENDOR_ID_IBM, \
482 .bios = TPACPI_MATCH_ANY, \
483 .ec = TPID(__id1, __id2), \
484 .quirks = (__quirk) }
485
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200486#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
487 { .vendor = PCI_VENDOR_ID_LENOVO, \
488 .bios = TPACPI_MATCH_ANY, \
489 .ec = TPID(__id1, __id2), \
490 .quirks = (__quirk) }
491
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300492struct tpacpi_quirk {
493 unsigned int vendor;
Jouke Witteveen846a4162018-07-11 11:44:41 +0200494 u32 bios;
495 u32 ec;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300496 unsigned long quirks;
497};
498
499/**
500 * tpacpi_check_quirks() - search BIOS/EC version on a list
501 * @qlist: array of &struct tpacpi_quirk
502 * @qlist_size: number of elements in @qlist
503 *
504 * Iterates over a quirks list until one is found that matches the
505 * ThinkPad's vendor, BIOS and EC model.
506 *
507 * Returns 0 if nothing matches, otherwise returns the quirks field of
508 * the matching &struct tpacpi_quirk entry.
509 *
510 * The match criteria is: vendor, ec and bios much match.
511 */
512static unsigned long __init tpacpi_check_quirks(
513 const struct tpacpi_quirk *qlist,
514 unsigned int qlist_size)
515{
516 while (qlist_size) {
517 if ((qlist->vendor == thinkpad_id.vendor ||
518 qlist->vendor == TPACPI_MATCH_ANY) &&
519 (qlist->bios == thinkpad_id.bios_model ||
520 qlist->bios == TPACPI_MATCH_ANY) &&
521 (qlist->ec == thinkpad_id.ec_model ||
522 qlist->ec == TPACPI_MATCH_ANY))
523 return qlist->quirks;
524
525 qlist_size--;
526 qlist++;
527 }
528 return 0;
529}
530
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300531static inline bool __pure __init tpacpi_is_lenovo(void)
532{
533 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
534}
535
536static inline bool __pure __init tpacpi_is_ibm(void)
537{
538 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
539}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300540
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300541/****************************************************************************
542 ****************************************************************************
543 *
544 * ACPI Helpers and device model
545 *
546 ****************************************************************************
547 ****************************************************************************/
548
549/*************************************************************************
550 * ACPI basic handles
551 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300553static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300554static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200556#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700558 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300559 &parent##_handle; \
560 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200562TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
563TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200565TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
566 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400567 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
568 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300569 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400570
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200571TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400572 "^HKEY", /* R30, R31 */
573 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300574 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400575
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300576/*************************************************************************
577 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200578 */
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700581 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400584 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200585 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400586 struct acpi_buffer result, *resultp;
587 union acpi_object out_obj;
588 acpi_status status;
589 va_list ap;
590 char res_type;
591 int success;
592 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700595 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return 0;
597 }
598
599 if (*fmt == 'q') {
600 quiet = 1;
601 fmt++;
602 } else
603 quiet = 0;
604
605 res_type = *(fmt++);
606
607 params.count = 0;
608 params.pointer = &in_objs[0];
609
610 va_start(ap, fmt);
611 while (*fmt) {
612 char c = *(fmt++);
613 switch (c) {
614 case 'd': /* int */
615 in_objs[params.count].integer.value = va_arg(ap, int);
616 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
617 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400618 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300620 pr_err("acpi_evalf() called with invalid format character '%c'\n",
621 c);
Jesper Juhl21365852010-12-24 19:56:28 +0100622 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624 }
625 }
626 va_end(ap);
627
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400628 if (res_type != 'v') {
629 result.length = sizeof(out_obj);
630 result.pointer = &out_obj;
631 resultp = &result;
632 } else
633 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400635 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400638 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300639 success = (status == AE_OK &&
640 out_obj.type == ACPI_TYPE_INTEGER);
641 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700642 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400644 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 success = status == AE_OK;
646 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400647 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300649 pr_err("acpi_evalf() called with invalid format character '%c'\n",
650 res_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return 0;
652 }
653
654 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700655 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300656 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 return success;
659}
660
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200661static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300662{
663 int v;
664
665 if (ecrd_handle) {
666 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
667 return 0;
668 *p = v;
669 } else {
670 if (ec_read(i, p) < 0)
671 return 0;
672 }
673
674 return 1;
675}
676
677static int acpi_ec_write(int i, u8 v)
678{
679 if (ecwr_handle) {
680 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
681 return 0;
682 } else {
683 if (ec_write(i, v) < 0)
684 return 0;
685 }
686
687 return 1;
688}
689
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300690static int issue_thinkpad_cmos_command(int cmos_cmd)
691{
692 if (!cmos_handle)
693 return -ENXIO;
694
695 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
696 return -EIO;
697
698 return 0;
699}
700
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300701/*************************************************************************
702 * ACPI device model
703 */
704
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200705#define TPACPI_ACPIHANDLE_INIT(object) \
706 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300707 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200708
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300709static void __init drv_acpi_handle_init(const char *name,
710 acpi_handle *handle, const acpi_handle parent,
711 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300712{
713 int i;
714 acpi_status status;
715
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300716 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
717 name);
718
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300719 for (i = 0; i < num_paths; i++) {
720 status = acpi_get_handle(parent, paths[i], handle);
721 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300722 dbg_printk(TPACPI_DBG_INIT,
723 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300724 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300725 return;
726 }
727 }
728
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300729 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
730 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300731 *handle = NULL;
732}
733
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300734static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
735 u32 level, void *context, void **return_value)
736{
Aaron Lu46445b62013-10-11 21:27:46 +0800737 struct acpi_device *dev;
738 if (!strcmp(context, "video")) {
739 if (acpi_bus_get_device(handle, &dev))
740 return AE_OK;
741 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
742 return AE_OK;
743 }
744
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300745 *(acpi_handle *)return_value = handle;
746
747 return AE_CTRL_TERMINATE;
748}
749
750static void __init tpacpi_acpi_handle_locate(const char *name,
751 const char *hid,
752 acpi_handle *handle)
753{
754 acpi_status status;
755 acpi_handle device_found;
756
Aaron Lu46445b62013-10-11 21:27:46 +0800757 BUG_ON(!name || !handle);
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300758 vdbg_printk(TPACPI_DBG_INIT,
759 "trying to locate ACPI handle for %s, using HID %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +0800760 name, hid ? hid : "NULL");
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300761
762 memset(&device_found, 0, sizeof(device_found));
763 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
764 (void *)name, &device_found);
765
766 *handle = NULL;
767
768 if (ACPI_SUCCESS(status)) {
769 *handle = device_found;
770 dbg_printk(TPACPI_DBG_INIT,
771 "Found ACPI handle for %s\n", name);
772 } else {
773 vdbg_printk(TPACPI_DBG_INIT,
774 "Could not locate an ACPI handle for %s: %s\n",
775 name, acpi_format_exception(status));
776 }
777}
778
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300779static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300780{
781 struct ibm_struct *ibm = data;
782
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300783 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
784 return;
785
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300786 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300787 return;
788
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300789 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300790}
791
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300792static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300793{
794 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300795 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300796
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300797 BUG_ON(!ibm->acpi);
798
799 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300800 return 0;
801
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300802 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300803 "setting up ACPI notify for %s\n", ibm->name);
804
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300805 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
806 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700807 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300808 return -ENODEV;
809 }
810
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700811 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300812 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200813 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300814 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300815
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300816 status = acpi_install_notify_handler(*ibm->acpi->handle,
817 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300818 if (ACPI_FAILURE(status)) {
819 if (status == AE_ALREADY_EXISTS) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300820 pr_notice("another device driver is already handling %s events\n",
821 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300822 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700823 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300824 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300825 }
826 return -ENODEV;
827 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300828 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300829 return 0;
830}
831
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300832static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300833{
834 return 0;
835}
836
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300837static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300838{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300839 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300840
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300841 dbg_printk(TPACPI_DBG_INIT,
842 "registering %s as an ACPI driver\n", ibm->name);
843
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300844 BUG_ON(!ibm->acpi);
845
846 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
847 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700848 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300849 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300850 }
851
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200852 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300853 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200854
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300855 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300856
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300857 rc = acpi_bus_register_driver(ibm->acpi->driver);
858 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700859 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200860 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300861 kfree(ibm->acpi->driver);
862 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300863 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300864 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300865
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300866 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300867}
868
869
870/****************************************************************************
871 ****************************************************************************
872 *
873 * Procfs Helpers
874 *
875 ****************************************************************************
876 ****************************************************************************/
877
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200878static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300879{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200880 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300881
882 if (!ibm || !ibm->read)
883 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200884 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300885}
886
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200887static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300888{
Al Virod9dda782013-03-31 18:16:14 -0400889 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200890}
891
892static ssize_t dispatch_proc_write(struct file *file,
893 const char __user *userbuf,
894 size_t count, loff_t *pos)
895{
Al Virod9dda782013-03-31 18:16:14 -0400896 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300897 char *kernbuf;
898 int ret;
899
900 if (!ibm || !ibm->write)
901 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300902 if (count > PAGE_SIZE - 2)
903 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300904
905 kernbuf = kmalloc(count + 2, GFP_KERNEL);
906 if (!kernbuf)
907 return -ENOMEM;
908
909 if (copy_from_user(kernbuf, userbuf, count)) {
910 kfree(kernbuf);
911 return -EFAULT;
912 }
913
914 kernbuf[count] = 0;
915 strcat(kernbuf, ",");
916 ret = ibm->write(kernbuf);
917 if (ret == 0)
918 ret = count;
919
920 kfree(kernbuf);
921
922 return ret;
923}
924
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200925static const struct file_operations dispatch_proc_fops = {
926 .owner = THIS_MODULE,
927 .open = dispatch_proc_open,
928 .read = seq_read,
929 .llseek = seq_lseek,
930 .release = single_release,
931 .write = dispatch_proc_write,
932};
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934static char *next_cmd(char **cmds)
935{
936 char *start = *cmds;
937 char *end;
938
939 while ((end = strchr(start, ',')) && end == start)
940 start = end + 1;
941
942 if (!end)
943 return NULL;
944
945 *end = 0;
946 *cmds = end + 1;
947 return start;
948}
949
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300950
951/****************************************************************************
952 ****************************************************************************
953 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300954 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300955 *
956 ****************************************************************************
957 ****************************************************************************/
958
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300959static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300960static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700961static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300962static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300963static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200964static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300965
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200966#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200967static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200968{
969 struct ibm_struct *ibm, *itmp;
970
971 list_for_each_entry_safe(ibm, itmp,
972 &tpacpi_all_drivers,
973 all_drivers) {
974 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200975 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200976 }
977
978 return 0;
979}
980
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200981static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300982{
983 struct ibm_struct *ibm, *itmp;
984
985 list_for_each_entry_safe(ibm, itmp,
986 &tpacpi_all_drivers,
987 all_drivers) {
988 if (ibm->resume)
989 (ibm->resume)();
990 }
991
992 return 0;
993}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200994#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300995
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200996static SIMPLE_DEV_PM_OPS(tpacpi_pm,
997 tpacpi_suspend_handler, tpacpi_resume_handler);
998
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200999static void tpacpi_shutdown_handler(struct platform_device *pdev)
1000{
1001 struct ibm_struct *ibm, *itmp;
1002
1003 list_for_each_entry_safe(ibm, itmp,
1004 &tpacpi_all_drivers,
1005 all_drivers) {
1006 if (ibm->shutdown)
1007 (ibm->shutdown)();
1008 }
1009}
1010
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001011static struct platform_driver tpacpi_pdriver = {
1012 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001013 .name = TPACPI_DRVR_NAME,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +02001014 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001015 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001016 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001017};
1018
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03001019static struct platform_driver tpacpi_hwmon_pdriver = {
1020 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001021 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03001022 },
1023};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001024
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03001025/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001026 * sysfs support helpers
1027 */
1028
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001029struct attribute_set {
1030 unsigned int members, max_members;
1031 struct attribute_group group;
1032};
1033
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001034struct attribute_set_obj {
1035 struct attribute_set s;
1036 struct attribute *a;
1037} __attribute__((packed));
1038
1039static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001040 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001041{
1042 struct attribute_set_obj *sobj;
1043
1044 if (max_members == 0)
1045 return NULL;
1046
1047 /* Allocates space for implicit NULL at the end too */
1048 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1049 max_members * sizeof(struct attribute *),
1050 GFP_KERNEL);
1051 if (!sobj)
1052 return NULL;
1053 sobj->s.max_members = max_members;
1054 sobj->s.group.attrs = &sobj->a;
1055 sobj->s.group.name = name;
1056
1057 return &sobj->s;
1058}
1059
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001060#define destroy_attr_set(_set) \
1061 kfree(_set);
1062
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001063/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001064static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001065{
1066 if (!s || !attr)
1067 return -EINVAL;
1068
1069 if (s->members >= s->max_members)
1070 return -ENOMEM;
1071
1072 s->group.attrs[s->members] = attr;
1073 s->members++;
1074
1075 return 0;
1076}
1077
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001078static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001079 struct attribute **attr,
1080 unsigned int count)
1081{
1082 int i, res;
1083
1084 for (i = 0; i < count; i++) {
1085 res = add_to_attr_set(s, attr[i]);
1086 if (res)
1087 return res;
1088 }
1089
1090 return 0;
1091}
1092
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001093static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001094{
1095 sysfs_remove_group(kobj, &s->group);
1096 destroy_attr_set(s);
1097}
1098
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001099#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1100 sysfs_create_group(_kobj, &_attr_set->group)
1101
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001102static int parse_strtoul(const char *buf,
1103 unsigned long max, unsigned long *value)
1104{
1105 char *endp;
1106
André Goddard Rosae7d28602009-12-14 18:01:06 -08001107 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1108 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001109 if (*endp || *value > max)
1110 return -EINVAL;
1111
1112 return 0;
1113}
1114
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001115static void tpacpi_disable_brightness_delay(void)
1116{
1117 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001118 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001119}
1120
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001121static void printk_deprecated_attribute(const char * const what,
1122 const char * const details)
1123{
1124 tpacpi_log_usertask("deprecated sysfs attribute");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001125 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001126 what, details);
1127}
1128
Johannes Berg19d337d2009-06-02 13:01:37 +02001129/*************************************************************************
1130 * rfkill and radio control support helpers
1131 */
1132
1133/*
1134 * ThinkPad-ACPI firmware handling model:
1135 *
1136 * WLSW (master wireless switch) is event-driven, and is common to all
1137 * firmware-controlled radios. It cannot be controlled, just monitored,
1138 * as expected. It overrides all radio state in firmware
1139 *
1140 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1141 * (TODO: verify how WLSW interacts with the returned radio state).
1142 *
1143 * The only time there are shadow radio state changes, is when
1144 * masked-off hotkeys are used.
1145 */
1146
1147/*
1148 * Internal driver API for radio state:
1149 *
1150 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1151 * bool: true means radio blocked (off)
1152 */
1153enum tpacpi_rfkill_state {
1154 TPACPI_RFK_RADIO_OFF = 0,
1155 TPACPI_RFK_RADIO_ON
1156};
1157
1158/* rfkill switches */
1159enum tpacpi_rfk_id {
1160 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1161 TPACPI_RFK_WWAN_SW_ID,
1162 TPACPI_RFK_UWB_SW_ID,
1163 TPACPI_RFK_SW_MAX
1164};
1165
1166static const char *tpacpi_rfkill_names[] = {
1167 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1168 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1169 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1170 [TPACPI_RFK_SW_MAX] = NULL
1171};
1172
1173/* ThinkPad-ACPI rfkill subdriver */
1174struct tpacpi_rfk {
1175 struct rfkill *rfkill;
1176 enum tpacpi_rfk_id id;
1177 const struct tpacpi_rfk_ops *ops;
1178};
1179
1180struct tpacpi_rfk_ops {
1181 /* firmware interface */
1182 int (*get_status)(void);
1183 int (*set_status)(const enum tpacpi_rfkill_state);
1184};
1185
1186static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1187
1188/* Query FW and update rfkill sw state for a given rfkill switch */
1189static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1190{
1191 int status;
1192
1193 if (!tp_rfk)
1194 return -ENODEV;
1195
1196 status = (tp_rfk->ops->get_status)();
1197 if (status < 0)
1198 return status;
1199
1200 rfkill_set_sw_state(tp_rfk->rfkill,
1201 (status == TPACPI_RFK_RADIO_OFF));
1202
1203 return status;
1204}
1205
1206/* Query FW and update rfkill sw state for all rfkill switches */
1207static void tpacpi_rfk_update_swstate_all(void)
1208{
1209 unsigned int i;
1210
1211 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1212 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1213}
1214
1215/*
1216 * Sync the HW-blocking state of all rfkill switches,
1217 * do notice it causes the rfkill core to schedule uevents
1218 */
1219static void tpacpi_rfk_update_hwblock_state(bool blocked)
1220{
1221 unsigned int i;
1222 struct tpacpi_rfk *tp_rfk;
1223
1224 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1225 tp_rfk = tpacpi_rfkill_switches[i];
1226 if (tp_rfk) {
1227 if (rfkill_set_hw_state(tp_rfk->rfkill,
1228 blocked)) {
1229 /* ignore -- we track sw block */
1230 }
1231 }
1232 }
1233}
1234
1235/* Call to get the WLSW state from the firmware */
1236static int hotkey_get_wlsw(void);
1237
1238/* Call to query WLSW state and update all rfkill switches */
1239static bool tpacpi_rfk_check_hwblock_state(void)
1240{
1241 int res = hotkey_get_wlsw();
1242 int hw_blocked;
1243
1244 /* When unknown or unsupported, we have to assume it is unblocked */
1245 if (res < 0)
1246 return false;
1247
1248 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1249 tpacpi_rfk_update_hwblock_state(hw_blocked);
1250
1251 return hw_blocked;
1252}
1253
1254static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1255{
1256 struct tpacpi_rfk *tp_rfk = data;
1257 int res;
1258
1259 dbg_printk(TPACPI_DBG_RFKILL,
1260 "request to change radio state to %s\n",
1261 blocked ? "blocked" : "unblocked");
1262
1263 /* try to set radio state */
1264 res = (tp_rfk->ops->set_status)(blocked ?
1265 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1266
1267 /* and update the rfkill core with whatever the FW really did */
1268 tpacpi_rfk_update_swstate(tp_rfk);
1269
1270 return (res < 0) ? res : 0;
1271}
1272
1273static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1274 .set_block = tpacpi_rfk_hook_set_block,
1275};
1276
1277static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1278 const struct tpacpi_rfk_ops *tp_rfkops,
1279 const enum rfkill_type rfktype,
1280 const char *name,
1281 const bool set_default)
1282{
1283 struct tpacpi_rfk *atp_rfk;
1284 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001285 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001286 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001287 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001288
1289 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1290
Johannes Berg19d337d2009-06-02 13:01:37 +02001291 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1292 if (atp_rfk)
1293 atp_rfk->rfkill = rfkill_alloc(name,
1294 &tpacpi_pdev->dev,
1295 rfktype,
1296 &tpacpi_rfk_rfkill_ops,
1297 atp_rfk);
1298 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001299 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001300 kfree(atp_rfk);
1301 return -ENOMEM;
1302 }
1303
1304 atp_rfk->id = id;
1305 atp_rfk->ops = tp_rfkops;
1306
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001307 sw_status = (tp_rfkops->get_status)();
1308 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001309 pr_err("failed to read initial state for %s, error %d\n",
1310 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001311 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001312 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001313 if (set_default) {
1314 /* try to keep the initial state, since we ask the
1315 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001316 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001317 }
1318 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001319 hw_state = tpacpi_rfk_check_hwblock_state();
1320 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001321
1322 res = rfkill_register(atp_rfk->rfkill);
1323 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001324 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001325 rfkill_destroy(atp_rfk->rfkill);
1326 kfree(atp_rfk);
1327 return res;
1328 }
1329
1330 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001331
Joe Perches0978e012011-04-04 10:06:25 -07001332 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001333 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001334 return 0;
1335}
1336
1337static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1338{
1339 struct tpacpi_rfk *tp_rfk;
1340
1341 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1342
1343 tp_rfk = tpacpi_rfkill_switches[id];
1344 if (tp_rfk) {
1345 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001346 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001347 tpacpi_rfkill_switches[id] = NULL;
1348 kfree(tp_rfk);
1349 }
1350}
1351
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001352static void printk_deprecated_rfkill_attribute(const char * const what)
1353{
1354 printk_deprecated_attribute(what,
1355 "Please switch to generic rfkill before year 2010");
1356}
1357
Johannes Berg19d337d2009-06-02 13:01:37 +02001358/* sysfs <radio> enable ------------------------------------------------ */
1359static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1360 struct device_attribute *attr,
1361 char *buf)
1362{
1363 int status;
1364
1365 printk_deprecated_rfkill_attribute(attr->attr.name);
1366
1367 /* This is in the ABI... */
1368 if (tpacpi_rfk_check_hwblock_state()) {
1369 status = TPACPI_RFK_RADIO_OFF;
1370 } else {
1371 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1372 if (status < 0)
1373 return status;
1374 }
1375
1376 return snprintf(buf, PAGE_SIZE, "%d\n",
1377 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1378}
1379
1380static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1381 struct device_attribute *attr,
1382 const char *buf, size_t count)
1383{
1384 unsigned long t;
1385 int res;
1386
1387 printk_deprecated_rfkill_attribute(attr->attr.name);
1388
1389 if (parse_strtoul(buf, 1, &t))
1390 return -EINVAL;
1391
1392 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1393
1394 /* This is in the ABI... */
1395 if (tpacpi_rfk_check_hwblock_state() && !!t)
1396 return -EPERM;
1397
1398 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1399 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1400 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1401
1402 return (res < 0) ? res : count;
1403}
1404
1405/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001406static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001407{
Johannes Berg19d337d2009-06-02 13:01:37 +02001408 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001409 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001410 else {
1411 int status;
1412
1413 /* This is in the ABI... */
1414 if (tpacpi_rfk_check_hwblock_state()) {
1415 status = TPACPI_RFK_RADIO_OFF;
1416 } else {
1417 status = tpacpi_rfk_update_swstate(
1418 tpacpi_rfkill_switches[id]);
1419 if (status < 0)
1420 return status;
1421 }
1422
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001423 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001424 (status == TPACPI_RFK_RADIO_ON) ?
1425 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001426 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001427 }
1428
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001429 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001430}
1431
1432static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1433{
1434 char *cmd;
1435 int status = -1;
1436 int res = 0;
1437
1438 if (id >= TPACPI_RFK_SW_MAX)
1439 return -ENODEV;
1440
1441 while ((cmd = next_cmd(&buf))) {
1442 if (strlencmp(cmd, "enable") == 0)
1443 status = TPACPI_RFK_RADIO_ON;
1444 else if (strlencmp(cmd, "disable") == 0)
1445 status = TPACPI_RFK_RADIO_OFF;
1446 else
1447 return -EINVAL;
1448 }
1449
1450 if (status != -1) {
1451 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1452 (status == TPACPI_RFK_RADIO_ON) ?
1453 "enable" : "disable",
1454 tpacpi_rfkill_names[id]);
1455 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1456 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1457 }
1458
1459 return res;
1460}
1461
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001462/*************************************************************************
1463 * thinkpad-acpi driver attributes
1464 */
1465
1466/* interface_version --------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001467static ssize_t interface_version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001468{
1469 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1470}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001471static DRIVER_ATTR_RO(interface_version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001472
1473/* debug_level --------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001474static ssize_t debug_level_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001475{
1476 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1477}
1478
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001479static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1480 size_t count)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001481{
1482 unsigned long t;
1483
1484 if (parse_strtoul(buf, 0xffff, &t))
1485 return -EINVAL;
1486
1487 dbg_level = t;
1488
1489 return count;
1490}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001491static DRIVER_ATTR_RW(debug_level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001492
1493/* version ------------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001494static ssize_t version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001495{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001496 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1497 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001498}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001499static DRIVER_ATTR_RO(version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001500
1501/* --------------------------------------------------------------------- */
1502
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001503#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1504
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001505/* wlsw_emulstate ------------------------------------------------------ */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001506static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001507{
1508 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1509}
1510
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001511static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1512 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001513{
1514 unsigned long t;
1515
1516 if (parse_strtoul(buf, 1, &t))
1517 return -EINVAL;
1518
Johannes Berg19d337d2009-06-02 13:01:37 +02001519 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001520 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001521 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1522 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001523
1524 return count;
1525}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001526static DRIVER_ATTR_RW(wlsw_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001527
1528/* bluetooth_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001529static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001530{
1531 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1532}
1533
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001534static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1535 const char *buf, size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001536{
1537 unsigned long t;
1538
1539 if (parse_strtoul(buf, 1, &t))
1540 return -EINVAL;
1541
1542 tpacpi_bluetooth_emulstate = !!t;
1543
1544 return count;
1545}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001546static DRIVER_ATTR_RW(bluetooth_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001547
1548/* wwan_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001549static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001550{
1551 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1552}
1553
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001554static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1555 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001556{
1557 unsigned long t;
1558
1559 if (parse_strtoul(buf, 1, &t))
1560 return -EINVAL;
1561
1562 tpacpi_wwan_emulstate = !!t;
1563
1564 return count;
1565}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001566static DRIVER_ATTR_RW(wwan_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001567
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001568/* uwb_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001569static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001570{
1571 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1572}
1573
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001574static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1575 size_t count)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001576{
1577 unsigned long t;
1578
1579 if (parse_strtoul(buf, 1, &t))
1580 return -EINVAL;
1581
1582 tpacpi_uwb_emulstate = !!t;
1583
1584 return count;
1585}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001586static DRIVER_ATTR_RW(uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001587#endif
1588
1589/* --------------------------------------------------------------------- */
1590
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001591static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001592 &driver_attr_debug_level, &driver_attr_version,
1593 &driver_attr_interface_version,
1594};
1595
1596static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1597{
1598 int i, res;
1599
1600 i = 0;
1601 res = 0;
1602 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1603 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1604 i++;
1605 }
1606
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001607#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1608 if (!res && dbg_wlswemul)
1609 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1610 if (!res && dbg_bluetoothemul)
1611 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1612 if (!res && dbg_wwanemul)
1613 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001614 if (!res && dbg_uwbemul)
1615 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001616#endif
1617
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001618 return res;
1619}
1620
1621static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1622{
1623 int i;
1624
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001625 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001626 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001627
1628#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1629 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1630 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1631 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001632 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001633#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001634}
1635
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001636/*************************************************************************
1637 * Firmware Data
1638 */
1639
1640/*
1641 * Table of recommended minimum BIOS versions
1642 *
1643 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001644 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001645 * bugs and bad ACPI behaviour on older versions
1646 *
1647 * 2. BIOS or EC fw with known bugs that trigger on Linux
1648 *
1649 * 3. BIOS with known reduced functionality in older versions
1650 *
1651 * We recommend the latest BIOS and EC version.
1652 * We only support the latest BIOS and EC fw version as a rule.
1653 *
1654 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1655 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001656 *
1657 * WARNING: we use this table also to detect that the machine is
1658 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001659 */
1660
1661#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1662 { .vendor = (__v), \
1663 .bios = TPID(__id1, __id2), \
1664 .ec = TPACPI_MATCH_ANY, \
Jouke Witteveen846a4162018-07-11 11:44:41 +02001665 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1666 | TPVER(__bv1, __bv2) }
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001667
1668#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001669 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001670 { .vendor = (__v), \
1671 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001672 .ec = __eid, \
Jouke Witteveen846a4162018-07-11 11:44:41 +02001673 .quirks = TPVER(__ev1, __ev2) << 16 \
1674 | TPVER(__bv1, __bv2) }
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001675
1676#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1677 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1678
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001679/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001680#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1681 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001682 __bv1, __bv2, TPID(__id1, __id2), \
1683 __ev1, __ev2), \
1684 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1685 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1686 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001687
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001688/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001689#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1690 __eid1, __eid2, __ev1, __ev2) \
1691 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001692 __bv1, __bv2, TPID(__eid1, __eid2), \
1693 __ev1, __ev2), \
1694 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1695 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1696 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001697
1698#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1699 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1700
1701#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1702 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001703 __bv1, __bv2, TPID(__id1, __id2), \
1704 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001705
1706#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1707 __eid1, __eid2, __ev1, __ev2) \
1708 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001709 __bv1, __bv2, TPID(__eid1, __eid2), \
1710 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001711
1712static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1713 /* Numeric models ------------------ */
1714 /* FW MODEL BIOS VERS */
1715 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1716 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1717 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1718 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1719 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1720 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1721 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1722 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1723 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1724
1725 /* A-series ------------------------- */
1726 /* FW MODEL BIOS VERS EC VERS */
1727 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1728 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1729 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1730 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1731 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1732 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1733 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1734 TPV_QI0('1', '3', '2', '0'), /* A22m */
1735 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1736 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1737 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1738
1739 /* G-series ------------------------- */
1740 /* FW MODEL BIOS VERS */
1741 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1742 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1743
1744 /* R-series, T-series --------------- */
1745 /* FW MODEL BIOS VERS EC VERS */
1746 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1747 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1748 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1749 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1750 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1751 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1752 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1753 T40/p, T41/p, T42/p (1) */
1754 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1755 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1756 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1757 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1758
1759 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1760 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1761 TPV_QI0('1', '6', '3', '2'), /* T22 */
1762 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1763 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1764 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1765
1766 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1767 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001768 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001769
1770 /* BIOS FW BIOS VERS EC FW EC VERS */
1771 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1772 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1773
1774 /* X-series ------------------------- */
1775 /* FW MODEL BIOS VERS EC VERS */
1776 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1777 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1778 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1779 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1780 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1781 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1782 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1783
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001784 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1785 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001786
1787 /* (0) - older versions lack DMI EC fw string and functionality */
1788 /* (1) - older versions known to lack functionality */
1789};
1790
1791#undef TPV_QL1
1792#undef TPV_QL0
1793#undef TPV_QI2
1794#undef TPV_QI1
1795#undef TPV_QI0
1796#undef TPV_Q_X
1797#undef TPV_Q
1798
1799static void __init tpacpi_check_outdated_fw(void)
1800{
1801 unsigned long fwvers;
1802 u16 ec_version, bios_version;
1803
1804 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1805 ARRAY_SIZE(tpacpi_bios_version_qtable));
1806
1807 if (!fwvers)
1808 return;
1809
1810 bios_version = fwvers & 0xffffU;
1811 ec_version = (fwvers >> 16) & 0xffffU;
1812
1813 /* note that unknown versions are set to 0x0000 and we use that */
1814 if ((bios_version > thinkpad_id.bios_release) ||
1815 (ec_version > thinkpad_id.ec_release &&
Jouke Witteveen846a4162018-07-11 11:44:41 +02001816 ec_version != TPACPI_MATCH_ANY_VERSION)) {
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001817 /*
1818 * The changelogs would let us track down the exact
1819 * reason, but it is just too much of a pain to track
1820 * it. We only list BIOSes that are either really
1821 * broken, or really stable to begin with, so it is
1822 * best if the user upgrades the firmware anyway.
1823 */
Joe Perches0978e012011-04-04 10:06:25 -07001824 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001825 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 -03001826 }
1827}
1828
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001829static bool __init tpacpi_is_fw_known(void)
1830{
1831 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1832 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1833}
1834
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001835/****************************************************************************
1836 ****************************************************************************
1837 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001838 * Subdrivers
1839 *
1840 ****************************************************************************
1841 ****************************************************************************/
1842
1843/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001844 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001845 */
1846
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001847static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001849 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1850 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001854static struct ibm_struct thinkpad_acpi_driver_data = {
1855 .name = "driver",
1856 .read = thinkpad_acpi_driver_read,
1857};
1858
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001859/*************************************************************************
1860 * Hotkey subdriver
1861 */
1862
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001863/*
1864 * ThinkPad firmware event model
1865 *
1866 * The ThinkPad firmware has two main event interfaces: normal ACPI
1867 * notifications (which follow the ACPI standard), and a private event
1868 * interface.
1869 *
1870 * The private event interface also issues events for the hotkeys. As
1871 * the driver gained features, the event handling code ended up being
1872 * built around the hotkey subdriver. This will need to be refactored
1873 * to a more formal event API eventually.
1874 *
1875 * Some "hotkeys" are actually supposed to be used as event reports,
1876 * such as "brightness has changed", "volume has changed", depending on
1877 * the ThinkPad model and how the firmware is operating.
1878 *
1879 * Unlike other classes, hotkey-class events have mask/unmask control on
1880 * non-ancient firmware. However, how it behaves changes a lot with the
1881 * firmware model and version.
1882 */
1883
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001884enum { /* hot key scan codes (derived from ACPI DSDT) */
1885 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1886 TP_ACPI_HOTKEYSCAN_FNF2,
1887 TP_ACPI_HOTKEYSCAN_FNF3,
1888 TP_ACPI_HOTKEYSCAN_FNF4,
1889 TP_ACPI_HOTKEYSCAN_FNF5,
1890 TP_ACPI_HOTKEYSCAN_FNF6,
1891 TP_ACPI_HOTKEYSCAN_FNF7,
1892 TP_ACPI_HOTKEYSCAN_FNF8,
1893 TP_ACPI_HOTKEYSCAN_FNF9,
1894 TP_ACPI_HOTKEYSCAN_FNF10,
1895 TP_ACPI_HOTKEYSCAN_FNF11,
1896 TP_ACPI_HOTKEYSCAN_FNF12,
1897 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1898 TP_ACPI_HOTKEYSCAN_FNINSERT,
1899 TP_ACPI_HOTKEYSCAN_FNDELETE,
1900 TP_ACPI_HOTKEYSCAN_FNHOME,
1901 TP_ACPI_HOTKEYSCAN_FNEND,
1902 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1903 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1904 TP_ACPI_HOTKEYSCAN_FNSPACE,
1905 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1906 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1907 TP_ACPI_HOTKEYSCAN_MUTE,
1908 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001909 TP_ACPI_HOTKEYSCAN_UNK1,
1910 TP_ACPI_HOTKEYSCAN_UNK2,
1911 TP_ACPI_HOTKEYSCAN_UNK3,
1912 TP_ACPI_HOTKEYSCAN_UNK4,
1913 TP_ACPI_HOTKEYSCAN_UNK5,
1914 TP_ACPI_HOTKEYSCAN_UNK6,
1915 TP_ACPI_HOTKEYSCAN_UNK7,
1916 TP_ACPI_HOTKEYSCAN_UNK8,
1917
Christian Kellner149c8c72017-02-28 17:10:56 +01001918 /* Adaptive keyboard keycodes */
1919 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1920 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
Bastien Nocera6a68d852015-03-02 14:45:31 +01001921 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1922 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1923 TP_ACPI_HOTKEYSCAN_CLOUD,
1924 TP_ACPI_HOTKEYSCAN_UNK9,
1925 TP_ACPI_HOTKEYSCAN_VOICE,
1926 TP_ACPI_HOTKEYSCAN_UNK10,
1927 TP_ACPI_HOTKEYSCAN_GESTURES,
1928 TP_ACPI_HOTKEYSCAN_UNK11,
1929 TP_ACPI_HOTKEYSCAN_UNK12,
1930 TP_ACPI_HOTKEYSCAN_UNK13,
1931 TP_ACPI_HOTKEYSCAN_CONFIG,
1932 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1933 TP_ACPI_HOTKEYSCAN_RELOAD,
1934 TP_ACPI_HOTKEYSCAN_BACK,
1935 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1936 TP_ACPI_HOTKEYSCAN_MIC_UP,
1937 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1938 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1939 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1940
Christian Kellner696c65232017-02-28 17:10:57 +01001941 /* Lenovo extended keymap, starting at 0x1300 */
1942 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1943 /* first new observed key (star, favorites) is 0x1311 */
1944 TP_ACPI_HOTKEYSCAN_STAR = 69,
1945 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
Benjamin Bergcb5c1972018-06-20 15:49:27 +02001946 TP_ACPI_HOTKEYSCAN_CALCULATOR,
Christian Kellner696c65232017-02-28 17:10:57 +01001947 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1948 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1949
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001950 /* Hotkey keymap size */
1951 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001952};
1953
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001954enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001955 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1956 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1957};
1958
1959enum { /* Positions of some of the keys in hotkey masks */
1960 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1961 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1962 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1963 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1964 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
Hans de Goedec685e202017-02-09 16:44:13 +01001965 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001966 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1967 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1968 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1969 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1970 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1971};
1972
1973enum { /* NVRAM to ACPI HKEY group map */
1974 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1975 TP_ACPI_HKEY_ZOOM_MASK |
1976 TP_ACPI_HKEY_DISPSWTCH_MASK |
1977 TP_ACPI_HKEY_HIBERNATE_MASK,
1978 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1979 TP_ACPI_HKEY_BRGHTDWN_MASK,
1980 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1981 TP_ACPI_HKEY_VOLDWN_MASK |
1982 TP_ACPI_HKEY_MUTE_MASK,
1983};
1984
1985#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1986struct tp_nvram_state {
1987 u16 thinkpad_toggle:1;
1988 u16 zoom_toggle:1;
1989 u16 display_toggle:1;
1990 u16 thinklight_toggle:1;
1991 u16 hibernate_toggle:1;
1992 u16 displayexp_toggle:1;
1993 u16 display_state:1;
1994 u16 brightness_toggle:1;
1995 u16 volume_toggle:1;
1996 u16 mute:1;
1997
1998 u8 brightness_level;
1999 u8 volume_level;
2000};
2001
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002002/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002003static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002004
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002005/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002006 * Acquire mutex to write poller control variables as an
2007 * atomic block.
2008 *
2009 * Increment hotkey_config_change when changing them if you
2010 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002011 *
2012 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2013 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002014static struct mutex hotkey_thread_data_mutex;
2015static unsigned int hotkey_config_change;
2016
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002017/*
2018 * hotkey poller control variables
2019 *
2020 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002021 *
2022 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2023 * should be used only when the changes need to be taken as
2024 * a block, OR when one needs to force the kthread to forget
2025 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002026 */
2027static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2028static unsigned int hotkey_poll_freq = 10; /* Hz */
2029
2030#define HOTKEY_CONFIG_CRITICAL_START \
2031 do { \
2032 mutex_lock(&hotkey_thread_data_mutex); \
2033 hotkey_config_change++; \
2034 } while (0);
2035#define HOTKEY_CONFIG_CRITICAL_END \
2036 mutex_unlock(&hotkey_thread_data_mutex);
2037
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002038#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2039
2040#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002041#define HOTKEY_CONFIG_CRITICAL_START
2042#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002043
2044#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2045
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002046static struct mutex hotkey_mutex;
2047
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002048static enum { /* Reasons for waking up */
2049 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2050 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2051 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2052} hotkey_wakeup_reason;
2053
2054static int hotkey_autosleep_ack;
2055
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002056static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2057static u32 hotkey_all_mask; /* all events supported in fw */
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002058static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002059static u32 hotkey_reserved_mask; /* events better left disabled */
2060static u32 hotkey_driver_mask; /* events needed by the driver */
2061static u32 hotkey_user_mask; /* events visible to userspace */
2062static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002063
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002064static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002065
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002066static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002067
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002068static void tpacpi_driver_event(const unsigned int hkey_event);
2069static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002070static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002071
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002072/* HKEY.MHKG() return bits */
2073#define TP_HOTKEY_TABLET_MASK (1 << 3)
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002074enum {
2075 TP_ACPI_MULTI_MODE_INVALID = 0,
2076 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
2077 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
2078 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
2079 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
2080 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
2081 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
2082 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
2083};
2084
2085enum {
2086 /* The following modes are considered tablet mode for the purpose of
2087 * reporting the status to userspace. i.e. in all these modes it makes
2088 * sense to disable the laptop input devices such as touchpad and
2089 * keyboard.
2090 */
2091 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
2092 TP_ACPI_MULTI_MODE_STAND |
2093 TP_ACPI_MULTI_MODE_TENT |
2094 TP_ACPI_MULTI_MODE_STAND_TENT,
2095};
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002096
Johannes Berg19d337d2009-06-02 13:01:37 +02002097static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002098{
Johannes Berg19d337d2009-06-02 13:01:37 +02002099 int status;
2100
2101 if (!tp_features.hotkey_wlsw)
2102 return -ENODEV;
2103
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002104#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002105 if (dbg_wlswemul)
2106 return (tpacpi_wlsw_emulstate) ?
2107 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002108#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002109
2110 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002111 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002112
2113 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002114}
2115
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002116static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
2117{
2118 int type = (s >> 16) & 0xffff;
2119 int value = s & 0xffff;
2120 int mode = TP_ACPI_MULTI_MODE_INVALID;
2121 int valid_modes = 0;
2122
2123 if (has_tablet_mode)
2124 *has_tablet_mode = 0;
2125
2126 switch (type) {
2127 case 1:
2128 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2129 TP_ACPI_MULTI_MODE_TABLET |
2130 TP_ACPI_MULTI_MODE_STAND_TENT;
2131 break;
2132 case 2:
2133 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2134 TP_ACPI_MULTI_MODE_FLAT |
2135 TP_ACPI_MULTI_MODE_TABLET |
2136 TP_ACPI_MULTI_MODE_STAND |
2137 TP_ACPI_MULTI_MODE_TENT;
2138 break;
2139 case 3:
2140 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2141 TP_ACPI_MULTI_MODE_FLAT;
2142 break;
2143 case 4:
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002144 case 5:
Benjamin Berg26befef2017-11-14 17:14:14 +01002145 /* In mode 4, FLAT is not specified as a valid mode. However,
2146 * it can be seen at least on the X1 Yoga 2nd Generation.
2147 */
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002148 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2149 TP_ACPI_MULTI_MODE_FLAT |
2150 TP_ACPI_MULTI_MODE_TABLET |
2151 TP_ACPI_MULTI_MODE_STAND |
2152 TP_ACPI_MULTI_MODE_TENT;
2153 break;
2154 default:
2155 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2156 type, value, TPACPI_MAIL);
2157 return 0;
2158 }
2159
2160 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2161 *has_tablet_mode = 1;
2162
2163 switch (value) {
2164 case 1:
2165 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2166 break;
2167 case 2:
2168 mode = TP_ACPI_MULTI_MODE_FLAT;
2169 break;
2170 case 3:
2171 mode = TP_ACPI_MULTI_MODE_TABLET;
2172 break;
2173 case 4:
2174 if (type == 1)
2175 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2176 else
2177 mode = TP_ACPI_MULTI_MODE_STAND;
2178 break;
2179 case 5:
2180 mode = TP_ACPI_MULTI_MODE_TENT;
2181 break;
2182 default:
2183 if (type == 5 && value == 0xffff) {
2184 pr_warn("Multi mode status is undetected, assuming laptop\n");
2185 return 0;
2186 }
2187 }
2188
2189 if (!(mode & valid_modes)) {
2190 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2191 value, type, TPACPI_MAIL);
2192 return 0;
2193 }
2194
2195 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2196}
2197
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002198static int hotkey_get_tablet_mode(int *status)
2199{
2200 int s;
2201
Lyudeb03f4d42016-11-11 15:15:03 -05002202 switch (tp_features.hotkey_tablet) {
2203 case TP_HOTKEY_TABLET_USES_MHKG:
2204 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2205 return -EIO;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002206
Lyudeb03f4d42016-11-11 15:15:03 -05002207 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2208 break;
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002209 case TP_HOTKEY_TABLET_USES_GMMS:
2210 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
Lyudeb03f4d42016-11-11 15:15:03 -05002211 return -EIO;
2212
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002213 *status = hotkey_gmms_get_tablet_mode(s, NULL);
Lyudeb03f4d42016-11-11 15:15:03 -05002214 break;
2215 default:
2216 break;
2217 }
2218
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002219 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002220}
2221
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002222/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002223 * Reads current event mask from firmware, and updates
2224 * hotkey_acpi_mask accordingly. Also resets any bits
2225 * from hotkey_user_mask that are unavailable to be
2226 * delivered (shadow requirement of the userspace ABI).
2227 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002228 * Call with hotkey_mutex held
2229 */
2230static int hotkey_mask_get(void)
2231{
2232 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002233 u32 m = 0;
2234
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002235 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002236 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002237
2238 hotkey_acpi_mask = m;
2239 } else {
2240 /* no mask support doesn't mean no event support... */
2241 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002242 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002243
2244 /* sync userspace-visible mask */
2245 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002246
2247 return 0;
2248}
2249
Jean Delvaredf6dd1b2015-04-27 09:45:06 +02002250static void hotkey_mask_warn_incomplete_mask(void)
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002251{
2252 /* log only what the user can fix... */
2253 const u32 wantedmask = hotkey_driver_mask &
2254 ~(hotkey_acpi_mask | hotkey_source_mask) &
2255 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2256
2257 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002258 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002259}
2260
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002261/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002262 * Set the firmware mask when supported
2263 *
2264 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2265 *
2266 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2267 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002268 * Call with hotkey_mutex held
2269 */
2270static int hotkey_mask_set(u32 mask)
2271{
2272 int i;
2273 int rc = 0;
2274
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002275 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002276
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002277 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002278 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002279 if (!acpi_evalf(hkey_handle,
2280 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002281 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002282 rc = -EIO;
2283 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002284 }
2285 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002286 }
2287
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002288 /*
2289 * We *must* make an inconditional call to hotkey_mask_get to
2290 * refresh hotkey_acpi_mask and update hotkey_user_mask
2291 *
2292 * Take the opportunity to also log when we cannot _enable_
2293 * a given event.
2294 */
2295 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002296 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002297 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002298 }
2299
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002300 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2301 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002302
2303 return rc;
2304}
2305
2306/*
2307 * Sets hotkey_user_mask and tries to set the firmware mask
2308 *
2309 * Call with hotkey_mutex held
2310 */
2311static int hotkey_user_mask_set(const u32 mask)
2312{
2313 int rc;
2314
2315 /* Give people a chance to notice they are doing something that
2316 * is bound to go boom on their users sooner or later */
2317 if (!tp_warned.hotkey_mask_ff &&
2318 (mask == 0xffff || mask == 0xffffff ||
2319 mask == 0xffffffff)) {
2320 tp_warned.hotkey_mask_ff = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002321 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2322 mask);
2323 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 -03002324 }
2325
2326 /* Try to enable what the user asked for, plus whatever we need.
2327 * this syncs everything but won't enable bits in hotkey_user_mask */
2328 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2329
2330 /* Enable the available bits in hotkey_user_mask */
2331 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2332
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002333 return rc;
2334}
2335
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002336/*
2337 * Sets the driver hotkey mask.
2338 *
2339 * Can be called even if the hotkey subdriver is inactive
2340 */
2341static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2342{
2343 int rc;
2344
2345 /* Do the right thing if hotkey_init has not been called yet */
2346 if (!tp_features.hotkey) {
2347 hotkey_driver_mask = mask;
2348 return 0;
2349 }
2350
2351 mutex_lock(&hotkey_mutex);
2352
2353 HOTKEY_CONFIG_CRITICAL_START
2354 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002355#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002356 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002357#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002358 HOTKEY_CONFIG_CRITICAL_END
2359
2360 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2361 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002362 hotkey_poll_setup(true);
2363
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002364 mutex_unlock(&hotkey_mutex);
2365
2366 return rc;
2367}
2368
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002369static int hotkey_status_get(int *status)
2370{
2371 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2372 return -EIO;
2373
2374 return 0;
2375}
2376
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002377static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002378{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002379 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002380 return -EIO;
2381
2382 return 0;
2383}
2384
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002385static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002386{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002387 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002388
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002389 if (tp_features.hotkey_tablet &&
2390 !hotkey_get_tablet_mode(&state)) {
2391 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002392
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002393 input_report_switch(tpacpi_inputdev,
2394 SW_TABLET_MODE, !!state);
2395 input_sync(tpacpi_inputdev);
2396
2397 mutex_unlock(&tpacpi_inputdev_send_mutex);
2398 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002399}
2400
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002401/* Do NOT call without validating scancode first */
2402static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002403{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002404 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002405
2406 if (keycode != KEY_RESERVED) {
2407 mutex_lock(&tpacpi_inputdev_send_mutex);
2408
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002409 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002410 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002411 input_sync(tpacpi_inputdev);
2412
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002413 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002414 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002415 input_sync(tpacpi_inputdev);
2416
2417 mutex_unlock(&tpacpi_inputdev_send_mutex);
2418 }
2419}
2420
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002421/* Do NOT call without validating scancode first */
2422static void tpacpi_input_send_key_masked(const unsigned int scancode)
2423{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002424 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002425 if (hotkey_user_mask & (1 << scancode))
2426 tpacpi_input_send_key(scancode);
2427}
2428
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002429#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2430static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2431
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002432/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002433static void tpacpi_hotkey_send_key(unsigned int scancode)
2434{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002435 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002436}
2437
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002438static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002439{
2440 u8 d;
2441
2442 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2443 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2444 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2445 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2446 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2447 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2448 }
Hans de Goedec685e202017-02-09 16:44:13 +01002449 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002450 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2451 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2452 }
2453 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2454 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2455 n->displayexp_toggle =
2456 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2457 }
2458 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2459 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2460 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2461 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2462 n->brightness_toggle =
2463 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2464 }
2465 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2466 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2467 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2468 >> TP_NVRAM_POS_LEVEL_VOLUME;
2469 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2470 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2471 }
2472}
2473
Behan Webstera4d44ba2014-02-12 21:58:46 +01002474#define TPACPI_COMPARE_KEY(__scancode, __member) \
2475do { \
2476 if ((event_mask & (1 << __scancode)) && \
2477 oldn->__member != newn->__member) \
2478 tpacpi_hotkey_send_key(__scancode); \
2479} while (0)
2480
2481#define TPACPI_MAY_SEND_KEY(__scancode) \
2482do { \
2483 if (event_mask & (1 << __scancode)) \
2484 tpacpi_hotkey_send_key(__scancode); \
2485} while (0)
2486
2487static void issue_volchange(const unsigned int oldvol,
2488 const unsigned int newvol,
2489 const u32 event_mask)
2490{
2491 unsigned int i = oldvol;
2492
2493 while (i > newvol) {
2494 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2495 i--;
2496 }
2497 while (i < newvol) {
2498 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2499 i++;
2500 }
2501}
2502
2503static void issue_brightnesschange(const unsigned int oldbrt,
2504 const unsigned int newbrt,
2505 const u32 event_mask)
2506{
2507 unsigned int i = oldbrt;
2508
2509 while (i > newbrt) {
2510 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2511 i--;
2512 }
2513 while (i < newbrt) {
2514 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2515 i++;
2516 }
2517}
2518
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002519static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2520 struct tp_nvram_state *newn,
2521 const u32 event_mask)
2522{
2523
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002524 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2525 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2526 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2527 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2528
2529 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2530
2531 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2532
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002533 /*
2534 * Handle volume
2535 *
2536 * This code is supposed to duplicate the IBM firmware behaviour:
2537 * - Pressing MUTE issues mute hotkey message, even when already mute
2538 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002539 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002540 * - The act of unmuting issues volume up/down notification,
2541 * depending which key was used to unmute
2542 *
2543 * We are constrained to what the NVRAM can tell us, which is not much
2544 * and certainly not enough if more than one volume hotkey was pressed
2545 * since the last poll cycle.
2546 *
2547 * Just to make our life interesting, some newer Lenovo ThinkPads have
2548 * bugs in the BIOS and may fail to update volume_toggle properly.
2549 */
2550 if (newn->mute) {
2551 /* muted */
2552 if (!oldn->mute ||
2553 oldn->volume_toggle != newn->volume_toggle ||
2554 oldn->volume_level != newn->volume_level) {
2555 /* recently muted, or repeated mute keypress, or
2556 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002557 issue_volchange(oldn->volume_level, newn->volume_level,
2558 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002559 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2560 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002561 } else {
2562 /* unmute */
2563 if (oldn->mute) {
2564 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002565 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002566 }
2567 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002568 issue_volchange(oldn->volume_level, newn->volume_level,
2569 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002570 } else if (oldn->volume_toggle != newn->volume_toggle) {
2571 /* repeated vol up/down keypress at end of scale ? */
2572 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002573 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002574 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2575 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002576 }
2577 }
2578
2579 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002580 if (oldn->brightness_level != newn->brightness_level) {
2581 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002582 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002583 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2584 /* repeated key presses that didn't change state */
2585 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002586 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002587 else if (newn->brightness_level >= bright_maxlvl
2588 && !tp_features.bright_unkfw)
2589 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002590 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002591
2592#undef TPACPI_COMPARE_KEY
2593#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002594}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002595
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002596/*
2597 * Polling driver
2598 *
2599 * We track all events in hotkey_source_mask all the time, since
2600 * most of them are edge-based. We only issue those requested by
2601 * hotkey_user_mask or hotkey_driver_mask, though.
2602 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002603static int hotkey_kthread(void *data)
2604{
2605 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002606 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002607 unsigned int si, so;
2608 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002609 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002610 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002611 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002612
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002613 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2614 goto exit;
2615
2616 set_freezable();
2617
2618 so = 0;
2619 si = 1;
2620 t = 0;
2621
2622 /* Initial state for compares */
2623 mutex_lock(&hotkey_thread_data_mutex);
2624 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002625 poll_mask = hotkey_source_mask;
2626 event_mask = hotkey_source_mask &
2627 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002628 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002629 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002630 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002631
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002632 while (!kthread_should_stop()) {
2633 if (t == 0) {
2634 if (likely(poll_freq))
2635 t = 1000/poll_freq;
2636 else
2637 t = 100; /* should never happen... */
2638 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002639 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002640 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002641 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002642
2643 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002644 continue;
2645
2646 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002647 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002648 /* forget old state on thaw or config change */
2649 si = so;
2650 t = 0;
2651 change_detector = hotkey_config_change;
2652 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002653 poll_mask = hotkey_source_mask;
2654 event_mask = hotkey_source_mask &
2655 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002656 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002657 mutex_unlock(&hotkey_thread_data_mutex);
2658
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002659 if (likely(poll_mask)) {
2660 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002661 if (likely(si != so)) {
2662 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002663 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002664 }
2665 }
2666
2667 so = si;
2668 si ^= 1;
2669 }
2670
2671exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002672 return 0;
2673}
2674
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002675/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002676static void hotkey_poll_stop_sync(void)
2677{
2678 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002679 kthread_stop(tpacpi_hotkey_task);
2680 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002681 }
2682}
2683
2684/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002685static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002686{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002687 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2688 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002689
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002690 if (hotkey_poll_freq > 0 &&
2691 (poll_driver_mask ||
2692 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002693 if (!tpacpi_hotkey_task) {
2694 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002695 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002696 if (IS_ERR(tpacpi_hotkey_task)) {
2697 tpacpi_hotkey_task = NULL;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002698 pr_err("could not create kernel thread for hotkey polling\n");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002699 }
2700 }
2701 } else {
2702 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002703 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002704 hotkey_poll_freq == 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002705 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 -07002706 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002707 }
2708 }
2709}
2710
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002711static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002712{
2713 mutex_lock(&hotkey_mutex);
2714 hotkey_poll_setup(may_warn);
2715 mutex_unlock(&hotkey_mutex);
2716}
2717
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002718/* call with hotkey_mutex held */
2719static void hotkey_poll_set_freq(unsigned int freq)
2720{
2721 if (!freq)
2722 hotkey_poll_stop_sync();
2723
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002724 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002725}
2726
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002727#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2728
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002729static void hotkey_poll_setup(const bool __unused)
2730{
2731}
2732
2733static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002734{
2735}
2736
2737#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2738
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002739static int hotkey_inputdev_open(struct input_dev *dev)
2740{
2741 switch (tpacpi_lifecycle) {
2742 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002743 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002744 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002745 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002746 case TPACPI_LIFE_EXITING:
2747 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002748 }
2749
2750 /* Should only happen if tpacpi_lifecycle is corrupt */
2751 BUG();
2752 return -EBUSY;
2753}
2754
2755static void hotkey_inputdev_close(struct input_dev *dev)
2756{
2757 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002758 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002759 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002760 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002761}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002762
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002763/* sysfs hotkey enable ------------------------------------------------- */
2764static ssize_t hotkey_enable_show(struct device *dev,
2765 struct device_attribute *attr,
2766 char *buf)
2767{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002768 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002769
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002770 printk_deprecated_attribute("hotkey_enable",
2771 "Hotkey reporting is always enabled");
2772
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002773 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002774 if (res)
2775 return res;
2776
2777 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2778}
2779
2780static ssize_t hotkey_enable_store(struct device *dev,
2781 struct device_attribute *attr,
2782 const char *buf, size_t count)
2783{
2784 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002785
2786 printk_deprecated_attribute("hotkey_enable",
2787 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002788
2789 if (parse_strtoul(buf, 1, &t))
2790 return -EINVAL;
2791
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002792 if (t == 0)
2793 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002794
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002795 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002796}
2797
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002798static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002799
2800/* sysfs hotkey mask --------------------------------------------------- */
2801static ssize_t hotkey_mask_show(struct device *dev,
2802 struct device_attribute *attr,
2803 char *buf)
2804{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002805 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002806}
2807
2808static ssize_t hotkey_mask_store(struct device *dev,
2809 struct device_attribute *attr,
2810 const char *buf, size_t count)
2811{
2812 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002813 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002814
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002815 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002816 return -EINVAL;
2817
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002818 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002819 return -ERESTARTSYS;
2820
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002821 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002822
2823#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002824 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002825#endif
2826
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002827 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002828
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002829 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2830
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002831 return (res) ? res : count;
2832}
2833
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002834static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002835
2836/* sysfs hotkey bios_enabled ------------------------------------------- */
2837static ssize_t hotkey_bios_enabled_show(struct device *dev,
2838 struct device_attribute *attr,
2839 char *buf)
2840{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002841 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002842}
2843
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002844static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002845
2846/* sysfs hotkey bios_mask ---------------------------------------------- */
2847static ssize_t hotkey_bios_mask_show(struct device *dev,
2848 struct device_attribute *attr,
2849 char *buf)
2850{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002851 printk_deprecated_attribute("hotkey_bios_mask",
2852 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002853 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002854}
2855
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002856static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002857
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002858/* sysfs hotkey all_mask ----------------------------------------------- */
2859static ssize_t hotkey_all_mask_show(struct device *dev,
2860 struct device_attribute *attr,
2861 char *buf)
2862{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002863 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2864 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002865}
2866
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002867static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002868
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002869/* sysfs hotkey all_mask ----------------------------------------------- */
2870static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2871 struct device_attribute *attr,
2872 char *buf)
2873{
2874 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2875 hotkey_adaptive_all_mask | hotkey_source_mask);
2876}
2877
2878static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2879
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002880/* sysfs hotkey recommended_mask --------------------------------------- */
2881static ssize_t hotkey_recommended_mask_show(struct device *dev,
2882 struct device_attribute *attr,
2883 char *buf)
2884{
2885 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002886 (hotkey_all_mask | hotkey_source_mask)
2887 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002888}
2889
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002890static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002891
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002892#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2893
2894/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2895static ssize_t hotkey_source_mask_show(struct device *dev,
2896 struct device_attribute *attr,
2897 char *buf)
2898{
2899 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2900}
2901
2902static ssize_t hotkey_source_mask_store(struct device *dev,
2903 struct device_attribute *attr,
2904 const char *buf, size_t count)
2905{
2906 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002907 u32 r_ev;
2908 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002909
2910 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2911 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2912 return -EINVAL;
2913
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002914 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002915 return -ERESTARTSYS;
2916
2917 HOTKEY_CONFIG_CRITICAL_START
2918 hotkey_source_mask = t;
2919 HOTKEY_CONFIG_CRITICAL_END
2920
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002921 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2922 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002923 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002924
2925 /* check if events needed by the driver got disabled */
2926 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2927 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002928
2929 mutex_unlock(&hotkey_mutex);
2930
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002931 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002932 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002933
2934 if (r_ev)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002935 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002936 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002937
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002938 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2939
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002940 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002941}
2942
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002943static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002944
2945/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2946static ssize_t hotkey_poll_freq_show(struct device *dev,
2947 struct device_attribute *attr,
2948 char *buf)
2949{
2950 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2951}
2952
2953static ssize_t hotkey_poll_freq_store(struct device *dev,
2954 struct device_attribute *attr,
2955 const char *buf, size_t count)
2956{
2957 unsigned long t;
2958
2959 if (parse_strtoul(buf, 25, &t))
2960 return -EINVAL;
2961
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002962 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002963 return -ERESTARTSYS;
2964
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002965 hotkey_poll_set_freq(t);
2966 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002967
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002968 mutex_unlock(&hotkey_mutex);
2969
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002970 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2971
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002972 return count;
2973}
2974
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002975static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002976
2977#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2978
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002979/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002980static ssize_t hotkey_radio_sw_show(struct device *dev,
2981 struct device_attribute *attr,
2982 char *buf)
2983{
Johannes Berg19d337d2009-06-02 13:01:37 +02002984 int res;
2985 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002986 if (res < 0)
2987 return res;
2988
Johannes Berg19d337d2009-06-02 13:01:37 +02002989 /* Opportunistic update */
2990 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2991
2992 return snprintf(buf, PAGE_SIZE, "%d\n",
2993 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002994}
2995
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002996static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002997
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002998static void hotkey_radio_sw_notify_change(void)
2999{
3000 if (tp_features.hotkey_wlsw)
3001 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3002 "hotkey_radio_sw");
3003}
3004
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003005/* sysfs hotkey tablet mode (pollable) --------------------------------- */
3006static ssize_t hotkey_tablet_mode_show(struct device *dev,
3007 struct device_attribute *attr,
3008 char *buf)
3009{
3010 int res, s;
3011 res = hotkey_get_tablet_mode(&s);
3012 if (res < 0)
3013 return res;
3014
3015 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
3016}
3017
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01003018static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003019
3020static void hotkey_tablet_mode_notify_change(void)
3021{
3022 if (tp_features.hotkey_tablet)
3023 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3024 "hotkey_tablet_mode");
3025}
3026
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003027/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003028static ssize_t hotkey_wakeup_reason_show(struct device *dev,
3029 struct device_attribute *attr,
3030 char *buf)
3031{
3032 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
3033}
3034
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003035static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003036
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02003037static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003038{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003039 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3040 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003041}
3042
3043/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003044static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
3045 struct device_attribute *attr,
3046 char *buf)
3047{
3048 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
3049}
3050
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003051static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
3052 hotkey_wakeup_hotunplug_complete_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003053
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02003054static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003055{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003056 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3057 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003058}
3059
Bastien Nocerab790cee2015-03-02 14:45:27 +01003060/* sysfs adaptive kbd mode --------------------------------------------- */
3061
3062static int adaptive_keyboard_get_mode(void);
3063static int adaptive_keyboard_set_mode(int new_mode);
3064
3065enum ADAPTIVE_KEY_MODE {
3066 HOME_MODE,
3067 WEB_BROWSER_MODE,
3068 WEB_CONFERENCE_MODE,
3069 FUNCTION_MODE,
3070 LAYFLAT_MODE
3071};
3072
3073static ssize_t adaptive_kbd_mode_show(struct device *dev,
3074 struct device_attribute *attr,
3075 char *buf)
3076{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003077 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01003078
3079 current_mode = adaptive_keyboard_get_mode();
3080 if (current_mode < 0)
3081 return current_mode;
3082
3083 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
3084}
3085
3086static ssize_t adaptive_kbd_mode_store(struct device *dev,
3087 struct device_attribute *attr,
3088 const char *buf, size_t count)
3089{
3090 unsigned long t;
3091 int res;
3092
3093 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
3094 return -EINVAL;
3095
3096 res = adaptive_keyboard_set_mode(t);
3097 return (res < 0) ? res : count;
3098}
3099
3100static DEVICE_ATTR_RW(adaptive_kbd_mode);
3101
3102static struct attribute *adaptive_kbd_attributes[] = {
3103 &dev_attr_adaptive_kbd_mode.attr,
3104 NULL
3105};
3106
3107static const struct attribute_group adaptive_kbd_attr_group = {
3108 .attrs = adaptive_kbd_attributes,
3109};
3110
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003111/* --------------------------------------------------------------------- */
3112
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003113static struct attribute *hotkey_attributes[] __initdata = {
3114 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003115 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003116 &dev_attr_hotkey_bios_mask.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003117 &dev_attr_wakeup_reason.attr,
3118 &dev_attr_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003119 &dev_attr_hotkey_mask.attr,
3120 &dev_attr_hotkey_all_mask.attr,
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003121 &dev_attr_hotkey_adaptive_all_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003122 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003123#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003124 &dev_attr_hotkey_source_mask.attr,
3125 &dev_attr_hotkey_poll_freq.attr,
3126#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003127};
3128
Johannes Berg19d337d2009-06-02 13:01:37 +02003129/*
3130 * Sync both the hw and sw blocking state of all switches
3131 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003132static void tpacpi_send_radiosw_update(void)
3133{
3134 int wlsw;
3135
Johannes Berg19d337d2009-06-02 13:01:37 +02003136 /*
3137 * We must sync all rfkill controllers *before* issuing any
3138 * rfkill input events, or we will race the rfkill core input
3139 * handler.
3140 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003141 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003142 * for the above.
3143 *
3144 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3145 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003146
Johannes Berg19d337d2009-06-02 13:01:37 +02003147 wlsw = hotkey_get_wlsw();
3148
3149 /* Sync hw blocking state first if it is hw-blocked */
3150 if (wlsw == TPACPI_RFK_RADIO_OFF)
3151 tpacpi_rfk_update_hwblock_state(true);
3152
3153 /* Sync sw blocking state */
3154 tpacpi_rfk_update_swstate_all();
3155
3156 /* Sync hw blocking state last if it is hw-unblocked */
3157 if (wlsw == TPACPI_RFK_RADIO_ON)
3158 tpacpi_rfk_update_hwblock_state(false);
3159
3160 /* Issue rfkill input event for WLSW switch */
3161 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003162 mutex_lock(&tpacpi_inputdev_send_mutex);
3163
3164 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003165 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003166 input_sync(tpacpi_inputdev);
3167
3168 mutex_unlock(&tpacpi_inputdev_send_mutex);
3169 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003170
3171 /*
3172 * this can be unconditional, as we will poll state again
3173 * if userspace uses the notify to read data
3174 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003175 hotkey_radio_sw_notify_change();
3176}
3177
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003178static void hotkey_exit(void)
3179{
3180#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003181 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003182 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003183 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003184#endif
3185
3186 if (hotkey_dev_attributes)
3187 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3188
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003189 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003190 "restoring original HKEY status and mask\n");
3191 /* yes, there is a bitwise or below, we want the
3192 * functions to be called even if one of them fail */
3193 if (((tp_features.hotkey_mask &&
3194 hotkey_mask_set(hotkey_orig_mask)) |
3195 hotkey_status_set(false)) != 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003196 pr_err("failed to restore hot key mask to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003197}
3198
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003199static void __init hotkey_unmap(const unsigned int scancode)
3200{
3201 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3202 clear_bit(hotkey_keycode_map[scancode],
3203 tpacpi_inputdev->keybit);
3204 hotkey_keycode_map[scancode] = KEY_RESERVED;
3205 }
3206}
3207
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003208/*
3209 * HKEY quirks:
3210 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3211 */
3212
3213#define TPACPI_HK_Q_INIMASK 0x0001
3214
3215static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3216 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3217 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3218 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3219 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3220 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3221 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3222 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3223 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3224 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3225 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3226 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3227 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3228 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3229 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3230 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3231 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3232 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3233 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3234 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3235};
3236
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003237typedef u16 tpacpi_keymap_entry_t;
3238typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003239
Lyudeb3180022016-11-11 15:15:02 -05003240static int hotkey_init_tablet_mode(void)
3241{
Darren Hartcb2bf252016-12-14 14:45:48 -08003242 int in_tablet_mode = 0, res;
3243 char *type = NULL;
Lyudeb3180022016-11-11 15:15:02 -05003244
Benjamin Bergdda3ec02017-09-15 15:20:49 +02003245 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3246 int has_tablet_mode;
3247
3248 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3249 &has_tablet_mode);
3250 if (has_tablet_mode)
3251 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3252 type = "GMMS";
3253 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
Lyudeb03f4d42016-11-11 15:15:03 -05003254 /* For X41t, X60t, X61t Tablets... */
Lyudeb3180022016-11-11 15:15:02 -05003255 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3256 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3257 type = "MHKG";
3258 }
3259
3260 if (!tp_features.hotkey_tablet)
3261 return 0;
3262
3263 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3264 type, in_tablet_mode ? "tablet" : "laptop");
3265
3266 res = add_to_attr_set(hotkey_dev_attributes,
3267 &dev_attr_hotkey_tablet_mode.attr);
3268 if (res)
3269 return -1;
3270
3271 return in_tablet_mode;
3272}
3273
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003274static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003275{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003276 /* Requirements for changing the default keymaps:
3277 *
3278 * 1. Many of the keys are mapped to KEY_RESERVED for very
3279 * good reasons. Do not change them unless you have deep
3280 * knowledge on the IBM and Lenovo ThinkPad firmware for
3281 * the various ThinkPad models. The driver behaves
3282 * differently for KEY_RESERVED: such keys have their
3283 * hot key mask *unset* in mask_recommended, and also
3284 * in the initial hot key mask programmed into the
3285 * firmware at driver load time, which means the firm-
3286 * ware may react very differently if you change them to
3287 * something else;
3288 *
3289 * 2. You must be subscribed to the linux-thinkpad and
3290 * ibm-acpi-devel mailing lists, and you should read the
3291 * list archives since 2007 if you want to change the
3292 * keymaps. This requirement exists so that you will
3293 * know the past history of problems with the thinkpad-
3294 * acpi driver keymaps, and also that you will be
3295 * listening to any bug reports;
3296 *
3297 * 3. Do not send thinkpad-acpi specific patches directly to
3298 * for merging, *ever*. Send them to the linux-acpi
3299 * mailinglist for comments. Merging is to be done only
3300 * through acpi-test and the ACPI maintainer.
3301 *
3302 * If the above is too much to ask, don't change the keymap.
3303 * Ask the thinkpad-acpi maintainer to do it, instead.
3304 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003305
3306 enum keymap_index {
3307 TPACPI_KEYMAP_IBM_GENERIC = 0,
3308 TPACPI_KEYMAP_LENOVO_GENERIC,
3309 };
3310
3311 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3312 /* Generic keymap for IBM ThinkPads */
3313 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003314 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003315 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003316 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3317 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003318
3319 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003320 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3321 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3322 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003323
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003324 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003325 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003326 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003327
3328 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003329 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003330
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003331 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3332 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003333
3334 /* Volume: firmware always react to it and reprograms
3335 * the built-in *extra* mixer. Never map it to control
3336 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003337 KEY_RESERVED, /* 0x14: VOLUME UP */
3338 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3339 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003340
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003341 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003342
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003343 /* (assignments unknown, please report if found) */
3344 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3345 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003346
3347 /* No assignments, only used for Adaptive keyboards. */
3348 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3349 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3350 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3351 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3352 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Christian Kellner696c65232017-02-28 17:10:57 +01003353
3354 /* No assignment, used for newer Lenovo models */
3355 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3356 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3357 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3358 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3359 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3360 KEY_UNKNOWN, KEY_UNKNOWN
3361
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003362 },
3363
3364 /* Generic keymap for Lenovo ThinkPads */
3365 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003366 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3367 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003368 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003369 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003370
3371 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003372 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3373 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3374 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003375
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003376 /* These should be enabled --only-- when ACPI video
3377 * is disabled (i.e. in "vendor" mode), and are handled
3378 * in a special way by the init code */
3379 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3380 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003381
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003382 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003383
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003384 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3385 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003386
3387 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3388 * react to it and reprograms the built-in *extra* mixer.
3389 * Never map it to control another mixer by default.
3390 *
3391 * T60?, T61, R60?, R61: firmware and EC tries to send
3392 * these over the regular keyboard, so these are no-ops,
3393 * but there are still weird bugs re. MUTE, so do not
3394 * change unless you get test reports from all Lenovo
3395 * models. May cause the BIOS to interfere with the
3396 * HDA mixer.
3397 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003398 KEY_RESERVED, /* 0x14: VOLUME UP */
3399 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3400 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003401
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003402 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003403
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003404 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003405 KEY_UNKNOWN, KEY_UNKNOWN,
3406
3407 /*
3408 * The mic mute button only sends 0x1a. It does not
3409 * automatically mute the mic or change the mute light.
3410 */
3411 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3412
3413 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003414 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003415
3416 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003417 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003418
3419 /*
3420 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3421 * The first item in this list is the Mute button which is
3422 * emitted with 0x103 through
3423 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3424 * symbol is held.
3425 * We'll need to offset those by 0x20.
3426 */
3427 KEY_RESERVED, /* Mute held, 0x103 */
3428 KEY_BRIGHTNESS_MIN, /* Backlight off */
3429 KEY_RESERVED, /* Clipping tool */
3430 KEY_RESERVED, /* Cloud */
3431 KEY_RESERVED,
3432 KEY_VOICECOMMAND, /* Voice */
3433 KEY_RESERVED,
3434 KEY_RESERVED, /* Gestures */
3435 KEY_RESERVED,
3436 KEY_RESERVED,
3437 KEY_RESERVED,
3438 KEY_CONFIG, /* Settings */
3439 KEY_RESERVED, /* New tab */
3440 KEY_REFRESH, /* Reload */
3441 KEY_BACK, /* Back */
3442 KEY_RESERVED, /* Microphone down */
3443 KEY_RESERVED, /* Microphone up */
3444 KEY_RESERVED, /* Microphone cancellation */
3445 KEY_RESERVED, /* Camera mode */
3446 KEY_RESERVED, /* Rotate display, 0x116 */
Christian Kellner696c65232017-02-28 17:10:57 +01003447
3448 /*
3449 * These are found in 2017 models (e.g. T470s, X270).
3450 * The lowest known value is 0x311, which according to
3451 * the manual should launch a user defined favorite
3452 * application.
3453 *
3454 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3455 * corresponding to 0x34.
3456 */
3457
3458 /* (assignments unknown, please report if found) */
3459 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3460 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3461 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3462 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3463 KEY_UNKNOWN,
3464
Zhang Xianwei8055db32018-10-11 13:59:31 +08003465 KEY_BOOKMARKS, /* Favorite app, 0x311 */
Christian Kellner696c65232017-02-28 17:10:57 +01003466 KEY_RESERVED, /* Clipping tool */
Benjamin Bergcb5c1972018-06-20 15:49:27 +02003467 KEY_CALC, /* Calculator (above numpad, P52) */
Christian Kellner696c65232017-02-28 17:10:57 +01003468 KEY_BLUETOOTH, /* Bluetooth */
3469 KEY_KEYBOARD /* Keyboard, 0x315 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003470 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003471 };
3472
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003473 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3474 /* Generic maps (fallback) */
3475 {
3476 .vendor = PCI_VENDOR_ID_IBM,
3477 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3478 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3479 },
3480 {
3481 .vendor = PCI_VENDOR_ID_LENOVO,
3482 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3483 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3484 },
3485 };
3486
3487#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003488#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003489
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003490 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003491 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003492 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003493 bool radiosw_state = false;
3494 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003495
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003496 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003497 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003498
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003499 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3500 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003501
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003502 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003503 BUG_ON(tpacpi_inputdev->open != NULL ||
3504 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003505
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003506 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003507 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003508
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003509#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003510 mutex_init(&hotkey_thread_data_mutex);
3511#endif
3512
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003513 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003514 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003515
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003516 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3517 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003518 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003519
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003520 if (!tp_features.hotkey)
3521 return 1;
3522
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003523 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3524 ARRAY_SIZE(tpacpi_hotkey_qtable));
3525
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003526 tpacpi_disable_brightness_delay();
3527
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003528 /* MUST have enough space for all attributes to be added to
3529 * hotkey_dev_attributes */
3530 hotkey_dev_attributes = create_attr_set(
3531 ARRAY_SIZE(hotkey_attributes) + 2,
3532 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003533 if (!hotkey_dev_attributes)
3534 return -ENOMEM;
3535 res = add_many_to_attr_set(hotkey_dev_attributes,
3536 hotkey_attributes,
3537 ARRAY_SIZE(hotkey_attributes));
3538 if (res)
3539 goto err_exit;
3540
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003541 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003542 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3543 for HKEY interface version 0x100 */
3544 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003545 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3546 "firmware HKEY interface version: 0x%x\n",
3547 hkeyv);
3548
3549 switch (hkeyv >> 8) {
3550 case 1:
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003551 /*
3552 * MHKV 0x100 in A31, R40, R40e,
3553 * T4x, X31, and later
3554 */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003555
3556 /* Paranoia check AND init hotkey_all_mask */
3557 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3558 "MHKA", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003559 pr_err("missing MHKA handler, please report this to %s\n",
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003560 TPACPI_MAIL);
3561 /* Fallback: pre-init for FN+F3,F4,F12 */
3562 hotkey_all_mask = 0x080cU;
3563 } else {
3564 tp_features.hotkey_mask = 1;
3565 }
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003566 break;
3567
3568 case 2:
3569 /*
3570 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3571 */
3572
3573 /* Paranoia check AND init hotkey_all_mask */
3574 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3575 "MHKA", "dd", 1)) {
3576 pr_err("missing MHKA handler, please report this to %s\n",
3577 TPACPI_MAIL);
3578 /* Fallback: pre-init for FN+F3,F4,F12 */
3579 hotkey_all_mask = 0x080cU;
3580 } else {
3581 tp_features.hotkey_mask = 1;
3582 }
3583
3584 /*
3585 * Check if we have an adaptive keyboard, like on the
3586 * Lenovo Carbon X1 2014 (2nd Gen).
3587 */
3588 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3589 "MHKA", "dd", 2)) {
3590 if (hotkey_adaptive_all_mask != 0) {
3591 tp_features.has_adaptive_kbd = true;
3592 res = sysfs_create_group(
3593 &tpacpi_pdev->dev.kobj,
3594 &adaptive_kbd_attr_group);
3595 if (res)
3596 goto err_exit;
3597 }
3598 } else {
3599 tp_features.has_adaptive_kbd = false;
3600 hotkey_adaptive_all_mask = 0x0U;
3601 }
3602 break;
3603
3604 default:
3605 pr_err("unknown version of the HKEY interface: 0x%x\n",
3606 hkeyv);
3607 pr_err("please report this to %s\n", TPACPI_MAIL);
3608 break;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003609 }
3610 }
3611
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003612 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3613 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003614 str_supported(tp_features.hotkey_mask));
3615
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003616 /* Init hotkey_all_mask if not initialized yet */
3617 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3618 (quirks & TPACPI_HK_Q_INIMASK))
3619 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003620
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003621 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003622 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003623 /* hotkey_source_mask *must* be zero for
3624 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003625 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003626 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003627 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003628
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003629 hotkey_orig_mask = hotkey_acpi_mask;
3630 } else {
3631 hotkey_orig_mask = hotkey_all_mask;
3632 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003633 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003634
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003635#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3636 if (dbg_wlswemul) {
3637 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003638 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003639 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003640 } else
3641#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003642 /* Not all thinkpads have a hardware radio switch */
3643 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3644 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003645 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003646 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003647 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003648 }
3649 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003650 res = add_to_attr_set(hotkey_dev_attributes,
3651 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003652
Lyudeb3180022016-11-11 15:15:02 -05003653 res = hotkey_init_tablet_mode();
3654 if (res < 0)
3655 goto err_exit;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003656
Lyudeb3180022016-11-11 15:15:02 -05003657 tabletsw_state = res;
3658
3659 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3660 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003661 if (res)
3662 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003663
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003664 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003665 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3666 GFP_KERNEL);
3667 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003668 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003669 res = -ENOMEM;
3670 goto err_exit;
3671 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003672
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003673 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3674 ARRAY_SIZE(tpacpi_keymap_qtable));
3675 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3676 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3677 "using keymap number %lu\n", keymap_id);
3678
3679 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3680 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003681
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003682 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003683 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3684 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3685 tpacpi_inputdev->keycode = hotkey_keycode_map;
3686 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3687 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003688 input_set_capability(tpacpi_inputdev, EV_KEY,
3689 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003690 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003691 if (i < sizeof(hotkey_reserved_mask)*8)
3692 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003693 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003694 }
3695
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003696 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003697 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003698 input_report_switch(tpacpi_inputdev,
3699 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003700 }
3701 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003702 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003703 input_report_switch(tpacpi_inputdev,
3704 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003705 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003706
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003707 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003708 * userspace. tpacpi_detect_brightness_capabilities() must have
3709 * been called before this point */
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02003710 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003711 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3712 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003713
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003714 /* Disable brightness up/down on Lenovo thinkpads when
3715 * ACPI is handling them, otherwise it is plain impossible
3716 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003717 hotkey_reserved_mask |=
3718 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3719 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003720 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3721 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003722 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003723
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003724#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003725 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3726 & ~hotkey_all_mask
3727 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003728
3729 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3730 "hotkey source mask 0x%08x, polling freq %u\n",
3731 hotkey_source_mask, hotkey_poll_freq);
3732#endif
3733
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003734 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3735 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003736 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003737 if (res) {
3738 hotkey_exit();
3739 return res;
3740 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003741 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3742 | hotkey_driver_mask)
3743 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003744 if (res < 0 && res != -ENXIO) {
3745 hotkey_exit();
3746 return res;
3747 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003748 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3749 & ~hotkey_reserved_mask;
3750 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3751 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3752 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003753
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003754 tpacpi_inputdev->open = &hotkey_inputdev_open;
3755 tpacpi_inputdev->close = &hotkey_inputdev_close;
3756
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003757 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003758
3759 return 0;
3760
3761err_exit:
3762 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
Bastien Nocerab790cee2015-03-02 14:45:27 +01003763 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3764 &adaptive_kbd_attr_group);
3765
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003766 hotkey_dev_attributes = NULL;
3767
Jan van den Berg72a979f2014-09-17 00:01:08 +02003768 return (res < 0) ? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003769}
3770
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003771/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3772 * mode, Web conference mode, Function mode and Lay-flat mode.
3773 * We support Home mode and Function mode currently.
3774 *
3775 * Will consider support rest of modes in future.
3776 *
3777 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003778static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003779 HOME_MODE,
3780/* WEB_BROWSER_MODE = 2,
3781 WEB_CONFERENCE_MODE = 3, */
3782 FUNCTION_MODE
3783};
3784
3785#define DFR_CHANGE_ROW 0x101
3786#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003787#define FIRST_ADAPTIVE_KEY 0x103
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003788
3789/* press Fn key a while second, it will switch to Function Mode. Then
3790 * release Fn key, previous mode be restored.
3791 */
3792static bool adaptive_keyboard_mode_is_saved;
3793static int adaptive_keyboard_prev_mode;
3794
Bastien Noceraf74587f2015-03-02 14:45:22 +01003795static int adaptive_keyboard_get_mode(void)
3796{
3797 int mode = 0;
3798
3799 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3800 pr_err("Cannot read adaptive keyboard mode\n");
3801 return -EIO;
3802 }
3803
3804 return mode;
3805}
3806
3807static int adaptive_keyboard_set_mode(int new_mode)
3808{
3809 if (new_mode < 0 ||
3810 new_mode > LAYFLAT_MODE)
3811 return -EINVAL;
3812
3813 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3814 pr_err("Cannot set adaptive keyboard mode\n");
3815 return -EIO;
3816 }
3817
3818 return 0;
3819}
3820
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003821static int adaptive_keyboard_get_next_mode(int mode)
3822{
3823 size_t i;
3824 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3825
3826 for (i = 0; i <= max_mode; i++) {
3827 if (adaptive_keyboard_modes[i] == mode)
3828 break;
3829 }
3830
3831 if (i >= max_mode)
3832 i = 0;
3833 else
3834 i++;
3835
3836 return adaptive_keyboard_modes[i];
3837}
3838
3839static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3840{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003841 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003842 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003843 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003844
3845 switch (scancode) {
3846 case DFR_CHANGE_ROW:
3847 if (adaptive_keyboard_mode_is_saved) {
3848 new_mode = adaptive_keyboard_prev_mode;
3849 adaptive_keyboard_mode_is_saved = false;
3850 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003851 current_mode = adaptive_keyboard_get_mode();
3852 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003853 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003854 new_mode = adaptive_keyboard_get_next_mode(
3855 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003856 }
3857
Bastien Noceraf74587f2015-03-02 14:45:22 +01003858 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003859 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003860
3861 return true;
3862
3863 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003864 current_mode = adaptive_keyboard_get_mode();
3865 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003866 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003867
Bastien Noceraf74587f2015-03-02 14:45:22 +01003868 adaptive_keyboard_prev_mode = current_mode;
3869 adaptive_keyboard_mode_is_saved = true;
3870
3871 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3872 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003873 return true;
3874
3875 default:
Dan Carpenter741d98c2015-03-11 12:36:07 +03003876 if (scancode < FIRST_ADAPTIVE_KEY ||
Christian Kellner696c65232017-02-28 17:10:57 +01003877 scancode >= FIRST_ADAPTIVE_KEY +
3878 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3879 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
Bastien Nocera6a68d852015-03-02 14:45:31 +01003880 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003881 scancode);
Bastien Nocera6a68d852015-03-02 14:45:31 +01003882 return false;
3883 }
Christian Kellner149c8c72017-02-28 17:10:56 +01003884 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3885 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
Bastien Nocera6a68d852015-03-02 14:45:31 +01003886 if (keycode != KEY_RESERVED) {
3887 mutex_lock(&tpacpi_inputdev_send_mutex);
3888
3889 input_report_key(tpacpi_inputdev, keycode, 1);
3890 input_sync(tpacpi_inputdev);
3891
3892 input_report_key(tpacpi_inputdev, keycode, 0);
3893 input_sync(tpacpi_inputdev);
3894
3895 mutex_unlock(&tpacpi_inputdev_send_mutex);
3896 }
3897 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003898 }
3899}
3900
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003901static bool hotkey_notify_hotkey(const u32 hkey,
3902 bool *send_acpi_ev,
3903 bool *ignore_acpi_ev)
3904{
3905 /* 0x1000-0x1FFF: key presses */
3906 unsigned int scancode = hkey & 0xfff;
3907 *send_acpi_ev = true;
3908 *ignore_acpi_ev = false;
3909
Christian Kellner696c65232017-02-28 17:10:57 +01003910 /*
3911 * Original events are in the 0x10XX range, the adaptive keyboard
3912 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3913 * models, additional keys are emitted through 0x13XX.
3914 */
3915 switch ((hkey >> 8) & 0xf) {
3916 case 0:
3917 if (scancode > 0 &&
3918 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3919 /* HKEY event 0x1001 is scancode 0x00 */
3920 scancode--;
3921 if (!(hotkey_source_mask & (1 << scancode))) {
3922 tpacpi_input_send_key_masked(scancode);
3923 *send_acpi_ev = false;
3924 } else {
3925 *ignore_acpi_ev = true;
3926 }
3927 return true;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003928 }
Christian Kellner696c65232017-02-28 17:10:57 +01003929 break;
3930
3931 case 1:
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003932 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Christian Kellner696c65232017-02-28 17:10:57 +01003933
3934 case 3:
3935 /* Extended keycodes start at 0x300 and our offset into the map
3936 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3937 * will be positive, but might not be in the correct range.
3938 */
3939 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3940 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3941 scancode < TPACPI_HOTKEY_MAP_LEN) {
3942 tpacpi_input_send_key(scancode);
3943 return true;
3944 }
3945 break;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003946 }
Christian Kellner696c65232017-02-28 17:10:57 +01003947
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003948 return false;
3949}
3950
3951static bool hotkey_notify_wakeup(const u32 hkey,
3952 bool *send_acpi_ev,
3953 bool *ignore_acpi_ev)
3954{
3955 /* 0x2000-0x2FFF: Wakeup reason */
3956 *send_acpi_ev = true;
3957 *ignore_acpi_ev = false;
3958
3959 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003960 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3961 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003962 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3963 *ignore_acpi_ev = true;
3964 break;
3965
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003966 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3967 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003968 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3969 *ignore_acpi_ev = true;
3970 break;
3971
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003972 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3973 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003974 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003975 /* how to auto-heal: */
3976 /* 2313: woke up from S3, go to S4/S5 */
3977 /* 2413: woke up from S4, go to S5 */
3978 break;
3979
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003980 default:
3981 return false;
3982 }
3983
3984 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003985 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003986 hotkey_wakeup_reason_notify_change();
3987 }
3988 return true;
3989}
3990
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003991static bool hotkey_notify_dockevent(const u32 hkey,
3992 bool *send_acpi_ev,
3993 bool *ignore_acpi_ev)
3994{
3995 /* 0x4000-0x4FFF: dock-related events */
3996 *send_acpi_ev = true;
3997 *ignore_acpi_ev = false;
3998
3999 switch (hkey) {
4000 case TP_HKEY_EV_UNDOCK_ACK:
4001 /* ACPI undock operation completed after wakeup */
4002 hotkey_autosleep_ack = 1;
4003 pr_info("undocked\n");
4004 hotkey_wakeup_hotunplug_complete_notify_change();
4005 return true;
4006
4007 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
4008 pr_info("docked into hotplug port replicator\n");
4009 return true;
4010 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
4011 pr_info("undocked from hotplug port replicator\n");
4012 return true;
4013
4014 default:
4015 return false;
4016 }
4017}
4018
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004019static bool hotkey_notify_usrevent(const u32 hkey,
4020 bool *send_acpi_ev,
4021 bool *ignore_acpi_ev)
4022{
4023 /* 0x5000-0x5FFF: human interface helpers */
4024 *send_acpi_ev = true;
4025 *ignore_acpi_ev = false;
4026
4027 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004028 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
4029 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004030 return true;
4031
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004032 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
4033 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004034 tpacpi_input_send_tabletsw();
4035 hotkey_tablet_mode_notify_change();
4036 *send_acpi_ev = false;
4037 return true;
4038
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004039 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
4040 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
4041 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03004042 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004043 *ignore_acpi_ev = true;
4044 return true;
4045
4046 default:
4047 return false;
4048 }
4049}
4050
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004051static void thermal_dump_all_sensors(void);
4052
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004053static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004054 bool *send_acpi_ev,
4055 bool *ignore_acpi_ev)
4056{
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004057 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004058 *send_acpi_ev = true;
4059 *ignore_acpi_ev = false;
4060
4061 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004062 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh6e6bc5f62018-04-24 16:56:03 -03004063 pr_debug("EC reports: Thermal Table has changed\n");
4064 /* recommended action: do nothing, we don't have
4065 * Lenovo ATM information */
4066 return true;
4067 case TP_HKEY_EV_THM_CSM_COMPLETED:
4068 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
4069 /* recommended action: do nothing, we don't have
4070 * Lenovo ATM information */
4071 return true;
4072 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
4073 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02004074 /* recommended action: do nothing, we don't have
4075 * Lenovo ATM information */
4076 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004077 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07004078 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004079 /* recommended action: warn user through gui */
4080 break;
4081 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07004082 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004083 /* recommended action: immediate sleep/hibernate */
4084 break;
4085 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004086 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004087 /* recommended action: warn user through gui, that */
4088 /* some internal component is too hot */
4089 break;
4090 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004091 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004092 /* recommended action: immediate sleep/hibernate */
4093 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01004094 case TP_HKEY_EV_AC_CHANGED:
4095 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4096 * AC status changed; can be triggered by plugging or
4097 * unplugging AC adapter, docking or undocking. */
4098
4099 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004100
4101 case TP_HKEY_EV_KEY_NUMLOCK:
4102 case TP_HKEY_EV_KEY_FN:
Xavier Naveira67ab6242015-02-10 08:45:18 +01004103 case TP_HKEY_EV_KEY_FN_ESC:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004104 /* key press events, we just ignore them as long as the EC
4105 * is still reporting them in the normal keyboard stream */
4106 *send_acpi_ev = false;
4107 *ignore_acpi_ev = true;
4108 return true;
4109
Lyudeb03f4d42016-11-11 15:15:03 -05004110 case TP_HKEY_EV_TABLET_CHANGED:
4111 tpacpi_input_send_tabletsw();
4112 hotkey_tablet_mode_notify_change();
4113 *send_acpi_ev = false;
Henrique de Moraes Holschuhedd1ed72018-04-24 16:56:04 -03004114 return true;
Lyudeb03f4d42016-11-11 15:15:03 -05004115
David Herrmann587d8622018-01-12 12:04:45 +01004116 case TP_HKEY_EV_PALM_DETECTED:
4117 case TP_HKEY_EV_PALM_UNDETECTED:
4118 /* palm detected hovering the keyboard, forward to user-space
4119 * via netlink for consumption */
4120 return true;
4121
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004122 default:
Henrique de Moraes Holschuhfd75ba22018-04-24 16:56:05 -03004123 /* report simply as unknown, no sensor dump */
4124 return false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004125 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004126
4127 thermal_dump_all_sensors();
Henrique de Moraes Holschuhfd75ba22018-04-24 16:56:05 -03004128 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004129}
4130
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004131static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4132{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004133 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004134 bool send_acpi_ev;
4135 bool ignore_acpi_ev;
4136 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004137
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004138 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07004139 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004140 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004141 acpi_bus_generate_netlink_event(
4142 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004143 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004144 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004145 return;
4146 }
4147
4148 while (1) {
4149 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07004150 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004151 return;
4152 }
4153
4154 if (hkey == 0) {
4155 /* queue empty */
4156 return;
4157 }
4158
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004159 send_acpi_ev = true;
4160 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004161
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004162 switch (hkey >> 12) {
4163 case 1:
4164 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004165 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4166 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004167 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004168 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004169 /* 0x2000-0x2FFF: Wakeup reason */
4170 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4171 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004172 break;
4173 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004174 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004175 switch (hkey) {
4176 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004177 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004178 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004179 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004180 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004181 break;
4182 case TP_HKEY_EV_OPTDRV_EJ:
4183 /* FIXME: kick libata if SATA link offline */
4184 known_ev = true;
4185 break;
4186 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004187 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004188 }
4189 break;
4190 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03004191 /* 0x4000-0x4FFF: dock-related events */
4192 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4193 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004194 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004195 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02004196 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004197 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4198 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004199 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004200 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004201 /* 0x6000-0x6FFF: thermal alarms/notices and
4202 * keyboard events */
4203 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004204 &ignore_acpi_ev);
4205 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004206 case 7:
4207 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004208 if (tp_features.hotkey_wlsw &&
4209 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004210 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004211 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004212 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004213 break;
4214 }
4215 /* fallthrough to default */
4216 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004217 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004218 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004219 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07004220 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004221 pr_notice("please report the conditions when this event happened to %s\n",
4222 TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004223 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004224
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004225 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03004226 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004227 acpi_bus_generate_netlink_event(
4228 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004229 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004230 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004231 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004232 }
4233}
4234
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02004235static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004236{
4237 /* Do these on suspend, we get the events on early resume! */
4238 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4239 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08004240
4241 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004242 if (tp_features.has_adaptive_kbd) {
4243 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4244 "GTRW", "dd", 0)) {
4245 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004246 }
4247 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004248}
4249
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004250static void hotkey_resume(void)
4251{
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03004252 tpacpi_disable_brightness_delay();
4253
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004254 if (hotkey_status_set(true) < 0 ||
4255 hotkey_mask_set(hotkey_acpi_mask) < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004256 pr_err("error while attempting to reset the event firmware interface\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004257
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004258 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02004259 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004260 hotkey_wakeup_reason_notify_change();
4261 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004262 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004263
4264 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004265 if (tp_features.has_adaptive_kbd) {
4266 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4267 adaptive_keyboard_prev_mode)) {
4268 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004269 }
4270 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004271}
4272
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004273/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004274static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004276 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004278 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004279 seq_printf(m, "status:\t\tnot supported\n");
4280 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004281 }
4282
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004283 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004284 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004285 res = hotkey_status_get(&status);
4286 if (!res)
4287 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004288 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004289 if (res)
4290 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004292 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004293 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004294 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4295 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004297 seq_printf(m, "mask:\t\tnot supported\n");
4298 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 }
4300
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302}
4303
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004304static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004305{
4306 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004307 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004308 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4309 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 +00004310}
4311
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004312static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004314 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004315 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004318 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 return -ENODEV;
4320
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004321 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004322 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004323
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004324 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004325
4326 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 while ((cmd = next_cmd(&buf))) {
4328 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004329 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004331 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004332 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03004334 mask = (hotkey_all_mask | hotkey_source_mask)
4335 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4337 /* mask set */
4338 } else if (sscanf(cmd, "%x", &mask) == 1) {
4339 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004340 } else {
4341 res = -EINVAL;
4342 goto errexit;
4343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004345
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004346 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004347 tpacpi_disclose_usertask("procfs hotkey",
4348 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004349 res = hotkey_user_mask_set(mask);
4350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004352errexit:
4353 mutex_unlock(&hotkey_mutex);
4354 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004355}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004357static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004358 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4359 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Hui Wanga3c42a42016-11-08 16:13:23 +08004360 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004361 {"", 0},
4362};
4363
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004364static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004365 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004366 .notify = hotkey_notify,
4367 .handle = &hkey_handle,
4368 .type = ACPI_DEVICE_NOTIFY,
4369};
4370
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004371static struct ibm_struct hotkey_driver_data = {
4372 .name = "hotkey",
4373 .read = hotkey_read,
4374 .write = hotkey_write,
4375 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004376 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004377 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004378 .acpi = &ibm_hotkey_acpidriver,
4379};
4380
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004381/*************************************************************************
4382 * Bluetooth subdriver
4383 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004385enum {
4386 /* ACPI GBDC/SBDC bits */
4387 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4388 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004389 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004390 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004391};
4392
4393enum {
4394 /* ACPI \BLTH commands */
4395 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4396 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4397 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4398 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4399 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004400};
4401
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004402#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4403
Johannes Berg19d337d2009-06-02 13:01:37 +02004404static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004405{
4406 int status;
4407
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004408#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4409 if (dbg_bluetoothemul)
4410 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004411 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004412#endif
4413
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004414 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4415 return -EIO;
4416
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004417 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004418 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004419}
4420
Johannes Berg19d337d2009-06-02 13:01:37 +02004421static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004422{
4423 int status;
4424
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004425 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004426 "will attempt to %s bluetooth\n",
4427 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004428
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004429#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4430 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004431 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004432 return 0;
4433 }
4434#endif
4435
Johannes Berg19d337d2009-06-02 13:01:37 +02004436 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004437 status = TP_ACPI_BLUETOOTH_RADIOSSW
4438 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4439 else
4440 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004441
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004442 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4443 return -EIO;
4444
4445 return 0;
4446}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004447
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004448/* sysfs bluetooth enable ---------------------------------------------- */
4449static ssize_t bluetooth_enable_show(struct device *dev,
4450 struct device_attribute *attr,
4451 char *buf)
4452{
Johannes Berg19d337d2009-06-02 13:01:37 +02004453 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4454 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004455}
4456
4457static ssize_t bluetooth_enable_store(struct device *dev,
4458 struct device_attribute *attr,
4459 const char *buf, size_t count)
4460{
Johannes Berg19d337d2009-06-02 13:01:37 +02004461 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4462 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004463}
4464
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004465static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004466
4467/* --------------------------------------------------------------------- */
4468
4469static struct attribute *bluetooth_attributes[] = {
4470 &dev_attr_bluetooth_enable.attr,
4471 NULL
4472};
4473
4474static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004475 .attrs = bluetooth_attributes,
4476};
4477
Johannes Berg19d337d2009-06-02 13:01:37 +02004478static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4479 .get_status = bluetooth_get_status,
4480 .set_status = bluetooth_set_status,
4481};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004482
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004483static void bluetooth_shutdown(void)
4484{
4485 /* Order firmware to save current state to NVRAM */
4486 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4487 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004488 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004489 else
4490 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004491 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004492}
4493
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004494static void bluetooth_exit(void)
4495{
4496 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4497 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004498
4499 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4500
4501 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004502}
4503
Jiaxun Yangf7db8392019-03-07 17:37:16 +08004504static const struct dmi_system_id bt_fwbug_list[] __initconst = {
4505 {
4506 .ident = "ThinkPad E485",
4507 .matches = {
4508 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4509 DMI_MATCH(DMI_BOARD_NAME, "20KU"),
4510 },
4511 },
4512 {
4513 .ident = "ThinkPad E585",
4514 .matches = {
4515 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4516 DMI_MATCH(DMI_BOARD_NAME, "20KV"),
4517 },
4518 },
4519 {
4520 .ident = "ThinkPad A285 - 20MW",
4521 .matches = {
4522 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4523 DMI_MATCH(DMI_BOARD_NAME, "20MW"),
4524 },
4525 },
4526 {
4527 .ident = "ThinkPad A285 - 20MX",
4528 .matches = {
4529 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4530 DMI_MATCH(DMI_BOARD_NAME, "20MX"),
4531 },
4532 },
4533 {
4534 .ident = "ThinkPad A485 - 20MU",
4535 .matches = {
4536 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4537 DMI_MATCH(DMI_BOARD_NAME, "20MU"),
4538 },
4539 },
4540 {
4541 .ident = "ThinkPad A485 - 20MV",
4542 .matches = {
4543 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4544 DMI_MATCH(DMI_BOARD_NAME, "20MV"),
4545 },
4546 },
4547 {}
4548};
4549
4550static const struct pci_device_id fwbug_cards_ids[] __initconst = {
4551 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
4552 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
4553 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
4554 {}
4555};
4556
4557
4558static int __init have_bt_fwbug(void)
4559{
4560 /*
4561 * Some AMD based ThinkPads have a firmware bug that calling
4562 * "GBDC" will cause bluetooth on Intel wireless cards blocked
4563 */
4564 if (dmi_check_system(bt_fwbug_list) && pci_dev_present(fwbug_cards_ids)) {
4565 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4566 FW_BUG "disable bluetooth subdriver for Intel cards\n");
4567 return 1;
4568 } else
4569 return 0;
4570}
4571
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004572static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004574 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004575 int status = 0;
4576
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004577 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4578 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004579
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004580 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004581
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004582 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4583 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Jiaxun Yangf7db8392019-03-07 17:37:16 +08004584 tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004585 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004587 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4588 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004589 str_supported(tp_features.bluetooth),
4590 status);
4591
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004592#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4593 if (dbg_bluetoothemul) {
4594 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004595 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004596 } else
4597#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004598 if (tp_features.bluetooth &&
4599 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4600 /* no bluetooth hardware present in system */
4601 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004602 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004603 "bluetooth hardware not installed\n");
4604 }
4605
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004606 if (!tp_features.bluetooth)
4607 return 1;
4608
Johannes Berg19d337d2009-06-02 13:01:37 +02004609 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4610 &bluetooth_tprfk_ops,
4611 RFKILL_TYPE_BLUETOOTH,
4612 TPACPI_RFK_BLUETOOTH_SW_NAME,
4613 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004614 if (res)
4615 return res;
4616
Johannes Berg19d337d2009-06-02 13:01:37 +02004617 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4618 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004619 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004620 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004621 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004622 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004623
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625}
4626
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004627/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004628static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004630 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631}
4632
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004633static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634{
Johannes Berg19d337d2009-06-02 13:01:37 +02004635 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636}
4637
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004638static struct ibm_struct bluetooth_driver_data = {
4639 .name = "bluetooth",
4640 .read = bluetooth_read,
4641 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004642 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004643 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004644};
4645
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004646/*************************************************************************
4647 * Wan subdriver
4648 */
4649
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004650enum {
4651 /* ACPI GWAN/SWAN bits */
4652 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4653 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004654 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004655 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004656};
4657
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004658#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4659
Johannes Berg19d337d2009-06-02 13:01:37 +02004660static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004661{
4662 int status;
4663
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004664#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4665 if (dbg_wwanemul)
4666 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004667 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004668#endif
4669
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004670 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4671 return -EIO;
4672
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004673 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004674 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004675}
4676
Johannes Berg19d337d2009-06-02 13:01:37 +02004677static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004678{
4679 int status;
4680
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004681 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004682 "will attempt to %s wwan\n",
4683 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004684
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004685#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4686 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004687 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004688 return 0;
4689 }
4690#endif
4691
Johannes Berg19d337d2009-06-02 13:01:37 +02004692 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004693 status = TP_ACPI_WANCARD_RADIOSSW
4694 | TP_ACPI_WANCARD_RESUMECTRL;
4695 else
4696 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004697
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004698 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4699 return -EIO;
4700
4701 return 0;
4702}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004703
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004704/* sysfs wan enable ---------------------------------------------------- */
4705static ssize_t wan_enable_show(struct device *dev,
4706 struct device_attribute *attr,
4707 char *buf)
4708{
Johannes Berg19d337d2009-06-02 13:01:37 +02004709 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4710 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004711}
4712
4713static ssize_t wan_enable_store(struct device *dev,
4714 struct device_attribute *attr,
4715 const char *buf, size_t count)
4716{
Johannes Berg19d337d2009-06-02 13:01:37 +02004717 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4718 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004719}
4720
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004721static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4722 wan_enable_show, wan_enable_store);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004723
4724/* --------------------------------------------------------------------- */
4725
4726static struct attribute *wan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004727 &dev_attr_wwan_enable.attr,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004728 NULL
4729};
4730
4731static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004732 .attrs = wan_attributes,
4733};
4734
Johannes Berg19d337d2009-06-02 13:01:37 +02004735static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4736 .get_status = wan_get_status,
4737 .set_status = wan_set_status,
4738};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004739
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004740static void wan_shutdown(void)
4741{
4742 /* Order firmware to save current state to NVRAM */
4743 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4744 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004745 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004746 else
4747 vdbg_printk(TPACPI_DBG_RFKILL,
4748 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004749}
4750
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004751static void wan_exit(void)
4752{
4753 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4754 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004755
4756 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4757
4758 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004759}
4760
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004761static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004762{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004763 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004764 int status = 0;
4765
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004766 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4767 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004768
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004769 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004770
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004771 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004772 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004773
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004774 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4775 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004776 str_supported(tp_features.wan),
4777 status);
4778
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004779#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4780 if (dbg_wwanemul) {
4781 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004782 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004783 } else
4784#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004785 if (tp_features.wan &&
4786 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4787 /* no wan hardware present in system */
4788 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004789 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004790 "wan hardware not installed\n");
4791 }
4792
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004793 if (!tp_features.wan)
4794 return 1;
4795
Johannes Berg19d337d2009-06-02 13:01:37 +02004796 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4797 &wan_tprfk_ops,
4798 RFKILL_TYPE_WWAN,
4799 TPACPI_RFK_WWAN_SW_NAME,
4800 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004801 if (res)
4802 return res;
4803
Johannes Berg19d337d2009-06-02 13:01:37 +02004804 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4805 &wan_attr_group);
4806
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004807 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004808 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004809 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004810 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004811
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004812 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004813}
4814
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004815/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004816static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004817{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004818 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004819}
4820
4821static int wan_write(char *buf)
4822{
Johannes Berg19d337d2009-06-02 13:01:37 +02004823 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004824}
4825
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004826static struct ibm_struct wan_driver_data = {
4827 .name = "wan",
4828 .read = wan_read,
4829 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004830 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004831 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004832};
4833
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004834/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004835 * UWB subdriver
4836 */
4837
4838enum {
4839 /* ACPI GUWB/SUWB bits */
4840 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4841 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4842};
4843
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004844#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4845
Johannes Berg19d337d2009-06-02 13:01:37 +02004846static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004847{
4848 int status;
4849
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004850#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4851 if (dbg_uwbemul)
4852 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004853 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004854#endif
4855
4856 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4857 return -EIO;
4858
4859 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004860 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004861}
4862
Johannes Berg19d337d2009-06-02 13:01:37 +02004863static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004864{
4865 int status;
4866
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004867 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004868 "will attempt to %s UWB\n",
4869 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004870
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004871#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4872 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004873 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004874 return 0;
4875 }
4876#endif
4877
Johannes Berg19d337d2009-06-02 13:01:37 +02004878 if (state == TPACPI_RFK_RADIO_ON)
4879 status = TP_ACPI_UWB_RADIOSSW;
4880 else
4881 status = 0;
4882
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004883 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4884 return -EIO;
4885
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004886 return 0;
4887}
4888
4889/* --------------------------------------------------------------------- */
4890
Johannes Berg19d337d2009-06-02 13:01:37 +02004891static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4892 .get_status = uwb_get_status,
4893 .set_status = uwb_set_status,
4894};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004895
4896static void uwb_exit(void)
4897{
Johannes Berg19d337d2009-06-02 13:01:37 +02004898 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004899}
4900
4901static int __init uwb_init(struct ibm_init_struct *iibm)
4902{
4903 int res;
4904 int status = 0;
4905
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004906 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4907 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004908
4909 TPACPI_ACPIHANDLE_INIT(hkey);
4910
4911 tp_features.uwb = hkey_handle &&
4912 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4913
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004914 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4915 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004916 str_supported(tp_features.uwb),
4917 status);
4918
4919#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4920 if (dbg_uwbemul) {
4921 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004922 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004923 } else
4924#endif
4925 if (tp_features.uwb &&
4926 !(status & TP_ACPI_UWB_HWPRESENT)) {
4927 /* no uwb hardware present in system */
4928 tp_features.uwb = 0;
4929 dbg_printk(TPACPI_DBG_INIT,
4930 "uwb hardware not installed\n");
4931 }
4932
4933 if (!tp_features.uwb)
4934 return 1;
4935
4936 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004937 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004938 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004939 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004940 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004941 return res;
4942}
4943
4944static struct ibm_struct uwb_driver_data = {
4945 .name = "uwb",
4946 .exit = uwb_exit,
4947 .flags.experimental = 1,
4948};
4949
4950/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004951 * Video subdriver
4952 */
4953
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004954#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4955
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004956enum video_access_mode {
4957 TPACPI_VIDEO_NONE = 0,
4958 TPACPI_VIDEO_570, /* 570 */
4959 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4960 TPACPI_VIDEO_NEW, /* all others */
4961};
4962
4963enum { /* video status flags, based on VIDEO_570 */
4964 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4965 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4966 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4967};
4968
4969enum { /* TPACPI_VIDEO_570 constants */
4970 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4971 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4972 * video_status_flags */
4973 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4974 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4975};
4976
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004977static enum video_access_mode video_supported;
4978static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004979
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004980static int video_autosw_get(void);
4981static int video_autosw_set(int enable);
4982
Joe Perches112a6ee2011-04-04 10:06:24 -07004983TPACPI_HANDLE(vid, root,
4984 "\\_SB.PCI.AGP.VGA", /* 570 */
4985 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4986 "\\_SB.PCI0.VID0", /* 770e */
4987 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4988 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4989 "\\_SB.PCI0.AGP.VID", /* all others */
4990 ); /* R30, R31 */
4991
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004992TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004993
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004994static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004996 int ivga;
4997
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004998 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4999
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005000 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005001 if (tpacpi_is_ibm())
5002 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005003
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005004 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
5005 /* G41, assume IVGA doesn't change */
5006 vid_handle = vid2_handle;
5007
5008 if (!vid_handle)
5009 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005010 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005011 else if (tpacpi_is_ibm() &&
5012 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005013 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005014 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005015 else if (tpacpi_is_ibm() &&
5016 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005017 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005018 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005019 else
5020 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005021 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005023 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
5024 str_supported(video_supported != TPACPI_VIDEO_NONE),
5025 video_supported);
5026
Jan van den Berg72a979f2014-09-17 00:01:08 +02005027 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028}
5029
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005030static void video_exit(void)
5031{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005032 dbg_printk(TPACPI_DBG_EXIT,
5033 "restoring original video autoswitch mode\n");
5034 if (video_autosw_set(video_orig_autosw))
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03005035 pr_err("error while trying to restore original video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005036}
5037
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005038static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039{
5040 int status = 0;
5041 int i;
5042
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005043 switch (video_supported) {
5044 case TPACPI_VIDEO_570:
5045 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
5046 TP_ACPI_VIDEO_570_PHSCMD))
5047 return -EIO;
5048 status = i & TP_ACPI_VIDEO_570_PHSMASK;
5049 break;
5050 case TPACPI_VIDEO_770:
5051 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
5052 return -EIO;
5053 if (i)
5054 status |= TP_ACPI_VIDEO_S_LCD;
5055 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
5056 return -EIO;
5057 if (i)
5058 status |= TP_ACPI_VIDEO_S_CRT;
5059 break;
5060 case TPACPI_VIDEO_NEW:
5061 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
5062 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
5063 return -EIO;
5064 if (i)
5065 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005067 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
5068 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
5069 return -EIO;
5070 if (i)
5071 status |= TP_ACPI_VIDEO_S_LCD;
5072 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
5073 return -EIO;
5074 if (i)
5075 status |= TP_ACPI_VIDEO_S_DVI;
5076 break;
5077 default:
5078 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
5081 return status;
5082}
5083
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005084static int video_outputsw_set(int status)
5085{
5086 int autosw;
5087 int res = 0;
5088
5089 switch (video_supported) {
5090 case TPACPI_VIDEO_570:
5091 res = acpi_evalf(NULL, NULL,
5092 "\\_SB.PHS2", "vdd",
5093 TP_ACPI_VIDEO_570_PHS2CMD,
5094 status | TP_ACPI_VIDEO_570_PHS2SET);
5095 break;
5096 case TPACPI_VIDEO_770:
5097 autosw = video_autosw_get();
5098 if (autosw < 0)
5099 return autosw;
5100
5101 res = video_autosw_set(1);
5102 if (res)
5103 return res;
5104 res = acpi_evalf(vid_handle, NULL,
5105 "ASWT", "vdd", status * 0x100, 0);
5106 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07005107 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005108 return -EIO;
5109 }
5110 break;
5111 case TPACPI_VIDEO_NEW:
5112 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005113 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005114 break;
5115 default:
5116 return -ENOSYS;
5117 }
5118
Jan van den Berg72a979f2014-09-17 00:01:08 +02005119 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005120}
5121
5122static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005124 int autosw = 0;
5125
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005126 switch (video_supported) {
5127 case TPACPI_VIDEO_570:
5128 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5129 return -EIO;
5130 break;
5131 case TPACPI_VIDEO_770:
5132 case TPACPI_VIDEO_NEW:
5133 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5134 return -EIO;
5135 break;
5136 default:
5137 return -ENOSYS;
5138 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005139
5140 return autosw & 1;
5141}
5142
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005143static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005144{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005145 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005146 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005147 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005148}
5149
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005150static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005151{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005152 int autosw = video_autosw_get();
5153 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005154
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005155 if (autosw < 0)
5156 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005157
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005158 switch (video_supported) {
5159 case TPACPI_VIDEO_570:
5160 res = video_autosw_set(1);
5161 if (res)
5162 return res;
5163 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5164 break;
5165 case TPACPI_VIDEO_770:
5166 case TPACPI_VIDEO_NEW:
5167 res = video_autosw_set(1);
5168 if (res)
5169 return res;
5170 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5171 break;
5172 default:
5173 return -ENOSYS;
5174 }
5175 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07005176 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005177 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005178 }
5179
Jan van den Berg72a979f2014-09-17 00:01:08 +02005180 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005181}
5182
5183static int video_expand_toggle(void)
5184{
5185 switch (video_supported) {
5186 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005187 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005188 0 : -EIO;
5189 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005190 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005191 0 : -EIO;
5192 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005193 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005194 0 : -EIO;
5195 default:
5196 return -ENOSYS;
5197 }
5198 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005199}
5200
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005201static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005202{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005203 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005204
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005205 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005206 seq_printf(m, "status:\t\tnot supported\n");
5207 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005208 }
5209
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005210 /* Even reads can crash X.org, so... */
5211 if (!capable(CAP_SYS_ADMIN))
5212 return -EPERM;
5213
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005214 status = video_outputsw_get();
5215 if (status < 0)
5216 return status;
5217
5218 autosw = video_autosw_get();
5219 if (autosw < 0)
5220 return autosw;
5221
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005222 seq_printf(m, "status:\t\tsupported\n");
5223 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5224 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005225 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005226 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5227 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5228 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5229 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005230 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005231 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5232 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5233 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005234
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005235 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005236}
5237
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005238static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239{
5240 char *cmd;
5241 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005242 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005244 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005245 return -ENODEV;
5246
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005247 /* Even reads can crash X.org, let alone writes... */
5248 if (!capable(CAP_SYS_ADMIN))
5249 return -EPERM;
5250
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005251 enable = 0;
5252 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
5254 while ((cmd = next_cmd(&buf))) {
5255 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005256 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005258 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005260 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005262 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005263 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005264 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005265 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005266 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005267 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005268 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005270 res = video_autosw_set(1);
5271 if (res)
5272 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005274 res = video_autosw_set(0);
5275 if (res)
5276 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005278 res = video_outputsw_cycle();
5279 if (res)
5280 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005282 res = video_expand_toggle();
5283 if (res)
5284 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 } else
5286 return -EINVAL;
5287 }
5288
5289 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005290 status = video_outputsw_get();
5291 if (status < 0)
5292 return status;
5293 res = video_outputsw_set((status & ~disable) | enable);
5294 if (res)
5295 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 }
5297
5298 return 0;
5299}
5300
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005301static struct ibm_struct video_driver_data = {
5302 .name = "video",
5303 .read = video_read,
5304 .write = video_write,
5305 .exit = video_exit,
5306};
5307
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005308#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5309
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005310/*************************************************************************
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005311 * Keyboard backlight subdriver
5312 */
5313
Hans de Goedec685e202017-02-09 16:44:13 +01005314static enum led_brightness kbdlight_brightness;
5315static DEFINE_MUTEX(kbdlight_mutex);
5316
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005317static int kbdlight_set_level(int level)
5318{
Hans de Goedec685e202017-02-09 16:44:13 +01005319 int ret = 0;
5320
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005321 if (!hkey_handle)
5322 return -ENXIO;
5323
Hans de Goedec685e202017-02-09 16:44:13 +01005324 mutex_lock(&kbdlight_mutex);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005325
Hans de Goedec685e202017-02-09 16:44:13 +01005326 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5327 ret = -EIO;
5328 else
5329 kbdlight_brightness = level;
5330
5331 mutex_unlock(&kbdlight_mutex);
5332
5333 return ret;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005334}
5335
5336static int kbdlight_get_level(void)
5337{
5338 int status = 0;
5339
5340 if (!hkey_handle)
5341 return -ENXIO;
5342
5343 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5344 return -EIO;
5345
5346 if (status < 0)
5347 return status;
5348
5349 return status & 0x3;
5350}
5351
5352static bool kbdlight_is_supported(void)
5353{
5354 int status = 0;
5355
5356 if (!hkey_handle)
5357 return false;
5358
5359 if (!acpi_has_method(hkey_handle, "MLCG")) {
5360 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5361 return false;
5362 }
5363
5364 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5365 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5366 return false;
5367 }
5368
5369 if (status < 0) {
5370 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5371 return false;
5372 }
5373
5374 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5375 /*
5376 * Guessed test for keyboard backlight:
5377 *
5378 * Machines with backlight keyboard return:
5379 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5380 * b110100010010000000XX - ThinkPad x230
5381 * b010100000010000000XX - ThinkPad x240
5382 * b010100000010000000XX - ThinkPad W541
5383 * (XX is current backlight level)
5384 *
5385 * Machines without backlight keyboard return:
5386 * b10100001000000000000 - ThinkPad x230
5387 * b10110001000000000000 - ThinkPad E430
5388 * b00000000000000000000 - ThinkPad E450
5389 *
5390 * Candidate BITs for detection test (XOR):
5391 * b01000000001000000000
5392 * ^
5393 */
5394 return status & BIT(9);
5395}
5396
Hans de Goede86ec0c22017-02-09 16:44:12 +01005397static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005398 enum led_brightness brightness)
5399{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005400 return kbdlight_set_level(brightness);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005401}
5402
5403static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5404{
5405 int level;
5406
5407 level = kbdlight_get_level();
5408 if (level < 0)
5409 return 0;
5410
5411 return level;
5412}
5413
5414static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5415 .led_classdev = {
5416 .name = "tpacpi::kbd_backlight",
5417 .max_brightness = 2,
Hans de Goedec685e202017-02-09 16:44:13 +01005418 .flags = LED_BRIGHT_HW_CHANGED,
Hans de Goede86ec0c22017-02-09 16:44:12 +01005419 .brightness_set_blocking = &kbdlight_sysfs_set,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005420 .brightness_get = &kbdlight_sysfs_get,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005421 }
5422};
5423
5424static int __init kbdlight_init(struct ibm_init_struct *iibm)
5425{
5426 int rc;
5427
5428 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5429
5430 TPACPI_ACPIHANDLE_INIT(hkey);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005431
5432 if (!kbdlight_is_supported()) {
5433 tp_features.kbdlight = 0;
5434 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5435 return 1;
5436 }
5437
Hans de Goedec685e202017-02-09 16:44:13 +01005438 kbdlight_brightness = kbdlight_sysfs_get(NULL);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005439 tp_features.kbdlight = 1;
5440
5441 rc = led_classdev_register(&tpacpi_pdev->dev,
5442 &tpacpi_led_kbdlight.led_classdev);
5443 if (rc < 0) {
5444 tp_features.kbdlight = 0;
5445 return rc;
5446 }
5447
Hans de Goedec685e202017-02-09 16:44:13 +01005448 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5449 TP_ACPI_HKEY_KBD_LIGHT_MASK);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005450 return 0;
5451}
5452
5453static void kbdlight_exit(void)
5454{
5455 if (tp_features.kbdlight)
5456 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005457}
5458
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005459static int kbdlight_set_level_and_update(int level)
5460{
5461 int ret;
5462 struct led_classdev *led_cdev;
5463
5464 ret = kbdlight_set_level(level);
5465 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5466
5467 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5468 led_cdev->brightness = level;
5469
5470 return ret;
5471}
5472
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005473static int kbdlight_read(struct seq_file *m)
5474{
5475 int level;
5476
5477 if (!tp_features.kbdlight) {
5478 seq_printf(m, "status:\t\tnot supported\n");
5479 } else {
5480 level = kbdlight_get_level();
5481 if (level < 0)
5482 seq_printf(m, "status:\t\terror %d\n", level);
5483 else
5484 seq_printf(m, "status:\t\t%d\n", level);
5485 seq_printf(m, "commands:\t0, 1, 2\n");
5486 }
5487
5488 return 0;
5489}
5490
5491static int kbdlight_write(char *buf)
5492{
5493 char *cmd;
5494 int level = -1;
5495
5496 if (!tp_features.kbdlight)
5497 return -ENODEV;
5498
5499 while ((cmd = next_cmd(&buf))) {
5500 if (strlencmp(cmd, "0") == 0)
5501 level = 0;
5502 else if (strlencmp(cmd, "1") == 0)
5503 level = 1;
5504 else if (strlencmp(cmd, "2") == 0)
5505 level = 2;
5506 else
5507 return -EINVAL;
5508 }
5509
5510 if (level == -1)
5511 return -EINVAL;
5512
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005513 return kbdlight_set_level_and_update(level);
5514}
5515
5516static void kbdlight_suspend(void)
5517{
5518 struct led_classdev *led_cdev;
5519
5520 if (!tp_features.kbdlight)
5521 return;
5522
5523 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5524 led_update_brightness(led_cdev);
5525 led_classdev_suspend(led_cdev);
5526}
5527
5528static void kbdlight_resume(void)
5529{
5530 if (!tp_features.kbdlight)
5531 return;
5532
5533 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005534}
5535
5536static struct ibm_struct kbdlight_driver_data = {
5537 .name = "kbdlight",
5538 .read = kbdlight_read,
5539 .write = kbdlight_write,
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005540 .suspend = kbdlight_suspend,
5541 .resume = kbdlight_resume,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005542 .exit = kbdlight_exit,
5543};
5544
5545/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005546 * Light (thinklight) subdriver
5547 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005549TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5550TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005551
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005552static int light_get_status(void)
5553{
5554 int status = 0;
5555
5556 if (tp_features.light_status) {
5557 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5558 return -EIO;
5559 return (!!status);
5560 }
5561
5562 return -ENXIO;
5563}
5564
5565static int light_set_status(int status)
5566{
5567 int rc;
5568
5569 if (tp_features.light) {
5570 if (cmos_handle) {
5571 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005572 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005573 TP_CMOS_THINKLIGHT_ON :
5574 TP_CMOS_THINKLIGHT_OFF);
5575 } else {
5576 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005577 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005578 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005579 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005580 }
5581
5582 return -ENXIO;
5583}
5584
Hans de Goede86ec0c22017-02-09 16:44:12 +01005585static int light_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005586 enum led_brightness brightness)
5587{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005588 return light_set_status((brightness != LED_OFF) ?
5589 TPACPI_LED_ON : TPACPI_LED_OFF);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005590}
5591
5592static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5593{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005594 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005595}
5596
5597static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5598 .led_classdev = {
5599 .name = "tpacpi::thinklight",
Hans de Goede86ec0c22017-02-09 16:44:12 +01005600 .brightness_set_blocking = &light_sysfs_set,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005601 .brightness_get = &light_sysfs_get,
5602 }
5603};
5604
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005605static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005607 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005608
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005609 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5610
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005611 if (tpacpi_is_ibm()) {
5612 TPACPI_ACPIHANDLE_INIT(ledb);
5613 TPACPI_ACPIHANDLE_INIT(lght);
5614 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005615 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005616
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005617 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005618 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005619
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005620 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005621 /* light status not supported on
5622 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005623 tp_features.light_status =
5624 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005626 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5627 str_supported(tp_features.light),
5628 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005629
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005630 if (!tp_features.light)
5631 return 1;
5632
5633 rc = led_classdev_register(&tpacpi_pdev->dev,
5634 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005635
5636 if (rc < 0) {
5637 tp_features.light = 0;
5638 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005639 } else {
5640 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005641 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005642
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005643 return rc;
5644}
5645
5646static void light_exit(void)
5647{
5648 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649}
5650
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005651static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005653 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005655 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005656 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005657 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005658 seq_printf(m, "status:\t\tunknown\n");
5659 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005660 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005661 status = light_get_status();
5662 if (status < 0)
5663 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005664 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5665 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669}
5670
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005671static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005674 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005675
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005676 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005677 return -ENODEV;
5678
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 while ((cmd = next_cmd(&buf))) {
5680 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005681 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005683 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 } else
5685 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 }
5687
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005688 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689}
5690
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005691static struct ibm_struct light_driver_data = {
5692 .name = "light",
5693 .read = light_read,
5694 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005695 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005696};
5697
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005698/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005699 * CMOS subdriver
5700 */
5701
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005702/* sysfs cmos_command -------------------------------------------------- */
5703static ssize_t cmos_command_store(struct device *dev,
5704 struct device_attribute *attr,
5705 const char *buf, size_t count)
5706{
5707 unsigned long cmos_cmd;
5708 int res;
5709
5710 if (parse_strtoul(buf, 21, &cmos_cmd))
5711 return -EINVAL;
5712
5713 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005714 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005715}
5716
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01005717static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005718
5719/* --------------------------------------------------------------------- */
5720
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005721static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005722{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005723 int res;
5724
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005725 vdbg_printk(TPACPI_DBG_INIT,
5726 "initializing cmos commands subdriver\n");
5727
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005728 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005729
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005730 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5731 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005732
5733 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5734 if (res)
5735 return res;
5736
Jan van den Berg72a979f2014-09-17 00:01:08 +02005737 return (cmos_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005738}
5739
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005740static void cmos_exit(void)
5741{
5742 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5743}
5744
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005745static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005747 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5748 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005750 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005752 seq_printf(m, "status:\t\tsupported\n");
5753 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 }
5755
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757}
5758
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005759static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760{
5761 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005762 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763
5764 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005765 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5766 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 /* cmos_cmd set */
5768 } else
5769 return -EINVAL;
5770
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005771 res = issue_thinkpad_cmos_command(cmos_cmd);
5772 if (res)
5773 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 }
5775
5776 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005777}
5778
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005779static struct ibm_struct cmos_driver_data = {
5780 .name = "cmos",
5781 .read = cmos_read,
5782 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005783 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005784};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005785
5786/*************************************************************************
5787 * LED subdriver
5788 */
5789
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005790enum led_access_mode {
5791 TPACPI_LED_NONE = 0,
5792 TPACPI_LED_570, /* 570 */
5793 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5794 TPACPI_LED_NEW, /* all others */
5795};
5796
5797enum { /* For TPACPI_LED_OLD */
5798 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5799 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5800 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5801};
5802
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005803static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005804
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005805static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005806
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005807#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005808static struct tpacpi_led_classdev *tpacpi_leds;
5809static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005810static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005811 /* there's a limit of 19 chars + NULL before 2.6.26 */
5812 "tpacpi::power",
5813 "tpacpi:orange:batt",
5814 "tpacpi:green:batt",
5815 "tpacpi::dock_active",
5816 "tpacpi::bay_active",
5817 "tpacpi::dock_batt",
5818 "tpacpi::unknown_led",
5819 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005820 "tpacpi::dock_status1",
5821 "tpacpi::dock_status2",
5822 "tpacpi::unknown_led2",
5823 "tpacpi::unknown_led3",
5824 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005825};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005826#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005827
5828static inline bool tpacpi_is_led_restricted(const unsigned int led)
5829{
5830#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5831 return false;
5832#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005833 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005834#endif
5835}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005836
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005837static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005838{
5839 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005840 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005841
5842 switch (led_supported) {
5843 case TPACPI_LED_570:
5844 if (!acpi_evalf(ec_handle,
5845 &status, "GLED", "dd", 1 << led))
5846 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005847 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005848 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005849 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005850 TPACPI_LED_ON :
5851 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005852 tpacpi_led_state_cache[led] = led_s;
5853 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005854 default:
5855 return -ENXIO;
5856 }
5857
5858 /* not reached */
5859}
5860
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005861static int led_set_status(const unsigned int led,
5862 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005863{
5864 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005865 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5866 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005867
5868 int rc = 0;
5869
5870 switch (led_supported) {
5871 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005872 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005873 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005874 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005875 if (unlikely(tpacpi_is_led_restricted(led)))
5876 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005877 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5878 (1 << led), led_sled_arg1[ledstatus]))
5879 rc = -EIO;
5880 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005881 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005882 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005883 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005884 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005885 if (unlikely(tpacpi_is_led_restricted(led)))
5886 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005887 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5888 if (rc >= 0)
5889 rc = ec_write(TPACPI_LED_EC_HLBL,
5890 (ledstatus == TPACPI_LED_BLINK) << led);
5891 if (rc >= 0)
5892 rc = ec_write(TPACPI_LED_EC_HLCL,
5893 (ledstatus != TPACPI_LED_OFF) << led);
5894 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005895 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005896 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005897 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5898 return -EINVAL;
5899 if (unlikely(tpacpi_is_led_restricted(led)))
5900 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005901 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5902 led, led_led_arg1[ledstatus]))
5903 rc = -EIO;
5904 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005905 default:
5906 rc = -ENXIO;
5907 }
5908
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005909 if (!rc)
5910 tpacpi_led_state_cache[led] = ledstatus;
5911
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005912 return rc;
5913}
5914
Hans de Goede86ec0c22017-02-09 16:44:12 +01005915static int led_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005916 enum led_brightness brightness)
5917{
5918 struct tpacpi_led_classdev *data = container_of(led_cdev,
5919 struct tpacpi_led_classdev, led_classdev);
Hans de Goede86ec0c22017-02-09 16:44:12 +01005920 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005921
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005922 if (brightness == LED_OFF)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005923 new_state = TPACPI_LED_OFF;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005924 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005925 new_state = TPACPI_LED_ON;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005926 else
Hans de Goede86ec0c22017-02-09 16:44:12 +01005927 new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005928
Hans de Goede86ec0c22017-02-09 16:44:12 +01005929 return led_set_status(data->led, new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005930}
5931
5932static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5933 unsigned long *delay_on, unsigned long *delay_off)
5934{
5935 struct tpacpi_led_classdev *data = container_of(led_cdev,
5936 struct tpacpi_led_classdev, led_classdev);
5937
5938 /* Can we choose the flash rate? */
5939 if (*delay_on == 0 && *delay_off == 0) {
5940 /* yes. set them to the hardware blink rate (1 Hz) */
5941 *delay_on = 500; /* ms */
5942 *delay_off = 500; /* ms */
5943 } else if ((*delay_on != 500) || (*delay_off != 500))
5944 return -EINVAL;
5945
Hans de Goede86ec0c22017-02-09 16:44:12 +01005946 return led_set_status(data->led, TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005947}
5948
5949static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5950{
5951 int rc;
5952
5953 struct tpacpi_led_classdev *data = container_of(led_cdev,
5954 struct tpacpi_led_classdev, led_classdev);
5955
5956 rc = led_get_status(data->led);
5957
5958 if (rc == TPACPI_LED_OFF || rc < 0)
5959 rc = LED_OFF; /* no error handling in led class :( */
5960 else
5961 rc = LED_FULL;
5962
5963 return rc;
5964}
5965
5966static void led_exit(void)
5967{
5968 unsigned int i;
5969
5970 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5971 if (tpacpi_leds[i].led_classdev.name)
5972 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5973 }
5974
5975 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005976}
5977
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005978static int __init tpacpi_init_led(unsigned int led)
5979{
5980 int rc;
5981
5982 tpacpi_leds[led].led = led;
5983
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005984 /* LEDs with no name don't get registered */
5985 if (!tpacpi_led_names[led])
5986 return 0;
5987
Hans de Goede86ec0c22017-02-09 16:44:12 +01005988 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005989 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5990 if (led_supported == TPACPI_LED_570)
5991 tpacpi_leds[led].led_classdev.brightness_get =
5992 &led_sysfs_get;
5993
5994 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5995
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005996 rc = led_classdev_register(&tpacpi_pdev->dev,
5997 &tpacpi_leds[led].led_classdev);
5998 if (rc < 0)
5999 tpacpi_leds[led].led_classdev.name = NULL;
6000
6001 return rc;
6002}
6003
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006004static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
6005 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
6006 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
6007 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
6008
6009 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
6010 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
6011 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
6012 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
6013 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
6014 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
6015 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
6016 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
6017
6018 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
6019 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
6020 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
6021 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
6022 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
6023
6024 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
6025 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
6026 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
6027 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
6028
6029 /* (1) - may have excess leds enabled on MSB */
6030
6031 /* Defaults (order matters, keep last, don't reorder!) */
6032 { /* Lenovo */
6033 .vendor = PCI_VENDOR_ID_LENOVO,
6034 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
6035 .quirks = 0x1fffU,
6036 },
6037 { /* IBM ThinkPads with no EC version string */
6038 .vendor = PCI_VENDOR_ID_IBM,
6039 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
6040 .quirks = 0x00ffU,
6041 },
6042 { /* IBM ThinkPads with EC version string */
6043 .vendor = PCI_VENDOR_ID_IBM,
6044 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
6045 .quirks = 0x00bfU,
6046 },
6047};
6048
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03006049static enum led_access_mode __init led_init_detect_mode(void)
6050{
6051 acpi_status status;
6052
6053 if (tpacpi_is_ibm()) {
6054 /* 570 */
6055 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
6056 if (ACPI_SUCCESS(status))
6057 return TPACPI_LED_570;
6058
6059 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
6060 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
6061 if (ACPI_SUCCESS(status))
6062 return TPACPI_LED_OLD;
6063 }
6064
6065 /* most others */
6066 status = acpi_get_handle(ec_handle, "LED", &led_handle);
6067 if (ACPI_SUCCESS(status))
6068 return TPACPI_LED_NEW;
6069
6070 /* R30, R31, and unknown firmwares */
6071 led_handle = NULL;
6072 return TPACPI_LED_NONE;
6073}
6074
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006075static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006076{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006077 unsigned int i;
6078 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006079 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006080
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006081 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
6082
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03006083 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006084
Adam Leefcb44e12013-06-07 16:20:08 +08006085 if (led_supported != TPACPI_LED_NONE) {
6086 useful_leds = tpacpi_check_quirks(led_useful_qtable,
6087 ARRAY_SIZE(led_useful_qtable));
6088
6089 if (!useful_leds) {
6090 led_handle = NULL;
6091 led_supported = TPACPI_LED_NONE;
6092 }
6093 }
6094
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006095 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
6096 str_supported(led_supported), led_supported);
6097
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006098 if (led_supported == TPACPI_LED_NONE)
6099 return 1;
6100
Kees Cook6396bb22018-06-12 14:03:40 -07006101 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006102 GFP_KERNEL);
6103 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07006104 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006105 return -ENOMEM;
6106 }
6107
6108 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08006109 tpacpi_leds[i].led = -1;
6110
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006111 if (!tpacpi_is_led_restricted(i) &&
6112 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006113 rc = tpacpi_init_led(i);
6114 if (rc < 0) {
6115 led_exit();
6116 return rc;
6117 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006118 }
6119 }
6120
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006121#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006122 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006123#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006124 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006125}
6126
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006127#define str_led_status(s) \
6128 ((s) == TPACPI_LED_OFF ? "off" : \
6129 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006130
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006131static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006133 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006134 seq_printf(m, "status:\t\tnot supported\n");
6135 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006136 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006137 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006138
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006139 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006140 /* 570 */
6141 int i, status;
6142 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006143 status = led_get_status(i);
6144 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006145 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006146 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006147 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006148 }
6149 }
6150
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006151 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154}
6155
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006156static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157{
6158 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006159 int led, rc;
6160 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006161
6162 if (!led_supported)
6163 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164
6165 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08006166 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 return -EINVAL;
6168
Adam Leeedf2d772013-06-08 16:51:15 +08006169 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
6170 tpacpi_leds[led].led < 0)
6171 return -ENODEV;
6172
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006173 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006174 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006176 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006177 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006178 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006179 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006180 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006181 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006182
6183 rc = led_set_status(led, s);
6184 if (rc < 0)
6185 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 }
6187
6188 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006189}
6190
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006191static struct ibm_struct led_driver_data = {
6192 .name = "led",
6193 .read = led_read,
6194 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006195 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006196};
6197
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006198/*************************************************************************
6199 * Beep subdriver
6200 */
6201
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006202TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006203
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006204#define TPACPI_BEEP_Q1 0x0001
6205
6206static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6207 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6208 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6209};
6210
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006211static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006212{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006213 unsigned long quirks;
6214
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006215 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6216
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006217 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006218
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006219 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6220 str_supported(beep_handle != NULL));
6221
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006222 quirks = tpacpi_check_quirks(beep_quirk_table,
6223 ARRAY_SIZE(beep_quirk_table));
6224
6225 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6226
Jan van den Berg72a979f2014-09-17 00:01:08 +02006227 return (beep_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006228}
6229
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006230static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006232 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006233 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006234 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006235 seq_printf(m, "status:\t\tsupported\n");
6236 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240}
6241
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006242static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243{
6244 char *cmd;
6245 int beep_cmd;
6246
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006247 if (!beep_handle)
6248 return -ENODEV;
6249
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006251 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6252 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 /* beep_cmd set */
6254 } else
6255 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006256 if (tp_features.beep_needs_two_args) {
6257 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6258 beep_cmd, 0))
6259 return -EIO;
6260 } else {
6261 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6262 beep_cmd))
6263 return -EIO;
6264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 }
6266
6267 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006268}
6269
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006270static struct ibm_struct beep_driver_data = {
6271 .name = "beep",
6272 .read = beep_read,
6273 .write = beep_write,
6274};
6275
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006276/*************************************************************************
6277 * Thermal subdriver
6278 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006279
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006280enum thermal_access_mode {
6281 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6282 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6283 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6284 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6285 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6286};
6287
6288enum { /* TPACPI_THERMAL_TPEC_* */
6289 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6290 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6291 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006292
6293 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006294};
6295
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006296
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006297#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6298struct ibm_thermal_sensors_struct {
6299 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6300};
6301
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006302static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006303
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006304/* idx is zero-based */
6305static int thermal_get_sensor(int idx, s32 *value)
6306{
6307 int t;
6308 s8 tmp;
6309 char tmpi[5];
6310
6311 t = TP_EC_THERMAL_TMP0;
6312
6313 switch (thermal_read_mode) {
6314#if TPACPI_MAX_THERMAL_SENSORS >= 16
6315 case TPACPI_THERMAL_TPEC_16:
6316 if (idx >= 8 && idx <= 15) {
6317 t = TP_EC_THERMAL_TMP8;
6318 idx -= 8;
6319 }
6320 /* fallthrough */
6321#endif
6322 case TPACPI_THERMAL_TPEC_8:
6323 if (idx <= 7) {
6324 if (!acpi_ec_read(t + idx, &tmp))
6325 return -EIO;
6326 *value = tmp * 1000;
6327 return 0;
6328 }
6329 break;
6330
6331 case TPACPI_THERMAL_ACPI_UPDT:
6332 if (idx <= 7) {
6333 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6334 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6335 return -EIO;
6336 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6337 return -EIO;
6338 *value = (t - 2732) * 100;
6339 return 0;
6340 }
6341 break;
6342
6343 case TPACPI_THERMAL_ACPI_TMP07:
6344 if (idx <= 7) {
6345 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6346 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6347 return -EIO;
6348 if (t > 127 || t < -127)
6349 t = TP_EC_THERMAL_TMP_NA;
6350 *value = t * 1000;
6351 return 0;
6352 }
6353 break;
6354
6355 case TPACPI_THERMAL_NONE:
6356 default:
6357 return -ENOSYS;
6358 }
6359
6360 return -EINVAL;
6361}
6362
6363static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6364{
6365 int res, i;
6366 int n;
6367
6368 n = 8;
6369 i = 0;
6370
6371 if (!s)
6372 return -EINVAL;
6373
6374 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6375 n = 16;
6376
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006377 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006378 res = thermal_get_sensor(i, &s->temp[i]);
6379 if (res)
6380 return res;
6381 }
6382
6383 return n;
6384}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006385
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006386static void thermal_dump_all_sensors(void)
6387{
6388 int n, i;
6389 struct ibm_thermal_sensors_struct t;
6390
6391 n = thermal_get_sensors(&t);
6392 if (n <= 0)
6393 return;
6394
Joe Perches0978e012011-04-04 10:06:25 -07006395 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006396
6397 for (i = 0; i < n; i++) {
6398 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07006399 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006400 else
Joe Perches0978e012011-04-04 10:06:25 -07006401 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006402 }
6403
Joe Perches0978e012011-04-04 10:06:25 -07006404 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006405}
6406
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006407/* sysfs temp##_input -------------------------------------------------- */
6408
6409static ssize_t thermal_temp_input_show(struct device *dev,
6410 struct device_attribute *attr,
6411 char *buf)
6412{
6413 struct sensor_device_attribute *sensor_attr =
6414 to_sensor_dev_attr(attr);
6415 int idx = sensor_attr->index;
6416 s32 value;
6417 int res;
6418
6419 res = thermal_get_sensor(idx, &value);
6420 if (res)
6421 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006422 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006423 return -ENXIO;
6424
6425 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6426}
6427
6428#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006429 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6430 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006431
6432static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6433 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6434 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6435 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6436 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6437 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6438 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6439 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6440 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6441 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6442 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6443 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6444 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6445 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6446 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6447 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6448 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6449};
6450
6451#define THERMAL_ATTRS(X) \
6452 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6453
6454static struct attribute *thermal_temp_input_attr[] = {
6455 THERMAL_ATTRS(8),
6456 THERMAL_ATTRS(9),
6457 THERMAL_ATTRS(10),
6458 THERMAL_ATTRS(11),
6459 THERMAL_ATTRS(12),
6460 THERMAL_ATTRS(13),
6461 THERMAL_ATTRS(14),
6462 THERMAL_ATTRS(15),
6463 THERMAL_ATTRS(0),
6464 THERMAL_ATTRS(1),
6465 THERMAL_ATTRS(2),
6466 THERMAL_ATTRS(3),
6467 THERMAL_ATTRS(4),
6468 THERMAL_ATTRS(5),
6469 THERMAL_ATTRS(6),
6470 THERMAL_ATTRS(7),
6471 NULL
6472};
6473
6474static const struct attribute_group thermal_temp_input16_group = {
6475 .attrs = thermal_temp_input_attr
6476};
6477
6478static const struct attribute_group thermal_temp_input8_group = {
6479 .attrs = &thermal_temp_input_attr[8]
6480};
6481
6482#undef THERMAL_SENSOR_ATTR_TEMP
6483#undef THERMAL_ATTRS
6484
6485/* --------------------------------------------------------------------- */
6486
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006487static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006488{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006489 u8 t, ta1, ta2;
6490 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006491 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006492 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006493
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006494 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6495
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006496 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006497
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03006498 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006499 /*
6500 * Direct EC access mode: sensors at registers
6501 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6502 * non-implemented, thermal sensors return 0x80 when
6503 * not available
6504 */
6505
6506 ta1 = ta2 = 0;
6507 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006508 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006509 ta1 |= t;
6510 } else {
6511 ta1 = 0;
6512 break;
6513 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006514 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006515 ta2 |= t;
6516 } else {
6517 ta1 = 0;
6518 break;
6519 }
6520 }
6521 if (ta1 == 0) {
6522 /* This is sheer paranoia, but we handle it anyway */
6523 if (acpi_tmp7) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006524 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006525 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006526 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006527 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006528 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006529 }
6530 } else {
6531 thermal_read_mode =
6532 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006533 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006534 }
6535 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006536 if (tpacpi_is_ibm() &&
6537 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006538 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006539 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006540 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006541 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006542 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006543 }
6544 } else {
6545 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006546 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006547 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006548
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006549 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6550 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6551 thermal_read_mode);
6552
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006553 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006554 case TPACPI_THERMAL_TPEC_16:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006555 res = sysfs_create_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006556 &thermal_temp_input16_group);
6557 if (res)
6558 return res;
6559 break;
6560 case TPACPI_THERMAL_TPEC_8:
6561 case TPACPI_THERMAL_ACPI_TMP07:
6562 case TPACPI_THERMAL_ACPI_UPDT:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006563 res = sysfs_create_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006564 &thermal_temp_input8_group);
6565 if (res)
6566 return res;
6567 break;
6568 case TPACPI_THERMAL_NONE:
6569 default:
6570 return 1;
6571 }
6572
6573 return 0;
6574}
6575
6576static void thermal_exit(void)
6577{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006578 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006579 case TPACPI_THERMAL_TPEC_16:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006580 sysfs_remove_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006581 &thermal_temp_input16_group);
6582 break;
6583 case TPACPI_THERMAL_TPEC_8:
6584 case TPACPI_THERMAL_ACPI_TMP07:
6585 case TPACPI_THERMAL_ACPI_UPDT:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006586 sysfs_remove_group(&tpacpi_hwmon->kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08006587 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006588 break;
6589 case TPACPI_THERMAL_NONE:
6590 default:
6591 break;
6592 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006593}
6594
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006595static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006596{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006597 int n, i;
6598 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006599
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006600 n = thermal_get_sensors(&t);
6601 if (unlikely(n < 0))
6602 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006603
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006604 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006605
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006606 if (n > 0) {
6607 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006608 seq_printf(m, "%d ", t.temp[i] / 1000);
6609 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006610 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006611 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006612
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006613 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006614}
6615
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006616static struct ibm_struct thermal_driver_data = {
6617 .name = "thermal",
6618 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006619 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006620};
6621
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006622/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006623 * Backlight/brightness subdriver
6624 */
Holger Macht8acb0252006-10-20 14:30:28 -07006625
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006626#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6627
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006628/*
6629 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6630 * CMOS NVRAM byte 0x5E, bits 0-3.
6631 *
6632 * EC HBRV (0x31) has the following layout
6633 * Bit 7: unknown function
6634 * Bit 6: unknown function
6635 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6636 * Bit 4: must be set to zero to avoid problems
6637 * Bit 3-0: backlight brightness level
6638 *
6639 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006640 *
6641 * WARNING: The X61 has been verified to use HBRV for something else, so
6642 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6643 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006644 */
6645
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006646enum {
6647 TP_EC_BACKLIGHT = 0x31,
6648
6649 /* TP_EC_BACKLIGHT bitmasks */
6650 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6651 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6652 TP_EC_BACKLIGHT_MAPSW = 0x20,
6653};
6654
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006655enum tpacpi_brightness_access_mode {
6656 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6657 TPACPI_BRGHT_MODE_EC, /* EC control */
6658 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6659 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6660 TPACPI_BRGHT_MODE_MAX
6661};
6662
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006663static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006664
6665static enum tpacpi_brightness_access_mode brightness_mode =
6666 TPACPI_BRGHT_MODE_MAX;
6667
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006668static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6669
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006670static struct mutex brightness_mutex;
6671
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006672/* NVRAM brightness access,
6673 * call with brightness_mutex held! */
6674static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006675{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006676 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006677
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006678 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6679 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6680 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006681 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006682
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006683 return lnvram;
6684}
6685
6686static void tpacpi_brightness_checkpoint_nvram(void)
6687{
6688 u8 lec = 0;
6689 u8 b_nvram;
6690
6691 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6692 return;
6693
6694 vdbg_printk(TPACPI_DBG_BRGHT,
6695 "trying to checkpoint backlight level to NVRAM...\n");
6696
6697 if (mutex_lock_killable(&brightness_mutex) < 0)
6698 return;
6699
6700 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6701 goto unlock;
6702 lec &= TP_EC_BACKLIGHT_LVLMSK;
6703 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6704
6705 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6706 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6707 /* NVRAM needs update */
6708 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6709 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6710 b_nvram |= lec;
6711 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6712 dbg_printk(TPACPI_DBG_BRGHT,
6713 "updated NVRAM backlight level to %u (0x%02x)\n",
6714 (unsigned int) lec, (unsigned int) b_nvram);
6715 } else
6716 vdbg_printk(TPACPI_DBG_BRGHT,
6717 "NVRAM backlight level already is %u (0x%02x)\n",
6718 (unsigned int) lec, (unsigned int) b_nvram);
6719
6720unlock:
6721 mutex_unlock(&brightness_mutex);
6722}
6723
6724
6725/* call with brightness_mutex held! */
6726static int tpacpi_brightness_get_raw(int *status)
6727{
6728 u8 lec = 0;
6729
6730 switch (brightness_mode) {
6731 case TPACPI_BRGHT_MODE_UCMS_STEP:
6732 *status = tpacpi_brightness_nvram_get();
6733 return 0;
6734 case TPACPI_BRGHT_MODE_EC:
6735 case TPACPI_BRGHT_MODE_ECNVRAM:
6736 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006737 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006738 *status = lec;
6739 return 0;
6740 default:
6741 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006742 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006743}
6744
6745/* call with brightness_mutex held! */
6746/* do NOT call with illegal backlight level value */
6747static int tpacpi_brightness_set_ec(unsigned int value)
6748{
6749 u8 lec = 0;
6750
6751 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6752 return -EIO;
6753
6754 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6755 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6756 (value & TP_EC_BACKLIGHT_LVLMSK))))
6757 return -EIO;
6758
6759 return 0;
6760}
6761
6762/* call with brightness_mutex held! */
6763static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6764{
6765 int cmos_cmd, inc;
6766 unsigned int current_value, i;
6767
6768 current_value = tpacpi_brightness_nvram_get();
6769
6770 if (value == current_value)
6771 return 0;
6772
6773 cmos_cmd = (value > current_value) ?
6774 TP_CMOS_BRIGHTNESS_UP :
6775 TP_CMOS_BRIGHTNESS_DOWN;
6776 inc = (value > current_value) ? 1 : -1;
6777
6778 for (i = current_value; i != value; i += inc)
6779 if (issue_thinkpad_cmos_command(cmos_cmd))
6780 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006781
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006782 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006783}
6784
6785/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006786static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006787{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006788 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006789
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006790 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006791 return -EINVAL;
6792
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006793 vdbg_printk(TPACPI_DBG_BRGHT,
6794 "set backlight level to %d\n", value);
6795
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006796 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006797 if (res < 0)
6798 return res;
6799
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006800 switch (brightness_mode) {
6801 case TPACPI_BRGHT_MODE_EC:
6802 case TPACPI_BRGHT_MODE_ECNVRAM:
6803 res = tpacpi_brightness_set_ec(value);
6804 break;
6805 case TPACPI_BRGHT_MODE_UCMS_STEP:
6806 res = tpacpi_brightness_set_ucmsstep(value);
6807 break;
6808 default:
6809 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006810 }
6811
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006812 mutex_unlock(&brightness_mutex);
6813 return res;
6814}
6815
6816/* sysfs backlight class ----------------------------------------------- */
6817
6818static int brightness_update_status(struct backlight_device *bd)
6819{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006820 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006821 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6822 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006823 bd->props.brightness : 0;
6824
6825 dbg_printk(TPACPI_DBG_BRGHT,
6826 "backlight: attempt to set level to %d\n",
6827 level);
6828
6829 /* it is the backlight class's job (caller) to handle
6830 * EINTR and other errors properly */
6831 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006832}
Holger Macht8acb0252006-10-20 14:30:28 -07006833
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006834static int brightness_get(struct backlight_device *bd)
6835{
6836 int status, res;
6837
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006838 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006839 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006840 return 0;
6841
6842 res = tpacpi_brightness_get_raw(&status);
6843
6844 mutex_unlock(&brightness_mutex);
6845
6846 if (res < 0)
6847 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006848
6849 return status & TP_EC_BACKLIGHT_LVLMSK;
6850}
6851
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006852static void tpacpi_brightness_notify_change(void)
6853{
6854 backlight_force_update(ibm_backlight_device,
6855 BACKLIGHT_UPDATE_HOTKEY);
6856}
6857
Lionel Debrouxacc24722010-11-16 14:14:02 +01006858static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006859 .get_brightness = brightness_get,
6860 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006861};
6862
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006863/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006864
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006865/*
6866 * Call _BCL method of video device. On some ThinkPads this will
6867 * switch the firmware to the ACPI brightness control mode.
6868 */
6869
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006870static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6871{
6872 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6873 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006874 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006875 int rc;
6876
Aaron Lu46445b62013-10-11 21:27:46 +08006877 if (acpi_bus_get_device(handle, &device))
6878 return 0;
6879
6880 rc = 0;
6881 list_for_each_entry(child, &device->children, node) {
6882 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6883 NULL, &buffer);
6884 if (ACPI_FAILURE(status))
6885 continue;
6886
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006887 obj = (union acpi_object *)buffer.pointer;
6888 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006889 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006890 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006891 rc = 0;
6892 } else {
6893 rc = obj->package.count;
6894 }
Aaron Lu46445b62013-10-11 21:27:46 +08006895 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006896 }
6897
6898 kfree(buffer.pointer);
6899 return rc;
6900}
6901
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006902
6903/*
6904 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6905 */
6906static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6907{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006908 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006909 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006910
Aaron Lu46445b62013-10-11 21:27:46 +08006911 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006912 if (video_device)
6913 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006914
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006915 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006916
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006917 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006918}
6919
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006920/*
6921 * These are only useful for models that have only one possibility
6922 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6923 * these quirks.
6924 */
6925#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6926#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6927#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6928
6929static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6930 /* Models with ATI GPUs known to require ECNVRAM mode */
6931 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6932
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006933 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006934 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006935 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006936 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006937 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6938
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006939 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006940 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006941 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6942 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006943
6944 /* Models with Intel GMA900 */
6945 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6946 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6947 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6948};
6949
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006950/*
6951 * Returns < 0 for error, otherwise sets tp_features.bright_*
6952 * and bright_maxlvl.
6953 */
6954static void __init tpacpi_detect_brightness_capabilities(void)
6955{
6956 unsigned int b;
6957
6958 vdbg_printk(TPACPI_DBG_INIT,
6959 "detecting firmware brightness interface capabilities\n");
6960
6961 /* we could run a quirks check here (same table used by
6962 * brightness_init) if needed */
6963
6964 /*
6965 * We always attempt to detect acpi support, so as to switch
6966 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6967 * going to publish a backlight interface
6968 */
6969 b = tpacpi_check_std_acpi_brightness_support();
6970 switch (b) {
6971 case 16:
6972 bright_maxlvl = 15;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006973 break;
6974 case 8:
6975 case 0:
6976 bright_maxlvl = 7;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006977 break;
6978 default:
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006979 tp_features.bright_unkfw = 1;
6980 bright_maxlvl = b - 1;
6981 }
Eric Curtin15c75622016-01-30 16:55:59 +00006982 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006983}
6984
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006985static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006986{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006987 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006988 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006989 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006990
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006991 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6992
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006993 mutex_init(&brightness_mutex);
6994
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006995 quirks = tpacpi_check_quirks(brightness_quirk_table,
6996 ARRAY_SIZE(brightness_quirk_table));
6997
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006998 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006999
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007000 /* if it is unknown, we don't handle it: it wouldn't be safe */
7001 if (tp_features.bright_unkfw)
7002 return 1;
7003
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03007004 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007005 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007006 "brightness support disabled by module parameter\n");
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03007007 return 1;
7008 }
7009
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02007010 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03007011 if (brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007012 pr_info("Standard ACPI backlight interface available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03007013 return 1;
7014 } else if (brightness_enable == 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007015 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 -03007016 return 1;
7017 }
7018 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007019 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03007020 }
7021
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007022 /*
7023 * Check for module parameter bogosity, note that we
7024 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
7025 * able to detect "unspecified"
7026 */
7027 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007028 return -EINVAL;
7029
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007030 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
7031 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
7032 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03007033 if (quirks & TPACPI_BRGHT_Q_EC)
7034 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
7035 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007036 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
7037
7038 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03007039 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007040 brightness_mode);
7041 }
7042
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03007043 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03007044 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03007045 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
7046 brightness_mode == TPACPI_BRGHT_MODE_EC))
7047 return -EINVAL;
7048
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007049 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007050 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02007051
Matthew Garretta19a6ee2010-02-17 16:39:44 -05007052 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07007053 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007054 props.max_brightness = bright_maxlvl;
7055 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05007056 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
7057 NULL, NULL,
7058 &ibm_backlight_data,
7059 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007060 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03007061 int rc = PTR_ERR(ibm_backlight_device);
7062 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07007063 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03007064 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007065 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007066 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7067 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007068
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03007069 if (quirks & TPACPI_BRGHT_Q_ASK) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007070 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
7071 brightness_mode);
7072 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
7073 TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03007074 }
7075
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03007076 /* Added by mistake in early 2007. Probably useless, but it could
7077 * be working around some unknown firmware problem where the value
7078 * read at startup doesn't match the real hardware state... so leave
7079 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02007080 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00007081
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007082 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007083 "brightness: registering brightness hotkeys as change notification\n");
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007084 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7085 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08007086 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007087 return 0;
7088}
7089
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007090static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007091{
7092 tpacpi_brightness_checkpoint_nvram();
7093}
7094
7095static void brightness_shutdown(void)
7096{
7097 tpacpi_brightness_checkpoint_nvram();
7098}
7099
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007100static void brightness_exit(void)
7101{
7102 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007103 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007104 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007105 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007106 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007107
7108 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007109}
7110
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007111static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007112{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007113 int level;
7114
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007115 level = brightness_get(NULL);
7116 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007117 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007118 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007119 seq_printf(m, "level:\t\t%d\n", level);
7120 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007121 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7122 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007123 }
7124
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007125 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007126}
7127
7128static int brightness_write(char *buf)
7129{
7130 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007131 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007132 char *cmd;
7133
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007134 level = brightness_get(NULL);
7135 if (level < 0)
7136 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007137
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007138 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007139 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007140 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007141 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007142 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007143 if (level > 0)
7144 level--;
7145 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007146 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007147 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007148 } else
7149 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007150 }
7151
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007152 tpacpi_disclose_usertask("procfs brightness",
7153 "set level to %d\n", level);
7154
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007155 /*
7156 * Now we know what the final level should be, so we try to set it.
7157 * Doing it this way makes the syscall restartable in case of EINTR
7158 */
7159 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007160 if (!rc && ibm_backlight_device)
7161 backlight_force_update(ibm_backlight_device,
7162 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02007163 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007164}
7165
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007166static struct ibm_struct brightness_driver_data = {
7167 .name = "brightness",
7168 .read = brightness_read,
7169 .write = brightness_write,
7170 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007171 .suspend = brightness_suspend,
7172 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007173};
7174
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007175/*************************************************************************
7176 * Volume subdriver
7177 */
7178
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007179/*
7180 * IBM ThinkPads have a simple volume controller with MUTE gating.
7181 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7182 *
7183 * Since the *61 series (and probably also the later *60 series), Lenovo
7184 * ThinkPads only implement the MUTE gate.
7185 *
7186 * EC register 0x30
7187 * Bit 6: MUTE (1 mutes sound)
7188 * Bit 3-0: Volume
7189 * Other bits should be zero as far as we know.
7190 *
7191 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7192 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7193 * such as bit 7 which is used to detect repeated presses of MUTE,
7194 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007195 *
7196 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7197 * in response to user input. Unfortunately, this rarely works well.
7198 * The laptop changes the state of its internal MUTE gate and, on some
7199 * models, sends KEY_MUTE, causing any user code that responds to the
7200 * mute button to get confused. The hardware MUTE gate is also
7201 * unnecessary, since user code can handle the mute button without
7202 * kernel or EC help.
7203 *
7204 * To avoid confusing userspace, we simply disable all EC-based mute
7205 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007206 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007207
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007208#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7209
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007210#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7211#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7212#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7213
Takashi Iwaicab66612013-10-24 16:06:32 +02007214#if SNDRV_CARDS <= 32
7215#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7216#else
7217#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7218#endif
7219static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007220static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307221static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007222
7223struct tpacpi_alsa_data {
7224 struct snd_card *card;
7225 struct snd_ctl_elem_id *ctl_mute_id;
7226 struct snd_ctl_elem_id *ctl_vol_id;
7227};
7228
7229static struct snd_card *alsa_card;
7230
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007231enum {
7232 TP_EC_AUDIO = 0x30,
7233
7234 /* TP_EC_AUDIO bits */
7235 TP_EC_AUDIO_MUTESW = 6,
7236
7237 /* TP_EC_AUDIO bitmasks */
7238 TP_EC_AUDIO_LVL_MSK = 0x0F,
7239 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7240
7241 /* Maximum volume */
7242 TP_EC_VOLUME_MAX = 14,
7243};
7244
7245enum tpacpi_volume_access_mode {
7246 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7247 TPACPI_VOL_MODE_EC, /* Pure EC control */
7248 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7249 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7250 TPACPI_VOL_MODE_MAX
7251};
7252
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007253enum tpacpi_volume_capabilities {
7254 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7255 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7256 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7257 TPACPI_VOL_CAP_MAX
7258};
7259
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007260enum tpacpi_mute_btn_mode {
7261 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7262 /* We don't know what mode 1 is. */
7263 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7264 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7265};
7266
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007267static enum tpacpi_volume_access_mode volume_mode =
7268 TPACPI_VOL_MODE_MAX;
7269
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007270static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307271static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007272static bool software_mute_requested = true;
7273static bool software_mute_active;
7274static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007275
7276/*
7277 * Used to syncronize writers to TP_EC_AUDIO and
7278 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7279 */
7280static struct mutex volume_mutex;
7281
7282static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007283{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007284 u8 lec = 0;
7285 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007286 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007287
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007288 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7289 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007290 if (!volume_control_allowed)
7291 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007292 if (software_mute_active)
7293 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007294
7295 vdbg_printk(TPACPI_DBG_MIXER,
7296 "trying to checkpoint mixer state to NVRAM...\n");
7297
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007298 if (tp_features.mixer_no_level_control)
7299 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7300 else
7301 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7302
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007303 if (mutex_lock_killable(&volume_mutex) < 0)
7304 return;
7305
7306 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7307 goto unlock;
7308 lec &= ec_mask;
7309 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7310
7311 if (lec != (b_nvram & ec_mask)) {
7312 /* NVRAM needs update */
7313 b_nvram &= ~ec_mask;
7314 b_nvram |= lec;
7315 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7316 dbg_printk(TPACPI_DBG_MIXER,
7317 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7318 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007319 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007320 vdbg_printk(TPACPI_DBG_MIXER,
7321 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7322 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007323 }
7324
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007325unlock:
7326 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007327}
7328
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007329static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007330{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007331 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007332
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007333 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7334 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007335
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007336 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007337
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007338 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007339
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007340 return 0;
7341}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007342
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007343static int volume_get_status(u8 *status)
7344{
7345 return volume_get_status_ec(status);
7346}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007347
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007348static int volume_set_status_ec(const u8 status)
7349{
7350 if (!acpi_ec_write(TP_EC_AUDIO, status))
7351 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007352
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007353 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7354
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007355 /*
7356 * On X200s, and possibly on others, it can take a while for
7357 * reads to become correct.
7358 */
7359 msleep(1);
7360
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007361 return 0;
7362}
7363
7364static int volume_set_status(const u8 status)
7365{
7366 return volume_set_status_ec(status);
7367}
7368
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007369/* returns < 0 on error, 0 on no change, 1 on change */
7370static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007371{
7372 int rc;
7373 u8 s, n;
7374
7375 if (mutex_lock_killable(&volume_mutex) < 0)
7376 return -EINTR;
7377
7378 rc = volume_get_status_ec(&s);
7379 if (rc)
7380 goto unlock;
7381
7382 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7383 s & ~TP_EC_AUDIO_MUTESW_MSK;
7384
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007385 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007386 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007387 if (!rc)
7388 rc = 1;
7389 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007390
7391unlock:
7392 mutex_unlock(&volume_mutex);
7393 return rc;
7394}
7395
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007396static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007397{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007398 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007399 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007400 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007401}
7402
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007403static int volume_set_mute(const bool mute)
7404{
7405 int rc;
7406
7407 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7408 (mute) ? "" : "un");
7409
7410 rc = __volume_set_mute_ec(mute);
7411 return (rc < 0) ? rc : 0;
7412}
7413
7414/* returns < 0 on error, 0 on no change, 1 on change */
7415static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007416{
7417 int rc;
7418 u8 s, n;
7419
7420 if (vol > TP_EC_VOLUME_MAX)
7421 return -EINVAL;
7422
7423 if (mutex_lock_killable(&volume_mutex) < 0)
7424 return -EINTR;
7425
7426 rc = volume_get_status_ec(&s);
7427 if (rc)
7428 goto unlock;
7429
7430 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7431
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007432 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007433 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007434 if (!rc)
7435 rc = 1;
7436 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007437
7438unlock:
7439 mutex_unlock(&volume_mutex);
7440 return rc;
7441}
7442
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007443static int volume_set_software_mute(bool startup)
7444{
7445 int result;
7446
7447 if (!tpacpi_is_lenovo())
7448 return -ENODEV;
7449
7450 if (startup) {
7451 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7452 "HAUM", "qd"))
7453 return -EIO;
7454
7455 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7456 "Initial HAUM setting was %d\n",
7457 software_mute_orig_mode);
7458 }
7459
7460 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7461 (int)TP_EC_MUTE_BTN_NONE))
7462 return -EIO;
7463
7464 if (result != TP_EC_MUTE_BTN_NONE)
7465 pr_warn("Unexpected SAUM result %d\n",
7466 result);
7467
7468 /*
7469 * In software mute mode, the standard codec controls take
7470 * precendence, so we unmute the ThinkPad HW switch at
7471 * startup. Just on case there are SAUM-capable ThinkPads
7472 * with level controls, set max HW volume as well.
7473 */
7474 if (tp_features.mixer_no_level_control)
7475 result = volume_set_mute(false);
7476 else
7477 result = volume_set_status(TP_EC_VOLUME_MAX);
7478
7479 if (result != 0)
7480 pr_warn("Failed to unmute the HW mute switch\n");
7481
7482 return 0;
7483}
7484
7485static void volume_exit_software_mute(void)
7486{
7487 int r;
7488
7489 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7490 || r != software_mute_orig_mode)
7491 pr_warn("Failed to restore mute mode\n");
7492}
7493
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007494static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007495{
7496 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007497 "ALSA: trying to set volume level to %hu\n", vol);
7498 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007499}
7500
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007501static void volume_alsa_notify_change(void)
7502{
7503 struct tpacpi_alsa_data *d;
7504
7505 if (alsa_card && alsa_card->private_data) {
7506 d = alsa_card->private_data;
7507 if (d->ctl_mute_id)
7508 snd_ctl_notify(alsa_card,
7509 SNDRV_CTL_EVENT_MASK_VALUE,
7510 d->ctl_mute_id);
7511 if (d->ctl_vol_id)
7512 snd_ctl_notify(alsa_card,
7513 SNDRV_CTL_EVENT_MASK_VALUE,
7514 d->ctl_vol_id);
7515 }
7516}
7517
7518static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7519 struct snd_ctl_elem_info *uinfo)
7520{
7521 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7522 uinfo->count = 1;
7523 uinfo->value.integer.min = 0;
7524 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7525 return 0;
7526}
7527
7528static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7529 struct snd_ctl_elem_value *ucontrol)
7530{
7531 u8 s;
7532 int rc;
7533
7534 rc = volume_get_status(&s);
7535 if (rc < 0)
7536 return rc;
7537
7538 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7539 return 0;
7540}
7541
7542static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7543 struct snd_ctl_elem_value *ucontrol)
7544{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007545 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7546 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007547 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007548}
7549
7550#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7551
7552static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7553 struct snd_ctl_elem_value *ucontrol)
7554{
7555 u8 s;
7556 int rc;
7557
7558 rc = volume_get_status(&s);
7559 if (rc < 0)
7560 return rc;
7561
7562 ucontrol->value.integer.value[0] =
7563 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7564 return 0;
7565}
7566
7567static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7568 struct snd_ctl_elem_value *ucontrol)
7569{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007570 tpacpi_disclose_usertask("ALSA", "%smute\n",
7571 ucontrol->value.integer.value[0] ?
7572 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007573 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007574}
7575
Mathias Krausec3aa4722014-07-16 19:43:17 +02007576static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007577 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7578 .name = "Console Playback Volume",
7579 .index = 0,
7580 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7581 .info = volume_alsa_vol_info,
7582 .get = volume_alsa_vol_get,
7583};
7584
Mathias Krausec3aa4722014-07-16 19:43:17 +02007585static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007586 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7587 .name = "Console Playback Switch",
7588 .index = 0,
7589 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7590 .info = volume_alsa_mute_info,
7591 .get = volume_alsa_mute_get,
7592};
7593
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007594static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007595{
7596 tpacpi_volume_checkpoint_nvram();
7597}
7598
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007599static void volume_resume(void)
7600{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007601 if (software_mute_active) {
7602 if (volume_set_software_mute(false) < 0)
7603 pr_warn("Failed to restore software mute\n");
7604 } else {
7605 volume_alsa_notify_change();
7606 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007607}
7608
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007609static void volume_shutdown(void)
7610{
7611 tpacpi_volume_checkpoint_nvram();
7612}
7613
7614static void volume_exit(void)
7615{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007616 if (alsa_card) {
7617 snd_card_free(alsa_card);
7618 alsa_card = NULL;
7619 }
7620
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007621 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007622
7623 if (software_mute_active)
7624 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007625}
7626
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007627static int __init volume_create_alsa_mixer(void)
7628{
7629 struct snd_card *card;
7630 struct tpacpi_alsa_data *data;
7631 struct snd_kcontrol *ctl_vol;
7632 struct snd_kcontrol *ctl_mute;
7633 int rc;
7634
Takashi Iwai89235e52014-01-29 15:01:27 +01007635 rc = snd_card_new(&tpacpi_pdev->dev,
7636 alsa_index, alsa_id, THIS_MODULE,
7637 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007638 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007639 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007640 return 1;
7641 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007642
7643 BUG_ON(!card->private_data);
7644 data = card->private_data;
7645 data->card = card;
7646
7647 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7648 sizeof(card->driver));
7649 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7650 sizeof(card->shortname));
7651 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7652 (thinkpad_id.ec_version_str) ?
7653 thinkpad_id.ec_version_str : "(unknown)");
7654 snprintf(card->longname, sizeof(card->longname),
7655 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7656 (thinkpad_id.ec_version_str) ?
7657 thinkpad_id.ec_version_str : "unknown");
7658
7659 if (volume_control_allowed) {
7660 volume_alsa_control_vol.put = volume_alsa_vol_put;
7661 volume_alsa_control_vol.access =
7662 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7663
7664 volume_alsa_control_mute.put = volume_alsa_mute_put;
7665 volume_alsa_control_mute.access =
7666 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7667 }
7668
7669 if (!tp_features.mixer_no_level_control) {
7670 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7671 rc = snd_ctl_add(card, ctl_vol);
7672 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007673 pr_err("Failed to create ALSA volume control: %d\n",
7674 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007675 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007676 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007677 data->ctl_vol_id = &ctl_vol->id;
7678 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007679
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007680 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7681 rc = snd_ctl_add(card, ctl_mute);
7682 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007683 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007684 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007685 }
7686 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007687
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007688 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007689 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007690 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007691 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007692 }
7693
7694 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007695 return 0;
7696
7697err_exit:
7698 snd_card_free(card);
7699 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007700}
7701
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007702#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7703#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7704
7705static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7706 /* Whitelist volume level on all IBM by default */
7707 { .vendor = PCI_VENDOR_ID_IBM,
7708 .bios = TPACPI_MATCH_ANY,
7709 .ec = TPACPI_MATCH_ANY,
7710 .quirks = TPACPI_VOL_Q_LEVEL },
7711
7712 /* Lenovo models with volume control (needs confirmation) */
7713 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7714 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7715 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7716 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7717 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7718 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7719 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7720
7721 /* Whitelist mute-only on all Lenovo by default */
7722 { .vendor = PCI_VENDOR_ID_LENOVO,
7723 .bios = TPACPI_MATCH_ANY,
7724 .ec = TPACPI_MATCH_ANY,
7725 .quirks = TPACPI_VOL_Q_MUTEONLY }
7726};
7727
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007728static int __init volume_init(struct ibm_init_struct *iibm)
7729{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007730 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007731 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007732
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007733 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7734
7735 mutex_init(&volume_mutex);
7736
7737 /*
7738 * Check for module parameter bogosity, note that we
7739 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7740 * able to detect "unspecified"
7741 */
7742 if (volume_mode > TPACPI_VOL_MODE_MAX)
7743 return -EINVAL;
7744
7745 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007746 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7747 TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007748 return 1;
7749 }
7750
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007751 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7752 return -EINVAL;
7753
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007754 /*
7755 * The ALSA mixer is our primary interface.
7756 * When disabled, don't install the subdriver at all
7757 */
7758 if (!alsa_enable) {
7759 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007760 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007761 return 1;
7762 }
7763
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007764 quirks = tpacpi_check_quirks(volume_quirk_table,
7765 ARRAY_SIZE(volume_quirk_table));
7766
7767 switch (volume_capabilities) {
7768 case TPACPI_VOL_CAP_AUTO:
7769 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7770 tp_features.mixer_no_level_control = 1;
7771 else if (quirks & TPACPI_VOL_Q_LEVEL)
7772 tp_features.mixer_no_level_control = 0;
7773 else
7774 return 1; /* no mixer */
7775 break;
7776 case TPACPI_VOL_CAP_VOLMUTE:
7777 tp_features.mixer_no_level_control = 0;
7778 break;
7779 case TPACPI_VOL_CAP_MUTEONLY:
7780 tp_features.mixer_no_level_control = 1;
7781 break;
7782 default:
7783 return 1;
7784 }
7785
7786 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7787 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7788 "using user-supplied volume_capabilities=%d\n",
7789 volume_capabilities);
7790
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007791 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7792 volume_mode == TPACPI_VOL_MODE_MAX) {
7793 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7794
7795 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7796 "driver auto-selected volume_mode=%d\n",
7797 volume_mode);
7798 } else {
7799 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7800 "using user-supplied volume_mode=%d\n",
7801 volume_mode);
7802 }
7803
7804 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007805 "mute is supported, volume control is %s\n",
7806 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007807
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007808 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7809 software_mute_active = true;
7810 } else {
7811 rc = volume_create_alsa_mixer();
7812 if (rc) {
7813 pr_err("Could not create the ALSA mixer interface\n");
7814 return rc;
7815 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007816
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007817 pr_info("Console audio control enabled, mode: %s\n",
7818 (volume_control_allowed) ?
7819 "override (read/write)" :
7820 "monitor (read only)");
7821 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007822
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007823 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7824 "registering volume hotkeys as change notification\n");
7825 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7826 | TP_ACPI_HKEY_VOLUP_MASK
7827 | TP_ACPI_HKEY_VOLDWN_MASK
7828 | TP_ACPI_HKEY_MUTE_MASK);
7829
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007830 return 0;
7831}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007832
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007833static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007834{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007835 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007836
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007837 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007838 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007839 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007840 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007841 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007842 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007843 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007844 status & TP_EC_AUDIO_LVL_MSK);
7845
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007846 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007847 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007848
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007849 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007850 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007851 if (!tp_features.mixer_no_level_control) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007852 seq_printf(m, "commands:\tup, down\n");
7853 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7854 TP_EC_VOLUME_MAX);
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007855 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007856 }
7857 }
7858
7859 return 0;
7860}
7861
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007862static int volume_write(char *buf)
7863{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007864 u8 s;
7865 u8 new_level, new_mute;
7866 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007867 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007868 int rc;
7869
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007870 /*
7871 * We do allow volume control at driver startup, so that the
7872 * user can set initial state through the volume=... parameter hack.
7873 */
7874 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7875 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7876 tp_warned.volume_ctrl_forbidden = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007877 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7878 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007879 }
7880 return -EPERM;
7881 }
7882
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007883 rc = volume_get_status(&s);
7884 if (rc < 0)
7885 return rc;
7886
7887 new_level = s & TP_EC_AUDIO_LVL_MSK;
7888 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007889
7890 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007891 if (!tp_features.mixer_no_level_control) {
7892 if (strlencmp(cmd, "up") == 0) {
7893 if (new_mute)
7894 new_mute = 0;
7895 else if (new_level < TP_EC_VOLUME_MAX)
7896 new_level++;
7897 continue;
7898 } else if (strlencmp(cmd, "down") == 0) {
7899 if (new_mute)
7900 new_mute = 0;
7901 else if (new_level > 0)
7902 new_level--;
7903 continue;
7904 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7905 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7906 new_level = l;
7907 continue;
7908 }
7909 }
7910 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007911 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007912 else if (strlencmp(cmd, "unmute") == 0)
7913 new_mute = 0;
7914 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007915 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007916 }
7917
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007918 if (tp_features.mixer_no_level_control) {
7919 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7920 new_mute ? "" : "un");
7921 rc = volume_set_mute(!!new_mute);
7922 } else {
7923 tpacpi_disclose_usertask("procfs volume",
7924 "%smute and set level to %d\n",
7925 new_mute ? "" : "un", new_level);
7926 rc = volume_set_status(new_mute | new_level);
7927 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007928 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007929
7930 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007931}
7932
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007933static struct ibm_struct volume_driver_data = {
7934 .name = "volume",
7935 .read = volume_read,
7936 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007937 .exit = volume_exit,
7938 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007939 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007940 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007941};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007942
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007943#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7944
7945#define alsa_card NULL
7946
Tobias Klauserbb55a2e2016-11-25 14:18:07 +01007947static inline void volume_alsa_notify_change(void)
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007948{
7949}
7950
7951static int __init volume_init(struct ibm_init_struct *iibm)
7952{
Joe Perches0978e012011-04-04 10:06:25 -07007953 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007954
7955 return 1;
7956}
7957
7958static struct ibm_struct volume_driver_data = {
7959 .name = "volume",
7960};
7961
7962#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7963
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007964/*************************************************************************
7965 * Fan subdriver
7966 */
7967
7968/*
7969 * FAN ACCESS MODES
7970 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007971 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007972 * ACPI GFAN method: returns fan level
7973 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007974 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007975 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007976 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007977 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007978 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7979 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007980 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7981 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007982 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007983 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007984 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7985 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007986 *
7987 * Fan speed changes of any sort (including those caused by the
7988 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007989 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007990 * limited.
7991 *
7992 * Reading is not available if GFAN exists.
7993 * Writing is not available if SFAN exists.
7994 *
7995 * Bits
7996 * 7 automatic mode engaged;
7997 * (default operation mode of the ThinkPad)
7998 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007999 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008000 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03008001 * the tachometer while the fan controller ramps up
8002 * the speed (which can take up to a few *minutes*).
8003 * Speeds up fan to 100% duty-cycle, which is far above
8004 * the standard RPM levels. It is not impossible that
8005 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008006 * 5-3 unused in some models. Extra bits for fan level
8007 * in others, but still useless as all values above
8008 * 7 map to the same speed as level 7 in these models.
8009 * 2-0 fan level (0..7 usually)
8010 * 0x00 = stop
8011 * 0x07 = max (set when temperatures critical)
8012 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008013 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008014 *
8015 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04008016 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008017 * does so, its initial value is meaningless (0x07).
8018 *
8019 * For firmware bugs, refer to:
8020 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
8021 *
8022 * ----
8023 *
8024 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
8025 * Main fan tachometer reading (in RPM)
8026 *
8027 * This register is present on all ThinkPads with a new-style EC, and
8028 * it is known not to be present on the A21m/e, and T22, as there is
8029 * something else in offset 0x84 according to the ACPI DSDT. Other
8030 * ThinkPads from this same time period (and earlier) probably lack the
8031 * tachometer as well.
8032 *
Nick Andrew877d0312009-01-26 11:06:57 +01008033 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008034 * was never fixed by IBM to report the EC firmware version string
8035 * probably support the tachometer (like the early X models), so
8036 * detecting it is quite hard. We need more data to know for sure.
8037 *
8038 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
8039 * might result.
8040 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03008041 * FIRMWARE BUG: may go stale while the EC is switching to full speed
8042 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008043 *
8044 * For firmware bugs, refer to:
8045 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
8046 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008047 * ----
8048 *
8049 * ThinkPad EC register 0x31 bit 0 (only on select models)
8050 *
8051 * When bit 0 of EC register 0x31 is zero, the tachometer registers
8052 * show the speed of the main fan. When bit 0 of EC register 0x31
8053 * is one, the tachometer registers show the speed of the auxiliary
8054 * fan.
8055 *
8056 * Fan control seems to affect both fans, regardless of the state
8057 * of this bit.
8058 *
8059 * So far, only the firmware for the X60/X61 non-tablet versions
8060 * seem to support this (firmware TP-7M).
8061 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008062 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008063 * ThinkPad X31, X40, X41. Not available in the X60.
8064 *
8065 * FANS ACPI handle: takes three arguments: low speed, medium speed,
8066 * high speed. ACPI DSDT seems to map these three speeds to levels
8067 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
8068 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
8069 *
8070 * The speeds are stored on handles
8071 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
8072 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008073 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008074 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
8075 *
8076 * ACPI DSDT switches which set is in use depending on various
8077 * factors.
8078 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008079 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008080 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
8081 * but the ACPI tables just mention level 7.
8082 */
8083
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008084enum { /* Fan control constants */
8085 fan_status_offset = 0x2f, /* EC register 0x2f */
8086 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
8087 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008088 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
8089 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008090
8091 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
8092 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
8093
8094 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
8095};
8096
8097enum fan_status_access_mode {
8098 TPACPI_FAN_NONE = 0, /* No fan status or control */
8099 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
8100 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8101};
8102
8103enum fan_control_access_mode {
8104 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8105 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8106 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8107 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8108};
8109
8110enum fan_control_commands {
8111 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8112 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8113 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8114 * and also watchdog cmd */
8115};
8116
Rusty Russell90ab5ee2012-01-13 09:32:20 +10308117static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008118
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008119static enum fan_status_access_mode fan_status_access_mode;
8120static enum fan_control_access_mode fan_control_access_mode;
8121static enum fan_control_commands fan_control_commands;
8122
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008123static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008124static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008125static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008126static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008127
8128static struct mutex fan_mutex;
8129
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008130static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05008131static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008132
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008133TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8134TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008135 "\\FSPD", /* 600e/x, 770e, 770x */
8136 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008137TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008138 "JFNS", /* 770x-JL */
8139 ); /* all others */
8140
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008141/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008142 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8143 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8144 * be in auto mode (0x80).
8145 *
8146 * This is corrected by any write to HFSP either by the driver, or
8147 * by the firmware.
8148 *
8149 * We assume 0x07 really means auto mode while this quirk is active,
8150 * as this is far more likely than the ThinkPad being in level 7,
8151 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008152 *
8153 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8154 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008155 */
8156
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008157static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008158{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008159 if (fan_control_initial_status == 0x07) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008160 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 -03008161 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008162 }
8163}
8164
8165static void fan_quirk1_handle(u8 *fan_status)
8166{
8167 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8168 if (*fan_status != fan_control_initial_status) {
8169 /* something changed the HFSP regisnter since
8170 * driver init time, so it is not undefined
8171 * anymore */
8172 tp_features.fan_ctrl_status_undef = 0;
8173 } else {
8174 /* Return most likely status. In fact, it
8175 * might be the only possible status */
8176 *fan_status = TP_EC_FAN_AUTO;
8177 }
8178 }
8179}
8180
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008181/* Select main fan on X60/X61, NOOP on others */
8182static bool fan_select_fan1(void)
8183{
8184 if (tp_features.second_fan) {
8185 u8 val;
8186
8187 if (ec_read(fan_select_offset, &val) < 0)
8188 return false;
8189 val &= 0xFEU;
8190 if (ec_write(fan_select_offset, val) < 0)
8191 return false;
8192 }
8193 return true;
8194}
8195
8196/* Select secondary fan on X60/X61 */
8197static bool fan_select_fan2(void)
8198{
8199 u8 val;
8200
8201 if (!tp_features.second_fan)
8202 return false;
8203
8204 if (ec_read(fan_select_offset, &val) < 0)
8205 return false;
8206 val |= 0x01U;
8207 if (ec_write(fan_select_offset, val) < 0)
8208 return false;
8209
8210 return true;
8211}
8212
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008213/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008214 * Call with fan_mutex held
8215 */
8216static void fan_update_desired_level(u8 status)
8217{
8218 if ((status &
8219 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8220 if (status > 7)
8221 fan_control_desired_level = 7;
8222 else
8223 fan_control_desired_level = status;
8224 }
8225}
8226
8227static int fan_get_status(u8 *status)
8228{
8229 u8 s;
8230
8231 /* TODO:
8232 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8233
8234 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07008235 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008236 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07008237 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008238
Dan Carpentereceeb432012-09-01 12:54:07 -07008239 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008240 return -EIO;
8241
8242 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07008243 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008244
8245 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07008246 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008247 case TPACPI_FAN_RD_TPEC:
8248 /* all except 570, 600e/x, 770e, 770x */
8249 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8250 return -EIO;
8251
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008252 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008253 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008254 fan_quirk1_handle(status);
8255 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008256
8257 break;
8258
8259 default:
8260 return -ENXIO;
8261 }
8262
8263 return 0;
8264}
8265
8266static int fan_get_status_safe(u8 *status)
8267{
8268 int rc;
8269 u8 s;
8270
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008271 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008272 return -ERESTARTSYS;
8273 rc = fan_get_status(&s);
8274 if (!rc)
8275 fan_update_desired_level(s);
8276 mutex_unlock(&fan_mutex);
8277
Dan Carpentera7718362016-04-15 17:46:34 +03008278 if (rc)
8279 return rc;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008280 if (status)
8281 *status = s;
8282
Dan Carpentera7718362016-04-15 17:46:34 +03008283 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008284}
8285
8286static int fan_get_speed(unsigned int *speed)
8287{
8288 u8 hi, lo;
8289
8290 switch (fan_status_access_mode) {
8291 case TPACPI_FAN_RD_TPEC:
8292 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008293 if (unlikely(!fan_select_fan1()))
8294 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008295 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8296 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8297 return -EIO;
8298
8299 if (likely(speed))
8300 *speed = (hi << 8) | lo;
8301
8302 break;
8303
8304 default:
8305 return -ENXIO;
8306 }
8307
8308 return 0;
8309}
8310
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008311static int fan2_get_speed(unsigned int *speed)
8312{
8313 u8 hi, lo;
8314 bool rc;
8315
8316 switch (fan_status_access_mode) {
8317 case TPACPI_FAN_RD_TPEC:
8318 /* all except 570, 600e/x, 770e, 770x */
8319 if (unlikely(!fan_select_fan2()))
8320 return -EIO;
8321 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8322 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8323 fan_select_fan1(); /* play it safe */
8324 if (rc)
8325 return -EIO;
8326
8327 if (likely(speed))
8328 *speed = (hi << 8) | lo;
8329
8330 break;
8331
8332 default:
8333 return -ENXIO;
8334 }
8335
8336 return 0;
8337}
8338
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008339static int fan_set_level(int level)
8340{
8341 if (!fan_control_allowed)
8342 return -EPERM;
8343
8344 switch (fan_control_access_mode) {
8345 case TPACPI_FAN_WR_ACPI_SFAN:
8346 if (level >= 0 && level <= 7) {
8347 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8348 return -EIO;
8349 } else
8350 return -EINVAL;
8351 break;
8352
8353 case TPACPI_FAN_WR_ACPI_FANS:
8354 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008355 if (!(level & TP_EC_FAN_AUTO) &&
8356 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008357 ((level < 0) || (level > 7)))
8358 return -EINVAL;
8359
8360 /* safety net should the EC not support AUTO
8361 * or FULLSPEED mode bits and just ignore them */
8362 if (level & TP_EC_FAN_FULLSPEED)
8363 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01008364 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008365 level |= 4; /* safety min speed 4 */
8366
8367 if (!acpi_ec_write(fan_status_offset, level))
8368 return -EIO;
8369 else
8370 tp_features.fan_ctrl_status_undef = 0;
8371 break;
8372
8373 default:
8374 return -ENXIO;
8375 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008376
8377 vdbg_printk(TPACPI_DBG_FAN,
8378 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008379 return 0;
8380}
8381
8382static int fan_set_level_safe(int level)
8383{
8384 int rc;
8385
8386 if (!fan_control_allowed)
8387 return -EPERM;
8388
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008389 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008390 return -ERESTARTSYS;
8391
8392 if (level == TPACPI_FAN_LAST_LEVEL)
8393 level = fan_control_desired_level;
8394
8395 rc = fan_set_level(level);
8396 if (!rc)
8397 fan_update_desired_level(level);
8398
8399 mutex_unlock(&fan_mutex);
8400 return rc;
8401}
8402
8403static int fan_set_enable(void)
8404{
8405 u8 s;
8406 int rc;
8407
8408 if (!fan_control_allowed)
8409 return -EPERM;
8410
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008411 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008412 return -ERESTARTSYS;
8413
8414 switch (fan_control_access_mode) {
8415 case TPACPI_FAN_WR_ACPI_FANS:
8416 case TPACPI_FAN_WR_TPEC:
8417 rc = fan_get_status(&s);
8418 if (rc < 0)
8419 break;
8420
8421 /* Don't go out of emergency fan mode */
8422 if (s != 7) {
8423 s &= 0x07;
8424 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8425 }
8426
8427 if (!acpi_ec_write(fan_status_offset, s))
8428 rc = -EIO;
8429 else {
8430 tp_features.fan_ctrl_status_undef = 0;
8431 rc = 0;
8432 }
8433 break;
8434
8435 case TPACPI_FAN_WR_ACPI_SFAN:
8436 rc = fan_get_status(&s);
8437 if (rc < 0)
8438 break;
8439
8440 s &= 0x07;
8441
8442 /* Set fan to at least level 4 */
8443 s |= 4;
8444
8445 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008446 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008447 else
8448 rc = 0;
8449 break;
8450
8451 default:
8452 rc = -ENXIO;
8453 }
8454
8455 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008456
8457 if (!rc)
8458 vdbg_printk(TPACPI_DBG_FAN,
8459 "fan control: set fan control register to 0x%02x\n",
8460 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008461 return rc;
8462}
8463
8464static int fan_set_disable(void)
8465{
8466 int rc;
8467
8468 if (!fan_control_allowed)
8469 return -EPERM;
8470
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008471 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008472 return -ERESTARTSYS;
8473
8474 rc = 0;
8475 switch (fan_control_access_mode) {
8476 case TPACPI_FAN_WR_ACPI_FANS:
8477 case TPACPI_FAN_WR_TPEC:
8478 if (!acpi_ec_write(fan_status_offset, 0x00))
8479 rc = -EIO;
8480 else {
8481 fan_control_desired_level = 0;
8482 tp_features.fan_ctrl_status_undef = 0;
8483 }
8484 break;
8485
8486 case TPACPI_FAN_WR_ACPI_SFAN:
8487 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8488 rc = -EIO;
8489 else
8490 fan_control_desired_level = 0;
8491 break;
8492
8493 default:
8494 rc = -ENXIO;
8495 }
8496
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008497 if (!rc)
8498 vdbg_printk(TPACPI_DBG_FAN,
8499 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008500
8501 mutex_unlock(&fan_mutex);
8502 return rc;
8503}
8504
8505static int fan_set_speed(int speed)
8506{
8507 int rc;
8508
8509 if (!fan_control_allowed)
8510 return -EPERM;
8511
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008512 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008513 return -ERESTARTSYS;
8514
8515 rc = 0;
8516 switch (fan_control_access_mode) {
8517 case TPACPI_FAN_WR_ACPI_FANS:
8518 if (speed >= 0 && speed <= 65535) {
8519 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8520 speed, speed, speed))
8521 rc = -EIO;
8522 } else
8523 rc = -EINVAL;
8524 break;
8525
8526 default:
8527 rc = -ENXIO;
8528 }
8529
8530 mutex_unlock(&fan_mutex);
8531 return rc;
8532}
8533
8534static void fan_watchdog_reset(void)
8535{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008536 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8537 return;
8538
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008539 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008540 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8541 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8542 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8543 else
8544 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008545}
8546
8547static void fan_watchdog_fire(struct work_struct *ignored)
8548{
8549 int rc;
8550
8551 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8552 return;
8553
Joe Perches0978e012011-04-04 10:06:25 -07008554 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008555 rc = fan_set_enable();
8556 if (rc < 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008557 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8558 rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008559 /* reschedule for later */
8560 fan_watchdog_reset();
8561 }
8562}
8563
8564/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008565 * SYSFS fan layout: hwmon compatible (device)
8566 *
8567 * pwm*_enable:
8568 * 0: "disengaged" mode
8569 * 1: manual mode
8570 * 2: native EC "auto" mode (recommended, hardware default)
8571 *
8572 * pwm*: set speed in manual mode, ignored otherwise.
8573 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8574 * interpolation.
8575 *
8576 * fan*_input: tachometer reading, RPM
8577 *
8578 *
8579 * SYSFS fan layout: extensions
8580 *
8581 * fan_watchdog (driver):
8582 * fan watchdog interval in seconds, 0 disables (default), max 120
8583 */
8584
8585/* sysfs fan pwm1_enable ----------------------------------------------- */
8586static ssize_t fan_pwm1_enable_show(struct device *dev,
8587 struct device_attribute *attr,
8588 char *buf)
8589{
8590 int res, mode;
8591 u8 status;
8592
8593 res = fan_get_status_safe(&status);
8594 if (res)
8595 return res;
8596
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008597 if (status & TP_EC_FAN_FULLSPEED) {
8598 mode = 0;
8599 } else if (status & TP_EC_FAN_AUTO) {
8600 mode = 2;
8601 } else
8602 mode = 1;
8603
8604 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8605}
8606
8607static ssize_t fan_pwm1_enable_store(struct device *dev,
8608 struct device_attribute *attr,
8609 const char *buf, size_t count)
8610{
8611 unsigned long t;
8612 int res, level;
8613
8614 if (parse_strtoul(buf, 2, &t))
8615 return -EINVAL;
8616
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008617 tpacpi_disclose_usertask("hwmon pwm1_enable",
8618 "set fan mode to %lu\n", t);
8619
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008620 switch (t) {
8621 case 0:
8622 level = TP_EC_FAN_FULLSPEED;
8623 break;
8624 case 1:
8625 level = TPACPI_FAN_LAST_LEVEL;
8626 break;
8627 case 2:
8628 level = TP_EC_FAN_AUTO;
8629 break;
8630 case 3:
8631 /* reserved for software-controlled auto mode */
8632 return -ENOSYS;
8633 default:
8634 return -EINVAL;
8635 }
8636
8637 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008638 if (res == -ENXIO)
8639 return -EINVAL;
8640 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008641 return res;
8642
8643 fan_watchdog_reset();
8644
8645 return count;
8646}
8647
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008648static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8649 fan_pwm1_enable_show, fan_pwm1_enable_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008650
8651/* sysfs fan pwm1 ------------------------------------------------------ */
8652static ssize_t fan_pwm1_show(struct device *dev,
8653 struct device_attribute *attr,
8654 char *buf)
8655{
8656 int res;
8657 u8 status;
8658
8659 res = fan_get_status_safe(&status);
8660 if (res)
8661 return res;
8662
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008663 if ((status &
8664 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8665 status = fan_control_desired_level;
8666
8667 if (status > 7)
8668 status = 7;
8669
8670 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8671}
8672
8673static ssize_t fan_pwm1_store(struct device *dev,
8674 struct device_attribute *attr,
8675 const char *buf, size_t count)
8676{
8677 unsigned long s;
8678 int rc;
8679 u8 status, newlevel;
8680
8681 if (parse_strtoul(buf, 255, &s))
8682 return -EINVAL;
8683
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008684 tpacpi_disclose_usertask("hwmon pwm1",
8685 "set fan speed to %lu\n", s);
8686
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008687 /* scale down from 0-255 to 0-7 */
8688 newlevel = (s >> 5) & 0x07;
8689
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008690 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008691 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008692
8693 rc = fan_get_status(&status);
8694 if (!rc && (status &
8695 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8696 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008697 if (rc == -ENXIO)
8698 rc = -EINVAL;
8699 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008700 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008701 fan_watchdog_reset();
8702 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008703 }
8704
8705 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008706 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008707}
8708
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008709static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008710
8711/* sysfs fan fan1_input ------------------------------------------------ */
8712static ssize_t fan_fan1_input_show(struct device *dev,
8713 struct device_attribute *attr,
8714 char *buf)
8715{
8716 int res;
8717 unsigned int speed;
8718
8719 res = fan_get_speed(&speed);
8720 if (res < 0)
8721 return res;
8722
8723 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8724}
8725
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008726static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008727
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008728/* sysfs fan fan2_input ------------------------------------------------ */
8729static ssize_t fan_fan2_input_show(struct device *dev,
8730 struct device_attribute *attr,
8731 char *buf)
8732{
8733 int res;
8734 unsigned int speed;
8735
8736 res = fan2_get_speed(&speed);
8737 if (res < 0)
8738 return res;
8739
8740 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8741}
8742
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008743static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008744
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008745/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008746static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008747{
8748 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8749}
8750
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008751static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8752 size_t count)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008753{
8754 unsigned long t;
8755
8756 if (parse_strtoul(buf, 120, &t))
8757 return -EINVAL;
8758
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008759 if (!fan_control_allowed)
8760 return -EPERM;
8761
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008762 fan_watchdog_maxinterval = t;
8763 fan_watchdog_reset();
8764
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008765 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8766
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008767 return count;
8768}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008769static DRIVER_ATTR_RW(fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008770
8771/* --------------------------------------------------------------------- */
8772static struct attribute *fan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008773 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8774 &dev_attr_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008775 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008776 NULL
8777};
8778
8779static const struct attribute_group fan_attr_group = {
8780 .attrs = fan_attributes,
8781};
8782
Jouke Witteveen599eeff2018-12-05 12:17:52 +01008783#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008784#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008785
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008786static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
Jouke Witteveen599eeff2018-12-05 12:17:52 +01008787 TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
8788 TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
8789 TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
8790 TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
8791 TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
8792 TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008793};
8794
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008795static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008796{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008797 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008798 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008799
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008800 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8801 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008802
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008803 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008804 fan_status_access_mode = TPACPI_FAN_NONE;
8805 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008806 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008807 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008808 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008809 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008810 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008811
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008812 if (tpacpi_is_ibm()) {
8813 TPACPI_ACPIHANDLE_INIT(fans);
8814 TPACPI_ACPIHANDLE_INIT(gfan);
8815 TPACPI_ACPIHANDLE_INIT(sfan);
8816 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008817
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008818 quirks = tpacpi_check_quirks(fan_quirk_table,
8819 ARRAY_SIZE(fan_quirk_table));
8820
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008821 if (gfan_handle) {
8822 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008823 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008824 } else {
8825 /* all other ThinkPads: note that even old-style
8826 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008827 if (likely(acpi_ec_read(fan_status_offset,
8828 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008829 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008830 if (quirks & TPACPI_FAN_Q1)
8831 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008832 if (quirks & TPACPI_FAN_2FAN) {
8833 tp_features.second_fan = 1;
8834 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8835 "secondary fan support enabled\n");
8836 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008837 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008838 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008839 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008840 }
8841 }
8842
8843 if (sfan_handle) {
8844 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008845 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008846 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008847 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008848 } else {
8849 if (!gfan_handle) {
8850 /* gfan without sfan means no fan control */
8851 /* all other models implement TP EC 0x2f control */
8852
8853 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008854 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008855 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008856 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008857 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008858 TPACPI_FAN_CMD_SPEED |
8859 TPACPI_FAN_CMD_LEVEL |
8860 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008861 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008862 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008863 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008864 TPACPI_FAN_CMD_LEVEL |
8865 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008866 }
8867 }
8868 }
8869
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008870 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8871 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008872 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8873 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8874 fan_status_access_mode, fan_control_access_mode);
8875
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008876 /* fan control master switch */
8877 if (!fan_control_allowed) {
8878 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8879 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008880 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008881 "fan control features disabled by parameter\n");
8882 }
8883
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008884 /* update fan_control_desired_level */
8885 if (fan_status_access_mode != TPACPI_FAN_NONE)
8886 fan_get_status_safe(NULL);
8887
8888 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8889 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008890 if (tp_features.second_fan) {
8891 /* attach second fan tachometer */
8892 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008893 &dev_attr_fan2_input.attr;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008894 }
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008895 rc = sysfs_create_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008896 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008897 if (rc < 0)
8898 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008899
8900 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8901 &driver_attr_fan_watchdog);
8902 if (rc < 0) {
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008903 sysfs_remove_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008904 &fan_attr_group);
8905 return rc;
8906 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008907 return 0;
8908 } else
8909 return 1;
8910}
8911
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008912static void fan_exit(void)
8913{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008914 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008915 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008916
8917 /* FIXME: can we really do this unconditionally? */
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008918 sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008919 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8920 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008921
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008922 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008923 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008924}
8925
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008926static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008927{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008928 int rc;
8929
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008930 if (!fan_control_allowed)
8931 return;
8932
8933 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008934 fan_control_resume_level = 0;
8935 rc = fan_get_status_safe(&fan_control_resume_level);
8936 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008937 pr_notice("failed to read fan level for later restore during resume: %d\n",
8938 rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008939
8940 /* if it is undefined, don't attempt to restore it.
8941 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008942 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008943 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008944}
8945
8946static void fan_resume(void)
8947{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008948 u8 current_level = 7;
8949 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008950 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008951
8952 /* DSDT *always* updates status on resume */
8953 tp_features.fan_ctrl_status_undef = 0;
8954
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008955 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008956 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008957 (fan_get_status_safe(&current_level) < 0))
8958 return;
8959
8960 switch (fan_control_access_mode) {
8961 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008962 /* never decrease fan level */
8963 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008964 break;
8965 case TPACPI_FAN_WR_ACPI_FANS:
8966 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008967 /* never decrease fan level, scale is:
8968 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8969 *
8970 * We expect the firmware to set either 7 or AUTO, but we
8971 * handle FULLSPEED out of paranoia.
8972 *
8973 * So, we can safely only restore FULLSPEED or 7, anything
8974 * else could slow the fan. Restoring AUTO is useless, at
8975 * best that's exactly what the DSDT already set (it is the
8976 * slower it uses).
8977 *
8978 * Always keep in mind that the DSDT *will* have set the
8979 * fans to what the vendor supposes is the best level. We
8980 * muck with it only to speed the fan up.
8981 */
8982 if (fan_control_resume_level != 7 &&
8983 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8984 return;
8985 else
8986 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8987 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008988 break;
8989 default:
8990 return;
8991 }
8992 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008993 pr_notice("restoring fan level to 0x%02x\n",
8994 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008995 rc = fan_set_level_safe(fan_control_resume_level);
8996 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008997 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008998 }
8999}
9000
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009001static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009002{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009003 int rc;
9004 u8 status;
9005 unsigned int speed = 0;
9006
9007 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009008 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009009 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009010 rc = fan_get_status_safe(&status);
9011 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009012 return rc;
9013
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009014 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009015 "level:\t\t%d\n",
9016 (status != 0) ? "enabled" : "disabled", status);
9017 break;
9018
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009019 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009020 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009021 rc = fan_get_status_safe(&status);
9022 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009023 return rc;
9024
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009025 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009026 (status != 0) ? "enabled" : "disabled");
9027
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009028 rc = fan_get_speed(&speed);
9029 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009030 return rc;
9031
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009032 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009033
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009034 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009035 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009036 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009037 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009038 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009039 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009040 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009041 break;
9042
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009043 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009044 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009045 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009046 }
9047
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009048 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009049 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009050
9051 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009052 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009053 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009054 break;
9055
9056 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009057 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009058 break;
9059 }
9060 }
9061
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009062 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009063 seq_printf(m, "commands:\tenable, disable\n"
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009064 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009065
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009066 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009067 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009068
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009069 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009070}
9071
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009072static int fan_write_cmd_level(const char *cmd, int *rc)
9073{
9074 int level;
9075
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02009076 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009077 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03009078 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009079 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009080 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02009081 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009082 return 0;
9083
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009084 *rc = fan_set_level_safe(level);
9085 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009086 pr_err("level command accepted for unsupported access mode %d\n",
9087 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009088 else if (!*rc)
9089 tpacpi_disclose_usertask("procfs fan",
9090 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009091
9092 return 1;
9093}
9094
9095static int fan_write_cmd_enable(const char *cmd, int *rc)
9096{
9097 if (strlencmp(cmd, "enable") != 0)
9098 return 0;
9099
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009100 *rc = fan_set_enable();
9101 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009102 pr_err("enable command accepted for unsupported access mode %d\n",
9103 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009104 else if (!*rc)
9105 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009106
9107 return 1;
9108}
9109
9110static int fan_write_cmd_disable(const char *cmd, int *rc)
9111{
9112 if (strlencmp(cmd, "disable") != 0)
9113 return 0;
9114
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009115 *rc = fan_set_disable();
9116 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009117 pr_err("disable command accepted for unsupported access mode %d\n",
9118 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009119 else if (!*rc)
9120 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009121
9122 return 1;
9123}
9124
9125static int fan_write_cmd_speed(const char *cmd, int *rc)
9126{
9127 int speed;
9128
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02009129 /* TODO:
9130 * Support speed <low> <medium> <high> ? */
9131
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009132 if (sscanf(cmd, "speed %d", &speed) != 1)
9133 return 0;
9134
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009135 *rc = fan_set_speed(speed);
9136 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009137 pr_err("speed command accepted for unsupported access mode %d\n",
9138 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009139 else if (!*rc)
9140 tpacpi_disclose_usertask("procfs fan",
9141 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009142
9143 return 1;
9144}
9145
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009146static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9147{
9148 int interval;
9149
9150 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9151 return 0;
9152
9153 if (interval < 0 || interval > 120)
9154 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009155 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009156 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009157 tpacpi_disclose_usertask("procfs fan",
9158 "set watchdog timer to %d\n",
9159 interval);
9160 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009161
9162 return 1;
9163}
9164
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009165static int fan_write(char *buf)
9166{
9167 char *cmd;
9168 int rc = 0;
9169
9170 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009171 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009172 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009173 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009174 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009175 fan_write_cmd_disable(cmd, &rc) ||
9176 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009177 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009178 fan_write_cmd_speed(cmd, &rc))
9179 )
9180 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009181 else if (!rc)
9182 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009183 }
9184
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009185 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009186}
9187
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009188static struct ibm_struct fan_driver_data = {
9189 .name = "fan",
9190 .read = fan_read,
9191 .write = fan_write,
9192 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03009193 .suspend = fan_suspend,
9194 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009195};
9196
David Henningsson420f9732013-10-16 23:10:31 +02009197/*************************************************************************
9198 * Mute LED subdriver
9199 */
9200
Takashi Iwai9e908a12018-11-26 17:47:47 +01009201#define TPACPI_LED_MAX 2
David Henningsson420f9732013-10-16 23:10:31 +02009202
9203struct tp_led_table {
9204 acpi_string name;
9205 int on_value;
9206 int off_value;
9207 int state;
9208};
9209
Takashi Iwai9e908a12018-11-26 17:47:47 +01009210static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
9211 [LED_AUDIO_MUTE] = {
David Henningsson420f9732013-10-16 23:10:31 +02009212 .name = "SSMS",
9213 .on_value = 1,
9214 .off_value = 0,
9215 },
Takashi Iwai9e908a12018-11-26 17:47:47 +01009216 [LED_AUDIO_MICMUTE] = {
David Henningsson420f9732013-10-16 23:10:31 +02009217 .name = "MMTS",
9218 .on_value = 2,
9219 .off_value = 0,
9220 },
9221};
9222
9223static int mute_led_on_off(struct tp_led_table *t, bool state)
9224{
9225 acpi_handle temp;
9226 int output;
9227
Axel Lin3526eca2016-09-19 09:33:51 +08009228 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009229 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9230 return -EIO;
9231 }
9232
9233 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9234 state ? t->on_value : t->off_value))
9235 return -EIO;
9236
9237 t->state = state;
9238 return state;
9239}
9240
Takashi Iwai9e908a12018-11-26 17:47:47 +01009241static int tpacpi_led_set(int whichled, bool on)
David Henningsson420f9732013-10-16 23:10:31 +02009242{
9243 struct tp_led_table *t;
9244
David Henningsson420f9732013-10-16 23:10:31 +02009245 t = &led_tables[whichled];
9246 if (t->state < 0 || t->state == on)
9247 return t->state;
9248 return mute_led_on_off(t, on);
9249}
David Henningsson420f9732013-10-16 23:10:31 +02009250
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009251static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
9252 enum led_brightness brightness)
9253{
Takashi Iwai9e908a12018-11-26 17:47:47 +01009254 return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009255}
9256
9257static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
9258 enum led_brightness brightness)
9259{
Takashi Iwai9e908a12018-11-26 17:47:47 +01009260 return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009261}
9262
Takashi Iwai9e908a12018-11-26 17:47:47 +01009263static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
9264 [LED_AUDIO_MUTE] = {
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009265 .name = "platform::mute",
9266 .max_brightness = 1,
9267 .brightness_set_blocking = tpacpi_led_mute_set,
9268 .default_trigger = "audio-mute",
9269 },
Takashi Iwai9e908a12018-11-26 17:47:47 +01009270 [LED_AUDIO_MICMUTE] = {
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009271 .name = "platform::micmute",
9272 .max_brightness = 1,
9273 .brightness_set_blocking = tpacpi_led_micmute_set,
9274 .default_trigger = "audio-micmute",
9275 },
9276};
David Henningsson420f9732013-10-16 23:10:31 +02009277
9278static int mute_led_init(struct ibm_init_struct *iibm)
9279{
9280 acpi_handle temp;
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009281 int i, err;
David Henningsson420f9732013-10-16 23:10:31 +02009282
9283 for (i = 0; i < TPACPI_LED_MAX; i++) {
9284 struct tp_led_table *t = &led_tables[i];
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009285 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009286 t->state = -ENODEV;
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009287 continue;
9288 }
9289
Takashi Iwai9e908a12018-11-26 17:47:47 +01009290 mute_led_cdev[i].brightness = ledtrig_audio_get(i);
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009291 err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
9292 if (err < 0) {
9293 while (i--) {
9294 if (led_tables[i].state >= 0)
9295 led_classdev_unregister(&mute_led_cdev[i]);
9296 }
9297 return err;
9298 }
David Henningsson420f9732013-10-16 23:10:31 +02009299 }
9300 return 0;
9301}
9302
9303static void mute_led_exit(void)
9304{
9305 int i;
9306
Takashi Iwai4e1d0922018-11-26 17:47:45 +01009307 for (i = 0; i < TPACPI_LED_MAX; i++) {
9308 if (led_tables[i].state >= 0) {
9309 led_classdev_unregister(&mute_led_cdev[i]);
9310 tpacpi_led_set(i, false);
9311 }
9312 }
David Henningsson420f9732013-10-16 23:10:31 +02009313}
9314
Takashi Iwai119f4492014-02-12 16:32:45 +01009315static void mute_led_resume(void)
9316{
9317 int i;
9318
9319 for (i = 0; i < TPACPI_LED_MAX; i++) {
9320 struct tp_led_table *t = &led_tables[i];
9321 if (t->state >= 0)
9322 mute_led_on_off(t, t->state);
9323 }
9324}
9325
David Henningsson420f9732013-10-16 23:10:31 +02009326static struct ibm_struct mute_led_driver_data = {
9327 .name = "mute_led",
9328 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01009329 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02009330};
9331
Ognjen Galic2801b962018-02-07 15:58:44 +01009332/*
9333 * Battery Wear Control Driver
9334 * Contact: Ognjen Galic <smclt30p@gmail.com>
9335 */
9336
9337/* Metadata */
9338
9339#define GET_START "BCTG"
9340#define SET_START "BCCS"
9341#define GET_STOP "BCSG"
9342#define SET_STOP "BCSS"
9343
9344#define START_ATTR "charge_start_threshold"
9345#define STOP_ATTR "charge_stop_threshold"
9346
9347enum {
9348 BAT_ANY = 0,
9349 BAT_PRIMARY = 1,
9350 BAT_SECONDARY = 2
9351};
9352
9353enum {
9354 /* Error condition bit */
9355 METHOD_ERR = BIT(31),
9356};
9357
9358enum {
9359 /* This is used in the get/set helpers */
9360 THRESHOLD_START,
9361 THRESHOLD_STOP,
9362};
9363
9364struct tpacpi_battery_data {
9365 int charge_start;
9366 int start_support;
9367 int charge_stop;
9368 int stop_support;
9369};
9370
9371struct tpacpi_battery_driver_data {
9372 struct tpacpi_battery_data batteries[3];
9373 int individual_addressing;
9374};
9375
9376static struct tpacpi_battery_driver_data battery_info;
9377
9378/* ACPI helpers/functions/probes */
9379
9380/**
9381 * This evaluates a ACPI method call specific to the battery
9382 * ACPI extension. The specifics are that an error is marked
9383 * in the 32rd bit of the response, so we just check that here.
9384 */
9385static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9386{
9387 int response;
9388
9389 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9390 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9391 return AE_ERROR;
9392 }
9393 if (response & METHOD_ERR) {
9394 acpi_handle_err(hkey_handle,
9395 "%s evaluated but flagged as error", method);
9396 return AE_ERROR;
9397 }
9398 *ret = response;
9399 return AE_OK;
9400}
9401
9402static int tpacpi_battery_get(int what, int battery, int *ret)
9403{
9404 switch (what) {
9405 case THRESHOLD_START:
9406 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9407 return -ENODEV;
9408
9409 /* The value is in the low 8 bits of the response */
9410 *ret = *ret & 0xFF;
9411 return 0;
9412 case THRESHOLD_STOP:
9413 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9414 return -ENODEV;
9415 /* Value is in lower 8 bits */
9416 *ret = *ret & 0xFF;
9417 /*
9418 * On the stop value, if we return 0 that
9419 * does not make any sense. 0 means Default, which
9420 * means that charging stops at 100%, so we return
9421 * that.
9422 */
9423 if (*ret == 0)
9424 *ret = 100;
9425 return 0;
9426 default:
9427 pr_crit("wrong parameter: %d", what);
9428 return -EINVAL;
9429 }
9430}
9431
9432static int tpacpi_battery_set(int what, int battery, int value)
9433{
9434 int param, ret;
9435 /* The first 8 bits are the value of the threshold */
9436 param = value;
9437 /* The battery ID is in bits 8-9, 2 bits */
9438 param |= battery << 8;
9439
9440 switch (what) {
9441 case THRESHOLD_START:
9442 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9443 pr_err("failed to set charge threshold on battery %d",
9444 battery);
9445 return -ENODEV;
9446 }
9447 return 0;
9448 case THRESHOLD_STOP:
9449 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9450 pr_err("failed to set stop threshold: %d", battery);
9451 return -ENODEV;
9452 }
9453 return 0;
9454 default:
9455 pr_crit("wrong parameter: %d", what);
9456 return -EINVAL;
9457 }
9458}
9459
9460static int tpacpi_battery_probe(int battery)
9461{
9462 int ret = 0;
9463
Thomas Weißschuhd22296d2018-08-02 00:24:18 +02009464 memset(&battery_info.batteries[battery], 0,
9465 sizeof(battery_info.batteries[battery]));
9466
Ognjen Galic2801b962018-02-07 15:58:44 +01009467 /*
9468 * 1) Get the current start threshold
9469 * 2) Check for support
9470 * 3) Get the current stop threshold
9471 * 4) Check for support
9472 */
9473 if (acpi_has_method(hkey_handle, GET_START)) {
9474 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9475 pr_err("Error probing battery %d\n", battery);
9476 return -ENODEV;
9477 }
9478 /* Individual addressing is in bit 9 */
9479 if (ret & BIT(9))
9480 battery_info.individual_addressing = true;
9481 /* Support is marked in bit 8 */
9482 if (ret & BIT(8))
9483 battery_info.batteries[battery].start_support = 1;
9484 else
9485 return -ENODEV;
9486 if (tpacpi_battery_get(THRESHOLD_START, battery,
9487 &battery_info.batteries[battery].charge_start)) {
9488 pr_err("Error probing battery %d\n", battery);
9489 return -ENODEV;
9490 }
9491 }
9492 if (acpi_has_method(hkey_handle, GET_STOP)) {
9493 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9494 pr_err("Error probing battery stop; %d\n", battery);
9495 return -ENODEV;
9496 }
9497 /* Support is marked in bit 8 */
9498 if (ret & BIT(8))
9499 battery_info.batteries[battery].stop_support = 1;
9500 else
9501 return -ENODEV;
9502 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9503 &battery_info.batteries[battery].charge_stop)) {
9504 pr_err("Error probing battery stop: %d\n", battery);
9505 return -ENODEV;
9506 }
9507 }
9508 pr_info("battery %d registered (start %d, stop %d)",
9509 battery,
9510 battery_info.batteries[battery].charge_start,
9511 battery_info.batteries[battery].charge_stop);
9512
9513 return 0;
9514}
9515
9516/* General helper functions */
9517
9518static int tpacpi_battery_get_id(const char *battery_name)
9519{
9520
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009521 if (strcmp(battery_name, "BAT0") == 0 ||
9522 tp_features.battery_force_primary)
Ognjen Galic2801b962018-02-07 15:58:44 +01009523 return BAT_PRIMARY;
9524 if (strcmp(battery_name, "BAT1") == 0)
9525 return BAT_SECONDARY;
9526 /*
9527 * If for some reason the battery is not BAT0 nor is it
9528 * BAT1, we will assume it's the default, first battery,
9529 * AKA primary.
9530 */
9531 pr_warn("unknown battery %s, assuming primary", battery_name);
9532 return BAT_PRIMARY;
9533}
9534
9535/* sysfs interface */
9536
9537static ssize_t tpacpi_battery_store(int what,
9538 struct device *dev,
9539 const char *buf, size_t count)
9540{
9541 struct power_supply *supply = to_power_supply(dev);
9542 unsigned long value;
9543 int battery, rval;
9544 /*
9545 * Some systems have support for more than
9546 * one battery. If that is the case,
9547 * tpacpi_battery_probe marked that addressing
9548 * them individually is supported, so we do that
9549 * based on the device struct.
9550 *
9551 * On systems that are not supported, we assume
9552 * the primary as most of the ACPI calls fail
9553 * with "Any Battery" as the parameter.
9554 */
9555 if (battery_info.individual_addressing)
9556 /* BAT_PRIMARY or BAT_SECONDARY */
9557 battery = tpacpi_battery_get_id(supply->desc->name);
9558 else
9559 battery = BAT_PRIMARY;
9560
9561 rval = kstrtoul(buf, 10, &value);
9562 if (rval)
9563 return rval;
9564
9565 switch (what) {
9566 case THRESHOLD_START:
9567 if (!battery_info.batteries[battery].start_support)
9568 return -ENODEV;
9569 /* valid values are [0, 99] */
9570 if (value < 0 || value > 99)
9571 return -EINVAL;
9572 if (value > battery_info.batteries[battery].charge_stop)
9573 return -EINVAL;
9574 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9575 return -ENODEV;
9576 battery_info.batteries[battery].charge_start = value;
9577 return count;
9578
9579 case THRESHOLD_STOP:
9580 if (!battery_info.batteries[battery].stop_support)
9581 return -ENODEV;
9582 /* valid values are [1, 100] */
9583 if (value < 1 || value > 100)
9584 return -EINVAL;
9585 if (value < battery_info.batteries[battery].charge_start)
9586 return -EINVAL;
9587 battery_info.batteries[battery].charge_stop = value;
9588 /*
9589 * When 100 is passed to stop, we need to flip
9590 * it to 0 as that the EC understands that as
9591 * "Default", which will charge to 100%
9592 */
9593 if (value == 100)
9594 value = 0;
9595 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9596 return -EINVAL;
9597 return count;
9598 default:
9599 pr_crit("Wrong parameter: %d", what);
9600 return -EINVAL;
9601 }
9602 return count;
9603}
9604
9605static ssize_t tpacpi_battery_show(int what,
9606 struct device *dev,
9607 char *buf)
9608{
9609 struct power_supply *supply = to_power_supply(dev);
9610 int ret, battery;
9611 /*
9612 * Some systems have support for more than
9613 * one battery. If that is the case,
9614 * tpacpi_battery_probe marked that addressing
9615 * them individually is supported, so we;
9616 * based on the device struct.
9617 *
9618 * On systems that are not supported, we assume
9619 * the primary as most of the ACPI calls fail
9620 * with "Any Battery" as the parameter.
9621 */
9622 if (battery_info.individual_addressing)
9623 /* BAT_PRIMARY or BAT_SECONDARY */
9624 battery = tpacpi_battery_get_id(supply->desc->name);
9625 else
9626 battery = BAT_PRIMARY;
9627 if (tpacpi_battery_get(what, battery, &ret))
9628 return -ENODEV;
9629 return sprintf(buf, "%d\n", ret);
9630}
9631
9632static ssize_t charge_start_threshold_show(struct device *device,
9633 struct device_attribute *attr,
9634 char *buf)
9635{
9636 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9637}
9638
9639static ssize_t charge_stop_threshold_show(struct device *device,
9640 struct device_attribute *attr,
9641 char *buf)
9642{
9643 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9644}
9645
9646static ssize_t charge_start_threshold_store(struct device *dev,
9647 struct device_attribute *attr,
9648 const char *buf, size_t count)
9649{
9650 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9651}
9652
9653static ssize_t charge_stop_threshold_store(struct device *dev,
9654 struct device_attribute *attr,
9655 const char *buf, size_t count)
9656{
9657 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9658}
9659
9660static DEVICE_ATTR_RW(charge_start_threshold);
9661static DEVICE_ATTR_RW(charge_stop_threshold);
9662
9663static struct attribute *tpacpi_battery_attrs[] = {
9664 &dev_attr_charge_start_threshold.attr,
9665 &dev_attr_charge_stop_threshold.attr,
9666 NULL,
9667};
9668
9669ATTRIBUTE_GROUPS(tpacpi_battery);
9670
9671/* ACPI battery hooking */
9672
9673static int tpacpi_battery_add(struct power_supply *battery)
9674{
9675 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9676
9677 if (tpacpi_battery_probe(batteryid))
9678 return -ENODEV;
9679 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9680 return -ENODEV;
9681 return 0;
9682}
9683
9684static int tpacpi_battery_remove(struct power_supply *battery)
9685{
9686 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9687 return 0;
9688}
9689
9690static struct acpi_battery_hook battery_hook = {
9691 .add_battery = tpacpi_battery_add,
9692 .remove_battery = tpacpi_battery_remove,
9693 .name = "ThinkPad Battery Extension",
9694};
9695
9696/* Subdriver init/exit */
9697
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009698static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9699 /*
9700 * Individual addressing is broken on models that expose the
9701 * primary battery as BAT1.
9702 */
Jouke Witteveen6640ee62018-08-02 22:28:49 +02009703 TPACPI_Q_LNV('J', '7', true), /* B5400 */
9704 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
9705 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009706 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
Jouke Witteveen6640ee62018-08-02 22:28:49 +02009707 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009708};
9709
Ognjen Galic2801b962018-02-07 15:58:44 +01009710static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9711{
Thomas Weißschuhd22296d2018-08-02 00:24:18 +02009712 memset(&battery_info, 0, sizeof(battery_info));
9713
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009714 tp_features.battery_force_primary = tpacpi_check_quirks(
9715 battery_quirk_table,
9716 ARRAY_SIZE(battery_quirk_table));
9717
Ognjen Galic2801b962018-02-07 15:58:44 +01009718 battery_hook_register(&battery_hook);
9719 return 0;
9720}
9721
9722static void tpacpi_battery_exit(void)
9723{
9724 battery_hook_unregister(&battery_hook);
9725}
9726
9727static struct ibm_struct battery_driver_data = {
9728 .name = "battery",
9729 .exit = tpacpi_battery_exit,
9730};
9731
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009732/****************************************************************************
9733 ****************************************************************************
9734 *
9735 * Infrastructure
9736 *
9737 ****************************************************************************
9738 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07009739
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009740/*
9741 * HKEY event callout for other subdrivers go here
9742 * (yes, it is ugly, but it is quick, safe, and gets the job done
9743 */
9744static void tpacpi_driver_event(const unsigned int hkey_event)
9745{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00009746 if (ibm_backlight_device) {
9747 switch (hkey_event) {
9748 case TP_HKEY_EV_BRGHT_UP:
9749 case TP_HKEY_EV_BRGHT_DOWN:
9750 tpacpi_brightness_notify_change();
9751 }
9752 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009753 if (alsa_card) {
9754 switch (hkey_event) {
9755 case TP_HKEY_EV_VOL_UP:
9756 case TP_HKEY_EV_VOL_DOWN:
9757 case TP_HKEY_EV_VOL_MUTE:
9758 volume_alsa_notify_change();
9759 }
9760 }
Hans de Goedec685e202017-02-09 16:44:13 +01009761 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9762 enum led_brightness brightness;
9763
9764 mutex_lock(&kbdlight_mutex);
9765
9766 /*
9767 * Check the brightness actually changed, setting the brightness
9768 * through kbdlight_set_level() also triggers this event.
9769 */
9770 brightness = kbdlight_sysfs_get(NULL);
9771 if (kbdlight_brightness != brightness) {
9772 kbdlight_brightness = brightness;
9773 led_classdev_notify_brightness_hw_changed(
9774 &tpacpi_led_kbdlight.led_classdev, brightness);
9775 }
9776
9777 mutex_unlock(&kbdlight_mutex);
9778 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009779}
9780
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009781static void hotkey_driver_event(const unsigned int scancode)
9782{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03009783 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009784}
9785
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009786/* --------------------------------------------------------------------- */
9787
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009788/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03009789static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009790
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009791/*
9792 * Module and infrastructure proble, init and exit handling
9793 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009794
Rusty Russell90ab5ee2012-01-13 09:32:20 +10309795static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009796
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009797#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009798static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009799{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009800 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009801
Jan van den Berg72a979f2014-09-17 00:01:08 +02009802 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009803}
9804#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9805
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009806static void ibm_exit(struct ibm_struct *ibm)
9807{
9808 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9809
9810 list_del_init(&ibm->all_drivers);
9811
9812 if (ibm->flags.acpi_notify_installed) {
9813 dbg_printk(TPACPI_DBG_EXIT,
9814 "%s: acpi_remove_notify_handler\n", ibm->name);
9815 BUG_ON(!ibm->acpi);
9816 acpi_remove_notify_handler(*ibm->acpi->handle,
9817 ibm->acpi->type,
9818 dispatch_acpi_notify);
9819 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009820 }
9821
9822 if (ibm->flags.proc_created) {
9823 dbg_printk(TPACPI_DBG_EXIT,
9824 "%s: remove_proc_entry\n", ibm->name);
9825 remove_proc_entry(ibm->name, proc_dir);
9826 ibm->flags.proc_created = 0;
9827 }
9828
9829 if (ibm->flags.acpi_driver_registered) {
9830 dbg_printk(TPACPI_DBG_EXIT,
9831 "%s: acpi_bus_unregister_driver\n", ibm->name);
9832 BUG_ON(!ibm->acpi);
9833 acpi_bus_unregister_driver(ibm->acpi->driver);
9834 kfree(ibm->acpi->driver);
9835 ibm->acpi->driver = NULL;
9836 ibm->flags.acpi_driver_registered = 0;
9837 }
9838
9839 if (ibm->flags.init_called && ibm->exit) {
9840 ibm->exit();
9841 ibm->flags.init_called = 0;
9842 }
9843
9844 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9845}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009846
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009847static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009848{
9849 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009850 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009851 struct proc_dir_entry *entry;
9852
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009853 BUG_ON(ibm == NULL);
9854
9855 INIT_LIST_HEAD(&ibm->all_drivers);
9856
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009857 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009858 return 0;
9859
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009860 dbg_printk(TPACPI_DBG_INIT,
9861 "probing for %s\n", ibm->name);
9862
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009863 if (iibm->init) {
9864 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009865 if (ret > 0)
9866 return 0; /* probe failed */
9867 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009868 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009869
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009870 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009871 }
9872
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009873 if (ibm->acpi) {
9874 if (ibm->acpi->hid) {
9875 ret = register_tpacpi_subdriver(ibm);
9876 if (ret)
9877 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009878 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009879
9880 if (ibm->acpi->notify) {
9881 ret = setup_acpi_notify(ibm);
9882 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07009883 pr_notice("disabling subdriver %s\n",
9884 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009885 ret = 0;
9886 goto err_out;
9887 }
9888 if (ret < 0)
9889 goto err_out;
9890 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009891 }
9892
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009893 dbg_printk(TPACPI_DBG_INIT,
9894 "%s installed\n", ibm->name);
9895
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009896 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04009897 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009898
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009899 if (!mode)
9900 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009901 if (ibm->write)
9902 mode |= S_IWUSR;
9903 entry = proc_create_data(ibm->name, mode, proc_dir,
9904 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009905 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07009906 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009907 ret = -ENODEV;
9908 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009909 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009910 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009912
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009913 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9914
Linus Torvalds1da177e2005-04-16 15:20:36 -07009915 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009916
9917err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009918 dbg_printk(TPACPI_DBG_INIT,
9919 "%s: at error exit path with result %d\n",
9920 ibm->name, ret);
9921
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009922 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +02009923 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009924}
9925
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009926/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009927
Jouke Witteveen846a4162018-07-11 11:44:41 +02009928static char __init tpacpi_parse_fw_id(const char * const s,
9929 u32 *model, u16 *release)
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009930{
Jouke Witteveen846a4162018-07-11 11:44:41 +02009931 int i;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009932
Jouke Witteveen846a4162018-07-11 11:44:41 +02009933 if (!s || strlen(s) < 8)
9934 goto invalid;
9935
9936 for (i = 0; i < 8; i++)
9937 if (!((s[i] >= '0' && s[i] <= '9') ||
9938 (s[i] >= 'A' && s[i] <= 'Z')))
9939 goto invalid;
9940
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009941 /*
9942 * Most models: xxyTkkWW (#.##c)
9943 * Ancient 570/600 and -SL lacks (#.##c)
9944 */
Jouke Witteveen846a4162018-07-11 11:44:41 +02009945 if (s[3] == 'T' || s[3] == 'N') {
9946 *model = TPID(s[0], s[1]);
9947 *release = TPVER(s[4], s[5]);
9948 return s[2];
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009949
9950 /* New models: xxxyTkkW (#.##c); T550 and some others */
Jouke Witteveen846a4162018-07-11 11:44:41 +02009951 } else if (s[4] == 'T' || s[4] == 'N') {
9952 *model = TPID3(s[0], s[1], s[2]);
9953 *release = TPVER(s[5], s[6]);
9954 return s[3];
9955 }
9956
9957invalid:
9958 return '\0';
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009959}
9960
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009961/* returns 0 - probe ok, or < 0 - probe error.
9962 * Probe ok doesn't mean thinkpad found.
9963 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9964static int __must_check __init get_thinkpad_model_data(
9965 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009966{
Jeff Garzik18552562007-10-03 15:15:40 -04009967 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009968 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009969 char const *s;
Jouke Witteveen846a4162018-07-11 11:44:41 +02009970 char t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009971
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009972 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009973 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009974
9975 memset(tp, 0, sizeof(*tp));
9976
9977 if (dmi_name_in_vendors("IBM"))
9978 tp->vendor = PCI_VENDOR_ID_IBM;
9979 else if (dmi_name_in_vendors("LENOVO"))
9980 tp->vendor = PCI_VENDOR_ID_LENOVO;
9981 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009982 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009983
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009984 s = dmi_get_system_info(DMI_BIOS_VERSION);
9985 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9986 if (s && !tp->bios_version_str)
9987 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009988
9989 /* Really ancient ThinkPad 240X will fail this, which is fine */
Jouke Witteveen846a4162018-07-11 11:44:41 +02009990 t = tpacpi_parse_fw_id(tp->bios_version_str,
9991 &tp->bios_model, &tp->bios_release);
9992 if (t != 'E' && t != 'C')
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009993 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009994
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009995 /*
9996 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9997 * X32 or newer, all Z series; Some models must have an
9998 * up-to-date BIOS or they will not be detected.
9999 *
10000 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
10001 */
10002 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -020010003 if (sscanf(dev->name,
10004 "IBM ThinkPad Embedded Controller -[%17c",
10005 ec_fw_string) == 1) {
10006 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
10007 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010008
10009 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010010 if (!tp->ec_version_str)
10011 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -030010012
Jouke Witteveen846a4162018-07-11 11:44:41 +020010013 t = tpacpi_parse_fw_id(ec_fw_string,
10014 &tp->ec_model, &tp->ec_release);
10015 if (t != 'H') {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010016 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
Joe Perches0978e012011-04-04 10:06:25 -070010017 ec_fw_string);
10018 pr_notice("please report this to %s\n",
10019 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -030010020 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010021 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -020010022 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020010023 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010024
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010025 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -070010026 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010027 tp->model_str = kstrdup(s, GFP_KERNEL);
10028 if (!tp->model_str)
10029 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -050010030 } else {
10031 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -070010032 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -050010033 tp->model_str = kstrdup(s, GFP_KERNEL);
10034 if (!tp->model_str)
10035 return -ENOMEM;
10036 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010037 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -030010038
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010039 s = dmi_get_system_info(DMI_PRODUCT_NAME);
10040 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
10041 if (s && !tp->nummodel_str)
10042 return -ENOMEM;
10043
10044 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010045}
10046
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010047static int __init probe_for_thinkpad(void)
10048{
10049 int is_thinkpad;
10050
10051 if (acpi_disabled)
10052 return -ENODEV;
10053
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -030010054 /* It would be dangerous to run the driver in this case */
10055 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
10056 return -ENODEV;
10057
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010058 /*
10059 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -030010060 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010061 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -030010062 is_thinkpad = (thinkpad_id.model_str != NULL) ||
10063 (thinkpad_id.ec_model != 0) ||
10064 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010065
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -030010066 /* The EC handler is required */
10067 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010068 if (!ec_handle) {
10069 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -070010070 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010071 return -ENODEV;
10072 }
10073
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -030010074 if (!is_thinkpad && !force_load)
10075 return -ENODEV;
10076
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010077 return 0;
10078}
10079
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030010080static void __init thinkpad_acpi_init_banner(void)
10081{
Joe Perches0978e012011-04-04 10:06:25 -070010082 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
10083 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030010084
Joe Perches0978e012011-04-04 10:06:25 -070010085 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030010086 (thinkpad_id.bios_version_str) ?
10087 thinkpad_id.bios_version_str : "unknown",
10088 (thinkpad_id.ec_version_str) ?
10089 thinkpad_id.ec_version_str : "unknown");
10090
10091 BUG_ON(!thinkpad_id.vendor);
10092
10093 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -070010094 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030010095 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
10096 "IBM" : ((thinkpad_id.vendor ==
10097 PCI_VENDOR_ID_LENOVO) ?
10098 "Lenovo" : "Unknown vendor"),
10099 thinkpad_id.model_str,
10100 (thinkpad_id.nummodel_str) ?
10101 thinkpad_id.nummodel_str : "unknown");
10102}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010103
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010104/* Module init, exit, parameters */
10105
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010106static struct ibm_init_struct ibms_init[] __initdata = {
10107 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010108 .data = &thinkpad_acpi_driver_data,
10109 },
10110 {
10111 .init = hotkey_init,
10112 .data = &hotkey_driver_data,
10113 },
10114 {
10115 .init = bluetooth_init,
10116 .data = &bluetooth_driver_data,
10117 },
10118 {
10119 .init = wan_init,
10120 .data = &wan_driver_data,
10121 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020010122 {
10123 .init = uwb_init,
10124 .data = &uwb_driver_data,
10125 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -020010126#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010127 {
10128 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -030010129 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010130 .data = &video_driver_data,
10131 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -020010132#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010133 {
Pali Rohárbb28f3d52015-12-30 23:27:41 +010010134 .init = kbdlight_init,
10135 .data = &kbdlight_driver_data,
10136 },
10137 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010138 .init = light_init,
10139 .data = &light_driver_data,
10140 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010141 {
10142 .init = cmos_init,
10143 .data = &cmos_driver_data,
10144 },
10145 {
10146 .init = led_init,
10147 .data = &led_driver_data,
10148 },
10149 {
10150 .init = beep_init,
10151 .data = &beep_driver_data,
10152 },
10153 {
10154 .init = thermal_init,
10155 .data = &thermal_driver_data,
10156 },
10157 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010158 .init = brightness_init,
10159 .data = &brightness_driver_data,
10160 },
10161 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020010162 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010163 .data = &volume_driver_data,
10164 },
10165 {
10166 .init = fan_init,
10167 .data = &fan_driver_data,
10168 },
David Henningsson420f9732013-10-16 23:10:31 +020010169 {
10170 .init = mute_led_init,
10171 .data = &mute_led_driver_data,
10172 },
Ognjen Galic2801b962018-02-07 15:58:44 +010010173 {
10174 .init = tpacpi_battery_init,
10175 .data = &battery_driver_data,
10176 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010177};
10178
Kees Cooke4dca7b2017-10-17 19:04:42 -070010179static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010180{
10181 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010182 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010183
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -020010184 if (!kp || !kp->name || !val)
10185 return -EINVAL;
10186
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010187 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10188 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -020010189 WARN_ON(ibm == NULL);
10190
10191 if (!ibm || !ibm->name)
10192 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010193
10194 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
10195 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010196 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010197 strcpy(ibms_init[i].param, val);
10198 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010199 return 0;
10200 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010201 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010202
10203 return -EINVAL;
10204}
10205
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010206module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010207MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010208 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010209
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -030010210module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010211MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -030010212
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010213module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010214MODULE_PARM_DESC(force_load,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010215 "Attempts to load the driver even on a mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -030010216
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010217module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010218MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010219 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -030010220
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010221module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010222MODULE_PARM_DESC(brightness_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010223 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -030010224
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010225module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010226MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010227 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -020010228
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -020010229#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020010230module_param_named(volume_mode, volume_mode, uint, 0444);
10231MODULE_PARM_DESC(volume_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010232 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020010233
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020010234module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
10235MODULE_PARM_DESC(volume_capabilities,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010236 "Selects the mixer capabilites: 0=auto, 1=volume and mute, 2=mute only");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020010237
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -020010238module_param_named(volume_control, volume_control_allowed, bool, 0444);
10239MODULE_PARM_DESC(volume_control,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010240 "Enables software override for the console audio control when true");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -020010241
Andy Lutomirski9a417ec2014-10-17 17:04:29 -070010242module_param_named(software_mute, software_mute_requested, bool, 0444);
10243MODULE_PARM_DESC(software_mute,
10244 "Request full software mute control");
10245
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020010246/* ALSA module API parameters */
10247module_param_named(index, alsa_index, int, 0444);
10248MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
10249module_param_named(id, alsa_id, charp, 0444);
10250MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
10251module_param_named(enable, alsa_enable, bool, 0444);
10252MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -020010253#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020010254
Andy Shevchenko9b4bbbd2017-05-09 17:17:21 +030010255/* The module parameter can't be read back, that's why 0 is used here */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010256#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010257 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010258 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010259
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010260TPACPI_PARAM(hotkey);
10261TPACPI_PARAM(bluetooth);
10262TPACPI_PARAM(video);
10263TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010264TPACPI_PARAM(cmos);
10265TPACPI_PARAM(led);
10266TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010267TPACPI_PARAM(brightness);
10268TPACPI_PARAM(volume);
10269TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010270
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010271#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010272module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010273MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
10274module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
10275MODULE_PARM_DESC(wlsw_state,
10276 "Initial state of the emulated WLSW switch");
10277
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010278module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010279MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
10280module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
10281MODULE_PARM_DESC(bluetooth_state,
10282 "Initial state of the emulated bluetooth switch");
10283
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010284module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010285MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
10286module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
10287MODULE_PARM_DESC(wwan_state,
10288 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020010289
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010290module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020010291MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
10292module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
10293MODULE_PARM_DESC(uwb_state,
10294 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010295#endif
10296
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010297static void thinkpad_acpi_module_exit(void)
10298{
10299 struct ibm_struct *ibm, *itmp;
10300
10301 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
10302
10303 list_for_each_entry_safe_reverse(ibm, itmp,
10304 &tpacpi_all_drivers,
10305 all_drivers) {
10306 ibm_exit(ibm);
10307 }
10308
10309 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
10310
10311 if (tpacpi_inputdev) {
10312 if (tp_features.input_device_registered)
10313 input_unregister_device(tpacpi_inputdev);
10314 else
10315 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +100010316 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010317 }
10318
10319 if (tpacpi_hwmon)
10320 hwmon_device_unregister(tpacpi_hwmon);
10321
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010322 if (tpacpi_sensors_pdev)
10323 platform_device_unregister(tpacpi_sensors_pdev);
10324 if (tpacpi_pdev)
10325 platform_device_unregister(tpacpi_pdev);
10326
10327 if (tp_features.sensors_pdrv_attrs_registered)
10328 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
10329 if (tp_features.platform_drv_attrs_registered)
10330 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
10331
10332 if (tp_features.sensors_pdrv_registered)
10333 platform_driver_unregister(&tpacpi_hwmon_pdriver);
10334
10335 if (tp_features.platform_drv_registered)
10336 platform_driver_unregister(&tpacpi_pdriver);
10337
10338 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010339 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010340
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -030010341 if (tpacpi_wq)
10342 destroy_workqueue(tpacpi_wq);
10343
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010344 kfree(thinkpad_id.bios_version_str);
10345 kfree(thinkpad_id.ec_version_str);
10346 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +100010347 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010348}
10349
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -020010350
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010351static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010352{
10353 int ret, i;
10354
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -030010355 tpacpi_lifecycle = TPACPI_LIFE_INIT;
10356
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010357 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010358
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010359 ret = get_thinkpad_model_data(&thinkpad_id);
10360 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010361 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010362 thinkpad_acpi_module_exit();
10363 return ret;
10364 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010365 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010366 if (ret) {
10367 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010368 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010370
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010371 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010372
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030010373 thinkpad_acpi_init_banner();
10374 tpacpi_check_outdated_fw();
10375
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010376 TPACPI_ACPIHANDLE_INIT(ecrd);
10377 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010378
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -030010379 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
10380 if (!tpacpi_wq) {
10381 thinkpad_acpi_module_exit();
10382 return -ENOMEM;
10383 }
10384
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010385 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010386 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -070010387 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010388 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010389 return -ENODEV;
10390 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -040010391
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010392 ret = platform_driver_register(&tpacpi_pdriver);
10393 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010394 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010395 thinkpad_acpi_module_exit();
10396 return ret;
10397 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -030010398 tp_features.platform_drv_registered = 1;
10399
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010400 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
10401 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010402 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010403 thinkpad_acpi_module_exit();
10404 return ret;
10405 }
10406 tp_features.sensors_pdrv_registered = 1;
10407
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010408 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -030010409 if (!ret) {
10410 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010411 ret = tpacpi_create_driver_attributes(
10412 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -030010413 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010414 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010415 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010416 thinkpad_acpi_module_exit();
10417 return ret;
10418 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -030010419 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010420
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010421
10422 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010423 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010424 NULL, 0);
10425 if (IS_ERR(tpacpi_pdev)) {
10426 ret = PTR_ERR(tpacpi_pdev);
10427 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070010428 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010429 thinkpad_acpi_module_exit();
10430 return ret;
10431 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010432 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010433 TPACPI_HWMON_DRVR_NAME,
10434 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010435 if (IS_ERR(tpacpi_sensors_pdev)) {
10436 ret = PTR_ERR(tpacpi_sensors_pdev);
10437 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070010438 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010439 thinkpad_acpi_module_exit();
10440 return ret;
10441 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010442 tp_features.sensors_pdev_attrs_registered = 1;
Stanislav Fomichev6b99e352017-06-20 20:45:13 -070010443 tpacpi_hwmon = hwmon_device_register_with_groups(
10444 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
10445
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010446 if (IS_ERR(tpacpi_hwmon)) {
10447 ret = PTR_ERR(tpacpi_hwmon);
10448 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070010449 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010450 thinkpad_acpi_module_exit();
10451 return ret;
10452 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -030010453 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010454 tpacpi_inputdev = input_allocate_device();
10455 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010456 thinkpad_acpi_module_exit();
10457 return -ENOMEM;
10458 } else {
10459 /* Prepare input device, but don't register */
10460 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010461 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010462 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -030010463 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010464 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
10465 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +000010466 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010467 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -030010468
10469 /* Init subdriver dependencies */
10470 tpacpi_detect_brightness_capabilities();
10471
10472 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010473 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10474 ret = ibm_init(&ibms_init[i]);
10475 if (ret >= 0 && *ibms_init[i].param)
10476 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010477 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010478 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010479 return ret;
10480 }
10481 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -030010482
10483 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
10484
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010485 ret = input_register_device(tpacpi_inputdev);
10486 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -070010487 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010488 thinkpad_acpi_module_exit();
10489 return ret;
10490 } else {
10491 tp_features.input_device_registered = 1;
10492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010493
10494 return 0;
10495}
10496
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -030010497MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
10498
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010499/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +000010500 * This will autoload the driver in almost every ThinkPad
10501 * in widespread use.
10502 *
10503 * Only _VERY_ old models, like the 240, 240x and 570 lack
10504 * the HKEY event interface.
10505 */
10506MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
10507
10508/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010509 * DMI matching for module autoloading
10510 *
10511 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
10512 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
10513 *
10514 * Only models listed in thinkwiki will be supported, so add yours
10515 * if it is not there yet.
10516 */
10517#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +010010518 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010519
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010520/* Ancient thinkpad BIOSes have to be identified by
10521 * BIOS type or model number, and there are far less
10522 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +000010523IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010524
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +000010525MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
10526MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010527MODULE_DESCRIPTION(TPACPI_DESC);
10528MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010529MODULE_LICENSE("GPL");
10530
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010531module_init(thinkpad_acpi_module_init);
10532module_exit(thinkpad_acpi_module_exit);