blob: 0a118f462aa4b65ac62ed024e6f755d3b4b70551 [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>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020060#include <linux/kthread.h>
61#include <linux/freezer.h>
62#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020064#include <linux/nvram.h>
65#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020066#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020067#include <linux/sysfs.h>
68#include <linux/backlight.h>
Ognjen Galic2801b962018-02-07 15:58:44 +010069#include <linux/bitops.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020070#include <linux/fb.h>
71#include <linux/platform_device.h>
72#include <linux/hwmon.h>
73#include <linux/hwmon-sysfs.h>
74#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030075#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030076#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020077#include <linux/dmi.h>
78#include <linux/jiffies.h>
79#include <linux/workqueue.h>
Lv Zheng8b484632013-12-03 08:49:16 +080080#include <linux/acpi.h>
81#include <linux/pci_ids.h>
Ognjen Galic2801b962018-02-07 15:58:44 +010082#include <linux/power_supply.h>
Lv Zheng8b484632013-12-03 08:49:16 +080083#include <linux/thinkpad_acpi.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
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200480#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
481 { .vendor = PCI_VENDOR_ID_LENOVO, \
482 .bios = TPACPI_MATCH_ANY, \
483 .ec = TPID(__id1, __id2), \
484 .quirks = (__quirk) }
485
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300486struct tpacpi_quirk {
487 unsigned int vendor;
Jouke Witteveen846a4162018-07-11 11:44:41 +0200488 u32 bios;
489 u32 ec;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300490 unsigned long quirks;
491};
492
493/**
494 * tpacpi_check_quirks() - search BIOS/EC version on a list
495 * @qlist: array of &struct tpacpi_quirk
496 * @qlist_size: number of elements in @qlist
497 *
498 * Iterates over a quirks list until one is found that matches the
499 * ThinkPad's vendor, BIOS and EC model.
500 *
501 * Returns 0 if nothing matches, otherwise returns the quirks field of
502 * the matching &struct tpacpi_quirk entry.
503 *
504 * The match criteria is: vendor, ec and bios much match.
505 */
506static unsigned long __init tpacpi_check_quirks(
507 const struct tpacpi_quirk *qlist,
508 unsigned int qlist_size)
509{
510 while (qlist_size) {
511 if ((qlist->vendor == thinkpad_id.vendor ||
512 qlist->vendor == TPACPI_MATCH_ANY) &&
513 (qlist->bios == thinkpad_id.bios_model ||
514 qlist->bios == TPACPI_MATCH_ANY) &&
515 (qlist->ec == thinkpad_id.ec_model ||
516 qlist->ec == TPACPI_MATCH_ANY))
517 return qlist->quirks;
518
519 qlist_size--;
520 qlist++;
521 }
522 return 0;
523}
524
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300525static inline bool __pure __init tpacpi_is_lenovo(void)
526{
527 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
528}
529
530static inline bool __pure __init tpacpi_is_ibm(void)
531{
532 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
533}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300534
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300535/****************************************************************************
536 ****************************************************************************
537 *
538 * ACPI Helpers and device model
539 *
540 ****************************************************************************
541 ****************************************************************************/
542
543/*************************************************************************
544 * ACPI basic handles
545 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300547static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300548static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200550#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700552 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300553 &parent##_handle; \
554 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200556TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
557TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200559TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
560 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400561 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
562 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300563 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400564
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200565TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400566 "^HKEY", /* R30, R31 */
567 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300568 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400569
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300570/*************************************************************************
571 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200572 */
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700575 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400578 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200579 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400580 struct acpi_buffer result, *resultp;
581 union acpi_object out_obj;
582 acpi_status status;
583 va_list ap;
584 char res_type;
585 int success;
586 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700589 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 0;
591 }
592
593 if (*fmt == 'q') {
594 quiet = 1;
595 fmt++;
596 } else
597 quiet = 0;
598
599 res_type = *(fmt++);
600
601 params.count = 0;
602 params.pointer = &in_objs[0];
603
604 va_start(ap, fmt);
605 while (*fmt) {
606 char c = *(fmt++);
607 switch (c) {
608 case 'd': /* int */
609 in_objs[params.count].integer.value = va_arg(ap, int);
610 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
611 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400612 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300614 pr_err("acpi_evalf() called with invalid format character '%c'\n",
615 c);
Jesper Juhl21365852010-12-24 19:56:28 +0100616 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return 0;
618 }
619 }
620 va_end(ap);
621
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400622 if (res_type != 'v') {
623 result.length = sizeof(out_obj);
624 result.pointer = &out_obj;
625 resultp = &result;
626 } else
627 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400629 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400632 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300633 success = (status == AE_OK &&
634 out_obj.type == ACPI_TYPE_INTEGER);
635 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700636 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400638 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 success = status == AE_OK;
640 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400641 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300643 pr_err("acpi_evalf() called with invalid format character '%c'\n",
644 res_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 0;
646 }
647
648 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700649 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300650 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 return success;
653}
654
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200655static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300656{
657 int v;
658
659 if (ecrd_handle) {
660 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
661 return 0;
662 *p = v;
663 } else {
664 if (ec_read(i, p) < 0)
665 return 0;
666 }
667
668 return 1;
669}
670
671static int acpi_ec_write(int i, u8 v)
672{
673 if (ecwr_handle) {
674 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
675 return 0;
676 } else {
677 if (ec_write(i, v) < 0)
678 return 0;
679 }
680
681 return 1;
682}
683
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300684static int issue_thinkpad_cmos_command(int cmos_cmd)
685{
686 if (!cmos_handle)
687 return -ENXIO;
688
689 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
690 return -EIO;
691
692 return 0;
693}
694
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300695/*************************************************************************
696 * ACPI device model
697 */
698
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200699#define TPACPI_ACPIHANDLE_INIT(object) \
700 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300701 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200702
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300703static void __init drv_acpi_handle_init(const char *name,
704 acpi_handle *handle, const acpi_handle parent,
705 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300706{
707 int i;
708 acpi_status status;
709
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300710 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
711 name);
712
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300713 for (i = 0; i < num_paths; i++) {
714 status = acpi_get_handle(parent, paths[i], handle);
715 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300716 dbg_printk(TPACPI_DBG_INIT,
717 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300718 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300719 return;
720 }
721 }
722
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300723 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
724 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300725 *handle = NULL;
726}
727
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300728static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
729 u32 level, void *context, void **return_value)
730{
Aaron Lu46445b62013-10-11 21:27:46 +0800731 struct acpi_device *dev;
732 if (!strcmp(context, "video")) {
733 if (acpi_bus_get_device(handle, &dev))
734 return AE_OK;
735 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
736 return AE_OK;
737 }
738
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300739 *(acpi_handle *)return_value = handle;
740
741 return AE_CTRL_TERMINATE;
742}
743
744static void __init tpacpi_acpi_handle_locate(const char *name,
745 const char *hid,
746 acpi_handle *handle)
747{
748 acpi_status status;
749 acpi_handle device_found;
750
Aaron Lu46445b62013-10-11 21:27:46 +0800751 BUG_ON(!name || !handle);
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300752 vdbg_printk(TPACPI_DBG_INIT,
753 "trying to locate ACPI handle for %s, using HID %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +0800754 name, hid ? hid : "NULL");
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300755
756 memset(&device_found, 0, sizeof(device_found));
757 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
758 (void *)name, &device_found);
759
760 *handle = NULL;
761
762 if (ACPI_SUCCESS(status)) {
763 *handle = device_found;
764 dbg_printk(TPACPI_DBG_INIT,
765 "Found ACPI handle for %s\n", name);
766 } else {
767 vdbg_printk(TPACPI_DBG_INIT,
768 "Could not locate an ACPI handle for %s: %s\n",
769 name, acpi_format_exception(status));
770 }
771}
772
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300773static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300774{
775 struct ibm_struct *ibm = data;
776
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300777 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
778 return;
779
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300780 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300781 return;
782
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300783 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300784}
785
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300786static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300787{
788 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300789 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300790
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300791 BUG_ON(!ibm->acpi);
792
793 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300794 return 0;
795
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300796 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300797 "setting up ACPI notify for %s\n", ibm->name);
798
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300799 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
800 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700801 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300802 return -ENODEV;
803 }
804
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700805 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300806 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200807 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300808 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300809
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300810 status = acpi_install_notify_handler(*ibm->acpi->handle,
811 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300812 if (ACPI_FAILURE(status)) {
813 if (status == AE_ALREADY_EXISTS) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300814 pr_notice("another device driver is already handling %s events\n",
815 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300816 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700817 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300818 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300819 }
820 return -ENODEV;
821 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300822 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300823 return 0;
824}
825
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300826static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300827{
828 return 0;
829}
830
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300831static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300832{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300833 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300834
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300835 dbg_printk(TPACPI_DBG_INIT,
836 "registering %s as an ACPI driver\n", ibm->name);
837
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300838 BUG_ON(!ibm->acpi);
839
840 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
841 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700842 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300843 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300844 }
845
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200846 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300847 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200848
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300849 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300850
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300851 rc = acpi_bus_register_driver(ibm->acpi->driver);
852 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700853 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200854 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300855 kfree(ibm->acpi->driver);
856 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300857 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300858 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300859
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300860 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300861}
862
863
864/****************************************************************************
865 ****************************************************************************
866 *
867 * Procfs Helpers
868 *
869 ****************************************************************************
870 ****************************************************************************/
871
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200872static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300873{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200874 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300875
876 if (!ibm || !ibm->read)
877 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200878 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300879}
880
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200881static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300882{
Al Virod9dda782013-03-31 18:16:14 -0400883 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200884}
885
886static ssize_t dispatch_proc_write(struct file *file,
887 const char __user *userbuf,
888 size_t count, loff_t *pos)
889{
Al Virod9dda782013-03-31 18:16:14 -0400890 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300891 char *kernbuf;
892 int ret;
893
894 if (!ibm || !ibm->write)
895 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300896 if (count > PAGE_SIZE - 2)
897 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300898
899 kernbuf = kmalloc(count + 2, GFP_KERNEL);
900 if (!kernbuf)
901 return -ENOMEM;
902
903 if (copy_from_user(kernbuf, userbuf, count)) {
904 kfree(kernbuf);
905 return -EFAULT;
906 }
907
908 kernbuf[count] = 0;
909 strcat(kernbuf, ",");
910 ret = ibm->write(kernbuf);
911 if (ret == 0)
912 ret = count;
913
914 kfree(kernbuf);
915
916 return ret;
917}
918
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200919static const struct file_operations dispatch_proc_fops = {
920 .owner = THIS_MODULE,
921 .open = dispatch_proc_open,
922 .read = seq_read,
923 .llseek = seq_lseek,
924 .release = single_release,
925 .write = dispatch_proc_write,
926};
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928static char *next_cmd(char **cmds)
929{
930 char *start = *cmds;
931 char *end;
932
933 while ((end = strchr(start, ',')) && end == start)
934 start = end + 1;
935
936 if (!end)
937 return NULL;
938
939 *end = 0;
940 *cmds = end + 1;
941 return start;
942}
943
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300944
945/****************************************************************************
946 ****************************************************************************
947 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300948 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300949 *
950 ****************************************************************************
951 ****************************************************************************/
952
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300953static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300954static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700955static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300956static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300957static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200958static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300959
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200960#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200961static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200962{
963 struct ibm_struct *ibm, *itmp;
964
965 list_for_each_entry_safe(ibm, itmp,
966 &tpacpi_all_drivers,
967 all_drivers) {
968 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200969 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200970 }
971
972 return 0;
973}
974
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200975static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300976{
977 struct ibm_struct *ibm, *itmp;
978
979 list_for_each_entry_safe(ibm, itmp,
980 &tpacpi_all_drivers,
981 all_drivers) {
982 if (ibm->resume)
983 (ibm->resume)();
984 }
985
986 return 0;
987}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200988#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300989
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200990static SIMPLE_DEV_PM_OPS(tpacpi_pm,
991 tpacpi_suspend_handler, tpacpi_resume_handler);
992
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200993static void tpacpi_shutdown_handler(struct platform_device *pdev)
994{
995 struct ibm_struct *ibm, *itmp;
996
997 list_for_each_entry_safe(ibm, itmp,
998 &tpacpi_all_drivers,
999 all_drivers) {
1000 if (ibm->shutdown)
1001 (ibm->shutdown)();
1002 }
1003}
1004
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001005static struct platform_driver tpacpi_pdriver = {
1006 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001007 .name = TPACPI_DRVR_NAME,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +02001008 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001009 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001010 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001011};
1012
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03001013static struct platform_driver tpacpi_hwmon_pdriver = {
1014 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001015 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03001016 },
1017};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001018
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03001019/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001020 * sysfs support helpers
1021 */
1022
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001023struct attribute_set {
1024 unsigned int members, max_members;
1025 struct attribute_group group;
1026};
1027
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001028struct attribute_set_obj {
1029 struct attribute_set s;
1030 struct attribute *a;
1031} __attribute__((packed));
1032
1033static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001034 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001035{
1036 struct attribute_set_obj *sobj;
1037
1038 if (max_members == 0)
1039 return NULL;
1040
1041 /* Allocates space for implicit NULL at the end too */
1042 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1043 max_members * sizeof(struct attribute *),
1044 GFP_KERNEL);
1045 if (!sobj)
1046 return NULL;
1047 sobj->s.max_members = max_members;
1048 sobj->s.group.attrs = &sobj->a;
1049 sobj->s.group.name = name;
1050
1051 return &sobj->s;
1052}
1053
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001054#define destroy_attr_set(_set) \
1055 kfree(_set);
1056
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001057/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001058static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001059{
1060 if (!s || !attr)
1061 return -EINVAL;
1062
1063 if (s->members >= s->max_members)
1064 return -ENOMEM;
1065
1066 s->group.attrs[s->members] = attr;
1067 s->members++;
1068
1069 return 0;
1070}
1071
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001072static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001073 struct attribute **attr,
1074 unsigned int count)
1075{
1076 int i, res;
1077
1078 for (i = 0; i < count; i++) {
1079 res = add_to_attr_set(s, attr[i]);
1080 if (res)
1081 return res;
1082 }
1083
1084 return 0;
1085}
1086
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001087static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001088{
1089 sysfs_remove_group(kobj, &s->group);
1090 destroy_attr_set(s);
1091}
1092
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001093#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1094 sysfs_create_group(_kobj, &_attr_set->group)
1095
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001096static int parse_strtoul(const char *buf,
1097 unsigned long max, unsigned long *value)
1098{
1099 char *endp;
1100
André Goddard Rosae7d28602009-12-14 18:01:06 -08001101 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1102 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001103 if (*endp || *value > max)
1104 return -EINVAL;
1105
1106 return 0;
1107}
1108
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001109static void tpacpi_disable_brightness_delay(void)
1110{
1111 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001112 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001113}
1114
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001115static void printk_deprecated_attribute(const char * const what,
1116 const char * const details)
1117{
1118 tpacpi_log_usertask("deprecated sysfs attribute");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001119 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001120 what, details);
1121}
1122
Johannes Berg19d337d2009-06-02 13:01:37 +02001123/*************************************************************************
1124 * rfkill and radio control support helpers
1125 */
1126
1127/*
1128 * ThinkPad-ACPI firmware handling model:
1129 *
1130 * WLSW (master wireless switch) is event-driven, and is common to all
1131 * firmware-controlled radios. It cannot be controlled, just monitored,
1132 * as expected. It overrides all radio state in firmware
1133 *
1134 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1135 * (TODO: verify how WLSW interacts with the returned radio state).
1136 *
1137 * The only time there are shadow radio state changes, is when
1138 * masked-off hotkeys are used.
1139 */
1140
1141/*
1142 * Internal driver API for radio state:
1143 *
1144 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1145 * bool: true means radio blocked (off)
1146 */
1147enum tpacpi_rfkill_state {
1148 TPACPI_RFK_RADIO_OFF = 0,
1149 TPACPI_RFK_RADIO_ON
1150};
1151
1152/* rfkill switches */
1153enum tpacpi_rfk_id {
1154 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1155 TPACPI_RFK_WWAN_SW_ID,
1156 TPACPI_RFK_UWB_SW_ID,
1157 TPACPI_RFK_SW_MAX
1158};
1159
1160static const char *tpacpi_rfkill_names[] = {
1161 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1162 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1163 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1164 [TPACPI_RFK_SW_MAX] = NULL
1165};
1166
1167/* ThinkPad-ACPI rfkill subdriver */
1168struct tpacpi_rfk {
1169 struct rfkill *rfkill;
1170 enum tpacpi_rfk_id id;
1171 const struct tpacpi_rfk_ops *ops;
1172};
1173
1174struct tpacpi_rfk_ops {
1175 /* firmware interface */
1176 int (*get_status)(void);
1177 int (*set_status)(const enum tpacpi_rfkill_state);
1178};
1179
1180static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1181
1182/* Query FW and update rfkill sw state for a given rfkill switch */
1183static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1184{
1185 int status;
1186
1187 if (!tp_rfk)
1188 return -ENODEV;
1189
1190 status = (tp_rfk->ops->get_status)();
1191 if (status < 0)
1192 return status;
1193
1194 rfkill_set_sw_state(tp_rfk->rfkill,
1195 (status == TPACPI_RFK_RADIO_OFF));
1196
1197 return status;
1198}
1199
1200/* Query FW and update rfkill sw state for all rfkill switches */
1201static void tpacpi_rfk_update_swstate_all(void)
1202{
1203 unsigned int i;
1204
1205 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1206 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1207}
1208
1209/*
1210 * Sync the HW-blocking state of all rfkill switches,
1211 * do notice it causes the rfkill core to schedule uevents
1212 */
1213static void tpacpi_rfk_update_hwblock_state(bool blocked)
1214{
1215 unsigned int i;
1216 struct tpacpi_rfk *tp_rfk;
1217
1218 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1219 tp_rfk = tpacpi_rfkill_switches[i];
1220 if (tp_rfk) {
1221 if (rfkill_set_hw_state(tp_rfk->rfkill,
1222 blocked)) {
1223 /* ignore -- we track sw block */
1224 }
1225 }
1226 }
1227}
1228
1229/* Call to get the WLSW state from the firmware */
1230static int hotkey_get_wlsw(void);
1231
1232/* Call to query WLSW state and update all rfkill switches */
1233static bool tpacpi_rfk_check_hwblock_state(void)
1234{
1235 int res = hotkey_get_wlsw();
1236 int hw_blocked;
1237
1238 /* When unknown or unsupported, we have to assume it is unblocked */
1239 if (res < 0)
1240 return false;
1241
1242 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1243 tpacpi_rfk_update_hwblock_state(hw_blocked);
1244
1245 return hw_blocked;
1246}
1247
1248static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1249{
1250 struct tpacpi_rfk *tp_rfk = data;
1251 int res;
1252
1253 dbg_printk(TPACPI_DBG_RFKILL,
1254 "request to change radio state to %s\n",
1255 blocked ? "blocked" : "unblocked");
1256
1257 /* try to set radio state */
1258 res = (tp_rfk->ops->set_status)(blocked ?
1259 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1260
1261 /* and update the rfkill core with whatever the FW really did */
1262 tpacpi_rfk_update_swstate(tp_rfk);
1263
1264 return (res < 0) ? res : 0;
1265}
1266
1267static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1268 .set_block = tpacpi_rfk_hook_set_block,
1269};
1270
1271static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1272 const struct tpacpi_rfk_ops *tp_rfkops,
1273 const enum rfkill_type rfktype,
1274 const char *name,
1275 const bool set_default)
1276{
1277 struct tpacpi_rfk *atp_rfk;
1278 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001279 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001280 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001281 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001282
1283 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1284
Johannes Berg19d337d2009-06-02 13:01:37 +02001285 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1286 if (atp_rfk)
1287 atp_rfk->rfkill = rfkill_alloc(name,
1288 &tpacpi_pdev->dev,
1289 rfktype,
1290 &tpacpi_rfk_rfkill_ops,
1291 atp_rfk);
1292 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001293 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001294 kfree(atp_rfk);
1295 return -ENOMEM;
1296 }
1297
1298 atp_rfk->id = id;
1299 atp_rfk->ops = tp_rfkops;
1300
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001301 sw_status = (tp_rfkops->get_status)();
1302 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001303 pr_err("failed to read initial state for %s, error %d\n",
1304 name, sw_status);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001305 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001306 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001307 if (set_default) {
1308 /* try to keep the initial state, since we ask the
1309 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001310 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001311 }
1312 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001313 hw_state = tpacpi_rfk_check_hwblock_state();
1314 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001315
1316 res = rfkill_register(atp_rfk->rfkill);
1317 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001318 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001319 rfkill_destroy(atp_rfk->rfkill);
1320 kfree(atp_rfk);
1321 return res;
1322 }
1323
1324 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001325
Joe Perches0978e012011-04-04 10:06:25 -07001326 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001327 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001328 return 0;
1329}
1330
1331static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1332{
1333 struct tpacpi_rfk *tp_rfk;
1334
1335 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1336
1337 tp_rfk = tpacpi_rfkill_switches[id];
1338 if (tp_rfk) {
1339 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001340 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001341 tpacpi_rfkill_switches[id] = NULL;
1342 kfree(tp_rfk);
1343 }
1344}
1345
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001346static void printk_deprecated_rfkill_attribute(const char * const what)
1347{
1348 printk_deprecated_attribute(what,
1349 "Please switch to generic rfkill before year 2010");
1350}
1351
Johannes Berg19d337d2009-06-02 13:01:37 +02001352/* sysfs <radio> enable ------------------------------------------------ */
1353static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1354 struct device_attribute *attr,
1355 char *buf)
1356{
1357 int status;
1358
1359 printk_deprecated_rfkill_attribute(attr->attr.name);
1360
1361 /* This is in the ABI... */
1362 if (tpacpi_rfk_check_hwblock_state()) {
1363 status = TPACPI_RFK_RADIO_OFF;
1364 } else {
1365 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1366 if (status < 0)
1367 return status;
1368 }
1369
1370 return snprintf(buf, PAGE_SIZE, "%d\n",
1371 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1372}
1373
1374static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1375 struct device_attribute *attr,
1376 const char *buf, size_t count)
1377{
1378 unsigned long t;
1379 int res;
1380
1381 printk_deprecated_rfkill_attribute(attr->attr.name);
1382
1383 if (parse_strtoul(buf, 1, &t))
1384 return -EINVAL;
1385
1386 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1387
1388 /* This is in the ABI... */
1389 if (tpacpi_rfk_check_hwblock_state() && !!t)
1390 return -EPERM;
1391
1392 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1393 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1394 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1395
1396 return (res < 0) ? res : count;
1397}
1398
1399/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001400static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001401{
Johannes Berg19d337d2009-06-02 13:01:37 +02001402 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001403 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001404 else {
1405 int status;
1406
1407 /* This is in the ABI... */
1408 if (tpacpi_rfk_check_hwblock_state()) {
1409 status = TPACPI_RFK_RADIO_OFF;
1410 } else {
1411 status = tpacpi_rfk_update_swstate(
1412 tpacpi_rfkill_switches[id]);
1413 if (status < 0)
1414 return status;
1415 }
1416
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001417 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001418 (status == TPACPI_RFK_RADIO_ON) ?
1419 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001420 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001421 }
1422
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001423 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001424}
1425
1426static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1427{
1428 char *cmd;
1429 int status = -1;
1430 int res = 0;
1431
1432 if (id >= TPACPI_RFK_SW_MAX)
1433 return -ENODEV;
1434
1435 while ((cmd = next_cmd(&buf))) {
1436 if (strlencmp(cmd, "enable") == 0)
1437 status = TPACPI_RFK_RADIO_ON;
1438 else if (strlencmp(cmd, "disable") == 0)
1439 status = TPACPI_RFK_RADIO_OFF;
1440 else
1441 return -EINVAL;
1442 }
1443
1444 if (status != -1) {
1445 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1446 (status == TPACPI_RFK_RADIO_ON) ?
1447 "enable" : "disable",
1448 tpacpi_rfkill_names[id]);
1449 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1450 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1451 }
1452
1453 return res;
1454}
1455
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001456/*************************************************************************
1457 * thinkpad-acpi driver attributes
1458 */
1459
1460/* interface_version --------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001461static ssize_t interface_version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001462{
1463 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1464}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001465static DRIVER_ATTR_RO(interface_version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001466
1467/* debug_level --------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001468static ssize_t debug_level_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001469{
1470 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1471}
1472
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001473static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1474 size_t count)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001475{
1476 unsigned long t;
1477
1478 if (parse_strtoul(buf, 0xffff, &t))
1479 return -EINVAL;
1480
1481 dbg_level = t;
1482
1483 return count;
1484}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001485static DRIVER_ATTR_RW(debug_level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001486
1487/* version ------------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001488static ssize_t version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001489{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001490 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1491 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001492}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001493static DRIVER_ATTR_RO(version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001494
1495/* --------------------------------------------------------------------- */
1496
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001497#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1498
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001499/* wlsw_emulstate ------------------------------------------------------ */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001500static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001501{
1502 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1503}
1504
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001505static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1506 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001507{
1508 unsigned long t;
1509
1510 if (parse_strtoul(buf, 1, &t))
1511 return -EINVAL;
1512
Johannes Berg19d337d2009-06-02 13:01:37 +02001513 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001514 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001515 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1516 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001517
1518 return count;
1519}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001520static DRIVER_ATTR_RW(wlsw_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001521
1522/* bluetooth_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001523static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001524{
1525 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1526}
1527
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001528static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1529 const char *buf, size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001530{
1531 unsigned long t;
1532
1533 if (parse_strtoul(buf, 1, &t))
1534 return -EINVAL;
1535
1536 tpacpi_bluetooth_emulstate = !!t;
1537
1538 return count;
1539}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001540static DRIVER_ATTR_RW(bluetooth_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001541
1542/* wwan_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001543static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001544{
1545 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1546}
1547
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001548static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1549 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001550{
1551 unsigned long t;
1552
1553 if (parse_strtoul(buf, 1, &t))
1554 return -EINVAL;
1555
1556 tpacpi_wwan_emulstate = !!t;
1557
1558 return count;
1559}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001560static DRIVER_ATTR_RW(wwan_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001561
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001562/* uwb_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001563static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001564{
1565 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1566}
1567
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001568static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1569 size_t count)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001570{
1571 unsigned long t;
1572
1573 if (parse_strtoul(buf, 1, &t))
1574 return -EINVAL;
1575
1576 tpacpi_uwb_emulstate = !!t;
1577
1578 return count;
1579}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001580static DRIVER_ATTR_RW(uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001581#endif
1582
1583/* --------------------------------------------------------------------- */
1584
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001585static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001586 &driver_attr_debug_level, &driver_attr_version,
1587 &driver_attr_interface_version,
1588};
1589
1590static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1591{
1592 int i, res;
1593
1594 i = 0;
1595 res = 0;
1596 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1597 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1598 i++;
1599 }
1600
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001601#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1602 if (!res && dbg_wlswemul)
1603 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1604 if (!res && dbg_bluetoothemul)
1605 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1606 if (!res && dbg_wwanemul)
1607 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001608 if (!res && dbg_uwbemul)
1609 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001610#endif
1611
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001612 return res;
1613}
1614
1615static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1616{
1617 int i;
1618
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001619 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001620 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001621
1622#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1623 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1624 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1625 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001626 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001627#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001628}
1629
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001630/*************************************************************************
1631 * Firmware Data
1632 */
1633
1634/*
1635 * Table of recommended minimum BIOS versions
1636 *
1637 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001638 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001639 * bugs and bad ACPI behaviour on older versions
1640 *
1641 * 2. BIOS or EC fw with known bugs that trigger on Linux
1642 *
1643 * 3. BIOS with known reduced functionality in older versions
1644 *
1645 * We recommend the latest BIOS and EC version.
1646 * We only support the latest BIOS and EC fw version as a rule.
1647 *
1648 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1649 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001650 *
1651 * WARNING: we use this table also to detect that the machine is
1652 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001653 */
1654
1655#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1656 { .vendor = (__v), \
1657 .bios = TPID(__id1, __id2), \
1658 .ec = TPACPI_MATCH_ANY, \
Jouke Witteveen846a4162018-07-11 11:44:41 +02001659 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1660 | TPVER(__bv1, __bv2) }
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001661
1662#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001663 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001664 { .vendor = (__v), \
1665 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001666 .ec = __eid, \
Jouke Witteveen846a4162018-07-11 11:44:41 +02001667 .quirks = TPVER(__ev1, __ev2) << 16 \
1668 | TPVER(__bv1, __bv2) }
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001669
1670#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1671 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1672
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001673/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001674#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1675 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001676 __bv1, __bv2, TPID(__id1, __id2), \
1677 __ev1, __ev2), \
1678 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1679 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1680 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001681
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001682/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001683#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1684 __eid1, __eid2, __ev1, __ev2) \
1685 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001686 __bv1, __bv2, TPID(__eid1, __eid2), \
1687 __ev1, __ev2), \
1688 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1689 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1690 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001691
1692#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1693 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1694
1695#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1696 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001697 __bv1, __bv2, TPID(__id1, __id2), \
1698 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001699
1700#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1701 __eid1, __eid2, __ev1, __ev2) \
1702 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001703 __bv1, __bv2, TPID(__eid1, __eid2), \
1704 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001705
1706static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1707 /* Numeric models ------------------ */
1708 /* FW MODEL BIOS VERS */
1709 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1710 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1711 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1712 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1713 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1714 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1715 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1716 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1717 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1718
1719 /* A-series ------------------------- */
1720 /* FW MODEL BIOS VERS EC VERS */
1721 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1722 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1723 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1724 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1725 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1726 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1727 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1728 TPV_QI0('1', '3', '2', '0'), /* A22m */
1729 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1730 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1731 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1732
1733 /* G-series ------------------------- */
1734 /* FW MODEL BIOS VERS */
1735 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1736 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1737
1738 /* R-series, T-series --------------- */
1739 /* FW MODEL BIOS VERS EC VERS */
1740 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1741 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1742 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1743 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1744 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1745 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1746 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1747 T40/p, T41/p, T42/p (1) */
1748 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1749 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1750 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1751 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1752
1753 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1754 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1755 TPV_QI0('1', '6', '3', '2'), /* T22 */
1756 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1757 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1758 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1759
1760 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1761 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001762 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001763
1764 /* BIOS FW BIOS VERS EC FW EC VERS */
1765 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1766 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1767
1768 /* X-series ------------------------- */
1769 /* FW MODEL BIOS VERS EC VERS */
1770 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1771 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1772 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1773 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1774 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1775 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1776 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1777
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001778 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1779 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001780
1781 /* (0) - older versions lack DMI EC fw string and functionality */
1782 /* (1) - older versions known to lack functionality */
1783};
1784
1785#undef TPV_QL1
1786#undef TPV_QL0
1787#undef TPV_QI2
1788#undef TPV_QI1
1789#undef TPV_QI0
1790#undef TPV_Q_X
1791#undef TPV_Q
1792
1793static void __init tpacpi_check_outdated_fw(void)
1794{
1795 unsigned long fwvers;
1796 u16 ec_version, bios_version;
1797
1798 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1799 ARRAY_SIZE(tpacpi_bios_version_qtable));
1800
1801 if (!fwvers)
1802 return;
1803
1804 bios_version = fwvers & 0xffffU;
1805 ec_version = (fwvers >> 16) & 0xffffU;
1806
1807 /* note that unknown versions are set to 0x0000 and we use that */
1808 if ((bios_version > thinkpad_id.bios_release) ||
1809 (ec_version > thinkpad_id.ec_release &&
Jouke Witteveen846a4162018-07-11 11:44:41 +02001810 ec_version != TPACPI_MATCH_ANY_VERSION)) {
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001811 /*
1812 * The changelogs would let us track down the exact
1813 * reason, but it is just too much of a pain to track
1814 * it. We only list BIOSes that are either really
1815 * broken, or really stable to begin with, so it is
1816 * best if the user upgrades the firmware anyway.
1817 */
Joe Perches0978e012011-04-04 10:06:25 -07001818 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001819 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 -03001820 }
1821}
1822
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001823static bool __init tpacpi_is_fw_known(void)
1824{
1825 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1826 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1827}
1828
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001829/****************************************************************************
1830 ****************************************************************************
1831 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001832 * Subdrivers
1833 *
1834 ****************************************************************************
1835 ****************************************************************************/
1836
1837/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001838 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001839 */
1840
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001841static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001843 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1844 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001848static struct ibm_struct thinkpad_acpi_driver_data = {
1849 .name = "driver",
1850 .read = thinkpad_acpi_driver_read,
1851};
1852
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001853/*************************************************************************
1854 * Hotkey subdriver
1855 */
1856
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001857/*
1858 * ThinkPad firmware event model
1859 *
1860 * The ThinkPad firmware has two main event interfaces: normal ACPI
1861 * notifications (which follow the ACPI standard), and a private event
1862 * interface.
1863 *
1864 * The private event interface also issues events for the hotkeys. As
1865 * the driver gained features, the event handling code ended up being
1866 * built around the hotkey subdriver. This will need to be refactored
1867 * to a more formal event API eventually.
1868 *
1869 * Some "hotkeys" are actually supposed to be used as event reports,
1870 * such as "brightness has changed", "volume has changed", depending on
1871 * the ThinkPad model and how the firmware is operating.
1872 *
1873 * Unlike other classes, hotkey-class events have mask/unmask control on
1874 * non-ancient firmware. However, how it behaves changes a lot with the
1875 * firmware model and version.
1876 */
1877
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001878enum { /* hot key scan codes (derived from ACPI DSDT) */
1879 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1880 TP_ACPI_HOTKEYSCAN_FNF2,
1881 TP_ACPI_HOTKEYSCAN_FNF3,
1882 TP_ACPI_HOTKEYSCAN_FNF4,
1883 TP_ACPI_HOTKEYSCAN_FNF5,
1884 TP_ACPI_HOTKEYSCAN_FNF6,
1885 TP_ACPI_HOTKEYSCAN_FNF7,
1886 TP_ACPI_HOTKEYSCAN_FNF8,
1887 TP_ACPI_HOTKEYSCAN_FNF9,
1888 TP_ACPI_HOTKEYSCAN_FNF10,
1889 TP_ACPI_HOTKEYSCAN_FNF11,
1890 TP_ACPI_HOTKEYSCAN_FNF12,
1891 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1892 TP_ACPI_HOTKEYSCAN_FNINSERT,
1893 TP_ACPI_HOTKEYSCAN_FNDELETE,
1894 TP_ACPI_HOTKEYSCAN_FNHOME,
1895 TP_ACPI_HOTKEYSCAN_FNEND,
1896 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1897 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1898 TP_ACPI_HOTKEYSCAN_FNSPACE,
1899 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1900 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1901 TP_ACPI_HOTKEYSCAN_MUTE,
1902 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001903 TP_ACPI_HOTKEYSCAN_UNK1,
1904 TP_ACPI_HOTKEYSCAN_UNK2,
1905 TP_ACPI_HOTKEYSCAN_UNK3,
1906 TP_ACPI_HOTKEYSCAN_UNK4,
1907 TP_ACPI_HOTKEYSCAN_UNK5,
1908 TP_ACPI_HOTKEYSCAN_UNK6,
1909 TP_ACPI_HOTKEYSCAN_UNK7,
1910 TP_ACPI_HOTKEYSCAN_UNK8,
1911
Christian Kellner149c8c72017-02-28 17:10:56 +01001912 /* Adaptive keyboard keycodes */
1913 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1914 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
Bastien Nocera6a68d852015-03-02 14:45:31 +01001915 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1916 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1917 TP_ACPI_HOTKEYSCAN_CLOUD,
1918 TP_ACPI_HOTKEYSCAN_UNK9,
1919 TP_ACPI_HOTKEYSCAN_VOICE,
1920 TP_ACPI_HOTKEYSCAN_UNK10,
1921 TP_ACPI_HOTKEYSCAN_GESTURES,
1922 TP_ACPI_HOTKEYSCAN_UNK11,
1923 TP_ACPI_HOTKEYSCAN_UNK12,
1924 TP_ACPI_HOTKEYSCAN_UNK13,
1925 TP_ACPI_HOTKEYSCAN_CONFIG,
1926 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1927 TP_ACPI_HOTKEYSCAN_RELOAD,
1928 TP_ACPI_HOTKEYSCAN_BACK,
1929 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1930 TP_ACPI_HOTKEYSCAN_MIC_UP,
1931 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1932 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1933 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1934
Christian Kellner696c65232017-02-28 17:10:57 +01001935 /* Lenovo extended keymap, starting at 0x1300 */
1936 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1937 /* first new observed key (star, favorites) is 0x1311 */
1938 TP_ACPI_HOTKEYSCAN_STAR = 69,
1939 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
Benjamin Bergcb5c1972018-06-20 15:49:27 +02001940 TP_ACPI_HOTKEYSCAN_CALCULATOR,
Christian Kellner696c65232017-02-28 17:10:57 +01001941 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1942 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1943
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001944 /* Hotkey keymap size */
1945 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001946};
1947
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001948enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001949 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1950 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1951};
1952
1953enum { /* Positions of some of the keys in hotkey masks */
1954 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1955 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1956 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1957 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1958 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
Hans de Goedec685e202017-02-09 16:44:13 +01001959 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001960 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1961 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1962 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1963 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1964 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1965};
1966
1967enum { /* NVRAM to ACPI HKEY group map */
1968 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1969 TP_ACPI_HKEY_ZOOM_MASK |
1970 TP_ACPI_HKEY_DISPSWTCH_MASK |
1971 TP_ACPI_HKEY_HIBERNATE_MASK,
1972 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1973 TP_ACPI_HKEY_BRGHTDWN_MASK,
1974 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1975 TP_ACPI_HKEY_VOLDWN_MASK |
1976 TP_ACPI_HKEY_MUTE_MASK,
1977};
1978
1979#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1980struct tp_nvram_state {
1981 u16 thinkpad_toggle:1;
1982 u16 zoom_toggle:1;
1983 u16 display_toggle:1;
1984 u16 thinklight_toggle:1;
1985 u16 hibernate_toggle:1;
1986 u16 displayexp_toggle:1;
1987 u16 display_state:1;
1988 u16 brightness_toggle:1;
1989 u16 volume_toggle:1;
1990 u16 mute:1;
1991
1992 u8 brightness_level;
1993 u8 volume_level;
1994};
1995
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001996/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001997static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001998
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001999/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002000 * Acquire mutex to write poller control variables as an
2001 * atomic block.
2002 *
2003 * Increment hotkey_config_change when changing them if you
2004 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002005 *
2006 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2007 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002008static struct mutex hotkey_thread_data_mutex;
2009static unsigned int hotkey_config_change;
2010
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002011/*
2012 * hotkey poller control variables
2013 *
2014 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002015 *
2016 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2017 * should be used only when the changes need to be taken as
2018 * a block, OR when one needs to force the kthread to forget
2019 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002020 */
2021static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2022static unsigned int hotkey_poll_freq = 10; /* Hz */
2023
2024#define HOTKEY_CONFIG_CRITICAL_START \
2025 do { \
2026 mutex_lock(&hotkey_thread_data_mutex); \
2027 hotkey_config_change++; \
2028 } while (0);
2029#define HOTKEY_CONFIG_CRITICAL_END \
2030 mutex_unlock(&hotkey_thread_data_mutex);
2031
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002032#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2033
2034#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002035#define HOTKEY_CONFIG_CRITICAL_START
2036#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002037
2038#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2039
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002040static struct mutex hotkey_mutex;
2041
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002042static enum { /* Reasons for waking up */
2043 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2044 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2045 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2046} hotkey_wakeup_reason;
2047
2048static int hotkey_autosleep_ack;
2049
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002050static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2051static u32 hotkey_all_mask; /* all events supported in fw */
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002052static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002053static u32 hotkey_reserved_mask; /* events better left disabled */
2054static u32 hotkey_driver_mask; /* events needed by the driver */
2055static u32 hotkey_user_mask; /* events visible to userspace */
2056static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002057
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002058static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002059
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002060static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002061
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002062static void tpacpi_driver_event(const unsigned int hkey_event);
2063static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002064static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002065
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002066/* HKEY.MHKG() return bits */
2067#define TP_HOTKEY_TABLET_MASK (1 << 3)
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002068enum {
2069 TP_ACPI_MULTI_MODE_INVALID = 0,
2070 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
2071 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
2072 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
2073 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
2074 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
2075 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
2076 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
2077};
2078
2079enum {
2080 /* The following modes are considered tablet mode for the purpose of
2081 * reporting the status to userspace. i.e. in all these modes it makes
2082 * sense to disable the laptop input devices such as touchpad and
2083 * keyboard.
2084 */
2085 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
2086 TP_ACPI_MULTI_MODE_STAND |
2087 TP_ACPI_MULTI_MODE_TENT |
2088 TP_ACPI_MULTI_MODE_STAND_TENT,
2089};
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002090
Johannes Berg19d337d2009-06-02 13:01:37 +02002091static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002092{
Johannes Berg19d337d2009-06-02 13:01:37 +02002093 int status;
2094
2095 if (!tp_features.hotkey_wlsw)
2096 return -ENODEV;
2097
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002098#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002099 if (dbg_wlswemul)
2100 return (tpacpi_wlsw_emulstate) ?
2101 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002102#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002103
2104 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002105 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002106
2107 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002108}
2109
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002110static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
2111{
2112 int type = (s >> 16) & 0xffff;
2113 int value = s & 0xffff;
2114 int mode = TP_ACPI_MULTI_MODE_INVALID;
2115 int valid_modes = 0;
2116
2117 if (has_tablet_mode)
2118 *has_tablet_mode = 0;
2119
2120 switch (type) {
2121 case 1:
2122 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2123 TP_ACPI_MULTI_MODE_TABLET |
2124 TP_ACPI_MULTI_MODE_STAND_TENT;
2125 break;
2126 case 2:
2127 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2128 TP_ACPI_MULTI_MODE_FLAT |
2129 TP_ACPI_MULTI_MODE_TABLET |
2130 TP_ACPI_MULTI_MODE_STAND |
2131 TP_ACPI_MULTI_MODE_TENT;
2132 break;
2133 case 3:
2134 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2135 TP_ACPI_MULTI_MODE_FLAT;
2136 break;
2137 case 4:
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002138 case 5:
Benjamin Berg26befef2017-11-14 17:14:14 +01002139 /* In mode 4, FLAT is not specified as a valid mode. However,
2140 * it can be seen at least on the X1 Yoga 2nd Generation.
2141 */
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002142 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2143 TP_ACPI_MULTI_MODE_FLAT |
2144 TP_ACPI_MULTI_MODE_TABLET |
2145 TP_ACPI_MULTI_MODE_STAND |
2146 TP_ACPI_MULTI_MODE_TENT;
2147 break;
2148 default:
2149 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2150 type, value, TPACPI_MAIL);
2151 return 0;
2152 }
2153
2154 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2155 *has_tablet_mode = 1;
2156
2157 switch (value) {
2158 case 1:
2159 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2160 break;
2161 case 2:
2162 mode = TP_ACPI_MULTI_MODE_FLAT;
2163 break;
2164 case 3:
2165 mode = TP_ACPI_MULTI_MODE_TABLET;
2166 break;
2167 case 4:
2168 if (type == 1)
2169 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2170 else
2171 mode = TP_ACPI_MULTI_MODE_STAND;
2172 break;
2173 case 5:
2174 mode = TP_ACPI_MULTI_MODE_TENT;
2175 break;
2176 default:
2177 if (type == 5 && value == 0xffff) {
2178 pr_warn("Multi mode status is undetected, assuming laptop\n");
2179 return 0;
2180 }
2181 }
2182
2183 if (!(mode & valid_modes)) {
2184 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2185 value, type, TPACPI_MAIL);
2186 return 0;
2187 }
2188
2189 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2190}
2191
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002192static int hotkey_get_tablet_mode(int *status)
2193{
2194 int s;
2195
Lyudeb03f4d42016-11-11 15:15:03 -05002196 switch (tp_features.hotkey_tablet) {
2197 case TP_HOTKEY_TABLET_USES_MHKG:
2198 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2199 return -EIO;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002200
Lyudeb03f4d42016-11-11 15:15:03 -05002201 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2202 break;
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002203 case TP_HOTKEY_TABLET_USES_GMMS:
2204 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
Lyudeb03f4d42016-11-11 15:15:03 -05002205 return -EIO;
2206
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002207 *status = hotkey_gmms_get_tablet_mode(s, NULL);
Lyudeb03f4d42016-11-11 15:15:03 -05002208 break;
2209 default:
2210 break;
2211 }
2212
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002213 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002214}
2215
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002216/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002217 * Reads current event mask from firmware, and updates
2218 * hotkey_acpi_mask accordingly. Also resets any bits
2219 * from hotkey_user_mask that are unavailable to be
2220 * delivered (shadow requirement of the userspace ABI).
2221 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002222 * Call with hotkey_mutex held
2223 */
2224static int hotkey_mask_get(void)
2225{
2226 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002227 u32 m = 0;
2228
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002229 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002230 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002231
2232 hotkey_acpi_mask = m;
2233 } else {
2234 /* no mask support doesn't mean no event support... */
2235 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002236 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002237
2238 /* sync userspace-visible mask */
2239 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002240
2241 return 0;
2242}
2243
Jean Delvaredf6dd1b2015-04-27 09:45:06 +02002244static void hotkey_mask_warn_incomplete_mask(void)
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002245{
2246 /* log only what the user can fix... */
2247 const u32 wantedmask = hotkey_driver_mask &
2248 ~(hotkey_acpi_mask | hotkey_source_mask) &
2249 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2250
2251 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002252 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002253}
2254
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002255/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002256 * Set the firmware mask when supported
2257 *
2258 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2259 *
2260 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2261 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002262 * Call with hotkey_mutex held
2263 */
2264static int hotkey_mask_set(u32 mask)
2265{
2266 int i;
2267 int rc = 0;
2268
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002269 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002270
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002271 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002272 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002273 if (!acpi_evalf(hkey_handle,
2274 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002275 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002276 rc = -EIO;
2277 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002278 }
2279 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002280 }
2281
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002282 /*
2283 * We *must* make an inconditional call to hotkey_mask_get to
2284 * refresh hotkey_acpi_mask and update hotkey_user_mask
2285 *
2286 * Take the opportunity to also log when we cannot _enable_
2287 * a given event.
2288 */
2289 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002290 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002291 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002292 }
2293
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002294 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2295 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002296
2297 return rc;
2298}
2299
2300/*
2301 * Sets hotkey_user_mask and tries to set the firmware mask
2302 *
2303 * Call with hotkey_mutex held
2304 */
2305static int hotkey_user_mask_set(const u32 mask)
2306{
2307 int rc;
2308
2309 /* Give people a chance to notice they are doing something that
2310 * is bound to go boom on their users sooner or later */
2311 if (!tp_warned.hotkey_mask_ff &&
2312 (mask == 0xffff || mask == 0xffffff ||
2313 mask == 0xffffffff)) {
2314 tp_warned.hotkey_mask_ff = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002315 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2316 mask);
2317 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 -03002318 }
2319
2320 /* Try to enable what the user asked for, plus whatever we need.
2321 * this syncs everything but won't enable bits in hotkey_user_mask */
2322 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2323
2324 /* Enable the available bits in hotkey_user_mask */
2325 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2326
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002327 return rc;
2328}
2329
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002330/*
2331 * Sets the driver hotkey mask.
2332 *
2333 * Can be called even if the hotkey subdriver is inactive
2334 */
2335static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2336{
2337 int rc;
2338
2339 /* Do the right thing if hotkey_init has not been called yet */
2340 if (!tp_features.hotkey) {
2341 hotkey_driver_mask = mask;
2342 return 0;
2343 }
2344
2345 mutex_lock(&hotkey_mutex);
2346
2347 HOTKEY_CONFIG_CRITICAL_START
2348 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002349#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002350 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002351#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002352 HOTKEY_CONFIG_CRITICAL_END
2353
2354 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2355 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002356 hotkey_poll_setup(true);
2357
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002358 mutex_unlock(&hotkey_mutex);
2359
2360 return rc;
2361}
2362
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002363static int hotkey_status_get(int *status)
2364{
2365 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2366 return -EIO;
2367
2368 return 0;
2369}
2370
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002371static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002372{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002373 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002374 return -EIO;
2375
2376 return 0;
2377}
2378
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002379static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002380{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002381 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002382
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002383 if (tp_features.hotkey_tablet &&
2384 !hotkey_get_tablet_mode(&state)) {
2385 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002386
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002387 input_report_switch(tpacpi_inputdev,
2388 SW_TABLET_MODE, !!state);
2389 input_sync(tpacpi_inputdev);
2390
2391 mutex_unlock(&tpacpi_inputdev_send_mutex);
2392 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002393}
2394
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002395/* Do NOT call without validating scancode first */
2396static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002397{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002398 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002399
2400 if (keycode != KEY_RESERVED) {
2401 mutex_lock(&tpacpi_inputdev_send_mutex);
2402
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002403 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002404 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002405 input_sync(tpacpi_inputdev);
2406
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002407 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002408 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002409 input_sync(tpacpi_inputdev);
2410
2411 mutex_unlock(&tpacpi_inputdev_send_mutex);
2412 }
2413}
2414
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002415/* Do NOT call without validating scancode first */
2416static void tpacpi_input_send_key_masked(const unsigned int scancode)
2417{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002418 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002419 if (hotkey_user_mask & (1 << scancode))
2420 tpacpi_input_send_key(scancode);
2421}
2422
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002423#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2424static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2425
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002426/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002427static void tpacpi_hotkey_send_key(unsigned int scancode)
2428{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002429 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002430}
2431
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002432static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002433{
2434 u8 d;
2435
2436 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2437 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2438 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2439 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2440 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2441 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2442 }
Hans de Goedec685e202017-02-09 16:44:13 +01002443 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002444 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2445 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2446 }
2447 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2448 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2449 n->displayexp_toggle =
2450 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2451 }
2452 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2453 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2454 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2455 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2456 n->brightness_toggle =
2457 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2458 }
2459 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2460 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2461 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2462 >> TP_NVRAM_POS_LEVEL_VOLUME;
2463 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2464 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2465 }
2466}
2467
Behan Webstera4d44ba2014-02-12 21:58:46 +01002468#define TPACPI_COMPARE_KEY(__scancode, __member) \
2469do { \
2470 if ((event_mask & (1 << __scancode)) && \
2471 oldn->__member != newn->__member) \
2472 tpacpi_hotkey_send_key(__scancode); \
2473} while (0)
2474
2475#define TPACPI_MAY_SEND_KEY(__scancode) \
2476do { \
2477 if (event_mask & (1 << __scancode)) \
2478 tpacpi_hotkey_send_key(__scancode); \
2479} while (0)
2480
2481static void issue_volchange(const unsigned int oldvol,
2482 const unsigned int newvol,
2483 const u32 event_mask)
2484{
2485 unsigned int i = oldvol;
2486
2487 while (i > newvol) {
2488 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2489 i--;
2490 }
2491 while (i < newvol) {
2492 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2493 i++;
2494 }
2495}
2496
2497static void issue_brightnesschange(const unsigned int oldbrt,
2498 const unsigned int newbrt,
2499 const u32 event_mask)
2500{
2501 unsigned int i = oldbrt;
2502
2503 while (i > newbrt) {
2504 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2505 i--;
2506 }
2507 while (i < newbrt) {
2508 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2509 i++;
2510 }
2511}
2512
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002513static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2514 struct tp_nvram_state *newn,
2515 const u32 event_mask)
2516{
2517
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002518 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2519 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2520 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2521 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2522
2523 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2524
2525 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2526
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002527 /*
2528 * Handle volume
2529 *
2530 * This code is supposed to duplicate the IBM firmware behaviour:
2531 * - Pressing MUTE issues mute hotkey message, even when already mute
2532 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002533 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002534 * - The act of unmuting issues volume up/down notification,
2535 * depending which key was used to unmute
2536 *
2537 * We are constrained to what the NVRAM can tell us, which is not much
2538 * and certainly not enough if more than one volume hotkey was pressed
2539 * since the last poll cycle.
2540 *
2541 * Just to make our life interesting, some newer Lenovo ThinkPads have
2542 * bugs in the BIOS and may fail to update volume_toggle properly.
2543 */
2544 if (newn->mute) {
2545 /* muted */
2546 if (!oldn->mute ||
2547 oldn->volume_toggle != newn->volume_toggle ||
2548 oldn->volume_level != newn->volume_level) {
2549 /* recently muted, or repeated mute keypress, or
2550 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002551 issue_volchange(oldn->volume_level, newn->volume_level,
2552 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002553 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2554 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002555 } else {
2556 /* unmute */
2557 if (oldn->mute) {
2558 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002559 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002560 }
2561 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002562 issue_volchange(oldn->volume_level, newn->volume_level,
2563 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002564 } else if (oldn->volume_toggle != newn->volume_toggle) {
2565 /* repeated vol up/down keypress at end of scale ? */
2566 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002567 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002568 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2569 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002570 }
2571 }
2572
2573 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002574 if (oldn->brightness_level != newn->brightness_level) {
2575 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002576 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002577 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2578 /* repeated key presses that didn't change state */
2579 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002580 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002581 else if (newn->brightness_level >= bright_maxlvl
2582 && !tp_features.bright_unkfw)
2583 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002584 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002585
2586#undef TPACPI_COMPARE_KEY
2587#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002588}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002589
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002590/*
2591 * Polling driver
2592 *
2593 * We track all events in hotkey_source_mask all the time, since
2594 * most of them are edge-based. We only issue those requested by
2595 * hotkey_user_mask or hotkey_driver_mask, though.
2596 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002597static int hotkey_kthread(void *data)
2598{
2599 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002600 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002601 unsigned int si, so;
2602 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002603 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002604 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002605 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002606
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002607 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2608 goto exit;
2609
2610 set_freezable();
2611
2612 so = 0;
2613 si = 1;
2614 t = 0;
2615
2616 /* Initial state for compares */
2617 mutex_lock(&hotkey_thread_data_mutex);
2618 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002619 poll_mask = hotkey_source_mask;
2620 event_mask = hotkey_source_mask &
2621 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002622 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002623 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002624 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002625
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002626 while (!kthread_should_stop()) {
2627 if (t == 0) {
2628 if (likely(poll_freq))
2629 t = 1000/poll_freq;
2630 else
2631 t = 100; /* should never happen... */
2632 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002633 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002634 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002635 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002636
2637 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002638 continue;
2639
2640 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002641 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002642 /* forget old state on thaw or config change */
2643 si = so;
2644 t = 0;
2645 change_detector = hotkey_config_change;
2646 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002647 poll_mask = hotkey_source_mask;
2648 event_mask = hotkey_source_mask &
2649 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002650 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002651 mutex_unlock(&hotkey_thread_data_mutex);
2652
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002653 if (likely(poll_mask)) {
2654 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002655 if (likely(si != so)) {
2656 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002657 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002658 }
2659 }
2660
2661 so = si;
2662 si ^= 1;
2663 }
2664
2665exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002666 return 0;
2667}
2668
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002669/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002670static void hotkey_poll_stop_sync(void)
2671{
2672 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002673 kthread_stop(tpacpi_hotkey_task);
2674 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002675 }
2676}
2677
2678/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002679static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002680{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002681 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2682 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002683
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002684 if (hotkey_poll_freq > 0 &&
2685 (poll_driver_mask ||
2686 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002687 if (!tpacpi_hotkey_task) {
2688 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002689 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002690 if (IS_ERR(tpacpi_hotkey_task)) {
2691 tpacpi_hotkey_task = NULL;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002692 pr_err("could not create kernel thread for hotkey polling\n");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002693 }
2694 }
2695 } else {
2696 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002697 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002698 hotkey_poll_freq == 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002699 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 -07002700 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002701 }
2702 }
2703}
2704
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002705static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002706{
2707 mutex_lock(&hotkey_mutex);
2708 hotkey_poll_setup(may_warn);
2709 mutex_unlock(&hotkey_mutex);
2710}
2711
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002712/* call with hotkey_mutex held */
2713static void hotkey_poll_set_freq(unsigned int freq)
2714{
2715 if (!freq)
2716 hotkey_poll_stop_sync();
2717
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002718 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002719}
2720
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002721#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2722
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002723static void hotkey_poll_setup(const bool __unused)
2724{
2725}
2726
2727static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002728{
2729}
2730
2731#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2732
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002733static int hotkey_inputdev_open(struct input_dev *dev)
2734{
2735 switch (tpacpi_lifecycle) {
2736 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002737 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002738 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002739 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002740 case TPACPI_LIFE_EXITING:
2741 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002742 }
2743
2744 /* Should only happen if tpacpi_lifecycle is corrupt */
2745 BUG();
2746 return -EBUSY;
2747}
2748
2749static void hotkey_inputdev_close(struct input_dev *dev)
2750{
2751 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002752 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002753 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002754 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002755}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002756
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002757/* sysfs hotkey enable ------------------------------------------------- */
2758static ssize_t hotkey_enable_show(struct device *dev,
2759 struct device_attribute *attr,
2760 char *buf)
2761{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002762 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002763
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002764 printk_deprecated_attribute("hotkey_enable",
2765 "Hotkey reporting is always enabled");
2766
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002767 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002768 if (res)
2769 return res;
2770
2771 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2772}
2773
2774static ssize_t hotkey_enable_store(struct device *dev,
2775 struct device_attribute *attr,
2776 const char *buf, size_t count)
2777{
2778 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002779
2780 printk_deprecated_attribute("hotkey_enable",
2781 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002782
2783 if (parse_strtoul(buf, 1, &t))
2784 return -EINVAL;
2785
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002786 if (t == 0)
2787 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002788
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002789 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002790}
2791
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002792static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002793
2794/* sysfs hotkey mask --------------------------------------------------- */
2795static ssize_t hotkey_mask_show(struct device *dev,
2796 struct device_attribute *attr,
2797 char *buf)
2798{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002799 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002800}
2801
2802static ssize_t hotkey_mask_store(struct device *dev,
2803 struct device_attribute *attr,
2804 const char *buf, size_t count)
2805{
2806 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002807 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002808
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002809 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002810 return -EINVAL;
2811
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002812 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002813 return -ERESTARTSYS;
2814
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002815 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002816
2817#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002818 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002819#endif
2820
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002821 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002822
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002823 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2824
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002825 return (res) ? res : count;
2826}
2827
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002828static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002829
2830/* sysfs hotkey bios_enabled ------------------------------------------- */
2831static ssize_t hotkey_bios_enabled_show(struct device *dev,
2832 struct device_attribute *attr,
2833 char *buf)
2834{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002835 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002836}
2837
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002838static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002839
2840/* sysfs hotkey bios_mask ---------------------------------------------- */
2841static ssize_t hotkey_bios_mask_show(struct device *dev,
2842 struct device_attribute *attr,
2843 char *buf)
2844{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002845 printk_deprecated_attribute("hotkey_bios_mask",
2846 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002847 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002848}
2849
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002850static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002851
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002852/* sysfs hotkey all_mask ----------------------------------------------- */
2853static ssize_t hotkey_all_mask_show(struct device *dev,
2854 struct device_attribute *attr,
2855 char *buf)
2856{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002857 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2858 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002859}
2860
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002861static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002862
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002863/* sysfs hotkey all_mask ----------------------------------------------- */
2864static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2865 struct device_attribute *attr,
2866 char *buf)
2867{
2868 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2869 hotkey_adaptive_all_mask | hotkey_source_mask);
2870}
2871
2872static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2873
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002874/* sysfs hotkey recommended_mask --------------------------------------- */
2875static ssize_t hotkey_recommended_mask_show(struct device *dev,
2876 struct device_attribute *attr,
2877 char *buf)
2878{
2879 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002880 (hotkey_all_mask | hotkey_source_mask)
2881 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002882}
2883
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002884static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002885
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002886#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2887
2888/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2889static ssize_t hotkey_source_mask_show(struct device *dev,
2890 struct device_attribute *attr,
2891 char *buf)
2892{
2893 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2894}
2895
2896static ssize_t hotkey_source_mask_store(struct device *dev,
2897 struct device_attribute *attr,
2898 const char *buf, size_t count)
2899{
2900 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002901 u32 r_ev;
2902 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002903
2904 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2905 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2906 return -EINVAL;
2907
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002908 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002909 return -ERESTARTSYS;
2910
2911 HOTKEY_CONFIG_CRITICAL_START
2912 hotkey_source_mask = t;
2913 HOTKEY_CONFIG_CRITICAL_END
2914
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002915 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2916 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002917 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002918
2919 /* check if events needed by the driver got disabled */
2920 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2921 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002922
2923 mutex_unlock(&hotkey_mutex);
2924
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002925 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002926 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002927
2928 if (r_ev)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002929 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002930 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002931
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002932 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2933
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002934 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002935}
2936
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002937static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002938
2939/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2940static ssize_t hotkey_poll_freq_show(struct device *dev,
2941 struct device_attribute *attr,
2942 char *buf)
2943{
2944 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2945}
2946
2947static ssize_t hotkey_poll_freq_store(struct device *dev,
2948 struct device_attribute *attr,
2949 const char *buf, size_t count)
2950{
2951 unsigned long t;
2952
2953 if (parse_strtoul(buf, 25, &t))
2954 return -EINVAL;
2955
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002956 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002957 return -ERESTARTSYS;
2958
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002959 hotkey_poll_set_freq(t);
2960 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002961
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002962 mutex_unlock(&hotkey_mutex);
2963
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002964 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2965
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002966 return count;
2967}
2968
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002969static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002970
2971#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2972
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002973/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002974static ssize_t hotkey_radio_sw_show(struct device *dev,
2975 struct device_attribute *attr,
2976 char *buf)
2977{
Johannes Berg19d337d2009-06-02 13:01:37 +02002978 int res;
2979 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002980 if (res < 0)
2981 return res;
2982
Johannes Berg19d337d2009-06-02 13:01:37 +02002983 /* Opportunistic update */
2984 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2985
2986 return snprintf(buf, PAGE_SIZE, "%d\n",
2987 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002988}
2989
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002990static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002991
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002992static void hotkey_radio_sw_notify_change(void)
2993{
2994 if (tp_features.hotkey_wlsw)
2995 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2996 "hotkey_radio_sw");
2997}
2998
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002999/* sysfs hotkey tablet mode (pollable) --------------------------------- */
3000static ssize_t hotkey_tablet_mode_show(struct device *dev,
3001 struct device_attribute *attr,
3002 char *buf)
3003{
3004 int res, s;
3005 res = hotkey_get_tablet_mode(&s);
3006 if (res < 0)
3007 return res;
3008
3009 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
3010}
3011
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01003012static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003013
3014static void hotkey_tablet_mode_notify_change(void)
3015{
3016 if (tp_features.hotkey_tablet)
3017 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3018 "hotkey_tablet_mode");
3019}
3020
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003021/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003022static ssize_t hotkey_wakeup_reason_show(struct device *dev,
3023 struct device_attribute *attr,
3024 char *buf)
3025{
3026 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
3027}
3028
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003029static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003030
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02003031static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003032{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003033 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3034 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003035}
3036
3037/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003038static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
3039 struct device_attribute *attr,
3040 char *buf)
3041{
3042 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
3043}
3044
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003045static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
3046 hotkey_wakeup_hotunplug_complete_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003047
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02003048static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003049{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003050 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3051 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003052}
3053
Bastien Nocerab790cee2015-03-02 14:45:27 +01003054/* sysfs adaptive kbd mode --------------------------------------------- */
3055
3056static int adaptive_keyboard_get_mode(void);
3057static int adaptive_keyboard_set_mode(int new_mode);
3058
3059enum ADAPTIVE_KEY_MODE {
3060 HOME_MODE,
3061 WEB_BROWSER_MODE,
3062 WEB_CONFERENCE_MODE,
3063 FUNCTION_MODE,
3064 LAYFLAT_MODE
3065};
3066
3067static ssize_t adaptive_kbd_mode_show(struct device *dev,
3068 struct device_attribute *attr,
3069 char *buf)
3070{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003071 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01003072
3073 current_mode = adaptive_keyboard_get_mode();
3074 if (current_mode < 0)
3075 return current_mode;
3076
3077 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
3078}
3079
3080static ssize_t adaptive_kbd_mode_store(struct device *dev,
3081 struct device_attribute *attr,
3082 const char *buf, size_t count)
3083{
3084 unsigned long t;
3085 int res;
3086
3087 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
3088 return -EINVAL;
3089
3090 res = adaptive_keyboard_set_mode(t);
3091 return (res < 0) ? res : count;
3092}
3093
3094static DEVICE_ATTR_RW(adaptive_kbd_mode);
3095
3096static struct attribute *adaptive_kbd_attributes[] = {
3097 &dev_attr_adaptive_kbd_mode.attr,
3098 NULL
3099};
3100
3101static const struct attribute_group adaptive_kbd_attr_group = {
3102 .attrs = adaptive_kbd_attributes,
3103};
3104
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003105/* --------------------------------------------------------------------- */
3106
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003107static struct attribute *hotkey_attributes[] __initdata = {
3108 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003109 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003110 &dev_attr_hotkey_bios_mask.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003111 &dev_attr_wakeup_reason.attr,
3112 &dev_attr_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003113 &dev_attr_hotkey_mask.attr,
3114 &dev_attr_hotkey_all_mask.attr,
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003115 &dev_attr_hotkey_adaptive_all_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003116 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003117#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003118 &dev_attr_hotkey_source_mask.attr,
3119 &dev_attr_hotkey_poll_freq.attr,
3120#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003121};
3122
Johannes Berg19d337d2009-06-02 13:01:37 +02003123/*
3124 * Sync both the hw and sw blocking state of all switches
3125 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003126static void tpacpi_send_radiosw_update(void)
3127{
3128 int wlsw;
3129
Johannes Berg19d337d2009-06-02 13:01:37 +02003130 /*
3131 * We must sync all rfkill controllers *before* issuing any
3132 * rfkill input events, or we will race the rfkill core input
3133 * handler.
3134 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003135 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003136 * for the above.
3137 *
3138 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3139 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003140
Johannes Berg19d337d2009-06-02 13:01:37 +02003141 wlsw = hotkey_get_wlsw();
3142
3143 /* Sync hw blocking state first if it is hw-blocked */
3144 if (wlsw == TPACPI_RFK_RADIO_OFF)
3145 tpacpi_rfk_update_hwblock_state(true);
3146
3147 /* Sync sw blocking state */
3148 tpacpi_rfk_update_swstate_all();
3149
3150 /* Sync hw blocking state last if it is hw-unblocked */
3151 if (wlsw == TPACPI_RFK_RADIO_ON)
3152 tpacpi_rfk_update_hwblock_state(false);
3153
3154 /* Issue rfkill input event for WLSW switch */
3155 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003156 mutex_lock(&tpacpi_inputdev_send_mutex);
3157
3158 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003159 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003160 input_sync(tpacpi_inputdev);
3161
3162 mutex_unlock(&tpacpi_inputdev_send_mutex);
3163 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003164
3165 /*
3166 * this can be unconditional, as we will poll state again
3167 * if userspace uses the notify to read data
3168 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003169 hotkey_radio_sw_notify_change();
3170}
3171
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003172static void hotkey_exit(void)
3173{
3174#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003175 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003176 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003177 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003178#endif
3179
3180 if (hotkey_dev_attributes)
3181 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3182
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003183 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003184 "restoring original HKEY status and mask\n");
3185 /* yes, there is a bitwise or below, we want the
3186 * functions to be called even if one of them fail */
3187 if (((tp_features.hotkey_mask &&
3188 hotkey_mask_set(hotkey_orig_mask)) |
3189 hotkey_status_set(false)) != 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003190 pr_err("failed to restore hot key mask to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003191}
3192
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003193static void __init hotkey_unmap(const unsigned int scancode)
3194{
3195 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3196 clear_bit(hotkey_keycode_map[scancode],
3197 tpacpi_inputdev->keybit);
3198 hotkey_keycode_map[scancode] = KEY_RESERVED;
3199 }
3200}
3201
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003202/*
3203 * HKEY quirks:
3204 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3205 */
3206
3207#define TPACPI_HK_Q_INIMASK 0x0001
3208
3209static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3210 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3211 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3212 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3213 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3214 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3215 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3216 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3217 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3218 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3219 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3220 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3221 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3222 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3223 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3224 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3225 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3226 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3227 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3228 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3229};
3230
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003231typedef u16 tpacpi_keymap_entry_t;
3232typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003233
Lyudeb3180022016-11-11 15:15:02 -05003234static int hotkey_init_tablet_mode(void)
3235{
Darren Hartcb2bf252016-12-14 14:45:48 -08003236 int in_tablet_mode = 0, res;
3237 char *type = NULL;
Lyudeb3180022016-11-11 15:15:02 -05003238
Benjamin Bergdda3ec02017-09-15 15:20:49 +02003239 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3240 int has_tablet_mode;
3241
3242 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3243 &has_tablet_mode);
3244 if (has_tablet_mode)
3245 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3246 type = "GMMS";
3247 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
Lyudeb03f4d42016-11-11 15:15:03 -05003248 /* For X41t, X60t, X61t Tablets... */
Lyudeb3180022016-11-11 15:15:02 -05003249 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3250 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3251 type = "MHKG";
3252 }
3253
3254 if (!tp_features.hotkey_tablet)
3255 return 0;
3256
3257 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3258 type, in_tablet_mode ? "tablet" : "laptop");
3259
3260 res = add_to_attr_set(hotkey_dev_attributes,
3261 &dev_attr_hotkey_tablet_mode.attr);
3262 if (res)
3263 return -1;
3264
3265 return in_tablet_mode;
3266}
3267
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003268static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003269{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003270 /* Requirements for changing the default keymaps:
3271 *
3272 * 1. Many of the keys are mapped to KEY_RESERVED for very
3273 * good reasons. Do not change them unless you have deep
3274 * knowledge on the IBM and Lenovo ThinkPad firmware for
3275 * the various ThinkPad models. The driver behaves
3276 * differently for KEY_RESERVED: such keys have their
3277 * hot key mask *unset* in mask_recommended, and also
3278 * in the initial hot key mask programmed into the
3279 * firmware at driver load time, which means the firm-
3280 * ware may react very differently if you change them to
3281 * something else;
3282 *
3283 * 2. You must be subscribed to the linux-thinkpad and
3284 * ibm-acpi-devel mailing lists, and you should read the
3285 * list archives since 2007 if you want to change the
3286 * keymaps. This requirement exists so that you will
3287 * know the past history of problems with the thinkpad-
3288 * acpi driver keymaps, and also that you will be
3289 * listening to any bug reports;
3290 *
3291 * 3. Do not send thinkpad-acpi specific patches directly to
3292 * for merging, *ever*. Send them to the linux-acpi
3293 * mailinglist for comments. Merging is to be done only
3294 * through acpi-test and the ACPI maintainer.
3295 *
3296 * If the above is too much to ask, don't change the keymap.
3297 * Ask the thinkpad-acpi maintainer to do it, instead.
3298 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003299
3300 enum keymap_index {
3301 TPACPI_KEYMAP_IBM_GENERIC = 0,
3302 TPACPI_KEYMAP_LENOVO_GENERIC,
3303 };
3304
3305 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3306 /* Generic keymap for IBM ThinkPads */
3307 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003308 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003309 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003310 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3311 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003312
3313 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003314 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3315 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3316 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003317
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003318 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003319 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003320 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003321
3322 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003323 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003324
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003325 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3326 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003327
3328 /* Volume: firmware always react to it and reprograms
3329 * the built-in *extra* mixer. Never map it to control
3330 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003331 KEY_RESERVED, /* 0x14: VOLUME UP */
3332 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3333 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003334
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003335 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003336
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003337 /* (assignments unknown, please report if found) */
3338 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3339 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003340
3341 /* No assignments, only used for Adaptive keyboards. */
3342 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3343 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3344 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3345 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3346 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Christian Kellner696c65232017-02-28 17:10:57 +01003347
3348 /* No assignment, used for newer Lenovo models */
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, KEY_UNKNOWN,
3353 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3354 KEY_UNKNOWN, KEY_UNKNOWN
3355
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003356 },
3357
3358 /* Generic keymap for Lenovo ThinkPads */
3359 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003360 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3361 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003362 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003363 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003364
3365 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003366 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3367 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3368 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003369
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003370 /* These should be enabled --only-- when ACPI video
3371 * is disabled (i.e. in "vendor" mode), and are handled
3372 * in a special way by the init code */
3373 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3374 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003375
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003376 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003377
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003378 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3379 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003380
3381 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3382 * react to it and reprograms the built-in *extra* mixer.
3383 * Never map it to control another mixer by default.
3384 *
3385 * T60?, T61, R60?, R61: firmware and EC tries to send
3386 * these over the regular keyboard, so these are no-ops,
3387 * but there are still weird bugs re. MUTE, so do not
3388 * change unless you get test reports from all Lenovo
3389 * models. May cause the BIOS to interfere with the
3390 * HDA mixer.
3391 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003392 KEY_RESERVED, /* 0x14: VOLUME UP */
3393 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3394 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003395
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003396 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003397
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003398 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003399 KEY_UNKNOWN, KEY_UNKNOWN,
3400
3401 /*
3402 * The mic mute button only sends 0x1a. It does not
3403 * automatically mute the mic or change the mute light.
3404 */
3405 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3406
3407 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003408 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003409
3410 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003411 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003412
3413 /*
3414 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3415 * The first item in this list is the Mute button which is
3416 * emitted with 0x103 through
3417 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3418 * symbol is held.
3419 * We'll need to offset those by 0x20.
3420 */
3421 KEY_RESERVED, /* Mute held, 0x103 */
3422 KEY_BRIGHTNESS_MIN, /* Backlight off */
3423 KEY_RESERVED, /* Clipping tool */
3424 KEY_RESERVED, /* Cloud */
3425 KEY_RESERVED,
3426 KEY_VOICECOMMAND, /* Voice */
3427 KEY_RESERVED,
3428 KEY_RESERVED, /* Gestures */
3429 KEY_RESERVED,
3430 KEY_RESERVED,
3431 KEY_RESERVED,
3432 KEY_CONFIG, /* Settings */
3433 KEY_RESERVED, /* New tab */
3434 KEY_REFRESH, /* Reload */
3435 KEY_BACK, /* Back */
3436 KEY_RESERVED, /* Microphone down */
3437 KEY_RESERVED, /* Microphone up */
3438 KEY_RESERVED, /* Microphone cancellation */
3439 KEY_RESERVED, /* Camera mode */
3440 KEY_RESERVED, /* Rotate display, 0x116 */
Christian Kellner696c65232017-02-28 17:10:57 +01003441
3442 /*
3443 * These are found in 2017 models (e.g. T470s, X270).
3444 * The lowest known value is 0x311, which according to
3445 * the manual should launch a user defined favorite
3446 * application.
3447 *
3448 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3449 * corresponding to 0x34.
3450 */
3451
3452 /* (assignments unknown, please report if found) */
3453 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3454 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3455 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3456 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3457 KEY_UNKNOWN,
3458
3459 KEY_FAVORITES, /* Favorite app, 0x311 */
3460 KEY_RESERVED, /* Clipping tool */
Benjamin Bergcb5c1972018-06-20 15:49:27 +02003461 KEY_CALC, /* Calculator (above numpad, P52) */
Christian Kellner696c65232017-02-28 17:10:57 +01003462 KEY_BLUETOOTH, /* Bluetooth */
3463 KEY_KEYBOARD /* Keyboard, 0x315 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003464 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003465 };
3466
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003467 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3468 /* Generic maps (fallback) */
3469 {
3470 .vendor = PCI_VENDOR_ID_IBM,
3471 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3472 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3473 },
3474 {
3475 .vendor = PCI_VENDOR_ID_LENOVO,
3476 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3477 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3478 },
3479 };
3480
3481#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003482#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003483
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003484 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003485 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003486 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003487 bool radiosw_state = false;
3488 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003489
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003490 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003491 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003492
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003493 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3494 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003495
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003496 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003497 BUG_ON(tpacpi_inputdev->open != NULL ||
3498 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003499
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003500 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003501 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003502
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003503#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003504 mutex_init(&hotkey_thread_data_mutex);
3505#endif
3506
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003507 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003508 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003509
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003510 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3511 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003512 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003513
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003514 if (!tp_features.hotkey)
3515 return 1;
3516
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003517 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3518 ARRAY_SIZE(tpacpi_hotkey_qtable));
3519
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003520 tpacpi_disable_brightness_delay();
3521
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003522 /* MUST have enough space for all attributes to be added to
3523 * hotkey_dev_attributes */
3524 hotkey_dev_attributes = create_attr_set(
3525 ARRAY_SIZE(hotkey_attributes) + 2,
3526 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003527 if (!hotkey_dev_attributes)
3528 return -ENOMEM;
3529 res = add_many_to_attr_set(hotkey_dev_attributes,
3530 hotkey_attributes,
3531 ARRAY_SIZE(hotkey_attributes));
3532 if (res)
3533 goto err_exit;
3534
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003535 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003536 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3537 for HKEY interface version 0x100 */
3538 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003539 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3540 "firmware HKEY interface version: 0x%x\n",
3541 hkeyv);
3542
3543 switch (hkeyv >> 8) {
3544 case 1:
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003545 /*
3546 * MHKV 0x100 in A31, R40, R40e,
3547 * T4x, X31, and later
3548 */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003549
3550 /* Paranoia check AND init hotkey_all_mask */
3551 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3552 "MHKA", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003553 pr_err("missing MHKA handler, please report this to %s\n",
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003554 TPACPI_MAIL);
3555 /* Fallback: pre-init for FN+F3,F4,F12 */
3556 hotkey_all_mask = 0x080cU;
3557 } else {
3558 tp_features.hotkey_mask = 1;
3559 }
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003560 break;
3561
3562 case 2:
3563 /*
3564 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3565 */
3566
3567 /* Paranoia check AND init hotkey_all_mask */
3568 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3569 "MHKA", "dd", 1)) {
3570 pr_err("missing MHKA handler, please report this to %s\n",
3571 TPACPI_MAIL);
3572 /* Fallback: pre-init for FN+F3,F4,F12 */
3573 hotkey_all_mask = 0x080cU;
3574 } else {
3575 tp_features.hotkey_mask = 1;
3576 }
3577
3578 /*
3579 * Check if we have an adaptive keyboard, like on the
3580 * Lenovo Carbon X1 2014 (2nd Gen).
3581 */
3582 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3583 "MHKA", "dd", 2)) {
3584 if (hotkey_adaptive_all_mask != 0) {
3585 tp_features.has_adaptive_kbd = true;
3586 res = sysfs_create_group(
3587 &tpacpi_pdev->dev.kobj,
3588 &adaptive_kbd_attr_group);
3589 if (res)
3590 goto err_exit;
3591 }
3592 } else {
3593 tp_features.has_adaptive_kbd = false;
3594 hotkey_adaptive_all_mask = 0x0U;
3595 }
3596 break;
3597
3598 default:
3599 pr_err("unknown version of the HKEY interface: 0x%x\n",
3600 hkeyv);
3601 pr_err("please report this to %s\n", TPACPI_MAIL);
3602 break;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003603 }
3604 }
3605
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003606 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3607 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003608 str_supported(tp_features.hotkey_mask));
3609
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003610 /* Init hotkey_all_mask if not initialized yet */
3611 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3612 (quirks & TPACPI_HK_Q_INIMASK))
3613 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003614
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003615 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003616 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003617 /* hotkey_source_mask *must* be zero for
3618 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003619 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003620 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003621 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003622
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003623 hotkey_orig_mask = hotkey_acpi_mask;
3624 } else {
3625 hotkey_orig_mask = hotkey_all_mask;
3626 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003627 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003628
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003629#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3630 if (dbg_wlswemul) {
3631 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003632 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003633 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003634 } else
3635#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003636 /* Not all thinkpads have a hardware radio switch */
3637 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3638 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003639 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003640 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003641 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003642 }
3643 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003644 res = add_to_attr_set(hotkey_dev_attributes,
3645 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003646
Lyudeb3180022016-11-11 15:15:02 -05003647 res = hotkey_init_tablet_mode();
3648 if (res < 0)
3649 goto err_exit;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003650
Lyudeb3180022016-11-11 15:15:02 -05003651 tabletsw_state = res;
3652
3653 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3654 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003655 if (res)
3656 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003657
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003658 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003659 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3660 GFP_KERNEL);
3661 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003662 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003663 res = -ENOMEM;
3664 goto err_exit;
3665 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003666
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003667 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3668 ARRAY_SIZE(tpacpi_keymap_qtable));
3669 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3670 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3671 "using keymap number %lu\n", keymap_id);
3672
3673 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3674 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003675
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003676 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003677 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3678 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3679 tpacpi_inputdev->keycode = hotkey_keycode_map;
3680 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3681 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003682 input_set_capability(tpacpi_inputdev, EV_KEY,
3683 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003684 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003685 if (i < sizeof(hotkey_reserved_mask)*8)
3686 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003687 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003688 }
3689
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003690 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003691 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003692 input_report_switch(tpacpi_inputdev,
3693 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003694 }
3695 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003696 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003697 input_report_switch(tpacpi_inputdev,
3698 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003699 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003700
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003701 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003702 * userspace. tpacpi_detect_brightness_capabilities() must have
3703 * been called before this point */
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02003704 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003705 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3706 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003707
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003708 /* Disable brightness up/down on Lenovo thinkpads when
3709 * ACPI is handling them, otherwise it is plain impossible
3710 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003711 hotkey_reserved_mask |=
3712 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3713 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003714 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3715 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003716 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003717
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003718#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003719 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3720 & ~hotkey_all_mask
3721 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003722
3723 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3724 "hotkey source mask 0x%08x, polling freq %u\n",
3725 hotkey_source_mask, hotkey_poll_freq);
3726#endif
3727
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003728 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3729 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003730 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003731 if (res) {
3732 hotkey_exit();
3733 return res;
3734 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003735 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3736 | hotkey_driver_mask)
3737 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003738 if (res < 0 && res != -ENXIO) {
3739 hotkey_exit();
3740 return res;
3741 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003742 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3743 & ~hotkey_reserved_mask;
3744 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3745 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3746 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003747
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003748 tpacpi_inputdev->open = &hotkey_inputdev_open;
3749 tpacpi_inputdev->close = &hotkey_inputdev_close;
3750
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003751 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003752
3753 return 0;
3754
3755err_exit:
3756 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
Bastien Nocerab790cee2015-03-02 14:45:27 +01003757 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3758 &adaptive_kbd_attr_group);
3759
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003760 hotkey_dev_attributes = NULL;
3761
Jan van den Berg72a979f2014-09-17 00:01:08 +02003762 return (res < 0) ? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003763}
3764
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003765/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3766 * mode, Web conference mode, Function mode and Lay-flat mode.
3767 * We support Home mode and Function mode currently.
3768 *
3769 * Will consider support rest of modes in future.
3770 *
3771 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003772static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003773 HOME_MODE,
3774/* WEB_BROWSER_MODE = 2,
3775 WEB_CONFERENCE_MODE = 3, */
3776 FUNCTION_MODE
3777};
3778
3779#define DFR_CHANGE_ROW 0x101
3780#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003781#define FIRST_ADAPTIVE_KEY 0x103
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003782
3783/* press Fn key a while second, it will switch to Function Mode. Then
3784 * release Fn key, previous mode be restored.
3785 */
3786static bool adaptive_keyboard_mode_is_saved;
3787static int adaptive_keyboard_prev_mode;
3788
Bastien Noceraf74587f2015-03-02 14:45:22 +01003789static int adaptive_keyboard_get_mode(void)
3790{
3791 int mode = 0;
3792
3793 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3794 pr_err("Cannot read adaptive keyboard mode\n");
3795 return -EIO;
3796 }
3797
3798 return mode;
3799}
3800
3801static int adaptive_keyboard_set_mode(int new_mode)
3802{
3803 if (new_mode < 0 ||
3804 new_mode > LAYFLAT_MODE)
3805 return -EINVAL;
3806
3807 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3808 pr_err("Cannot set adaptive keyboard mode\n");
3809 return -EIO;
3810 }
3811
3812 return 0;
3813}
3814
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003815static int adaptive_keyboard_get_next_mode(int mode)
3816{
3817 size_t i;
3818 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3819
3820 for (i = 0; i <= max_mode; i++) {
3821 if (adaptive_keyboard_modes[i] == mode)
3822 break;
3823 }
3824
3825 if (i >= max_mode)
3826 i = 0;
3827 else
3828 i++;
3829
3830 return adaptive_keyboard_modes[i];
3831}
3832
3833static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3834{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003835 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003836 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003837 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003838
3839 switch (scancode) {
3840 case DFR_CHANGE_ROW:
3841 if (adaptive_keyboard_mode_is_saved) {
3842 new_mode = adaptive_keyboard_prev_mode;
3843 adaptive_keyboard_mode_is_saved = false;
3844 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003845 current_mode = adaptive_keyboard_get_mode();
3846 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003847 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003848 new_mode = adaptive_keyboard_get_next_mode(
3849 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003850 }
3851
Bastien Noceraf74587f2015-03-02 14:45:22 +01003852 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003853 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003854
3855 return true;
3856
3857 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003858 current_mode = adaptive_keyboard_get_mode();
3859 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003860 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003861
Bastien Noceraf74587f2015-03-02 14:45:22 +01003862 adaptive_keyboard_prev_mode = current_mode;
3863 adaptive_keyboard_mode_is_saved = true;
3864
3865 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3866 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003867 return true;
3868
3869 default:
Dan Carpenter741d98c2015-03-11 12:36:07 +03003870 if (scancode < FIRST_ADAPTIVE_KEY ||
Christian Kellner696c65232017-02-28 17:10:57 +01003871 scancode >= FIRST_ADAPTIVE_KEY +
3872 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3873 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
Bastien Nocera6a68d852015-03-02 14:45:31 +01003874 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003875 scancode);
Bastien Nocera6a68d852015-03-02 14:45:31 +01003876 return false;
3877 }
Christian Kellner149c8c72017-02-28 17:10:56 +01003878 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3879 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
Bastien Nocera6a68d852015-03-02 14:45:31 +01003880 if (keycode != KEY_RESERVED) {
3881 mutex_lock(&tpacpi_inputdev_send_mutex);
3882
3883 input_report_key(tpacpi_inputdev, keycode, 1);
3884 input_sync(tpacpi_inputdev);
3885
3886 input_report_key(tpacpi_inputdev, keycode, 0);
3887 input_sync(tpacpi_inputdev);
3888
3889 mutex_unlock(&tpacpi_inputdev_send_mutex);
3890 }
3891 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003892 }
3893}
3894
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003895static bool hotkey_notify_hotkey(const u32 hkey,
3896 bool *send_acpi_ev,
3897 bool *ignore_acpi_ev)
3898{
3899 /* 0x1000-0x1FFF: key presses */
3900 unsigned int scancode = hkey & 0xfff;
3901 *send_acpi_ev = true;
3902 *ignore_acpi_ev = false;
3903
Christian Kellner696c65232017-02-28 17:10:57 +01003904 /*
3905 * Original events are in the 0x10XX range, the adaptive keyboard
3906 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3907 * models, additional keys are emitted through 0x13XX.
3908 */
3909 switch ((hkey >> 8) & 0xf) {
3910 case 0:
3911 if (scancode > 0 &&
3912 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3913 /* HKEY event 0x1001 is scancode 0x00 */
3914 scancode--;
3915 if (!(hotkey_source_mask & (1 << scancode))) {
3916 tpacpi_input_send_key_masked(scancode);
3917 *send_acpi_ev = false;
3918 } else {
3919 *ignore_acpi_ev = true;
3920 }
3921 return true;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003922 }
Christian Kellner696c65232017-02-28 17:10:57 +01003923 break;
3924
3925 case 1:
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003926 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Christian Kellner696c65232017-02-28 17:10:57 +01003927
3928 case 3:
3929 /* Extended keycodes start at 0x300 and our offset into the map
3930 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3931 * will be positive, but might not be in the correct range.
3932 */
3933 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3934 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3935 scancode < TPACPI_HOTKEY_MAP_LEN) {
3936 tpacpi_input_send_key(scancode);
3937 return true;
3938 }
3939 break;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003940 }
Christian Kellner696c65232017-02-28 17:10:57 +01003941
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003942 return false;
3943}
3944
3945static bool hotkey_notify_wakeup(const u32 hkey,
3946 bool *send_acpi_ev,
3947 bool *ignore_acpi_ev)
3948{
3949 /* 0x2000-0x2FFF: Wakeup reason */
3950 *send_acpi_ev = true;
3951 *ignore_acpi_ev = false;
3952
3953 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003954 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3955 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003956 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3957 *ignore_acpi_ev = true;
3958 break;
3959
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003960 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3961 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003962 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3963 *ignore_acpi_ev = true;
3964 break;
3965
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003966 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3967 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003968 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003969 /* how to auto-heal: */
3970 /* 2313: woke up from S3, go to S4/S5 */
3971 /* 2413: woke up from S4, go to S5 */
3972 break;
3973
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003974 default:
3975 return false;
3976 }
3977
3978 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003979 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003980 hotkey_wakeup_reason_notify_change();
3981 }
3982 return true;
3983}
3984
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003985static bool hotkey_notify_dockevent(const u32 hkey,
3986 bool *send_acpi_ev,
3987 bool *ignore_acpi_ev)
3988{
3989 /* 0x4000-0x4FFF: dock-related events */
3990 *send_acpi_ev = true;
3991 *ignore_acpi_ev = false;
3992
3993 switch (hkey) {
3994 case TP_HKEY_EV_UNDOCK_ACK:
3995 /* ACPI undock operation completed after wakeup */
3996 hotkey_autosleep_ack = 1;
3997 pr_info("undocked\n");
3998 hotkey_wakeup_hotunplug_complete_notify_change();
3999 return true;
4000
4001 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
4002 pr_info("docked into hotplug port replicator\n");
4003 return true;
4004 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
4005 pr_info("undocked from hotplug port replicator\n");
4006 return true;
4007
4008 default:
4009 return false;
4010 }
4011}
4012
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004013static bool hotkey_notify_usrevent(const u32 hkey,
4014 bool *send_acpi_ev,
4015 bool *ignore_acpi_ev)
4016{
4017 /* 0x5000-0x5FFF: human interface helpers */
4018 *send_acpi_ev = true;
4019 *ignore_acpi_ev = false;
4020
4021 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004022 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
4023 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004024 return true;
4025
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004026 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
4027 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004028 tpacpi_input_send_tabletsw();
4029 hotkey_tablet_mode_notify_change();
4030 *send_acpi_ev = false;
4031 return true;
4032
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004033 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
4034 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
4035 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03004036 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004037 *ignore_acpi_ev = true;
4038 return true;
4039
4040 default:
4041 return false;
4042 }
4043}
4044
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004045static void thermal_dump_all_sensors(void);
4046
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004047static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004048 bool *send_acpi_ev,
4049 bool *ignore_acpi_ev)
4050{
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004051 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004052 *send_acpi_ev = true;
4053 *ignore_acpi_ev = false;
4054
4055 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004056 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh6e6bc5f62018-04-24 16:56:03 -03004057 pr_debug("EC reports: Thermal Table has changed\n");
4058 /* recommended action: do nothing, we don't have
4059 * Lenovo ATM information */
4060 return true;
4061 case TP_HKEY_EV_THM_CSM_COMPLETED:
4062 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
4063 /* recommended action: do nothing, we don't have
4064 * Lenovo ATM information */
4065 return true;
4066 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
4067 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02004068 /* recommended action: do nothing, we don't have
4069 * Lenovo ATM information */
4070 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004071 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07004072 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004073 /* recommended action: warn user through gui */
4074 break;
4075 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07004076 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004077 /* recommended action: immediate sleep/hibernate */
4078 break;
4079 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004080 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004081 /* recommended action: warn user through gui, that */
4082 /* some internal component is too hot */
4083 break;
4084 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004085 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004086 /* recommended action: immediate sleep/hibernate */
4087 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01004088 case TP_HKEY_EV_AC_CHANGED:
4089 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4090 * AC status changed; can be triggered by plugging or
4091 * unplugging AC adapter, docking or undocking. */
4092
4093 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004094
4095 case TP_HKEY_EV_KEY_NUMLOCK:
4096 case TP_HKEY_EV_KEY_FN:
Xavier Naveira67ab6242015-02-10 08:45:18 +01004097 case TP_HKEY_EV_KEY_FN_ESC:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004098 /* key press events, we just ignore them as long as the EC
4099 * is still reporting them in the normal keyboard stream */
4100 *send_acpi_ev = false;
4101 *ignore_acpi_ev = true;
4102 return true;
4103
Lyudeb03f4d42016-11-11 15:15:03 -05004104 case TP_HKEY_EV_TABLET_CHANGED:
4105 tpacpi_input_send_tabletsw();
4106 hotkey_tablet_mode_notify_change();
4107 *send_acpi_ev = false;
Henrique de Moraes Holschuhedd1ed72018-04-24 16:56:04 -03004108 return true;
Lyudeb03f4d42016-11-11 15:15:03 -05004109
David Herrmann587d8622018-01-12 12:04:45 +01004110 case TP_HKEY_EV_PALM_DETECTED:
4111 case TP_HKEY_EV_PALM_UNDETECTED:
4112 /* palm detected hovering the keyboard, forward to user-space
4113 * via netlink for consumption */
4114 return true;
4115
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004116 default:
Henrique de Moraes Holschuhfd75ba22018-04-24 16:56:05 -03004117 /* report simply as unknown, no sensor dump */
4118 return false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004119 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004120
4121 thermal_dump_all_sensors();
Henrique de Moraes Holschuhfd75ba22018-04-24 16:56:05 -03004122 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004123}
4124
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004125static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4126{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004127 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004128 bool send_acpi_ev;
4129 bool ignore_acpi_ev;
4130 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004131
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004132 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07004133 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004134 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004135 acpi_bus_generate_netlink_event(
4136 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004137 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004138 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004139 return;
4140 }
4141
4142 while (1) {
4143 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07004144 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004145 return;
4146 }
4147
4148 if (hkey == 0) {
4149 /* queue empty */
4150 return;
4151 }
4152
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004153 send_acpi_ev = true;
4154 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004155
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004156 switch (hkey >> 12) {
4157 case 1:
4158 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004159 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4160 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004161 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004162 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004163 /* 0x2000-0x2FFF: Wakeup reason */
4164 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4165 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004166 break;
4167 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004168 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004169 switch (hkey) {
4170 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004171 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004172 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004173 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004174 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004175 break;
4176 case TP_HKEY_EV_OPTDRV_EJ:
4177 /* FIXME: kick libata if SATA link offline */
4178 known_ev = true;
4179 break;
4180 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004181 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004182 }
4183 break;
4184 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03004185 /* 0x4000-0x4FFF: dock-related events */
4186 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4187 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004188 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004189 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02004190 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004191 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4192 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004193 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004194 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004195 /* 0x6000-0x6FFF: thermal alarms/notices and
4196 * keyboard events */
4197 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004198 &ignore_acpi_ev);
4199 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004200 case 7:
4201 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004202 if (tp_features.hotkey_wlsw &&
4203 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004204 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004205 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004206 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004207 break;
4208 }
4209 /* fallthrough to default */
4210 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004211 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004212 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004213 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07004214 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004215 pr_notice("please report the conditions when this event happened to %s\n",
4216 TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004217 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004218
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004219 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03004220 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004221 acpi_bus_generate_netlink_event(
4222 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004223 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004224 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004225 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004226 }
4227}
4228
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02004229static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004230{
4231 /* Do these on suspend, we get the events on early resume! */
4232 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4233 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08004234
4235 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004236 if (tp_features.has_adaptive_kbd) {
4237 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4238 "GTRW", "dd", 0)) {
4239 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004240 }
4241 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004242}
4243
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004244static void hotkey_resume(void)
4245{
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03004246 tpacpi_disable_brightness_delay();
4247
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004248 if (hotkey_status_set(true) < 0 ||
4249 hotkey_mask_set(hotkey_acpi_mask) < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004250 pr_err("error while attempting to reset the event firmware interface\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004251
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004252 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02004253 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004254 hotkey_wakeup_reason_notify_change();
4255 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004256 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004257
4258 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004259 if (tp_features.has_adaptive_kbd) {
4260 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4261 adaptive_keyboard_prev_mode)) {
4262 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004263 }
4264 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004265}
4266
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004267/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004268static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004270 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004272 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004273 seq_printf(m, "status:\t\tnot supported\n");
4274 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004275 }
4276
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004277 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004278 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004279 res = hotkey_status_get(&status);
4280 if (!res)
4281 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004282 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004283 if (res)
4284 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004286 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004287 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004288 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4289 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004291 seq_printf(m, "mask:\t\tnot supported\n");
4292 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 }
4294
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296}
4297
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004298static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004299{
4300 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004301 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004302 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4303 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 +00004304}
4305
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004306static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004308 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004309 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004312 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 return -ENODEV;
4314
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004315 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004316 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004317
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004318 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004319
4320 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 while ((cmd = next_cmd(&buf))) {
4322 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004323 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004325 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004326 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa462009-09-12 15:22:16 -03004328 mask = (hotkey_all_mask | hotkey_source_mask)
4329 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4331 /* mask set */
4332 } else if (sscanf(cmd, "%x", &mask) == 1) {
4333 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004334 } else {
4335 res = -EINVAL;
4336 goto errexit;
4337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004339
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004340 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004341 tpacpi_disclose_usertask("procfs hotkey",
4342 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004343 res = hotkey_user_mask_set(mask);
4344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004346errexit:
4347 mutex_unlock(&hotkey_mutex);
4348 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004349}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004351static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004352 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4353 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Hui Wanga3c42a42016-11-08 16:13:23 +08004354 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004355 {"", 0},
4356};
4357
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004358static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004359 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004360 .notify = hotkey_notify,
4361 .handle = &hkey_handle,
4362 .type = ACPI_DEVICE_NOTIFY,
4363};
4364
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004365static struct ibm_struct hotkey_driver_data = {
4366 .name = "hotkey",
4367 .read = hotkey_read,
4368 .write = hotkey_write,
4369 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004370 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004371 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004372 .acpi = &ibm_hotkey_acpidriver,
4373};
4374
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004375/*************************************************************************
4376 * Bluetooth subdriver
4377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004379enum {
4380 /* ACPI GBDC/SBDC bits */
4381 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4382 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004383 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004384 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004385};
4386
4387enum {
4388 /* ACPI \BLTH commands */
4389 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4390 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4391 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4392 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4393 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004394};
4395
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004396#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4397
Johannes Berg19d337d2009-06-02 13:01:37 +02004398static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004399{
4400 int status;
4401
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004402#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4403 if (dbg_bluetoothemul)
4404 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004405 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004406#endif
4407
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004408 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4409 return -EIO;
4410
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004411 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004412 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004413}
4414
Johannes Berg19d337d2009-06-02 13:01:37 +02004415static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004416{
4417 int status;
4418
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004419 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004420 "will attempt to %s bluetooth\n",
4421 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004422
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004423#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4424 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004425 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004426 return 0;
4427 }
4428#endif
4429
Johannes Berg19d337d2009-06-02 13:01:37 +02004430 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004431 status = TP_ACPI_BLUETOOTH_RADIOSSW
4432 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4433 else
4434 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004435
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004436 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4437 return -EIO;
4438
4439 return 0;
4440}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004441
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004442/* sysfs bluetooth enable ---------------------------------------------- */
4443static ssize_t bluetooth_enable_show(struct device *dev,
4444 struct device_attribute *attr,
4445 char *buf)
4446{
Johannes Berg19d337d2009-06-02 13:01:37 +02004447 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4448 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004449}
4450
4451static ssize_t bluetooth_enable_store(struct device *dev,
4452 struct device_attribute *attr,
4453 const char *buf, size_t count)
4454{
Johannes Berg19d337d2009-06-02 13:01:37 +02004455 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4456 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004457}
4458
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004459static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004460
4461/* --------------------------------------------------------------------- */
4462
4463static struct attribute *bluetooth_attributes[] = {
4464 &dev_attr_bluetooth_enable.attr,
4465 NULL
4466};
4467
4468static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004469 .attrs = bluetooth_attributes,
4470};
4471
Johannes Berg19d337d2009-06-02 13:01:37 +02004472static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4473 .get_status = bluetooth_get_status,
4474 .set_status = bluetooth_set_status,
4475};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004476
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004477static void bluetooth_shutdown(void)
4478{
4479 /* Order firmware to save current state to NVRAM */
4480 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4481 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004482 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004483 else
4484 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004485 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004486}
4487
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004488static void bluetooth_exit(void)
4489{
4490 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4491 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004492
4493 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4494
4495 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004496}
4497
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004498static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004500 int res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004501 int status = 0;
4502
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004503 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4504 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004505
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004506 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004507
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004508 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4509 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004510 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004511 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004513 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4514 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004515 str_supported(tp_features.bluetooth),
4516 status);
4517
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004518#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4519 if (dbg_bluetoothemul) {
4520 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004521 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004522 } else
4523#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004524 if (tp_features.bluetooth &&
4525 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4526 /* no bluetooth hardware present in system */
4527 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004528 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004529 "bluetooth hardware not installed\n");
4530 }
4531
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004532 if (!tp_features.bluetooth)
4533 return 1;
4534
Johannes Berg19d337d2009-06-02 13:01:37 +02004535 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4536 &bluetooth_tprfk_ops,
4537 RFKILL_TYPE_BLUETOOTH,
4538 TPACPI_RFK_BLUETOOTH_SW_NAME,
4539 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004540 if (res)
4541 return res;
4542
Johannes Berg19d337d2009-06-02 13:01:37 +02004543 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4544 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004545 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004546 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004547 return res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004548 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004549
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551}
4552
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004553/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004554static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004556 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557}
4558
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004559static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560{
Johannes Berg19d337d2009-06-02 13:01:37 +02004561 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562}
4563
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004564static struct ibm_struct bluetooth_driver_data = {
4565 .name = "bluetooth",
4566 .read = bluetooth_read,
4567 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004568 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004569 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004570};
4571
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004572/*************************************************************************
4573 * Wan subdriver
4574 */
4575
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004576enum {
4577 /* ACPI GWAN/SWAN bits */
4578 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4579 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004580 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004581 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004582};
4583
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004584#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4585
Johannes Berg19d337d2009-06-02 13:01:37 +02004586static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004587{
4588 int status;
4589
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004590#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4591 if (dbg_wwanemul)
4592 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004593 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004594#endif
4595
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004596 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4597 return -EIO;
4598
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004599 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004600 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004601}
4602
Johannes Berg19d337d2009-06-02 13:01:37 +02004603static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004604{
4605 int status;
4606
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004607 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004608 "will attempt to %s wwan\n",
4609 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004610
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004611#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4612 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004613 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004614 return 0;
4615 }
4616#endif
4617
Johannes Berg19d337d2009-06-02 13:01:37 +02004618 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004619 status = TP_ACPI_WANCARD_RADIOSSW
4620 | TP_ACPI_WANCARD_RESUMECTRL;
4621 else
4622 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004623
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004624 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4625 return -EIO;
4626
4627 return 0;
4628}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004629
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004630/* sysfs wan enable ---------------------------------------------------- */
4631static ssize_t wan_enable_show(struct device *dev,
4632 struct device_attribute *attr,
4633 char *buf)
4634{
Johannes Berg19d337d2009-06-02 13:01:37 +02004635 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4636 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004637}
4638
4639static ssize_t wan_enable_store(struct device *dev,
4640 struct device_attribute *attr,
4641 const char *buf, size_t count)
4642{
Johannes Berg19d337d2009-06-02 13:01:37 +02004643 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4644 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004645}
4646
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004647static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4648 wan_enable_show, wan_enable_store);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004649
4650/* --------------------------------------------------------------------- */
4651
4652static struct attribute *wan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004653 &dev_attr_wwan_enable.attr,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004654 NULL
4655};
4656
4657static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004658 .attrs = wan_attributes,
4659};
4660
Johannes Berg19d337d2009-06-02 13:01:37 +02004661static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4662 .get_status = wan_get_status,
4663 .set_status = wan_set_status,
4664};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004665
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004666static void wan_shutdown(void)
4667{
4668 /* Order firmware to save current state to NVRAM */
4669 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4670 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004671 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004672 else
4673 vdbg_printk(TPACPI_DBG_RFKILL,
4674 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004675}
4676
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004677static void wan_exit(void)
4678{
4679 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4680 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004681
4682 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4683
4684 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004685}
4686
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004687static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004688{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004689 int res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004690 int status = 0;
4691
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004692 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4693 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004694
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004695 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004696
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004697 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004698 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004699
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004700 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4701 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004702 str_supported(tp_features.wan),
4703 status);
4704
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004705#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4706 if (dbg_wwanemul) {
4707 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004708 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004709 } else
4710#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004711 if (tp_features.wan &&
4712 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4713 /* no wan hardware present in system */
4714 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004715 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004716 "wan hardware not installed\n");
4717 }
4718
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004719 if (!tp_features.wan)
4720 return 1;
4721
Johannes Berg19d337d2009-06-02 13:01:37 +02004722 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4723 &wan_tprfk_ops,
4724 RFKILL_TYPE_WWAN,
4725 TPACPI_RFK_WWAN_SW_NAME,
4726 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004727 if (res)
4728 return res;
4729
Johannes Berg19d337d2009-06-02 13:01:37 +02004730 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4731 &wan_attr_group);
4732
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004733 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004734 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004735 return res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004736 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004737
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004738 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004739}
4740
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004741/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004742static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004743{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004744 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004745}
4746
4747static int wan_write(char *buf)
4748{
Johannes Berg19d337d2009-06-02 13:01:37 +02004749 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004750}
4751
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004752static struct ibm_struct wan_driver_data = {
4753 .name = "wan",
4754 .read = wan_read,
4755 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004756 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004757 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004758};
4759
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004760/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004761 * UWB subdriver
4762 */
4763
4764enum {
4765 /* ACPI GUWB/SUWB bits */
4766 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4767 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4768};
4769
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004770#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4771
Johannes Berg19d337d2009-06-02 13:01:37 +02004772static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004773{
4774 int status;
4775
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004776#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4777 if (dbg_uwbemul)
4778 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004779 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004780#endif
4781
4782 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4783 return -EIO;
4784
4785 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004786 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004787}
4788
Johannes Berg19d337d2009-06-02 13:01:37 +02004789static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004790{
4791 int status;
4792
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004793 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004794 "will attempt to %s UWB\n",
4795 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004796
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004797#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4798 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004799 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004800 return 0;
4801 }
4802#endif
4803
Johannes Berg19d337d2009-06-02 13:01:37 +02004804 if (state == TPACPI_RFK_RADIO_ON)
4805 status = TP_ACPI_UWB_RADIOSSW;
4806 else
4807 status = 0;
4808
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004809 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4810 return -EIO;
4811
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004812 return 0;
4813}
4814
4815/* --------------------------------------------------------------------- */
4816
Johannes Berg19d337d2009-06-02 13:01:37 +02004817static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4818 .get_status = uwb_get_status,
4819 .set_status = uwb_set_status,
4820};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004821
4822static void uwb_exit(void)
4823{
Johannes Berg19d337d2009-06-02 13:01:37 +02004824 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004825}
4826
4827static int __init uwb_init(struct ibm_init_struct *iibm)
4828{
4829 int res;
4830 int status = 0;
4831
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004832 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4833 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004834
4835 TPACPI_ACPIHANDLE_INIT(hkey);
4836
4837 tp_features.uwb = hkey_handle &&
4838 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4839
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004840 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4841 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004842 str_supported(tp_features.uwb),
4843 status);
4844
4845#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4846 if (dbg_uwbemul) {
4847 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004848 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004849 } else
4850#endif
4851 if (tp_features.uwb &&
4852 !(status & TP_ACPI_UWB_HWPRESENT)) {
4853 /* no uwb hardware present in system */
4854 tp_features.uwb = 0;
4855 dbg_printk(TPACPI_DBG_INIT,
4856 "uwb hardware not installed\n");
4857 }
4858
4859 if (!tp_features.uwb)
4860 return 1;
4861
4862 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004863 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004864 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004865 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004866 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004867 return res;
4868}
4869
4870static struct ibm_struct uwb_driver_data = {
4871 .name = "uwb",
4872 .exit = uwb_exit,
4873 .flags.experimental = 1,
4874};
4875
4876/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004877 * Video subdriver
4878 */
4879
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004880#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4881
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004882enum video_access_mode {
4883 TPACPI_VIDEO_NONE = 0,
4884 TPACPI_VIDEO_570, /* 570 */
4885 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4886 TPACPI_VIDEO_NEW, /* all others */
4887};
4888
4889enum { /* video status flags, based on VIDEO_570 */
4890 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4891 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4892 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4893};
4894
4895enum { /* TPACPI_VIDEO_570 constants */
4896 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4897 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4898 * video_status_flags */
4899 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4900 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4901};
4902
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004903static enum video_access_mode video_supported;
4904static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004905
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004906static int video_autosw_get(void);
4907static int video_autosw_set(int enable);
4908
Joe Perches112a6ee2011-04-04 10:06:24 -07004909TPACPI_HANDLE(vid, root,
4910 "\\_SB.PCI.AGP.VGA", /* 570 */
4911 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4912 "\\_SB.PCI0.VID0", /* 770e */
4913 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4914 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4915 "\\_SB.PCI0.AGP.VID", /* all others */
4916 ); /* R30, R31 */
4917
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004918TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004919
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004920static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004922 int ivga;
4923
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004924 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4925
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004926 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004927 if (tpacpi_is_ibm())
4928 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004929
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004930 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4931 /* G41, assume IVGA doesn't change */
4932 vid_handle = vid2_handle;
4933
4934 if (!vid_handle)
4935 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004936 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004937 else if (tpacpi_is_ibm() &&
4938 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004939 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004940 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004941 else if (tpacpi_is_ibm() &&
4942 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004943 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004944 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004945 else
4946 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004947 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004949 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4950 str_supported(video_supported != TPACPI_VIDEO_NONE),
4951 video_supported);
4952
Jan van den Berg72a979f2014-09-17 00:01:08 +02004953 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954}
4955
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004956static void video_exit(void)
4957{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004958 dbg_printk(TPACPI_DBG_EXIT,
4959 "restoring original video autoswitch mode\n");
4960 if (video_autosw_set(video_orig_autosw))
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004961 pr_err("error while trying to restore original video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004962}
4963
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004964static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965{
4966 int status = 0;
4967 int i;
4968
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004969 switch (video_supported) {
4970 case TPACPI_VIDEO_570:
4971 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4972 TP_ACPI_VIDEO_570_PHSCMD))
4973 return -EIO;
4974 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4975 break;
4976 case TPACPI_VIDEO_770:
4977 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4978 return -EIO;
4979 if (i)
4980 status |= TP_ACPI_VIDEO_S_LCD;
4981 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4982 return -EIO;
4983 if (i)
4984 status |= TP_ACPI_VIDEO_S_CRT;
4985 break;
4986 case TPACPI_VIDEO_NEW:
4987 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4988 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4989 return -EIO;
4990 if (i)
4991 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004993 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4994 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4995 return -EIO;
4996 if (i)
4997 status |= TP_ACPI_VIDEO_S_LCD;
4998 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4999 return -EIO;
5000 if (i)
5001 status |= TP_ACPI_VIDEO_S_DVI;
5002 break;
5003 default:
5004 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
5007 return status;
5008}
5009
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005010static int video_outputsw_set(int status)
5011{
5012 int autosw;
5013 int res = 0;
5014
5015 switch (video_supported) {
5016 case TPACPI_VIDEO_570:
5017 res = acpi_evalf(NULL, NULL,
5018 "\\_SB.PHS2", "vdd",
5019 TP_ACPI_VIDEO_570_PHS2CMD,
5020 status | TP_ACPI_VIDEO_570_PHS2SET);
5021 break;
5022 case TPACPI_VIDEO_770:
5023 autosw = video_autosw_get();
5024 if (autosw < 0)
5025 return autosw;
5026
5027 res = video_autosw_set(1);
5028 if (res)
5029 return res;
5030 res = acpi_evalf(vid_handle, NULL,
5031 "ASWT", "vdd", status * 0x100, 0);
5032 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07005033 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005034 return -EIO;
5035 }
5036 break;
5037 case TPACPI_VIDEO_NEW:
5038 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005039 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005040 break;
5041 default:
5042 return -ENOSYS;
5043 }
5044
Jan van den Berg72a979f2014-09-17 00:01:08 +02005045 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005046}
5047
5048static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005050 int autosw = 0;
5051
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005052 switch (video_supported) {
5053 case TPACPI_VIDEO_570:
5054 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5055 return -EIO;
5056 break;
5057 case TPACPI_VIDEO_770:
5058 case TPACPI_VIDEO_NEW:
5059 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5060 return -EIO;
5061 break;
5062 default:
5063 return -ENOSYS;
5064 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005065
5066 return autosw & 1;
5067}
5068
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005069static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005070{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005071 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005072 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005073 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005074}
5075
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005076static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005077{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005078 int autosw = video_autosw_get();
5079 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005080
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005081 if (autosw < 0)
5082 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005083
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005084 switch (video_supported) {
5085 case TPACPI_VIDEO_570:
5086 res = video_autosw_set(1);
5087 if (res)
5088 return res;
5089 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5090 break;
5091 case TPACPI_VIDEO_770:
5092 case TPACPI_VIDEO_NEW:
5093 res = video_autosw_set(1);
5094 if (res)
5095 return res;
5096 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5097 break;
5098 default:
5099 return -ENOSYS;
5100 }
5101 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07005102 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005103 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005104 }
5105
Jan van den Berg72a979f2014-09-17 00:01:08 +02005106 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005107}
5108
5109static int video_expand_toggle(void)
5110{
5111 switch (video_supported) {
5112 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005113 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005114 0 : -EIO;
5115 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005116 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005117 0 : -EIO;
5118 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005119 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005120 0 : -EIO;
5121 default:
5122 return -ENOSYS;
5123 }
5124 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005125}
5126
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005127static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005128{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005129 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005130
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005131 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005132 seq_printf(m, "status:\t\tnot supported\n");
5133 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005134 }
5135
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005136 /* Even reads can crash X.org, so... */
5137 if (!capable(CAP_SYS_ADMIN))
5138 return -EPERM;
5139
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005140 status = video_outputsw_get();
5141 if (status < 0)
5142 return status;
5143
5144 autosw = video_autosw_get();
5145 if (autosw < 0)
5146 return autosw;
5147
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005148 seq_printf(m, "status:\t\tsupported\n");
5149 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5150 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005151 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005152 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5153 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5154 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5155 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005156 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005157 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5158 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5159 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005160
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005161 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005162}
5163
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005164static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165{
5166 char *cmd;
5167 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005168 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005170 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005171 return -ENODEV;
5172
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005173 /* Even reads can crash X.org, let alone writes... */
5174 if (!capable(CAP_SYS_ADMIN))
5175 return -EPERM;
5176
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005177 enable = 0;
5178 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179
5180 while ((cmd = next_cmd(&buf))) {
5181 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005182 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005184 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005186 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005188 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005189 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005190 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005191 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005192 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005193 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005194 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005196 res = video_autosw_set(1);
5197 if (res)
5198 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005200 res = video_autosw_set(0);
5201 if (res)
5202 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005204 res = video_outputsw_cycle();
5205 if (res)
5206 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005208 res = video_expand_toggle();
5209 if (res)
5210 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 } else
5212 return -EINVAL;
5213 }
5214
5215 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005216 status = video_outputsw_get();
5217 if (status < 0)
5218 return status;
5219 res = video_outputsw_set((status & ~disable) | enable);
5220 if (res)
5221 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 }
5223
5224 return 0;
5225}
5226
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005227static struct ibm_struct video_driver_data = {
5228 .name = "video",
5229 .read = video_read,
5230 .write = video_write,
5231 .exit = video_exit,
5232};
5233
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005234#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5235
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005236/*************************************************************************
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005237 * Keyboard backlight subdriver
5238 */
5239
Hans de Goedec685e202017-02-09 16:44:13 +01005240static enum led_brightness kbdlight_brightness;
5241static DEFINE_MUTEX(kbdlight_mutex);
5242
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005243static int kbdlight_set_level(int level)
5244{
Hans de Goedec685e202017-02-09 16:44:13 +01005245 int ret = 0;
5246
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005247 if (!hkey_handle)
5248 return -ENXIO;
5249
Hans de Goedec685e202017-02-09 16:44:13 +01005250 mutex_lock(&kbdlight_mutex);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005251
Hans de Goedec685e202017-02-09 16:44:13 +01005252 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5253 ret = -EIO;
5254 else
5255 kbdlight_brightness = level;
5256
5257 mutex_unlock(&kbdlight_mutex);
5258
5259 return ret;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005260}
5261
5262static int kbdlight_get_level(void)
5263{
5264 int status = 0;
5265
5266 if (!hkey_handle)
5267 return -ENXIO;
5268
5269 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5270 return -EIO;
5271
5272 if (status < 0)
5273 return status;
5274
5275 return status & 0x3;
5276}
5277
5278static bool kbdlight_is_supported(void)
5279{
5280 int status = 0;
5281
5282 if (!hkey_handle)
5283 return false;
5284
5285 if (!acpi_has_method(hkey_handle, "MLCG")) {
5286 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5287 return false;
5288 }
5289
5290 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5291 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5292 return false;
5293 }
5294
5295 if (status < 0) {
5296 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5297 return false;
5298 }
5299
5300 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5301 /*
5302 * Guessed test for keyboard backlight:
5303 *
5304 * Machines with backlight keyboard return:
5305 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5306 * b110100010010000000XX - ThinkPad x230
5307 * b010100000010000000XX - ThinkPad x240
5308 * b010100000010000000XX - ThinkPad W541
5309 * (XX is current backlight level)
5310 *
5311 * Machines without backlight keyboard return:
5312 * b10100001000000000000 - ThinkPad x230
5313 * b10110001000000000000 - ThinkPad E430
5314 * b00000000000000000000 - ThinkPad E450
5315 *
5316 * Candidate BITs for detection test (XOR):
5317 * b01000000001000000000
5318 * ^
5319 */
5320 return status & BIT(9);
5321}
5322
Hans de Goede86ec0c22017-02-09 16:44:12 +01005323static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005324 enum led_brightness brightness)
5325{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005326 return kbdlight_set_level(brightness);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005327}
5328
5329static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5330{
5331 int level;
5332
5333 level = kbdlight_get_level();
5334 if (level < 0)
5335 return 0;
5336
5337 return level;
5338}
5339
5340static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5341 .led_classdev = {
5342 .name = "tpacpi::kbd_backlight",
5343 .max_brightness = 2,
Hans de Goedec685e202017-02-09 16:44:13 +01005344 .flags = LED_BRIGHT_HW_CHANGED,
Hans de Goede86ec0c22017-02-09 16:44:12 +01005345 .brightness_set_blocking = &kbdlight_sysfs_set,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005346 .brightness_get = &kbdlight_sysfs_get,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005347 }
5348};
5349
5350static int __init kbdlight_init(struct ibm_init_struct *iibm)
5351{
5352 int rc;
5353
5354 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5355
5356 TPACPI_ACPIHANDLE_INIT(hkey);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005357
5358 if (!kbdlight_is_supported()) {
5359 tp_features.kbdlight = 0;
5360 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5361 return 1;
5362 }
5363
Hans de Goedec685e202017-02-09 16:44:13 +01005364 kbdlight_brightness = kbdlight_sysfs_get(NULL);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005365 tp_features.kbdlight = 1;
5366
5367 rc = led_classdev_register(&tpacpi_pdev->dev,
5368 &tpacpi_led_kbdlight.led_classdev);
5369 if (rc < 0) {
5370 tp_features.kbdlight = 0;
5371 return rc;
5372 }
5373
Hans de Goedec685e202017-02-09 16:44:13 +01005374 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5375 TP_ACPI_HKEY_KBD_LIGHT_MASK);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005376 return 0;
5377}
5378
5379static void kbdlight_exit(void)
5380{
5381 if (tp_features.kbdlight)
5382 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005383}
5384
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005385static int kbdlight_set_level_and_update(int level)
5386{
5387 int ret;
5388 struct led_classdev *led_cdev;
5389
5390 ret = kbdlight_set_level(level);
5391 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5392
5393 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5394 led_cdev->brightness = level;
5395
5396 return ret;
5397}
5398
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005399static int kbdlight_read(struct seq_file *m)
5400{
5401 int level;
5402
5403 if (!tp_features.kbdlight) {
5404 seq_printf(m, "status:\t\tnot supported\n");
5405 } else {
5406 level = kbdlight_get_level();
5407 if (level < 0)
5408 seq_printf(m, "status:\t\terror %d\n", level);
5409 else
5410 seq_printf(m, "status:\t\t%d\n", level);
5411 seq_printf(m, "commands:\t0, 1, 2\n");
5412 }
5413
5414 return 0;
5415}
5416
5417static int kbdlight_write(char *buf)
5418{
5419 char *cmd;
5420 int level = -1;
5421
5422 if (!tp_features.kbdlight)
5423 return -ENODEV;
5424
5425 while ((cmd = next_cmd(&buf))) {
5426 if (strlencmp(cmd, "0") == 0)
5427 level = 0;
5428 else if (strlencmp(cmd, "1") == 0)
5429 level = 1;
5430 else if (strlencmp(cmd, "2") == 0)
5431 level = 2;
5432 else
5433 return -EINVAL;
5434 }
5435
5436 if (level == -1)
5437 return -EINVAL;
5438
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005439 return kbdlight_set_level_and_update(level);
5440}
5441
5442static void kbdlight_suspend(void)
5443{
5444 struct led_classdev *led_cdev;
5445
5446 if (!tp_features.kbdlight)
5447 return;
5448
5449 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5450 led_update_brightness(led_cdev);
5451 led_classdev_suspend(led_cdev);
5452}
5453
5454static void kbdlight_resume(void)
5455{
5456 if (!tp_features.kbdlight)
5457 return;
5458
5459 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005460}
5461
5462static struct ibm_struct kbdlight_driver_data = {
5463 .name = "kbdlight",
5464 .read = kbdlight_read,
5465 .write = kbdlight_write,
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005466 .suspend = kbdlight_suspend,
5467 .resume = kbdlight_resume,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005468 .exit = kbdlight_exit,
5469};
5470
5471/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005472 * Light (thinklight) subdriver
5473 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005475TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5476TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005477
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005478static int light_get_status(void)
5479{
5480 int status = 0;
5481
5482 if (tp_features.light_status) {
5483 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5484 return -EIO;
5485 return (!!status);
5486 }
5487
5488 return -ENXIO;
5489}
5490
5491static int light_set_status(int status)
5492{
5493 int rc;
5494
5495 if (tp_features.light) {
5496 if (cmos_handle) {
5497 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005498 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005499 TP_CMOS_THINKLIGHT_ON :
5500 TP_CMOS_THINKLIGHT_OFF);
5501 } else {
5502 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005503 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005504 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005505 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005506 }
5507
5508 return -ENXIO;
5509}
5510
Hans de Goede86ec0c22017-02-09 16:44:12 +01005511static int light_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005512 enum led_brightness brightness)
5513{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005514 return light_set_status((brightness != LED_OFF) ?
5515 TPACPI_LED_ON : TPACPI_LED_OFF);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005516}
5517
5518static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5519{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005520 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005521}
5522
5523static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5524 .led_classdev = {
5525 .name = "tpacpi::thinklight",
Hans de Goede86ec0c22017-02-09 16:44:12 +01005526 .brightness_set_blocking = &light_sysfs_set,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005527 .brightness_get = &light_sysfs_get,
5528 }
5529};
5530
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005531static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005533 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005534
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005535 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5536
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005537 if (tpacpi_is_ibm()) {
5538 TPACPI_ACPIHANDLE_INIT(ledb);
5539 TPACPI_ACPIHANDLE_INIT(lght);
5540 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005541 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005542
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005543 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005544 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005545
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005546 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005547 /* light status not supported on
5548 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005549 tp_features.light_status =
5550 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005552 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5553 str_supported(tp_features.light),
5554 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005555
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005556 if (!tp_features.light)
5557 return 1;
5558
5559 rc = led_classdev_register(&tpacpi_pdev->dev,
5560 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005561
5562 if (rc < 0) {
5563 tp_features.light = 0;
5564 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005565 } else {
5566 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005567 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005568
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005569 return rc;
5570}
5571
5572static void light_exit(void)
5573{
5574 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575}
5576
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005577static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005579 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005581 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005582 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005583 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005584 seq_printf(m, "status:\t\tunknown\n");
5585 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005586 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005587 status = light_get_status();
5588 if (status < 0)
5589 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005590 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5591 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595}
5596
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005597static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005600 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005601
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005602 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005603 return -ENODEV;
5604
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 while ((cmd = next_cmd(&buf))) {
5606 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005607 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005609 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 } else
5611 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 }
5613
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005614 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615}
5616
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005617static struct ibm_struct light_driver_data = {
5618 .name = "light",
5619 .read = light_read,
5620 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005621 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005622};
5623
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005624/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005625 * CMOS subdriver
5626 */
5627
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005628/* sysfs cmos_command -------------------------------------------------- */
5629static ssize_t cmos_command_store(struct device *dev,
5630 struct device_attribute *attr,
5631 const char *buf, size_t count)
5632{
5633 unsigned long cmos_cmd;
5634 int res;
5635
5636 if (parse_strtoul(buf, 21, &cmos_cmd))
5637 return -EINVAL;
5638
5639 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005640 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005641}
5642
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01005643static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005644
5645/* --------------------------------------------------------------------- */
5646
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005647static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005648{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005649 int res;
5650
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005651 vdbg_printk(TPACPI_DBG_INIT,
5652 "initializing cmos commands subdriver\n");
5653
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005654 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005655
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005656 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5657 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005658
5659 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5660 if (res)
5661 return res;
5662
Jan van den Berg72a979f2014-09-17 00:01:08 +02005663 return (cmos_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005664}
5665
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005666static void cmos_exit(void)
5667{
5668 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5669}
5670
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005671static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005673 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5674 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005676 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005678 seq_printf(m, "status:\t\tsupported\n");
5679 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 }
5681
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683}
5684
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005685static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686{
5687 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005688 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689
5690 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005691 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5692 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 /* cmos_cmd set */
5694 } else
5695 return -EINVAL;
5696
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005697 res = issue_thinkpad_cmos_command(cmos_cmd);
5698 if (res)
5699 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 }
5701
5702 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005703}
5704
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005705static struct ibm_struct cmos_driver_data = {
5706 .name = "cmos",
5707 .read = cmos_read,
5708 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005709 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005710};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005711
5712/*************************************************************************
5713 * LED subdriver
5714 */
5715
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005716enum led_access_mode {
5717 TPACPI_LED_NONE = 0,
5718 TPACPI_LED_570, /* 570 */
5719 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5720 TPACPI_LED_NEW, /* all others */
5721};
5722
5723enum { /* For TPACPI_LED_OLD */
5724 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5725 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5726 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5727};
5728
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005729static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005730
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005731static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005732
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005733#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005734static struct tpacpi_led_classdev *tpacpi_leds;
5735static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005736static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005737 /* there's a limit of 19 chars + NULL before 2.6.26 */
5738 "tpacpi::power",
5739 "tpacpi:orange:batt",
5740 "tpacpi:green:batt",
5741 "tpacpi::dock_active",
5742 "tpacpi::bay_active",
5743 "tpacpi::dock_batt",
5744 "tpacpi::unknown_led",
5745 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005746 "tpacpi::dock_status1",
5747 "tpacpi::dock_status2",
5748 "tpacpi::unknown_led2",
5749 "tpacpi::unknown_led3",
5750 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005751};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005752#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005753
5754static inline bool tpacpi_is_led_restricted(const unsigned int led)
5755{
5756#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5757 return false;
5758#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005759 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005760#endif
5761}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005762
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005763static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005764{
5765 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005766 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005767
5768 switch (led_supported) {
5769 case TPACPI_LED_570:
5770 if (!acpi_evalf(ec_handle,
5771 &status, "GLED", "dd", 1 << led))
5772 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005773 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005774 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005775 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005776 TPACPI_LED_ON :
5777 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005778 tpacpi_led_state_cache[led] = led_s;
5779 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005780 default:
5781 return -ENXIO;
5782 }
5783
5784 /* not reached */
5785}
5786
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005787static int led_set_status(const unsigned int led,
5788 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005789{
5790 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005791 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5792 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005793
5794 int rc = 0;
5795
5796 switch (led_supported) {
5797 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005798 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005799 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005800 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005801 if (unlikely(tpacpi_is_led_restricted(led)))
5802 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005803 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5804 (1 << led), led_sled_arg1[ledstatus]))
5805 rc = -EIO;
5806 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005807 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005808 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005809 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005810 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005811 if (unlikely(tpacpi_is_led_restricted(led)))
5812 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005813 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5814 if (rc >= 0)
5815 rc = ec_write(TPACPI_LED_EC_HLBL,
5816 (ledstatus == TPACPI_LED_BLINK) << led);
5817 if (rc >= 0)
5818 rc = ec_write(TPACPI_LED_EC_HLCL,
5819 (ledstatus != TPACPI_LED_OFF) << led);
5820 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005821 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005822 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005823 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5824 return -EINVAL;
5825 if (unlikely(tpacpi_is_led_restricted(led)))
5826 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005827 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5828 led, led_led_arg1[ledstatus]))
5829 rc = -EIO;
5830 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005831 default:
5832 rc = -ENXIO;
5833 }
5834
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005835 if (!rc)
5836 tpacpi_led_state_cache[led] = ledstatus;
5837
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005838 return rc;
5839}
5840
Hans de Goede86ec0c22017-02-09 16:44:12 +01005841static int led_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005842 enum led_brightness brightness)
5843{
5844 struct tpacpi_led_classdev *data = container_of(led_cdev,
5845 struct tpacpi_led_classdev, led_classdev);
Hans de Goede86ec0c22017-02-09 16:44:12 +01005846 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005847
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005848 if (brightness == LED_OFF)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005849 new_state = TPACPI_LED_OFF;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005850 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005851 new_state = TPACPI_LED_ON;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005852 else
Hans de Goede86ec0c22017-02-09 16:44:12 +01005853 new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005854
Hans de Goede86ec0c22017-02-09 16:44:12 +01005855 return led_set_status(data->led, new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005856}
5857
5858static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5859 unsigned long *delay_on, unsigned long *delay_off)
5860{
5861 struct tpacpi_led_classdev *data = container_of(led_cdev,
5862 struct tpacpi_led_classdev, led_classdev);
5863
5864 /* Can we choose the flash rate? */
5865 if (*delay_on == 0 && *delay_off == 0) {
5866 /* yes. set them to the hardware blink rate (1 Hz) */
5867 *delay_on = 500; /* ms */
5868 *delay_off = 500; /* ms */
5869 } else if ((*delay_on != 500) || (*delay_off != 500))
5870 return -EINVAL;
5871
Hans de Goede86ec0c22017-02-09 16:44:12 +01005872 return led_set_status(data->led, TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005873}
5874
5875static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5876{
5877 int rc;
5878
5879 struct tpacpi_led_classdev *data = container_of(led_cdev,
5880 struct tpacpi_led_classdev, led_classdev);
5881
5882 rc = led_get_status(data->led);
5883
5884 if (rc == TPACPI_LED_OFF || rc < 0)
5885 rc = LED_OFF; /* no error handling in led class :( */
5886 else
5887 rc = LED_FULL;
5888
5889 return rc;
5890}
5891
5892static void led_exit(void)
5893{
5894 unsigned int i;
5895
5896 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5897 if (tpacpi_leds[i].led_classdev.name)
5898 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5899 }
5900
5901 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005902}
5903
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005904static int __init tpacpi_init_led(unsigned int led)
5905{
5906 int rc;
5907
5908 tpacpi_leds[led].led = led;
5909
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005910 /* LEDs with no name don't get registered */
5911 if (!tpacpi_led_names[led])
5912 return 0;
5913
Hans de Goede86ec0c22017-02-09 16:44:12 +01005914 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005915 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5916 if (led_supported == TPACPI_LED_570)
5917 tpacpi_leds[led].led_classdev.brightness_get =
5918 &led_sysfs_get;
5919
5920 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5921
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005922 rc = led_classdev_register(&tpacpi_pdev->dev,
5923 &tpacpi_leds[led].led_classdev);
5924 if (rc < 0)
5925 tpacpi_leds[led].led_classdev.name = NULL;
5926
5927 return rc;
5928}
5929
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005930static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5931 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5932 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5933 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5934
5935 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5936 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5937 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5938 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5939 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5940 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5941 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5942 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5943
5944 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5945 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5946 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5947 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5948 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5949
5950 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5951 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5952 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5953 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5954
5955 /* (1) - may have excess leds enabled on MSB */
5956
5957 /* Defaults (order matters, keep last, don't reorder!) */
5958 { /* Lenovo */
5959 .vendor = PCI_VENDOR_ID_LENOVO,
5960 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5961 .quirks = 0x1fffU,
5962 },
5963 { /* IBM ThinkPads with no EC version string */
5964 .vendor = PCI_VENDOR_ID_IBM,
5965 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5966 .quirks = 0x00ffU,
5967 },
5968 { /* IBM ThinkPads with EC version string */
5969 .vendor = PCI_VENDOR_ID_IBM,
5970 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5971 .quirks = 0x00bfU,
5972 },
5973};
5974
5975#undef TPACPI_LEDQ_IBM
5976#undef TPACPI_LEDQ_LNV
5977
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005978static enum led_access_mode __init led_init_detect_mode(void)
5979{
5980 acpi_status status;
5981
5982 if (tpacpi_is_ibm()) {
5983 /* 570 */
5984 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5985 if (ACPI_SUCCESS(status))
5986 return TPACPI_LED_570;
5987
5988 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5989 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5990 if (ACPI_SUCCESS(status))
5991 return TPACPI_LED_OLD;
5992 }
5993
5994 /* most others */
5995 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5996 if (ACPI_SUCCESS(status))
5997 return TPACPI_LED_NEW;
5998
5999 /* R30, R31, and unknown firmwares */
6000 led_handle = NULL;
6001 return TPACPI_LED_NONE;
6002}
6003
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006004static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006005{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006006 unsigned int i;
6007 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006008 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006009
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006010 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
6011
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03006012 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006013
Adam Leefcb44e12013-06-07 16:20:08 +08006014 if (led_supported != TPACPI_LED_NONE) {
6015 useful_leds = tpacpi_check_quirks(led_useful_qtable,
6016 ARRAY_SIZE(led_useful_qtable));
6017
6018 if (!useful_leds) {
6019 led_handle = NULL;
6020 led_supported = TPACPI_LED_NONE;
6021 }
6022 }
6023
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006024 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
6025 str_supported(led_supported), led_supported);
6026
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006027 if (led_supported == TPACPI_LED_NONE)
6028 return 1;
6029
Kees Cook6396bb22018-06-12 14:03:40 -07006030 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006031 GFP_KERNEL);
6032 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07006033 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006034 return -ENOMEM;
6035 }
6036
6037 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08006038 tpacpi_leds[i].led = -1;
6039
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006040 if (!tpacpi_is_led_restricted(i) &&
6041 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006042 rc = tpacpi_init_led(i);
6043 if (rc < 0) {
6044 led_exit();
6045 return rc;
6046 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006047 }
6048 }
6049
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006050#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006051 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006052#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006053 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006054}
6055
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006056#define str_led_status(s) \
6057 ((s) == TPACPI_LED_OFF ? "off" : \
6058 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006059
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006060static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006062 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006063 seq_printf(m, "status:\t\tnot supported\n");
6064 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006065 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006066 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006067
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006068 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006069 /* 570 */
6070 int i, status;
6071 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006072 status = led_get_status(i);
6073 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006074 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006075 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006076 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006077 }
6078 }
6079
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006080 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083}
6084
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006085static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086{
6087 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006088 int led, rc;
6089 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006090
6091 if (!led_supported)
6092 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
6094 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08006095 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 return -EINVAL;
6097
Adam Leeedf2d772013-06-08 16:51:15 +08006098 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
6099 tpacpi_leds[led].led < 0)
6100 return -ENODEV;
6101
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006102 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006103 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006105 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006106 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006107 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006108 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006109 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006110 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006111
6112 rc = led_set_status(led, s);
6113 if (rc < 0)
6114 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 }
6116
6117 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006118}
6119
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006120static struct ibm_struct led_driver_data = {
6121 .name = "led",
6122 .read = led_read,
6123 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006124 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006125};
6126
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006127/*************************************************************************
6128 * Beep subdriver
6129 */
6130
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006131TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006132
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006133#define TPACPI_BEEP_Q1 0x0001
6134
6135static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6136 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6137 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6138};
6139
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006140static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006141{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006142 unsigned long quirks;
6143
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006144 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6145
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006146 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006147
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006148 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6149 str_supported(beep_handle != NULL));
6150
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006151 quirks = tpacpi_check_quirks(beep_quirk_table,
6152 ARRAY_SIZE(beep_quirk_table));
6153
6154 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6155
Jan van den Berg72a979f2014-09-17 00:01:08 +02006156 return (beep_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006157}
6158
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006159static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006161 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006162 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006163 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006164 seq_printf(m, "status:\t\tsupported\n");
6165 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169}
6170
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006171static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172{
6173 char *cmd;
6174 int beep_cmd;
6175
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006176 if (!beep_handle)
6177 return -ENODEV;
6178
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006180 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6181 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 /* beep_cmd set */
6183 } else
6184 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006185 if (tp_features.beep_needs_two_args) {
6186 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6187 beep_cmd, 0))
6188 return -EIO;
6189 } else {
6190 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6191 beep_cmd))
6192 return -EIO;
6193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 }
6195
6196 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006197}
6198
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006199static struct ibm_struct beep_driver_data = {
6200 .name = "beep",
6201 .read = beep_read,
6202 .write = beep_write,
6203};
6204
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006205/*************************************************************************
6206 * Thermal subdriver
6207 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006208
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006209enum thermal_access_mode {
6210 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6211 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6212 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6213 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6214 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6215};
6216
6217enum { /* TPACPI_THERMAL_TPEC_* */
6218 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6219 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6220 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006221
6222 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006223};
6224
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006225
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006226#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6227struct ibm_thermal_sensors_struct {
6228 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6229};
6230
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006231static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006232
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006233/* idx is zero-based */
6234static int thermal_get_sensor(int idx, s32 *value)
6235{
6236 int t;
6237 s8 tmp;
6238 char tmpi[5];
6239
6240 t = TP_EC_THERMAL_TMP0;
6241
6242 switch (thermal_read_mode) {
6243#if TPACPI_MAX_THERMAL_SENSORS >= 16
6244 case TPACPI_THERMAL_TPEC_16:
6245 if (idx >= 8 && idx <= 15) {
6246 t = TP_EC_THERMAL_TMP8;
6247 idx -= 8;
6248 }
6249 /* fallthrough */
6250#endif
6251 case TPACPI_THERMAL_TPEC_8:
6252 if (idx <= 7) {
6253 if (!acpi_ec_read(t + idx, &tmp))
6254 return -EIO;
6255 *value = tmp * 1000;
6256 return 0;
6257 }
6258 break;
6259
6260 case TPACPI_THERMAL_ACPI_UPDT:
6261 if (idx <= 7) {
6262 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6263 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6264 return -EIO;
6265 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6266 return -EIO;
6267 *value = (t - 2732) * 100;
6268 return 0;
6269 }
6270 break;
6271
6272 case TPACPI_THERMAL_ACPI_TMP07:
6273 if (idx <= 7) {
6274 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6275 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6276 return -EIO;
6277 if (t > 127 || t < -127)
6278 t = TP_EC_THERMAL_TMP_NA;
6279 *value = t * 1000;
6280 return 0;
6281 }
6282 break;
6283
6284 case TPACPI_THERMAL_NONE:
6285 default:
6286 return -ENOSYS;
6287 }
6288
6289 return -EINVAL;
6290}
6291
6292static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6293{
6294 int res, i;
6295 int n;
6296
6297 n = 8;
6298 i = 0;
6299
6300 if (!s)
6301 return -EINVAL;
6302
6303 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6304 n = 16;
6305
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006306 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006307 res = thermal_get_sensor(i, &s->temp[i]);
6308 if (res)
6309 return res;
6310 }
6311
6312 return n;
6313}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006314
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006315static void thermal_dump_all_sensors(void)
6316{
6317 int n, i;
6318 struct ibm_thermal_sensors_struct t;
6319
6320 n = thermal_get_sensors(&t);
6321 if (n <= 0)
6322 return;
6323
Joe Perches0978e012011-04-04 10:06:25 -07006324 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006325
6326 for (i = 0; i < n; i++) {
6327 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07006328 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006329 else
Joe Perches0978e012011-04-04 10:06:25 -07006330 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006331 }
6332
Joe Perches0978e012011-04-04 10:06:25 -07006333 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006334}
6335
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006336/* sysfs temp##_input -------------------------------------------------- */
6337
6338static ssize_t thermal_temp_input_show(struct device *dev,
6339 struct device_attribute *attr,
6340 char *buf)
6341{
6342 struct sensor_device_attribute *sensor_attr =
6343 to_sensor_dev_attr(attr);
6344 int idx = sensor_attr->index;
6345 s32 value;
6346 int res;
6347
6348 res = thermal_get_sensor(idx, &value);
6349 if (res)
6350 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006351 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006352 return -ENXIO;
6353
6354 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6355}
6356
6357#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006358 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6359 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006360
6361static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6362 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6363 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6364 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6365 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6366 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6367 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6368 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6369 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6370 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6371 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6372 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6373 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6374 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6375 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6376 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6377 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6378};
6379
6380#define THERMAL_ATTRS(X) \
6381 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6382
6383static struct attribute *thermal_temp_input_attr[] = {
6384 THERMAL_ATTRS(8),
6385 THERMAL_ATTRS(9),
6386 THERMAL_ATTRS(10),
6387 THERMAL_ATTRS(11),
6388 THERMAL_ATTRS(12),
6389 THERMAL_ATTRS(13),
6390 THERMAL_ATTRS(14),
6391 THERMAL_ATTRS(15),
6392 THERMAL_ATTRS(0),
6393 THERMAL_ATTRS(1),
6394 THERMAL_ATTRS(2),
6395 THERMAL_ATTRS(3),
6396 THERMAL_ATTRS(4),
6397 THERMAL_ATTRS(5),
6398 THERMAL_ATTRS(6),
6399 THERMAL_ATTRS(7),
6400 NULL
6401};
6402
6403static const struct attribute_group thermal_temp_input16_group = {
6404 .attrs = thermal_temp_input_attr
6405};
6406
6407static const struct attribute_group thermal_temp_input8_group = {
6408 .attrs = &thermal_temp_input_attr[8]
6409};
6410
6411#undef THERMAL_SENSOR_ATTR_TEMP
6412#undef THERMAL_ATTRS
6413
6414/* --------------------------------------------------------------------- */
6415
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006416static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006417{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006418 u8 t, ta1, ta2;
6419 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006420 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006421 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006422
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006423 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6424
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006425 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006426
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03006427 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006428 /*
6429 * Direct EC access mode: sensors at registers
6430 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6431 * non-implemented, thermal sensors return 0x80 when
6432 * not available
6433 */
6434
6435 ta1 = ta2 = 0;
6436 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006437 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006438 ta1 |= t;
6439 } else {
6440 ta1 = 0;
6441 break;
6442 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006443 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006444 ta2 |= t;
6445 } else {
6446 ta1 = 0;
6447 break;
6448 }
6449 }
6450 if (ta1 == 0) {
6451 /* This is sheer paranoia, but we handle it anyway */
6452 if (acpi_tmp7) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006453 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006454 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006455 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006456 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006457 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006458 }
6459 } else {
6460 thermal_read_mode =
6461 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006462 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006463 }
6464 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006465 if (tpacpi_is_ibm() &&
6466 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006467 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006468 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006469 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006470 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006471 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006472 }
6473 } else {
6474 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006475 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006476 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006477
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006478 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6479 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6480 thermal_read_mode);
6481
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006482 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006483 case TPACPI_THERMAL_TPEC_16:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006484 res = sysfs_create_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006485 &thermal_temp_input16_group);
6486 if (res)
6487 return res;
6488 break;
6489 case TPACPI_THERMAL_TPEC_8:
6490 case TPACPI_THERMAL_ACPI_TMP07:
6491 case TPACPI_THERMAL_ACPI_UPDT:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006492 res = sysfs_create_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006493 &thermal_temp_input8_group);
6494 if (res)
6495 return res;
6496 break;
6497 case TPACPI_THERMAL_NONE:
6498 default:
6499 return 1;
6500 }
6501
6502 return 0;
6503}
6504
6505static void thermal_exit(void)
6506{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006507 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006508 case TPACPI_THERMAL_TPEC_16:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006509 sysfs_remove_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006510 &thermal_temp_input16_group);
6511 break;
6512 case TPACPI_THERMAL_TPEC_8:
6513 case TPACPI_THERMAL_ACPI_TMP07:
6514 case TPACPI_THERMAL_ACPI_UPDT:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006515 sysfs_remove_group(&tpacpi_hwmon->kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08006516 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006517 break;
6518 case TPACPI_THERMAL_NONE:
6519 default:
6520 break;
6521 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006522}
6523
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006524static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006525{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006526 int n, i;
6527 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006528
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006529 n = thermal_get_sensors(&t);
6530 if (unlikely(n < 0))
6531 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006532
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006533 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006534
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006535 if (n > 0) {
6536 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006537 seq_printf(m, "%d ", t.temp[i] / 1000);
6538 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006539 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006540 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006541
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006542 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006543}
6544
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006545static struct ibm_struct thermal_driver_data = {
6546 .name = "thermal",
6547 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006548 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006549};
6550
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006551/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006552 * Backlight/brightness subdriver
6553 */
Holger Macht8acb0252006-10-20 14:30:28 -07006554
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006555#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6556
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006557/*
6558 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6559 * CMOS NVRAM byte 0x5E, bits 0-3.
6560 *
6561 * EC HBRV (0x31) has the following layout
6562 * Bit 7: unknown function
6563 * Bit 6: unknown function
6564 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6565 * Bit 4: must be set to zero to avoid problems
6566 * Bit 3-0: backlight brightness level
6567 *
6568 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006569 *
6570 * WARNING: The X61 has been verified to use HBRV for something else, so
6571 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6572 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006573 */
6574
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006575enum {
6576 TP_EC_BACKLIGHT = 0x31,
6577
6578 /* TP_EC_BACKLIGHT bitmasks */
6579 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6580 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6581 TP_EC_BACKLIGHT_MAPSW = 0x20,
6582};
6583
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006584enum tpacpi_brightness_access_mode {
6585 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6586 TPACPI_BRGHT_MODE_EC, /* EC control */
6587 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6588 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6589 TPACPI_BRGHT_MODE_MAX
6590};
6591
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006592static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006593
6594static enum tpacpi_brightness_access_mode brightness_mode =
6595 TPACPI_BRGHT_MODE_MAX;
6596
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006597static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6598
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006599static struct mutex brightness_mutex;
6600
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006601/* NVRAM brightness access,
6602 * call with brightness_mutex held! */
6603static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006604{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006605 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006606
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006607 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6608 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6609 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006610 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006611
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006612 return lnvram;
6613}
6614
6615static void tpacpi_brightness_checkpoint_nvram(void)
6616{
6617 u8 lec = 0;
6618 u8 b_nvram;
6619
6620 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6621 return;
6622
6623 vdbg_printk(TPACPI_DBG_BRGHT,
6624 "trying to checkpoint backlight level to NVRAM...\n");
6625
6626 if (mutex_lock_killable(&brightness_mutex) < 0)
6627 return;
6628
6629 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6630 goto unlock;
6631 lec &= TP_EC_BACKLIGHT_LVLMSK;
6632 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6633
6634 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6635 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6636 /* NVRAM needs update */
6637 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6638 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6639 b_nvram |= lec;
6640 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6641 dbg_printk(TPACPI_DBG_BRGHT,
6642 "updated NVRAM backlight level to %u (0x%02x)\n",
6643 (unsigned int) lec, (unsigned int) b_nvram);
6644 } else
6645 vdbg_printk(TPACPI_DBG_BRGHT,
6646 "NVRAM backlight level already is %u (0x%02x)\n",
6647 (unsigned int) lec, (unsigned int) b_nvram);
6648
6649unlock:
6650 mutex_unlock(&brightness_mutex);
6651}
6652
6653
6654/* call with brightness_mutex held! */
6655static int tpacpi_brightness_get_raw(int *status)
6656{
6657 u8 lec = 0;
6658
6659 switch (brightness_mode) {
6660 case TPACPI_BRGHT_MODE_UCMS_STEP:
6661 *status = tpacpi_brightness_nvram_get();
6662 return 0;
6663 case TPACPI_BRGHT_MODE_EC:
6664 case TPACPI_BRGHT_MODE_ECNVRAM:
6665 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006666 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006667 *status = lec;
6668 return 0;
6669 default:
6670 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006671 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006672}
6673
6674/* call with brightness_mutex held! */
6675/* do NOT call with illegal backlight level value */
6676static int tpacpi_brightness_set_ec(unsigned int value)
6677{
6678 u8 lec = 0;
6679
6680 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6681 return -EIO;
6682
6683 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6684 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6685 (value & TP_EC_BACKLIGHT_LVLMSK))))
6686 return -EIO;
6687
6688 return 0;
6689}
6690
6691/* call with brightness_mutex held! */
6692static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6693{
6694 int cmos_cmd, inc;
6695 unsigned int current_value, i;
6696
6697 current_value = tpacpi_brightness_nvram_get();
6698
6699 if (value == current_value)
6700 return 0;
6701
6702 cmos_cmd = (value > current_value) ?
6703 TP_CMOS_BRIGHTNESS_UP :
6704 TP_CMOS_BRIGHTNESS_DOWN;
6705 inc = (value > current_value) ? 1 : -1;
6706
6707 for (i = current_value; i != value; i += inc)
6708 if (issue_thinkpad_cmos_command(cmos_cmd))
6709 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006710
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006711 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006712}
6713
6714/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006715static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006716{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006717 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006718
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006719 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006720 return -EINVAL;
6721
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006722 vdbg_printk(TPACPI_DBG_BRGHT,
6723 "set backlight level to %d\n", value);
6724
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006725 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006726 if (res < 0)
6727 return res;
6728
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006729 switch (brightness_mode) {
6730 case TPACPI_BRGHT_MODE_EC:
6731 case TPACPI_BRGHT_MODE_ECNVRAM:
6732 res = tpacpi_brightness_set_ec(value);
6733 break;
6734 case TPACPI_BRGHT_MODE_UCMS_STEP:
6735 res = tpacpi_brightness_set_ucmsstep(value);
6736 break;
6737 default:
6738 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006739 }
6740
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006741 mutex_unlock(&brightness_mutex);
6742 return res;
6743}
6744
6745/* sysfs backlight class ----------------------------------------------- */
6746
6747static int brightness_update_status(struct backlight_device *bd)
6748{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006749 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006750 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6751 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006752 bd->props.brightness : 0;
6753
6754 dbg_printk(TPACPI_DBG_BRGHT,
6755 "backlight: attempt to set level to %d\n",
6756 level);
6757
6758 /* it is the backlight class's job (caller) to handle
6759 * EINTR and other errors properly */
6760 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006761}
Holger Macht8acb0252006-10-20 14:30:28 -07006762
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006763static int brightness_get(struct backlight_device *bd)
6764{
6765 int status, res;
6766
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006767 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006768 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006769 return 0;
6770
6771 res = tpacpi_brightness_get_raw(&status);
6772
6773 mutex_unlock(&brightness_mutex);
6774
6775 if (res < 0)
6776 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006777
6778 return status & TP_EC_BACKLIGHT_LVLMSK;
6779}
6780
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006781static void tpacpi_brightness_notify_change(void)
6782{
6783 backlight_force_update(ibm_backlight_device,
6784 BACKLIGHT_UPDATE_HOTKEY);
6785}
6786
Lionel Debrouxacc24722010-11-16 14:14:02 +01006787static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006788 .get_brightness = brightness_get,
6789 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006790};
6791
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006792/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006793
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006794/*
6795 * Call _BCL method of video device. On some ThinkPads this will
6796 * switch the firmware to the ACPI brightness control mode.
6797 */
6798
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006799static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6800{
6801 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6802 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006803 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006804 int rc;
6805
Aaron Lu46445b62013-10-11 21:27:46 +08006806 if (acpi_bus_get_device(handle, &device))
6807 return 0;
6808
6809 rc = 0;
6810 list_for_each_entry(child, &device->children, node) {
6811 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6812 NULL, &buffer);
6813 if (ACPI_FAILURE(status))
6814 continue;
6815
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006816 obj = (union acpi_object *)buffer.pointer;
6817 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006818 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006819 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006820 rc = 0;
6821 } else {
6822 rc = obj->package.count;
6823 }
Aaron Lu46445b62013-10-11 21:27:46 +08006824 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006825 }
6826
6827 kfree(buffer.pointer);
6828 return rc;
6829}
6830
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006831
6832/*
6833 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6834 */
6835static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6836{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006837 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006838 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006839
Aaron Lu46445b62013-10-11 21:27:46 +08006840 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006841 if (video_device)
6842 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006843
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006844 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006845
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006846 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006847}
6848
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006849/*
6850 * These are only useful for models that have only one possibility
6851 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6852 * these quirks.
6853 */
6854#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6855#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6856#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6857
6858static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6859 /* Models with ATI GPUs known to require ECNVRAM mode */
6860 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6861
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006862 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006863 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006864 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006865 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006866 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6867
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006868 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006869 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006870 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6871 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006872
6873 /* Models with Intel GMA900 */
6874 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6875 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6876 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6877};
6878
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006879/*
6880 * Returns < 0 for error, otherwise sets tp_features.bright_*
6881 * and bright_maxlvl.
6882 */
6883static void __init tpacpi_detect_brightness_capabilities(void)
6884{
6885 unsigned int b;
6886
6887 vdbg_printk(TPACPI_DBG_INIT,
6888 "detecting firmware brightness interface capabilities\n");
6889
6890 /* we could run a quirks check here (same table used by
6891 * brightness_init) if needed */
6892
6893 /*
6894 * We always attempt to detect acpi support, so as to switch
6895 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6896 * going to publish a backlight interface
6897 */
6898 b = tpacpi_check_std_acpi_brightness_support();
6899 switch (b) {
6900 case 16:
6901 bright_maxlvl = 15;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006902 break;
6903 case 8:
6904 case 0:
6905 bright_maxlvl = 7;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006906 break;
6907 default:
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006908 tp_features.bright_unkfw = 1;
6909 bright_maxlvl = b - 1;
6910 }
Eric Curtin15c75622016-01-30 16:55:59 +00006911 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006912}
6913
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006914static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006915{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006916 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006917 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006918 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006919
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006920 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6921
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006922 mutex_init(&brightness_mutex);
6923
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006924 quirks = tpacpi_check_quirks(brightness_quirk_table,
6925 ARRAY_SIZE(brightness_quirk_table));
6926
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006927 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006928
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006929 /* if it is unknown, we don't handle it: it wouldn't be safe */
6930 if (tp_features.bright_unkfw)
6931 return 1;
6932
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006933 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006934 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006935 "brightness support disabled by module parameter\n");
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006936 return 1;
6937 }
6938
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02006939 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006940 if (brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006941 pr_info("Standard ACPI backlight interface available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006942 return 1;
6943 } else if (brightness_enable == 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006944 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 -03006945 return 1;
6946 }
6947 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006948 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006949 }
6950
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006951 /*
6952 * Check for module parameter bogosity, note that we
6953 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6954 * able to detect "unspecified"
6955 */
6956 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006957 return -EINVAL;
6958
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006959 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6960 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6961 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006962 if (quirks & TPACPI_BRGHT_Q_EC)
6963 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6964 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006965 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6966
6967 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006968 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006969 brightness_mode);
6970 }
6971
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006972 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006973 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006974 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6975 brightness_mode == TPACPI_BRGHT_MODE_EC))
6976 return -EINVAL;
6977
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006978 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006979 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006980
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006981 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006982 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006983 props.max_brightness = bright_maxlvl;
6984 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006985 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6986 NULL, NULL,
6987 &ibm_backlight_data,
6988 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006989 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006990 int rc = PTR_ERR(ibm_backlight_device);
6991 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006992 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006993 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006994 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006995 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6996 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006997
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006998 if (quirks & TPACPI_BRGHT_Q_ASK) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006999 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
7000 brightness_mode);
7001 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
7002 TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03007003 }
7004
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03007005 /* Added by mistake in early 2007. Probably useless, but it could
7006 * be working around some unknown firmware problem where the value
7007 * read at startup doesn't match the real hardware state... so leave
7008 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02007009 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00007010
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007011 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007012 "brightness: registering brightness hotkeys as change notification\n");
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007013 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7014 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08007015 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007016 return 0;
7017}
7018
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007019static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007020{
7021 tpacpi_brightness_checkpoint_nvram();
7022}
7023
7024static void brightness_shutdown(void)
7025{
7026 tpacpi_brightness_checkpoint_nvram();
7027}
7028
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007029static void brightness_exit(void)
7030{
7031 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007032 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007033 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007034 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007035 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007036
7037 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007038}
7039
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007040static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007041{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007042 int level;
7043
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007044 level = brightness_get(NULL);
7045 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007046 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007047 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007048 seq_printf(m, "level:\t\t%d\n", level);
7049 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007050 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7051 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007052 }
7053
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007054 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007055}
7056
7057static int brightness_write(char *buf)
7058{
7059 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007060 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007061 char *cmd;
7062
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007063 level = brightness_get(NULL);
7064 if (level < 0)
7065 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007066
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007067 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007068 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007069 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007070 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007071 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007072 if (level > 0)
7073 level--;
7074 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007075 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007076 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007077 } else
7078 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007079 }
7080
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007081 tpacpi_disclose_usertask("procfs brightness",
7082 "set level to %d\n", level);
7083
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007084 /*
7085 * Now we know what the final level should be, so we try to set it.
7086 * Doing it this way makes the syscall restartable in case of EINTR
7087 */
7088 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007089 if (!rc && ibm_backlight_device)
7090 backlight_force_update(ibm_backlight_device,
7091 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02007092 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007093}
7094
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007095static struct ibm_struct brightness_driver_data = {
7096 .name = "brightness",
7097 .read = brightness_read,
7098 .write = brightness_write,
7099 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007100 .suspend = brightness_suspend,
7101 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007102};
7103
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007104/*************************************************************************
7105 * Volume subdriver
7106 */
7107
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007108/*
7109 * IBM ThinkPads have a simple volume controller with MUTE gating.
7110 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7111 *
7112 * Since the *61 series (and probably also the later *60 series), Lenovo
7113 * ThinkPads only implement the MUTE gate.
7114 *
7115 * EC register 0x30
7116 * Bit 6: MUTE (1 mutes sound)
7117 * Bit 3-0: Volume
7118 * Other bits should be zero as far as we know.
7119 *
7120 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7121 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7122 * such as bit 7 which is used to detect repeated presses of MUTE,
7123 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007124 *
7125 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7126 * in response to user input. Unfortunately, this rarely works well.
7127 * The laptop changes the state of its internal MUTE gate and, on some
7128 * models, sends KEY_MUTE, causing any user code that responds to the
7129 * mute button to get confused. The hardware MUTE gate is also
7130 * unnecessary, since user code can handle the mute button without
7131 * kernel or EC help.
7132 *
7133 * To avoid confusing userspace, we simply disable all EC-based mute
7134 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007135 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007136
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007137#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7138
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007139#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7140#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7141#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7142
Takashi Iwaicab66612013-10-24 16:06:32 +02007143#if SNDRV_CARDS <= 32
7144#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7145#else
7146#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7147#endif
7148static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007149static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307150static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007151
7152struct tpacpi_alsa_data {
7153 struct snd_card *card;
7154 struct snd_ctl_elem_id *ctl_mute_id;
7155 struct snd_ctl_elem_id *ctl_vol_id;
7156};
7157
7158static struct snd_card *alsa_card;
7159
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007160enum {
7161 TP_EC_AUDIO = 0x30,
7162
7163 /* TP_EC_AUDIO bits */
7164 TP_EC_AUDIO_MUTESW = 6,
7165
7166 /* TP_EC_AUDIO bitmasks */
7167 TP_EC_AUDIO_LVL_MSK = 0x0F,
7168 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7169
7170 /* Maximum volume */
7171 TP_EC_VOLUME_MAX = 14,
7172};
7173
7174enum tpacpi_volume_access_mode {
7175 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7176 TPACPI_VOL_MODE_EC, /* Pure EC control */
7177 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7178 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7179 TPACPI_VOL_MODE_MAX
7180};
7181
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007182enum tpacpi_volume_capabilities {
7183 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7184 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7185 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7186 TPACPI_VOL_CAP_MAX
7187};
7188
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007189enum tpacpi_mute_btn_mode {
7190 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7191 /* We don't know what mode 1 is. */
7192 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7193 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7194};
7195
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007196static enum tpacpi_volume_access_mode volume_mode =
7197 TPACPI_VOL_MODE_MAX;
7198
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007199static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307200static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007201static bool software_mute_requested = true;
7202static bool software_mute_active;
7203static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007204
7205/*
7206 * Used to syncronize writers to TP_EC_AUDIO and
7207 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7208 */
7209static struct mutex volume_mutex;
7210
7211static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007212{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007213 u8 lec = 0;
7214 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007215 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007216
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007217 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7218 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007219 if (!volume_control_allowed)
7220 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007221 if (software_mute_active)
7222 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007223
7224 vdbg_printk(TPACPI_DBG_MIXER,
7225 "trying to checkpoint mixer state to NVRAM...\n");
7226
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007227 if (tp_features.mixer_no_level_control)
7228 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7229 else
7230 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7231
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007232 if (mutex_lock_killable(&volume_mutex) < 0)
7233 return;
7234
7235 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7236 goto unlock;
7237 lec &= ec_mask;
7238 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7239
7240 if (lec != (b_nvram & ec_mask)) {
7241 /* NVRAM needs update */
7242 b_nvram &= ~ec_mask;
7243 b_nvram |= lec;
7244 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7245 dbg_printk(TPACPI_DBG_MIXER,
7246 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7247 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007248 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007249 vdbg_printk(TPACPI_DBG_MIXER,
7250 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7251 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007252 }
7253
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007254unlock:
7255 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007256}
7257
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007258static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007259{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007260 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007261
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007262 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7263 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007264
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007265 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007266
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007267 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007268
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007269 return 0;
7270}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007271
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007272static int volume_get_status(u8 *status)
7273{
7274 return volume_get_status_ec(status);
7275}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007276
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007277static int volume_set_status_ec(const u8 status)
7278{
7279 if (!acpi_ec_write(TP_EC_AUDIO, status))
7280 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007281
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007282 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7283
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007284 /*
7285 * On X200s, and possibly on others, it can take a while for
7286 * reads to become correct.
7287 */
7288 msleep(1);
7289
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007290 return 0;
7291}
7292
7293static int volume_set_status(const u8 status)
7294{
7295 return volume_set_status_ec(status);
7296}
7297
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007298/* returns < 0 on error, 0 on no change, 1 on change */
7299static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007300{
7301 int rc;
7302 u8 s, n;
7303
7304 if (mutex_lock_killable(&volume_mutex) < 0)
7305 return -EINTR;
7306
7307 rc = volume_get_status_ec(&s);
7308 if (rc)
7309 goto unlock;
7310
7311 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7312 s & ~TP_EC_AUDIO_MUTESW_MSK;
7313
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007314 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007315 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007316 if (!rc)
7317 rc = 1;
7318 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007319
7320unlock:
7321 mutex_unlock(&volume_mutex);
7322 return rc;
7323}
7324
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007325static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007326{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007327 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007328 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007329 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007330}
7331
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007332static int volume_set_mute(const bool mute)
7333{
7334 int rc;
7335
7336 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7337 (mute) ? "" : "un");
7338
7339 rc = __volume_set_mute_ec(mute);
7340 return (rc < 0) ? rc : 0;
7341}
7342
7343/* returns < 0 on error, 0 on no change, 1 on change */
7344static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007345{
7346 int rc;
7347 u8 s, n;
7348
7349 if (vol > TP_EC_VOLUME_MAX)
7350 return -EINVAL;
7351
7352 if (mutex_lock_killable(&volume_mutex) < 0)
7353 return -EINTR;
7354
7355 rc = volume_get_status_ec(&s);
7356 if (rc)
7357 goto unlock;
7358
7359 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7360
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007361 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007362 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007363 if (!rc)
7364 rc = 1;
7365 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007366
7367unlock:
7368 mutex_unlock(&volume_mutex);
7369 return rc;
7370}
7371
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007372static int volume_set_software_mute(bool startup)
7373{
7374 int result;
7375
7376 if (!tpacpi_is_lenovo())
7377 return -ENODEV;
7378
7379 if (startup) {
7380 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7381 "HAUM", "qd"))
7382 return -EIO;
7383
7384 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7385 "Initial HAUM setting was %d\n",
7386 software_mute_orig_mode);
7387 }
7388
7389 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7390 (int)TP_EC_MUTE_BTN_NONE))
7391 return -EIO;
7392
7393 if (result != TP_EC_MUTE_BTN_NONE)
7394 pr_warn("Unexpected SAUM result %d\n",
7395 result);
7396
7397 /*
7398 * In software mute mode, the standard codec controls take
7399 * precendence, so we unmute the ThinkPad HW switch at
7400 * startup. Just on case there are SAUM-capable ThinkPads
7401 * with level controls, set max HW volume as well.
7402 */
7403 if (tp_features.mixer_no_level_control)
7404 result = volume_set_mute(false);
7405 else
7406 result = volume_set_status(TP_EC_VOLUME_MAX);
7407
7408 if (result != 0)
7409 pr_warn("Failed to unmute the HW mute switch\n");
7410
7411 return 0;
7412}
7413
7414static void volume_exit_software_mute(void)
7415{
7416 int r;
7417
7418 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7419 || r != software_mute_orig_mode)
7420 pr_warn("Failed to restore mute mode\n");
7421}
7422
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007423static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007424{
7425 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007426 "ALSA: trying to set volume level to %hu\n", vol);
7427 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007428}
7429
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007430static void volume_alsa_notify_change(void)
7431{
7432 struct tpacpi_alsa_data *d;
7433
7434 if (alsa_card && alsa_card->private_data) {
7435 d = alsa_card->private_data;
7436 if (d->ctl_mute_id)
7437 snd_ctl_notify(alsa_card,
7438 SNDRV_CTL_EVENT_MASK_VALUE,
7439 d->ctl_mute_id);
7440 if (d->ctl_vol_id)
7441 snd_ctl_notify(alsa_card,
7442 SNDRV_CTL_EVENT_MASK_VALUE,
7443 d->ctl_vol_id);
7444 }
7445}
7446
7447static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7448 struct snd_ctl_elem_info *uinfo)
7449{
7450 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7451 uinfo->count = 1;
7452 uinfo->value.integer.min = 0;
7453 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7454 return 0;
7455}
7456
7457static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7458 struct snd_ctl_elem_value *ucontrol)
7459{
7460 u8 s;
7461 int rc;
7462
7463 rc = volume_get_status(&s);
7464 if (rc < 0)
7465 return rc;
7466
7467 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7468 return 0;
7469}
7470
7471static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7472 struct snd_ctl_elem_value *ucontrol)
7473{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007474 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7475 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007476 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007477}
7478
7479#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7480
7481static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7482 struct snd_ctl_elem_value *ucontrol)
7483{
7484 u8 s;
7485 int rc;
7486
7487 rc = volume_get_status(&s);
7488 if (rc < 0)
7489 return rc;
7490
7491 ucontrol->value.integer.value[0] =
7492 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7493 return 0;
7494}
7495
7496static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7497 struct snd_ctl_elem_value *ucontrol)
7498{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007499 tpacpi_disclose_usertask("ALSA", "%smute\n",
7500 ucontrol->value.integer.value[0] ?
7501 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007502 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007503}
7504
Mathias Krausec3aa4722014-07-16 19:43:17 +02007505static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007506 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7507 .name = "Console Playback Volume",
7508 .index = 0,
7509 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7510 .info = volume_alsa_vol_info,
7511 .get = volume_alsa_vol_get,
7512};
7513
Mathias Krausec3aa4722014-07-16 19:43:17 +02007514static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007515 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7516 .name = "Console Playback Switch",
7517 .index = 0,
7518 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7519 .info = volume_alsa_mute_info,
7520 .get = volume_alsa_mute_get,
7521};
7522
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007523static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007524{
7525 tpacpi_volume_checkpoint_nvram();
7526}
7527
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007528static void volume_resume(void)
7529{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007530 if (software_mute_active) {
7531 if (volume_set_software_mute(false) < 0)
7532 pr_warn("Failed to restore software mute\n");
7533 } else {
7534 volume_alsa_notify_change();
7535 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007536}
7537
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007538static void volume_shutdown(void)
7539{
7540 tpacpi_volume_checkpoint_nvram();
7541}
7542
7543static void volume_exit(void)
7544{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007545 if (alsa_card) {
7546 snd_card_free(alsa_card);
7547 alsa_card = NULL;
7548 }
7549
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007550 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007551
7552 if (software_mute_active)
7553 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007554}
7555
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007556static int __init volume_create_alsa_mixer(void)
7557{
7558 struct snd_card *card;
7559 struct tpacpi_alsa_data *data;
7560 struct snd_kcontrol *ctl_vol;
7561 struct snd_kcontrol *ctl_mute;
7562 int rc;
7563
Takashi Iwai89235e52014-01-29 15:01:27 +01007564 rc = snd_card_new(&tpacpi_pdev->dev,
7565 alsa_index, alsa_id, THIS_MODULE,
7566 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007567 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007568 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007569 return 1;
7570 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007571
7572 BUG_ON(!card->private_data);
7573 data = card->private_data;
7574 data->card = card;
7575
7576 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7577 sizeof(card->driver));
7578 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7579 sizeof(card->shortname));
7580 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7581 (thinkpad_id.ec_version_str) ?
7582 thinkpad_id.ec_version_str : "(unknown)");
7583 snprintf(card->longname, sizeof(card->longname),
7584 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7585 (thinkpad_id.ec_version_str) ?
7586 thinkpad_id.ec_version_str : "unknown");
7587
7588 if (volume_control_allowed) {
7589 volume_alsa_control_vol.put = volume_alsa_vol_put;
7590 volume_alsa_control_vol.access =
7591 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7592
7593 volume_alsa_control_mute.put = volume_alsa_mute_put;
7594 volume_alsa_control_mute.access =
7595 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7596 }
7597
7598 if (!tp_features.mixer_no_level_control) {
7599 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7600 rc = snd_ctl_add(card, ctl_vol);
7601 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007602 pr_err("Failed to create ALSA volume control: %d\n",
7603 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007604 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007605 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007606 data->ctl_vol_id = &ctl_vol->id;
7607 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007608
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007609 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7610 rc = snd_ctl_add(card, ctl_mute);
7611 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007612 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007613 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007614 }
7615 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007616
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007617 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007618 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007619 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007620 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007621 }
7622
7623 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007624 return 0;
7625
7626err_exit:
7627 snd_card_free(card);
7628 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007629}
7630
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007631#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7632#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7633
7634static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7635 /* Whitelist volume level on all IBM by default */
7636 { .vendor = PCI_VENDOR_ID_IBM,
7637 .bios = TPACPI_MATCH_ANY,
7638 .ec = TPACPI_MATCH_ANY,
7639 .quirks = TPACPI_VOL_Q_LEVEL },
7640
7641 /* Lenovo models with volume control (needs confirmation) */
7642 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7643 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7644 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7645 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7646 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7647 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7648 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7649
7650 /* Whitelist mute-only on all Lenovo by default */
7651 { .vendor = PCI_VENDOR_ID_LENOVO,
7652 .bios = TPACPI_MATCH_ANY,
7653 .ec = TPACPI_MATCH_ANY,
7654 .quirks = TPACPI_VOL_Q_MUTEONLY }
7655};
7656
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007657static int __init volume_init(struct ibm_init_struct *iibm)
7658{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007659 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007660 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007661
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007662 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7663
7664 mutex_init(&volume_mutex);
7665
7666 /*
7667 * Check for module parameter bogosity, note that we
7668 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7669 * able to detect "unspecified"
7670 */
7671 if (volume_mode > TPACPI_VOL_MODE_MAX)
7672 return -EINVAL;
7673
7674 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007675 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7676 TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007677 return 1;
7678 }
7679
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007680 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7681 return -EINVAL;
7682
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007683 /*
7684 * The ALSA mixer is our primary interface.
7685 * When disabled, don't install the subdriver at all
7686 */
7687 if (!alsa_enable) {
7688 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007689 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007690 return 1;
7691 }
7692
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007693 quirks = tpacpi_check_quirks(volume_quirk_table,
7694 ARRAY_SIZE(volume_quirk_table));
7695
7696 switch (volume_capabilities) {
7697 case TPACPI_VOL_CAP_AUTO:
7698 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7699 tp_features.mixer_no_level_control = 1;
7700 else if (quirks & TPACPI_VOL_Q_LEVEL)
7701 tp_features.mixer_no_level_control = 0;
7702 else
7703 return 1; /* no mixer */
7704 break;
7705 case TPACPI_VOL_CAP_VOLMUTE:
7706 tp_features.mixer_no_level_control = 0;
7707 break;
7708 case TPACPI_VOL_CAP_MUTEONLY:
7709 tp_features.mixer_no_level_control = 1;
7710 break;
7711 default:
7712 return 1;
7713 }
7714
7715 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7716 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7717 "using user-supplied volume_capabilities=%d\n",
7718 volume_capabilities);
7719
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007720 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7721 volume_mode == TPACPI_VOL_MODE_MAX) {
7722 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7723
7724 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7725 "driver auto-selected volume_mode=%d\n",
7726 volume_mode);
7727 } else {
7728 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7729 "using user-supplied volume_mode=%d\n",
7730 volume_mode);
7731 }
7732
7733 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007734 "mute is supported, volume control is %s\n",
7735 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007736
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007737 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7738 software_mute_active = true;
7739 } else {
7740 rc = volume_create_alsa_mixer();
7741 if (rc) {
7742 pr_err("Could not create the ALSA mixer interface\n");
7743 return rc;
7744 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007745
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007746 pr_info("Console audio control enabled, mode: %s\n",
7747 (volume_control_allowed) ?
7748 "override (read/write)" :
7749 "monitor (read only)");
7750 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007751
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007752 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7753 "registering volume hotkeys as change notification\n");
7754 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7755 | TP_ACPI_HKEY_VOLUP_MASK
7756 | TP_ACPI_HKEY_VOLDWN_MASK
7757 | TP_ACPI_HKEY_MUTE_MASK);
7758
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007759 return 0;
7760}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007761
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007762static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007763{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007764 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007765
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007766 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007767 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007768 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007769 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007770 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007771 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007772 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007773 status & TP_EC_AUDIO_LVL_MSK);
7774
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007775 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007776 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007777
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007778 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007779 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007780 if (!tp_features.mixer_no_level_control) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007781 seq_printf(m, "commands:\tup, down\n");
7782 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7783 TP_EC_VOLUME_MAX);
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007784 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007785 }
7786 }
7787
7788 return 0;
7789}
7790
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007791static int volume_write(char *buf)
7792{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007793 u8 s;
7794 u8 new_level, new_mute;
7795 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007796 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007797 int rc;
7798
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007799 /*
7800 * We do allow volume control at driver startup, so that the
7801 * user can set initial state through the volume=... parameter hack.
7802 */
7803 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7804 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7805 tp_warned.volume_ctrl_forbidden = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007806 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7807 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007808 }
7809 return -EPERM;
7810 }
7811
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007812 rc = volume_get_status(&s);
7813 if (rc < 0)
7814 return rc;
7815
7816 new_level = s & TP_EC_AUDIO_LVL_MSK;
7817 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007818
7819 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007820 if (!tp_features.mixer_no_level_control) {
7821 if (strlencmp(cmd, "up") == 0) {
7822 if (new_mute)
7823 new_mute = 0;
7824 else if (new_level < TP_EC_VOLUME_MAX)
7825 new_level++;
7826 continue;
7827 } else if (strlencmp(cmd, "down") == 0) {
7828 if (new_mute)
7829 new_mute = 0;
7830 else if (new_level > 0)
7831 new_level--;
7832 continue;
7833 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7834 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7835 new_level = l;
7836 continue;
7837 }
7838 }
7839 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007840 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007841 else if (strlencmp(cmd, "unmute") == 0)
7842 new_mute = 0;
7843 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007844 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007845 }
7846
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007847 if (tp_features.mixer_no_level_control) {
7848 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7849 new_mute ? "" : "un");
7850 rc = volume_set_mute(!!new_mute);
7851 } else {
7852 tpacpi_disclose_usertask("procfs volume",
7853 "%smute and set level to %d\n",
7854 new_mute ? "" : "un", new_level);
7855 rc = volume_set_status(new_mute | new_level);
7856 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007857 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007858
7859 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007860}
7861
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007862static struct ibm_struct volume_driver_data = {
7863 .name = "volume",
7864 .read = volume_read,
7865 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007866 .exit = volume_exit,
7867 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007868 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007869 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007870};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007871
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007872#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7873
7874#define alsa_card NULL
7875
Tobias Klauserbb55a2e2016-11-25 14:18:07 +01007876static inline void volume_alsa_notify_change(void)
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007877{
7878}
7879
7880static int __init volume_init(struct ibm_init_struct *iibm)
7881{
Joe Perches0978e012011-04-04 10:06:25 -07007882 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007883
7884 return 1;
7885}
7886
7887static struct ibm_struct volume_driver_data = {
7888 .name = "volume",
7889};
7890
7891#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7892
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007893/*************************************************************************
7894 * Fan subdriver
7895 */
7896
7897/*
7898 * FAN ACCESS MODES
7899 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007900 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007901 * ACPI GFAN method: returns fan level
7902 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007903 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007904 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007905 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007906 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007907 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7908 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007909 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7910 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007911 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007912 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007913 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7914 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007915 *
7916 * Fan speed changes of any sort (including those caused by the
7917 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007918 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007919 * limited.
7920 *
7921 * Reading is not available if GFAN exists.
7922 * Writing is not available if SFAN exists.
7923 *
7924 * Bits
7925 * 7 automatic mode engaged;
7926 * (default operation mode of the ThinkPad)
7927 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007928 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007929 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007930 * the tachometer while the fan controller ramps up
7931 * the speed (which can take up to a few *minutes*).
7932 * Speeds up fan to 100% duty-cycle, which is far above
7933 * the standard RPM levels. It is not impossible that
7934 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007935 * 5-3 unused in some models. Extra bits for fan level
7936 * in others, but still useless as all values above
7937 * 7 map to the same speed as level 7 in these models.
7938 * 2-0 fan level (0..7 usually)
7939 * 0x00 = stop
7940 * 0x07 = max (set when temperatures critical)
7941 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007942 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007943 *
7944 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007945 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007946 * does so, its initial value is meaningless (0x07).
7947 *
7948 * For firmware bugs, refer to:
7949 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7950 *
7951 * ----
7952 *
7953 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7954 * Main fan tachometer reading (in RPM)
7955 *
7956 * This register is present on all ThinkPads with a new-style EC, and
7957 * it is known not to be present on the A21m/e, and T22, as there is
7958 * something else in offset 0x84 according to the ACPI DSDT. Other
7959 * ThinkPads from this same time period (and earlier) probably lack the
7960 * tachometer as well.
7961 *
Nick Andrew877d0312009-01-26 11:06:57 +01007962 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007963 * was never fixed by IBM to report the EC firmware version string
7964 * probably support the tachometer (like the early X models), so
7965 * detecting it is quite hard. We need more data to know for sure.
7966 *
7967 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7968 * might result.
7969 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007970 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7971 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007972 *
7973 * For firmware bugs, refer to:
7974 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7975 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007976 * ----
7977 *
7978 * ThinkPad EC register 0x31 bit 0 (only on select models)
7979 *
7980 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7981 * show the speed of the main fan. When bit 0 of EC register 0x31
7982 * is one, the tachometer registers show the speed of the auxiliary
7983 * fan.
7984 *
7985 * Fan control seems to affect both fans, regardless of the state
7986 * of this bit.
7987 *
7988 * So far, only the firmware for the X60/X61 non-tablet versions
7989 * seem to support this (firmware TP-7M).
7990 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007991 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007992 * ThinkPad X31, X40, X41. Not available in the X60.
7993 *
7994 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7995 * high speed. ACPI DSDT seems to map these three speeds to levels
7996 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7997 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7998 *
7999 * The speeds are stored on handles
8000 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
8001 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008002 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008003 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
8004 *
8005 * ACPI DSDT switches which set is in use depending on various
8006 * factors.
8007 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008008 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008009 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
8010 * but the ACPI tables just mention level 7.
8011 */
8012
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008013enum { /* Fan control constants */
8014 fan_status_offset = 0x2f, /* EC register 0x2f */
8015 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
8016 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008017 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
8018 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008019
8020 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
8021 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
8022
8023 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
8024};
8025
8026enum fan_status_access_mode {
8027 TPACPI_FAN_NONE = 0, /* No fan status or control */
8028 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
8029 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8030};
8031
8032enum fan_control_access_mode {
8033 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8034 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8035 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8036 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8037};
8038
8039enum fan_control_commands {
8040 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8041 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8042 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8043 * and also watchdog cmd */
8044};
8045
Rusty Russell90ab5ee2012-01-13 09:32:20 +10308046static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008047
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008048static enum fan_status_access_mode fan_status_access_mode;
8049static enum fan_control_access_mode fan_control_access_mode;
8050static enum fan_control_commands fan_control_commands;
8051
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008052static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008053static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008054static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008055static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008056
8057static struct mutex fan_mutex;
8058
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008059static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05008060static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008061
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008062TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8063TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008064 "\\FSPD", /* 600e/x, 770e, 770x */
8065 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008066TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008067 "JFNS", /* 770x-JL */
8068 ); /* all others */
8069
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008070/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008071 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8072 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8073 * be in auto mode (0x80).
8074 *
8075 * This is corrected by any write to HFSP either by the driver, or
8076 * by the firmware.
8077 *
8078 * We assume 0x07 really means auto mode while this quirk is active,
8079 * as this is far more likely than the ThinkPad being in level 7,
8080 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008081 *
8082 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8083 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008084 */
8085
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008086static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008087{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008088 if (fan_control_initial_status == 0x07) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008089 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 -03008090 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008091 }
8092}
8093
8094static void fan_quirk1_handle(u8 *fan_status)
8095{
8096 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8097 if (*fan_status != fan_control_initial_status) {
8098 /* something changed the HFSP regisnter since
8099 * driver init time, so it is not undefined
8100 * anymore */
8101 tp_features.fan_ctrl_status_undef = 0;
8102 } else {
8103 /* Return most likely status. In fact, it
8104 * might be the only possible status */
8105 *fan_status = TP_EC_FAN_AUTO;
8106 }
8107 }
8108}
8109
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008110/* Select main fan on X60/X61, NOOP on others */
8111static bool fan_select_fan1(void)
8112{
8113 if (tp_features.second_fan) {
8114 u8 val;
8115
8116 if (ec_read(fan_select_offset, &val) < 0)
8117 return false;
8118 val &= 0xFEU;
8119 if (ec_write(fan_select_offset, val) < 0)
8120 return false;
8121 }
8122 return true;
8123}
8124
8125/* Select secondary fan on X60/X61 */
8126static bool fan_select_fan2(void)
8127{
8128 u8 val;
8129
8130 if (!tp_features.second_fan)
8131 return false;
8132
8133 if (ec_read(fan_select_offset, &val) < 0)
8134 return false;
8135 val |= 0x01U;
8136 if (ec_write(fan_select_offset, val) < 0)
8137 return false;
8138
8139 return true;
8140}
8141
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008142/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008143 * Call with fan_mutex held
8144 */
8145static void fan_update_desired_level(u8 status)
8146{
8147 if ((status &
8148 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8149 if (status > 7)
8150 fan_control_desired_level = 7;
8151 else
8152 fan_control_desired_level = status;
8153 }
8154}
8155
8156static int fan_get_status(u8 *status)
8157{
8158 u8 s;
8159
8160 /* TODO:
8161 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8162
8163 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07008164 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008165 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07008166 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008167
Dan Carpentereceeb432012-09-01 12:54:07 -07008168 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008169 return -EIO;
8170
8171 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07008172 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008173
8174 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07008175 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008176 case TPACPI_FAN_RD_TPEC:
8177 /* all except 570, 600e/x, 770e, 770x */
8178 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8179 return -EIO;
8180
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008181 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008182 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008183 fan_quirk1_handle(status);
8184 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008185
8186 break;
8187
8188 default:
8189 return -ENXIO;
8190 }
8191
8192 return 0;
8193}
8194
8195static int fan_get_status_safe(u8 *status)
8196{
8197 int rc;
8198 u8 s;
8199
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008200 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008201 return -ERESTARTSYS;
8202 rc = fan_get_status(&s);
8203 if (!rc)
8204 fan_update_desired_level(s);
8205 mutex_unlock(&fan_mutex);
8206
Dan Carpentera7718362016-04-15 17:46:34 +03008207 if (rc)
8208 return rc;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008209 if (status)
8210 *status = s;
8211
Dan Carpentera7718362016-04-15 17:46:34 +03008212 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008213}
8214
8215static int fan_get_speed(unsigned int *speed)
8216{
8217 u8 hi, lo;
8218
8219 switch (fan_status_access_mode) {
8220 case TPACPI_FAN_RD_TPEC:
8221 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008222 if (unlikely(!fan_select_fan1()))
8223 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008224 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8225 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8226 return -EIO;
8227
8228 if (likely(speed))
8229 *speed = (hi << 8) | lo;
8230
8231 break;
8232
8233 default:
8234 return -ENXIO;
8235 }
8236
8237 return 0;
8238}
8239
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008240static int fan2_get_speed(unsigned int *speed)
8241{
8242 u8 hi, lo;
8243 bool rc;
8244
8245 switch (fan_status_access_mode) {
8246 case TPACPI_FAN_RD_TPEC:
8247 /* all except 570, 600e/x, 770e, 770x */
8248 if (unlikely(!fan_select_fan2()))
8249 return -EIO;
8250 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8251 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8252 fan_select_fan1(); /* play it safe */
8253 if (rc)
8254 return -EIO;
8255
8256 if (likely(speed))
8257 *speed = (hi << 8) | lo;
8258
8259 break;
8260
8261 default:
8262 return -ENXIO;
8263 }
8264
8265 return 0;
8266}
8267
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008268static int fan_set_level(int level)
8269{
8270 if (!fan_control_allowed)
8271 return -EPERM;
8272
8273 switch (fan_control_access_mode) {
8274 case TPACPI_FAN_WR_ACPI_SFAN:
8275 if (level >= 0 && level <= 7) {
8276 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8277 return -EIO;
8278 } else
8279 return -EINVAL;
8280 break;
8281
8282 case TPACPI_FAN_WR_ACPI_FANS:
8283 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008284 if (!(level & TP_EC_FAN_AUTO) &&
8285 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008286 ((level < 0) || (level > 7)))
8287 return -EINVAL;
8288
8289 /* safety net should the EC not support AUTO
8290 * or FULLSPEED mode bits and just ignore them */
8291 if (level & TP_EC_FAN_FULLSPEED)
8292 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01008293 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008294 level |= 4; /* safety min speed 4 */
8295
8296 if (!acpi_ec_write(fan_status_offset, level))
8297 return -EIO;
8298 else
8299 tp_features.fan_ctrl_status_undef = 0;
8300 break;
8301
8302 default:
8303 return -ENXIO;
8304 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008305
8306 vdbg_printk(TPACPI_DBG_FAN,
8307 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008308 return 0;
8309}
8310
8311static int fan_set_level_safe(int level)
8312{
8313 int rc;
8314
8315 if (!fan_control_allowed)
8316 return -EPERM;
8317
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008318 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008319 return -ERESTARTSYS;
8320
8321 if (level == TPACPI_FAN_LAST_LEVEL)
8322 level = fan_control_desired_level;
8323
8324 rc = fan_set_level(level);
8325 if (!rc)
8326 fan_update_desired_level(level);
8327
8328 mutex_unlock(&fan_mutex);
8329 return rc;
8330}
8331
8332static int fan_set_enable(void)
8333{
8334 u8 s;
8335 int rc;
8336
8337 if (!fan_control_allowed)
8338 return -EPERM;
8339
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008340 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008341 return -ERESTARTSYS;
8342
8343 switch (fan_control_access_mode) {
8344 case TPACPI_FAN_WR_ACPI_FANS:
8345 case TPACPI_FAN_WR_TPEC:
8346 rc = fan_get_status(&s);
8347 if (rc < 0)
8348 break;
8349
8350 /* Don't go out of emergency fan mode */
8351 if (s != 7) {
8352 s &= 0x07;
8353 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8354 }
8355
8356 if (!acpi_ec_write(fan_status_offset, s))
8357 rc = -EIO;
8358 else {
8359 tp_features.fan_ctrl_status_undef = 0;
8360 rc = 0;
8361 }
8362 break;
8363
8364 case TPACPI_FAN_WR_ACPI_SFAN:
8365 rc = fan_get_status(&s);
8366 if (rc < 0)
8367 break;
8368
8369 s &= 0x07;
8370
8371 /* Set fan to at least level 4 */
8372 s |= 4;
8373
8374 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008375 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008376 else
8377 rc = 0;
8378 break;
8379
8380 default:
8381 rc = -ENXIO;
8382 }
8383
8384 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008385
8386 if (!rc)
8387 vdbg_printk(TPACPI_DBG_FAN,
8388 "fan control: set fan control register to 0x%02x\n",
8389 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008390 return rc;
8391}
8392
8393static int fan_set_disable(void)
8394{
8395 int rc;
8396
8397 if (!fan_control_allowed)
8398 return -EPERM;
8399
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008400 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008401 return -ERESTARTSYS;
8402
8403 rc = 0;
8404 switch (fan_control_access_mode) {
8405 case TPACPI_FAN_WR_ACPI_FANS:
8406 case TPACPI_FAN_WR_TPEC:
8407 if (!acpi_ec_write(fan_status_offset, 0x00))
8408 rc = -EIO;
8409 else {
8410 fan_control_desired_level = 0;
8411 tp_features.fan_ctrl_status_undef = 0;
8412 }
8413 break;
8414
8415 case TPACPI_FAN_WR_ACPI_SFAN:
8416 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8417 rc = -EIO;
8418 else
8419 fan_control_desired_level = 0;
8420 break;
8421
8422 default:
8423 rc = -ENXIO;
8424 }
8425
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008426 if (!rc)
8427 vdbg_printk(TPACPI_DBG_FAN,
8428 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008429
8430 mutex_unlock(&fan_mutex);
8431 return rc;
8432}
8433
8434static int fan_set_speed(int speed)
8435{
8436 int rc;
8437
8438 if (!fan_control_allowed)
8439 return -EPERM;
8440
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008441 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008442 return -ERESTARTSYS;
8443
8444 rc = 0;
8445 switch (fan_control_access_mode) {
8446 case TPACPI_FAN_WR_ACPI_FANS:
8447 if (speed >= 0 && speed <= 65535) {
8448 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8449 speed, speed, speed))
8450 rc = -EIO;
8451 } else
8452 rc = -EINVAL;
8453 break;
8454
8455 default:
8456 rc = -ENXIO;
8457 }
8458
8459 mutex_unlock(&fan_mutex);
8460 return rc;
8461}
8462
8463static void fan_watchdog_reset(void)
8464{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008465 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8466 return;
8467
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008468 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008469 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8470 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8471 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8472 else
8473 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008474}
8475
8476static void fan_watchdog_fire(struct work_struct *ignored)
8477{
8478 int rc;
8479
8480 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8481 return;
8482
Joe Perches0978e012011-04-04 10:06:25 -07008483 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008484 rc = fan_set_enable();
8485 if (rc < 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008486 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8487 rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008488 /* reschedule for later */
8489 fan_watchdog_reset();
8490 }
8491}
8492
8493/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008494 * SYSFS fan layout: hwmon compatible (device)
8495 *
8496 * pwm*_enable:
8497 * 0: "disengaged" mode
8498 * 1: manual mode
8499 * 2: native EC "auto" mode (recommended, hardware default)
8500 *
8501 * pwm*: set speed in manual mode, ignored otherwise.
8502 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8503 * interpolation.
8504 *
8505 * fan*_input: tachometer reading, RPM
8506 *
8507 *
8508 * SYSFS fan layout: extensions
8509 *
8510 * fan_watchdog (driver):
8511 * fan watchdog interval in seconds, 0 disables (default), max 120
8512 */
8513
8514/* sysfs fan pwm1_enable ----------------------------------------------- */
8515static ssize_t fan_pwm1_enable_show(struct device *dev,
8516 struct device_attribute *attr,
8517 char *buf)
8518{
8519 int res, mode;
8520 u8 status;
8521
8522 res = fan_get_status_safe(&status);
8523 if (res)
8524 return res;
8525
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008526 if (status & TP_EC_FAN_FULLSPEED) {
8527 mode = 0;
8528 } else if (status & TP_EC_FAN_AUTO) {
8529 mode = 2;
8530 } else
8531 mode = 1;
8532
8533 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8534}
8535
8536static ssize_t fan_pwm1_enable_store(struct device *dev,
8537 struct device_attribute *attr,
8538 const char *buf, size_t count)
8539{
8540 unsigned long t;
8541 int res, level;
8542
8543 if (parse_strtoul(buf, 2, &t))
8544 return -EINVAL;
8545
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008546 tpacpi_disclose_usertask("hwmon pwm1_enable",
8547 "set fan mode to %lu\n", t);
8548
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008549 switch (t) {
8550 case 0:
8551 level = TP_EC_FAN_FULLSPEED;
8552 break;
8553 case 1:
8554 level = TPACPI_FAN_LAST_LEVEL;
8555 break;
8556 case 2:
8557 level = TP_EC_FAN_AUTO;
8558 break;
8559 case 3:
8560 /* reserved for software-controlled auto mode */
8561 return -ENOSYS;
8562 default:
8563 return -EINVAL;
8564 }
8565
8566 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008567 if (res == -ENXIO)
8568 return -EINVAL;
8569 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008570 return res;
8571
8572 fan_watchdog_reset();
8573
8574 return count;
8575}
8576
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008577static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8578 fan_pwm1_enable_show, fan_pwm1_enable_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008579
8580/* sysfs fan pwm1 ------------------------------------------------------ */
8581static ssize_t fan_pwm1_show(struct device *dev,
8582 struct device_attribute *attr,
8583 char *buf)
8584{
8585 int res;
8586 u8 status;
8587
8588 res = fan_get_status_safe(&status);
8589 if (res)
8590 return res;
8591
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008592 if ((status &
8593 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8594 status = fan_control_desired_level;
8595
8596 if (status > 7)
8597 status = 7;
8598
8599 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8600}
8601
8602static ssize_t fan_pwm1_store(struct device *dev,
8603 struct device_attribute *attr,
8604 const char *buf, size_t count)
8605{
8606 unsigned long s;
8607 int rc;
8608 u8 status, newlevel;
8609
8610 if (parse_strtoul(buf, 255, &s))
8611 return -EINVAL;
8612
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008613 tpacpi_disclose_usertask("hwmon pwm1",
8614 "set fan speed to %lu\n", s);
8615
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008616 /* scale down from 0-255 to 0-7 */
8617 newlevel = (s >> 5) & 0x07;
8618
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008619 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008620 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008621
8622 rc = fan_get_status(&status);
8623 if (!rc && (status &
8624 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8625 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008626 if (rc == -ENXIO)
8627 rc = -EINVAL;
8628 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008629 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008630 fan_watchdog_reset();
8631 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008632 }
8633
8634 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008635 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008636}
8637
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008638static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008639
8640/* sysfs fan fan1_input ------------------------------------------------ */
8641static ssize_t fan_fan1_input_show(struct device *dev,
8642 struct device_attribute *attr,
8643 char *buf)
8644{
8645 int res;
8646 unsigned int speed;
8647
8648 res = fan_get_speed(&speed);
8649 if (res < 0)
8650 return res;
8651
8652 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8653}
8654
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008655static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008656
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008657/* sysfs fan fan2_input ------------------------------------------------ */
8658static ssize_t fan_fan2_input_show(struct device *dev,
8659 struct device_attribute *attr,
8660 char *buf)
8661{
8662 int res;
8663 unsigned int speed;
8664
8665 res = fan2_get_speed(&speed);
8666 if (res < 0)
8667 return res;
8668
8669 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8670}
8671
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008672static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008673
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008674/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008675static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008676{
8677 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8678}
8679
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008680static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8681 size_t count)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008682{
8683 unsigned long t;
8684
8685 if (parse_strtoul(buf, 120, &t))
8686 return -EINVAL;
8687
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008688 if (!fan_control_allowed)
8689 return -EPERM;
8690
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008691 fan_watchdog_maxinterval = t;
8692 fan_watchdog_reset();
8693
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008694 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8695
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008696 return count;
8697}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008698static DRIVER_ATTR_RW(fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008699
8700/* --------------------------------------------------------------------- */
8701static struct attribute *fan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008702 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8703 &dev_attr_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008704 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008705 NULL
8706};
8707
8708static const struct attribute_group fan_attr_group = {
8709 .attrs = fan_attributes,
8710};
8711
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008712#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8713#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008714
8715#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8716 { .vendor = PCI_VENDOR_ID_IBM, \
8717 .bios = TPACPI_MATCH_ANY, \
8718 .ec = TPID(__id1, __id2), \
8719 .quirks = __quirks }
8720
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008721#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8722 { .vendor = PCI_VENDOR_ID_LENOVO, \
8723 .bios = TPACPI_MATCH_ANY, \
8724 .ec = TPID(__id1, __id2), \
8725 .quirks = __quirks }
8726
Alexander Kappnera986c752018-04-02 11:27:01 -07008727#define TPACPI_FAN_QB(__id1, __id2, __quirks) \
8728 { .vendor = PCI_VENDOR_ID_LENOVO, \
8729 .bios = TPID(__id1, __id2), \
8730 .ec = TPACPI_MATCH_ANY, \
8731 .quirks = __quirks }
8732
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008733static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8734 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8735 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8736 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8737 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008738 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Alexander Kappnera986c752018-04-02 11:27:01 -07008739 TPACPI_FAN_QB('N', '1', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008740};
8741
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008742#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008743#undef TPACPI_FAN_QI
Alexander Kappnera986c752018-04-02 11:27:01 -07008744#undef TPACPI_FAN_QB
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008745
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008746static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008747{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008748 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008749 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008750
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008751 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8752 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008753
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008754 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008755 fan_status_access_mode = TPACPI_FAN_NONE;
8756 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008757 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008758 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008759 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008760 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008761 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008762
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008763 if (tpacpi_is_ibm()) {
8764 TPACPI_ACPIHANDLE_INIT(fans);
8765 TPACPI_ACPIHANDLE_INIT(gfan);
8766 TPACPI_ACPIHANDLE_INIT(sfan);
8767 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008768
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008769 quirks = tpacpi_check_quirks(fan_quirk_table,
8770 ARRAY_SIZE(fan_quirk_table));
8771
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008772 if (gfan_handle) {
8773 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008774 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008775 } else {
8776 /* all other ThinkPads: note that even old-style
8777 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008778 if (likely(acpi_ec_read(fan_status_offset,
8779 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008780 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008781 if (quirks & TPACPI_FAN_Q1)
8782 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008783 if (quirks & TPACPI_FAN_2FAN) {
8784 tp_features.second_fan = 1;
8785 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8786 "secondary fan support enabled\n");
8787 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008788 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008789 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008790 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008791 }
8792 }
8793
8794 if (sfan_handle) {
8795 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008796 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008797 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008798 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008799 } else {
8800 if (!gfan_handle) {
8801 /* gfan without sfan means no fan control */
8802 /* all other models implement TP EC 0x2f control */
8803
8804 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008805 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008806 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008807 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008808 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008809 TPACPI_FAN_CMD_SPEED |
8810 TPACPI_FAN_CMD_LEVEL |
8811 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008812 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008813 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008814 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008815 TPACPI_FAN_CMD_LEVEL |
8816 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008817 }
8818 }
8819 }
8820
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008821 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8822 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008823 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8824 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8825 fan_status_access_mode, fan_control_access_mode);
8826
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008827 /* fan control master switch */
8828 if (!fan_control_allowed) {
8829 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8830 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008831 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008832 "fan control features disabled by parameter\n");
8833 }
8834
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008835 /* update fan_control_desired_level */
8836 if (fan_status_access_mode != TPACPI_FAN_NONE)
8837 fan_get_status_safe(NULL);
8838
8839 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8840 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008841 if (tp_features.second_fan) {
8842 /* attach second fan tachometer */
8843 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008844 &dev_attr_fan2_input.attr;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008845 }
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008846 rc = sysfs_create_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008847 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008848 if (rc < 0)
8849 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008850
8851 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8852 &driver_attr_fan_watchdog);
8853 if (rc < 0) {
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008854 sysfs_remove_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008855 &fan_attr_group);
8856 return rc;
8857 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008858 return 0;
8859 } else
8860 return 1;
8861}
8862
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008863static void fan_exit(void)
8864{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008865 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008866 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008867
8868 /* FIXME: can we really do this unconditionally? */
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008869 sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008870 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8871 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008872
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008873 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008874 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008875}
8876
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008877static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008878{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008879 int rc;
8880
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008881 if (!fan_control_allowed)
8882 return;
8883
8884 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008885 fan_control_resume_level = 0;
8886 rc = fan_get_status_safe(&fan_control_resume_level);
8887 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008888 pr_notice("failed to read fan level for later restore during resume: %d\n",
8889 rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008890
8891 /* if it is undefined, don't attempt to restore it.
8892 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008893 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008894 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008895}
8896
8897static void fan_resume(void)
8898{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008899 u8 current_level = 7;
8900 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008901 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008902
8903 /* DSDT *always* updates status on resume */
8904 tp_features.fan_ctrl_status_undef = 0;
8905
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008906 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008907 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008908 (fan_get_status_safe(&current_level) < 0))
8909 return;
8910
8911 switch (fan_control_access_mode) {
8912 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008913 /* never decrease fan level */
8914 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008915 break;
8916 case TPACPI_FAN_WR_ACPI_FANS:
8917 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008918 /* never decrease fan level, scale is:
8919 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8920 *
8921 * We expect the firmware to set either 7 or AUTO, but we
8922 * handle FULLSPEED out of paranoia.
8923 *
8924 * So, we can safely only restore FULLSPEED or 7, anything
8925 * else could slow the fan. Restoring AUTO is useless, at
8926 * best that's exactly what the DSDT already set (it is the
8927 * slower it uses).
8928 *
8929 * Always keep in mind that the DSDT *will* have set the
8930 * fans to what the vendor supposes is the best level. We
8931 * muck with it only to speed the fan up.
8932 */
8933 if (fan_control_resume_level != 7 &&
8934 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8935 return;
8936 else
8937 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8938 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008939 break;
8940 default:
8941 return;
8942 }
8943 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008944 pr_notice("restoring fan level to 0x%02x\n",
8945 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008946 rc = fan_set_level_safe(fan_control_resume_level);
8947 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008948 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008949 }
8950}
8951
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008952static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008953{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008954 int rc;
8955 u8 status;
8956 unsigned int speed = 0;
8957
8958 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008959 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008960 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008961 rc = fan_get_status_safe(&status);
8962 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008963 return rc;
8964
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008965 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008966 "level:\t\t%d\n",
8967 (status != 0) ? "enabled" : "disabled", status);
8968 break;
8969
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008970 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008971 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008972 rc = fan_get_status_safe(&status);
8973 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008974 return rc;
8975
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008976 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008977 (status != 0) ? "enabled" : "disabled");
8978
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008979 rc = fan_get_speed(&speed);
8980 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008981 return rc;
8982
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008983 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008984
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008985 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008986 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008987 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008988 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008989 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008990 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008991 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008992 break;
8993
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008994 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008995 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008996 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008997 }
8998
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008999 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009000 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009001
9002 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009003 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009004 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009005 break;
9006
9007 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009008 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009009 break;
9010 }
9011 }
9012
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009013 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009014 seq_printf(m, "commands:\tenable, disable\n"
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009015 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009016
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009017 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009018 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009019
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009020 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009021}
9022
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009023static int fan_write_cmd_level(const char *cmd, int *rc)
9024{
9025 int level;
9026
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02009027 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009028 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03009029 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009030 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009031 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02009032 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009033 return 0;
9034
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009035 *rc = fan_set_level_safe(level);
9036 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009037 pr_err("level command accepted for unsupported access mode %d\n",
9038 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009039 else if (!*rc)
9040 tpacpi_disclose_usertask("procfs fan",
9041 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009042
9043 return 1;
9044}
9045
9046static int fan_write_cmd_enable(const char *cmd, int *rc)
9047{
9048 if (strlencmp(cmd, "enable") != 0)
9049 return 0;
9050
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009051 *rc = fan_set_enable();
9052 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009053 pr_err("enable command accepted for unsupported access mode %d\n",
9054 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009055 else if (!*rc)
9056 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009057
9058 return 1;
9059}
9060
9061static int fan_write_cmd_disable(const char *cmd, int *rc)
9062{
9063 if (strlencmp(cmd, "disable") != 0)
9064 return 0;
9065
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009066 *rc = fan_set_disable();
9067 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009068 pr_err("disable command accepted for unsupported access mode %d\n",
9069 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009070 else if (!*rc)
9071 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009072
9073 return 1;
9074}
9075
9076static int fan_write_cmd_speed(const char *cmd, int *rc)
9077{
9078 int speed;
9079
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02009080 /* TODO:
9081 * Support speed <low> <medium> <high> ? */
9082
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009083 if (sscanf(cmd, "speed %d", &speed) != 1)
9084 return 0;
9085
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009086 *rc = fan_set_speed(speed);
9087 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009088 pr_err("speed command accepted for unsupported access mode %d\n",
9089 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009090 else if (!*rc)
9091 tpacpi_disclose_usertask("procfs fan",
9092 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009093
9094 return 1;
9095}
9096
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009097static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9098{
9099 int interval;
9100
9101 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9102 return 0;
9103
9104 if (interval < 0 || interval > 120)
9105 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009106 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009107 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009108 tpacpi_disclose_usertask("procfs fan",
9109 "set watchdog timer to %d\n",
9110 interval);
9111 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009112
9113 return 1;
9114}
9115
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009116static int fan_write(char *buf)
9117{
9118 char *cmd;
9119 int rc = 0;
9120
9121 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009122 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009123 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009124 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009125 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009126 fan_write_cmd_disable(cmd, &rc) ||
9127 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009128 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009129 fan_write_cmd_speed(cmd, &rc))
9130 )
9131 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009132 else if (!rc)
9133 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009134 }
9135
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009136 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009137}
9138
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009139static struct ibm_struct fan_driver_data = {
9140 .name = "fan",
9141 .read = fan_read,
9142 .write = fan_write,
9143 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03009144 .suspend = fan_suspend,
9145 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009146};
9147
David Henningsson420f9732013-10-16 23:10:31 +02009148/*************************************************************************
9149 * Mute LED subdriver
9150 */
9151
9152
9153struct tp_led_table {
9154 acpi_string name;
9155 int on_value;
9156 int off_value;
9157 int state;
9158};
9159
9160static struct tp_led_table led_tables[] = {
9161 [TPACPI_LED_MUTE] = {
9162 .name = "SSMS",
9163 .on_value = 1,
9164 .off_value = 0,
9165 },
9166 [TPACPI_LED_MICMUTE] = {
9167 .name = "MMTS",
9168 .on_value = 2,
9169 .off_value = 0,
9170 },
9171};
9172
9173static int mute_led_on_off(struct tp_led_table *t, bool state)
9174{
9175 acpi_handle temp;
9176 int output;
9177
Axel Lin3526eca2016-09-19 09:33:51 +08009178 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009179 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9180 return -EIO;
9181 }
9182
9183 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9184 state ? t->on_value : t->off_value))
9185 return -EIO;
9186
9187 t->state = state;
9188 return state;
9189}
9190
9191int tpacpi_led_set(int whichled, bool on)
9192{
9193 struct tp_led_table *t;
9194
9195 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9196 return -EINVAL;
9197
9198 t = &led_tables[whichled];
9199 if (t->state < 0 || t->state == on)
9200 return t->state;
9201 return mute_led_on_off(t, on);
9202}
9203EXPORT_SYMBOL_GPL(tpacpi_led_set);
9204
9205static int mute_led_init(struct ibm_init_struct *iibm)
9206{
9207 acpi_handle temp;
9208 int i;
9209
9210 for (i = 0; i < TPACPI_LED_MAX; i++) {
9211 struct tp_led_table *t = &led_tables[i];
9212 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9213 mute_led_on_off(t, false);
9214 else
9215 t->state = -ENODEV;
9216 }
9217 return 0;
9218}
9219
9220static void mute_led_exit(void)
9221{
9222 int i;
9223
9224 for (i = 0; i < TPACPI_LED_MAX; i++)
9225 tpacpi_led_set(i, false);
9226}
9227
Takashi Iwai119f4492014-02-12 16:32:45 +01009228static void mute_led_resume(void)
9229{
9230 int i;
9231
9232 for (i = 0; i < TPACPI_LED_MAX; i++) {
9233 struct tp_led_table *t = &led_tables[i];
9234 if (t->state >= 0)
9235 mute_led_on_off(t, t->state);
9236 }
9237}
9238
David Henningsson420f9732013-10-16 23:10:31 +02009239static struct ibm_struct mute_led_driver_data = {
9240 .name = "mute_led",
9241 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01009242 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02009243};
9244
Ognjen Galic2801b962018-02-07 15:58:44 +01009245/*
9246 * Battery Wear Control Driver
9247 * Contact: Ognjen Galic <smclt30p@gmail.com>
9248 */
9249
9250/* Metadata */
9251
9252#define GET_START "BCTG"
9253#define SET_START "BCCS"
9254#define GET_STOP "BCSG"
9255#define SET_STOP "BCSS"
9256
9257#define START_ATTR "charge_start_threshold"
9258#define STOP_ATTR "charge_stop_threshold"
9259
9260enum {
9261 BAT_ANY = 0,
9262 BAT_PRIMARY = 1,
9263 BAT_SECONDARY = 2
9264};
9265
9266enum {
9267 /* Error condition bit */
9268 METHOD_ERR = BIT(31),
9269};
9270
9271enum {
9272 /* This is used in the get/set helpers */
9273 THRESHOLD_START,
9274 THRESHOLD_STOP,
9275};
9276
9277struct tpacpi_battery_data {
9278 int charge_start;
9279 int start_support;
9280 int charge_stop;
9281 int stop_support;
9282};
9283
9284struct tpacpi_battery_driver_data {
9285 struct tpacpi_battery_data batteries[3];
9286 int individual_addressing;
9287};
9288
9289static struct tpacpi_battery_driver_data battery_info;
9290
9291/* ACPI helpers/functions/probes */
9292
9293/**
9294 * This evaluates a ACPI method call specific to the battery
9295 * ACPI extension. The specifics are that an error is marked
9296 * in the 32rd bit of the response, so we just check that here.
9297 */
9298static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9299{
9300 int response;
9301
9302 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9303 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9304 return AE_ERROR;
9305 }
9306 if (response & METHOD_ERR) {
9307 acpi_handle_err(hkey_handle,
9308 "%s evaluated but flagged as error", method);
9309 return AE_ERROR;
9310 }
9311 *ret = response;
9312 return AE_OK;
9313}
9314
9315static int tpacpi_battery_get(int what, int battery, int *ret)
9316{
9317 switch (what) {
9318 case THRESHOLD_START:
9319 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9320 return -ENODEV;
9321
9322 /* The value is in the low 8 bits of the response */
9323 *ret = *ret & 0xFF;
9324 return 0;
9325 case THRESHOLD_STOP:
9326 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9327 return -ENODEV;
9328 /* Value is in lower 8 bits */
9329 *ret = *ret & 0xFF;
9330 /*
9331 * On the stop value, if we return 0 that
9332 * does not make any sense. 0 means Default, which
9333 * means that charging stops at 100%, so we return
9334 * that.
9335 */
9336 if (*ret == 0)
9337 *ret = 100;
9338 return 0;
9339 default:
9340 pr_crit("wrong parameter: %d", what);
9341 return -EINVAL;
9342 }
9343}
9344
9345static int tpacpi_battery_set(int what, int battery, int value)
9346{
9347 int param, ret;
9348 /* The first 8 bits are the value of the threshold */
9349 param = value;
9350 /* The battery ID is in bits 8-9, 2 bits */
9351 param |= battery << 8;
9352
9353 switch (what) {
9354 case THRESHOLD_START:
9355 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9356 pr_err("failed to set charge threshold on battery %d",
9357 battery);
9358 return -ENODEV;
9359 }
9360 return 0;
9361 case THRESHOLD_STOP:
9362 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9363 pr_err("failed to set stop threshold: %d", battery);
9364 return -ENODEV;
9365 }
9366 return 0;
9367 default:
9368 pr_crit("wrong parameter: %d", what);
9369 return -EINVAL;
9370 }
9371}
9372
9373static int tpacpi_battery_probe(int battery)
9374{
9375 int ret = 0;
9376
Thomas Weißschuhd22296d2018-08-02 00:24:18 +02009377 memset(&battery_info.batteries[battery], 0,
9378 sizeof(battery_info.batteries[battery]));
9379
Ognjen Galic2801b962018-02-07 15:58:44 +01009380 /*
9381 * 1) Get the current start threshold
9382 * 2) Check for support
9383 * 3) Get the current stop threshold
9384 * 4) Check for support
9385 */
9386 if (acpi_has_method(hkey_handle, GET_START)) {
9387 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9388 pr_err("Error probing battery %d\n", battery);
9389 return -ENODEV;
9390 }
9391 /* Individual addressing is in bit 9 */
9392 if (ret & BIT(9))
9393 battery_info.individual_addressing = true;
9394 /* Support is marked in bit 8 */
9395 if (ret & BIT(8))
9396 battery_info.batteries[battery].start_support = 1;
9397 else
9398 return -ENODEV;
9399 if (tpacpi_battery_get(THRESHOLD_START, battery,
9400 &battery_info.batteries[battery].charge_start)) {
9401 pr_err("Error probing battery %d\n", battery);
9402 return -ENODEV;
9403 }
9404 }
9405 if (acpi_has_method(hkey_handle, GET_STOP)) {
9406 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9407 pr_err("Error probing battery stop; %d\n", battery);
9408 return -ENODEV;
9409 }
9410 /* Support is marked in bit 8 */
9411 if (ret & BIT(8))
9412 battery_info.batteries[battery].stop_support = 1;
9413 else
9414 return -ENODEV;
9415 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9416 &battery_info.batteries[battery].charge_stop)) {
9417 pr_err("Error probing battery stop: %d\n", battery);
9418 return -ENODEV;
9419 }
9420 }
9421 pr_info("battery %d registered (start %d, stop %d)",
9422 battery,
9423 battery_info.batteries[battery].charge_start,
9424 battery_info.batteries[battery].charge_stop);
9425
9426 return 0;
9427}
9428
9429/* General helper functions */
9430
9431static int tpacpi_battery_get_id(const char *battery_name)
9432{
9433
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009434 if (strcmp(battery_name, "BAT0") == 0 ||
9435 tp_features.battery_force_primary)
Ognjen Galic2801b962018-02-07 15:58:44 +01009436 return BAT_PRIMARY;
9437 if (strcmp(battery_name, "BAT1") == 0)
9438 return BAT_SECONDARY;
9439 /*
9440 * If for some reason the battery is not BAT0 nor is it
9441 * BAT1, we will assume it's the default, first battery,
9442 * AKA primary.
9443 */
9444 pr_warn("unknown battery %s, assuming primary", battery_name);
9445 return BAT_PRIMARY;
9446}
9447
9448/* sysfs interface */
9449
9450static ssize_t tpacpi_battery_store(int what,
9451 struct device *dev,
9452 const char *buf, size_t count)
9453{
9454 struct power_supply *supply = to_power_supply(dev);
9455 unsigned long value;
9456 int battery, rval;
9457 /*
9458 * Some systems have support for more than
9459 * one battery. If that is the case,
9460 * tpacpi_battery_probe marked that addressing
9461 * them individually is supported, so we do that
9462 * based on the device struct.
9463 *
9464 * On systems that are not supported, we assume
9465 * the primary as most of the ACPI calls fail
9466 * with "Any Battery" as the parameter.
9467 */
9468 if (battery_info.individual_addressing)
9469 /* BAT_PRIMARY or BAT_SECONDARY */
9470 battery = tpacpi_battery_get_id(supply->desc->name);
9471 else
9472 battery = BAT_PRIMARY;
9473
9474 rval = kstrtoul(buf, 10, &value);
9475 if (rval)
9476 return rval;
9477
9478 switch (what) {
9479 case THRESHOLD_START:
9480 if (!battery_info.batteries[battery].start_support)
9481 return -ENODEV;
9482 /* valid values are [0, 99] */
9483 if (value < 0 || value > 99)
9484 return -EINVAL;
9485 if (value > battery_info.batteries[battery].charge_stop)
9486 return -EINVAL;
9487 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9488 return -ENODEV;
9489 battery_info.batteries[battery].charge_start = value;
9490 return count;
9491
9492 case THRESHOLD_STOP:
9493 if (!battery_info.batteries[battery].stop_support)
9494 return -ENODEV;
9495 /* valid values are [1, 100] */
9496 if (value < 1 || value > 100)
9497 return -EINVAL;
9498 if (value < battery_info.batteries[battery].charge_start)
9499 return -EINVAL;
9500 battery_info.batteries[battery].charge_stop = value;
9501 /*
9502 * When 100 is passed to stop, we need to flip
9503 * it to 0 as that the EC understands that as
9504 * "Default", which will charge to 100%
9505 */
9506 if (value == 100)
9507 value = 0;
9508 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9509 return -EINVAL;
9510 return count;
9511 default:
9512 pr_crit("Wrong parameter: %d", what);
9513 return -EINVAL;
9514 }
9515 return count;
9516}
9517
9518static ssize_t tpacpi_battery_show(int what,
9519 struct device *dev,
9520 char *buf)
9521{
9522 struct power_supply *supply = to_power_supply(dev);
9523 int ret, battery;
9524 /*
9525 * Some systems have support for more than
9526 * one battery. If that is the case,
9527 * tpacpi_battery_probe marked that addressing
9528 * them individually is supported, so we;
9529 * based on the device struct.
9530 *
9531 * On systems that are not supported, we assume
9532 * the primary as most of the ACPI calls fail
9533 * with "Any Battery" as the parameter.
9534 */
9535 if (battery_info.individual_addressing)
9536 /* BAT_PRIMARY or BAT_SECONDARY */
9537 battery = tpacpi_battery_get_id(supply->desc->name);
9538 else
9539 battery = BAT_PRIMARY;
9540 if (tpacpi_battery_get(what, battery, &ret))
9541 return -ENODEV;
9542 return sprintf(buf, "%d\n", ret);
9543}
9544
9545static ssize_t charge_start_threshold_show(struct device *device,
9546 struct device_attribute *attr,
9547 char *buf)
9548{
9549 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9550}
9551
9552static ssize_t charge_stop_threshold_show(struct device *device,
9553 struct device_attribute *attr,
9554 char *buf)
9555{
9556 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9557}
9558
9559static ssize_t charge_start_threshold_store(struct device *dev,
9560 struct device_attribute *attr,
9561 const char *buf, size_t count)
9562{
9563 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9564}
9565
9566static ssize_t charge_stop_threshold_store(struct device *dev,
9567 struct device_attribute *attr,
9568 const char *buf, size_t count)
9569{
9570 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9571}
9572
9573static DEVICE_ATTR_RW(charge_start_threshold);
9574static DEVICE_ATTR_RW(charge_stop_threshold);
9575
9576static struct attribute *tpacpi_battery_attrs[] = {
9577 &dev_attr_charge_start_threshold.attr,
9578 &dev_attr_charge_stop_threshold.attr,
9579 NULL,
9580};
9581
9582ATTRIBUTE_GROUPS(tpacpi_battery);
9583
9584/* ACPI battery hooking */
9585
9586static int tpacpi_battery_add(struct power_supply *battery)
9587{
9588 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9589
9590 if (tpacpi_battery_probe(batteryid))
9591 return -ENODEV;
9592 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9593 return -ENODEV;
9594 return 0;
9595}
9596
9597static int tpacpi_battery_remove(struct power_supply *battery)
9598{
9599 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9600 return 0;
9601}
9602
9603static struct acpi_battery_hook battery_hook = {
9604 .add_battery = tpacpi_battery_add,
9605 .remove_battery = tpacpi_battery_remove,
9606 .name = "ThinkPad Battery Extension",
9607};
9608
9609/* Subdriver init/exit */
9610
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009611static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9612 /*
9613 * Individual addressing is broken on models that expose the
9614 * primary battery as BAT1.
9615 */
Jouke Witteveen6640ee62018-08-02 22:28:49 +02009616 TPACPI_Q_LNV('J', '7', true), /* B5400 */
9617 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
9618 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009619 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
Jouke Witteveen6640ee62018-08-02 22:28:49 +02009620 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009621};
9622
Ognjen Galic2801b962018-02-07 15:58:44 +01009623static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9624{
Thomas Weißschuhd22296d2018-08-02 00:24:18 +02009625 memset(&battery_info, 0, sizeof(battery_info));
9626
Jouke Witteveen1a32ebb2018-07-11 11:45:36 +02009627 tp_features.battery_force_primary = tpacpi_check_quirks(
9628 battery_quirk_table,
9629 ARRAY_SIZE(battery_quirk_table));
9630
Ognjen Galic2801b962018-02-07 15:58:44 +01009631 battery_hook_register(&battery_hook);
9632 return 0;
9633}
9634
9635static void tpacpi_battery_exit(void)
9636{
9637 battery_hook_unregister(&battery_hook);
9638}
9639
9640static struct ibm_struct battery_driver_data = {
9641 .name = "battery",
9642 .exit = tpacpi_battery_exit,
9643};
9644
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009645/****************************************************************************
9646 ****************************************************************************
9647 *
9648 * Infrastructure
9649 *
9650 ****************************************************************************
9651 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07009652
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009653/*
9654 * HKEY event callout for other subdrivers go here
9655 * (yes, it is ugly, but it is quick, safe, and gets the job done
9656 */
9657static void tpacpi_driver_event(const unsigned int hkey_event)
9658{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00009659 if (ibm_backlight_device) {
9660 switch (hkey_event) {
9661 case TP_HKEY_EV_BRGHT_UP:
9662 case TP_HKEY_EV_BRGHT_DOWN:
9663 tpacpi_brightness_notify_change();
9664 }
9665 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009666 if (alsa_card) {
9667 switch (hkey_event) {
9668 case TP_HKEY_EV_VOL_UP:
9669 case TP_HKEY_EV_VOL_DOWN:
9670 case TP_HKEY_EV_VOL_MUTE:
9671 volume_alsa_notify_change();
9672 }
9673 }
Hans de Goedec685e202017-02-09 16:44:13 +01009674 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9675 enum led_brightness brightness;
9676
9677 mutex_lock(&kbdlight_mutex);
9678
9679 /*
9680 * Check the brightness actually changed, setting the brightness
9681 * through kbdlight_set_level() also triggers this event.
9682 */
9683 brightness = kbdlight_sysfs_get(NULL);
9684 if (kbdlight_brightness != brightness) {
9685 kbdlight_brightness = brightness;
9686 led_classdev_notify_brightness_hw_changed(
9687 &tpacpi_led_kbdlight.led_classdev, brightness);
9688 }
9689
9690 mutex_unlock(&kbdlight_mutex);
9691 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009692}
9693
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009694static void hotkey_driver_event(const unsigned int scancode)
9695{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03009696 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009697}
9698
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009699/* --------------------------------------------------------------------- */
9700
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009701/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03009702static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009703
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009704/*
9705 * Module and infrastructure proble, init and exit handling
9706 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009707
Rusty Russell90ab5ee2012-01-13 09:32:20 +10309708static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009709
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009710#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009711static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009712{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009713 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009714
Jan van den Berg72a979f2014-09-17 00:01:08 +02009715 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009716}
9717#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9718
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009719static void ibm_exit(struct ibm_struct *ibm)
9720{
9721 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9722
9723 list_del_init(&ibm->all_drivers);
9724
9725 if (ibm->flags.acpi_notify_installed) {
9726 dbg_printk(TPACPI_DBG_EXIT,
9727 "%s: acpi_remove_notify_handler\n", ibm->name);
9728 BUG_ON(!ibm->acpi);
9729 acpi_remove_notify_handler(*ibm->acpi->handle,
9730 ibm->acpi->type,
9731 dispatch_acpi_notify);
9732 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009733 }
9734
9735 if (ibm->flags.proc_created) {
9736 dbg_printk(TPACPI_DBG_EXIT,
9737 "%s: remove_proc_entry\n", ibm->name);
9738 remove_proc_entry(ibm->name, proc_dir);
9739 ibm->flags.proc_created = 0;
9740 }
9741
9742 if (ibm->flags.acpi_driver_registered) {
9743 dbg_printk(TPACPI_DBG_EXIT,
9744 "%s: acpi_bus_unregister_driver\n", ibm->name);
9745 BUG_ON(!ibm->acpi);
9746 acpi_bus_unregister_driver(ibm->acpi->driver);
9747 kfree(ibm->acpi->driver);
9748 ibm->acpi->driver = NULL;
9749 ibm->flags.acpi_driver_registered = 0;
9750 }
9751
9752 if (ibm->flags.init_called && ibm->exit) {
9753 ibm->exit();
9754 ibm->flags.init_called = 0;
9755 }
9756
9757 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9758}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009759
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009760static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009761{
9762 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009763 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764 struct proc_dir_entry *entry;
9765
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009766 BUG_ON(ibm == NULL);
9767
9768 INIT_LIST_HEAD(&ibm->all_drivers);
9769
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009770 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009771 return 0;
9772
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009773 dbg_printk(TPACPI_DBG_INIT,
9774 "probing for %s\n", ibm->name);
9775
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009776 if (iibm->init) {
9777 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009778 if (ret > 0)
9779 return 0; /* probe failed */
9780 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009781 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009782
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009783 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784 }
9785
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009786 if (ibm->acpi) {
9787 if (ibm->acpi->hid) {
9788 ret = register_tpacpi_subdriver(ibm);
9789 if (ret)
9790 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009791 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009792
9793 if (ibm->acpi->notify) {
9794 ret = setup_acpi_notify(ibm);
9795 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07009796 pr_notice("disabling subdriver %s\n",
9797 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009798 ret = 0;
9799 goto err_out;
9800 }
9801 if (ret < 0)
9802 goto err_out;
9803 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009804 }
9805
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009806 dbg_printk(TPACPI_DBG_INIT,
9807 "%s installed\n", ibm->name);
9808
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009809 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04009810 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009811
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009812 if (!mode)
9813 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009814 if (ibm->write)
9815 mode |= S_IWUSR;
9816 entry = proc_create_data(ibm->name, mode, proc_dir,
9817 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009818 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07009819 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009820 ret = -ENODEV;
9821 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009822 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009823 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009825
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009826 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9827
Linus Torvalds1da177e2005-04-16 15:20:36 -07009828 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009829
9830err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009831 dbg_printk(TPACPI_DBG_INIT,
9832 "%s: at error exit path with result %d\n",
9833 ibm->name, ret);
9834
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009835 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +02009836 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837}
9838
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009839/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840
Jouke Witteveen846a4162018-07-11 11:44:41 +02009841static char __init tpacpi_parse_fw_id(const char * const s,
9842 u32 *model, u16 *release)
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009843{
Jouke Witteveen846a4162018-07-11 11:44:41 +02009844 int i;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009845
Jouke Witteveen846a4162018-07-11 11:44:41 +02009846 if (!s || strlen(s) < 8)
9847 goto invalid;
9848
9849 for (i = 0; i < 8; i++)
9850 if (!((s[i] >= '0' && s[i] <= '9') ||
9851 (s[i] >= 'A' && s[i] <= 'Z')))
9852 goto invalid;
9853
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009854 /*
9855 * Most models: xxyTkkWW (#.##c)
9856 * Ancient 570/600 and -SL lacks (#.##c)
9857 */
Jouke Witteveen846a4162018-07-11 11:44:41 +02009858 if (s[3] == 'T' || s[3] == 'N') {
9859 *model = TPID(s[0], s[1]);
9860 *release = TPVER(s[4], s[5]);
9861 return s[2];
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009862
9863 /* New models: xxxyTkkW (#.##c); T550 and some others */
Jouke Witteveen846a4162018-07-11 11:44:41 +02009864 } else if (s[4] == 'T' || s[4] == 'N') {
9865 *model = TPID3(s[0], s[1], s[2]);
9866 *release = TPVER(s[5], s[6]);
9867 return s[3];
9868 }
9869
9870invalid:
9871 return '\0';
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009872}
9873
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009874/* returns 0 - probe ok, or < 0 - probe error.
9875 * Probe ok doesn't mean thinkpad found.
9876 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9877static int __must_check __init get_thinkpad_model_data(
9878 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009879{
Jeff Garzik18552562007-10-03 15:15:40 -04009880 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009881 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009882 char const *s;
Jouke Witteveen846a4162018-07-11 11:44:41 +02009883 char t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009884
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009885 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009886 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009887
9888 memset(tp, 0, sizeof(*tp));
9889
9890 if (dmi_name_in_vendors("IBM"))
9891 tp->vendor = PCI_VENDOR_ID_IBM;
9892 else if (dmi_name_in_vendors("LENOVO"))
9893 tp->vendor = PCI_VENDOR_ID_LENOVO;
9894 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009895 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009896
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009897 s = dmi_get_system_info(DMI_BIOS_VERSION);
9898 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9899 if (s && !tp->bios_version_str)
9900 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009901
9902 /* Really ancient ThinkPad 240X will fail this, which is fine */
Jouke Witteveen846a4162018-07-11 11:44:41 +02009903 t = tpacpi_parse_fw_id(tp->bios_version_str,
9904 &tp->bios_model, &tp->bios_release);
9905 if (t != 'E' && t != 'C')
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009906 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009907
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009908 /*
9909 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9910 * X32 or newer, all Z series; Some models must have an
9911 * up-to-date BIOS or they will not be detected.
9912 *
9913 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9914 */
9915 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009916 if (sscanf(dev->name,
9917 "IBM ThinkPad Embedded Controller -[%17c",
9918 ec_fw_string) == 1) {
9919 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9920 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009921
9922 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009923 if (!tp->ec_version_str)
9924 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009925
Jouke Witteveen846a4162018-07-11 11:44:41 +02009926 t = tpacpi_parse_fw_id(ec_fw_string,
9927 &tp->ec_model, &tp->ec_release);
9928 if (t != 'H') {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009929 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
Joe Perches0978e012011-04-04 10:06:25 -07009930 ec_fw_string);
9931 pr_notice("please report this to %s\n",
9932 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009933 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009934 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009935 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009936 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009937
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009938 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009939 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009940 tp->model_str = kstrdup(s, GFP_KERNEL);
9941 if (!tp->model_str)
9942 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009943 } else {
9944 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009945 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009946 tp->model_str = kstrdup(s, GFP_KERNEL);
9947 if (!tp->model_str)
9948 return -ENOMEM;
9949 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009950 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03009951
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009952 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9953 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9954 if (s && !tp->nummodel_str)
9955 return -ENOMEM;
9956
9957 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009958}
9959
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009960static int __init probe_for_thinkpad(void)
9961{
9962 int is_thinkpad;
9963
9964 if (acpi_disabled)
9965 return -ENODEV;
9966
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009967 /* It would be dangerous to run the driver in this case */
9968 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9969 return -ENODEV;
9970
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009971 /*
9972 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009973 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009974 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009975 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9976 (thinkpad_id.ec_model != 0) ||
9977 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009978
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03009979 /* The EC handler is required */
9980 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009981 if (!ec_handle) {
9982 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07009983 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009984 return -ENODEV;
9985 }
9986
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009987 if (!is_thinkpad && !force_load)
9988 return -ENODEV;
9989
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009990 return 0;
9991}
9992
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009993static void __init thinkpad_acpi_init_banner(void)
9994{
Joe Perches0978e012011-04-04 10:06:25 -07009995 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9996 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009997
Joe Perches0978e012011-04-04 10:06:25 -07009998 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009999 (thinkpad_id.bios_version_str) ?
10000 thinkpad_id.bios_version_str : "unknown",
10001 (thinkpad_id.ec_version_str) ?
10002 thinkpad_id.ec_version_str : "unknown");
10003
10004 BUG_ON(!thinkpad_id.vendor);
10005
10006 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -070010007 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030010008 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
10009 "IBM" : ((thinkpad_id.vendor ==
10010 PCI_VENDOR_ID_LENOVO) ?
10011 "Lenovo" : "Unknown vendor"),
10012 thinkpad_id.model_str,
10013 (thinkpad_id.nummodel_str) ?
10014 thinkpad_id.nummodel_str : "unknown");
10015}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010016
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010017/* Module init, exit, parameters */
10018
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010019static struct ibm_init_struct ibms_init[] __initdata = {
10020 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010021 .data = &thinkpad_acpi_driver_data,
10022 },
10023 {
10024 .init = hotkey_init,
10025 .data = &hotkey_driver_data,
10026 },
10027 {
10028 .init = bluetooth_init,
10029 .data = &bluetooth_driver_data,
10030 },
10031 {
10032 .init = wan_init,
10033 .data = &wan_driver_data,
10034 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020010035 {
10036 .init = uwb_init,
10037 .data = &uwb_driver_data,
10038 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -020010039#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010040 {
10041 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -030010042 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010043 .data = &video_driver_data,
10044 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -020010045#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010046 {
Pali Rohárbb28f3d52015-12-30 23:27:41 +010010047 .init = kbdlight_init,
10048 .data = &kbdlight_driver_data,
10049 },
10050 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010051 .init = light_init,
10052 .data = &light_driver_data,
10053 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010054 {
10055 .init = cmos_init,
10056 .data = &cmos_driver_data,
10057 },
10058 {
10059 .init = led_init,
10060 .data = &led_driver_data,
10061 },
10062 {
10063 .init = beep_init,
10064 .data = &beep_driver_data,
10065 },
10066 {
10067 .init = thermal_init,
10068 .data = &thermal_driver_data,
10069 },
10070 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010071 .init = brightness_init,
10072 .data = &brightness_driver_data,
10073 },
10074 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020010075 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010076 .data = &volume_driver_data,
10077 },
10078 {
10079 .init = fan_init,
10080 .data = &fan_driver_data,
10081 },
David Henningsson420f9732013-10-16 23:10:31 +020010082 {
10083 .init = mute_led_init,
10084 .data = &mute_led_driver_data,
10085 },
Ognjen Galic2801b962018-02-07 15:58:44 +010010086 {
10087 .init = tpacpi_battery_init,
10088 .data = &battery_driver_data,
10089 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010090};
10091
Kees Cooke4dca7b2017-10-17 19:04:42 -070010092static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010093{
10094 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010095 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010096
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -020010097 if (!kp || !kp->name || !val)
10098 return -EINVAL;
10099
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010100 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10101 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -020010102 WARN_ON(ibm == NULL);
10103
10104 if (!ibm || !ibm->name)
10105 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010106
10107 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
10108 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010109 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010110 strcpy(ibms_init[i].param, val);
10111 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010112 return 0;
10113 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010114 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010115
10116 return -EINVAL;
10117}
10118
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +000010119module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010120MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010121 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010122
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -030010123module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010124MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -030010125
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +000010126module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010127MODULE_PARM_DESC(force_load,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010128 "Attempts to load the driver even on a mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -030010129
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +000010130module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010131MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010132 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -030010133
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +000010134module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010135MODULE_PARM_DESC(brightness_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010136 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -030010137
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +000010138module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010139MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010140 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -020010141
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -020010142#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020010143module_param_named(volume_mode, volume_mode, uint, 0444);
10144MODULE_PARM_DESC(volume_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010145 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020010146
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020010147module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
10148MODULE_PARM_DESC(volume_capabilities,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010149 "Selects the mixer capabilites: 0=auto, 1=volume and mute, 2=mute only");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020010150
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -020010151module_param_named(volume_control, volume_control_allowed, bool, 0444);
10152MODULE_PARM_DESC(volume_control,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010153 "Enables software override for the console audio control when true");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -020010154
Andy Lutomirski9a417ec2014-10-17 17:04:29 -070010155module_param_named(software_mute, software_mute_requested, bool, 0444);
10156MODULE_PARM_DESC(software_mute,
10157 "Request full software mute control");
10158
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020010159/* ALSA module API parameters */
10160module_param_named(index, alsa_index, int, 0444);
10161MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
10162module_param_named(id, alsa_id, charp, 0444);
10163MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
10164module_param_named(enable, alsa_enable, bool, 0444);
10165MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -020010166#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020010167
Andy Shevchenko9b4bbbd2017-05-09 17:17:21 +030010168/* The module parameter can't be read back, that's why 0 is used here */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010169#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010170 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010171 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010172
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010173TPACPI_PARAM(hotkey);
10174TPACPI_PARAM(bluetooth);
10175TPACPI_PARAM(video);
10176TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010177TPACPI_PARAM(cmos);
10178TPACPI_PARAM(led);
10179TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010180TPACPI_PARAM(brightness);
10181TPACPI_PARAM(volume);
10182TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010183
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010184#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +000010185module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010186MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
10187module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
10188MODULE_PARM_DESC(wlsw_state,
10189 "Initial state of the emulated WLSW switch");
10190
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +000010191module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010192MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
10193module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
10194MODULE_PARM_DESC(bluetooth_state,
10195 "Initial state of the emulated bluetooth switch");
10196
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +000010197module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010198MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
10199module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
10200MODULE_PARM_DESC(wwan_state,
10201 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020010202
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +000010203module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020010204MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
10205module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
10206MODULE_PARM_DESC(uwb_state,
10207 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010208#endif
10209
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010210static void thinkpad_acpi_module_exit(void)
10211{
10212 struct ibm_struct *ibm, *itmp;
10213
10214 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
10215
10216 list_for_each_entry_safe_reverse(ibm, itmp,
10217 &tpacpi_all_drivers,
10218 all_drivers) {
10219 ibm_exit(ibm);
10220 }
10221
10222 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
10223
10224 if (tpacpi_inputdev) {
10225 if (tp_features.input_device_registered)
10226 input_unregister_device(tpacpi_inputdev);
10227 else
10228 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +100010229 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010230 }
10231
10232 if (tpacpi_hwmon)
10233 hwmon_device_unregister(tpacpi_hwmon);
10234
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010235 if (tpacpi_sensors_pdev)
10236 platform_device_unregister(tpacpi_sensors_pdev);
10237 if (tpacpi_pdev)
10238 platform_device_unregister(tpacpi_pdev);
10239
10240 if (tp_features.sensors_pdrv_attrs_registered)
10241 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
10242 if (tp_features.platform_drv_attrs_registered)
10243 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
10244
10245 if (tp_features.sensors_pdrv_registered)
10246 platform_driver_unregister(&tpacpi_hwmon_pdriver);
10247
10248 if (tp_features.platform_drv_registered)
10249 platform_driver_unregister(&tpacpi_pdriver);
10250
10251 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010252 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010253
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -030010254 if (tpacpi_wq)
10255 destroy_workqueue(tpacpi_wq);
10256
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010257 kfree(thinkpad_id.bios_version_str);
10258 kfree(thinkpad_id.ec_version_str);
10259 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +100010260 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010261}
10262
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -020010263
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010264static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010265{
10266 int ret, i;
10267
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -030010268 tpacpi_lifecycle = TPACPI_LIFE_INIT;
10269
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010270 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010271
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010272 ret = get_thinkpad_model_data(&thinkpad_id);
10273 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010274 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010275 thinkpad_acpi_module_exit();
10276 return ret;
10277 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010278 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010279 if (ret) {
10280 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010281 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010283
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010284 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010285
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030010286 thinkpad_acpi_init_banner();
10287 tpacpi_check_outdated_fw();
10288
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010289 TPACPI_ACPIHANDLE_INIT(ecrd);
10290 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010291
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -030010292 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
10293 if (!tpacpi_wq) {
10294 thinkpad_acpi_module_exit();
10295 return -ENOMEM;
10296 }
10297
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010298 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010299 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -070010300 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010301 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010302 return -ENODEV;
10303 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -040010304
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010305 ret = platform_driver_register(&tpacpi_pdriver);
10306 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010307 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010308 thinkpad_acpi_module_exit();
10309 return ret;
10310 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -030010311 tp_features.platform_drv_registered = 1;
10312
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010313 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
10314 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010315 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010316 thinkpad_acpi_module_exit();
10317 return ret;
10318 }
10319 tp_features.sensors_pdrv_registered = 1;
10320
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010321 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -030010322 if (!ret) {
10323 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010324 ret = tpacpi_create_driver_attributes(
10325 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -030010326 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010327 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010328 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010329 thinkpad_acpi_module_exit();
10330 return ret;
10331 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -030010332 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010333
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010334
10335 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010336 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010337 NULL, 0);
10338 if (IS_ERR(tpacpi_pdev)) {
10339 ret = PTR_ERR(tpacpi_pdev);
10340 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070010341 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010342 thinkpad_acpi_module_exit();
10343 return ret;
10344 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010345 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010346 TPACPI_HWMON_DRVR_NAME,
10347 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010348 if (IS_ERR(tpacpi_sensors_pdev)) {
10349 ret = PTR_ERR(tpacpi_sensors_pdev);
10350 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070010351 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010352 thinkpad_acpi_module_exit();
10353 return ret;
10354 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010355 tp_features.sensors_pdev_attrs_registered = 1;
Stanislav Fomichev6b99e352017-06-20 20:45:13 -070010356 tpacpi_hwmon = hwmon_device_register_with_groups(
10357 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
10358
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010359 if (IS_ERR(tpacpi_hwmon)) {
10360 ret = PTR_ERR(tpacpi_hwmon);
10361 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070010362 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010363 thinkpad_acpi_module_exit();
10364 return ret;
10365 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -030010366 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010367 tpacpi_inputdev = input_allocate_device();
10368 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010369 thinkpad_acpi_module_exit();
10370 return -ENOMEM;
10371 } else {
10372 /* Prepare input device, but don't register */
10373 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010374 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010375 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -030010376 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010377 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
10378 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +000010379 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010380 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -030010381
10382 /* Init subdriver dependencies */
10383 tpacpi_detect_brightness_capabilities();
10384
10385 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010386 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10387 ret = ibm_init(&ibms_init[i]);
10388 if (ret >= 0 && *ibms_init[i].param)
10389 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010390 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010391 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010392 return ret;
10393 }
10394 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -030010395
10396 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
10397
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010398 ret = input_register_device(tpacpi_inputdev);
10399 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -070010400 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010401 thinkpad_acpi_module_exit();
10402 return ret;
10403 } else {
10404 tp_features.input_device_registered = 1;
10405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010406
10407 return 0;
10408}
10409
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -030010410MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
10411
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010412/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +000010413 * This will autoload the driver in almost every ThinkPad
10414 * in widespread use.
10415 *
10416 * Only _VERY_ old models, like the 240, 240x and 570 lack
10417 * the HKEY event interface.
10418 */
10419MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
10420
10421/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010422 * DMI matching for module autoloading
10423 *
10424 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
10425 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
10426 *
10427 * Only models listed in thinkwiki will be supported, so add yours
10428 * if it is not there yet.
10429 */
10430#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +010010431 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010432
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010433/* Ancient thinkpad BIOSes have to be identified by
10434 * BIOS type or model number, and there are far less
10435 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +000010436IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010437
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +000010438MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
10439MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010440MODULE_DESCRIPTION(TPACPI_DESC);
10441MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010442MODULE_LICENSE("GPL");
10443
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010444module_init(thinkpad_acpi_module_init);
10445module_exit(thinkpad_acpi_module_exit);