blob: d556e95c532c09221bd7db6a6ebd3c577396b409 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Joe Perches0978e012011-04-04 10:06:25 -070024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
Ognjen Galic2801b962018-02-07 15:58:44 +010026#define TPACPI_VERSION "0.26"
Stanislav Fomichev6b99e352017-06-20 20:45:13 -070027#define TPACPI_SYSFS_VERSION 0x030000
Borislav Deianov78f81cc2005-08-17 00:00:00 -040028
29/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020031 * 2007-10-20 changelog trimmed down
32 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030033 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020035 *
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030039 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040040 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020042 *
43 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040044 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
51
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020052#include <linux/kernel.h>
53#include <linux/module.h>
54#include <linux/init.h>
55#include <linux/types.h>
56#include <linux/string.h>
57#include <linux/list.h>
58#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000059#include <linux/sched.h>
Eric W. Biederman7a360942017-09-26 12:45:33 -050060#include <linux/sched/signal.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020061#include <linux/kthread.h>
62#include <linux/freezer.h>
63#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090064#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020065#include <linux/nvram.h>
66#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020067#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020068#include <linux/sysfs.h>
69#include <linux/backlight.h>
Ognjen Galic2801b962018-02-07 15:58:44 +010070#include <linux/bitops.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020071#include <linux/fb.h>
72#include <linux/platform_device.h>
73#include <linux/hwmon.h>
74#include <linux/hwmon-sysfs.h>
75#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030076#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030077#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020078#include <linux/dmi.h>
79#include <linux/jiffies.h>
80#include <linux/workqueue.h>
Lv Zheng8b484632013-12-03 08:49:16 +080081#include <linux/acpi.h>
82#include <linux/pci_ids.h>
Ognjen Galic2801b962018-02-07 15:58:44 +010083#include <linux/power_supply.h>
Lv Zheng8b484632013-12-03 08:49:16 +080084#include <linux/thinkpad_acpi.h>
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020085#include <sound/core.h>
86#include <sound/control.h>
87#include <sound/initval.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080088#include <linux/uaccess.h>
Ognjen Galic2801b962018-02-07 15:58:44 +010089#include <acpi/battery.h>
Hans de Goedeb33c6ce2015-06-16 16:28:10 +020090#include <acpi/video.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020091
92/* ThinkPad CMOS commands */
93#define TP_CMOS_VOLUME_DOWN 0
94#define TP_CMOS_VOLUME_UP 1
95#define TP_CMOS_VOLUME_MUTE 2
96#define TP_CMOS_BRIGHTNESS_UP 4
97#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030098#define TP_CMOS_THINKLIGHT_ON 12
99#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200100
101/* NVRAM Addresses */
102enum tp_nvram_addr {
103 TP_NVRAM_ADDR_HK2 = 0x57,
104 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
105 TP_NVRAM_ADDR_VIDEO = 0x59,
106 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
107 TP_NVRAM_ADDR_MIXER = 0x60,
108};
109
110/* NVRAM bit masks */
111enum {
112 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
113 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
114 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
115 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
116 TP_NVRAM_MASK_THINKLIGHT = 0x10,
117 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
118 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
119 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
120 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
121 TP_NVRAM_MASK_MUTE = 0x40,
122 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
123 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
124 TP_NVRAM_POS_LEVEL_VOLUME = 0,
125};
126
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300127/* Misc NVRAM-related */
128enum {
129 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
130};
131
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200132/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400133#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
134#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Hui Wanga3c42a42016-11-08 16:13:23 +0800135#define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300136#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200137
138/* Input IDs */
139#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
140#define TPACPI_HKEY_INPUT_VERSION 0x4101
141
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200142/* ACPI \WGSV commands */
143enum {
144 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
145 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
146 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
147 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
148};
149
150/* TP_ACPI_WGSV_GET_STATE bits */
151enum {
152 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
153 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
154 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
155 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
156 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
157 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
158 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
159 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
160 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
161 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
162};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200163
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300164/* HKEY events */
165enum tpacpi_hkey_event_t {
166 /* Hotkey-related */
167 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
168 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
169 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
Hans de Goedec685e202017-02-09 16:44:13 +0100170 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300171 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
172 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
173 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
174
175 /* Reasons for waking up from S3/S4 */
176 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
177 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
178 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
179 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
180 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
181 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
182
183 /* Auto-sleep after eject request */
184 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
185 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
186
187 /* Misc bay events */
188 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -0300189 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
190 or port replicator */
191 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
192 dock or port replicator */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300193
194 /* User-interface events */
195 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
196 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
197 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
198 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
Lyudeb03f4d42016-11-11 15:15:03 -0500199 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
200 * enter/leave tablet mode
201 */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300202 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
203 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
204 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
205
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300206 /* Key-related user-interface events */
207 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
208 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
Xavier Naveira67ab6242015-02-10 08:45:18 +0100209 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300210
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300211 /* Thermal events */
212 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
213 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
214 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
215 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
Henrique de Moraes Holschuh6e6bc5f62018-04-24 16:56:03 -0300216 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
217 TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
218 * command completed. Related to
219 * AML DYTC */
220 TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
221 * changed. Related to AML GMTS */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300222
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100223 /* AC-related events */
224 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300225
David Herrmann587d8622018-01-12 12:04:45 +0100226 /* Further user-interface events */
227 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
228 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
229
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300230 /* Misc */
231 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
232};
233
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200234/****************************************************************************
235 * Main driver
236 */
237
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200238#define TPACPI_NAME "thinkpad"
239#define TPACPI_DESC "ThinkPad ACPI Extras"
240#define TPACPI_FILE TPACPI_NAME "_acpi"
241#define TPACPI_URL "http://ibm-acpi.sf.net/"
242#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200243
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200244#define TPACPI_PROC_DIR "ibm"
245#define TPACPI_ACPI_EVENT_PREFIX "ibm"
246#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300247#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200248#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200249
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300250#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300251#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300252
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200253#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200254
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000255/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200256#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000257#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200258#define TPACPI_DBG_INIT 0x0001
259#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +0000260#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000261#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000262#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000263#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200264#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200265
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200266#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
267#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
268#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200269
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200270
271/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200272 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200273 */
274
275struct ibm_struct;
276
277struct tp_acpi_drv_struct {
278 const struct acpi_device_id *hid;
279 struct acpi_driver *driver;
280
281 void (*notify) (struct ibm_struct *, u32);
282 acpi_handle *handle;
283 u32 type;
284 struct acpi_device *device;
285};
286
287struct ibm_struct {
288 char *name;
289
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200290 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200291 int (*write) (char *);
292 void (*exit) (void);
293 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200294 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200295 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200296
297 struct list_head all_drivers;
298
299 struct tp_acpi_drv_struct *acpi;
300
301 struct {
302 u8 acpi_driver_registered:1;
303 u8 acpi_notify_installed:1;
304 u8 proc_created:1;
305 u8 init_called:1;
306 u8 experimental:1;
307 } flags;
308};
309
310struct ibm_init_struct {
311 char param[32];
312
313 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400314 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200315 struct ibm_struct *data;
316};
317
318static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200319 u32 bluetooth:1;
320 u32 hotkey:1;
321 u32 hotkey_mask:1;
322 u32 hotkey_wlsw:1;
Lyudeb3180022016-11-11 15:15:02 -0500323 enum {
324 TP_HOTKEY_TABLET_NONE = 0,
325 TP_HOTKEY_TABLET_USES_MHKG,
Benjamin Bergdda3ec02017-09-15 15:20:49 +0200326 TP_HOTKEY_TABLET_USES_GMMS,
Lyudeb3180022016-11-11 15:15:02 -0500327 } hotkey_tablet;
Pali Rohárbb28f3d52015-12-30 23:27:41 +0100328 u32 kbdlight:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200329 u32 light:1;
330 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300331 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300332 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200333 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200334 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200335 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300336 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300337 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200338 u32 mixer_no_level_control: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;
Ognjen Galic2801b962018-02-07 15:58:44 +0100347 u32 battery:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200348} tp_features;
349
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300350static struct {
351 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200352 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300353} tp_warned;
354
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200355struct thinkpad_id_data {
356 unsigned int vendor; /* ThinkPad vendor:
357 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
358
359 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
360 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
361
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300362 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200363 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300364 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
365 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200366
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300367 char *model_str; /* ThinkPad T43 */
368 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200369};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200370static struct thinkpad_id_data thinkpad_id;
371
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300372static enum {
373 TPACPI_LIFE_INIT = 0,
374 TPACPI_LIFE_RUNNING,
375 TPACPI_LIFE_EXITING,
376} tpacpi_lifecycle;
377
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200378static int experimental;
379static u32 dbg_level;
380
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300381static struct workqueue_struct *tpacpi_wq;
382
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000383enum led_status_t {
384 TPACPI_LED_OFF = 0,
385 TPACPI_LED_ON,
386 TPACPI_LED_BLINK,
387};
388
Hans de Goede86ec0c22017-02-09 16:44:12 +0100389/* tpacpi LED class */
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300390struct tpacpi_led_classdev {
391 struct led_classdev led_classdev;
Adam Leeedf2d772013-06-08 16:51:15 +0800392 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300393};
394
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300395/* brightness level capabilities */
396static unsigned int bright_maxlvl; /* 0 = unknown */
397
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200398#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
399static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030400static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200401static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030402static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200403static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030404static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200405static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030406static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200407#endif
408
409
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000410/*************************************************************************
411 * Debugging helpers
412 */
413
Joe Perches0978e012011-04-04 10:06:25 -0700414#define dbg_printk(a_dbg_level, format, arg...) \
415do { \
416 if (dbg_level & (a_dbg_level)) \
417 printk(KERN_DEBUG pr_fmt("%s: " format), \
418 __func__, ##arg); \
419} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000420
421#ifdef CONFIG_THINKPAD_ACPI_DEBUG
422#define vdbg_printk dbg_printk
423static const char *str_supported(int is_supported);
424#else
Joe Perches0978e012011-04-04 10:06:25 -0700425static inline const char *str_supported(int is_supported) { return ""; }
426#define vdbg_printk(a_dbg_level, format, arg...) \
Joe Perchesefd85cf2015-08-26 11:13:38 -0700427 do { if (0) no_printk(format, ##arg); } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000428#endif
429
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000430static void tpacpi_log_usertask(const char * const what)
431{
Joe Perches0978e012011-04-04 10:06:25 -0700432 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
433 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000434}
435
Joe Perches0978e012011-04-04 10:06:25 -0700436#define tpacpi_disclose_usertask(what, format, arg...) \
437do { \
438 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
439 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
440 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
441 what, task_tgid_vnr(current), ## arg); \
442 } \
443} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000444
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300445/*
446 * Quirk handling helpers
447 *
448 * ThinkPad IDs and versions seen in the field so far
449 * are two-characters from the set [0-9A-Z], i.e. base 36.
450 *
451 * We use values well outside that range as specials.
452 */
453
454#define TPACPI_MATCH_ANY 0xffffU
455#define TPACPI_MATCH_UNKNOWN 0U
456
457/* TPID('1', 'Y') == 0x5931 */
458#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
459
460#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
461 { .vendor = PCI_VENDOR_ID_IBM, \
462 .bios = TPID(__id1, __id2), \
463 .ec = TPACPI_MATCH_ANY, \
464 .quirks = (__quirk) }
465
466#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
467 { .vendor = PCI_VENDOR_ID_LENOVO, \
468 .bios = TPID(__id1, __id2), \
469 .ec = TPACPI_MATCH_ANY, \
470 .quirks = (__quirk) }
471
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200472#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
473 { .vendor = PCI_VENDOR_ID_LENOVO, \
474 .bios = TPACPI_MATCH_ANY, \
475 .ec = TPID(__id1, __id2), \
476 .quirks = (__quirk) }
477
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300478struct tpacpi_quirk {
479 unsigned int vendor;
480 u16 bios;
481 u16 ec;
482 unsigned long quirks;
483};
484
485/**
486 * tpacpi_check_quirks() - search BIOS/EC version on a list
487 * @qlist: array of &struct tpacpi_quirk
488 * @qlist_size: number of elements in @qlist
489 *
490 * Iterates over a quirks list until one is found that matches the
491 * ThinkPad's vendor, BIOS and EC model.
492 *
493 * Returns 0 if nothing matches, otherwise returns the quirks field of
494 * the matching &struct tpacpi_quirk entry.
495 *
496 * The match criteria is: vendor, ec and bios much match.
497 */
498static unsigned long __init tpacpi_check_quirks(
499 const struct tpacpi_quirk *qlist,
500 unsigned int qlist_size)
501{
502 while (qlist_size) {
503 if ((qlist->vendor == thinkpad_id.vendor ||
504 qlist->vendor == TPACPI_MATCH_ANY) &&
505 (qlist->bios == thinkpad_id.bios_model ||
506 qlist->bios == TPACPI_MATCH_ANY) &&
507 (qlist->ec == thinkpad_id.ec_model ||
508 qlist->ec == TPACPI_MATCH_ANY))
509 return qlist->quirks;
510
511 qlist_size--;
512 qlist++;
513 }
514 return 0;
515}
516
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300517static inline bool __pure __init tpacpi_is_lenovo(void)
518{
519 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
520}
521
522static inline bool __pure __init tpacpi_is_ibm(void)
523{
524 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
525}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300526
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300527/****************************************************************************
528 ****************************************************************************
529 *
530 * ACPI Helpers and device model
531 *
532 ****************************************************************************
533 ****************************************************************************/
534
535/*************************************************************************
536 * ACPI basic handles
537 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300539static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300540static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200542#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700544 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300545 &parent##_handle; \
546 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200548TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
549TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200551TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
552 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400553 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
554 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300555 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400556
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200557TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400558 "^HKEY", /* R30, R31 */
559 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300560 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400561
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300562/*************************************************************************
563 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200564 */
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700567 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400570 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200571 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400572 struct acpi_buffer result, *resultp;
573 union acpi_object out_obj;
574 acpi_status status;
575 va_list ap;
576 char res_type;
577 int success;
578 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700581 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return 0;
583 }
584
585 if (*fmt == 'q') {
586 quiet = 1;
587 fmt++;
588 } else
589 quiet = 0;
590
591 res_type = *(fmt++);
592
593 params.count = 0;
594 params.pointer = &in_objs[0];
595
596 va_start(ap, fmt);
597 while (*fmt) {
598 char c = *(fmt++);
599 switch (c) {
600 case 'd': /* int */
601 in_objs[params.count].integer.value = va_arg(ap, int);
602 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
603 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400604 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300606 pr_err("acpi_evalf() called with invalid format character '%c'\n",
607 c);
Jesper Juhl21365852010-12-24 19:56:28 +0100608 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 0;
610 }
611 }
612 va_end(ap);
613
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400614 if (res_type != 'v') {
615 result.length = sizeof(out_obj);
616 result.pointer = &out_obj;
617 resultp = &result;
618 } else
619 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400621 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400624 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300625 success = (status == AE_OK &&
626 out_obj.type == ACPI_TYPE_INTEGER);
627 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700628 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400630 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 success = status == AE_OK;
632 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400633 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300635 pr_err("acpi_evalf() called with invalid format character '%c'\n",
636 res_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return 0;
638 }
639
640 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700641 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300642 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 return success;
645}
646
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200647static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300648{
649 int v;
650
651 if (ecrd_handle) {
652 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
653 return 0;
654 *p = v;
655 } else {
656 if (ec_read(i, p) < 0)
657 return 0;
658 }
659
660 return 1;
661}
662
663static int acpi_ec_write(int i, u8 v)
664{
665 if (ecwr_handle) {
666 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
667 return 0;
668 } else {
669 if (ec_write(i, v) < 0)
670 return 0;
671 }
672
673 return 1;
674}
675
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300676static int issue_thinkpad_cmos_command(int cmos_cmd)
677{
678 if (!cmos_handle)
679 return -ENXIO;
680
681 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
682 return -EIO;
683
684 return 0;
685}
686
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300687/*************************************************************************
688 * ACPI device model
689 */
690
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200691#define TPACPI_ACPIHANDLE_INIT(object) \
692 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300693 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200694
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300695static void __init drv_acpi_handle_init(const char *name,
696 acpi_handle *handle, const acpi_handle parent,
697 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300698{
699 int i;
700 acpi_status status;
701
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300702 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
703 name);
704
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300705 for (i = 0; i < num_paths; i++) {
706 status = acpi_get_handle(parent, paths[i], handle);
707 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300708 dbg_printk(TPACPI_DBG_INIT,
709 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300710 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300711 return;
712 }
713 }
714
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300715 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
716 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300717 *handle = NULL;
718}
719
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300720static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
721 u32 level, void *context, void **return_value)
722{
Aaron Lu46445b62013-10-11 21:27:46 +0800723 struct acpi_device *dev;
724 if (!strcmp(context, "video")) {
725 if (acpi_bus_get_device(handle, &dev))
726 return AE_OK;
727 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
728 return AE_OK;
729 }
730
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300731 *(acpi_handle *)return_value = handle;
732
733 return AE_CTRL_TERMINATE;
734}
735
736static void __init tpacpi_acpi_handle_locate(const char *name,
737 const char *hid,
738 acpi_handle *handle)
739{
740 acpi_status status;
741 acpi_handle device_found;
742
Aaron Lu46445b62013-10-11 21:27:46 +0800743 BUG_ON(!name || !handle);
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300744 vdbg_printk(TPACPI_DBG_INIT,
745 "trying to locate ACPI handle for %s, using HID %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +0800746 name, hid ? hid : "NULL");
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300747
748 memset(&device_found, 0, sizeof(device_found));
749 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
750 (void *)name, &device_found);
751
752 *handle = NULL;
753
754 if (ACPI_SUCCESS(status)) {
755 *handle = device_found;
756 dbg_printk(TPACPI_DBG_INIT,
757 "Found ACPI handle for %s\n", name);
758 } else {
759 vdbg_printk(TPACPI_DBG_INIT,
760 "Could not locate an ACPI handle for %s: %s\n",
761 name, acpi_format_exception(status));
762 }
763}
764
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300765static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300766{
767 struct ibm_struct *ibm = data;
768
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300769 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
770 return;
771
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300772 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300773 return;
774
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300775 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300776}
777
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300778static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300779{
780 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300781 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300782
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300783 BUG_ON(!ibm->acpi);
784
785 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300786 return 0;
787
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300788 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300789 "setting up ACPI notify for %s\n", ibm->name);
790
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300791 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
792 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700793 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300794 return -ENODEV;
795 }
796
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700797 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300798 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200799 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300800 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300801
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300802 status = acpi_install_notify_handler(*ibm->acpi->handle,
803 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300804 if (ACPI_FAILURE(status)) {
805 if (status == AE_ALREADY_EXISTS) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300806 pr_notice("another device driver is already handling %s events\n",
807 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300808 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700809 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300810 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300811 }
812 return -ENODEV;
813 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300814 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300815 return 0;
816}
817
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300818static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300819{
820 return 0;
821}
822
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300823static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300824{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300825 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300826
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300827 dbg_printk(TPACPI_DBG_INIT,
828 "registering %s as an ACPI driver\n", ibm->name);
829
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300830 BUG_ON(!ibm->acpi);
831
832 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
833 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700834 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300835 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300836 }
837
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200838 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300839 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200840
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300841 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300842
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300843 rc = acpi_bus_register_driver(ibm->acpi->driver);
844 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700845 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200846 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300847 kfree(ibm->acpi->driver);
848 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300849 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300850 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300851
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300852 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300853}
854
855
856/****************************************************************************
857 ****************************************************************************
858 *
859 * Procfs Helpers
860 *
861 ****************************************************************************
862 ****************************************************************************/
863
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200864static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300865{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200866 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300867
868 if (!ibm || !ibm->read)
869 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200870 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300871}
872
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200873static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300874{
Al Virod9dda782013-03-31 18:16:14 -0400875 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200876}
877
878static ssize_t dispatch_proc_write(struct file *file,
879 const char __user *userbuf,
880 size_t count, loff_t *pos)
881{
Al Virod9dda782013-03-31 18:16:14 -0400882 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300883 char *kernbuf;
884 int ret;
885
886 if (!ibm || !ibm->write)
887 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300888 if (count > PAGE_SIZE - 2)
889 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300890
891 kernbuf = kmalloc(count + 2, GFP_KERNEL);
892 if (!kernbuf)
893 return -ENOMEM;
894
895 if (copy_from_user(kernbuf, userbuf, count)) {
896 kfree(kernbuf);
897 return -EFAULT;
898 }
899
900 kernbuf[count] = 0;
901 strcat(kernbuf, ",");
902 ret = ibm->write(kernbuf);
903 if (ret == 0)
904 ret = count;
905
906 kfree(kernbuf);
907
908 return ret;
909}
910
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200911static const struct file_operations dispatch_proc_fops = {
912 .owner = THIS_MODULE,
913 .open = dispatch_proc_open,
914 .read = seq_read,
915 .llseek = seq_lseek,
916 .release = single_release,
917 .write = dispatch_proc_write,
918};
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920static char *next_cmd(char **cmds)
921{
922 char *start = *cmds;
923 char *end;
924
925 while ((end = strchr(start, ',')) && end == start)
926 start = end + 1;
927
928 if (!end)
929 return NULL;
930
931 *end = 0;
932 *cmds = end + 1;
933 return start;
934}
935
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300936
937/****************************************************************************
938 ****************************************************************************
939 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300940 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300941 *
942 ****************************************************************************
943 ****************************************************************************/
944
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300945static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300946static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700947static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300948static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300949static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200950static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300951
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200952#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200953static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200954{
955 struct ibm_struct *ibm, *itmp;
956
957 list_for_each_entry_safe(ibm, itmp,
958 &tpacpi_all_drivers,
959 all_drivers) {
960 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200961 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200962 }
963
964 return 0;
965}
966
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200967static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300968{
969 struct ibm_struct *ibm, *itmp;
970
971 list_for_each_entry_safe(ibm, itmp,
972 &tpacpi_all_drivers,
973 all_drivers) {
974 if (ibm->resume)
975 (ibm->resume)();
976 }
977
978 return 0;
979}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200980#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300981
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200982static SIMPLE_DEV_PM_OPS(tpacpi_pm,
983 tpacpi_suspend_handler, tpacpi_resume_handler);
984
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200985static void tpacpi_shutdown_handler(struct platform_device *pdev)
986{
987 struct ibm_struct *ibm, *itmp;
988
989 list_for_each_entry_safe(ibm, itmp,
990 &tpacpi_all_drivers,
991 all_drivers) {
992 if (ibm->shutdown)
993 (ibm->shutdown)();
994 }
995}
996
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300997static struct platform_driver tpacpi_pdriver = {
998 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200999 .name = TPACPI_DRVR_NAME,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +02001000 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001001 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001002 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001003};
1004
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03001005static struct platform_driver tpacpi_hwmon_pdriver = {
1006 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001007 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03001008 },
1009};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001010
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03001011/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001012 * sysfs support helpers
1013 */
1014
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001015struct attribute_set {
1016 unsigned int members, max_members;
1017 struct attribute_group group;
1018};
1019
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001020struct attribute_set_obj {
1021 struct attribute_set s;
1022 struct attribute *a;
1023} __attribute__((packed));
1024
1025static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001026 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001027{
1028 struct attribute_set_obj *sobj;
1029
1030 if (max_members == 0)
1031 return NULL;
1032
1033 /* Allocates space for implicit NULL at the end too */
1034 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1035 max_members * sizeof(struct attribute *),
1036 GFP_KERNEL);
1037 if (!sobj)
1038 return NULL;
1039 sobj->s.max_members = max_members;
1040 sobj->s.group.attrs = &sobj->a;
1041 sobj->s.group.name = name;
1042
1043 return &sobj->s;
1044}
1045
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001046#define destroy_attr_set(_set) \
1047 kfree(_set);
1048
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001049/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001050static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001051{
1052 if (!s || !attr)
1053 return -EINVAL;
1054
1055 if (s->members >= s->max_members)
1056 return -ENOMEM;
1057
1058 s->group.attrs[s->members] = attr;
1059 s->members++;
1060
1061 return 0;
1062}
1063
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001064static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001065 struct attribute **attr,
1066 unsigned int count)
1067{
1068 int i, res;
1069
1070 for (i = 0; i < count; i++) {
1071 res = add_to_attr_set(s, attr[i]);
1072 if (res)
1073 return res;
1074 }
1075
1076 return 0;
1077}
1078
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001079static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001080{
1081 sysfs_remove_group(kobj, &s->group);
1082 destroy_attr_set(s);
1083}
1084
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001085#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1086 sysfs_create_group(_kobj, &_attr_set->group)
1087
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001088static int parse_strtoul(const char *buf,
1089 unsigned long max, unsigned long *value)
1090{
1091 char *endp;
1092
André Goddard Rosae7d28602009-12-14 18:01:06 -08001093 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1094 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001095 if (*endp || *value > max)
1096 return -EINVAL;
1097
1098 return 0;
1099}
1100
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001101static void tpacpi_disable_brightness_delay(void)
1102{
1103 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001104 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001105}
1106
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001107static void printk_deprecated_attribute(const char * const what,
1108 const char * const details)
1109{
1110 tpacpi_log_usertask("deprecated sysfs attribute");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001111 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001112 what, details);
1113}
1114
Johannes Berg19d337d2009-06-02 13:01:37 +02001115/*************************************************************************
1116 * rfkill and radio control support helpers
1117 */
1118
1119/*
1120 * ThinkPad-ACPI firmware handling model:
1121 *
1122 * WLSW (master wireless switch) is event-driven, and is common to all
1123 * firmware-controlled radios. It cannot be controlled, just monitored,
1124 * as expected. It overrides all radio state in firmware
1125 *
1126 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1127 * (TODO: verify how WLSW interacts with the returned radio state).
1128 *
1129 * The only time there are shadow radio state changes, is when
1130 * masked-off hotkeys are used.
1131 */
1132
1133/*
1134 * Internal driver API for radio state:
1135 *
1136 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1137 * bool: true means radio blocked (off)
1138 */
1139enum tpacpi_rfkill_state {
1140 TPACPI_RFK_RADIO_OFF = 0,
1141 TPACPI_RFK_RADIO_ON
1142};
1143
1144/* rfkill switches */
1145enum tpacpi_rfk_id {
1146 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1147 TPACPI_RFK_WWAN_SW_ID,
1148 TPACPI_RFK_UWB_SW_ID,
1149 TPACPI_RFK_SW_MAX
1150};
1151
1152static const char *tpacpi_rfkill_names[] = {
1153 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1154 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1155 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1156 [TPACPI_RFK_SW_MAX] = NULL
1157};
1158
1159/* ThinkPad-ACPI rfkill subdriver */
1160struct tpacpi_rfk {
1161 struct rfkill *rfkill;
1162 enum tpacpi_rfk_id id;
1163 const struct tpacpi_rfk_ops *ops;
1164};
1165
1166struct tpacpi_rfk_ops {
1167 /* firmware interface */
1168 int (*get_status)(void);
1169 int (*set_status)(const enum tpacpi_rfkill_state);
1170};
1171
1172static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1173
1174/* Query FW and update rfkill sw state for a given rfkill switch */
1175static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1176{
1177 int status;
1178
1179 if (!tp_rfk)
1180 return -ENODEV;
1181
1182 status = (tp_rfk->ops->get_status)();
1183 if (status < 0)
1184 return status;
1185
1186 rfkill_set_sw_state(tp_rfk->rfkill,
1187 (status == TPACPI_RFK_RADIO_OFF));
1188
1189 return status;
1190}
1191
1192/* Query FW and update rfkill sw state for all rfkill switches */
1193static void tpacpi_rfk_update_swstate_all(void)
1194{
1195 unsigned int i;
1196
1197 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1198 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1199}
1200
1201/*
1202 * Sync the HW-blocking state of all rfkill switches,
1203 * do notice it causes the rfkill core to schedule uevents
1204 */
1205static void tpacpi_rfk_update_hwblock_state(bool blocked)
1206{
1207 unsigned int i;
1208 struct tpacpi_rfk *tp_rfk;
1209
1210 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1211 tp_rfk = tpacpi_rfkill_switches[i];
1212 if (tp_rfk) {
1213 if (rfkill_set_hw_state(tp_rfk->rfkill,
1214 blocked)) {
1215 /* ignore -- we track sw block */
1216 }
1217 }
1218 }
1219}
1220
1221/* Call to get the WLSW state from the firmware */
1222static int hotkey_get_wlsw(void);
1223
1224/* Call to query WLSW state and update all rfkill switches */
1225static bool tpacpi_rfk_check_hwblock_state(void)
1226{
1227 int res = hotkey_get_wlsw();
1228 int hw_blocked;
1229
1230 /* When unknown or unsupported, we have to assume it is unblocked */
1231 if (res < 0)
1232 return false;
1233
1234 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1235 tpacpi_rfk_update_hwblock_state(hw_blocked);
1236
1237 return hw_blocked;
1238}
1239
1240static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1241{
1242 struct tpacpi_rfk *tp_rfk = data;
1243 int res;
1244
1245 dbg_printk(TPACPI_DBG_RFKILL,
1246 "request to change radio state to %s\n",
1247 blocked ? "blocked" : "unblocked");
1248
1249 /* try to set radio state */
1250 res = (tp_rfk->ops->set_status)(blocked ?
1251 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1252
1253 /* and update the rfkill core with whatever the FW really did */
1254 tpacpi_rfk_update_swstate(tp_rfk);
1255
1256 return (res < 0) ? res : 0;
1257}
1258
1259static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1260 .set_block = tpacpi_rfk_hook_set_block,
1261};
1262
1263static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1264 const struct tpacpi_rfk_ops *tp_rfkops,
1265 const enum rfkill_type rfktype,
1266 const char *name,
1267 const bool set_default)
1268{
1269 struct tpacpi_rfk *atp_rfk;
1270 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001271 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001272 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001273 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001274
1275 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1276
Johannes Berg19d337d2009-06-02 13:01:37 +02001277 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1278 if (atp_rfk)
1279 atp_rfk->rfkill = rfkill_alloc(name,
1280 &tpacpi_pdev->dev,
1281 rfktype,
1282 &tpacpi_rfk_rfkill_ops,
1283 atp_rfk);
1284 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001285 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001286 kfree(atp_rfk);
1287 return -ENOMEM;
1288 }
1289
1290 atp_rfk->id = id;
1291 atp_rfk->ops = tp_rfkops;
1292
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001293 sw_status = (tp_rfkops->get_status)();
1294 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001295 pr_err("failed to read initial state for %s, error %d\n",
1296 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001297 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001298 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001299 if (set_default) {
1300 /* try to keep the initial state, since we ask the
1301 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001302 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001303 }
1304 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001305 hw_state = tpacpi_rfk_check_hwblock_state();
1306 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001307
1308 res = rfkill_register(atp_rfk->rfkill);
1309 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001310 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001311 rfkill_destroy(atp_rfk->rfkill);
1312 kfree(atp_rfk);
1313 return res;
1314 }
1315
1316 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001317
Joe Perches0978e012011-04-04 10:06:25 -07001318 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001319 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001320 return 0;
1321}
1322
1323static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1324{
1325 struct tpacpi_rfk *tp_rfk;
1326
1327 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1328
1329 tp_rfk = tpacpi_rfkill_switches[id];
1330 if (tp_rfk) {
1331 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001332 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001333 tpacpi_rfkill_switches[id] = NULL;
1334 kfree(tp_rfk);
1335 }
1336}
1337
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001338static void printk_deprecated_rfkill_attribute(const char * const what)
1339{
1340 printk_deprecated_attribute(what,
1341 "Please switch to generic rfkill before year 2010");
1342}
1343
Johannes Berg19d337d2009-06-02 13:01:37 +02001344/* sysfs <radio> enable ------------------------------------------------ */
1345static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1346 struct device_attribute *attr,
1347 char *buf)
1348{
1349 int status;
1350
1351 printk_deprecated_rfkill_attribute(attr->attr.name);
1352
1353 /* This is in the ABI... */
1354 if (tpacpi_rfk_check_hwblock_state()) {
1355 status = TPACPI_RFK_RADIO_OFF;
1356 } else {
1357 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1358 if (status < 0)
1359 return status;
1360 }
1361
1362 return snprintf(buf, PAGE_SIZE, "%d\n",
1363 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1364}
1365
1366static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1367 struct device_attribute *attr,
1368 const char *buf, size_t count)
1369{
1370 unsigned long t;
1371 int res;
1372
1373 printk_deprecated_rfkill_attribute(attr->attr.name);
1374
1375 if (parse_strtoul(buf, 1, &t))
1376 return -EINVAL;
1377
1378 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1379
1380 /* This is in the ABI... */
1381 if (tpacpi_rfk_check_hwblock_state() && !!t)
1382 return -EPERM;
1383
1384 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1385 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1386 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1387
1388 return (res < 0) ? res : count;
1389}
1390
1391/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001392static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001393{
Johannes Berg19d337d2009-06-02 13:01:37 +02001394 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001395 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001396 else {
1397 int status;
1398
1399 /* This is in the ABI... */
1400 if (tpacpi_rfk_check_hwblock_state()) {
1401 status = TPACPI_RFK_RADIO_OFF;
1402 } else {
1403 status = tpacpi_rfk_update_swstate(
1404 tpacpi_rfkill_switches[id]);
1405 if (status < 0)
1406 return status;
1407 }
1408
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001409 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001410 (status == TPACPI_RFK_RADIO_ON) ?
1411 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001412 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001413 }
1414
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001415 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001416}
1417
1418static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1419{
1420 char *cmd;
1421 int status = -1;
1422 int res = 0;
1423
1424 if (id >= TPACPI_RFK_SW_MAX)
1425 return -ENODEV;
1426
1427 while ((cmd = next_cmd(&buf))) {
1428 if (strlencmp(cmd, "enable") == 0)
1429 status = TPACPI_RFK_RADIO_ON;
1430 else if (strlencmp(cmd, "disable") == 0)
1431 status = TPACPI_RFK_RADIO_OFF;
1432 else
1433 return -EINVAL;
1434 }
1435
1436 if (status != -1) {
1437 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1438 (status == TPACPI_RFK_RADIO_ON) ?
1439 "enable" : "disable",
1440 tpacpi_rfkill_names[id]);
1441 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1442 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1443 }
1444
1445 return res;
1446}
1447
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001448/*************************************************************************
1449 * thinkpad-acpi driver attributes
1450 */
1451
1452/* interface_version --------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001453static ssize_t interface_version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001454{
1455 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1456}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001457static DRIVER_ATTR_RO(interface_version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001458
1459/* debug_level --------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001460static ssize_t debug_level_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001461{
1462 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1463}
1464
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001465static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1466 size_t count)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001467{
1468 unsigned long t;
1469
1470 if (parse_strtoul(buf, 0xffff, &t))
1471 return -EINVAL;
1472
1473 dbg_level = t;
1474
1475 return count;
1476}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001477static DRIVER_ATTR_RW(debug_level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001478
1479/* version ------------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001480static ssize_t version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001481{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001482 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1483 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001484}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001485static DRIVER_ATTR_RO(version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001486
1487/* --------------------------------------------------------------------- */
1488
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001489#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1490
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001491/* wlsw_emulstate ------------------------------------------------------ */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001492static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001493{
1494 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1495}
1496
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001497static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1498 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001499{
1500 unsigned long t;
1501
1502 if (parse_strtoul(buf, 1, &t))
1503 return -EINVAL;
1504
Johannes Berg19d337d2009-06-02 13:01:37 +02001505 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001506 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001507 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1508 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001509
1510 return count;
1511}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001512static DRIVER_ATTR_RW(wlsw_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001513
1514/* bluetooth_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001515static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001516{
1517 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1518}
1519
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001520static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1521 const char *buf, size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001522{
1523 unsigned long t;
1524
1525 if (parse_strtoul(buf, 1, &t))
1526 return -EINVAL;
1527
1528 tpacpi_bluetooth_emulstate = !!t;
1529
1530 return count;
1531}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001532static DRIVER_ATTR_RW(bluetooth_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001533
1534/* wwan_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001535static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001536{
1537 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1538}
1539
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001540static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1541 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001542{
1543 unsigned long t;
1544
1545 if (parse_strtoul(buf, 1, &t))
1546 return -EINVAL;
1547
1548 tpacpi_wwan_emulstate = !!t;
1549
1550 return count;
1551}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001552static DRIVER_ATTR_RW(wwan_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001553
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001554/* uwb_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001555static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001556{
1557 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1558}
1559
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001560static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1561 size_t count)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001562{
1563 unsigned long t;
1564
1565 if (parse_strtoul(buf, 1, &t))
1566 return -EINVAL;
1567
1568 tpacpi_uwb_emulstate = !!t;
1569
1570 return count;
1571}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001572static DRIVER_ATTR_RW(uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001573#endif
1574
1575/* --------------------------------------------------------------------- */
1576
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001577static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001578 &driver_attr_debug_level, &driver_attr_version,
1579 &driver_attr_interface_version,
1580};
1581
1582static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1583{
1584 int i, res;
1585
1586 i = 0;
1587 res = 0;
1588 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1589 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1590 i++;
1591 }
1592
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001593#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1594 if (!res && dbg_wlswemul)
1595 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1596 if (!res && dbg_bluetoothemul)
1597 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1598 if (!res && dbg_wwanemul)
1599 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001600 if (!res && dbg_uwbemul)
1601 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001602#endif
1603
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001604 return res;
1605}
1606
1607static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1608{
1609 int i;
1610
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001611 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001612 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001613
1614#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1615 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1616 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1617 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001618 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001619#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001620}
1621
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001622/*************************************************************************
1623 * Firmware Data
1624 */
1625
1626/*
1627 * Table of recommended minimum BIOS versions
1628 *
1629 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001630 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001631 * bugs and bad ACPI behaviour on older versions
1632 *
1633 * 2. BIOS or EC fw with known bugs that trigger on Linux
1634 *
1635 * 3. BIOS with known reduced functionality in older versions
1636 *
1637 * We recommend the latest BIOS and EC version.
1638 * We only support the latest BIOS and EC fw version as a rule.
1639 *
1640 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1641 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001642 *
1643 * WARNING: we use this table also to detect that the machine is
1644 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001645 */
1646
1647#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1648 { .vendor = (__v), \
1649 .bios = TPID(__id1, __id2), \
1650 .ec = TPACPI_MATCH_ANY, \
1651 .quirks = TPACPI_MATCH_ANY << 16 \
1652 | (__bv1) << 8 | (__bv2) }
1653
1654#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001655 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001656 { .vendor = (__v), \
1657 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001658 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001659 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1660 | (__bv1) << 8 | (__bv2) }
1661
1662#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1663 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1664
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001665/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001666#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1667 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001668 __bv1, __bv2, TPID(__id1, __id2), \
1669 __ev1, __ev2), \
1670 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1671 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1672 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001673
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001674/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001675#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1676 __eid1, __eid2, __ev1, __ev2) \
1677 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001678 __bv1, __bv2, TPID(__eid1, __eid2), \
1679 __ev1, __ev2), \
1680 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1681 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1682 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001683
1684#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1685 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1686
1687#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1688 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001689 __bv1, __bv2, TPID(__id1, __id2), \
1690 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001691
1692#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1693 __eid1, __eid2, __ev1, __ev2) \
1694 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001695 __bv1, __bv2, TPID(__eid1, __eid2), \
1696 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001697
1698static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1699 /* Numeric models ------------------ */
1700 /* FW MODEL BIOS VERS */
1701 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1702 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1703 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1704 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1705 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1706 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1707 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1708 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1709 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1710
1711 /* A-series ------------------------- */
1712 /* FW MODEL BIOS VERS EC VERS */
1713 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1714 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1715 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1716 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1717 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1718 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1719 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1720 TPV_QI0('1', '3', '2', '0'), /* A22m */
1721 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1722 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1723 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1724
1725 /* G-series ------------------------- */
1726 /* FW MODEL BIOS VERS */
1727 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1728 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1729
1730 /* R-series, T-series --------------- */
1731 /* FW MODEL BIOS VERS EC VERS */
1732 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1733 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1734 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1735 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1736 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1737 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1738 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1739 T40/p, T41/p, T42/p (1) */
1740 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1741 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1742 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1743 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1744
1745 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1746 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1747 TPV_QI0('1', '6', '3', '2'), /* T22 */
1748 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1749 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1750 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1751
1752 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1753 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001754 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001755
1756 /* BIOS FW BIOS VERS EC FW EC VERS */
1757 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1758 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1759
1760 /* X-series ------------------------- */
1761 /* FW MODEL BIOS VERS EC VERS */
1762 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1763 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1764 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1765 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1766 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1767 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1768 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1769
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001770 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1771 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001772
1773 /* (0) - older versions lack DMI EC fw string and functionality */
1774 /* (1) - older versions known to lack functionality */
1775};
1776
1777#undef TPV_QL1
1778#undef TPV_QL0
1779#undef TPV_QI2
1780#undef TPV_QI1
1781#undef TPV_QI0
1782#undef TPV_Q_X
1783#undef TPV_Q
1784
1785static void __init tpacpi_check_outdated_fw(void)
1786{
1787 unsigned long fwvers;
1788 u16 ec_version, bios_version;
1789
1790 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1791 ARRAY_SIZE(tpacpi_bios_version_qtable));
1792
1793 if (!fwvers)
1794 return;
1795
1796 bios_version = fwvers & 0xffffU;
1797 ec_version = (fwvers >> 16) & 0xffffU;
1798
1799 /* note that unknown versions are set to 0x0000 and we use that */
1800 if ((bios_version > thinkpad_id.bios_release) ||
1801 (ec_version > thinkpad_id.ec_release &&
1802 ec_version != TPACPI_MATCH_ANY)) {
1803 /*
1804 * The changelogs would let us track down the exact
1805 * reason, but it is just too much of a pain to track
1806 * it. We only list BIOSes that are either really
1807 * broken, or really stable to begin with, so it is
1808 * best if the user upgrades the firmware anyway.
1809 */
Joe Perches0978e012011-04-04 10:06:25 -07001810 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001811 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 -03001812 }
1813}
1814
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001815static bool __init tpacpi_is_fw_known(void)
1816{
1817 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1818 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1819}
1820
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001821/****************************************************************************
1822 ****************************************************************************
1823 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001824 * Subdrivers
1825 *
1826 ****************************************************************************
1827 ****************************************************************************/
1828
1829/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001830 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001831 */
1832
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001833static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001835 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1836 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1837 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001840static struct ibm_struct thinkpad_acpi_driver_data = {
1841 .name = "driver",
1842 .read = thinkpad_acpi_driver_read,
1843};
1844
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001845/*************************************************************************
1846 * Hotkey subdriver
1847 */
1848
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001849/*
1850 * ThinkPad firmware event model
1851 *
1852 * The ThinkPad firmware has two main event interfaces: normal ACPI
1853 * notifications (which follow the ACPI standard), and a private event
1854 * interface.
1855 *
1856 * The private event interface also issues events for the hotkeys. As
1857 * the driver gained features, the event handling code ended up being
1858 * built around the hotkey subdriver. This will need to be refactored
1859 * to a more formal event API eventually.
1860 *
1861 * Some "hotkeys" are actually supposed to be used as event reports,
1862 * such as "brightness has changed", "volume has changed", depending on
1863 * the ThinkPad model and how the firmware is operating.
1864 *
1865 * Unlike other classes, hotkey-class events have mask/unmask control on
1866 * non-ancient firmware. However, how it behaves changes a lot with the
1867 * firmware model and version.
1868 */
1869
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001870enum { /* hot key scan codes (derived from ACPI DSDT) */
1871 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1872 TP_ACPI_HOTKEYSCAN_FNF2,
1873 TP_ACPI_HOTKEYSCAN_FNF3,
1874 TP_ACPI_HOTKEYSCAN_FNF4,
1875 TP_ACPI_HOTKEYSCAN_FNF5,
1876 TP_ACPI_HOTKEYSCAN_FNF6,
1877 TP_ACPI_HOTKEYSCAN_FNF7,
1878 TP_ACPI_HOTKEYSCAN_FNF8,
1879 TP_ACPI_HOTKEYSCAN_FNF9,
1880 TP_ACPI_HOTKEYSCAN_FNF10,
1881 TP_ACPI_HOTKEYSCAN_FNF11,
1882 TP_ACPI_HOTKEYSCAN_FNF12,
1883 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1884 TP_ACPI_HOTKEYSCAN_FNINSERT,
1885 TP_ACPI_HOTKEYSCAN_FNDELETE,
1886 TP_ACPI_HOTKEYSCAN_FNHOME,
1887 TP_ACPI_HOTKEYSCAN_FNEND,
1888 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1889 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1890 TP_ACPI_HOTKEYSCAN_FNSPACE,
1891 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1892 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1893 TP_ACPI_HOTKEYSCAN_MUTE,
1894 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001895 TP_ACPI_HOTKEYSCAN_UNK1,
1896 TP_ACPI_HOTKEYSCAN_UNK2,
1897 TP_ACPI_HOTKEYSCAN_UNK3,
1898 TP_ACPI_HOTKEYSCAN_UNK4,
1899 TP_ACPI_HOTKEYSCAN_UNK5,
1900 TP_ACPI_HOTKEYSCAN_UNK6,
1901 TP_ACPI_HOTKEYSCAN_UNK7,
1902 TP_ACPI_HOTKEYSCAN_UNK8,
1903
Christian Kellner149c8c72017-02-28 17:10:56 +01001904 /* Adaptive keyboard keycodes */
1905 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1906 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
Bastien Nocera6a68d852015-03-02 14:45:31 +01001907 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1908 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1909 TP_ACPI_HOTKEYSCAN_CLOUD,
1910 TP_ACPI_HOTKEYSCAN_UNK9,
1911 TP_ACPI_HOTKEYSCAN_VOICE,
1912 TP_ACPI_HOTKEYSCAN_UNK10,
1913 TP_ACPI_HOTKEYSCAN_GESTURES,
1914 TP_ACPI_HOTKEYSCAN_UNK11,
1915 TP_ACPI_HOTKEYSCAN_UNK12,
1916 TP_ACPI_HOTKEYSCAN_UNK13,
1917 TP_ACPI_HOTKEYSCAN_CONFIG,
1918 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1919 TP_ACPI_HOTKEYSCAN_RELOAD,
1920 TP_ACPI_HOTKEYSCAN_BACK,
1921 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1922 TP_ACPI_HOTKEYSCAN_MIC_UP,
1923 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1924 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1925 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1926
Christian Kellner696c65232017-02-28 17:10:57 +01001927 /* Lenovo extended keymap, starting at 0x1300 */
1928 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1929 /* first new observed key (star, favorites) is 0x1311 */
1930 TP_ACPI_HOTKEYSCAN_STAR = 69,
1931 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1932 TP_ACPI_HOTKEYSCAN_UNK25,
1933 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1934 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1935
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001936 /* Hotkey keymap size */
1937 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001938};
1939
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001940enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001941 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1942 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1943};
1944
1945enum { /* Positions of some of the keys in hotkey masks */
1946 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1947 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1948 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1949 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1950 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
Hans de Goedec685e202017-02-09 16:44:13 +01001951 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001952 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1953 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1954 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1955 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1956 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1957};
1958
1959enum { /* NVRAM to ACPI HKEY group map */
1960 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1961 TP_ACPI_HKEY_ZOOM_MASK |
1962 TP_ACPI_HKEY_DISPSWTCH_MASK |
1963 TP_ACPI_HKEY_HIBERNATE_MASK,
1964 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1965 TP_ACPI_HKEY_BRGHTDWN_MASK,
1966 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1967 TP_ACPI_HKEY_VOLDWN_MASK |
1968 TP_ACPI_HKEY_MUTE_MASK,
1969};
1970
1971#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1972struct tp_nvram_state {
1973 u16 thinkpad_toggle:1;
1974 u16 zoom_toggle:1;
1975 u16 display_toggle:1;
1976 u16 thinklight_toggle:1;
1977 u16 hibernate_toggle:1;
1978 u16 displayexp_toggle:1;
1979 u16 display_state:1;
1980 u16 brightness_toggle:1;
1981 u16 volume_toggle:1;
1982 u16 mute:1;
1983
1984 u8 brightness_level;
1985 u8 volume_level;
1986};
1987
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001988/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001989static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001990
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001991/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001992 * Acquire mutex to write poller control variables as an
1993 * atomic block.
1994 *
1995 * Increment hotkey_config_change when changing them if you
1996 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001997 *
1998 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1999 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002000static struct mutex hotkey_thread_data_mutex;
2001static unsigned int hotkey_config_change;
2002
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002003/*
2004 * hotkey poller control variables
2005 *
2006 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002007 *
2008 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2009 * should be used only when the changes need to be taken as
2010 * a block, OR when one needs to force the kthread to forget
2011 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002012 */
2013static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2014static unsigned int hotkey_poll_freq = 10; /* Hz */
2015
2016#define HOTKEY_CONFIG_CRITICAL_START \
2017 do { \
2018 mutex_lock(&hotkey_thread_data_mutex); \
2019 hotkey_config_change++; \
2020 } while (0);
2021#define HOTKEY_CONFIG_CRITICAL_END \
2022 mutex_unlock(&hotkey_thread_data_mutex);
2023
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002024#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2025
2026#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002027#define HOTKEY_CONFIG_CRITICAL_START
2028#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002029
2030#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2031
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002032static struct mutex hotkey_mutex;
2033
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002034static enum { /* Reasons for waking up */
2035 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2036 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2037 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2038} hotkey_wakeup_reason;
2039
2040static int hotkey_autosleep_ack;
2041
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002042static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2043static u32 hotkey_all_mask; /* all events supported in fw */
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002044static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002045static u32 hotkey_reserved_mask; /* events better left disabled */
2046static u32 hotkey_driver_mask; /* events needed by the driver */
2047static u32 hotkey_user_mask; /* events visible to userspace */
2048static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002049
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002050static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002051
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002052static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002053
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002054static void tpacpi_driver_event(const unsigned int hkey_event);
2055static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002056static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002057
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002058/* HKEY.MHKG() return bits */
2059#define TP_HOTKEY_TABLET_MASK (1 << 3)
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002060enum {
2061 TP_ACPI_MULTI_MODE_INVALID = 0,
2062 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
2063 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
2064 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
2065 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
2066 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
2067 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
2068 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
2069};
2070
2071enum {
2072 /* The following modes are considered tablet mode for the purpose of
2073 * reporting the status to userspace. i.e. in all these modes it makes
2074 * sense to disable the laptop input devices such as touchpad and
2075 * keyboard.
2076 */
2077 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
2078 TP_ACPI_MULTI_MODE_STAND |
2079 TP_ACPI_MULTI_MODE_TENT |
2080 TP_ACPI_MULTI_MODE_STAND_TENT,
2081};
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002082
Johannes Berg19d337d2009-06-02 13:01:37 +02002083static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002084{
Johannes Berg19d337d2009-06-02 13:01:37 +02002085 int status;
2086
2087 if (!tp_features.hotkey_wlsw)
2088 return -ENODEV;
2089
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002090#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002091 if (dbg_wlswemul)
2092 return (tpacpi_wlsw_emulstate) ?
2093 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002094#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002095
2096 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002097 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002098
2099 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002100}
2101
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002102static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
2103{
2104 int type = (s >> 16) & 0xffff;
2105 int value = s & 0xffff;
2106 int mode = TP_ACPI_MULTI_MODE_INVALID;
2107 int valid_modes = 0;
2108
2109 if (has_tablet_mode)
2110 *has_tablet_mode = 0;
2111
2112 switch (type) {
2113 case 1:
2114 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2115 TP_ACPI_MULTI_MODE_TABLET |
2116 TP_ACPI_MULTI_MODE_STAND_TENT;
2117 break;
2118 case 2:
2119 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2120 TP_ACPI_MULTI_MODE_FLAT |
2121 TP_ACPI_MULTI_MODE_TABLET |
2122 TP_ACPI_MULTI_MODE_STAND |
2123 TP_ACPI_MULTI_MODE_TENT;
2124 break;
2125 case 3:
2126 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2127 TP_ACPI_MULTI_MODE_FLAT;
2128 break;
2129 case 4:
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002130 case 5:
Benjamin Berg26befef2017-11-14 17:14:14 +01002131 /* In mode 4, FLAT is not specified as a valid mode. However,
2132 * it can be seen at least on the X1 Yoga 2nd Generation.
2133 */
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002134 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2135 TP_ACPI_MULTI_MODE_FLAT |
2136 TP_ACPI_MULTI_MODE_TABLET |
2137 TP_ACPI_MULTI_MODE_STAND |
2138 TP_ACPI_MULTI_MODE_TENT;
2139 break;
2140 default:
2141 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2142 type, value, TPACPI_MAIL);
2143 return 0;
2144 }
2145
2146 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2147 *has_tablet_mode = 1;
2148
2149 switch (value) {
2150 case 1:
2151 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2152 break;
2153 case 2:
2154 mode = TP_ACPI_MULTI_MODE_FLAT;
2155 break;
2156 case 3:
2157 mode = TP_ACPI_MULTI_MODE_TABLET;
2158 break;
2159 case 4:
2160 if (type == 1)
2161 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2162 else
2163 mode = TP_ACPI_MULTI_MODE_STAND;
2164 break;
2165 case 5:
2166 mode = TP_ACPI_MULTI_MODE_TENT;
2167 break;
2168 default:
2169 if (type == 5 && value == 0xffff) {
2170 pr_warn("Multi mode status is undetected, assuming laptop\n");
2171 return 0;
2172 }
2173 }
2174
2175 if (!(mode & valid_modes)) {
2176 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2177 value, type, TPACPI_MAIL);
2178 return 0;
2179 }
2180
2181 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2182}
2183
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002184static int hotkey_get_tablet_mode(int *status)
2185{
2186 int s;
2187
Lyudeb03f4d42016-11-11 15:15:03 -05002188 switch (tp_features.hotkey_tablet) {
2189 case TP_HOTKEY_TABLET_USES_MHKG:
2190 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2191 return -EIO;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002192
Lyudeb03f4d42016-11-11 15:15:03 -05002193 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2194 break;
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002195 case TP_HOTKEY_TABLET_USES_GMMS:
2196 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
Lyudeb03f4d42016-11-11 15:15:03 -05002197 return -EIO;
2198
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002199 *status = hotkey_gmms_get_tablet_mode(s, NULL);
Lyudeb03f4d42016-11-11 15:15:03 -05002200 break;
2201 default:
2202 break;
2203 }
2204
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002205 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002206}
2207
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002208/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002209 * Reads current event mask from firmware, and updates
2210 * hotkey_acpi_mask accordingly. Also resets any bits
2211 * from hotkey_user_mask that are unavailable to be
2212 * delivered (shadow requirement of the userspace ABI).
2213 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002214 * Call with hotkey_mutex held
2215 */
2216static int hotkey_mask_get(void)
2217{
2218 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002219 u32 m = 0;
2220
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002221 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002222 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002223
2224 hotkey_acpi_mask = m;
2225 } else {
2226 /* no mask support doesn't mean no event support... */
2227 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002228 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002229
2230 /* sync userspace-visible mask */
2231 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002232
2233 return 0;
2234}
2235
Jean Delvaredf6dd1b2015-04-27 09:45:06 +02002236static void hotkey_mask_warn_incomplete_mask(void)
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002237{
2238 /* log only what the user can fix... */
2239 const u32 wantedmask = hotkey_driver_mask &
2240 ~(hotkey_acpi_mask | hotkey_source_mask) &
2241 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2242
2243 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002244 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002245}
2246
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002247/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002248 * Set the firmware mask when supported
2249 *
2250 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2251 *
2252 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2253 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002254 * Call with hotkey_mutex held
2255 */
2256static int hotkey_mask_set(u32 mask)
2257{
2258 int i;
2259 int rc = 0;
2260
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002261 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002262
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002263 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002264 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002265 if (!acpi_evalf(hkey_handle,
2266 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002267 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002268 rc = -EIO;
2269 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002270 }
2271 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002272 }
2273
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002274 /*
2275 * We *must* make an inconditional call to hotkey_mask_get to
2276 * refresh hotkey_acpi_mask and update hotkey_user_mask
2277 *
2278 * Take the opportunity to also log when we cannot _enable_
2279 * a given event.
2280 */
2281 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002282 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002283 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002284 }
2285
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002286 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2287 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002288
2289 return rc;
2290}
2291
2292/*
2293 * Sets hotkey_user_mask and tries to set the firmware mask
2294 *
2295 * Call with hotkey_mutex held
2296 */
2297static int hotkey_user_mask_set(const u32 mask)
2298{
2299 int rc;
2300
2301 /* Give people a chance to notice they are doing something that
2302 * is bound to go boom on their users sooner or later */
2303 if (!tp_warned.hotkey_mask_ff &&
2304 (mask == 0xffff || mask == 0xffffff ||
2305 mask == 0xffffffff)) {
2306 tp_warned.hotkey_mask_ff = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002307 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2308 mask);
2309 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 -03002310 }
2311
2312 /* Try to enable what the user asked for, plus whatever we need.
2313 * this syncs everything but won't enable bits in hotkey_user_mask */
2314 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2315
2316 /* Enable the available bits in hotkey_user_mask */
2317 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2318
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002319 return rc;
2320}
2321
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002322/*
2323 * Sets the driver hotkey mask.
2324 *
2325 * Can be called even if the hotkey subdriver is inactive
2326 */
2327static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2328{
2329 int rc;
2330
2331 /* Do the right thing if hotkey_init has not been called yet */
2332 if (!tp_features.hotkey) {
2333 hotkey_driver_mask = mask;
2334 return 0;
2335 }
2336
2337 mutex_lock(&hotkey_mutex);
2338
2339 HOTKEY_CONFIG_CRITICAL_START
2340 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002341#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002342 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002343#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002344 HOTKEY_CONFIG_CRITICAL_END
2345
2346 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2347 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002348 hotkey_poll_setup(true);
2349
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002350 mutex_unlock(&hotkey_mutex);
2351
2352 return rc;
2353}
2354
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002355static int hotkey_status_get(int *status)
2356{
2357 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2358 return -EIO;
2359
2360 return 0;
2361}
2362
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002363static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002364{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002365 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002366 return -EIO;
2367
2368 return 0;
2369}
2370
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002371static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002372{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002373 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002374
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002375 if (tp_features.hotkey_tablet &&
2376 !hotkey_get_tablet_mode(&state)) {
2377 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002378
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002379 input_report_switch(tpacpi_inputdev,
2380 SW_TABLET_MODE, !!state);
2381 input_sync(tpacpi_inputdev);
2382
2383 mutex_unlock(&tpacpi_inputdev_send_mutex);
2384 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002385}
2386
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002387/* Do NOT call without validating scancode first */
2388static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002389{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002390 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002391
2392 if (keycode != KEY_RESERVED) {
2393 mutex_lock(&tpacpi_inputdev_send_mutex);
2394
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002395 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002396 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002397 input_sync(tpacpi_inputdev);
2398
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002399 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002400 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002401 input_sync(tpacpi_inputdev);
2402
2403 mutex_unlock(&tpacpi_inputdev_send_mutex);
2404 }
2405}
2406
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002407/* Do NOT call without validating scancode first */
2408static void tpacpi_input_send_key_masked(const unsigned int scancode)
2409{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002410 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002411 if (hotkey_user_mask & (1 << scancode))
2412 tpacpi_input_send_key(scancode);
2413}
2414
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002415#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2416static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2417
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002418/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002419static void tpacpi_hotkey_send_key(unsigned int scancode)
2420{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002421 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002422}
2423
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002424static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002425{
2426 u8 d;
2427
2428 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2429 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2430 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2431 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2432 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2433 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2434 }
Hans de Goedec685e202017-02-09 16:44:13 +01002435 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002436 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2437 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2438 }
2439 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2440 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2441 n->displayexp_toggle =
2442 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2443 }
2444 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2445 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2446 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2447 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2448 n->brightness_toggle =
2449 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2450 }
2451 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2452 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2453 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2454 >> TP_NVRAM_POS_LEVEL_VOLUME;
2455 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2456 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2457 }
2458}
2459
Behan Webstera4d44ba2014-02-12 21:58:46 +01002460#define TPACPI_COMPARE_KEY(__scancode, __member) \
2461do { \
2462 if ((event_mask & (1 << __scancode)) && \
2463 oldn->__member != newn->__member) \
2464 tpacpi_hotkey_send_key(__scancode); \
2465} while (0)
2466
2467#define TPACPI_MAY_SEND_KEY(__scancode) \
2468do { \
2469 if (event_mask & (1 << __scancode)) \
2470 tpacpi_hotkey_send_key(__scancode); \
2471} while (0)
2472
2473static void issue_volchange(const unsigned int oldvol,
2474 const unsigned int newvol,
2475 const u32 event_mask)
2476{
2477 unsigned int i = oldvol;
2478
2479 while (i > newvol) {
2480 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2481 i--;
2482 }
2483 while (i < newvol) {
2484 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2485 i++;
2486 }
2487}
2488
2489static void issue_brightnesschange(const unsigned int oldbrt,
2490 const unsigned int newbrt,
2491 const u32 event_mask)
2492{
2493 unsigned int i = oldbrt;
2494
2495 while (i > newbrt) {
2496 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2497 i--;
2498 }
2499 while (i < newbrt) {
2500 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2501 i++;
2502 }
2503}
2504
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002505static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2506 struct tp_nvram_state *newn,
2507 const u32 event_mask)
2508{
2509
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002510 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2511 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2512 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2513 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2514
2515 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2516
2517 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2518
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002519 /*
2520 * Handle volume
2521 *
2522 * This code is supposed to duplicate the IBM firmware behaviour:
2523 * - Pressing MUTE issues mute hotkey message, even when already mute
2524 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002525 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002526 * - The act of unmuting issues volume up/down notification,
2527 * depending which key was used to unmute
2528 *
2529 * We are constrained to what the NVRAM can tell us, which is not much
2530 * and certainly not enough if more than one volume hotkey was pressed
2531 * since the last poll cycle.
2532 *
2533 * Just to make our life interesting, some newer Lenovo ThinkPads have
2534 * bugs in the BIOS and may fail to update volume_toggle properly.
2535 */
2536 if (newn->mute) {
2537 /* muted */
2538 if (!oldn->mute ||
2539 oldn->volume_toggle != newn->volume_toggle ||
2540 oldn->volume_level != newn->volume_level) {
2541 /* recently muted, or repeated mute keypress, or
2542 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002543 issue_volchange(oldn->volume_level, newn->volume_level,
2544 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002545 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2546 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002547 } else {
2548 /* unmute */
2549 if (oldn->mute) {
2550 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002551 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002552 }
2553 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002554 issue_volchange(oldn->volume_level, newn->volume_level,
2555 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002556 } else if (oldn->volume_toggle != newn->volume_toggle) {
2557 /* repeated vol up/down keypress at end of scale ? */
2558 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002559 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002560 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2561 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002562 }
2563 }
2564
2565 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002566 if (oldn->brightness_level != newn->brightness_level) {
2567 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002568 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002569 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2570 /* repeated key presses that didn't change state */
2571 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002572 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002573 else if (newn->brightness_level >= bright_maxlvl
2574 && !tp_features.bright_unkfw)
2575 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002576 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002577
2578#undef TPACPI_COMPARE_KEY
2579#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002580}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002581
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002582/*
2583 * Polling driver
2584 *
2585 * We track all events in hotkey_source_mask all the time, since
2586 * most of them are edge-based. We only issue those requested by
2587 * hotkey_user_mask or hotkey_driver_mask, though.
2588 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002589static int hotkey_kthread(void *data)
2590{
2591 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002592 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002593 unsigned int si, so;
2594 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002595 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002596 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002597 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002598
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002599 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2600 goto exit;
2601
2602 set_freezable();
2603
2604 so = 0;
2605 si = 1;
2606 t = 0;
2607
2608 /* Initial state for compares */
2609 mutex_lock(&hotkey_thread_data_mutex);
2610 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002611 poll_mask = hotkey_source_mask;
2612 event_mask = hotkey_source_mask &
2613 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002614 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002615 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002616 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002617
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002618 while (!kthread_should_stop()) {
2619 if (t == 0) {
2620 if (likely(poll_freq))
2621 t = 1000/poll_freq;
2622 else
2623 t = 100; /* should never happen... */
2624 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002625 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002626 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002627 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002628
2629 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002630 continue;
2631
2632 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002633 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002634 /* forget old state on thaw or config change */
2635 si = so;
2636 t = 0;
2637 change_detector = hotkey_config_change;
2638 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002639 poll_mask = hotkey_source_mask;
2640 event_mask = hotkey_source_mask &
2641 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002642 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002643 mutex_unlock(&hotkey_thread_data_mutex);
2644
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002645 if (likely(poll_mask)) {
2646 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002647 if (likely(si != so)) {
2648 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002649 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002650 }
2651 }
2652
2653 so = si;
2654 si ^= 1;
2655 }
2656
2657exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002658 return 0;
2659}
2660
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002661/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002662static void hotkey_poll_stop_sync(void)
2663{
2664 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002665 kthread_stop(tpacpi_hotkey_task);
2666 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002667 }
2668}
2669
2670/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002671static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002672{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002673 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2674 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002675
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002676 if (hotkey_poll_freq > 0 &&
2677 (poll_driver_mask ||
2678 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002679 if (!tpacpi_hotkey_task) {
2680 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002681 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002682 if (IS_ERR(tpacpi_hotkey_task)) {
2683 tpacpi_hotkey_task = NULL;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002684 pr_err("could not create kernel thread for hotkey polling\n");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002685 }
2686 }
2687 } else {
2688 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002689 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002690 hotkey_poll_freq == 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002691 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 -07002692 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002693 }
2694 }
2695}
2696
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002697static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002698{
2699 mutex_lock(&hotkey_mutex);
2700 hotkey_poll_setup(may_warn);
2701 mutex_unlock(&hotkey_mutex);
2702}
2703
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002704/* call with hotkey_mutex held */
2705static void hotkey_poll_set_freq(unsigned int freq)
2706{
2707 if (!freq)
2708 hotkey_poll_stop_sync();
2709
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002710 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002711}
2712
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002713#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2714
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002715static void hotkey_poll_setup(const bool __unused)
2716{
2717}
2718
2719static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002720{
2721}
2722
2723#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2724
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002725static int hotkey_inputdev_open(struct input_dev *dev)
2726{
2727 switch (tpacpi_lifecycle) {
2728 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002729 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002730 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002731 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002732 case TPACPI_LIFE_EXITING:
2733 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002734 }
2735
2736 /* Should only happen if tpacpi_lifecycle is corrupt */
2737 BUG();
2738 return -EBUSY;
2739}
2740
2741static void hotkey_inputdev_close(struct input_dev *dev)
2742{
2743 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002744 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002745 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002746 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002747}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002748
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002749/* sysfs hotkey enable ------------------------------------------------- */
2750static ssize_t hotkey_enable_show(struct device *dev,
2751 struct device_attribute *attr,
2752 char *buf)
2753{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002754 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002755
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002756 printk_deprecated_attribute("hotkey_enable",
2757 "Hotkey reporting is always enabled");
2758
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002759 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002760 if (res)
2761 return res;
2762
2763 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2764}
2765
2766static ssize_t hotkey_enable_store(struct device *dev,
2767 struct device_attribute *attr,
2768 const char *buf, size_t count)
2769{
2770 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002771
2772 printk_deprecated_attribute("hotkey_enable",
2773 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002774
2775 if (parse_strtoul(buf, 1, &t))
2776 return -EINVAL;
2777
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002778 if (t == 0)
2779 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002780
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002781 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002782}
2783
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002784static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002785
2786/* sysfs hotkey mask --------------------------------------------------- */
2787static ssize_t hotkey_mask_show(struct device *dev,
2788 struct device_attribute *attr,
2789 char *buf)
2790{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002791 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002792}
2793
2794static ssize_t hotkey_mask_store(struct device *dev,
2795 struct device_attribute *attr,
2796 const char *buf, size_t count)
2797{
2798 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002799 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002800
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002801 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002802 return -EINVAL;
2803
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002804 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002805 return -ERESTARTSYS;
2806
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002807 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002808
2809#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002810 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002811#endif
2812
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002813 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002814
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002815 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2816
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002817 return (res) ? res : count;
2818}
2819
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002820static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002821
2822/* sysfs hotkey bios_enabled ------------------------------------------- */
2823static ssize_t hotkey_bios_enabled_show(struct device *dev,
2824 struct device_attribute *attr,
2825 char *buf)
2826{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002827 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002828}
2829
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002830static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002831
2832/* sysfs hotkey bios_mask ---------------------------------------------- */
2833static ssize_t hotkey_bios_mask_show(struct device *dev,
2834 struct device_attribute *attr,
2835 char *buf)
2836{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002837 printk_deprecated_attribute("hotkey_bios_mask",
2838 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002839 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002840}
2841
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002842static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002843
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002844/* sysfs hotkey all_mask ----------------------------------------------- */
2845static ssize_t hotkey_all_mask_show(struct device *dev,
2846 struct device_attribute *attr,
2847 char *buf)
2848{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002849 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2850 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002851}
2852
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002853static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002854
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002855/* sysfs hotkey all_mask ----------------------------------------------- */
2856static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2857 struct device_attribute *attr,
2858 char *buf)
2859{
2860 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2861 hotkey_adaptive_all_mask | hotkey_source_mask);
2862}
2863
2864static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2865
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002866/* sysfs hotkey recommended_mask --------------------------------------- */
2867static ssize_t hotkey_recommended_mask_show(struct device *dev,
2868 struct device_attribute *attr,
2869 char *buf)
2870{
2871 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002872 (hotkey_all_mask | hotkey_source_mask)
2873 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002874}
2875
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002876static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002877
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002878#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2879
2880/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2881static ssize_t hotkey_source_mask_show(struct device *dev,
2882 struct device_attribute *attr,
2883 char *buf)
2884{
2885 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2886}
2887
2888static ssize_t hotkey_source_mask_store(struct device *dev,
2889 struct device_attribute *attr,
2890 const char *buf, size_t count)
2891{
2892 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002893 u32 r_ev;
2894 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002895
2896 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2897 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2898 return -EINVAL;
2899
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002900 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002901 return -ERESTARTSYS;
2902
2903 HOTKEY_CONFIG_CRITICAL_START
2904 hotkey_source_mask = t;
2905 HOTKEY_CONFIG_CRITICAL_END
2906
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002907 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2908 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002909 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002910
2911 /* check if events needed by the driver got disabled */
2912 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2913 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002914
2915 mutex_unlock(&hotkey_mutex);
2916
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002917 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002918 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002919
2920 if (r_ev)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002921 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002922 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002923
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002924 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2925
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002926 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002927}
2928
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002929static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002930
2931/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2932static ssize_t hotkey_poll_freq_show(struct device *dev,
2933 struct device_attribute *attr,
2934 char *buf)
2935{
2936 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2937}
2938
2939static ssize_t hotkey_poll_freq_store(struct device *dev,
2940 struct device_attribute *attr,
2941 const char *buf, size_t count)
2942{
2943 unsigned long t;
2944
2945 if (parse_strtoul(buf, 25, &t))
2946 return -EINVAL;
2947
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002948 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002949 return -ERESTARTSYS;
2950
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002951 hotkey_poll_set_freq(t);
2952 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002953
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002954 mutex_unlock(&hotkey_mutex);
2955
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002956 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2957
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002958 return count;
2959}
2960
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002961static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002962
2963#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2964
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002965/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002966static ssize_t hotkey_radio_sw_show(struct device *dev,
2967 struct device_attribute *attr,
2968 char *buf)
2969{
Johannes Berg19d337d2009-06-02 13:01:37 +02002970 int res;
2971 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002972 if (res < 0)
2973 return res;
2974
Johannes Berg19d337d2009-06-02 13:01:37 +02002975 /* Opportunistic update */
2976 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2977
2978 return snprintf(buf, PAGE_SIZE, "%d\n",
2979 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002980}
2981
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002982static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002983
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002984static void hotkey_radio_sw_notify_change(void)
2985{
2986 if (tp_features.hotkey_wlsw)
2987 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2988 "hotkey_radio_sw");
2989}
2990
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002991/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2992static ssize_t hotkey_tablet_mode_show(struct device *dev,
2993 struct device_attribute *attr,
2994 char *buf)
2995{
2996 int res, s;
2997 res = hotkey_get_tablet_mode(&s);
2998 if (res < 0)
2999 return res;
3000
3001 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
3002}
3003
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01003004static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003005
3006static void hotkey_tablet_mode_notify_change(void)
3007{
3008 if (tp_features.hotkey_tablet)
3009 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3010 "hotkey_tablet_mode");
3011}
3012
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003013/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003014static ssize_t hotkey_wakeup_reason_show(struct device *dev,
3015 struct device_attribute *attr,
3016 char *buf)
3017{
3018 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
3019}
3020
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003021static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003022
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02003023static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003024{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003025 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3026 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003027}
3028
3029/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003030static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
3031 struct device_attribute *attr,
3032 char *buf)
3033{
3034 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
3035}
3036
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003037static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
3038 hotkey_wakeup_hotunplug_complete_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003039
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02003040static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003041{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003042 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3043 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003044}
3045
Bastien Nocerab790cee2015-03-02 14:45:27 +01003046/* sysfs adaptive kbd mode --------------------------------------------- */
3047
3048static int adaptive_keyboard_get_mode(void);
3049static int adaptive_keyboard_set_mode(int new_mode);
3050
3051enum ADAPTIVE_KEY_MODE {
3052 HOME_MODE,
3053 WEB_BROWSER_MODE,
3054 WEB_CONFERENCE_MODE,
3055 FUNCTION_MODE,
3056 LAYFLAT_MODE
3057};
3058
3059static ssize_t adaptive_kbd_mode_show(struct device *dev,
3060 struct device_attribute *attr,
3061 char *buf)
3062{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003063 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01003064
3065 current_mode = adaptive_keyboard_get_mode();
3066 if (current_mode < 0)
3067 return current_mode;
3068
3069 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
3070}
3071
3072static ssize_t adaptive_kbd_mode_store(struct device *dev,
3073 struct device_attribute *attr,
3074 const char *buf, size_t count)
3075{
3076 unsigned long t;
3077 int res;
3078
3079 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
3080 return -EINVAL;
3081
3082 res = adaptive_keyboard_set_mode(t);
3083 return (res < 0) ? res : count;
3084}
3085
3086static DEVICE_ATTR_RW(adaptive_kbd_mode);
3087
3088static struct attribute *adaptive_kbd_attributes[] = {
3089 &dev_attr_adaptive_kbd_mode.attr,
3090 NULL
3091};
3092
3093static const struct attribute_group adaptive_kbd_attr_group = {
3094 .attrs = adaptive_kbd_attributes,
3095};
3096
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003097/* --------------------------------------------------------------------- */
3098
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003099static struct attribute *hotkey_attributes[] __initdata = {
3100 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003101 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003102 &dev_attr_hotkey_bios_mask.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003103 &dev_attr_wakeup_reason.attr,
3104 &dev_attr_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003105 &dev_attr_hotkey_mask.attr,
3106 &dev_attr_hotkey_all_mask.attr,
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003107 &dev_attr_hotkey_adaptive_all_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003108 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003109#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003110 &dev_attr_hotkey_source_mask.attr,
3111 &dev_attr_hotkey_poll_freq.attr,
3112#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003113};
3114
Johannes Berg19d337d2009-06-02 13:01:37 +02003115/*
3116 * Sync both the hw and sw blocking state of all switches
3117 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003118static void tpacpi_send_radiosw_update(void)
3119{
3120 int wlsw;
3121
Johannes Berg19d337d2009-06-02 13:01:37 +02003122 /*
3123 * We must sync all rfkill controllers *before* issuing any
3124 * rfkill input events, or we will race the rfkill core input
3125 * handler.
3126 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003127 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003128 * for the above.
3129 *
3130 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3131 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003132
Johannes Berg19d337d2009-06-02 13:01:37 +02003133 wlsw = hotkey_get_wlsw();
3134
3135 /* Sync hw blocking state first if it is hw-blocked */
3136 if (wlsw == TPACPI_RFK_RADIO_OFF)
3137 tpacpi_rfk_update_hwblock_state(true);
3138
3139 /* Sync sw blocking state */
3140 tpacpi_rfk_update_swstate_all();
3141
3142 /* Sync hw blocking state last if it is hw-unblocked */
3143 if (wlsw == TPACPI_RFK_RADIO_ON)
3144 tpacpi_rfk_update_hwblock_state(false);
3145
3146 /* Issue rfkill input event for WLSW switch */
3147 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003148 mutex_lock(&tpacpi_inputdev_send_mutex);
3149
3150 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003151 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003152 input_sync(tpacpi_inputdev);
3153
3154 mutex_unlock(&tpacpi_inputdev_send_mutex);
3155 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003156
3157 /*
3158 * this can be unconditional, as we will poll state again
3159 * if userspace uses the notify to read data
3160 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003161 hotkey_radio_sw_notify_change();
3162}
3163
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003164static void hotkey_exit(void)
3165{
3166#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003167 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003168 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003169 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003170#endif
3171
3172 if (hotkey_dev_attributes)
3173 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3174
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003175 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003176 "restoring original HKEY status and mask\n");
3177 /* yes, there is a bitwise or below, we want the
3178 * functions to be called even if one of them fail */
3179 if (((tp_features.hotkey_mask &&
3180 hotkey_mask_set(hotkey_orig_mask)) |
3181 hotkey_status_set(false)) != 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003182 pr_err("failed to restore hot key mask to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003183}
3184
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003185static void __init hotkey_unmap(const unsigned int scancode)
3186{
3187 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3188 clear_bit(hotkey_keycode_map[scancode],
3189 tpacpi_inputdev->keybit);
3190 hotkey_keycode_map[scancode] = KEY_RESERVED;
3191 }
3192}
3193
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003194/*
3195 * HKEY quirks:
3196 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3197 */
3198
3199#define TPACPI_HK_Q_INIMASK 0x0001
3200
3201static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3202 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3203 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3204 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3205 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3206 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3207 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3208 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3209 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3210 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3211 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3212 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3213 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3214 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3215 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3216 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3217 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3218 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3219 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3220 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3221};
3222
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003223typedef u16 tpacpi_keymap_entry_t;
3224typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003225
Lyudeb3180022016-11-11 15:15:02 -05003226static int hotkey_init_tablet_mode(void)
3227{
Darren Hartcb2bf252016-12-14 14:45:48 -08003228 int in_tablet_mode = 0, res;
3229 char *type = NULL;
Lyudeb3180022016-11-11 15:15:02 -05003230
Benjamin Bergdda3ec02017-09-15 15:20:49 +02003231 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3232 int has_tablet_mode;
3233
3234 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3235 &has_tablet_mode);
3236 if (has_tablet_mode)
3237 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3238 type = "GMMS";
3239 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
Lyudeb03f4d42016-11-11 15:15:03 -05003240 /* For X41t, X60t, X61t Tablets... */
Lyudeb3180022016-11-11 15:15:02 -05003241 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3242 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3243 type = "MHKG";
3244 }
3245
3246 if (!tp_features.hotkey_tablet)
3247 return 0;
3248
3249 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3250 type, in_tablet_mode ? "tablet" : "laptop");
3251
3252 res = add_to_attr_set(hotkey_dev_attributes,
3253 &dev_attr_hotkey_tablet_mode.attr);
3254 if (res)
3255 return -1;
3256
3257 return in_tablet_mode;
3258}
3259
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003260static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003261{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003262 /* Requirements for changing the default keymaps:
3263 *
3264 * 1. Many of the keys are mapped to KEY_RESERVED for very
3265 * good reasons. Do not change them unless you have deep
3266 * knowledge on the IBM and Lenovo ThinkPad firmware for
3267 * the various ThinkPad models. The driver behaves
3268 * differently for KEY_RESERVED: such keys have their
3269 * hot key mask *unset* in mask_recommended, and also
3270 * in the initial hot key mask programmed into the
3271 * firmware at driver load time, which means the firm-
3272 * ware may react very differently if you change them to
3273 * something else;
3274 *
3275 * 2. You must be subscribed to the linux-thinkpad and
3276 * ibm-acpi-devel mailing lists, and you should read the
3277 * list archives since 2007 if you want to change the
3278 * keymaps. This requirement exists so that you will
3279 * know the past history of problems with the thinkpad-
3280 * acpi driver keymaps, and also that you will be
3281 * listening to any bug reports;
3282 *
3283 * 3. Do not send thinkpad-acpi specific patches directly to
3284 * for merging, *ever*. Send them to the linux-acpi
3285 * mailinglist for comments. Merging is to be done only
3286 * through acpi-test and the ACPI maintainer.
3287 *
3288 * If the above is too much to ask, don't change the keymap.
3289 * Ask the thinkpad-acpi maintainer to do it, instead.
3290 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003291
3292 enum keymap_index {
3293 TPACPI_KEYMAP_IBM_GENERIC = 0,
3294 TPACPI_KEYMAP_LENOVO_GENERIC,
3295 };
3296
3297 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3298 /* Generic keymap for IBM ThinkPads */
3299 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003300 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003301 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003302 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3303 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003304
3305 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003306 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3307 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3308 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003309
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003310 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003311 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003312 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003313
3314 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003315 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003316
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003317 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3318 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003319
3320 /* Volume: firmware always react to it and reprograms
3321 * the built-in *extra* mixer. Never map it to control
3322 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003323 KEY_RESERVED, /* 0x14: VOLUME UP */
3324 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3325 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003326
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003327 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003328
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003329 /* (assignments unknown, please report if found) */
3330 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3331 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003332
3333 /* No assignments, only used for Adaptive keyboards. */
3334 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3335 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3336 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3337 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3338 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Christian Kellner696c65232017-02-28 17:10:57 +01003339
3340 /* No assignment, used for newer Lenovo models */
3341 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
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
3347
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003348 },
3349
3350 /* Generic keymap for Lenovo ThinkPads */
3351 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003352 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3353 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003354 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003355 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003356
3357 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003358 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3359 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3360 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003361
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003362 /* These should be enabled --only-- when ACPI video
3363 * is disabled (i.e. in "vendor" mode), and are handled
3364 * in a special way by the init code */
3365 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3366 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003367
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003368 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003369
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003370 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3371 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003372
3373 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3374 * react to it and reprograms the built-in *extra* mixer.
3375 * Never map it to control another mixer by default.
3376 *
3377 * T60?, T61, R60?, R61: firmware and EC tries to send
3378 * these over the regular keyboard, so these are no-ops,
3379 * but there are still weird bugs re. MUTE, so do not
3380 * change unless you get test reports from all Lenovo
3381 * models. May cause the BIOS to interfere with the
3382 * HDA mixer.
3383 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003384 KEY_RESERVED, /* 0x14: VOLUME UP */
3385 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3386 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003387
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003388 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003389
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003390 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003391 KEY_UNKNOWN, KEY_UNKNOWN,
3392
3393 /*
3394 * The mic mute button only sends 0x1a. It does not
3395 * automatically mute the mic or change the mute light.
3396 */
3397 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3398
3399 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003400 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003401
3402 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003403 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003404
3405 /*
3406 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3407 * The first item in this list is the Mute button which is
3408 * emitted with 0x103 through
3409 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3410 * symbol is held.
3411 * We'll need to offset those by 0x20.
3412 */
3413 KEY_RESERVED, /* Mute held, 0x103 */
3414 KEY_BRIGHTNESS_MIN, /* Backlight off */
3415 KEY_RESERVED, /* Clipping tool */
3416 KEY_RESERVED, /* Cloud */
3417 KEY_RESERVED,
3418 KEY_VOICECOMMAND, /* Voice */
3419 KEY_RESERVED,
3420 KEY_RESERVED, /* Gestures */
3421 KEY_RESERVED,
3422 KEY_RESERVED,
3423 KEY_RESERVED,
3424 KEY_CONFIG, /* Settings */
3425 KEY_RESERVED, /* New tab */
3426 KEY_REFRESH, /* Reload */
3427 KEY_BACK, /* Back */
3428 KEY_RESERVED, /* Microphone down */
3429 KEY_RESERVED, /* Microphone up */
3430 KEY_RESERVED, /* Microphone cancellation */
3431 KEY_RESERVED, /* Camera mode */
3432 KEY_RESERVED, /* Rotate display, 0x116 */
Christian Kellner696c65232017-02-28 17:10:57 +01003433
3434 /*
3435 * These are found in 2017 models (e.g. T470s, X270).
3436 * The lowest known value is 0x311, which according to
3437 * the manual should launch a user defined favorite
3438 * application.
3439 *
3440 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3441 * corresponding to 0x34.
3442 */
3443
3444 /* (assignments unknown, please report if found) */
3445 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3446 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3447 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3448 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3449 KEY_UNKNOWN,
3450
3451 KEY_FAVORITES, /* Favorite app, 0x311 */
3452 KEY_RESERVED, /* Clipping tool */
3453 KEY_RESERVED,
3454 KEY_BLUETOOTH, /* Bluetooth */
3455 KEY_KEYBOARD /* Keyboard, 0x315 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003456 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003457 };
3458
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003459 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3460 /* Generic maps (fallback) */
3461 {
3462 .vendor = PCI_VENDOR_ID_IBM,
3463 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3464 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3465 },
3466 {
3467 .vendor = PCI_VENDOR_ID_LENOVO,
3468 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3469 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3470 },
3471 };
3472
3473#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003474#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003475
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003476 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003477 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003478 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003479 bool radiosw_state = false;
3480 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003481
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003482 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003483 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003484
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003485 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3486 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003487
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003488 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003489 BUG_ON(tpacpi_inputdev->open != NULL ||
3490 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003491
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003492 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003493 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003494
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003495#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003496 mutex_init(&hotkey_thread_data_mutex);
3497#endif
3498
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003499 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003500 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003501
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003502 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3503 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003504 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003505
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003506 if (!tp_features.hotkey)
3507 return 1;
3508
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003509 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3510 ARRAY_SIZE(tpacpi_hotkey_qtable));
3511
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003512 tpacpi_disable_brightness_delay();
3513
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003514 /* MUST have enough space for all attributes to be added to
3515 * hotkey_dev_attributes */
3516 hotkey_dev_attributes = create_attr_set(
3517 ARRAY_SIZE(hotkey_attributes) + 2,
3518 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003519 if (!hotkey_dev_attributes)
3520 return -ENOMEM;
3521 res = add_many_to_attr_set(hotkey_dev_attributes,
3522 hotkey_attributes,
3523 ARRAY_SIZE(hotkey_attributes));
3524 if (res)
3525 goto err_exit;
3526
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003527 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003528 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3529 for HKEY interface version 0x100 */
3530 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003531 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3532 "firmware HKEY interface version: 0x%x\n",
3533 hkeyv);
3534
3535 switch (hkeyv >> 8) {
3536 case 1:
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003537 /*
3538 * MHKV 0x100 in A31, R40, R40e,
3539 * T4x, X31, and later
3540 */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003541
3542 /* Paranoia check AND init hotkey_all_mask */
3543 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3544 "MHKA", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003545 pr_err("missing MHKA handler, please report this to %s\n",
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003546 TPACPI_MAIL);
3547 /* Fallback: pre-init for FN+F3,F4,F12 */
3548 hotkey_all_mask = 0x080cU;
3549 } else {
3550 tp_features.hotkey_mask = 1;
3551 }
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003552 break;
3553
3554 case 2:
3555 /*
3556 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3557 */
3558
3559 /* Paranoia check AND init hotkey_all_mask */
3560 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3561 "MHKA", "dd", 1)) {
3562 pr_err("missing MHKA handler, please report this to %s\n",
3563 TPACPI_MAIL);
3564 /* Fallback: pre-init for FN+F3,F4,F12 */
3565 hotkey_all_mask = 0x080cU;
3566 } else {
3567 tp_features.hotkey_mask = 1;
3568 }
3569
3570 /*
3571 * Check if we have an adaptive keyboard, like on the
3572 * Lenovo Carbon X1 2014 (2nd Gen).
3573 */
3574 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3575 "MHKA", "dd", 2)) {
3576 if (hotkey_adaptive_all_mask != 0) {
3577 tp_features.has_adaptive_kbd = true;
3578 res = sysfs_create_group(
3579 &tpacpi_pdev->dev.kobj,
3580 &adaptive_kbd_attr_group);
3581 if (res)
3582 goto err_exit;
3583 }
3584 } else {
3585 tp_features.has_adaptive_kbd = false;
3586 hotkey_adaptive_all_mask = 0x0U;
3587 }
3588 break;
3589
3590 default:
3591 pr_err("unknown version of the HKEY interface: 0x%x\n",
3592 hkeyv);
3593 pr_err("please report this to %s\n", TPACPI_MAIL);
3594 break;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003595 }
3596 }
3597
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003598 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3599 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003600 str_supported(tp_features.hotkey_mask));
3601
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003602 /* Init hotkey_all_mask if not initialized yet */
3603 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3604 (quirks & TPACPI_HK_Q_INIMASK))
3605 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003606
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003607 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003608 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003609 /* hotkey_source_mask *must* be zero for
3610 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003611 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003612 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003613 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003614
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003615 hotkey_orig_mask = hotkey_acpi_mask;
3616 } else {
3617 hotkey_orig_mask = hotkey_all_mask;
3618 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003619 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003620
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003621#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3622 if (dbg_wlswemul) {
3623 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003624 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003625 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003626 } else
3627#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003628 /* Not all thinkpads have a hardware radio switch */
3629 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3630 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003631 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003632 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003633 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003634 }
3635 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003636 res = add_to_attr_set(hotkey_dev_attributes,
3637 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003638
Lyudeb3180022016-11-11 15:15:02 -05003639 res = hotkey_init_tablet_mode();
3640 if (res < 0)
3641 goto err_exit;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003642
Lyudeb3180022016-11-11 15:15:02 -05003643 tabletsw_state = res;
3644
3645 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3646 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003647 if (res)
3648 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003649
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003650 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003651 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3652 GFP_KERNEL);
3653 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003654 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003655 res = -ENOMEM;
3656 goto err_exit;
3657 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003658
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003659 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3660 ARRAY_SIZE(tpacpi_keymap_qtable));
3661 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3662 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3663 "using keymap number %lu\n", keymap_id);
3664
3665 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3666 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003667
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003668 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003669 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3670 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3671 tpacpi_inputdev->keycode = hotkey_keycode_map;
3672 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3673 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003674 input_set_capability(tpacpi_inputdev, EV_KEY,
3675 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003676 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003677 if (i < sizeof(hotkey_reserved_mask)*8)
3678 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003679 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003680 }
3681
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003682 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003683 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003684 input_report_switch(tpacpi_inputdev,
3685 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003686 }
3687 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003688 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003689 input_report_switch(tpacpi_inputdev,
3690 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003691 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003692
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003693 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003694 * userspace. tpacpi_detect_brightness_capabilities() must have
3695 * been called before this point */
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02003696 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003697 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3698 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003699
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003700 /* Disable brightness up/down on Lenovo thinkpads when
3701 * ACPI is handling them, otherwise it is plain impossible
3702 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003703 hotkey_reserved_mask |=
3704 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3705 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003706 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3707 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003708 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003709
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003710#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003711 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3712 & ~hotkey_all_mask
3713 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003714
3715 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3716 "hotkey source mask 0x%08x, polling freq %u\n",
3717 hotkey_source_mask, hotkey_poll_freq);
3718#endif
3719
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003720 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3721 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003722 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003723 if (res) {
3724 hotkey_exit();
3725 return res;
3726 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003727 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3728 | hotkey_driver_mask)
3729 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003730 if (res < 0 && res != -ENXIO) {
3731 hotkey_exit();
3732 return res;
3733 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003734 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3735 & ~hotkey_reserved_mask;
3736 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3737 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3738 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003739
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003740 tpacpi_inputdev->open = &hotkey_inputdev_open;
3741 tpacpi_inputdev->close = &hotkey_inputdev_close;
3742
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003743 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003744
3745 return 0;
3746
3747err_exit:
3748 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
Bastien Nocerab790cee2015-03-02 14:45:27 +01003749 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3750 &adaptive_kbd_attr_group);
3751
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003752 hotkey_dev_attributes = NULL;
3753
Jan van den Berg72a979f2014-09-17 00:01:08 +02003754 return (res < 0) ? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003755}
3756
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003757/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3758 * mode, Web conference mode, Function mode and Lay-flat mode.
3759 * We support Home mode and Function mode currently.
3760 *
3761 * Will consider support rest of modes in future.
3762 *
3763 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003764static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003765 HOME_MODE,
3766/* WEB_BROWSER_MODE = 2,
3767 WEB_CONFERENCE_MODE = 3, */
3768 FUNCTION_MODE
3769};
3770
3771#define DFR_CHANGE_ROW 0x101
3772#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003773#define FIRST_ADAPTIVE_KEY 0x103
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003774
3775/* press Fn key a while second, it will switch to Function Mode. Then
3776 * release Fn key, previous mode be restored.
3777 */
3778static bool adaptive_keyboard_mode_is_saved;
3779static int adaptive_keyboard_prev_mode;
3780
Bastien Noceraf74587f2015-03-02 14:45:22 +01003781static int adaptive_keyboard_get_mode(void)
3782{
3783 int mode = 0;
3784
3785 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3786 pr_err("Cannot read adaptive keyboard mode\n");
3787 return -EIO;
3788 }
3789
3790 return mode;
3791}
3792
3793static int adaptive_keyboard_set_mode(int new_mode)
3794{
3795 if (new_mode < 0 ||
3796 new_mode > LAYFLAT_MODE)
3797 return -EINVAL;
3798
3799 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3800 pr_err("Cannot set adaptive keyboard mode\n");
3801 return -EIO;
3802 }
3803
3804 return 0;
3805}
3806
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003807static int adaptive_keyboard_get_next_mode(int mode)
3808{
3809 size_t i;
3810 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3811
3812 for (i = 0; i <= max_mode; i++) {
3813 if (adaptive_keyboard_modes[i] == mode)
3814 break;
3815 }
3816
3817 if (i >= max_mode)
3818 i = 0;
3819 else
3820 i++;
3821
3822 return adaptive_keyboard_modes[i];
3823}
3824
3825static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3826{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003827 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003828 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003829 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003830
3831 switch (scancode) {
3832 case DFR_CHANGE_ROW:
3833 if (adaptive_keyboard_mode_is_saved) {
3834 new_mode = adaptive_keyboard_prev_mode;
3835 adaptive_keyboard_mode_is_saved = false;
3836 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003837 current_mode = adaptive_keyboard_get_mode();
3838 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003839 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003840 new_mode = adaptive_keyboard_get_next_mode(
3841 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003842 }
3843
Bastien Noceraf74587f2015-03-02 14:45:22 +01003844 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003845 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003846
3847 return true;
3848
3849 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003850 current_mode = adaptive_keyboard_get_mode();
3851 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003852 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003853
Bastien Noceraf74587f2015-03-02 14:45:22 +01003854 adaptive_keyboard_prev_mode = current_mode;
3855 adaptive_keyboard_mode_is_saved = true;
3856
3857 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3858 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003859 return true;
3860
3861 default:
Dan Carpenter741d98c2015-03-11 12:36:07 +03003862 if (scancode < FIRST_ADAPTIVE_KEY ||
Christian Kellner696c65232017-02-28 17:10:57 +01003863 scancode >= FIRST_ADAPTIVE_KEY +
3864 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3865 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
Bastien Nocera6a68d852015-03-02 14:45:31 +01003866 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003867 scancode);
Bastien Nocera6a68d852015-03-02 14:45:31 +01003868 return false;
3869 }
Christian Kellner149c8c72017-02-28 17:10:56 +01003870 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3871 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
Bastien Nocera6a68d852015-03-02 14:45:31 +01003872 if (keycode != KEY_RESERVED) {
3873 mutex_lock(&tpacpi_inputdev_send_mutex);
3874
3875 input_report_key(tpacpi_inputdev, keycode, 1);
3876 input_sync(tpacpi_inputdev);
3877
3878 input_report_key(tpacpi_inputdev, keycode, 0);
3879 input_sync(tpacpi_inputdev);
3880
3881 mutex_unlock(&tpacpi_inputdev_send_mutex);
3882 }
3883 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003884 }
3885}
3886
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003887static bool hotkey_notify_hotkey(const u32 hkey,
3888 bool *send_acpi_ev,
3889 bool *ignore_acpi_ev)
3890{
3891 /* 0x1000-0x1FFF: key presses */
3892 unsigned int scancode = hkey & 0xfff;
3893 *send_acpi_ev = true;
3894 *ignore_acpi_ev = false;
3895
Christian Kellner696c65232017-02-28 17:10:57 +01003896 /*
3897 * Original events are in the 0x10XX range, the adaptive keyboard
3898 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3899 * models, additional keys are emitted through 0x13XX.
3900 */
3901 switch ((hkey >> 8) & 0xf) {
3902 case 0:
3903 if (scancode > 0 &&
3904 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3905 /* HKEY event 0x1001 is scancode 0x00 */
3906 scancode--;
3907 if (!(hotkey_source_mask & (1 << scancode))) {
3908 tpacpi_input_send_key_masked(scancode);
3909 *send_acpi_ev = false;
3910 } else {
3911 *ignore_acpi_ev = true;
3912 }
3913 return true;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003914 }
Christian Kellner696c65232017-02-28 17:10:57 +01003915 break;
3916
3917 case 1:
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003918 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Christian Kellner696c65232017-02-28 17:10:57 +01003919
3920 case 3:
3921 /* Extended keycodes start at 0x300 and our offset into the map
3922 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3923 * will be positive, but might not be in the correct range.
3924 */
3925 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3926 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3927 scancode < TPACPI_HOTKEY_MAP_LEN) {
3928 tpacpi_input_send_key(scancode);
3929 return true;
3930 }
3931 break;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003932 }
Christian Kellner696c65232017-02-28 17:10:57 +01003933
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003934 return false;
3935}
3936
3937static bool hotkey_notify_wakeup(const u32 hkey,
3938 bool *send_acpi_ev,
3939 bool *ignore_acpi_ev)
3940{
3941 /* 0x2000-0x2FFF: Wakeup reason */
3942 *send_acpi_ev = true;
3943 *ignore_acpi_ev = false;
3944
3945 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003946 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3947 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003948 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3949 *ignore_acpi_ev = true;
3950 break;
3951
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003952 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3953 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003954 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3955 *ignore_acpi_ev = true;
3956 break;
3957
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003958 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3959 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003960 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003961 /* how to auto-heal: */
3962 /* 2313: woke up from S3, go to S4/S5 */
3963 /* 2413: woke up from S4, go to S5 */
3964 break;
3965
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003966 default:
3967 return false;
3968 }
3969
3970 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003971 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003972 hotkey_wakeup_reason_notify_change();
3973 }
3974 return true;
3975}
3976
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003977static bool hotkey_notify_dockevent(const u32 hkey,
3978 bool *send_acpi_ev,
3979 bool *ignore_acpi_ev)
3980{
3981 /* 0x4000-0x4FFF: dock-related events */
3982 *send_acpi_ev = true;
3983 *ignore_acpi_ev = false;
3984
3985 switch (hkey) {
3986 case TP_HKEY_EV_UNDOCK_ACK:
3987 /* ACPI undock operation completed after wakeup */
3988 hotkey_autosleep_ack = 1;
3989 pr_info("undocked\n");
3990 hotkey_wakeup_hotunplug_complete_notify_change();
3991 return true;
3992
3993 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3994 pr_info("docked into hotplug port replicator\n");
3995 return true;
3996 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3997 pr_info("undocked from hotplug port replicator\n");
3998 return true;
3999
4000 default:
4001 return false;
4002 }
4003}
4004
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004005static bool hotkey_notify_usrevent(const u32 hkey,
4006 bool *send_acpi_ev,
4007 bool *ignore_acpi_ev)
4008{
4009 /* 0x5000-0x5FFF: human interface helpers */
4010 *send_acpi_ev = true;
4011 *ignore_acpi_ev = false;
4012
4013 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004014 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
4015 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004016 return true;
4017
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004018 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
4019 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004020 tpacpi_input_send_tabletsw();
4021 hotkey_tablet_mode_notify_change();
4022 *send_acpi_ev = false;
4023 return true;
4024
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004025 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
4026 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
4027 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03004028 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004029 *ignore_acpi_ev = true;
4030 return true;
4031
4032 default:
4033 return false;
4034 }
4035}
4036
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004037static void thermal_dump_all_sensors(void);
4038
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004039static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004040 bool *send_acpi_ev,
4041 bool *ignore_acpi_ev)
4042{
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004043 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004044 *send_acpi_ev = true;
4045 *ignore_acpi_ev = false;
4046
4047 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004048 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh6e6bc5f62018-04-24 16:56:03 -03004049 pr_debug("EC reports: Thermal Table has changed\n");
4050 /* recommended action: do nothing, we don't have
4051 * Lenovo ATM information */
4052 return true;
4053 case TP_HKEY_EV_THM_CSM_COMPLETED:
4054 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
4055 /* recommended action: do nothing, we don't have
4056 * Lenovo ATM information */
4057 return true;
4058 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
4059 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02004060 /* recommended action: do nothing, we don't have
4061 * Lenovo ATM information */
4062 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004063 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07004064 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004065 /* recommended action: warn user through gui */
4066 break;
4067 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07004068 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004069 /* recommended action: immediate sleep/hibernate */
4070 break;
4071 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004072 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004073 /* recommended action: warn user through gui, that */
4074 /* some internal component is too hot */
4075 break;
4076 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004077 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004078 /* recommended action: immediate sleep/hibernate */
4079 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01004080 case TP_HKEY_EV_AC_CHANGED:
4081 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4082 * AC status changed; can be triggered by plugging or
4083 * unplugging AC adapter, docking or undocking. */
4084
4085 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004086
4087 case TP_HKEY_EV_KEY_NUMLOCK:
4088 case TP_HKEY_EV_KEY_FN:
Xavier Naveira67ab6242015-02-10 08:45:18 +01004089 case TP_HKEY_EV_KEY_FN_ESC:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004090 /* key press events, we just ignore them as long as the EC
4091 * is still reporting them in the normal keyboard stream */
4092 *send_acpi_ev = false;
4093 *ignore_acpi_ev = true;
4094 return true;
4095
Lyudeb03f4d42016-11-11 15:15:03 -05004096 case TP_HKEY_EV_TABLET_CHANGED:
4097 tpacpi_input_send_tabletsw();
4098 hotkey_tablet_mode_notify_change();
4099 *send_acpi_ev = false;
Henrique de Moraes Holschuhedd1ed72018-04-24 16:56:04 -03004100 return true;
Lyudeb03f4d42016-11-11 15:15:03 -05004101
David Herrmann587d8622018-01-12 12:04:45 +01004102 case TP_HKEY_EV_PALM_DETECTED:
4103 case TP_HKEY_EV_PALM_UNDETECTED:
4104 /* palm detected hovering the keyboard, forward to user-space
4105 * via netlink for consumption */
4106 return true;
4107
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004108 default:
Henrique de Moraes Holschuhfd75ba22018-04-24 16:56:05 -03004109 /* report simply as unknown, no sensor dump */
4110 return false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004111 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004112
4113 thermal_dump_all_sensors();
Henrique de Moraes Holschuhfd75ba22018-04-24 16:56:05 -03004114 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004115}
4116
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004117static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4118{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004119 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004120 bool send_acpi_ev;
4121 bool ignore_acpi_ev;
4122 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004123
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004124 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07004125 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004126 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004127 acpi_bus_generate_netlink_event(
4128 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004129 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004130 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004131 return;
4132 }
4133
4134 while (1) {
4135 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07004136 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004137 return;
4138 }
4139
4140 if (hkey == 0) {
4141 /* queue empty */
4142 return;
4143 }
4144
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004145 send_acpi_ev = true;
4146 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004147
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004148 switch (hkey >> 12) {
4149 case 1:
4150 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004151 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4152 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004153 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004154 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004155 /* 0x2000-0x2FFF: Wakeup reason */
4156 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4157 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004158 break;
4159 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004160 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004161 switch (hkey) {
4162 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004163 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004164 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004165 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004166 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004167 break;
4168 case TP_HKEY_EV_OPTDRV_EJ:
4169 /* FIXME: kick libata if SATA link offline */
4170 known_ev = true;
4171 break;
4172 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004173 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004174 }
4175 break;
4176 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03004177 /* 0x4000-0x4FFF: dock-related events */
4178 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4179 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004180 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004181 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02004182 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004183 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4184 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004185 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004186 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004187 /* 0x6000-0x6FFF: thermal alarms/notices and
4188 * keyboard events */
4189 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004190 &ignore_acpi_ev);
4191 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004192 case 7:
4193 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004194 if (tp_features.hotkey_wlsw &&
4195 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004196 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004197 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004198 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004199 break;
4200 }
4201 /* fallthrough to default */
4202 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004203 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004204 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004205 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07004206 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004207 pr_notice("please report the conditions when this event happened to %s\n",
4208 TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004209 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004210
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004211 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03004212 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004213 acpi_bus_generate_netlink_event(
4214 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004215 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004216 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004217 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004218 }
4219}
4220
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02004221static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004222{
4223 /* Do these on suspend, we get the events on early resume! */
4224 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4225 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08004226
4227 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004228 if (tp_features.has_adaptive_kbd) {
4229 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4230 "GTRW", "dd", 0)) {
4231 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004232 }
4233 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004234}
4235
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004236static void hotkey_resume(void)
4237{
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03004238 tpacpi_disable_brightness_delay();
4239
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004240 if (hotkey_status_set(true) < 0 ||
4241 hotkey_mask_set(hotkey_acpi_mask) < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004242 pr_err("error while attempting to reset the event firmware interface\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004243
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004244 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02004245 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004246 hotkey_wakeup_reason_notify_change();
4247 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004248 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004249
4250 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004251 if (tp_features.has_adaptive_kbd) {
4252 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4253 adaptive_keyboard_prev_mode)) {
4254 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004255 }
4256 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004257}
4258
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004259/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004260static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004262 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004264 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004265 seq_printf(m, "status:\t\tnot supported\n");
4266 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004267 }
4268
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004269 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004270 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004271 res = hotkey_status_get(&status);
4272 if (!res)
4273 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004274 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004275 if (res)
4276 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004278 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004279 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004280 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4281 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004283 seq_printf(m, "mask:\t\tnot supported\n");
4284 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 }
4286
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288}
4289
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004290static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004291{
4292 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004293 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004294 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4295 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 +00004296}
4297
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004298static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004300 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004301 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004304 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 return -ENODEV;
4306
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004307 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004308 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004309
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004310 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004311
4312 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 while ((cmd = next_cmd(&buf))) {
4314 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004315 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004317 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004318 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03004320 mask = (hotkey_all_mask | hotkey_source_mask)
4321 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4323 /* mask set */
4324 } else if (sscanf(cmd, "%x", &mask) == 1) {
4325 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004326 } else {
4327 res = -EINVAL;
4328 goto errexit;
4329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004331
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004332 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004333 tpacpi_disclose_usertask("procfs hotkey",
4334 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004335 res = hotkey_user_mask_set(mask);
4336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004338errexit:
4339 mutex_unlock(&hotkey_mutex);
4340 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004341}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004343static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004344 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4345 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Hui Wanga3c42a42016-11-08 16:13:23 +08004346 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004347 {"", 0},
4348};
4349
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004350static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004351 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004352 .notify = hotkey_notify,
4353 .handle = &hkey_handle,
4354 .type = ACPI_DEVICE_NOTIFY,
4355};
4356
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004357static struct ibm_struct hotkey_driver_data = {
4358 .name = "hotkey",
4359 .read = hotkey_read,
4360 .write = hotkey_write,
4361 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004362 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004363 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004364 .acpi = &ibm_hotkey_acpidriver,
4365};
4366
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004367/*************************************************************************
4368 * Bluetooth subdriver
4369 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004371enum {
4372 /* ACPI GBDC/SBDC bits */
4373 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4374 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004375 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004376 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004377};
4378
4379enum {
4380 /* ACPI \BLTH commands */
4381 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4382 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4383 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4384 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4385 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004386};
4387
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004388#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4389
Johannes Berg19d337d2009-06-02 13:01:37 +02004390static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004391{
4392 int status;
4393
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004394#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4395 if (dbg_bluetoothemul)
4396 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004397 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004398#endif
4399
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004400 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4401 return -EIO;
4402
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004403 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004404 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004405}
4406
Johannes Berg19d337d2009-06-02 13:01:37 +02004407static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004408{
4409 int status;
4410
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004411 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004412 "will attempt to %s bluetooth\n",
4413 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004414
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004415#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4416 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004417 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004418 return 0;
4419 }
4420#endif
4421
Johannes Berg19d337d2009-06-02 13:01:37 +02004422 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004423 status = TP_ACPI_BLUETOOTH_RADIOSSW
4424 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4425 else
4426 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004427
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004428 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4429 return -EIO;
4430
4431 return 0;
4432}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004433
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004434/* sysfs bluetooth enable ---------------------------------------------- */
4435static ssize_t bluetooth_enable_show(struct device *dev,
4436 struct device_attribute *attr,
4437 char *buf)
4438{
Johannes Berg19d337d2009-06-02 13:01:37 +02004439 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4440 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004441}
4442
4443static ssize_t bluetooth_enable_store(struct device *dev,
4444 struct device_attribute *attr,
4445 const char *buf, size_t count)
4446{
Johannes Berg19d337d2009-06-02 13:01:37 +02004447 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4448 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004449}
4450
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004451static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004452
4453/* --------------------------------------------------------------------- */
4454
4455static struct attribute *bluetooth_attributes[] = {
4456 &dev_attr_bluetooth_enable.attr,
4457 NULL
4458};
4459
4460static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004461 .attrs = bluetooth_attributes,
4462};
4463
Johannes Berg19d337d2009-06-02 13:01:37 +02004464static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4465 .get_status = bluetooth_get_status,
4466 .set_status = bluetooth_set_status,
4467};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004468
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004469static void bluetooth_shutdown(void)
4470{
4471 /* Order firmware to save current state to NVRAM */
4472 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4473 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004474 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004475 else
4476 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004477 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004478}
4479
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004480static void bluetooth_exit(void)
4481{
4482 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4483 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004484
4485 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4486
4487 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004488}
4489
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004490static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004492 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004493 int status = 0;
4494
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004495 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4496 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004497
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004498 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004499
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004500 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4501 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004502 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004503 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004505 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4506 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004507 str_supported(tp_features.bluetooth),
4508 status);
4509
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004510#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4511 if (dbg_bluetoothemul) {
4512 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004513 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004514 } else
4515#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004516 if (tp_features.bluetooth &&
4517 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4518 /* no bluetooth hardware present in system */
4519 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004520 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004521 "bluetooth hardware not installed\n");
4522 }
4523
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004524 if (!tp_features.bluetooth)
4525 return 1;
4526
Johannes Berg19d337d2009-06-02 13:01:37 +02004527 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4528 &bluetooth_tprfk_ops,
4529 RFKILL_TYPE_BLUETOOTH,
4530 TPACPI_RFK_BLUETOOTH_SW_NAME,
4531 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004532 if (res)
4533 return res;
4534
Johannes Berg19d337d2009-06-02 13:01:37 +02004535 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4536 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004537 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004538 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004539 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004540 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004541
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543}
4544
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004545/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004546static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004548 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549}
4550
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004551static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552{
Johannes Berg19d337d2009-06-02 13:01:37 +02004553 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554}
4555
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004556static struct ibm_struct bluetooth_driver_data = {
4557 .name = "bluetooth",
4558 .read = bluetooth_read,
4559 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004560 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004561 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004562};
4563
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004564/*************************************************************************
4565 * Wan subdriver
4566 */
4567
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004568enum {
4569 /* ACPI GWAN/SWAN bits */
4570 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4571 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004572 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004573 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004574};
4575
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004576#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4577
Johannes Berg19d337d2009-06-02 13:01:37 +02004578static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004579{
4580 int status;
4581
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004582#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4583 if (dbg_wwanemul)
4584 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004585 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004586#endif
4587
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004588 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4589 return -EIO;
4590
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004591 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004592 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004593}
4594
Johannes Berg19d337d2009-06-02 13:01:37 +02004595static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004596{
4597 int status;
4598
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004599 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004600 "will attempt to %s wwan\n",
4601 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004602
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004603#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4604 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004605 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004606 return 0;
4607 }
4608#endif
4609
Johannes Berg19d337d2009-06-02 13:01:37 +02004610 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004611 status = TP_ACPI_WANCARD_RADIOSSW
4612 | TP_ACPI_WANCARD_RESUMECTRL;
4613 else
4614 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004615
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004616 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4617 return -EIO;
4618
4619 return 0;
4620}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004621
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004622/* sysfs wan enable ---------------------------------------------------- */
4623static ssize_t wan_enable_show(struct device *dev,
4624 struct device_attribute *attr,
4625 char *buf)
4626{
Johannes Berg19d337d2009-06-02 13:01:37 +02004627 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4628 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004629}
4630
4631static ssize_t wan_enable_store(struct device *dev,
4632 struct device_attribute *attr,
4633 const char *buf, size_t count)
4634{
Johannes Berg19d337d2009-06-02 13:01:37 +02004635 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4636 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004637}
4638
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004639static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4640 wan_enable_show, wan_enable_store);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004641
4642/* --------------------------------------------------------------------- */
4643
4644static struct attribute *wan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004645 &dev_attr_wwan_enable.attr,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004646 NULL
4647};
4648
4649static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004650 .attrs = wan_attributes,
4651};
4652
Johannes Berg19d337d2009-06-02 13:01:37 +02004653static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4654 .get_status = wan_get_status,
4655 .set_status = wan_set_status,
4656};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004657
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004658static void wan_shutdown(void)
4659{
4660 /* Order firmware to save current state to NVRAM */
4661 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4662 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004663 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004664 else
4665 vdbg_printk(TPACPI_DBG_RFKILL,
4666 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004667}
4668
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004669static void wan_exit(void)
4670{
4671 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4672 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004673
4674 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4675
4676 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004677}
4678
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004679static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004680{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004681 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004682 int status = 0;
4683
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004684 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4685 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004686
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004687 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004688
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004689 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004690 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004691
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004692 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4693 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004694 str_supported(tp_features.wan),
4695 status);
4696
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004697#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4698 if (dbg_wwanemul) {
4699 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004700 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004701 } else
4702#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004703 if (tp_features.wan &&
4704 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4705 /* no wan hardware present in system */
4706 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004707 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004708 "wan hardware not installed\n");
4709 }
4710
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004711 if (!tp_features.wan)
4712 return 1;
4713
Johannes Berg19d337d2009-06-02 13:01:37 +02004714 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4715 &wan_tprfk_ops,
4716 RFKILL_TYPE_WWAN,
4717 TPACPI_RFK_WWAN_SW_NAME,
4718 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004719 if (res)
4720 return res;
4721
Johannes Berg19d337d2009-06-02 13:01:37 +02004722 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4723 &wan_attr_group);
4724
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004725 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004726 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004727 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004728 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004729
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004730 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004731}
4732
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004733/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004734static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004735{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004736 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004737}
4738
4739static int wan_write(char *buf)
4740{
Johannes Berg19d337d2009-06-02 13:01:37 +02004741 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004742}
4743
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004744static struct ibm_struct wan_driver_data = {
4745 .name = "wan",
4746 .read = wan_read,
4747 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004748 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004749 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004750};
4751
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004752/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004753 * UWB subdriver
4754 */
4755
4756enum {
4757 /* ACPI GUWB/SUWB bits */
4758 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4759 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4760};
4761
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004762#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4763
Johannes Berg19d337d2009-06-02 13:01:37 +02004764static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004765{
4766 int status;
4767
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004768#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4769 if (dbg_uwbemul)
4770 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004771 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004772#endif
4773
4774 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4775 return -EIO;
4776
4777 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004778 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004779}
4780
Johannes Berg19d337d2009-06-02 13:01:37 +02004781static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004782{
4783 int status;
4784
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004785 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004786 "will attempt to %s UWB\n",
4787 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004788
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004789#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4790 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004791 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004792 return 0;
4793 }
4794#endif
4795
Johannes Berg19d337d2009-06-02 13:01:37 +02004796 if (state == TPACPI_RFK_RADIO_ON)
4797 status = TP_ACPI_UWB_RADIOSSW;
4798 else
4799 status = 0;
4800
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004801 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4802 return -EIO;
4803
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004804 return 0;
4805}
4806
4807/* --------------------------------------------------------------------- */
4808
Johannes Berg19d337d2009-06-02 13:01:37 +02004809static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4810 .get_status = uwb_get_status,
4811 .set_status = uwb_set_status,
4812};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004813
4814static void uwb_exit(void)
4815{
Johannes Berg19d337d2009-06-02 13:01:37 +02004816 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004817}
4818
4819static int __init uwb_init(struct ibm_init_struct *iibm)
4820{
4821 int res;
4822 int status = 0;
4823
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004824 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4825 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004826
4827 TPACPI_ACPIHANDLE_INIT(hkey);
4828
4829 tp_features.uwb = hkey_handle &&
4830 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4831
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004832 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4833 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004834 str_supported(tp_features.uwb),
4835 status);
4836
4837#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4838 if (dbg_uwbemul) {
4839 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004840 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004841 } else
4842#endif
4843 if (tp_features.uwb &&
4844 !(status & TP_ACPI_UWB_HWPRESENT)) {
4845 /* no uwb hardware present in system */
4846 tp_features.uwb = 0;
4847 dbg_printk(TPACPI_DBG_INIT,
4848 "uwb hardware not installed\n");
4849 }
4850
4851 if (!tp_features.uwb)
4852 return 1;
4853
4854 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004855 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004856 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004857 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004858 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004859 return res;
4860}
4861
4862static struct ibm_struct uwb_driver_data = {
4863 .name = "uwb",
4864 .exit = uwb_exit,
4865 .flags.experimental = 1,
4866};
4867
4868/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004869 * Video subdriver
4870 */
4871
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004872#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4873
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004874enum video_access_mode {
4875 TPACPI_VIDEO_NONE = 0,
4876 TPACPI_VIDEO_570, /* 570 */
4877 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4878 TPACPI_VIDEO_NEW, /* all others */
4879};
4880
4881enum { /* video status flags, based on VIDEO_570 */
4882 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4883 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4884 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4885};
4886
4887enum { /* TPACPI_VIDEO_570 constants */
4888 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4889 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4890 * video_status_flags */
4891 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4892 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4893};
4894
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004895static enum video_access_mode video_supported;
4896static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004897
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004898static int video_autosw_get(void);
4899static int video_autosw_set(int enable);
4900
Joe Perches112a6ee2011-04-04 10:06:24 -07004901TPACPI_HANDLE(vid, root,
4902 "\\_SB.PCI.AGP.VGA", /* 570 */
4903 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4904 "\\_SB.PCI0.VID0", /* 770e */
4905 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4906 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4907 "\\_SB.PCI0.AGP.VID", /* all others */
4908 ); /* R30, R31 */
4909
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004910TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004911
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004912static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004914 int ivga;
4915
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004916 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4917
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004918 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004919 if (tpacpi_is_ibm())
4920 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004921
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004922 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4923 /* G41, assume IVGA doesn't change */
4924 vid_handle = vid2_handle;
4925
4926 if (!vid_handle)
4927 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004928 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004929 else if (tpacpi_is_ibm() &&
4930 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004931 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004932 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004933 else if (tpacpi_is_ibm() &&
4934 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004935 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004936 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004937 else
4938 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004939 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004941 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4942 str_supported(video_supported != TPACPI_VIDEO_NONE),
4943 video_supported);
4944
Jan van den Berg72a979f2014-09-17 00:01:08 +02004945 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946}
4947
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004948static void video_exit(void)
4949{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004950 dbg_printk(TPACPI_DBG_EXIT,
4951 "restoring original video autoswitch mode\n");
4952 if (video_autosw_set(video_orig_autosw))
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004953 pr_err("error while trying to restore original video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004954}
4955
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004956static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957{
4958 int status = 0;
4959 int i;
4960
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004961 switch (video_supported) {
4962 case TPACPI_VIDEO_570:
4963 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4964 TP_ACPI_VIDEO_570_PHSCMD))
4965 return -EIO;
4966 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4967 break;
4968 case TPACPI_VIDEO_770:
4969 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4970 return -EIO;
4971 if (i)
4972 status |= TP_ACPI_VIDEO_S_LCD;
4973 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4974 return -EIO;
4975 if (i)
4976 status |= TP_ACPI_VIDEO_S_CRT;
4977 break;
4978 case TPACPI_VIDEO_NEW:
4979 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4980 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4981 return -EIO;
4982 if (i)
4983 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004985 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4986 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4987 return -EIO;
4988 if (i)
4989 status |= TP_ACPI_VIDEO_S_LCD;
4990 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4991 return -EIO;
4992 if (i)
4993 status |= TP_ACPI_VIDEO_S_DVI;
4994 break;
4995 default:
4996 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
4999 return status;
5000}
5001
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005002static int video_outputsw_set(int status)
5003{
5004 int autosw;
5005 int res = 0;
5006
5007 switch (video_supported) {
5008 case TPACPI_VIDEO_570:
5009 res = acpi_evalf(NULL, NULL,
5010 "\\_SB.PHS2", "vdd",
5011 TP_ACPI_VIDEO_570_PHS2CMD,
5012 status | TP_ACPI_VIDEO_570_PHS2SET);
5013 break;
5014 case TPACPI_VIDEO_770:
5015 autosw = video_autosw_get();
5016 if (autosw < 0)
5017 return autosw;
5018
5019 res = video_autosw_set(1);
5020 if (res)
5021 return res;
5022 res = acpi_evalf(vid_handle, NULL,
5023 "ASWT", "vdd", status * 0x100, 0);
5024 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07005025 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005026 return -EIO;
5027 }
5028 break;
5029 case TPACPI_VIDEO_NEW:
5030 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005031 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005032 break;
5033 default:
5034 return -ENOSYS;
5035 }
5036
Jan van den Berg72a979f2014-09-17 00:01:08 +02005037 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005038}
5039
5040static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005042 int autosw = 0;
5043
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005044 switch (video_supported) {
5045 case TPACPI_VIDEO_570:
5046 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5047 return -EIO;
5048 break;
5049 case TPACPI_VIDEO_770:
5050 case TPACPI_VIDEO_NEW:
5051 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5052 return -EIO;
5053 break;
5054 default:
5055 return -ENOSYS;
5056 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005057
5058 return autosw & 1;
5059}
5060
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005061static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005062{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005063 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005064 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005065 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005066}
5067
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005068static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005069{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005070 int autosw = video_autosw_get();
5071 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005072
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005073 if (autosw < 0)
5074 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005075
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005076 switch (video_supported) {
5077 case TPACPI_VIDEO_570:
5078 res = video_autosw_set(1);
5079 if (res)
5080 return res;
5081 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5082 break;
5083 case TPACPI_VIDEO_770:
5084 case TPACPI_VIDEO_NEW:
5085 res = video_autosw_set(1);
5086 if (res)
5087 return res;
5088 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5089 break;
5090 default:
5091 return -ENOSYS;
5092 }
5093 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07005094 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005095 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005096 }
5097
Jan van den Berg72a979f2014-09-17 00:01:08 +02005098 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005099}
5100
5101static int video_expand_toggle(void)
5102{
5103 switch (video_supported) {
5104 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005105 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005106 0 : -EIO;
5107 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005108 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005109 0 : -EIO;
5110 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005111 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005112 0 : -EIO;
5113 default:
5114 return -ENOSYS;
5115 }
5116 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005117}
5118
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005119static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005120{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005121 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005122
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005123 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005124 seq_printf(m, "status:\t\tnot supported\n");
5125 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005126 }
5127
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005128 /* Even reads can crash X.org, so... */
5129 if (!capable(CAP_SYS_ADMIN))
5130 return -EPERM;
5131
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005132 status = video_outputsw_get();
5133 if (status < 0)
5134 return status;
5135
5136 autosw = video_autosw_get();
5137 if (autosw < 0)
5138 return autosw;
5139
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005140 seq_printf(m, "status:\t\tsupported\n");
5141 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5142 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005143 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005144 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5145 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5146 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5147 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005148 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005149 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5150 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5151 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005152
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005153 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005154}
5155
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005156static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157{
5158 char *cmd;
5159 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005160 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005162 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005163 return -ENODEV;
5164
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005165 /* Even reads can crash X.org, let alone writes... */
5166 if (!capable(CAP_SYS_ADMIN))
5167 return -EPERM;
5168
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005169 enable = 0;
5170 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
5172 while ((cmd = next_cmd(&buf))) {
5173 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005174 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005176 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005178 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005180 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005181 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005182 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005183 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005184 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005185 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005186 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005188 res = video_autosw_set(1);
5189 if (res)
5190 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005192 res = video_autosw_set(0);
5193 if (res)
5194 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005196 res = video_outputsw_cycle();
5197 if (res)
5198 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005200 res = video_expand_toggle();
5201 if (res)
5202 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 } else
5204 return -EINVAL;
5205 }
5206
5207 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005208 status = video_outputsw_get();
5209 if (status < 0)
5210 return status;
5211 res = video_outputsw_set((status & ~disable) | enable);
5212 if (res)
5213 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 }
5215
5216 return 0;
5217}
5218
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005219static struct ibm_struct video_driver_data = {
5220 .name = "video",
5221 .read = video_read,
5222 .write = video_write,
5223 .exit = video_exit,
5224};
5225
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005226#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5227
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005228/*************************************************************************
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005229 * Keyboard backlight subdriver
5230 */
5231
Hans de Goedec685e202017-02-09 16:44:13 +01005232static enum led_brightness kbdlight_brightness;
5233static DEFINE_MUTEX(kbdlight_mutex);
5234
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005235static int kbdlight_set_level(int level)
5236{
Hans de Goedec685e202017-02-09 16:44:13 +01005237 int ret = 0;
5238
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005239 if (!hkey_handle)
5240 return -ENXIO;
5241
Hans de Goedec685e202017-02-09 16:44:13 +01005242 mutex_lock(&kbdlight_mutex);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005243
Hans de Goedec685e202017-02-09 16:44:13 +01005244 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5245 ret = -EIO;
5246 else
5247 kbdlight_brightness = level;
5248
5249 mutex_unlock(&kbdlight_mutex);
5250
5251 return ret;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005252}
5253
5254static int kbdlight_get_level(void)
5255{
5256 int status = 0;
5257
5258 if (!hkey_handle)
5259 return -ENXIO;
5260
5261 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5262 return -EIO;
5263
5264 if (status < 0)
5265 return status;
5266
5267 return status & 0x3;
5268}
5269
5270static bool kbdlight_is_supported(void)
5271{
5272 int status = 0;
5273
5274 if (!hkey_handle)
5275 return false;
5276
5277 if (!acpi_has_method(hkey_handle, "MLCG")) {
5278 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5279 return false;
5280 }
5281
5282 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5283 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5284 return false;
5285 }
5286
5287 if (status < 0) {
5288 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5289 return false;
5290 }
5291
5292 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5293 /*
5294 * Guessed test for keyboard backlight:
5295 *
5296 * Machines with backlight keyboard return:
5297 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5298 * b110100010010000000XX - ThinkPad x230
5299 * b010100000010000000XX - ThinkPad x240
5300 * b010100000010000000XX - ThinkPad W541
5301 * (XX is current backlight level)
5302 *
5303 * Machines without backlight keyboard return:
5304 * b10100001000000000000 - ThinkPad x230
5305 * b10110001000000000000 - ThinkPad E430
5306 * b00000000000000000000 - ThinkPad E450
5307 *
5308 * Candidate BITs for detection test (XOR):
5309 * b01000000001000000000
5310 * ^
5311 */
5312 return status & BIT(9);
5313}
5314
Hans de Goede86ec0c22017-02-09 16:44:12 +01005315static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005316 enum led_brightness brightness)
5317{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005318 return kbdlight_set_level(brightness);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005319}
5320
5321static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5322{
5323 int level;
5324
5325 level = kbdlight_get_level();
5326 if (level < 0)
5327 return 0;
5328
5329 return level;
5330}
5331
5332static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5333 .led_classdev = {
5334 .name = "tpacpi::kbd_backlight",
5335 .max_brightness = 2,
Hans de Goedec685e202017-02-09 16:44:13 +01005336 .flags = LED_BRIGHT_HW_CHANGED,
Hans de Goede86ec0c22017-02-09 16:44:12 +01005337 .brightness_set_blocking = &kbdlight_sysfs_set,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005338 .brightness_get = &kbdlight_sysfs_get,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005339 }
5340};
5341
5342static int __init kbdlight_init(struct ibm_init_struct *iibm)
5343{
5344 int rc;
5345
5346 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5347
5348 TPACPI_ACPIHANDLE_INIT(hkey);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005349
5350 if (!kbdlight_is_supported()) {
5351 tp_features.kbdlight = 0;
5352 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5353 return 1;
5354 }
5355
Hans de Goedec685e202017-02-09 16:44:13 +01005356 kbdlight_brightness = kbdlight_sysfs_get(NULL);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005357 tp_features.kbdlight = 1;
5358
5359 rc = led_classdev_register(&tpacpi_pdev->dev,
5360 &tpacpi_led_kbdlight.led_classdev);
5361 if (rc < 0) {
5362 tp_features.kbdlight = 0;
5363 return rc;
5364 }
5365
Hans de Goedec685e202017-02-09 16:44:13 +01005366 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5367 TP_ACPI_HKEY_KBD_LIGHT_MASK);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005368 return 0;
5369}
5370
5371static void kbdlight_exit(void)
5372{
5373 if (tp_features.kbdlight)
5374 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005375}
5376
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005377static int kbdlight_set_level_and_update(int level)
5378{
5379 int ret;
5380 struct led_classdev *led_cdev;
5381
5382 ret = kbdlight_set_level(level);
5383 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5384
5385 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5386 led_cdev->brightness = level;
5387
5388 return ret;
5389}
5390
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005391static int kbdlight_read(struct seq_file *m)
5392{
5393 int level;
5394
5395 if (!tp_features.kbdlight) {
5396 seq_printf(m, "status:\t\tnot supported\n");
5397 } else {
5398 level = kbdlight_get_level();
5399 if (level < 0)
5400 seq_printf(m, "status:\t\terror %d\n", level);
5401 else
5402 seq_printf(m, "status:\t\t%d\n", level);
5403 seq_printf(m, "commands:\t0, 1, 2\n");
5404 }
5405
5406 return 0;
5407}
5408
5409static int kbdlight_write(char *buf)
5410{
5411 char *cmd;
5412 int level = -1;
5413
5414 if (!tp_features.kbdlight)
5415 return -ENODEV;
5416
5417 while ((cmd = next_cmd(&buf))) {
5418 if (strlencmp(cmd, "0") == 0)
5419 level = 0;
5420 else if (strlencmp(cmd, "1") == 0)
5421 level = 1;
5422 else if (strlencmp(cmd, "2") == 0)
5423 level = 2;
5424 else
5425 return -EINVAL;
5426 }
5427
5428 if (level == -1)
5429 return -EINVAL;
5430
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005431 return kbdlight_set_level_and_update(level);
5432}
5433
5434static void kbdlight_suspend(void)
5435{
5436 struct led_classdev *led_cdev;
5437
5438 if (!tp_features.kbdlight)
5439 return;
5440
5441 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5442 led_update_brightness(led_cdev);
5443 led_classdev_suspend(led_cdev);
5444}
5445
5446static void kbdlight_resume(void)
5447{
5448 if (!tp_features.kbdlight)
5449 return;
5450
5451 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005452}
5453
5454static struct ibm_struct kbdlight_driver_data = {
5455 .name = "kbdlight",
5456 .read = kbdlight_read,
5457 .write = kbdlight_write,
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005458 .suspend = kbdlight_suspend,
5459 .resume = kbdlight_resume,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005460 .exit = kbdlight_exit,
5461};
5462
5463/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005464 * Light (thinklight) subdriver
5465 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005467TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5468TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005469
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005470static int light_get_status(void)
5471{
5472 int status = 0;
5473
5474 if (tp_features.light_status) {
5475 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5476 return -EIO;
5477 return (!!status);
5478 }
5479
5480 return -ENXIO;
5481}
5482
5483static int light_set_status(int status)
5484{
5485 int rc;
5486
5487 if (tp_features.light) {
5488 if (cmos_handle) {
5489 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005490 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005491 TP_CMOS_THINKLIGHT_ON :
5492 TP_CMOS_THINKLIGHT_OFF);
5493 } else {
5494 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005495 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005496 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005497 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005498 }
5499
5500 return -ENXIO;
5501}
5502
Hans de Goede86ec0c22017-02-09 16:44:12 +01005503static int light_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005504 enum led_brightness brightness)
5505{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005506 return light_set_status((brightness != LED_OFF) ?
5507 TPACPI_LED_ON : TPACPI_LED_OFF);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005508}
5509
5510static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5511{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005512 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005513}
5514
5515static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5516 .led_classdev = {
5517 .name = "tpacpi::thinklight",
Hans de Goede86ec0c22017-02-09 16:44:12 +01005518 .brightness_set_blocking = &light_sysfs_set,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005519 .brightness_get = &light_sysfs_get,
5520 }
5521};
5522
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005523static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005525 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005526
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005527 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5528
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005529 if (tpacpi_is_ibm()) {
5530 TPACPI_ACPIHANDLE_INIT(ledb);
5531 TPACPI_ACPIHANDLE_INIT(lght);
5532 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005533 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005534
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005535 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005536 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005537
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005538 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005539 /* light status not supported on
5540 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005541 tp_features.light_status =
5542 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005544 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5545 str_supported(tp_features.light),
5546 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005547
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005548 if (!tp_features.light)
5549 return 1;
5550
5551 rc = led_classdev_register(&tpacpi_pdev->dev,
5552 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005553
5554 if (rc < 0) {
5555 tp_features.light = 0;
5556 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005557 } else {
5558 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005559 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005560
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005561 return rc;
5562}
5563
5564static void light_exit(void)
5565{
5566 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567}
5568
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005569static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005571 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005573 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005574 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005575 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005576 seq_printf(m, "status:\t\tunknown\n");
5577 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005578 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005579 status = light_get_status();
5580 if (status < 0)
5581 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005582 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5583 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587}
5588
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005589static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005592 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005593
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005594 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005595 return -ENODEV;
5596
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 while ((cmd = next_cmd(&buf))) {
5598 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005599 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005601 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 } else
5603 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 }
5605
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005606 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607}
5608
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005609static struct ibm_struct light_driver_data = {
5610 .name = "light",
5611 .read = light_read,
5612 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005613 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005614};
5615
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005616/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005617 * CMOS subdriver
5618 */
5619
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005620/* sysfs cmos_command -------------------------------------------------- */
5621static ssize_t cmos_command_store(struct device *dev,
5622 struct device_attribute *attr,
5623 const char *buf, size_t count)
5624{
5625 unsigned long cmos_cmd;
5626 int res;
5627
5628 if (parse_strtoul(buf, 21, &cmos_cmd))
5629 return -EINVAL;
5630
5631 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005632 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005633}
5634
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01005635static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005636
5637/* --------------------------------------------------------------------- */
5638
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005639static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005640{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005641 int res;
5642
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005643 vdbg_printk(TPACPI_DBG_INIT,
5644 "initializing cmos commands subdriver\n");
5645
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005646 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005647
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005648 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5649 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005650
5651 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5652 if (res)
5653 return res;
5654
Jan van den Berg72a979f2014-09-17 00:01:08 +02005655 return (cmos_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005656}
5657
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005658static void cmos_exit(void)
5659{
5660 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5661}
5662
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005663static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005665 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5666 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005668 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005670 seq_printf(m, "status:\t\tsupported\n");
5671 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 }
5673
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675}
5676
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005677static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678{
5679 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005680 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
5682 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005683 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5684 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 /* cmos_cmd set */
5686 } else
5687 return -EINVAL;
5688
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005689 res = issue_thinkpad_cmos_command(cmos_cmd);
5690 if (res)
5691 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 }
5693
5694 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005695}
5696
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005697static struct ibm_struct cmos_driver_data = {
5698 .name = "cmos",
5699 .read = cmos_read,
5700 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005701 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005702};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005703
5704/*************************************************************************
5705 * LED subdriver
5706 */
5707
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005708enum led_access_mode {
5709 TPACPI_LED_NONE = 0,
5710 TPACPI_LED_570, /* 570 */
5711 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5712 TPACPI_LED_NEW, /* all others */
5713};
5714
5715enum { /* For TPACPI_LED_OLD */
5716 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5717 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5718 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5719};
5720
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005721static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005722
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005723static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005724
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005725#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005726static struct tpacpi_led_classdev *tpacpi_leds;
5727static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005728static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005729 /* there's a limit of 19 chars + NULL before 2.6.26 */
5730 "tpacpi::power",
5731 "tpacpi:orange:batt",
5732 "tpacpi:green:batt",
5733 "tpacpi::dock_active",
5734 "tpacpi::bay_active",
5735 "tpacpi::dock_batt",
5736 "tpacpi::unknown_led",
5737 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005738 "tpacpi::dock_status1",
5739 "tpacpi::dock_status2",
5740 "tpacpi::unknown_led2",
5741 "tpacpi::unknown_led3",
5742 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005743};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005744#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005745
5746static inline bool tpacpi_is_led_restricted(const unsigned int led)
5747{
5748#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5749 return false;
5750#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005751 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005752#endif
5753}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005754
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005755static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005756{
5757 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005758 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005759
5760 switch (led_supported) {
5761 case TPACPI_LED_570:
5762 if (!acpi_evalf(ec_handle,
5763 &status, "GLED", "dd", 1 << led))
5764 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005765 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005766 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005767 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005768 TPACPI_LED_ON :
5769 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005770 tpacpi_led_state_cache[led] = led_s;
5771 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005772 default:
5773 return -ENXIO;
5774 }
5775
5776 /* not reached */
5777}
5778
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005779static int led_set_status(const unsigned int led,
5780 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005781{
5782 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005783 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5784 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005785
5786 int rc = 0;
5787
5788 switch (led_supported) {
5789 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005790 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005791 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005792 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005793 if (unlikely(tpacpi_is_led_restricted(led)))
5794 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005795 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5796 (1 << led), led_sled_arg1[ledstatus]))
5797 rc = -EIO;
5798 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005799 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005800 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005801 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005802 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005803 if (unlikely(tpacpi_is_led_restricted(led)))
5804 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005805 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5806 if (rc >= 0)
5807 rc = ec_write(TPACPI_LED_EC_HLBL,
5808 (ledstatus == TPACPI_LED_BLINK) << led);
5809 if (rc >= 0)
5810 rc = ec_write(TPACPI_LED_EC_HLCL,
5811 (ledstatus != TPACPI_LED_OFF) << led);
5812 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005813 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005814 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005815 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5816 return -EINVAL;
5817 if (unlikely(tpacpi_is_led_restricted(led)))
5818 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005819 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5820 led, led_led_arg1[ledstatus]))
5821 rc = -EIO;
5822 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005823 default:
5824 rc = -ENXIO;
5825 }
5826
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005827 if (!rc)
5828 tpacpi_led_state_cache[led] = ledstatus;
5829
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005830 return rc;
5831}
5832
Hans de Goede86ec0c22017-02-09 16:44:12 +01005833static int led_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005834 enum led_brightness brightness)
5835{
5836 struct tpacpi_led_classdev *data = container_of(led_cdev,
5837 struct tpacpi_led_classdev, led_classdev);
Hans de Goede86ec0c22017-02-09 16:44:12 +01005838 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005839
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005840 if (brightness == LED_OFF)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005841 new_state = TPACPI_LED_OFF;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005842 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005843 new_state = TPACPI_LED_ON;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005844 else
Hans de Goede86ec0c22017-02-09 16:44:12 +01005845 new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005846
Hans de Goede86ec0c22017-02-09 16:44:12 +01005847 return led_set_status(data->led, new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005848}
5849
5850static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5851 unsigned long *delay_on, unsigned long *delay_off)
5852{
5853 struct tpacpi_led_classdev *data = container_of(led_cdev,
5854 struct tpacpi_led_classdev, led_classdev);
5855
5856 /* Can we choose the flash rate? */
5857 if (*delay_on == 0 && *delay_off == 0) {
5858 /* yes. set them to the hardware blink rate (1 Hz) */
5859 *delay_on = 500; /* ms */
5860 *delay_off = 500; /* ms */
5861 } else if ((*delay_on != 500) || (*delay_off != 500))
5862 return -EINVAL;
5863
Hans de Goede86ec0c22017-02-09 16:44:12 +01005864 return led_set_status(data->led, TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005865}
5866
5867static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5868{
5869 int rc;
5870
5871 struct tpacpi_led_classdev *data = container_of(led_cdev,
5872 struct tpacpi_led_classdev, led_classdev);
5873
5874 rc = led_get_status(data->led);
5875
5876 if (rc == TPACPI_LED_OFF || rc < 0)
5877 rc = LED_OFF; /* no error handling in led class :( */
5878 else
5879 rc = LED_FULL;
5880
5881 return rc;
5882}
5883
5884static void led_exit(void)
5885{
5886 unsigned int i;
5887
5888 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5889 if (tpacpi_leds[i].led_classdev.name)
5890 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5891 }
5892
5893 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005894}
5895
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005896static int __init tpacpi_init_led(unsigned int led)
5897{
5898 int rc;
5899
5900 tpacpi_leds[led].led = led;
5901
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005902 /* LEDs with no name don't get registered */
5903 if (!tpacpi_led_names[led])
5904 return 0;
5905
Hans de Goede86ec0c22017-02-09 16:44:12 +01005906 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005907 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5908 if (led_supported == TPACPI_LED_570)
5909 tpacpi_leds[led].led_classdev.brightness_get =
5910 &led_sysfs_get;
5911
5912 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5913
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005914 rc = led_classdev_register(&tpacpi_pdev->dev,
5915 &tpacpi_leds[led].led_classdev);
5916 if (rc < 0)
5917 tpacpi_leds[led].led_classdev.name = NULL;
5918
5919 return rc;
5920}
5921
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005922static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5923 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5924 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5925 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5926
5927 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5928 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5929 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5930 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5931 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5932 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5933 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5934 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5935
5936 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5937 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5938 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5939 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5940 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5941
5942 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5943 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5944 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5945 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5946
5947 /* (1) - may have excess leds enabled on MSB */
5948
5949 /* Defaults (order matters, keep last, don't reorder!) */
5950 { /* Lenovo */
5951 .vendor = PCI_VENDOR_ID_LENOVO,
5952 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5953 .quirks = 0x1fffU,
5954 },
5955 { /* IBM ThinkPads with no EC version string */
5956 .vendor = PCI_VENDOR_ID_IBM,
5957 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5958 .quirks = 0x00ffU,
5959 },
5960 { /* IBM ThinkPads with EC version string */
5961 .vendor = PCI_VENDOR_ID_IBM,
5962 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5963 .quirks = 0x00bfU,
5964 },
5965};
5966
5967#undef TPACPI_LEDQ_IBM
5968#undef TPACPI_LEDQ_LNV
5969
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005970static enum led_access_mode __init led_init_detect_mode(void)
5971{
5972 acpi_status status;
5973
5974 if (tpacpi_is_ibm()) {
5975 /* 570 */
5976 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5977 if (ACPI_SUCCESS(status))
5978 return TPACPI_LED_570;
5979
5980 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5981 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5982 if (ACPI_SUCCESS(status))
5983 return TPACPI_LED_OLD;
5984 }
5985
5986 /* most others */
5987 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5988 if (ACPI_SUCCESS(status))
5989 return TPACPI_LED_NEW;
5990
5991 /* R30, R31, and unknown firmwares */
5992 led_handle = NULL;
5993 return TPACPI_LED_NONE;
5994}
5995
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005996static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005997{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005998 unsigned int i;
5999 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006000 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006001
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006002 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
6003
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03006004 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006005
Adam Leefcb44e12013-06-07 16:20:08 +08006006 if (led_supported != TPACPI_LED_NONE) {
6007 useful_leds = tpacpi_check_quirks(led_useful_qtable,
6008 ARRAY_SIZE(led_useful_qtable));
6009
6010 if (!useful_leds) {
6011 led_handle = NULL;
6012 led_supported = TPACPI_LED_NONE;
6013 }
6014 }
6015
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006016 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
6017 str_supported(led_supported), led_supported);
6018
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006019 if (led_supported == TPACPI_LED_NONE)
6020 return 1;
6021
Kees Cook6396bb22018-06-12 14:03:40 -07006022 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006023 GFP_KERNEL);
6024 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07006025 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006026 return -ENOMEM;
6027 }
6028
6029 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08006030 tpacpi_leds[i].led = -1;
6031
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006032 if (!tpacpi_is_led_restricted(i) &&
6033 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006034 rc = tpacpi_init_led(i);
6035 if (rc < 0) {
6036 led_exit();
6037 return rc;
6038 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006039 }
6040 }
6041
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006042#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006043 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006044#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006045 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006046}
6047
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006048#define str_led_status(s) \
6049 ((s) == TPACPI_LED_OFF ? "off" : \
6050 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006051
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006052static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006054 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006055 seq_printf(m, "status:\t\tnot supported\n");
6056 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006057 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006058 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006059
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006060 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006061 /* 570 */
6062 int i, status;
6063 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006064 status = led_get_status(i);
6065 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006066 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006067 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006068 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006069 }
6070 }
6071
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006072 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006074 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075}
6076
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006077static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078{
6079 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006080 int led, rc;
6081 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006082
6083 if (!led_supported)
6084 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085
6086 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08006087 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 return -EINVAL;
6089
Adam Leeedf2d772013-06-08 16:51:15 +08006090 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
6091 tpacpi_leds[led].led < 0)
6092 return -ENODEV;
6093
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006094 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006095 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006097 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006098 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006099 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006100 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006101 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006102 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006103
6104 rc = led_set_status(led, s);
6105 if (rc < 0)
6106 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 }
6108
6109 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006110}
6111
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006112static struct ibm_struct led_driver_data = {
6113 .name = "led",
6114 .read = led_read,
6115 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006116 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006117};
6118
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006119/*************************************************************************
6120 * Beep subdriver
6121 */
6122
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006123TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006124
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006125#define TPACPI_BEEP_Q1 0x0001
6126
6127static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6128 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6129 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6130};
6131
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006132static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006133{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006134 unsigned long quirks;
6135
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006136 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6137
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006138 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006139
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006140 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6141 str_supported(beep_handle != NULL));
6142
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006143 quirks = tpacpi_check_quirks(beep_quirk_table,
6144 ARRAY_SIZE(beep_quirk_table));
6145
6146 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6147
Jan van den Berg72a979f2014-09-17 00:01:08 +02006148 return (beep_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006149}
6150
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006151static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006153 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006154 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006155 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006156 seq_printf(m, "status:\t\tsupported\n");
6157 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161}
6162
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006163static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164{
6165 char *cmd;
6166 int beep_cmd;
6167
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006168 if (!beep_handle)
6169 return -ENODEV;
6170
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006172 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6173 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 /* beep_cmd set */
6175 } else
6176 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006177 if (tp_features.beep_needs_two_args) {
6178 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6179 beep_cmd, 0))
6180 return -EIO;
6181 } else {
6182 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6183 beep_cmd))
6184 return -EIO;
6185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 }
6187
6188 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006189}
6190
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006191static struct ibm_struct beep_driver_data = {
6192 .name = "beep",
6193 .read = beep_read,
6194 .write = beep_write,
6195};
6196
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006197/*************************************************************************
6198 * Thermal subdriver
6199 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006200
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006201enum thermal_access_mode {
6202 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6203 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6204 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6205 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6206 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6207};
6208
6209enum { /* TPACPI_THERMAL_TPEC_* */
6210 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6211 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6212 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006213
6214 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006215};
6216
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006217
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006218#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6219struct ibm_thermal_sensors_struct {
6220 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6221};
6222
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006223static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006224
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006225/* idx is zero-based */
6226static int thermal_get_sensor(int idx, s32 *value)
6227{
6228 int t;
6229 s8 tmp;
6230 char tmpi[5];
6231
6232 t = TP_EC_THERMAL_TMP0;
6233
6234 switch (thermal_read_mode) {
6235#if TPACPI_MAX_THERMAL_SENSORS >= 16
6236 case TPACPI_THERMAL_TPEC_16:
6237 if (idx >= 8 && idx <= 15) {
6238 t = TP_EC_THERMAL_TMP8;
6239 idx -= 8;
6240 }
6241 /* fallthrough */
6242#endif
6243 case TPACPI_THERMAL_TPEC_8:
6244 if (idx <= 7) {
6245 if (!acpi_ec_read(t + idx, &tmp))
6246 return -EIO;
6247 *value = tmp * 1000;
6248 return 0;
6249 }
6250 break;
6251
6252 case TPACPI_THERMAL_ACPI_UPDT:
6253 if (idx <= 7) {
6254 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6255 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6256 return -EIO;
6257 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6258 return -EIO;
6259 *value = (t - 2732) * 100;
6260 return 0;
6261 }
6262 break;
6263
6264 case TPACPI_THERMAL_ACPI_TMP07:
6265 if (idx <= 7) {
6266 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6267 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6268 return -EIO;
6269 if (t > 127 || t < -127)
6270 t = TP_EC_THERMAL_TMP_NA;
6271 *value = t * 1000;
6272 return 0;
6273 }
6274 break;
6275
6276 case TPACPI_THERMAL_NONE:
6277 default:
6278 return -ENOSYS;
6279 }
6280
6281 return -EINVAL;
6282}
6283
6284static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6285{
6286 int res, i;
6287 int n;
6288
6289 n = 8;
6290 i = 0;
6291
6292 if (!s)
6293 return -EINVAL;
6294
6295 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6296 n = 16;
6297
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006298 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006299 res = thermal_get_sensor(i, &s->temp[i]);
6300 if (res)
6301 return res;
6302 }
6303
6304 return n;
6305}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006306
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006307static void thermal_dump_all_sensors(void)
6308{
6309 int n, i;
6310 struct ibm_thermal_sensors_struct t;
6311
6312 n = thermal_get_sensors(&t);
6313 if (n <= 0)
6314 return;
6315
Joe Perches0978e012011-04-04 10:06:25 -07006316 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006317
6318 for (i = 0; i < n; i++) {
6319 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07006320 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006321 else
Joe Perches0978e012011-04-04 10:06:25 -07006322 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006323 }
6324
Joe Perches0978e012011-04-04 10:06:25 -07006325 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006326}
6327
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006328/* sysfs temp##_input -------------------------------------------------- */
6329
6330static ssize_t thermal_temp_input_show(struct device *dev,
6331 struct device_attribute *attr,
6332 char *buf)
6333{
6334 struct sensor_device_attribute *sensor_attr =
6335 to_sensor_dev_attr(attr);
6336 int idx = sensor_attr->index;
6337 s32 value;
6338 int res;
6339
6340 res = thermal_get_sensor(idx, &value);
6341 if (res)
6342 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006343 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006344 return -ENXIO;
6345
6346 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6347}
6348
6349#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006350 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6351 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006352
6353static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6354 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6355 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6356 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6357 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6358 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6359 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6360 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6361 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6362 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6363 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6364 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6365 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6366 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6367 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6368 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6369 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6370};
6371
6372#define THERMAL_ATTRS(X) \
6373 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6374
6375static struct attribute *thermal_temp_input_attr[] = {
6376 THERMAL_ATTRS(8),
6377 THERMAL_ATTRS(9),
6378 THERMAL_ATTRS(10),
6379 THERMAL_ATTRS(11),
6380 THERMAL_ATTRS(12),
6381 THERMAL_ATTRS(13),
6382 THERMAL_ATTRS(14),
6383 THERMAL_ATTRS(15),
6384 THERMAL_ATTRS(0),
6385 THERMAL_ATTRS(1),
6386 THERMAL_ATTRS(2),
6387 THERMAL_ATTRS(3),
6388 THERMAL_ATTRS(4),
6389 THERMAL_ATTRS(5),
6390 THERMAL_ATTRS(6),
6391 THERMAL_ATTRS(7),
6392 NULL
6393};
6394
6395static const struct attribute_group thermal_temp_input16_group = {
6396 .attrs = thermal_temp_input_attr
6397};
6398
6399static const struct attribute_group thermal_temp_input8_group = {
6400 .attrs = &thermal_temp_input_attr[8]
6401};
6402
6403#undef THERMAL_SENSOR_ATTR_TEMP
6404#undef THERMAL_ATTRS
6405
6406/* --------------------------------------------------------------------- */
6407
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006408static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006409{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006410 u8 t, ta1, ta2;
6411 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006412 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006413 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006414
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006415 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6416
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006417 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006418
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03006419 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006420 /*
6421 * Direct EC access mode: sensors at registers
6422 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6423 * non-implemented, thermal sensors return 0x80 when
6424 * not available
6425 */
6426
6427 ta1 = ta2 = 0;
6428 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006429 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006430 ta1 |= t;
6431 } else {
6432 ta1 = 0;
6433 break;
6434 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006435 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006436 ta2 |= t;
6437 } else {
6438 ta1 = 0;
6439 break;
6440 }
6441 }
6442 if (ta1 == 0) {
6443 /* This is sheer paranoia, but we handle it anyway */
6444 if (acpi_tmp7) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006445 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006446 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006447 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006448 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006449 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006450 }
6451 } else {
6452 thermal_read_mode =
6453 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006454 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006455 }
6456 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006457 if (tpacpi_is_ibm() &&
6458 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006459 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006460 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006461 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006462 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006463 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006464 }
6465 } else {
6466 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006467 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006468 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006469
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006470 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6471 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6472 thermal_read_mode);
6473
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006474 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006475 case TPACPI_THERMAL_TPEC_16:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006476 res = sysfs_create_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006477 &thermal_temp_input16_group);
6478 if (res)
6479 return res;
6480 break;
6481 case TPACPI_THERMAL_TPEC_8:
6482 case TPACPI_THERMAL_ACPI_TMP07:
6483 case TPACPI_THERMAL_ACPI_UPDT:
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_input8_group);
6486 if (res)
6487 return res;
6488 break;
6489 case TPACPI_THERMAL_NONE:
6490 default:
6491 return 1;
6492 }
6493
6494 return 0;
6495}
6496
6497static void thermal_exit(void)
6498{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006499 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006500 case TPACPI_THERMAL_TPEC_16:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006501 sysfs_remove_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006502 &thermal_temp_input16_group);
6503 break;
6504 case TPACPI_THERMAL_TPEC_8:
6505 case TPACPI_THERMAL_ACPI_TMP07:
6506 case TPACPI_THERMAL_ACPI_UPDT:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006507 sysfs_remove_group(&tpacpi_hwmon->kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08006508 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006509 break;
6510 case TPACPI_THERMAL_NONE:
6511 default:
6512 break;
6513 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006514}
6515
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006516static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006517{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006518 int n, i;
6519 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006520
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006521 n = thermal_get_sensors(&t);
6522 if (unlikely(n < 0))
6523 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006524
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006525 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006526
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006527 if (n > 0) {
6528 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006529 seq_printf(m, "%d ", t.temp[i] / 1000);
6530 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006531 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006532 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006533
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006534 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006535}
6536
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006537static struct ibm_struct thermal_driver_data = {
6538 .name = "thermal",
6539 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006540 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006541};
6542
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006543/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006544 * Backlight/brightness subdriver
6545 */
Holger Macht8acb0252006-10-20 14:30:28 -07006546
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006547#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6548
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006549/*
6550 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6551 * CMOS NVRAM byte 0x5E, bits 0-3.
6552 *
6553 * EC HBRV (0x31) has the following layout
6554 * Bit 7: unknown function
6555 * Bit 6: unknown function
6556 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6557 * Bit 4: must be set to zero to avoid problems
6558 * Bit 3-0: backlight brightness level
6559 *
6560 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006561 *
6562 * WARNING: The X61 has been verified to use HBRV for something else, so
6563 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6564 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006565 */
6566
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006567enum {
6568 TP_EC_BACKLIGHT = 0x31,
6569
6570 /* TP_EC_BACKLIGHT bitmasks */
6571 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6572 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6573 TP_EC_BACKLIGHT_MAPSW = 0x20,
6574};
6575
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006576enum tpacpi_brightness_access_mode {
6577 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6578 TPACPI_BRGHT_MODE_EC, /* EC control */
6579 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6580 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6581 TPACPI_BRGHT_MODE_MAX
6582};
6583
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006584static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006585
6586static enum tpacpi_brightness_access_mode brightness_mode =
6587 TPACPI_BRGHT_MODE_MAX;
6588
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006589static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6590
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006591static struct mutex brightness_mutex;
6592
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006593/* NVRAM brightness access,
6594 * call with brightness_mutex held! */
6595static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006596{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006597 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006598
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006599 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6600 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6601 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006602 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006603
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006604 return lnvram;
6605}
6606
6607static void tpacpi_brightness_checkpoint_nvram(void)
6608{
6609 u8 lec = 0;
6610 u8 b_nvram;
6611
6612 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6613 return;
6614
6615 vdbg_printk(TPACPI_DBG_BRGHT,
6616 "trying to checkpoint backlight level to NVRAM...\n");
6617
6618 if (mutex_lock_killable(&brightness_mutex) < 0)
6619 return;
6620
6621 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6622 goto unlock;
6623 lec &= TP_EC_BACKLIGHT_LVLMSK;
6624 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6625
6626 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6627 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6628 /* NVRAM needs update */
6629 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6630 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6631 b_nvram |= lec;
6632 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6633 dbg_printk(TPACPI_DBG_BRGHT,
6634 "updated NVRAM backlight level to %u (0x%02x)\n",
6635 (unsigned int) lec, (unsigned int) b_nvram);
6636 } else
6637 vdbg_printk(TPACPI_DBG_BRGHT,
6638 "NVRAM backlight level already is %u (0x%02x)\n",
6639 (unsigned int) lec, (unsigned int) b_nvram);
6640
6641unlock:
6642 mutex_unlock(&brightness_mutex);
6643}
6644
6645
6646/* call with brightness_mutex held! */
6647static int tpacpi_brightness_get_raw(int *status)
6648{
6649 u8 lec = 0;
6650
6651 switch (brightness_mode) {
6652 case TPACPI_BRGHT_MODE_UCMS_STEP:
6653 *status = tpacpi_brightness_nvram_get();
6654 return 0;
6655 case TPACPI_BRGHT_MODE_EC:
6656 case TPACPI_BRGHT_MODE_ECNVRAM:
6657 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006658 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006659 *status = lec;
6660 return 0;
6661 default:
6662 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006663 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006664}
6665
6666/* call with brightness_mutex held! */
6667/* do NOT call with illegal backlight level value */
6668static int tpacpi_brightness_set_ec(unsigned int value)
6669{
6670 u8 lec = 0;
6671
6672 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6673 return -EIO;
6674
6675 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6676 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6677 (value & TP_EC_BACKLIGHT_LVLMSK))))
6678 return -EIO;
6679
6680 return 0;
6681}
6682
6683/* call with brightness_mutex held! */
6684static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6685{
6686 int cmos_cmd, inc;
6687 unsigned int current_value, i;
6688
6689 current_value = tpacpi_brightness_nvram_get();
6690
6691 if (value == current_value)
6692 return 0;
6693
6694 cmos_cmd = (value > current_value) ?
6695 TP_CMOS_BRIGHTNESS_UP :
6696 TP_CMOS_BRIGHTNESS_DOWN;
6697 inc = (value > current_value) ? 1 : -1;
6698
6699 for (i = current_value; i != value; i += inc)
6700 if (issue_thinkpad_cmos_command(cmos_cmd))
6701 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006702
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006703 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006704}
6705
6706/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006707static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006708{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006709 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006710
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006711 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006712 return -EINVAL;
6713
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006714 vdbg_printk(TPACPI_DBG_BRGHT,
6715 "set backlight level to %d\n", value);
6716
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006717 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006718 if (res < 0)
6719 return res;
6720
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006721 switch (brightness_mode) {
6722 case TPACPI_BRGHT_MODE_EC:
6723 case TPACPI_BRGHT_MODE_ECNVRAM:
6724 res = tpacpi_brightness_set_ec(value);
6725 break;
6726 case TPACPI_BRGHT_MODE_UCMS_STEP:
6727 res = tpacpi_brightness_set_ucmsstep(value);
6728 break;
6729 default:
6730 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006731 }
6732
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006733 mutex_unlock(&brightness_mutex);
6734 return res;
6735}
6736
6737/* sysfs backlight class ----------------------------------------------- */
6738
6739static int brightness_update_status(struct backlight_device *bd)
6740{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006741 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006742 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6743 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006744 bd->props.brightness : 0;
6745
6746 dbg_printk(TPACPI_DBG_BRGHT,
6747 "backlight: attempt to set level to %d\n",
6748 level);
6749
6750 /* it is the backlight class's job (caller) to handle
6751 * EINTR and other errors properly */
6752 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006753}
Holger Macht8acb0252006-10-20 14:30:28 -07006754
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006755static int brightness_get(struct backlight_device *bd)
6756{
6757 int status, res;
6758
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006759 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006760 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006761 return 0;
6762
6763 res = tpacpi_brightness_get_raw(&status);
6764
6765 mutex_unlock(&brightness_mutex);
6766
6767 if (res < 0)
6768 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006769
6770 return status & TP_EC_BACKLIGHT_LVLMSK;
6771}
6772
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006773static void tpacpi_brightness_notify_change(void)
6774{
6775 backlight_force_update(ibm_backlight_device,
6776 BACKLIGHT_UPDATE_HOTKEY);
6777}
6778
Lionel Debrouxacc24722010-11-16 14:14:02 +01006779static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006780 .get_brightness = brightness_get,
6781 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006782};
6783
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006784/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006785
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006786/*
6787 * Call _BCL method of video device. On some ThinkPads this will
6788 * switch the firmware to the ACPI brightness control mode.
6789 */
6790
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006791static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6792{
6793 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6794 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006795 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006796 int rc;
6797
Aaron Lu46445b62013-10-11 21:27:46 +08006798 if (acpi_bus_get_device(handle, &device))
6799 return 0;
6800
6801 rc = 0;
6802 list_for_each_entry(child, &device->children, node) {
6803 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6804 NULL, &buffer);
6805 if (ACPI_FAILURE(status))
6806 continue;
6807
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006808 obj = (union acpi_object *)buffer.pointer;
6809 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006810 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006811 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006812 rc = 0;
6813 } else {
6814 rc = obj->package.count;
6815 }
Aaron Lu46445b62013-10-11 21:27:46 +08006816 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006817 }
6818
6819 kfree(buffer.pointer);
6820 return rc;
6821}
6822
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006823
6824/*
6825 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6826 */
6827static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6828{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006829 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006830 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006831
Aaron Lu46445b62013-10-11 21:27:46 +08006832 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006833 if (video_device)
6834 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006835
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006836 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006837
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006838 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006839}
6840
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006841/*
6842 * These are only useful for models that have only one possibility
6843 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6844 * these quirks.
6845 */
6846#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6847#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6848#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6849
6850static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6851 /* Models with ATI GPUs known to require ECNVRAM mode */
6852 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6853
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006854 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006855 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006856 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006857 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006858 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6859
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006860 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006861 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006862 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6863 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006864
6865 /* Models with Intel GMA900 */
6866 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6867 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6868 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6869};
6870
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006871/*
6872 * Returns < 0 for error, otherwise sets tp_features.bright_*
6873 * and bright_maxlvl.
6874 */
6875static void __init tpacpi_detect_brightness_capabilities(void)
6876{
6877 unsigned int b;
6878
6879 vdbg_printk(TPACPI_DBG_INIT,
6880 "detecting firmware brightness interface capabilities\n");
6881
6882 /* we could run a quirks check here (same table used by
6883 * brightness_init) if needed */
6884
6885 /*
6886 * We always attempt to detect acpi support, so as to switch
6887 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6888 * going to publish a backlight interface
6889 */
6890 b = tpacpi_check_std_acpi_brightness_support();
6891 switch (b) {
6892 case 16:
6893 bright_maxlvl = 15;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006894 break;
6895 case 8:
6896 case 0:
6897 bright_maxlvl = 7;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006898 break;
6899 default:
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006900 tp_features.bright_unkfw = 1;
6901 bright_maxlvl = b - 1;
6902 }
Eric Curtin15c75622016-01-30 16:55:59 +00006903 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006904}
6905
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006906static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006907{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006908 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006909 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006910 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006911
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006912 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6913
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006914 mutex_init(&brightness_mutex);
6915
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006916 quirks = tpacpi_check_quirks(brightness_quirk_table,
6917 ARRAY_SIZE(brightness_quirk_table));
6918
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006919 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006920
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006921 /* if it is unknown, we don't handle it: it wouldn't be safe */
6922 if (tp_features.bright_unkfw)
6923 return 1;
6924
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006925 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006926 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006927 "brightness support disabled by module parameter\n");
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006928 return 1;
6929 }
6930
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02006931 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006932 if (brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006933 pr_info("Standard ACPI backlight interface available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006934 return 1;
6935 } else if (brightness_enable == 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006936 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 -03006937 return 1;
6938 }
6939 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006940 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006941 }
6942
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006943 /*
6944 * Check for module parameter bogosity, note that we
6945 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6946 * able to detect "unspecified"
6947 */
6948 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006949 return -EINVAL;
6950
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006951 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6952 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6953 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006954 if (quirks & TPACPI_BRGHT_Q_EC)
6955 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6956 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006957 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6958
6959 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006960 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006961 brightness_mode);
6962 }
6963
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006964 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006965 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006966 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6967 brightness_mode == TPACPI_BRGHT_MODE_EC))
6968 return -EINVAL;
6969
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006970 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006971 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006972
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006973 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006974 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006975 props.max_brightness = bright_maxlvl;
6976 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006977 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6978 NULL, NULL,
6979 &ibm_backlight_data,
6980 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006981 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006982 int rc = PTR_ERR(ibm_backlight_device);
6983 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006984 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006985 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006986 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006987 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6988 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006989
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006990 if (quirks & TPACPI_BRGHT_Q_ASK) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006991 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
6992 brightness_mode);
6993 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
6994 TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006995 }
6996
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006997 /* Added by mistake in early 2007. Probably useless, but it could
6998 * be working around some unknown firmware problem where the value
6999 * read at startup doesn't match the real hardware state... so leave
7000 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02007001 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00007002
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007003 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007004 "brightness: registering brightness hotkeys as change notification\n");
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007005 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7006 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08007007 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007008 return 0;
7009}
7010
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007011static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007012{
7013 tpacpi_brightness_checkpoint_nvram();
7014}
7015
7016static void brightness_shutdown(void)
7017{
7018 tpacpi_brightness_checkpoint_nvram();
7019}
7020
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007021static void brightness_exit(void)
7022{
7023 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007024 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007025 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007026 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007027 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007028
7029 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007030}
7031
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007032static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007033{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007034 int level;
7035
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007036 level = brightness_get(NULL);
7037 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007038 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007039 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007040 seq_printf(m, "level:\t\t%d\n", level);
7041 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007042 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7043 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007044 }
7045
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007046 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007047}
7048
7049static int brightness_write(char *buf)
7050{
7051 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007052 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007053 char *cmd;
7054
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007055 level = brightness_get(NULL);
7056 if (level < 0)
7057 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007058
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007059 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007060 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007061 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007062 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007063 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007064 if (level > 0)
7065 level--;
7066 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007067 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007068 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007069 } else
7070 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007071 }
7072
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007073 tpacpi_disclose_usertask("procfs brightness",
7074 "set level to %d\n", level);
7075
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007076 /*
7077 * Now we know what the final level should be, so we try to set it.
7078 * Doing it this way makes the syscall restartable in case of EINTR
7079 */
7080 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007081 if (!rc && ibm_backlight_device)
7082 backlight_force_update(ibm_backlight_device,
7083 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02007084 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007085}
7086
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007087static struct ibm_struct brightness_driver_data = {
7088 .name = "brightness",
7089 .read = brightness_read,
7090 .write = brightness_write,
7091 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007092 .suspend = brightness_suspend,
7093 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007094};
7095
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007096/*************************************************************************
7097 * Volume subdriver
7098 */
7099
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007100/*
7101 * IBM ThinkPads have a simple volume controller with MUTE gating.
7102 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7103 *
7104 * Since the *61 series (and probably also the later *60 series), Lenovo
7105 * ThinkPads only implement the MUTE gate.
7106 *
7107 * EC register 0x30
7108 * Bit 6: MUTE (1 mutes sound)
7109 * Bit 3-0: Volume
7110 * Other bits should be zero as far as we know.
7111 *
7112 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7113 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7114 * such as bit 7 which is used to detect repeated presses of MUTE,
7115 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007116 *
7117 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7118 * in response to user input. Unfortunately, this rarely works well.
7119 * The laptop changes the state of its internal MUTE gate and, on some
7120 * models, sends KEY_MUTE, causing any user code that responds to the
7121 * mute button to get confused. The hardware MUTE gate is also
7122 * unnecessary, since user code can handle the mute button without
7123 * kernel or EC help.
7124 *
7125 * To avoid confusing userspace, we simply disable all EC-based mute
7126 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007127 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007128
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007129#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7130
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007131#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7132#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7133#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7134
Takashi Iwaicab66612013-10-24 16:06:32 +02007135#if SNDRV_CARDS <= 32
7136#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7137#else
7138#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7139#endif
7140static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007141static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307142static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007143
7144struct tpacpi_alsa_data {
7145 struct snd_card *card;
7146 struct snd_ctl_elem_id *ctl_mute_id;
7147 struct snd_ctl_elem_id *ctl_vol_id;
7148};
7149
7150static struct snd_card *alsa_card;
7151
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007152enum {
7153 TP_EC_AUDIO = 0x30,
7154
7155 /* TP_EC_AUDIO bits */
7156 TP_EC_AUDIO_MUTESW = 6,
7157
7158 /* TP_EC_AUDIO bitmasks */
7159 TP_EC_AUDIO_LVL_MSK = 0x0F,
7160 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7161
7162 /* Maximum volume */
7163 TP_EC_VOLUME_MAX = 14,
7164};
7165
7166enum tpacpi_volume_access_mode {
7167 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7168 TPACPI_VOL_MODE_EC, /* Pure EC control */
7169 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7170 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7171 TPACPI_VOL_MODE_MAX
7172};
7173
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007174enum tpacpi_volume_capabilities {
7175 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7176 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7177 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7178 TPACPI_VOL_CAP_MAX
7179};
7180
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007181enum tpacpi_mute_btn_mode {
7182 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7183 /* We don't know what mode 1 is. */
7184 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7185 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7186};
7187
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007188static enum tpacpi_volume_access_mode volume_mode =
7189 TPACPI_VOL_MODE_MAX;
7190
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007191static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307192static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007193static bool software_mute_requested = true;
7194static bool software_mute_active;
7195static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007196
7197/*
7198 * Used to syncronize writers to TP_EC_AUDIO and
7199 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7200 */
7201static struct mutex volume_mutex;
7202
7203static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007204{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007205 u8 lec = 0;
7206 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007207 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007208
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007209 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7210 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007211 if (!volume_control_allowed)
7212 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007213 if (software_mute_active)
7214 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007215
7216 vdbg_printk(TPACPI_DBG_MIXER,
7217 "trying to checkpoint mixer state to NVRAM...\n");
7218
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007219 if (tp_features.mixer_no_level_control)
7220 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7221 else
7222 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7223
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007224 if (mutex_lock_killable(&volume_mutex) < 0)
7225 return;
7226
7227 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7228 goto unlock;
7229 lec &= ec_mask;
7230 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7231
7232 if (lec != (b_nvram & ec_mask)) {
7233 /* NVRAM needs update */
7234 b_nvram &= ~ec_mask;
7235 b_nvram |= lec;
7236 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7237 dbg_printk(TPACPI_DBG_MIXER,
7238 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7239 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007240 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007241 vdbg_printk(TPACPI_DBG_MIXER,
7242 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7243 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007244 }
7245
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007246unlock:
7247 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007248}
7249
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007250static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007251{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007252 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007253
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007254 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7255 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007256
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007257 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007258
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007259 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007260
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007261 return 0;
7262}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007263
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007264static int volume_get_status(u8 *status)
7265{
7266 return volume_get_status_ec(status);
7267}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007268
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007269static int volume_set_status_ec(const u8 status)
7270{
7271 if (!acpi_ec_write(TP_EC_AUDIO, status))
7272 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007273
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007274 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7275
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007276 /*
7277 * On X200s, and possibly on others, it can take a while for
7278 * reads to become correct.
7279 */
7280 msleep(1);
7281
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007282 return 0;
7283}
7284
7285static int volume_set_status(const u8 status)
7286{
7287 return volume_set_status_ec(status);
7288}
7289
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007290/* returns < 0 on error, 0 on no change, 1 on change */
7291static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007292{
7293 int rc;
7294 u8 s, n;
7295
7296 if (mutex_lock_killable(&volume_mutex) < 0)
7297 return -EINTR;
7298
7299 rc = volume_get_status_ec(&s);
7300 if (rc)
7301 goto unlock;
7302
7303 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7304 s & ~TP_EC_AUDIO_MUTESW_MSK;
7305
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007306 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007307 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007308 if (!rc)
7309 rc = 1;
7310 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007311
7312unlock:
7313 mutex_unlock(&volume_mutex);
7314 return rc;
7315}
7316
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007317static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007318{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007319 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007320 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007321 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007322}
7323
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007324static int volume_set_mute(const bool mute)
7325{
7326 int rc;
7327
7328 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7329 (mute) ? "" : "un");
7330
7331 rc = __volume_set_mute_ec(mute);
7332 return (rc < 0) ? rc : 0;
7333}
7334
7335/* returns < 0 on error, 0 on no change, 1 on change */
7336static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007337{
7338 int rc;
7339 u8 s, n;
7340
7341 if (vol > TP_EC_VOLUME_MAX)
7342 return -EINVAL;
7343
7344 if (mutex_lock_killable(&volume_mutex) < 0)
7345 return -EINTR;
7346
7347 rc = volume_get_status_ec(&s);
7348 if (rc)
7349 goto unlock;
7350
7351 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7352
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007353 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007354 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007355 if (!rc)
7356 rc = 1;
7357 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007358
7359unlock:
7360 mutex_unlock(&volume_mutex);
7361 return rc;
7362}
7363
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007364static int volume_set_software_mute(bool startup)
7365{
7366 int result;
7367
7368 if (!tpacpi_is_lenovo())
7369 return -ENODEV;
7370
7371 if (startup) {
7372 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7373 "HAUM", "qd"))
7374 return -EIO;
7375
7376 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7377 "Initial HAUM setting was %d\n",
7378 software_mute_orig_mode);
7379 }
7380
7381 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7382 (int)TP_EC_MUTE_BTN_NONE))
7383 return -EIO;
7384
7385 if (result != TP_EC_MUTE_BTN_NONE)
7386 pr_warn("Unexpected SAUM result %d\n",
7387 result);
7388
7389 /*
7390 * In software mute mode, the standard codec controls take
7391 * precendence, so we unmute the ThinkPad HW switch at
7392 * startup. Just on case there are SAUM-capable ThinkPads
7393 * with level controls, set max HW volume as well.
7394 */
7395 if (tp_features.mixer_no_level_control)
7396 result = volume_set_mute(false);
7397 else
7398 result = volume_set_status(TP_EC_VOLUME_MAX);
7399
7400 if (result != 0)
7401 pr_warn("Failed to unmute the HW mute switch\n");
7402
7403 return 0;
7404}
7405
7406static void volume_exit_software_mute(void)
7407{
7408 int r;
7409
7410 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7411 || r != software_mute_orig_mode)
7412 pr_warn("Failed to restore mute mode\n");
7413}
7414
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007415static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007416{
7417 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007418 "ALSA: trying to set volume level to %hu\n", vol);
7419 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007420}
7421
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007422static void volume_alsa_notify_change(void)
7423{
7424 struct tpacpi_alsa_data *d;
7425
7426 if (alsa_card && alsa_card->private_data) {
7427 d = alsa_card->private_data;
7428 if (d->ctl_mute_id)
7429 snd_ctl_notify(alsa_card,
7430 SNDRV_CTL_EVENT_MASK_VALUE,
7431 d->ctl_mute_id);
7432 if (d->ctl_vol_id)
7433 snd_ctl_notify(alsa_card,
7434 SNDRV_CTL_EVENT_MASK_VALUE,
7435 d->ctl_vol_id);
7436 }
7437}
7438
7439static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7440 struct snd_ctl_elem_info *uinfo)
7441{
7442 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7443 uinfo->count = 1;
7444 uinfo->value.integer.min = 0;
7445 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7446 return 0;
7447}
7448
7449static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7450 struct snd_ctl_elem_value *ucontrol)
7451{
7452 u8 s;
7453 int rc;
7454
7455 rc = volume_get_status(&s);
7456 if (rc < 0)
7457 return rc;
7458
7459 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7460 return 0;
7461}
7462
7463static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7464 struct snd_ctl_elem_value *ucontrol)
7465{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007466 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7467 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007468 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007469}
7470
7471#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7472
7473static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7474 struct snd_ctl_elem_value *ucontrol)
7475{
7476 u8 s;
7477 int rc;
7478
7479 rc = volume_get_status(&s);
7480 if (rc < 0)
7481 return rc;
7482
7483 ucontrol->value.integer.value[0] =
7484 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7485 return 0;
7486}
7487
7488static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7489 struct snd_ctl_elem_value *ucontrol)
7490{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007491 tpacpi_disclose_usertask("ALSA", "%smute\n",
7492 ucontrol->value.integer.value[0] ?
7493 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007494 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007495}
7496
Mathias Krausec3aa4722014-07-16 19:43:17 +02007497static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007498 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7499 .name = "Console Playback Volume",
7500 .index = 0,
7501 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7502 .info = volume_alsa_vol_info,
7503 .get = volume_alsa_vol_get,
7504};
7505
Mathias Krausec3aa4722014-07-16 19:43:17 +02007506static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007507 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7508 .name = "Console Playback Switch",
7509 .index = 0,
7510 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7511 .info = volume_alsa_mute_info,
7512 .get = volume_alsa_mute_get,
7513};
7514
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007515static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007516{
7517 tpacpi_volume_checkpoint_nvram();
7518}
7519
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007520static void volume_resume(void)
7521{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007522 if (software_mute_active) {
7523 if (volume_set_software_mute(false) < 0)
7524 pr_warn("Failed to restore software mute\n");
7525 } else {
7526 volume_alsa_notify_change();
7527 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007528}
7529
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007530static void volume_shutdown(void)
7531{
7532 tpacpi_volume_checkpoint_nvram();
7533}
7534
7535static void volume_exit(void)
7536{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007537 if (alsa_card) {
7538 snd_card_free(alsa_card);
7539 alsa_card = NULL;
7540 }
7541
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007542 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007543
7544 if (software_mute_active)
7545 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007546}
7547
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007548static int __init volume_create_alsa_mixer(void)
7549{
7550 struct snd_card *card;
7551 struct tpacpi_alsa_data *data;
7552 struct snd_kcontrol *ctl_vol;
7553 struct snd_kcontrol *ctl_mute;
7554 int rc;
7555
Takashi Iwai89235e52014-01-29 15:01:27 +01007556 rc = snd_card_new(&tpacpi_pdev->dev,
7557 alsa_index, alsa_id, THIS_MODULE,
7558 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007559 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007560 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007561 return 1;
7562 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007563
7564 BUG_ON(!card->private_data);
7565 data = card->private_data;
7566 data->card = card;
7567
7568 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7569 sizeof(card->driver));
7570 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7571 sizeof(card->shortname));
7572 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7573 (thinkpad_id.ec_version_str) ?
7574 thinkpad_id.ec_version_str : "(unknown)");
7575 snprintf(card->longname, sizeof(card->longname),
7576 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7577 (thinkpad_id.ec_version_str) ?
7578 thinkpad_id.ec_version_str : "unknown");
7579
7580 if (volume_control_allowed) {
7581 volume_alsa_control_vol.put = volume_alsa_vol_put;
7582 volume_alsa_control_vol.access =
7583 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7584
7585 volume_alsa_control_mute.put = volume_alsa_mute_put;
7586 volume_alsa_control_mute.access =
7587 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7588 }
7589
7590 if (!tp_features.mixer_no_level_control) {
7591 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7592 rc = snd_ctl_add(card, ctl_vol);
7593 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007594 pr_err("Failed to create ALSA volume control: %d\n",
7595 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007596 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007597 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007598 data->ctl_vol_id = &ctl_vol->id;
7599 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007600
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007601 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7602 rc = snd_ctl_add(card, ctl_mute);
7603 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007604 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007605 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007606 }
7607 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007608
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007609 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007610 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007611 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007612 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007613 }
7614
7615 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007616 return 0;
7617
7618err_exit:
7619 snd_card_free(card);
7620 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007621}
7622
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007623#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7624#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7625
7626static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7627 /* Whitelist volume level on all IBM by default */
7628 { .vendor = PCI_VENDOR_ID_IBM,
7629 .bios = TPACPI_MATCH_ANY,
7630 .ec = TPACPI_MATCH_ANY,
7631 .quirks = TPACPI_VOL_Q_LEVEL },
7632
7633 /* Lenovo models with volume control (needs confirmation) */
7634 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7635 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7636 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7637 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7638 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7639 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7640 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7641
7642 /* Whitelist mute-only on all Lenovo by default */
7643 { .vendor = PCI_VENDOR_ID_LENOVO,
7644 .bios = TPACPI_MATCH_ANY,
7645 .ec = TPACPI_MATCH_ANY,
7646 .quirks = TPACPI_VOL_Q_MUTEONLY }
7647};
7648
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007649static int __init volume_init(struct ibm_init_struct *iibm)
7650{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007651 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007652 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007653
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007654 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7655
7656 mutex_init(&volume_mutex);
7657
7658 /*
7659 * Check for module parameter bogosity, note that we
7660 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7661 * able to detect "unspecified"
7662 */
7663 if (volume_mode > TPACPI_VOL_MODE_MAX)
7664 return -EINVAL;
7665
7666 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007667 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7668 TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007669 return 1;
7670 }
7671
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007672 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7673 return -EINVAL;
7674
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007675 /*
7676 * The ALSA mixer is our primary interface.
7677 * When disabled, don't install the subdriver at all
7678 */
7679 if (!alsa_enable) {
7680 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007681 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007682 return 1;
7683 }
7684
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007685 quirks = tpacpi_check_quirks(volume_quirk_table,
7686 ARRAY_SIZE(volume_quirk_table));
7687
7688 switch (volume_capabilities) {
7689 case TPACPI_VOL_CAP_AUTO:
7690 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7691 tp_features.mixer_no_level_control = 1;
7692 else if (quirks & TPACPI_VOL_Q_LEVEL)
7693 tp_features.mixer_no_level_control = 0;
7694 else
7695 return 1; /* no mixer */
7696 break;
7697 case TPACPI_VOL_CAP_VOLMUTE:
7698 tp_features.mixer_no_level_control = 0;
7699 break;
7700 case TPACPI_VOL_CAP_MUTEONLY:
7701 tp_features.mixer_no_level_control = 1;
7702 break;
7703 default:
7704 return 1;
7705 }
7706
7707 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7708 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7709 "using user-supplied volume_capabilities=%d\n",
7710 volume_capabilities);
7711
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007712 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7713 volume_mode == TPACPI_VOL_MODE_MAX) {
7714 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7715
7716 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7717 "driver auto-selected volume_mode=%d\n",
7718 volume_mode);
7719 } else {
7720 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7721 "using user-supplied volume_mode=%d\n",
7722 volume_mode);
7723 }
7724
7725 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007726 "mute is supported, volume control is %s\n",
7727 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007728
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007729 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7730 software_mute_active = true;
7731 } else {
7732 rc = volume_create_alsa_mixer();
7733 if (rc) {
7734 pr_err("Could not create the ALSA mixer interface\n");
7735 return rc;
7736 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007737
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007738 pr_info("Console audio control enabled, mode: %s\n",
7739 (volume_control_allowed) ?
7740 "override (read/write)" :
7741 "monitor (read only)");
7742 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007743
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007744 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7745 "registering volume hotkeys as change notification\n");
7746 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7747 | TP_ACPI_HKEY_VOLUP_MASK
7748 | TP_ACPI_HKEY_VOLDWN_MASK
7749 | TP_ACPI_HKEY_MUTE_MASK);
7750
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007751 return 0;
7752}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007753
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007754static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007755{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007756 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007757
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007758 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007759 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007760 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007761 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007762 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007763 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007764 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007765 status & TP_EC_AUDIO_LVL_MSK);
7766
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007767 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007768 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007769
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007770 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007771 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007772 if (!tp_features.mixer_no_level_control) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007773 seq_printf(m, "commands:\tup, down\n");
7774 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7775 TP_EC_VOLUME_MAX);
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007776 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007777 }
7778 }
7779
7780 return 0;
7781}
7782
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007783static int volume_write(char *buf)
7784{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007785 u8 s;
7786 u8 new_level, new_mute;
7787 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007788 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007789 int rc;
7790
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007791 /*
7792 * We do allow volume control at driver startup, so that the
7793 * user can set initial state through the volume=... parameter hack.
7794 */
7795 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7796 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7797 tp_warned.volume_ctrl_forbidden = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007798 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7799 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007800 }
7801 return -EPERM;
7802 }
7803
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007804 rc = volume_get_status(&s);
7805 if (rc < 0)
7806 return rc;
7807
7808 new_level = s & TP_EC_AUDIO_LVL_MSK;
7809 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007810
7811 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007812 if (!tp_features.mixer_no_level_control) {
7813 if (strlencmp(cmd, "up") == 0) {
7814 if (new_mute)
7815 new_mute = 0;
7816 else if (new_level < TP_EC_VOLUME_MAX)
7817 new_level++;
7818 continue;
7819 } else if (strlencmp(cmd, "down") == 0) {
7820 if (new_mute)
7821 new_mute = 0;
7822 else if (new_level > 0)
7823 new_level--;
7824 continue;
7825 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7826 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7827 new_level = l;
7828 continue;
7829 }
7830 }
7831 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007832 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007833 else if (strlencmp(cmd, "unmute") == 0)
7834 new_mute = 0;
7835 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007836 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007837 }
7838
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007839 if (tp_features.mixer_no_level_control) {
7840 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7841 new_mute ? "" : "un");
7842 rc = volume_set_mute(!!new_mute);
7843 } else {
7844 tpacpi_disclose_usertask("procfs volume",
7845 "%smute and set level to %d\n",
7846 new_mute ? "" : "un", new_level);
7847 rc = volume_set_status(new_mute | new_level);
7848 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007849 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007850
7851 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007852}
7853
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007854static struct ibm_struct volume_driver_data = {
7855 .name = "volume",
7856 .read = volume_read,
7857 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007858 .exit = volume_exit,
7859 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007860 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007861 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007862};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007863
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007864#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7865
7866#define alsa_card NULL
7867
Tobias Klauserbb55a2e2016-11-25 14:18:07 +01007868static inline void volume_alsa_notify_change(void)
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007869{
7870}
7871
7872static int __init volume_init(struct ibm_init_struct *iibm)
7873{
Joe Perches0978e012011-04-04 10:06:25 -07007874 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007875
7876 return 1;
7877}
7878
7879static struct ibm_struct volume_driver_data = {
7880 .name = "volume",
7881};
7882
7883#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7884
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007885/*************************************************************************
7886 * Fan subdriver
7887 */
7888
7889/*
7890 * FAN ACCESS MODES
7891 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007892 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007893 * ACPI GFAN method: returns fan level
7894 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007895 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007896 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007897 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007898 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007899 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7900 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007901 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7902 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007903 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007904 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007905 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7906 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007907 *
7908 * Fan speed changes of any sort (including those caused by the
7909 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007910 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007911 * limited.
7912 *
7913 * Reading is not available if GFAN exists.
7914 * Writing is not available if SFAN exists.
7915 *
7916 * Bits
7917 * 7 automatic mode engaged;
7918 * (default operation mode of the ThinkPad)
7919 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007920 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007921 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007922 * the tachometer while the fan controller ramps up
7923 * the speed (which can take up to a few *minutes*).
7924 * Speeds up fan to 100% duty-cycle, which is far above
7925 * the standard RPM levels. It is not impossible that
7926 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007927 * 5-3 unused in some models. Extra bits for fan level
7928 * in others, but still useless as all values above
7929 * 7 map to the same speed as level 7 in these models.
7930 * 2-0 fan level (0..7 usually)
7931 * 0x00 = stop
7932 * 0x07 = max (set when temperatures critical)
7933 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007934 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007935 *
7936 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007937 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007938 * does so, its initial value is meaningless (0x07).
7939 *
7940 * For firmware bugs, refer to:
7941 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7942 *
7943 * ----
7944 *
7945 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7946 * Main fan tachometer reading (in RPM)
7947 *
7948 * This register is present on all ThinkPads with a new-style EC, and
7949 * it is known not to be present on the A21m/e, and T22, as there is
7950 * something else in offset 0x84 according to the ACPI DSDT. Other
7951 * ThinkPads from this same time period (and earlier) probably lack the
7952 * tachometer as well.
7953 *
Nick Andrew877d0312009-01-26 11:06:57 +01007954 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007955 * was never fixed by IBM to report the EC firmware version string
7956 * probably support the tachometer (like the early X models), so
7957 * detecting it is quite hard. We need more data to know for sure.
7958 *
7959 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7960 * might result.
7961 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007962 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7963 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007964 *
7965 * For firmware bugs, refer to:
7966 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7967 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007968 * ----
7969 *
7970 * ThinkPad EC register 0x31 bit 0 (only on select models)
7971 *
7972 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7973 * show the speed of the main fan. When bit 0 of EC register 0x31
7974 * is one, the tachometer registers show the speed of the auxiliary
7975 * fan.
7976 *
7977 * Fan control seems to affect both fans, regardless of the state
7978 * of this bit.
7979 *
7980 * So far, only the firmware for the X60/X61 non-tablet versions
7981 * seem to support this (firmware TP-7M).
7982 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007983 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007984 * ThinkPad X31, X40, X41. Not available in the X60.
7985 *
7986 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7987 * high speed. ACPI DSDT seems to map these three speeds to levels
7988 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7989 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7990 *
7991 * The speeds are stored on handles
7992 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7993 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007994 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007995 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7996 *
7997 * ACPI DSDT switches which set is in use depending on various
7998 * factors.
7999 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008000 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008001 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
8002 * but the ACPI tables just mention level 7.
8003 */
8004
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008005enum { /* Fan control constants */
8006 fan_status_offset = 0x2f, /* EC register 0x2f */
8007 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
8008 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008009 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
8010 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008011
8012 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
8013 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
8014
8015 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
8016};
8017
8018enum fan_status_access_mode {
8019 TPACPI_FAN_NONE = 0, /* No fan status or control */
8020 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
8021 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8022};
8023
8024enum fan_control_access_mode {
8025 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8026 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8027 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8028 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8029};
8030
8031enum fan_control_commands {
8032 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8033 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8034 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8035 * and also watchdog cmd */
8036};
8037
Rusty Russell90ab5ee2012-01-13 09:32:20 +10308038static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008039
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008040static enum fan_status_access_mode fan_status_access_mode;
8041static enum fan_control_access_mode fan_control_access_mode;
8042static enum fan_control_commands fan_control_commands;
8043
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008044static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008045static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008046static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008047static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008048
8049static struct mutex fan_mutex;
8050
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008051static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05008052static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008053
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008054TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8055TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008056 "\\FSPD", /* 600e/x, 770e, 770x */
8057 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008058TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008059 "JFNS", /* 770x-JL */
8060 ); /* all others */
8061
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008062/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008063 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8064 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8065 * be in auto mode (0x80).
8066 *
8067 * This is corrected by any write to HFSP either by the driver, or
8068 * by the firmware.
8069 *
8070 * We assume 0x07 really means auto mode while this quirk is active,
8071 * as this is far more likely than the ThinkPad being in level 7,
8072 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008073 *
8074 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8075 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008076 */
8077
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008078static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008079{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008080 if (fan_control_initial_status == 0x07) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008081 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 -03008082 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008083 }
8084}
8085
8086static void fan_quirk1_handle(u8 *fan_status)
8087{
8088 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8089 if (*fan_status != fan_control_initial_status) {
8090 /* something changed the HFSP regisnter since
8091 * driver init time, so it is not undefined
8092 * anymore */
8093 tp_features.fan_ctrl_status_undef = 0;
8094 } else {
8095 /* Return most likely status. In fact, it
8096 * might be the only possible status */
8097 *fan_status = TP_EC_FAN_AUTO;
8098 }
8099 }
8100}
8101
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008102/* Select main fan on X60/X61, NOOP on others */
8103static bool fan_select_fan1(void)
8104{
8105 if (tp_features.second_fan) {
8106 u8 val;
8107
8108 if (ec_read(fan_select_offset, &val) < 0)
8109 return false;
8110 val &= 0xFEU;
8111 if (ec_write(fan_select_offset, val) < 0)
8112 return false;
8113 }
8114 return true;
8115}
8116
8117/* Select secondary fan on X60/X61 */
8118static bool fan_select_fan2(void)
8119{
8120 u8 val;
8121
8122 if (!tp_features.second_fan)
8123 return false;
8124
8125 if (ec_read(fan_select_offset, &val) < 0)
8126 return false;
8127 val |= 0x01U;
8128 if (ec_write(fan_select_offset, val) < 0)
8129 return false;
8130
8131 return true;
8132}
8133
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008134/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008135 * Call with fan_mutex held
8136 */
8137static void fan_update_desired_level(u8 status)
8138{
8139 if ((status &
8140 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8141 if (status > 7)
8142 fan_control_desired_level = 7;
8143 else
8144 fan_control_desired_level = status;
8145 }
8146}
8147
8148static int fan_get_status(u8 *status)
8149{
8150 u8 s;
8151
8152 /* TODO:
8153 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8154
8155 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07008156 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008157 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07008158 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008159
Dan Carpentereceeb432012-09-01 12:54:07 -07008160 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008161 return -EIO;
8162
8163 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07008164 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008165
8166 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07008167 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008168 case TPACPI_FAN_RD_TPEC:
8169 /* all except 570, 600e/x, 770e, 770x */
8170 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8171 return -EIO;
8172
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008173 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008174 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008175 fan_quirk1_handle(status);
8176 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008177
8178 break;
8179
8180 default:
8181 return -ENXIO;
8182 }
8183
8184 return 0;
8185}
8186
8187static int fan_get_status_safe(u8 *status)
8188{
8189 int rc;
8190 u8 s;
8191
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008192 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008193 return -ERESTARTSYS;
8194 rc = fan_get_status(&s);
8195 if (!rc)
8196 fan_update_desired_level(s);
8197 mutex_unlock(&fan_mutex);
8198
Dan Carpentera7718362016-04-15 17:46:34 +03008199 if (rc)
8200 return rc;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008201 if (status)
8202 *status = s;
8203
Dan Carpentera7718362016-04-15 17:46:34 +03008204 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008205}
8206
8207static int fan_get_speed(unsigned int *speed)
8208{
8209 u8 hi, lo;
8210
8211 switch (fan_status_access_mode) {
8212 case TPACPI_FAN_RD_TPEC:
8213 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008214 if (unlikely(!fan_select_fan1()))
8215 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008216 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8217 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8218 return -EIO;
8219
8220 if (likely(speed))
8221 *speed = (hi << 8) | lo;
8222
8223 break;
8224
8225 default:
8226 return -ENXIO;
8227 }
8228
8229 return 0;
8230}
8231
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008232static int fan2_get_speed(unsigned int *speed)
8233{
8234 u8 hi, lo;
8235 bool rc;
8236
8237 switch (fan_status_access_mode) {
8238 case TPACPI_FAN_RD_TPEC:
8239 /* all except 570, 600e/x, 770e, 770x */
8240 if (unlikely(!fan_select_fan2()))
8241 return -EIO;
8242 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8243 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8244 fan_select_fan1(); /* play it safe */
8245 if (rc)
8246 return -EIO;
8247
8248 if (likely(speed))
8249 *speed = (hi << 8) | lo;
8250
8251 break;
8252
8253 default:
8254 return -ENXIO;
8255 }
8256
8257 return 0;
8258}
8259
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008260static int fan_set_level(int level)
8261{
8262 if (!fan_control_allowed)
8263 return -EPERM;
8264
8265 switch (fan_control_access_mode) {
8266 case TPACPI_FAN_WR_ACPI_SFAN:
8267 if (level >= 0 && level <= 7) {
8268 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8269 return -EIO;
8270 } else
8271 return -EINVAL;
8272 break;
8273
8274 case TPACPI_FAN_WR_ACPI_FANS:
8275 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008276 if (!(level & TP_EC_FAN_AUTO) &&
8277 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008278 ((level < 0) || (level > 7)))
8279 return -EINVAL;
8280
8281 /* safety net should the EC not support AUTO
8282 * or FULLSPEED mode bits and just ignore them */
8283 if (level & TP_EC_FAN_FULLSPEED)
8284 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01008285 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008286 level |= 4; /* safety min speed 4 */
8287
8288 if (!acpi_ec_write(fan_status_offset, level))
8289 return -EIO;
8290 else
8291 tp_features.fan_ctrl_status_undef = 0;
8292 break;
8293
8294 default:
8295 return -ENXIO;
8296 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008297
8298 vdbg_printk(TPACPI_DBG_FAN,
8299 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008300 return 0;
8301}
8302
8303static int fan_set_level_safe(int level)
8304{
8305 int rc;
8306
8307 if (!fan_control_allowed)
8308 return -EPERM;
8309
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008310 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008311 return -ERESTARTSYS;
8312
8313 if (level == TPACPI_FAN_LAST_LEVEL)
8314 level = fan_control_desired_level;
8315
8316 rc = fan_set_level(level);
8317 if (!rc)
8318 fan_update_desired_level(level);
8319
8320 mutex_unlock(&fan_mutex);
8321 return rc;
8322}
8323
8324static int fan_set_enable(void)
8325{
8326 u8 s;
8327 int rc;
8328
8329 if (!fan_control_allowed)
8330 return -EPERM;
8331
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008332 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008333 return -ERESTARTSYS;
8334
8335 switch (fan_control_access_mode) {
8336 case TPACPI_FAN_WR_ACPI_FANS:
8337 case TPACPI_FAN_WR_TPEC:
8338 rc = fan_get_status(&s);
8339 if (rc < 0)
8340 break;
8341
8342 /* Don't go out of emergency fan mode */
8343 if (s != 7) {
8344 s &= 0x07;
8345 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8346 }
8347
8348 if (!acpi_ec_write(fan_status_offset, s))
8349 rc = -EIO;
8350 else {
8351 tp_features.fan_ctrl_status_undef = 0;
8352 rc = 0;
8353 }
8354 break;
8355
8356 case TPACPI_FAN_WR_ACPI_SFAN:
8357 rc = fan_get_status(&s);
8358 if (rc < 0)
8359 break;
8360
8361 s &= 0x07;
8362
8363 /* Set fan to at least level 4 */
8364 s |= 4;
8365
8366 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008367 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008368 else
8369 rc = 0;
8370 break;
8371
8372 default:
8373 rc = -ENXIO;
8374 }
8375
8376 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008377
8378 if (!rc)
8379 vdbg_printk(TPACPI_DBG_FAN,
8380 "fan control: set fan control register to 0x%02x\n",
8381 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008382 return rc;
8383}
8384
8385static int fan_set_disable(void)
8386{
8387 int rc;
8388
8389 if (!fan_control_allowed)
8390 return -EPERM;
8391
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008392 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008393 return -ERESTARTSYS;
8394
8395 rc = 0;
8396 switch (fan_control_access_mode) {
8397 case TPACPI_FAN_WR_ACPI_FANS:
8398 case TPACPI_FAN_WR_TPEC:
8399 if (!acpi_ec_write(fan_status_offset, 0x00))
8400 rc = -EIO;
8401 else {
8402 fan_control_desired_level = 0;
8403 tp_features.fan_ctrl_status_undef = 0;
8404 }
8405 break;
8406
8407 case TPACPI_FAN_WR_ACPI_SFAN:
8408 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8409 rc = -EIO;
8410 else
8411 fan_control_desired_level = 0;
8412 break;
8413
8414 default:
8415 rc = -ENXIO;
8416 }
8417
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008418 if (!rc)
8419 vdbg_printk(TPACPI_DBG_FAN,
8420 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008421
8422 mutex_unlock(&fan_mutex);
8423 return rc;
8424}
8425
8426static int fan_set_speed(int speed)
8427{
8428 int rc;
8429
8430 if (!fan_control_allowed)
8431 return -EPERM;
8432
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008433 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008434 return -ERESTARTSYS;
8435
8436 rc = 0;
8437 switch (fan_control_access_mode) {
8438 case TPACPI_FAN_WR_ACPI_FANS:
8439 if (speed >= 0 && speed <= 65535) {
8440 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8441 speed, speed, speed))
8442 rc = -EIO;
8443 } else
8444 rc = -EINVAL;
8445 break;
8446
8447 default:
8448 rc = -ENXIO;
8449 }
8450
8451 mutex_unlock(&fan_mutex);
8452 return rc;
8453}
8454
8455static void fan_watchdog_reset(void)
8456{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008457 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8458 return;
8459
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008460 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008461 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8462 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8463 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8464 else
8465 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008466}
8467
8468static void fan_watchdog_fire(struct work_struct *ignored)
8469{
8470 int rc;
8471
8472 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8473 return;
8474
Joe Perches0978e012011-04-04 10:06:25 -07008475 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008476 rc = fan_set_enable();
8477 if (rc < 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008478 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8479 rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008480 /* reschedule for later */
8481 fan_watchdog_reset();
8482 }
8483}
8484
8485/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008486 * SYSFS fan layout: hwmon compatible (device)
8487 *
8488 * pwm*_enable:
8489 * 0: "disengaged" mode
8490 * 1: manual mode
8491 * 2: native EC "auto" mode (recommended, hardware default)
8492 *
8493 * pwm*: set speed in manual mode, ignored otherwise.
8494 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8495 * interpolation.
8496 *
8497 * fan*_input: tachometer reading, RPM
8498 *
8499 *
8500 * SYSFS fan layout: extensions
8501 *
8502 * fan_watchdog (driver):
8503 * fan watchdog interval in seconds, 0 disables (default), max 120
8504 */
8505
8506/* sysfs fan pwm1_enable ----------------------------------------------- */
8507static ssize_t fan_pwm1_enable_show(struct device *dev,
8508 struct device_attribute *attr,
8509 char *buf)
8510{
8511 int res, mode;
8512 u8 status;
8513
8514 res = fan_get_status_safe(&status);
8515 if (res)
8516 return res;
8517
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008518 if (status & TP_EC_FAN_FULLSPEED) {
8519 mode = 0;
8520 } else if (status & TP_EC_FAN_AUTO) {
8521 mode = 2;
8522 } else
8523 mode = 1;
8524
8525 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8526}
8527
8528static ssize_t fan_pwm1_enable_store(struct device *dev,
8529 struct device_attribute *attr,
8530 const char *buf, size_t count)
8531{
8532 unsigned long t;
8533 int res, level;
8534
8535 if (parse_strtoul(buf, 2, &t))
8536 return -EINVAL;
8537
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008538 tpacpi_disclose_usertask("hwmon pwm1_enable",
8539 "set fan mode to %lu\n", t);
8540
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008541 switch (t) {
8542 case 0:
8543 level = TP_EC_FAN_FULLSPEED;
8544 break;
8545 case 1:
8546 level = TPACPI_FAN_LAST_LEVEL;
8547 break;
8548 case 2:
8549 level = TP_EC_FAN_AUTO;
8550 break;
8551 case 3:
8552 /* reserved for software-controlled auto mode */
8553 return -ENOSYS;
8554 default:
8555 return -EINVAL;
8556 }
8557
8558 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008559 if (res == -ENXIO)
8560 return -EINVAL;
8561 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008562 return res;
8563
8564 fan_watchdog_reset();
8565
8566 return count;
8567}
8568
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008569static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8570 fan_pwm1_enable_show, fan_pwm1_enable_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008571
8572/* sysfs fan pwm1 ------------------------------------------------------ */
8573static ssize_t fan_pwm1_show(struct device *dev,
8574 struct device_attribute *attr,
8575 char *buf)
8576{
8577 int res;
8578 u8 status;
8579
8580 res = fan_get_status_safe(&status);
8581 if (res)
8582 return res;
8583
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008584 if ((status &
8585 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8586 status = fan_control_desired_level;
8587
8588 if (status > 7)
8589 status = 7;
8590
8591 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8592}
8593
8594static ssize_t fan_pwm1_store(struct device *dev,
8595 struct device_attribute *attr,
8596 const char *buf, size_t count)
8597{
8598 unsigned long s;
8599 int rc;
8600 u8 status, newlevel;
8601
8602 if (parse_strtoul(buf, 255, &s))
8603 return -EINVAL;
8604
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008605 tpacpi_disclose_usertask("hwmon pwm1",
8606 "set fan speed to %lu\n", s);
8607
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008608 /* scale down from 0-255 to 0-7 */
8609 newlevel = (s >> 5) & 0x07;
8610
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008611 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008612 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008613
8614 rc = fan_get_status(&status);
8615 if (!rc && (status &
8616 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8617 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008618 if (rc == -ENXIO)
8619 rc = -EINVAL;
8620 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008621 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008622 fan_watchdog_reset();
8623 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008624 }
8625
8626 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008627 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008628}
8629
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008630static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008631
8632/* sysfs fan fan1_input ------------------------------------------------ */
8633static ssize_t fan_fan1_input_show(struct device *dev,
8634 struct device_attribute *attr,
8635 char *buf)
8636{
8637 int res;
8638 unsigned int speed;
8639
8640 res = fan_get_speed(&speed);
8641 if (res < 0)
8642 return res;
8643
8644 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8645}
8646
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008647static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008648
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008649/* sysfs fan fan2_input ------------------------------------------------ */
8650static ssize_t fan_fan2_input_show(struct device *dev,
8651 struct device_attribute *attr,
8652 char *buf)
8653{
8654 int res;
8655 unsigned int speed;
8656
8657 res = fan2_get_speed(&speed);
8658 if (res < 0)
8659 return res;
8660
8661 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8662}
8663
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008664static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008665
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008666/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008667static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008668{
8669 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8670}
8671
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008672static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8673 size_t count)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008674{
8675 unsigned long t;
8676
8677 if (parse_strtoul(buf, 120, &t))
8678 return -EINVAL;
8679
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008680 if (!fan_control_allowed)
8681 return -EPERM;
8682
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008683 fan_watchdog_maxinterval = t;
8684 fan_watchdog_reset();
8685
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008686 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8687
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008688 return count;
8689}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008690static DRIVER_ATTR_RW(fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008691
8692/* --------------------------------------------------------------------- */
8693static struct attribute *fan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008694 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8695 &dev_attr_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008696 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008697 NULL
8698};
8699
8700static const struct attribute_group fan_attr_group = {
8701 .attrs = fan_attributes,
8702};
8703
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008704#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8705#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008706
8707#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8708 { .vendor = PCI_VENDOR_ID_IBM, \
8709 .bios = TPACPI_MATCH_ANY, \
8710 .ec = TPID(__id1, __id2), \
8711 .quirks = __quirks }
8712
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008713#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8714 { .vendor = PCI_VENDOR_ID_LENOVO, \
8715 .bios = TPACPI_MATCH_ANY, \
8716 .ec = TPID(__id1, __id2), \
8717 .quirks = __quirks }
8718
Alexander Kappnera986c752018-04-02 11:27:01 -07008719#define TPACPI_FAN_QB(__id1, __id2, __quirks) \
8720 { .vendor = PCI_VENDOR_ID_LENOVO, \
8721 .bios = TPID(__id1, __id2), \
8722 .ec = TPACPI_MATCH_ANY, \
8723 .quirks = __quirks }
8724
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008725static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8726 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8727 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8728 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8729 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008730 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Alexander Kappnera986c752018-04-02 11:27:01 -07008731 TPACPI_FAN_QB('N', '1', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008732};
8733
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008734#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008735#undef TPACPI_FAN_QI
Alexander Kappnera986c752018-04-02 11:27:01 -07008736#undef TPACPI_FAN_QB
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008737
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008738static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008739{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008740 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008741 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008742
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008743 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8744 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008745
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008746 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008747 fan_status_access_mode = TPACPI_FAN_NONE;
8748 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008749 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008750 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008751 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008752 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008753 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008754
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008755 if (tpacpi_is_ibm()) {
8756 TPACPI_ACPIHANDLE_INIT(fans);
8757 TPACPI_ACPIHANDLE_INIT(gfan);
8758 TPACPI_ACPIHANDLE_INIT(sfan);
8759 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008760
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008761 quirks = tpacpi_check_quirks(fan_quirk_table,
8762 ARRAY_SIZE(fan_quirk_table));
8763
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008764 if (gfan_handle) {
8765 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008766 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008767 } else {
8768 /* all other ThinkPads: note that even old-style
8769 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008770 if (likely(acpi_ec_read(fan_status_offset,
8771 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008772 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008773 if (quirks & TPACPI_FAN_Q1)
8774 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008775 if (quirks & TPACPI_FAN_2FAN) {
8776 tp_features.second_fan = 1;
8777 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8778 "secondary fan support enabled\n");
8779 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008780 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008781 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008782 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008783 }
8784 }
8785
8786 if (sfan_handle) {
8787 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008788 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008789 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008790 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008791 } else {
8792 if (!gfan_handle) {
8793 /* gfan without sfan means no fan control */
8794 /* all other models implement TP EC 0x2f control */
8795
8796 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008797 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008798 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008799 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008800 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008801 TPACPI_FAN_CMD_SPEED |
8802 TPACPI_FAN_CMD_LEVEL |
8803 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008804 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008805 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008806 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008807 TPACPI_FAN_CMD_LEVEL |
8808 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008809 }
8810 }
8811 }
8812
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008813 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8814 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008815 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8816 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8817 fan_status_access_mode, fan_control_access_mode);
8818
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008819 /* fan control master switch */
8820 if (!fan_control_allowed) {
8821 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8822 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008823 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008824 "fan control features disabled by parameter\n");
8825 }
8826
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008827 /* update fan_control_desired_level */
8828 if (fan_status_access_mode != TPACPI_FAN_NONE)
8829 fan_get_status_safe(NULL);
8830
8831 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8832 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008833 if (tp_features.second_fan) {
8834 /* attach second fan tachometer */
8835 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008836 &dev_attr_fan2_input.attr;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008837 }
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008838 rc = sysfs_create_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008839 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008840 if (rc < 0)
8841 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008842
8843 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8844 &driver_attr_fan_watchdog);
8845 if (rc < 0) {
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008846 sysfs_remove_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008847 &fan_attr_group);
8848 return rc;
8849 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008850 return 0;
8851 } else
8852 return 1;
8853}
8854
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008855static void fan_exit(void)
8856{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008857 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008858 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008859
8860 /* FIXME: can we really do this unconditionally? */
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008861 sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008862 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8863 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008864
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008865 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008866 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008867}
8868
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008869static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008870{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008871 int rc;
8872
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008873 if (!fan_control_allowed)
8874 return;
8875
8876 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008877 fan_control_resume_level = 0;
8878 rc = fan_get_status_safe(&fan_control_resume_level);
8879 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008880 pr_notice("failed to read fan level for later restore during resume: %d\n",
8881 rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008882
8883 /* if it is undefined, don't attempt to restore it.
8884 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008885 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008886 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008887}
8888
8889static void fan_resume(void)
8890{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008891 u8 current_level = 7;
8892 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008893 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008894
8895 /* DSDT *always* updates status on resume */
8896 tp_features.fan_ctrl_status_undef = 0;
8897
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008898 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008899 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008900 (fan_get_status_safe(&current_level) < 0))
8901 return;
8902
8903 switch (fan_control_access_mode) {
8904 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008905 /* never decrease fan level */
8906 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008907 break;
8908 case TPACPI_FAN_WR_ACPI_FANS:
8909 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008910 /* never decrease fan level, scale is:
8911 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8912 *
8913 * We expect the firmware to set either 7 or AUTO, but we
8914 * handle FULLSPEED out of paranoia.
8915 *
8916 * So, we can safely only restore FULLSPEED or 7, anything
8917 * else could slow the fan. Restoring AUTO is useless, at
8918 * best that's exactly what the DSDT already set (it is the
8919 * slower it uses).
8920 *
8921 * Always keep in mind that the DSDT *will* have set the
8922 * fans to what the vendor supposes is the best level. We
8923 * muck with it only to speed the fan up.
8924 */
8925 if (fan_control_resume_level != 7 &&
8926 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8927 return;
8928 else
8929 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8930 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008931 break;
8932 default:
8933 return;
8934 }
8935 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008936 pr_notice("restoring fan level to 0x%02x\n",
8937 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008938 rc = fan_set_level_safe(fan_control_resume_level);
8939 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008940 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008941 }
8942}
8943
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008944static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008945{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008946 int rc;
8947 u8 status;
8948 unsigned int speed = 0;
8949
8950 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008951 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008952 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008953 rc = fan_get_status_safe(&status);
8954 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008955 return rc;
8956
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008957 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008958 "level:\t\t%d\n",
8959 (status != 0) ? "enabled" : "disabled", status);
8960 break;
8961
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008962 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008963 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008964 rc = fan_get_status_safe(&status);
8965 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008966 return rc;
8967
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008968 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008969 (status != 0) ? "enabled" : "disabled");
8970
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008971 rc = fan_get_speed(&speed);
8972 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008973 return rc;
8974
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008975 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008976
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008977 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008978 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008979 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008980 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008981 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008982 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008983 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008984 break;
8985
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008986 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008987 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008988 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008989 }
8990
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008991 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008992 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008993
8994 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008995 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008996 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008997 break;
8998
8999 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009000 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009001 break;
9002 }
9003 }
9004
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009005 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009006 seq_printf(m, "commands:\tenable, disable\n"
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009007 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009008
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009009 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009010 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009011
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009012 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009013}
9014
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009015static int fan_write_cmd_level(const char *cmd, int *rc)
9016{
9017 int level;
9018
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02009019 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009020 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03009021 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009022 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009023 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02009024 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009025 return 0;
9026
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009027 *rc = fan_set_level_safe(level);
9028 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009029 pr_err("level command accepted for unsupported access mode %d\n",
9030 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009031 else if (!*rc)
9032 tpacpi_disclose_usertask("procfs fan",
9033 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009034
9035 return 1;
9036}
9037
9038static int fan_write_cmd_enable(const char *cmd, int *rc)
9039{
9040 if (strlencmp(cmd, "enable") != 0)
9041 return 0;
9042
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009043 *rc = fan_set_enable();
9044 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009045 pr_err("enable command accepted for unsupported access mode %d\n",
9046 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009047 else if (!*rc)
9048 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009049
9050 return 1;
9051}
9052
9053static int fan_write_cmd_disable(const char *cmd, int *rc)
9054{
9055 if (strlencmp(cmd, "disable") != 0)
9056 return 0;
9057
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009058 *rc = fan_set_disable();
9059 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009060 pr_err("disable command accepted for unsupported access mode %d\n",
9061 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009062 else if (!*rc)
9063 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009064
9065 return 1;
9066}
9067
9068static int fan_write_cmd_speed(const char *cmd, int *rc)
9069{
9070 int speed;
9071
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02009072 /* TODO:
9073 * Support speed <low> <medium> <high> ? */
9074
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009075 if (sscanf(cmd, "speed %d", &speed) != 1)
9076 return 0;
9077
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009078 *rc = fan_set_speed(speed);
9079 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009080 pr_err("speed command accepted for unsupported access mode %d\n",
9081 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009082 else if (!*rc)
9083 tpacpi_disclose_usertask("procfs fan",
9084 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009085
9086 return 1;
9087}
9088
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009089static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9090{
9091 int interval;
9092
9093 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9094 return 0;
9095
9096 if (interval < 0 || interval > 120)
9097 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009098 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009099 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009100 tpacpi_disclose_usertask("procfs fan",
9101 "set watchdog timer to %d\n",
9102 interval);
9103 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009104
9105 return 1;
9106}
9107
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009108static int fan_write(char *buf)
9109{
9110 char *cmd;
9111 int rc = 0;
9112
9113 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009114 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009115 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009116 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009117 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009118 fan_write_cmd_disable(cmd, &rc) ||
9119 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009120 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009121 fan_write_cmd_speed(cmd, &rc))
9122 )
9123 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009124 else if (!rc)
9125 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009126 }
9127
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009128 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009129}
9130
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009131static struct ibm_struct fan_driver_data = {
9132 .name = "fan",
9133 .read = fan_read,
9134 .write = fan_write,
9135 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03009136 .suspend = fan_suspend,
9137 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009138};
9139
David Henningsson420f9732013-10-16 23:10:31 +02009140/*************************************************************************
9141 * Mute LED subdriver
9142 */
9143
9144
9145struct tp_led_table {
9146 acpi_string name;
9147 int on_value;
9148 int off_value;
9149 int state;
9150};
9151
9152static struct tp_led_table led_tables[] = {
9153 [TPACPI_LED_MUTE] = {
9154 .name = "SSMS",
9155 .on_value = 1,
9156 .off_value = 0,
9157 },
9158 [TPACPI_LED_MICMUTE] = {
9159 .name = "MMTS",
9160 .on_value = 2,
9161 .off_value = 0,
9162 },
9163};
9164
9165static int mute_led_on_off(struct tp_led_table *t, bool state)
9166{
9167 acpi_handle temp;
9168 int output;
9169
Axel Lin3526eca2016-09-19 09:33:51 +08009170 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009171 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9172 return -EIO;
9173 }
9174
9175 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9176 state ? t->on_value : t->off_value))
9177 return -EIO;
9178
9179 t->state = state;
9180 return state;
9181}
9182
9183int tpacpi_led_set(int whichled, bool on)
9184{
9185 struct tp_led_table *t;
9186
9187 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9188 return -EINVAL;
9189
9190 t = &led_tables[whichled];
9191 if (t->state < 0 || t->state == on)
9192 return t->state;
9193 return mute_led_on_off(t, on);
9194}
9195EXPORT_SYMBOL_GPL(tpacpi_led_set);
9196
9197static int mute_led_init(struct ibm_init_struct *iibm)
9198{
9199 acpi_handle temp;
9200 int i;
9201
9202 for (i = 0; i < TPACPI_LED_MAX; i++) {
9203 struct tp_led_table *t = &led_tables[i];
9204 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9205 mute_led_on_off(t, false);
9206 else
9207 t->state = -ENODEV;
9208 }
9209 return 0;
9210}
9211
9212static void mute_led_exit(void)
9213{
9214 int i;
9215
9216 for (i = 0; i < TPACPI_LED_MAX; i++)
9217 tpacpi_led_set(i, false);
9218}
9219
Takashi Iwai119f4492014-02-12 16:32:45 +01009220static void mute_led_resume(void)
9221{
9222 int i;
9223
9224 for (i = 0; i < TPACPI_LED_MAX; i++) {
9225 struct tp_led_table *t = &led_tables[i];
9226 if (t->state >= 0)
9227 mute_led_on_off(t, t->state);
9228 }
9229}
9230
David Henningsson420f9732013-10-16 23:10:31 +02009231static struct ibm_struct mute_led_driver_data = {
9232 .name = "mute_led",
9233 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01009234 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02009235};
9236
Ognjen Galic2801b962018-02-07 15:58:44 +01009237/*
9238 * Battery Wear Control Driver
9239 * Contact: Ognjen Galic <smclt30p@gmail.com>
9240 */
9241
9242/* Metadata */
9243
9244#define GET_START "BCTG"
9245#define SET_START "BCCS"
9246#define GET_STOP "BCSG"
9247#define SET_STOP "BCSS"
9248
9249#define START_ATTR "charge_start_threshold"
9250#define STOP_ATTR "charge_stop_threshold"
9251
9252enum {
9253 BAT_ANY = 0,
9254 BAT_PRIMARY = 1,
9255 BAT_SECONDARY = 2
9256};
9257
9258enum {
9259 /* Error condition bit */
9260 METHOD_ERR = BIT(31),
9261};
9262
9263enum {
9264 /* This is used in the get/set helpers */
9265 THRESHOLD_START,
9266 THRESHOLD_STOP,
9267};
9268
9269struct tpacpi_battery_data {
9270 int charge_start;
9271 int start_support;
9272 int charge_stop;
9273 int stop_support;
9274};
9275
9276struct tpacpi_battery_driver_data {
9277 struct tpacpi_battery_data batteries[3];
9278 int individual_addressing;
9279};
9280
9281static struct tpacpi_battery_driver_data battery_info;
9282
9283/* ACPI helpers/functions/probes */
9284
9285/**
9286 * This evaluates a ACPI method call specific to the battery
9287 * ACPI extension. The specifics are that an error is marked
9288 * in the 32rd bit of the response, so we just check that here.
9289 */
9290static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9291{
9292 int response;
9293
9294 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9295 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9296 return AE_ERROR;
9297 }
9298 if (response & METHOD_ERR) {
9299 acpi_handle_err(hkey_handle,
9300 "%s evaluated but flagged as error", method);
9301 return AE_ERROR;
9302 }
9303 *ret = response;
9304 return AE_OK;
9305}
9306
9307static int tpacpi_battery_get(int what, int battery, int *ret)
9308{
9309 switch (what) {
9310 case THRESHOLD_START:
9311 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9312 return -ENODEV;
9313
9314 /* The value is in the low 8 bits of the response */
9315 *ret = *ret & 0xFF;
9316 return 0;
9317 case THRESHOLD_STOP:
9318 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9319 return -ENODEV;
9320 /* Value is in lower 8 bits */
9321 *ret = *ret & 0xFF;
9322 /*
9323 * On the stop value, if we return 0 that
9324 * does not make any sense. 0 means Default, which
9325 * means that charging stops at 100%, so we return
9326 * that.
9327 */
9328 if (*ret == 0)
9329 *ret = 100;
9330 return 0;
9331 default:
9332 pr_crit("wrong parameter: %d", what);
9333 return -EINVAL;
9334 }
9335}
9336
9337static int tpacpi_battery_set(int what, int battery, int value)
9338{
9339 int param, ret;
9340 /* The first 8 bits are the value of the threshold */
9341 param = value;
9342 /* The battery ID is in bits 8-9, 2 bits */
9343 param |= battery << 8;
9344
9345 switch (what) {
9346 case THRESHOLD_START:
9347 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9348 pr_err("failed to set charge threshold on battery %d",
9349 battery);
9350 return -ENODEV;
9351 }
9352 return 0;
9353 case THRESHOLD_STOP:
9354 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9355 pr_err("failed to set stop threshold: %d", battery);
9356 return -ENODEV;
9357 }
9358 return 0;
9359 default:
9360 pr_crit("wrong parameter: %d", what);
9361 return -EINVAL;
9362 }
9363}
9364
9365static int tpacpi_battery_probe(int battery)
9366{
9367 int ret = 0;
9368
9369 memset(&battery_info, 0, sizeof(struct tpacpi_battery_driver_data));
9370 /*
9371 * 1) Get the current start threshold
9372 * 2) Check for support
9373 * 3) Get the current stop threshold
9374 * 4) Check for support
9375 */
9376 if (acpi_has_method(hkey_handle, GET_START)) {
9377 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9378 pr_err("Error probing battery %d\n", battery);
9379 return -ENODEV;
9380 }
9381 /* Individual addressing is in bit 9 */
9382 if (ret & BIT(9))
9383 battery_info.individual_addressing = true;
9384 /* Support is marked in bit 8 */
9385 if (ret & BIT(8))
9386 battery_info.batteries[battery].start_support = 1;
9387 else
9388 return -ENODEV;
9389 if (tpacpi_battery_get(THRESHOLD_START, battery,
9390 &battery_info.batteries[battery].charge_start)) {
9391 pr_err("Error probing battery %d\n", battery);
9392 return -ENODEV;
9393 }
9394 }
9395 if (acpi_has_method(hkey_handle, GET_STOP)) {
9396 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9397 pr_err("Error probing battery stop; %d\n", battery);
9398 return -ENODEV;
9399 }
9400 /* Support is marked in bit 8 */
9401 if (ret & BIT(8))
9402 battery_info.batteries[battery].stop_support = 1;
9403 else
9404 return -ENODEV;
9405 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9406 &battery_info.batteries[battery].charge_stop)) {
9407 pr_err("Error probing battery stop: %d\n", battery);
9408 return -ENODEV;
9409 }
9410 }
9411 pr_info("battery %d registered (start %d, stop %d)",
9412 battery,
9413 battery_info.batteries[battery].charge_start,
9414 battery_info.batteries[battery].charge_stop);
9415
9416 return 0;
9417}
9418
9419/* General helper functions */
9420
9421static int tpacpi_battery_get_id(const char *battery_name)
9422{
9423
9424 if (strcmp(battery_name, "BAT0") == 0)
9425 return BAT_PRIMARY;
9426 if (strcmp(battery_name, "BAT1") == 0)
9427 return BAT_SECONDARY;
9428 /*
9429 * If for some reason the battery is not BAT0 nor is it
9430 * BAT1, we will assume it's the default, first battery,
9431 * AKA primary.
9432 */
9433 pr_warn("unknown battery %s, assuming primary", battery_name);
9434 return BAT_PRIMARY;
9435}
9436
9437/* sysfs interface */
9438
9439static ssize_t tpacpi_battery_store(int what,
9440 struct device *dev,
9441 const char *buf, size_t count)
9442{
9443 struct power_supply *supply = to_power_supply(dev);
9444 unsigned long value;
9445 int battery, rval;
9446 /*
9447 * Some systems have support for more than
9448 * one battery. If that is the case,
9449 * tpacpi_battery_probe marked that addressing
9450 * them individually is supported, so we do that
9451 * based on the device struct.
9452 *
9453 * On systems that are not supported, we assume
9454 * the primary as most of the ACPI calls fail
9455 * with "Any Battery" as the parameter.
9456 */
9457 if (battery_info.individual_addressing)
9458 /* BAT_PRIMARY or BAT_SECONDARY */
9459 battery = tpacpi_battery_get_id(supply->desc->name);
9460 else
9461 battery = BAT_PRIMARY;
9462
9463 rval = kstrtoul(buf, 10, &value);
9464 if (rval)
9465 return rval;
9466
9467 switch (what) {
9468 case THRESHOLD_START:
9469 if (!battery_info.batteries[battery].start_support)
9470 return -ENODEV;
9471 /* valid values are [0, 99] */
9472 if (value < 0 || value > 99)
9473 return -EINVAL;
9474 if (value > battery_info.batteries[battery].charge_stop)
9475 return -EINVAL;
9476 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9477 return -ENODEV;
9478 battery_info.batteries[battery].charge_start = value;
9479 return count;
9480
9481 case THRESHOLD_STOP:
9482 if (!battery_info.batteries[battery].stop_support)
9483 return -ENODEV;
9484 /* valid values are [1, 100] */
9485 if (value < 1 || value > 100)
9486 return -EINVAL;
9487 if (value < battery_info.batteries[battery].charge_start)
9488 return -EINVAL;
9489 battery_info.batteries[battery].charge_stop = value;
9490 /*
9491 * When 100 is passed to stop, we need to flip
9492 * it to 0 as that the EC understands that as
9493 * "Default", which will charge to 100%
9494 */
9495 if (value == 100)
9496 value = 0;
9497 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9498 return -EINVAL;
9499 return count;
9500 default:
9501 pr_crit("Wrong parameter: %d", what);
9502 return -EINVAL;
9503 }
9504 return count;
9505}
9506
9507static ssize_t tpacpi_battery_show(int what,
9508 struct device *dev,
9509 char *buf)
9510{
9511 struct power_supply *supply = to_power_supply(dev);
9512 int ret, battery;
9513 /*
9514 * Some systems have support for more than
9515 * one battery. If that is the case,
9516 * tpacpi_battery_probe marked that addressing
9517 * them individually is supported, so we;
9518 * based on the device struct.
9519 *
9520 * On systems that are not supported, we assume
9521 * the primary as most of the ACPI calls fail
9522 * with "Any Battery" as the parameter.
9523 */
9524 if (battery_info.individual_addressing)
9525 /* BAT_PRIMARY or BAT_SECONDARY */
9526 battery = tpacpi_battery_get_id(supply->desc->name);
9527 else
9528 battery = BAT_PRIMARY;
9529 if (tpacpi_battery_get(what, battery, &ret))
9530 return -ENODEV;
9531 return sprintf(buf, "%d\n", ret);
9532}
9533
9534static ssize_t charge_start_threshold_show(struct device *device,
9535 struct device_attribute *attr,
9536 char *buf)
9537{
9538 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9539}
9540
9541static ssize_t charge_stop_threshold_show(struct device *device,
9542 struct device_attribute *attr,
9543 char *buf)
9544{
9545 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9546}
9547
9548static ssize_t charge_start_threshold_store(struct device *dev,
9549 struct device_attribute *attr,
9550 const char *buf, size_t count)
9551{
9552 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9553}
9554
9555static ssize_t charge_stop_threshold_store(struct device *dev,
9556 struct device_attribute *attr,
9557 const char *buf, size_t count)
9558{
9559 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9560}
9561
9562static DEVICE_ATTR_RW(charge_start_threshold);
9563static DEVICE_ATTR_RW(charge_stop_threshold);
9564
9565static struct attribute *tpacpi_battery_attrs[] = {
9566 &dev_attr_charge_start_threshold.attr,
9567 &dev_attr_charge_stop_threshold.attr,
9568 NULL,
9569};
9570
9571ATTRIBUTE_GROUPS(tpacpi_battery);
9572
9573/* ACPI battery hooking */
9574
9575static int tpacpi_battery_add(struct power_supply *battery)
9576{
9577 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9578
9579 if (tpacpi_battery_probe(batteryid))
9580 return -ENODEV;
9581 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9582 return -ENODEV;
9583 return 0;
9584}
9585
9586static int tpacpi_battery_remove(struct power_supply *battery)
9587{
9588 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9589 return 0;
9590}
9591
9592static struct acpi_battery_hook battery_hook = {
9593 .add_battery = tpacpi_battery_add,
9594 .remove_battery = tpacpi_battery_remove,
9595 .name = "ThinkPad Battery Extension",
9596};
9597
9598/* Subdriver init/exit */
9599
9600static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9601{
9602 battery_hook_register(&battery_hook);
9603 return 0;
9604}
9605
9606static void tpacpi_battery_exit(void)
9607{
9608 battery_hook_unregister(&battery_hook);
9609}
9610
9611static struct ibm_struct battery_driver_data = {
9612 .name = "battery",
9613 .exit = tpacpi_battery_exit,
9614};
9615
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009616/****************************************************************************
9617 ****************************************************************************
9618 *
9619 * Infrastructure
9620 *
9621 ****************************************************************************
9622 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07009623
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009624/*
9625 * HKEY event callout for other subdrivers go here
9626 * (yes, it is ugly, but it is quick, safe, and gets the job done
9627 */
9628static void tpacpi_driver_event(const unsigned int hkey_event)
9629{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00009630 if (ibm_backlight_device) {
9631 switch (hkey_event) {
9632 case TP_HKEY_EV_BRGHT_UP:
9633 case TP_HKEY_EV_BRGHT_DOWN:
9634 tpacpi_brightness_notify_change();
9635 }
9636 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009637 if (alsa_card) {
9638 switch (hkey_event) {
9639 case TP_HKEY_EV_VOL_UP:
9640 case TP_HKEY_EV_VOL_DOWN:
9641 case TP_HKEY_EV_VOL_MUTE:
9642 volume_alsa_notify_change();
9643 }
9644 }
Hans de Goedec685e202017-02-09 16:44:13 +01009645 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9646 enum led_brightness brightness;
9647
9648 mutex_lock(&kbdlight_mutex);
9649
9650 /*
9651 * Check the brightness actually changed, setting the brightness
9652 * through kbdlight_set_level() also triggers this event.
9653 */
9654 brightness = kbdlight_sysfs_get(NULL);
9655 if (kbdlight_brightness != brightness) {
9656 kbdlight_brightness = brightness;
9657 led_classdev_notify_brightness_hw_changed(
9658 &tpacpi_led_kbdlight.led_classdev, brightness);
9659 }
9660
9661 mutex_unlock(&kbdlight_mutex);
9662 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009663}
9664
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009665static void hotkey_driver_event(const unsigned int scancode)
9666{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03009667 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009668}
9669
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009670/* --------------------------------------------------------------------- */
9671
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009672/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03009673static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009674
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009675/*
9676 * Module and infrastructure proble, init and exit handling
9677 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009678
Rusty Russell90ab5ee2012-01-13 09:32:20 +10309679static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009680
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009681#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009682static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009683{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009684 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009685
Jan van den Berg72a979f2014-09-17 00:01:08 +02009686 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009687}
9688#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9689
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009690static void ibm_exit(struct ibm_struct *ibm)
9691{
9692 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9693
9694 list_del_init(&ibm->all_drivers);
9695
9696 if (ibm->flags.acpi_notify_installed) {
9697 dbg_printk(TPACPI_DBG_EXIT,
9698 "%s: acpi_remove_notify_handler\n", ibm->name);
9699 BUG_ON(!ibm->acpi);
9700 acpi_remove_notify_handler(*ibm->acpi->handle,
9701 ibm->acpi->type,
9702 dispatch_acpi_notify);
9703 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009704 }
9705
9706 if (ibm->flags.proc_created) {
9707 dbg_printk(TPACPI_DBG_EXIT,
9708 "%s: remove_proc_entry\n", ibm->name);
9709 remove_proc_entry(ibm->name, proc_dir);
9710 ibm->flags.proc_created = 0;
9711 }
9712
9713 if (ibm->flags.acpi_driver_registered) {
9714 dbg_printk(TPACPI_DBG_EXIT,
9715 "%s: acpi_bus_unregister_driver\n", ibm->name);
9716 BUG_ON(!ibm->acpi);
9717 acpi_bus_unregister_driver(ibm->acpi->driver);
9718 kfree(ibm->acpi->driver);
9719 ibm->acpi->driver = NULL;
9720 ibm->flags.acpi_driver_registered = 0;
9721 }
9722
9723 if (ibm->flags.init_called && ibm->exit) {
9724 ibm->exit();
9725 ibm->flags.init_called = 0;
9726 }
9727
9728 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9729}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009730
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009731static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009732{
9733 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009734 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735 struct proc_dir_entry *entry;
9736
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009737 BUG_ON(ibm == NULL);
9738
9739 INIT_LIST_HEAD(&ibm->all_drivers);
9740
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009741 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009742 return 0;
9743
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009744 dbg_printk(TPACPI_DBG_INIT,
9745 "probing for %s\n", ibm->name);
9746
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009747 if (iibm->init) {
9748 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009749 if (ret > 0)
9750 return 0; /* probe failed */
9751 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009752 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009753
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009754 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009755 }
9756
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009757 if (ibm->acpi) {
9758 if (ibm->acpi->hid) {
9759 ret = register_tpacpi_subdriver(ibm);
9760 if (ret)
9761 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009762 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009763
9764 if (ibm->acpi->notify) {
9765 ret = setup_acpi_notify(ibm);
9766 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07009767 pr_notice("disabling subdriver %s\n",
9768 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009769 ret = 0;
9770 goto err_out;
9771 }
9772 if (ret < 0)
9773 goto err_out;
9774 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009775 }
9776
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009777 dbg_printk(TPACPI_DBG_INIT,
9778 "%s installed\n", ibm->name);
9779
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009780 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04009781 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009782
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009783 if (!mode)
9784 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009785 if (ibm->write)
9786 mode |= S_IWUSR;
9787 entry = proc_create_data(ibm->name, mode, proc_dir,
9788 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009789 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07009790 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009791 ret = -ENODEV;
9792 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009793 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009794 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009797 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9798
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009800
9801err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009802 dbg_printk(TPACPI_DBG_INIT,
9803 "%s: at error exit path with result %d\n",
9804 ibm->name, ret);
9805
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009806 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +02009807 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808}
9809
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009810/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009811
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009812static bool __pure __init tpacpi_is_fw_digit(const char c)
9813{
9814 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9815}
9816
Jan van den Berg72a979f2014-09-17 00:01:08 +02009817static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009818 const char t)
9819{
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009820 /*
9821 * Most models: xxyTkkWW (#.##c)
9822 * Ancient 570/600 and -SL lacks (#.##c)
9823 */
9824 if (s && strlen(s) >= 8 &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009825 tpacpi_is_fw_digit(s[0]) &&
9826 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009827 s[2] == t &&
9828 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009829 tpacpi_is_fw_digit(s[4]) &&
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009830 tpacpi_is_fw_digit(s[5]))
9831 return true;
9832
9833 /* New models: xxxyTkkW (#.##c); T550 and some others */
9834 return s && strlen(s) >= 8 &&
9835 tpacpi_is_fw_digit(s[0]) &&
9836 tpacpi_is_fw_digit(s[1]) &&
9837 tpacpi_is_fw_digit(s[2]) &&
9838 s[3] == t &&
9839 (s[4] == 'T' || s[4] == 'N') &&
9840 tpacpi_is_fw_digit(s[5]) &&
9841 tpacpi_is_fw_digit(s[6]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009842}
9843
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009844/* returns 0 - probe ok, or < 0 - probe error.
9845 * Probe ok doesn't mean thinkpad found.
9846 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9847static int __must_check __init get_thinkpad_model_data(
9848 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009849{
Jeff Garzik18552562007-10-03 15:15:40 -04009850 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009851 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009852 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009853
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009854 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009855 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009856
9857 memset(tp, 0, sizeof(*tp));
9858
9859 if (dmi_name_in_vendors("IBM"))
9860 tp->vendor = PCI_VENDOR_ID_IBM;
9861 else if (dmi_name_in_vendors("LENOVO"))
9862 tp->vendor = PCI_VENDOR_ID_LENOVO;
9863 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009864 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009865
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009866 s = dmi_get_system_info(DMI_BIOS_VERSION);
9867 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9868 if (s && !tp->bios_version_str)
9869 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009870
9871 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009872 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9873 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009874 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009875
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009876 tp->bios_model = tp->bios_version_str[0]
9877 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009878 tp->bios_release = (tp->bios_version_str[4] << 8)
9879 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009880
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009881 /*
9882 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9883 * X32 or newer, all Z series; Some models must have an
9884 * up-to-date BIOS or they will not be detected.
9885 *
9886 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9887 */
9888 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009889 if (sscanf(dev->name,
9890 "IBM ThinkPad Embedded Controller -[%17c",
9891 ec_fw_string) == 1) {
9892 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9893 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009894
9895 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009896 if (!tp->ec_version_str)
9897 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009898
9899 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9900 tp->ec_model = ec_fw_string[0]
9901 | (ec_fw_string[1] << 8);
9902 tp->ec_release = (ec_fw_string[4] << 8)
9903 | ec_fw_string[5];
9904 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009905 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
Joe Perches0978e012011-04-04 10:06:25 -07009906 ec_fw_string);
9907 pr_notice("please report this to %s\n",
9908 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009909 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009910 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009911 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009912 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009913
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009914 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009915 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009916 tp->model_str = kstrdup(s, GFP_KERNEL);
9917 if (!tp->model_str)
9918 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009919 } else {
9920 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009921 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009922 tp->model_str = kstrdup(s, GFP_KERNEL);
9923 if (!tp->model_str)
9924 return -ENOMEM;
9925 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009926 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03009927
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009928 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9929 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9930 if (s && !tp->nummodel_str)
9931 return -ENOMEM;
9932
9933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009934}
9935
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009936static int __init probe_for_thinkpad(void)
9937{
9938 int is_thinkpad;
9939
9940 if (acpi_disabled)
9941 return -ENODEV;
9942
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009943 /* It would be dangerous to run the driver in this case */
9944 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9945 return -ENODEV;
9946
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009947 /*
9948 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009949 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009950 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009951 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9952 (thinkpad_id.ec_model != 0) ||
9953 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009954
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03009955 /* The EC handler is required */
9956 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009957 if (!ec_handle) {
9958 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07009959 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009960 return -ENODEV;
9961 }
9962
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009963 if (!is_thinkpad && !force_load)
9964 return -ENODEV;
9965
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009966 return 0;
9967}
9968
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009969static void __init thinkpad_acpi_init_banner(void)
9970{
Joe Perches0978e012011-04-04 10:06:25 -07009971 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9972 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009973
Joe Perches0978e012011-04-04 10:06:25 -07009974 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009975 (thinkpad_id.bios_version_str) ?
9976 thinkpad_id.bios_version_str : "unknown",
9977 (thinkpad_id.ec_version_str) ?
9978 thinkpad_id.ec_version_str : "unknown");
9979
9980 BUG_ON(!thinkpad_id.vendor);
9981
9982 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07009983 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009984 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9985 "IBM" : ((thinkpad_id.vendor ==
9986 PCI_VENDOR_ID_LENOVO) ?
9987 "Lenovo" : "Unknown vendor"),
9988 thinkpad_id.model_str,
9989 (thinkpad_id.nummodel_str) ?
9990 thinkpad_id.nummodel_str : "unknown");
9991}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009992
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009993/* Module init, exit, parameters */
9994
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009995static struct ibm_init_struct ibms_init[] __initdata = {
9996 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009997 .data = &thinkpad_acpi_driver_data,
9998 },
9999 {
10000 .init = hotkey_init,
10001 .data = &hotkey_driver_data,
10002 },
10003 {
10004 .init = bluetooth_init,
10005 .data = &bluetooth_driver_data,
10006 },
10007 {
10008 .init = wan_init,
10009 .data = &wan_driver_data,
10010 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020010011 {
10012 .init = uwb_init,
10013 .data = &uwb_driver_data,
10014 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -020010015#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010016 {
10017 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -030010018 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010019 .data = &video_driver_data,
10020 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -020010021#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010022 {
Pali Rohárbb28f3d52015-12-30 23:27:41 +010010023 .init = kbdlight_init,
10024 .data = &kbdlight_driver_data,
10025 },
10026 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010027 .init = light_init,
10028 .data = &light_driver_data,
10029 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010030 {
10031 .init = cmos_init,
10032 .data = &cmos_driver_data,
10033 },
10034 {
10035 .init = led_init,
10036 .data = &led_driver_data,
10037 },
10038 {
10039 .init = beep_init,
10040 .data = &beep_driver_data,
10041 },
10042 {
10043 .init = thermal_init,
10044 .data = &thermal_driver_data,
10045 },
10046 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010047 .init = brightness_init,
10048 .data = &brightness_driver_data,
10049 },
10050 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020010051 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010052 .data = &volume_driver_data,
10053 },
10054 {
10055 .init = fan_init,
10056 .data = &fan_driver_data,
10057 },
David Henningsson420f9732013-10-16 23:10:31 +020010058 {
10059 .init = mute_led_init,
10060 .data = &mute_led_driver_data,
10061 },
Ognjen Galic2801b962018-02-07 15:58:44 +010010062 {
10063 .init = tpacpi_battery_init,
10064 .data = &battery_driver_data,
10065 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010066};
10067
Kees Cooke4dca7b2017-10-17 19:04:42 -070010068static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010069{
10070 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010071 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010072
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -020010073 if (!kp || !kp->name || !val)
10074 return -EINVAL;
10075
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010076 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10077 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -020010078 WARN_ON(ibm == NULL);
10079
10080 if (!ibm || !ibm->name)
10081 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010082
10083 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
10084 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010085 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010086 strcpy(ibms_init[i].param, val);
10087 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010088 return 0;
10089 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010090 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010091
10092 return -EINVAL;
10093}
10094
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010095module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010096MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010097 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010098
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -030010099module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010100MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -030010101
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010102module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010103MODULE_PARM_DESC(force_load,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010104 "Attempts to load the driver even on a mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -030010105
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010106module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010107MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010108 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -030010109
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010110module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010111MODULE_PARM_DESC(brightness_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010112 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -030010113
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010114module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010115MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010116 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -020010117
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -020010118#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020010119module_param_named(volume_mode, volume_mode, uint, 0444);
10120MODULE_PARM_DESC(volume_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010121 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020010122
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020010123module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
10124MODULE_PARM_DESC(volume_capabilities,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010125 "Selects the mixer capabilites: 0=auto, 1=volume and mute, 2=mute only");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020010126
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -020010127module_param_named(volume_control, volume_control_allowed, bool, 0444);
10128MODULE_PARM_DESC(volume_control,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010129 "Enables software override for the console audio control when true");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -020010130
Andy Lutomirski9a417ec2014-10-17 17:04:29 -070010131module_param_named(software_mute, software_mute_requested, bool, 0444);
10132MODULE_PARM_DESC(software_mute,
10133 "Request full software mute control");
10134
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020010135/* ALSA module API parameters */
10136module_param_named(index, alsa_index, int, 0444);
10137MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
10138module_param_named(id, alsa_id, charp, 0444);
10139MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
10140module_param_named(enable, alsa_enable, bool, 0444);
10141MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -020010142#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020010143
Andy Shevchenko9b4bbbd2017-05-09 17:17:21 +030010144/* The module parameter can't be read back, that's why 0 is used here */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010145#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010146 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010147 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010148
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010149TPACPI_PARAM(hotkey);
10150TPACPI_PARAM(bluetooth);
10151TPACPI_PARAM(video);
10152TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010153TPACPI_PARAM(cmos);
10154TPACPI_PARAM(led);
10155TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010156TPACPI_PARAM(brightness);
10157TPACPI_PARAM(volume);
10158TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010159
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010160#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010161module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010162MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
10163module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
10164MODULE_PARM_DESC(wlsw_state,
10165 "Initial state of the emulated WLSW switch");
10166
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010167module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010168MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
10169module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
10170MODULE_PARM_DESC(bluetooth_state,
10171 "Initial state of the emulated bluetooth switch");
10172
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010173module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010174MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
10175module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
10176MODULE_PARM_DESC(wwan_state,
10177 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020010178
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010179module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020010180MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
10181module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
10182MODULE_PARM_DESC(uwb_state,
10183 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010184#endif
10185
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010186static void thinkpad_acpi_module_exit(void)
10187{
10188 struct ibm_struct *ibm, *itmp;
10189
10190 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
10191
10192 list_for_each_entry_safe_reverse(ibm, itmp,
10193 &tpacpi_all_drivers,
10194 all_drivers) {
10195 ibm_exit(ibm);
10196 }
10197
10198 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
10199
10200 if (tpacpi_inputdev) {
10201 if (tp_features.input_device_registered)
10202 input_unregister_device(tpacpi_inputdev);
10203 else
10204 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +100010205 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010206 }
10207
10208 if (tpacpi_hwmon)
10209 hwmon_device_unregister(tpacpi_hwmon);
10210
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010211 if (tpacpi_sensors_pdev)
10212 platform_device_unregister(tpacpi_sensors_pdev);
10213 if (tpacpi_pdev)
10214 platform_device_unregister(tpacpi_pdev);
10215
10216 if (tp_features.sensors_pdrv_attrs_registered)
10217 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
10218 if (tp_features.platform_drv_attrs_registered)
10219 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
10220
10221 if (tp_features.sensors_pdrv_registered)
10222 platform_driver_unregister(&tpacpi_hwmon_pdriver);
10223
10224 if (tp_features.platform_drv_registered)
10225 platform_driver_unregister(&tpacpi_pdriver);
10226
10227 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010228 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010229
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -030010230 if (tpacpi_wq)
10231 destroy_workqueue(tpacpi_wq);
10232
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010233 kfree(thinkpad_id.bios_version_str);
10234 kfree(thinkpad_id.ec_version_str);
10235 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +100010236 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010237}
10238
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -020010239
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010240static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010241{
10242 int ret, i;
10243
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -030010244 tpacpi_lifecycle = TPACPI_LIFE_INIT;
10245
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010246 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010247
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010248 ret = get_thinkpad_model_data(&thinkpad_id);
10249 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010250 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010251 thinkpad_acpi_module_exit();
10252 return ret;
10253 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010254 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010255 if (ret) {
10256 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010257 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010259
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010260 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010261
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030010262 thinkpad_acpi_init_banner();
10263 tpacpi_check_outdated_fw();
10264
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010265 TPACPI_ACPIHANDLE_INIT(ecrd);
10266 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010267
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -030010268 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
10269 if (!tpacpi_wq) {
10270 thinkpad_acpi_module_exit();
10271 return -ENOMEM;
10272 }
10273
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010274 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010275 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -070010276 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010277 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010278 return -ENODEV;
10279 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -040010280
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010281 ret = platform_driver_register(&tpacpi_pdriver);
10282 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010283 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010284 thinkpad_acpi_module_exit();
10285 return ret;
10286 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -030010287 tp_features.platform_drv_registered = 1;
10288
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010289 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
10290 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010291 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010292 thinkpad_acpi_module_exit();
10293 return ret;
10294 }
10295 tp_features.sensors_pdrv_registered = 1;
10296
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010297 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -030010298 if (!ret) {
10299 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010300 ret = tpacpi_create_driver_attributes(
10301 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -030010302 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010303 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010304 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010305 thinkpad_acpi_module_exit();
10306 return ret;
10307 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -030010308 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010309
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010310
10311 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010312 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010313 NULL, 0);
10314 if (IS_ERR(tpacpi_pdev)) {
10315 ret = PTR_ERR(tpacpi_pdev);
10316 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070010317 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010318 thinkpad_acpi_module_exit();
10319 return ret;
10320 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010321 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010322 TPACPI_HWMON_DRVR_NAME,
10323 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010324 if (IS_ERR(tpacpi_sensors_pdev)) {
10325 ret = PTR_ERR(tpacpi_sensors_pdev);
10326 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070010327 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010328 thinkpad_acpi_module_exit();
10329 return ret;
10330 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010331 tp_features.sensors_pdev_attrs_registered = 1;
Stanislav Fomichev6b99e352017-06-20 20:45:13 -070010332 tpacpi_hwmon = hwmon_device_register_with_groups(
10333 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
10334
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010335 if (IS_ERR(tpacpi_hwmon)) {
10336 ret = PTR_ERR(tpacpi_hwmon);
10337 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070010338 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010339 thinkpad_acpi_module_exit();
10340 return ret;
10341 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -030010342 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010343 tpacpi_inputdev = input_allocate_device();
10344 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010345 thinkpad_acpi_module_exit();
10346 return -ENOMEM;
10347 } else {
10348 /* Prepare input device, but don't register */
10349 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010350 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010351 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -030010352 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010353 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
10354 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +000010355 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010356 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -030010357
10358 /* Init subdriver dependencies */
10359 tpacpi_detect_brightness_capabilities();
10360
10361 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010362 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10363 ret = ibm_init(&ibms_init[i]);
10364 if (ret >= 0 && *ibms_init[i].param)
10365 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010366 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010367 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010368 return ret;
10369 }
10370 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -030010371
10372 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
10373
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010374 ret = input_register_device(tpacpi_inputdev);
10375 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -070010376 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010377 thinkpad_acpi_module_exit();
10378 return ret;
10379 } else {
10380 tp_features.input_device_registered = 1;
10381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010382
10383 return 0;
10384}
10385
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -030010386MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
10387
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010388/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +000010389 * This will autoload the driver in almost every ThinkPad
10390 * in widespread use.
10391 *
10392 * Only _VERY_ old models, like the 240, 240x and 570 lack
10393 * the HKEY event interface.
10394 */
10395MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
10396
10397/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010398 * DMI matching for module autoloading
10399 *
10400 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
10401 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
10402 *
10403 * Only models listed in thinkwiki will be supported, so add yours
10404 * if it is not there yet.
10405 */
10406#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +010010407 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010408
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010409/* Ancient thinkpad BIOSes have to be identified by
10410 * BIOS type or model number, and there are far less
10411 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +000010412IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010413
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +000010414MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
10415MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010416MODULE_DESCRIPTION(TPACPI_DESC);
10417MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010418MODULE_LICENSE("GPL");
10419
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010420module_init(thinkpad_acpi_module_init);
10421module_exit(thinkpad_acpi_module_exit);