blob: 1c57ee2b6d190501dd3dffe47e82fd1cf628fc4c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Joe Perches0978e012011-04-04 10:06:25 -070024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
Ognjen Galic2801b962018-02-07 15:58:44 +010026#define TPACPI_VERSION "0.26"
Stanislav Fomichev6b99e352017-06-20 20:45:13 -070027#define TPACPI_SYSFS_VERSION 0x030000
Borislav Deianov78f81cc2005-08-17 00:00:00 -040028
29/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020031 * 2007-10-20 changelog trimmed down
32 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030033 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020035 *
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030039 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040040 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020042 *
43 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040044 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
51
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020052#include <linux/kernel.h>
53#include <linux/module.h>
54#include <linux/init.h>
55#include <linux/types.h>
56#include <linux/string.h>
57#include <linux/list.h>
58#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000059#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020060#include <linux/kthread.h>
61#include <linux/freezer.h>
62#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020064#include <linux/nvram.h>
65#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020066#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020067#include <linux/sysfs.h>
68#include <linux/backlight.h>
Ognjen Galic2801b962018-02-07 15:58:44 +010069#include <linux/bitops.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020070#include <linux/fb.h>
71#include <linux/platform_device.h>
72#include <linux/hwmon.h>
73#include <linux/hwmon-sysfs.h>
74#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030075#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030076#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020077#include <linux/dmi.h>
78#include <linux/jiffies.h>
79#include <linux/workqueue.h>
Lv Zheng8b484632013-12-03 08:49:16 +080080#include <linux/acpi.h>
81#include <linux/pci_ids.h>
Ognjen Galic2801b962018-02-07 15:58:44 +010082#include <linux/power_supply.h>
Lv Zheng8b484632013-12-03 08:49:16 +080083#include <linux/thinkpad_acpi.h>
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020084#include <sound/core.h>
85#include <sound/control.h>
86#include <sound/initval.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080087#include <linux/uaccess.h>
Ognjen Galic2801b962018-02-07 15:58:44 +010088#include <acpi/battery.h>
Hans de Goedeb33c6ce2015-06-16 16:28:10 +020089#include <acpi/video.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020090
91/* ThinkPad CMOS commands */
92#define TP_CMOS_VOLUME_DOWN 0
93#define TP_CMOS_VOLUME_UP 1
94#define TP_CMOS_VOLUME_MUTE 2
95#define TP_CMOS_BRIGHTNESS_UP 4
96#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030097#define TP_CMOS_THINKLIGHT_ON 12
98#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020099
100/* NVRAM Addresses */
101enum tp_nvram_addr {
102 TP_NVRAM_ADDR_HK2 = 0x57,
103 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
104 TP_NVRAM_ADDR_VIDEO = 0x59,
105 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
106 TP_NVRAM_ADDR_MIXER = 0x60,
107};
108
109/* NVRAM bit masks */
110enum {
111 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
112 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
113 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
114 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
115 TP_NVRAM_MASK_THINKLIGHT = 0x10,
116 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
117 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
118 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
119 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
120 TP_NVRAM_MASK_MUTE = 0x40,
121 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
122 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
123 TP_NVRAM_POS_LEVEL_VOLUME = 0,
124};
125
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300126/* Misc NVRAM-related */
127enum {
128 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
129};
130
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200131/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400132#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
133#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Hui Wanga3c42a42016-11-08 16:13:23 +0800134#define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300135#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200136
137/* Input IDs */
138#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
139#define TPACPI_HKEY_INPUT_VERSION 0x4101
140
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200141/* ACPI \WGSV commands */
142enum {
143 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
144 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
145 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
146 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
147};
148
149/* TP_ACPI_WGSV_GET_STATE bits */
150enum {
151 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
152 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
153 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
154 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
155 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
156 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
157 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
158 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
159 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
160 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
161};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200162
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300163/* HKEY events */
164enum tpacpi_hkey_event_t {
165 /* Hotkey-related */
166 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
167 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
168 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
Hans de Goedec685e202017-02-09 16:44:13 +0100169 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300170 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
171 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
172 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
173
174 /* Reasons for waking up from S3/S4 */
175 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
176 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
177 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
178 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
179 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
180 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
181
182 /* Auto-sleep after eject request */
183 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
184 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
185
186 /* Misc bay events */
187 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -0300188 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
189 or port replicator */
190 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
191 dock or port replicator */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300192
193 /* User-interface events */
194 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
195 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
196 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
197 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
Lyudeb03f4d42016-11-11 15:15:03 -0500198 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
199 * enter/leave tablet mode
200 */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300201 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
202 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
203 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
204
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300205 /* Key-related user-interface events */
206 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
207 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
Xavier Naveira67ab6242015-02-10 08:45:18 +0100208 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300209
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300210 /* Thermal events */
211 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
212 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
213 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
214 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
215 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
216
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100217 /* AC-related events */
218 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300219
David Herrmann587d8622018-01-12 12:04:45 +0100220 /* Further user-interface events */
221 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
222 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
223
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300224 /* Misc */
225 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
226};
227
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200228/****************************************************************************
229 * Main driver
230 */
231
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200232#define TPACPI_NAME "thinkpad"
233#define TPACPI_DESC "ThinkPad ACPI Extras"
234#define TPACPI_FILE TPACPI_NAME "_acpi"
235#define TPACPI_URL "http://ibm-acpi.sf.net/"
236#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200237
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200238#define TPACPI_PROC_DIR "ibm"
239#define TPACPI_ACPI_EVENT_PREFIX "ibm"
240#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300241#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200242#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200243
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300244#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300245#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300246
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200247#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200248
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000249/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200250#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000251#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200252#define TPACPI_DBG_INIT 0x0001
253#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000254#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000255#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000256#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000257#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200258#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200259
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200260#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
261#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
262#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200263
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200264
265/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200266 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200267 */
268
269struct ibm_struct;
270
271struct tp_acpi_drv_struct {
272 const struct acpi_device_id *hid;
273 struct acpi_driver *driver;
274
275 void (*notify) (struct ibm_struct *, u32);
276 acpi_handle *handle;
277 u32 type;
278 struct acpi_device *device;
279};
280
281struct ibm_struct {
282 char *name;
283
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200284 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200285 int (*write) (char *);
286 void (*exit) (void);
287 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200288 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200289 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200290
291 struct list_head all_drivers;
292
293 struct tp_acpi_drv_struct *acpi;
294
295 struct {
296 u8 acpi_driver_registered:1;
297 u8 acpi_notify_installed:1;
298 u8 proc_created:1;
299 u8 init_called:1;
300 u8 experimental:1;
301 } flags;
302};
303
304struct ibm_init_struct {
305 char param[32];
306
307 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400308 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200309 struct ibm_struct *data;
310};
311
312static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200313 u32 bluetooth:1;
314 u32 hotkey:1;
315 u32 hotkey_mask:1;
316 u32 hotkey_wlsw:1;
Lyudeb3180022016-11-11 15:15:02 -0500317 enum {
318 TP_HOTKEY_TABLET_NONE = 0,
319 TP_HOTKEY_TABLET_USES_MHKG,
Benjamin Bergdda3ec02017-09-15 15:20:49 +0200320 TP_HOTKEY_TABLET_USES_GMMS,
Lyudeb3180022016-11-11 15:15:02 -0500321 } hotkey_tablet;
Pali Rohárbb28f3d52015-12-30 23:27:41 +0100322 u32 kbdlight:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200323 u32 light:1;
324 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300325 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300326 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200327 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200328 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200329 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300330 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300331 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200332 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200333 u32 input_device_registered:1;
334 u32 platform_drv_registered:1;
335 u32 platform_drv_attrs_registered:1;
336 u32 sensors_pdrv_registered:1;
337 u32 sensors_pdrv_attrs_registered:1;
338 u32 sensors_pdev_attrs_registered:1;
339 u32 hotkey_poll_active:1;
Bastien Noceraf23a5bc2015-03-02 14:45:16 +0100340 u32 has_adaptive_kbd:1;
Ognjen Galic2801b962018-02-07 15:58:44 +0100341 u32 battery:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200342} tp_features;
343
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300344static struct {
345 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200346 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300347} tp_warned;
348
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200349struct thinkpad_id_data {
350 unsigned int vendor; /* ThinkPad vendor:
351 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
352
353 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
354 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
355
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300356 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200357 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300358 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
359 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200360
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300361 char *model_str; /* ThinkPad T43 */
362 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200363};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200364static struct thinkpad_id_data thinkpad_id;
365
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300366static enum {
367 TPACPI_LIFE_INIT = 0,
368 TPACPI_LIFE_RUNNING,
369 TPACPI_LIFE_EXITING,
370} tpacpi_lifecycle;
371
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200372static int experimental;
373static u32 dbg_level;
374
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300375static struct workqueue_struct *tpacpi_wq;
376
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000377enum led_status_t {
378 TPACPI_LED_OFF = 0,
379 TPACPI_LED_ON,
380 TPACPI_LED_BLINK,
381};
382
Hans de Goede86ec0c22017-02-09 16:44:12 +0100383/* tpacpi LED class */
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300384struct tpacpi_led_classdev {
385 struct led_classdev led_classdev;
Adam Leeedf2d772013-06-08 16:51:15 +0800386 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300387};
388
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300389/* brightness level capabilities */
390static unsigned int bright_maxlvl; /* 0 = unknown */
391
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200392#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
393static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030394static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200395static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030396static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200397static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030398static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200399static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030400static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200401#endif
402
403
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000404/*************************************************************************
405 * Debugging helpers
406 */
407
Joe Perches0978e012011-04-04 10:06:25 -0700408#define dbg_printk(a_dbg_level, format, arg...) \
409do { \
410 if (dbg_level & (a_dbg_level)) \
411 printk(KERN_DEBUG pr_fmt("%s: " format), \
412 __func__, ##arg); \
413} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000414
415#ifdef CONFIG_THINKPAD_ACPI_DEBUG
416#define vdbg_printk dbg_printk
417static const char *str_supported(int is_supported);
418#else
Joe Perches0978e012011-04-04 10:06:25 -0700419static inline const char *str_supported(int is_supported) { return ""; }
420#define vdbg_printk(a_dbg_level, format, arg...) \
Joe Perchesefd85cf2015-08-26 11:13:38 -0700421 do { if (0) no_printk(format, ##arg); } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000422#endif
423
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000424static void tpacpi_log_usertask(const char * const what)
425{
Joe Perches0978e012011-04-04 10:06:25 -0700426 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
427 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000428}
429
Joe Perches0978e012011-04-04 10:06:25 -0700430#define tpacpi_disclose_usertask(what, format, arg...) \
431do { \
432 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
433 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
434 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
435 what, task_tgid_vnr(current), ## arg); \
436 } \
437} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000438
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300439/*
440 * Quirk handling helpers
441 *
442 * ThinkPad IDs and versions seen in the field so far
443 * are two-characters from the set [0-9A-Z], i.e. base 36.
444 *
445 * We use values well outside that range as specials.
446 */
447
448#define TPACPI_MATCH_ANY 0xffffU
449#define TPACPI_MATCH_UNKNOWN 0U
450
451/* TPID('1', 'Y') == 0x5931 */
452#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
453
454#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
455 { .vendor = PCI_VENDOR_ID_IBM, \
456 .bios = TPID(__id1, __id2), \
457 .ec = TPACPI_MATCH_ANY, \
458 .quirks = (__quirk) }
459
460#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
461 { .vendor = PCI_VENDOR_ID_LENOVO, \
462 .bios = TPID(__id1, __id2), \
463 .ec = TPACPI_MATCH_ANY, \
464 .quirks = (__quirk) }
465
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200466#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
467 { .vendor = PCI_VENDOR_ID_LENOVO, \
468 .bios = TPACPI_MATCH_ANY, \
469 .ec = TPID(__id1, __id2), \
470 .quirks = (__quirk) }
471
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300472struct tpacpi_quirk {
473 unsigned int vendor;
474 u16 bios;
475 u16 ec;
476 unsigned long quirks;
477};
478
479/**
480 * tpacpi_check_quirks() - search BIOS/EC version on a list
481 * @qlist: array of &struct tpacpi_quirk
482 * @qlist_size: number of elements in @qlist
483 *
484 * Iterates over a quirks list until one is found that matches the
485 * ThinkPad's vendor, BIOS and EC model.
486 *
487 * Returns 0 if nothing matches, otherwise returns the quirks field of
488 * the matching &struct tpacpi_quirk entry.
489 *
490 * The match criteria is: vendor, ec and bios much match.
491 */
492static unsigned long __init tpacpi_check_quirks(
493 const struct tpacpi_quirk *qlist,
494 unsigned int qlist_size)
495{
496 while (qlist_size) {
497 if ((qlist->vendor == thinkpad_id.vendor ||
498 qlist->vendor == TPACPI_MATCH_ANY) &&
499 (qlist->bios == thinkpad_id.bios_model ||
500 qlist->bios == TPACPI_MATCH_ANY) &&
501 (qlist->ec == thinkpad_id.ec_model ||
502 qlist->ec == TPACPI_MATCH_ANY))
503 return qlist->quirks;
504
505 qlist_size--;
506 qlist++;
507 }
508 return 0;
509}
510
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300511static inline bool __pure __init tpacpi_is_lenovo(void)
512{
513 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
514}
515
516static inline bool __pure __init tpacpi_is_ibm(void)
517{
518 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
519}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300520
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300521/****************************************************************************
522 ****************************************************************************
523 *
524 * ACPI Helpers and device model
525 *
526 ****************************************************************************
527 ****************************************************************************/
528
529/*************************************************************************
530 * ACPI basic handles
531 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300533static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300534static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200536#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700538 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300539 &parent##_handle; \
540 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200542TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
543TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200545TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
546 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400547 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
548 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300549 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400550
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200551TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400552 "^HKEY", /* R30, R31 */
553 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300554 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400555
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300556/*************************************************************************
557 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200558 */
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700561 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400564 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200565 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400566 struct acpi_buffer result, *resultp;
567 union acpi_object out_obj;
568 acpi_status status;
569 va_list ap;
570 char res_type;
571 int success;
572 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700575 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return 0;
577 }
578
579 if (*fmt == 'q') {
580 quiet = 1;
581 fmt++;
582 } else
583 quiet = 0;
584
585 res_type = *(fmt++);
586
587 params.count = 0;
588 params.pointer = &in_objs[0];
589
590 va_start(ap, fmt);
591 while (*fmt) {
592 char c = *(fmt++);
593 switch (c) {
594 case 'd': /* int */
595 in_objs[params.count].integer.value = va_arg(ap, int);
596 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
597 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400598 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300600 pr_err("acpi_evalf() called with invalid format character '%c'\n",
601 c);
Jesper Juhl21365852010-12-24 19:56:28 +0100602 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
604 }
605 }
606 va_end(ap);
607
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400608 if (res_type != 'v') {
609 result.length = sizeof(out_obj);
610 result.pointer = &out_obj;
611 resultp = &result;
612 } else
613 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400615 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400618 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300619 success = (status == AE_OK &&
620 out_obj.type == ACPI_TYPE_INTEGER);
621 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700622 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400624 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 success = status == AE_OK;
626 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400627 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300629 pr_err("acpi_evalf() called with invalid format character '%c'\n",
630 res_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return 0;
632 }
633
634 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700635 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300636 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 return success;
639}
640
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200641static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300642{
643 int v;
644
645 if (ecrd_handle) {
646 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
647 return 0;
648 *p = v;
649 } else {
650 if (ec_read(i, p) < 0)
651 return 0;
652 }
653
654 return 1;
655}
656
657static int acpi_ec_write(int i, u8 v)
658{
659 if (ecwr_handle) {
660 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
661 return 0;
662 } else {
663 if (ec_write(i, v) < 0)
664 return 0;
665 }
666
667 return 1;
668}
669
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300670static int issue_thinkpad_cmos_command(int cmos_cmd)
671{
672 if (!cmos_handle)
673 return -ENXIO;
674
675 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
676 return -EIO;
677
678 return 0;
679}
680
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300681/*************************************************************************
682 * ACPI device model
683 */
684
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200685#define TPACPI_ACPIHANDLE_INIT(object) \
686 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300687 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200688
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300689static void __init drv_acpi_handle_init(const char *name,
690 acpi_handle *handle, const acpi_handle parent,
691 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300692{
693 int i;
694 acpi_status status;
695
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300696 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
697 name);
698
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300699 for (i = 0; i < num_paths; i++) {
700 status = acpi_get_handle(parent, paths[i], handle);
701 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300702 dbg_printk(TPACPI_DBG_INIT,
703 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300704 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300705 return;
706 }
707 }
708
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300709 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
710 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300711 *handle = NULL;
712}
713
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300714static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
715 u32 level, void *context, void **return_value)
716{
Aaron Lu46445b62013-10-11 21:27:46 +0800717 struct acpi_device *dev;
718 if (!strcmp(context, "video")) {
719 if (acpi_bus_get_device(handle, &dev))
720 return AE_OK;
721 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
722 return AE_OK;
723 }
724
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300725 *(acpi_handle *)return_value = handle;
726
727 return AE_CTRL_TERMINATE;
728}
729
730static void __init tpacpi_acpi_handle_locate(const char *name,
731 const char *hid,
732 acpi_handle *handle)
733{
734 acpi_status status;
735 acpi_handle device_found;
736
Aaron Lu46445b62013-10-11 21:27:46 +0800737 BUG_ON(!name || !handle);
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300738 vdbg_printk(TPACPI_DBG_INIT,
739 "trying to locate ACPI handle for %s, using HID %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +0800740 name, hid ? hid : "NULL");
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300741
742 memset(&device_found, 0, sizeof(device_found));
743 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
744 (void *)name, &device_found);
745
746 *handle = NULL;
747
748 if (ACPI_SUCCESS(status)) {
749 *handle = device_found;
750 dbg_printk(TPACPI_DBG_INIT,
751 "Found ACPI handle for %s\n", name);
752 } else {
753 vdbg_printk(TPACPI_DBG_INIT,
754 "Could not locate an ACPI handle for %s: %s\n",
755 name, acpi_format_exception(status));
756 }
757}
758
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300759static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300760{
761 struct ibm_struct *ibm = data;
762
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300763 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
764 return;
765
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300766 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300767 return;
768
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300769 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300770}
771
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300772static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300773{
774 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300775 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300776
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300777 BUG_ON(!ibm->acpi);
778
779 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300780 return 0;
781
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300782 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300783 "setting up ACPI notify for %s\n", ibm->name);
784
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300785 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
786 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700787 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300788 return -ENODEV;
789 }
790
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700791 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300792 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200793 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300794 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300795
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300796 status = acpi_install_notify_handler(*ibm->acpi->handle,
797 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300798 if (ACPI_FAILURE(status)) {
799 if (status == AE_ALREADY_EXISTS) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300800 pr_notice("another device driver is already handling %s events\n",
801 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300802 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700803 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300804 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300805 }
806 return -ENODEV;
807 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300808 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300809 return 0;
810}
811
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300812static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300813{
814 return 0;
815}
816
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300817static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300818{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300819 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300820
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300821 dbg_printk(TPACPI_DBG_INIT,
822 "registering %s as an ACPI driver\n", ibm->name);
823
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300824 BUG_ON(!ibm->acpi);
825
826 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
827 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700828 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300829 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300830 }
831
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200832 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300833 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200834
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300835 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300836
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300837 rc = acpi_bus_register_driver(ibm->acpi->driver);
838 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700839 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200840 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300841 kfree(ibm->acpi->driver);
842 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300843 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300844 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300845
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300846 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300847}
848
849
850/****************************************************************************
851 ****************************************************************************
852 *
853 * Procfs Helpers
854 *
855 ****************************************************************************
856 ****************************************************************************/
857
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200858static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300859{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200860 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300861
862 if (!ibm || !ibm->read)
863 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200864 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300865}
866
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200867static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300868{
Al Virod9dda782013-03-31 18:16:14 -0400869 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200870}
871
872static ssize_t dispatch_proc_write(struct file *file,
873 const char __user *userbuf,
874 size_t count, loff_t *pos)
875{
Al Virod9dda782013-03-31 18:16:14 -0400876 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300877 char *kernbuf;
878 int ret;
879
880 if (!ibm || !ibm->write)
881 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300882 if (count > PAGE_SIZE - 2)
883 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300884
885 kernbuf = kmalloc(count + 2, GFP_KERNEL);
886 if (!kernbuf)
887 return -ENOMEM;
888
889 if (copy_from_user(kernbuf, userbuf, count)) {
890 kfree(kernbuf);
891 return -EFAULT;
892 }
893
894 kernbuf[count] = 0;
895 strcat(kernbuf, ",");
896 ret = ibm->write(kernbuf);
897 if (ret == 0)
898 ret = count;
899
900 kfree(kernbuf);
901
902 return ret;
903}
904
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200905static const struct file_operations dispatch_proc_fops = {
906 .owner = THIS_MODULE,
907 .open = dispatch_proc_open,
908 .read = seq_read,
909 .llseek = seq_lseek,
910 .release = single_release,
911 .write = dispatch_proc_write,
912};
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914static char *next_cmd(char **cmds)
915{
916 char *start = *cmds;
917 char *end;
918
919 while ((end = strchr(start, ',')) && end == start)
920 start = end + 1;
921
922 if (!end)
923 return NULL;
924
925 *end = 0;
926 *cmds = end + 1;
927 return start;
928}
929
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300930
931/****************************************************************************
932 ****************************************************************************
933 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300934 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300935 *
936 ****************************************************************************
937 ****************************************************************************/
938
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300939static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300940static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700941static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300942static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300943static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200944static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300945
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200946#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200947static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200948{
949 struct ibm_struct *ibm, *itmp;
950
951 list_for_each_entry_safe(ibm, itmp,
952 &tpacpi_all_drivers,
953 all_drivers) {
954 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200955 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200956 }
957
958 return 0;
959}
960
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200961static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300962{
963 struct ibm_struct *ibm, *itmp;
964
965 list_for_each_entry_safe(ibm, itmp,
966 &tpacpi_all_drivers,
967 all_drivers) {
968 if (ibm->resume)
969 (ibm->resume)();
970 }
971
972 return 0;
973}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200974#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300975
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200976static SIMPLE_DEV_PM_OPS(tpacpi_pm,
977 tpacpi_suspend_handler, tpacpi_resume_handler);
978
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200979static void tpacpi_shutdown_handler(struct platform_device *pdev)
980{
981 struct ibm_struct *ibm, *itmp;
982
983 list_for_each_entry_safe(ibm, itmp,
984 &tpacpi_all_drivers,
985 all_drivers) {
986 if (ibm->shutdown)
987 (ibm->shutdown)();
988 }
989}
990
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300991static struct platform_driver tpacpi_pdriver = {
992 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200993 .name = TPACPI_DRVR_NAME,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200994 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300995 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200996 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300997};
998
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300999static struct platform_driver tpacpi_hwmon_pdriver = {
1000 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001001 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03001002 },
1003};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001004
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03001005/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001006 * sysfs support helpers
1007 */
1008
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001009struct attribute_set {
1010 unsigned int members, max_members;
1011 struct attribute_group group;
1012};
1013
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001014struct attribute_set_obj {
1015 struct attribute_set s;
1016 struct attribute *a;
1017} __attribute__((packed));
1018
1019static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001020 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001021{
1022 struct attribute_set_obj *sobj;
1023
1024 if (max_members == 0)
1025 return NULL;
1026
1027 /* Allocates space for implicit NULL at the end too */
1028 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1029 max_members * sizeof(struct attribute *),
1030 GFP_KERNEL);
1031 if (!sobj)
1032 return NULL;
1033 sobj->s.max_members = max_members;
1034 sobj->s.group.attrs = &sobj->a;
1035 sobj->s.group.name = name;
1036
1037 return &sobj->s;
1038}
1039
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001040#define destroy_attr_set(_set) \
1041 kfree(_set);
1042
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001043/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001044static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001045{
1046 if (!s || !attr)
1047 return -EINVAL;
1048
1049 if (s->members >= s->max_members)
1050 return -ENOMEM;
1051
1052 s->group.attrs[s->members] = attr;
1053 s->members++;
1054
1055 return 0;
1056}
1057
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001058static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001059 struct attribute **attr,
1060 unsigned int count)
1061{
1062 int i, res;
1063
1064 for (i = 0; i < count; i++) {
1065 res = add_to_attr_set(s, attr[i]);
1066 if (res)
1067 return res;
1068 }
1069
1070 return 0;
1071}
1072
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001073static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001074{
1075 sysfs_remove_group(kobj, &s->group);
1076 destroy_attr_set(s);
1077}
1078
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001079#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1080 sysfs_create_group(_kobj, &_attr_set->group)
1081
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001082static int parse_strtoul(const char *buf,
1083 unsigned long max, unsigned long *value)
1084{
1085 char *endp;
1086
André Goddard Rosae7d28602009-12-14 18:01:06 -08001087 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1088 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001089 if (*endp || *value > max)
1090 return -EINVAL;
1091
1092 return 0;
1093}
1094
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001095static void tpacpi_disable_brightness_delay(void)
1096{
1097 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001098 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001099}
1100
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001101static void printk_deprecated_attribute(const char * const what,
1102 const char * const details)
1103{
1104 tpacpi_log_usertask("deprecated sysfs attribute");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001105 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001106 what, details);
1107}
1108
Johannes Berg19d337d2009-06-02 13:01:37 +02001109/*************************************************************************
1110 * rfkill and radio control support helpers
1111 */
1112
1113/*
1114 * ThinkPad-ACPI firmware handling model:
1115 *
1116 * WLSW (master wireless switch) is event-driven, and is common to all
1117 * firmware-controlled radios. It cannot be controlled, just monitored,
1118 * as expected. It overrides all radio state in firmware
1119 *
1120 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1121 * (TODO: verify how WLSW interacts with the returned radio state).
1122 *
1123 * The only time there are shadow radio state changes, is when
1124 * masked-off hotkeys are used.
1125 */
1126
1127/*
1128 * Internal driver API for radio state:
1129 *
1130 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1131 * bool: true means radio blocked (off)
1132 */
1133enum tpacpi_rfkill_state {
1134 TPACPI_RFK_RADIO_OFF = 0,
1135 TPACPI_RFK_RADIO_ON
1136};
1137
1138/* rfkill switches */
1139enum tpacpi_rfk_id {
1140 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1141 TPACPI_RFK_WWAN_SW_ID,
1142 TPACPI_RFK_UWB_SW_ID,
1143 TPACPI_RFK_SW_MAX
1144};
1145
1146static const char *tpacpi_rfkill_names[] = {
1147 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1148 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1149 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1150 [TPACPI_RFK_SW_MAX] = NULL
1151};
1152
1153/* ThinkPad-ACPI rfkill subdriver */
1154struct tpacpi_rfk {
1155 struct rfkill *rfkill;
1156 enum tpacpi_rfk_id id;
1157 const struct tpacpi_rfk_ops *ops;
1158};
1159
1160struct tpacpi_rfk_ops {
1161 /* firmware interface */
1162 int (*get_status)(void);
1163 int (*set_status)(const enum tpacpi_rfkill_state);
1164};
1165
1166static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1167
1168/* Query FW and update rfkill sw state for a given rfkill switch */
1169static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1170{
1171 int status;
1172
1173 if (!tp_rfk)
1174 return -ENODEV;
1175
1176 status = (tp_rfk->ops->get_status)();
1177 if (status < 0)
1178 return status;
1179
1180 rfkill_set_sw_state(tp_rfk->rfkill,
1181 (status == TPACPI_RFK_RADIO_OFF));
1182
1183 return status;
1184}
1185
1186/* Query FW and update rfkill sw state for all rfkill switches */
1187static void tpacpi_rfk_update_swstate_all(void)
1188{
1189 unsigned int i;
1190
1191 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1192 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1193}
1194
1195/*
1196 * Sync the HW-blocking state of all rfkill switches,
1197 * do notice it causes the rfkill core to schedule uevents
1198 */
1199static void tpacpi_rfk_update_hwblock_state(bool blocked)
1200{
1201 unsigned int i;
1202 struct tpacpi_rfk *tp_rfk;
1203
1204 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1205 tp_rfk = tpacpi_rfkill_switches[i];
1206 if (tp_rfk) {
1207 if (rfkill_set_hw_state(tp_rfk->rfkill,
1208 blocked)) {
1209 /* ignore -- we track sw block */
1210 }
1211 }
1212 }
1213}
1214
1215/* Call to get the WLSW state from the firmware */
1216static int hotkey_get_wlsw(void);
1217
1218/* Call to query WLSW state and update all rfkill switches */
1219static bool tpacpi_rfk_check_hwblock_state(void)
1220{
1221 int res = hotkey_get_wlsw();
1222 int hw_blocked;
1223
1224 /* When unknown or unsupported, we have to assume it is unblocked */
1225 if (res < 0)
1226 return false;
1227
1228 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1229 tpacpi_rfk_update_hwblock_state(hw_blocked);
1230
1231 return hw_blocked;
1232}
1233
1234static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1235{
1236 struct tpacpi_rfk *tp_rfk = data;
1237 int res;
1238
1239 dbg_printk(TPACPI_DBG_RFKILL,
1240 "request to change radio state to %s\n",
1241 blocked ? "blocked" : "unblocked");
1242
1243 /* try to set radio state */
1244 res = (tp_rfk->ops->set_status)(blocked ?
1245 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1246
1247 /* and update the rfkill core with whatever the FW really did */
1248 tpacpi_rfk_update_swstate(tp_rfk);
1249
1250 return (res < 0) ? res : 0;
1251}
1252
1253static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1254 .set_block = tpacpi_rfk_hook_set_block,
1255};
1256
1257static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1258 const struct tpacpi_rfk_ops *tp_rfkops,
1259 const enum rfkill_type rfktype,
1260 const char *name,
1261 const bool set_default)
1262{
1263 struct tpacpi_rfk *atp_rfk;
1264 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001265 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001266 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001267 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001268
1269 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1270
Johannes Berg19d337d2009-06-02 13:01:37 +02001271 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1272 if (atp_rfk)
1273 atp_rfk->rfkill = rfkill_alloc(name,
1274 &tpacpi_pdev->dev,
1275 rfktype,
1276 &tpacpi_rfk_rfkill_ops,
1277 atp_rfk);
1278 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001279 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001280 kfree(atp_rfk);
1281 return -ENOMEM;
1282 }
1283
1284 atp_rfk->id = id;
1285 atp_rfk->ops = tp_rfkops;
1286
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001287 sw_status = (tp_rfkops->get_status)();
1288 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001289 pr_err("failed to read initial state for %s, error %d\n",
1290 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001291 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001292 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001293 if (set_default) {
1294 /* try to keep the initial state, since we ask the
1295 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001296 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001297 }
1298 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001299 hw_state = tpacpi_rfk_check_hwblock_state();
1300 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001301
1302 res = rfkill_register(atp_rfk->rfkill);
1303 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001304 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001305 rfkill_destroy(atp_rfk->rfkill);
1306 kfree(atp_rfk);
1307 return res;
1308 }
1309
1310 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001311
Joe Perches0978e012011-04-04 10:06:25 -07001312 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001313 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001314 return 0;
1315}
1316
1317static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1318{
1319 struct tpacpi_rfk *tp_rfk;
1320
1321 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1322
1323 tp_rfk = tpacpi_rfkill_switches[id];
1324 if (tp_rfk) {
1325 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001326 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001327 tpacpi_rfkill_switches[id] = NULL;
1328 kfree(tp_rfk);
1329 }
1330}
1331
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001332static void printk_deprecated_rfkill_attribute(const char * const what)
1333{
1334 printk_deprecated_attribute(what,
1335 "Please switch to generic rfkill before year 2010");
1336}
1337
Johannes Berg19d337d2009-06-02 13:01:37 +02001338/* sysfs <radio> enable ------------------------------------------------ */
1339static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1340 struct device_attribute *attr,
1341 char *buf)
1342{
1343 int status;
1344
1345 printk_deprecated_rfkill_attribute(attr->attr.name);
1346
1347 /* This is in the ABI... */
1348 if (tpacpi_rfk_check_hwblock_state()) {
1349 status = TPACPI_RFK_RADIO_OFF;
1350 } else {
1351 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1352 if (status < 0)
1353 return status;
1354 }
1355
1356 return snprintf(buf, PAGE_SIZE, "%d\n",
1357 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1358}
1359
1360static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1361 struct device_attribute *attr,
1362 const char *buf, size_t count)
1363{
1364 unsigned long t;
1365 int res;
1366
1367 printk_deprecated_rfkill_attribute(attr->attr.name);
1368
1369 if (parse_strtoul(buf, 1, &t))
1370 return -EINVAL;
1371
1372 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1373
1374 /* This is in the ABI... */
1375 if (tpacpi_rfk_check_hwblock_state() && !!t)
1376 return -EPERM;
1377
1378 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1379 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1380 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1381
1382 return (res < 0) ? res : count;
1383}
1384
1385/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001386static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001387{
Johannes Berg19d337d2009-06-02 13:01:37 +02001388 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001389 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001390 else {
1391 int status;
1392
1393 /* This is in the ABI... */
1394 if (tpacpi_rfk_check_hwblock_state()) {
1395 status = TPACPI_RFK_RADIO_OFF;
1396 } else {
1397 status = tpacpi_rfk_update_swstate(
1398 tpacpi_rfkill_switches[id]);
1399 if (status < 0)
1400 return status;
1401 }
1402
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001403 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001404 (status == TPACPI_RFK_RADIO_ON) ?
1405 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001406 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001407 }
1408
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001409 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001410}
1411
1412static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1413{
1414 char *cmd;
1415 int status = -1;
1416 int res = 0;
1417
1418 if (id >= TPACPI_RFK_SW_MAX)
1419 return -ENODEV;
1420
1421 while ((cmd = next_cmd(&buf))) {
1422 if (strlencmp(cmd, "enable") == 0)
1423 status = TPACPI_RFK_RADIO_ON;
1424 else if (strlencmp(cmd, "disable") == 0)
1425 status = TPACPI_RFK_RADIO_OFF;
1426 else
1427 return -EINVAL;
1428 }
1429
1430 if (status != -1) {
1431 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1432 (status == TPACPI_RFK_RADIO_ON) ?
1433 "enable" : "disable",
1434 tpacpi_rfkill_names[id]);
1435 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1436 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1437 }
1438
1439 return res;
1440}
1441
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001442/*************************************************************************
1443 * thinkpad-acpi driver attributes
1444 */
1445
1446/* interface_version --------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001447static ssize_t interface_version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001448{
1449 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1450}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001451static DRIVER_ATTR_RO(interface_version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001452
1453/* debug_level --------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001454static ssize_t debug_level_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001455{
1456 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1457}
1458
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001459static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1460 size_t count)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001461{
1462 unsigned long t;
1463
1464 if (parse_strtoul(buf, 0xffff, &t))
1465 return -EINVAL;
1466
1467 dbg_level = t;
1468
1469 return count;
1470}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001471static DRIVER_ATTR_RW(debug_level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001472
1473/* version ------------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001474static ssize_t version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001475{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001476 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1477 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001478}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001479static DRIVER_ATTR_RO(version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001480
1481/* --------------------------------------------------------------------- */
1482
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001483#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1484
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001485/* wlsw_emulstate ------------------------------------------------------ */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001486static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001487{
1488 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1489}
1490
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001491static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1492 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001493{
1494 unsigned long t;
1495
1496 if (parse_strtoul(buf, 1, &t))
1497 return -EINVAL;
1498
Johannes Berg19d337d2009-06-02 13:01:37 +02001499 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001500 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001501 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1502 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001503
1504 return count;
1505}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001506static DRIVER_ATTR_RW(wlsw_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001507
1508/* bluetooth_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001509static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001510{
1511 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1512}
1513
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001514static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1515 const char *buf, size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001516{
1517 unsigned long t;
1518
1519 if (parse_strtoul(buf, 1, &t))
1520 return -EINVAL;
1521
1522 tpacpi_bluetooth_emulstate = !!t;
1523
1524 return count;
1525}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001526static DRIVER_ATTR_RW(bluetooth_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001527
1528/* wwan_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001529static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001530{
1531 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1532}
1533
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001534static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1535 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001536{
1537 unsigned long t;
1538
1539 if (parse_strtoul(buf, 1, &t))
1540 return -EINVAL;
1541
1542 tpacpi_wwan_emulstate = !!t;
1543
1544 return count;
1545}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001546static DRIVER_ATTR_RW(wwan_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001547
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001548/* uwb_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001549static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001550{
1551 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1552}
1553
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001554static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1555 size_t count)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001556{
1557 unsigned long t;
1558
1559 if (parse_strtoul(buf, 1, &t))
1560 return -EINVAL;
1561
1562 tpacpi_uwb_emulstate = !!t;
1563
1564 return count;
1565}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001566static DRIVER_ATTR_RW(uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001567#endif
1568
1569/* --------------------------------------------------------------------- */
1570
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001571static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001572 &driver_attr_debug_level, &driver_attr_version,
1573 &driver_attr_interface_version,
1574};
1575
1576static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1577{
1578 int i, res;
1579
1580 i = 0;
1581 res = 0;
1582 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1583 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1584 i++;
1585 }
1586
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001587#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1588 if (!res && dbg_wlswemul)
1589 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1590 if (!res && dbg_bluetoothemul)
1591 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1592 if (!res && dbg_wwanemul)
1593 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001594 if (!res && dbg_uwbemul)
1595 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001596#endif
1597
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001598 return res;
1599}
1600
1601static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1602{
1603 int i;
1604
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001605 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001606 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001607
1608#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1609 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1610 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1611 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001612 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001613#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001614}
1615
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001616/*************************************************************************
1617 * Firmware Data
1618 */
1619
1620/*
1621 * Table of recommended minimum BIOS versions
1622 *
1623 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001624 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001625 * bugs and bad ACPI behaviour on older versions
1626 *
1627 * 2. BIOS or EC fw with known bugs that trigger on Linux
1628 *
1629 * 3. BIOS with known reduced functionality in older versions
1630 *
1631 * We recommend the latest BIOS and EC version.
1632 * We only support the latest BIOS and EC fw version as a rule.
1633 *
1634 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1635 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001636 *
1637 * WARNING: we use this table also to detect that the machine is
1638 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001639 */
1640
1641#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1642 { .vendor = (__v), \
1643 .bios = TPID(__id1, __id2), \
1644 .ec = TPACPI_MATCH_ANY, \
1645 .quirks = TPACPI_MATCH_ANY << 16 \
1646 | (__bv1) << 8 | (__bv2) }
1647
1648#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001649 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001650 { .vendor = (__v), \
1651 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001652 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001653 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1654 | (__bv1) << 8 | (__bv2) }
1655
1656#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1657 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1658
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001659/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001660#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1661 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001662 __bv1, __bv2, TPID(__id1, __id2), \
1663 __ev1, __ev2), \
1664 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1665 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1666 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001667
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001668/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001669#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1670 __eid1, __eid2, __ev1, __ev2) \
1671 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001672 __bv1, __bv2, TPID(__eid1, __eid2), \
1673 __ev1, __ev2), \
1674 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1675 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1676 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001677
1678#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1679 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1680
1681#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1682 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001683 __bv1, __bv2, TPID(__id1, __id2), \
1684 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001685
1686#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1687 __eid1, __eid2, __ev1, __ev2) \
1688 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001689 __bv1, __bv2, TPID(__eid1, __eid2), \
1690 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001691
1692static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1693 /* Numeric models ------------------ */
1694 /* FW MODEL BIOS VERS */
1695 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1696 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1697 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1698 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1699 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1700 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1701 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1702 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1703 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1704
1705 /* A-series ------------------------- */
1706 /* FW MODEL BIOS VERS EC VERS */
1707 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1708 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1709 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1710 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1711 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1712 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1713 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1714 TPV_QI0('1', '3', '2', '0'), /* A22m */
1715 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1716 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1717 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1718
1719 /* G-series ------------------------- */
1720 /* FW MODEL BIOS VERS */
1721 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1722 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1723
1724 /* R-series, T-series --------------- */
1725 /* FW MODEL BIOS VERS EC VERS */
1726 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1727 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1728 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1729 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1730 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1731 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1732 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1733 T40/p, T41/p, T42/p (1) */
1734 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1735 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1736 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1737 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1738
1739 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1740 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1741 TPV_QI0('1', '6', '3', '2'), /* T22 */
1742 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1743 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1744 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1745
1746 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1747 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001748 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001749
1750 /* BIOS FW BIOS VERS EC FW EC VERS */
1751 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1752 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1753
1754 /* X-series ------------------------- */
1755 /* FW MODEL BIOS VERS EC VERS */
1756 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1757 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1758 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1759 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1760 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1761 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1762 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1763
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001764 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1765 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001766
1767 /* (0) - older versions lack DMI EC fw string and functionality */
1768 /* (1) - older versions known to lack functionality */
1769};
1770
1771#undef TPV_QL1
1772#undef TPV_QL0
1773#undef TPV_QI2
1774#undef TPV_QI1
1775#undef TPV_QI0
1776#undef TPV_Q_X
1777#undef TPV_Q
1778
1779static void __init tpacpi_check_outdated_fw(void)
1780{
1781 unsigned long fwvers;
1782 u16 ec_version, bios_version;
1783
1784 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1785 ARRAY_SIZE(tpacpi_bios_version_qtable));
1786
1787 if (!fwvers)
1788 return;
1789
1790 bios_version = fwvers & 0xffffU;
1791 ec_version = (fwvers >> 16) & 0xffffU;
1792
1793 /* note that unknown versions are set to 0x0000 and we use that */
1794 if ((bios_version > thinkpad_id.bios_release) ||
1795 (ec_version > thinkpad_id.ec_release &&
1796 ec_version != TPACPI_MATCH_ANY)) {
1797 /*
1798 * The changelogs would let us track down the exact
1799 * reason, but it is just too much of a pain to track
1800 * it. We only list BIOSes that are either really
1801 * broken, or really stable to begin with, so it is
1802 * best if the user upgrades the firmware anyway.
1803 */
Joe Perches0978e012011-04-04 10:06:25 -07001804 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001805 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 -03001806 }
1807}
1808
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001809static bool __init tpacpi_is_fw_known(void)
1810{
1811 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1812 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1813}
1814
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001815/****************************************************************************
1816 ****************************************************************************
1817 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001818 * Subdrivers
1819 *
1820 ****************************************************************************
1821 ****************************************************************************/
1822
1823/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001824 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001825 */
1826
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001827static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001829 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1830 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001834static struct ibm_struct thinkpad_acpi_driver_data = {
1835 .name = "driver",
1836 .read = thinkpad_acpi_driver_read,
1837};
1838
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001839/*************************************************************************
1840 * Hotkey subdriver
1841 */
1842
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001843/*
1844 * ThinkPad firmware event model
1845 *
1846 * The ThinkPad firmware has two main event interfaces: normal ACPI
1847 * notifications (which follow the ACPI standard), and a private event
1848 * interface.
1849 *
1850 * The private event interface also issues events for the hotkeys. As
1851 * the driver gained features, the event handling code ended up being
1852 * built around the hotkey subdriver. This will need to be refactored
1853 * to a more formal event API eventually.
1854 *
1855 * Some "hotkeys" are actually supposed to be used as event reports,
1856 * such as "brightness has changed", "volume has changed", depending on
1857 * the ThinkPad model and how the firmware is operating.
1858 *
1859 * Unlike other classes, hotkey-class events have mask/unmask control on
1860 * non-ancient firmware. However, how it behaves changes a lot with the
1861 * firmware model and version.
1862 */
1863
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001864enum { /* hot key scan codes (derived from ACPI DSDT) */
1865 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1866 TP_ACPI_HOTKEYSCAN_FNF2,
1867 TP_ACPI_HOTKEYSCAN_FNF3,
1868 TP_ACPI_HOTKEYSCAN_FNF4,
1869 TP_ACPI_HOTKEYSCAN_FNF5,
1870 TP_ACPI_HOTKEYSCAN_FNF6,
1871 TP_ACPI_HOTKEYSCAN_FNF7,
1872 TP_ACPI_HOTKEYSCAN_FNF8,
1873 TP_ACPI_HOTKEYSCAN_FNF9,
1874 TP_ACPI_HOTKEYSCAN_FNF10,
1875 TP_ACPI_HOTKEYSCAN_FNF11,
1876 TP_ACPI_HOTKEYSCAN_FNF12,
1877 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1878 TP_ACPI_HOTKEYSCAN_FNINSERT,
1879 TP_ACPI_HOTKEYSCAN_FNDELETE,
1880 TP_ACPI_HOTKEYSCAN_FNHOME,
1881 TP_ACPI_HOTKEYSCAN_FNEND,
1882 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1883 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1884 TP_ACPI_HOTKEYSCAN_FNSPACE,
1885 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1886 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1887 TP_ACPI_HOTKEYSCAN_MUTE,
1888 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001889 TP_ACPI_HOTKEYSCAN_UNK1,
1890 TP_ACPI_HOTKEYSCAN_UNK2,
1891 TP_ACPI_HOTKEYSCAN_UNK3,
1892 TP_ACPI_HOTKEYSCAN_UNK4,
1893 TP_ACPI_HOTKEYSCAN_UNK5,
1894 TP_ACPI_HOTKEYSCAN_UNK6,
1895 TP_ACPI_HOTKEYSCAN_UNK7,
1896 TP_ACPI_HOTKEYSCAN_UNK8,
1897
Christian Kellner149c8c72017-02-28 17:10:56 +01001898 /* Adaptive keyboard keycodes */
1899 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1900 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
Bastien Nocera6a68d852015-03-02 14:45:31 +01001901 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1902 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1903 TP_ACPI_HOTKEYSCAN_CLOUD,
1904 TP_ACPI_HOTKEYSCAN_UNK9,
1905 TP_ACPI_HOTKEYSCAN_VOICE,
1906 TP_ACPI_HOTKEYSCAN_UNK10,
1907 TP_ACPI_HOTKEYSCAN_GESTURES,
1908 TP_ACPI_HOTKEYSCAN_UNK11,
1909 TP_ACPI_HOTKEYSCAN_UNK12,
1910 TP_ACPI_HOTKEYSCAN_UNK13,
1911 TP_ACPI_HOTKEYSCAN_CONFIG,
1912 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1913 TP_ACPI_HOTKEYSCAN_RELOAD,
1914 TP_ACPI_HOTKEYSCAN_BACK,
1915 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1916 TP_ACPI_HOTKEYSCAN_MIC_UP,
1917 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1918 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1919 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1920
Christian Kellner696c65232017-02-28 17:10:57 +01001921 /* Lenovo extended keymap, starting at 0x1300 */
1922 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1923 /* first new observed key (star, favorites) is 0x1311 */
1924 TP_ACPI_HOTKEYSCAN_STAR = 69,
1925 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1926 TP_ACPI_HOTKEYSCAN_UNK25,
1927 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1928 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1929
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001930 /* Hotkey keymap size */
1931 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001932};
1933
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001934enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001935 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1936 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1937};
1938
1939enum { /* Positions of some of the keys in hotkey masks */
1940 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1941 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1942 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1943 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1944 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
Hans de Goedec685e202017-02-09 16:44:13 +01001945 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001946 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1947 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1948 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1949 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1950 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1951};
1952
1953enum { /* NVRAM to ACPI HKEY group map */
1954 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1955 TP_ACPI_HKEY_ZOOM_MASK |
1956 TP_ACPI_HKEY_DISPSWTCH_MASK |
1957 TP_ACPI_HKEY_HIBERNATE_MASK,
1958 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1959 TP_ACPI_HKEY_BRGHTDWN_MASK,
1960 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1961 TP_ACPI_HKEY_VOLDWN_MASK |
1962 TP_ACPI_HKEY_MUTE_MASK,
1963};
1964
1965#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1966struct tp_nvram_state {
1967 u16 thinkpad_toggle:1;
1968 u16 zoom_toggle:1;
1969 u16 display_toggle:1;
1970 u16 thinklight_toggle:1;
1971 u16 hibernate_toggle:1;
1972 u16 displayexp_toggle:1;
1973 u16 display_state:1;
1974 u16 brightness_toggle:1;
1975 u16 volume_toggle:1;
1976 u16 mute:1;
1977
1978 u8 brightness_level;
1979 u8 volume_level;
1980};
1981
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001982/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001983static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001984
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001985/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001986 * Acquire mutex to write poller control variables as an
1987 * atomic block.
1988 *
1989 * Increment hotkey_config_change when changing them if you
1990 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001991 *
1992 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1993 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001994static struct mutex hotkey_thread_data_mutex;
1995static unsigned int hotkey_config_change;
1996
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001997/*
1998 * hotkey poller control variables
1999 *
2000 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002001 *
2002 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2003 * should be used only when the changes need to be taken as
2004 * a block, OR when one needs to force the kthread to forget
2005 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002006 */
2007static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2008static unsigned int hotkey_poll_freq = 10; /* Hz */
2009
2010#define HOTKEY_CONFIG_CRITICAL_START \
2011 do { \
2012 mutex_lock(&hotkey_thread_data_mutex); \
2013 hotkey_config_change++; \
2014 } while (0);
2015#define HOTKEY_CONFIG_CRITICAL_END \
2016 mutex_unlock(&hotkey_thread_data_mutex);
2017
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002018#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2019
2020#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002021#define HOTKEY_CONFIG_CRITICAL_START
2022#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002023
2024#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2025
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002026static struct mutex hotkey_mutex;
2027
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002028static enum { /* Reasons for waking up */
2029 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2030 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2031 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2032} hotkey_wakeup_reason;
2033
2034static int hotkey_autosleep_ack;
2035
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002036static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2037static u32 hotkey_all_mask; /* all events supported in fw */
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002038static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002039static u32 hotkey_reserved_mask; /* events better left disabled */
2040static u32 hotkey_driver_mask; /* events needed by the driver */
2041static u32 hotkey_user_mask; /* events visible to userspace */
2042static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002043
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002044static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002045
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002046static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002047
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002048static void tpacpi_driver_event(const unsigned int hkey_event);
2049static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002050static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002051
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002052/* HKEY.MHKG() return bits */
2053#define TP_HOTKEY_TABLET_MASK (1 << 3)
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002054enum {
2055 TP_ACPI_MULTI_MODE_INVALID = 0,
2056 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
2057 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
2058 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
2059 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
2060 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
2061 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
2062 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
2063};
2064
2065enum {
2066 /* The following modes are considered tablet mode for the purpose of
2067 * reporting the status to userspace. i.e. in all these modes it makes
2068 * sense to disable the laptop input devices such as touchpad and
2069 * keyboard.
2070 */
2071 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
2072 TP_ACPI_MULTI_MODE_STAND |
2073 TP_ACPI_MULTI_MODE_TENT |
2074 TP_ACPI_MULTI_MODE_STAND_TENT,
2075};
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002076
Johannes Berg19d337d2009-06-02 13:01:37 +02002077static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002078{
Johannes Berg19d337d2009-06-02 13:01:37 +02002079 int status;
2080
2081 if (!tp_features.hotkey_wlsw)
2082 return -ENODEV;
2083
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002084#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002085 if (dbg_wlswemul)
2086 return (tpacpi_wlsw_emulstate) ?
2087 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002088#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002089
2090 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002091 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002092
2093 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002094}
2095
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002096static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
2097{
2098 int type = (s >> 16) & 0xffff;
2099 int value = s & 0xffff;
2100 int mode = TP_ACPI_MULTI_MODE_INVALID;
2101 int valid_modes = 0;
2102
2103 if (has_tablet_mode)
2104 *has_tablet_mode = 0;
2105
2106 switch (type) {
2107 case 1:
2108 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2109 TP_ACPI_MULTI_MODE_TABLET |
2110 TP_ACPI_MULTI_MODE_STAND_TENT;
2111 break;
2112 case 2:
2113 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2114 TP_ACPI_MULTI_MODE_FLAT |
2115 TP_ACPI_MULTI_MODE_TABLET |
2116 TP_ACPI_MULTI_MODE_STAND |
2117 TP_ACPI_MULTI_MODE_TENT;
2118 break;
2119 case 3:
2120 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2121 TP_ACPI_MULTI_MODE_FLAT;
2122 break;
2123 case 4:
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002124 case 5:
Benjamin Berg26befef2017-11-14 17:14:14 +01002125 /* In mode 4, FLAT is not specified as a valid mode. However,
2126 * it can be seen at least on the X1 Yoga 2nd Generation.
2127 */
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002128 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2129 TP_ACPI_MULTI_MODE_FLAT |
2130 TP_ACPI_MULTI_MODE_TABLET |
2131 TP_ACPI_MULTI_MODE_STAND |
2132 TP_ACPI_MULTI_MODE_TENT;
2133 break;
2134 default:
2135 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2136 type, value, TPACPI_MAIL);
2137 return 0;
2138 }
2139
2140 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2141 *has_tablet_mode = 1;
2142
2143 switch (value) {
2144 case 1:
2145 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2146 break;
2147 case 2:
2148 mode = TP_ACPI_MULTI_MODE_FLAT;
2149 break;
2150 case 3:
2151 mode = TP_ACPI_MULTI_MODE_TABLET;
2152 break;
2153 case 4:
2154 if (type == 1)
2155 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2156 else
2157 mode = TP_ACPI_MULTI_MODE_STAND;
2158 break;
2159 case 5:
2160 mode = TP_ACPI_MULTI_MODE_TENT;
2161 break;
2162 default:
2163 if (type == 5 && value == 0xffff) {
2164 pr_warn("Multi mode status is undetected, assuming laptop\n");
2165 return 0;
2166 }
2167 }
2168
2169 if (!(mode & valid_modes)) {
2170 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2171 value, type, TPACPI_MAIL);
2172 return 0;
2173 }
2174
2175 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2176}
2177
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002178static int hotkey_get_tablet_mode(int *status)
2179{
2180 int s;
2181
Lyudeb03f4d42016-11-11 15:15:03 -05002182 switch (tp_features.hotkey_tablet) {
2183 case TP_HOTKEY_TABLET_USES_MHKG:
2184 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2185 return -EIO;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002186
Lyudeb03f4d42016-11-11 15:15:03 -05002187 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2188 break;
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002189 case TP_HOTKEY_TABLET_USES_GMMS:
2190 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
Lyudeb03f4d42016-11-11 15:15:03 -05002191 return -EIO;
2192
Benjamin Bergdda3ec02017-09-15 15:20:49 +02002193 *status = hotkey_gmms_get_tablet_mode(s, NULL);
Lyudeb03f4d42016-11-11 15:15:03 -05002194 break;
2195 default:
2196 break;
2197 }
2198
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002199 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002200}
2201
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002202/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002203 * Reads current event mask from firmware, and updates
2204 * hotkey_acpi_mask accordingly. Also resets any bits
2205 * from hotkey_user_mask that are unavailable to be
2206 * delivered (shadow requirement of the userspace ABI).
2207 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002208 * Call with hotkey_mutex held
2209 */
2210static int hotkey_mask_get(void)
2211{
2212 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002213 u32 m = 0;
2214
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002215 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002216 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002217
2218 hotkey_acpi_mask = m;
2219 } else {
2220 /* no mask support doesn't mean no event support... */
2221 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002222 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002223
2224 /* sync userspace-visible mask */
2225 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002226
2227 return 0;
2228}
2229
Jean Delvaredf6dd1b2015-04-27 09:45:06 +02002230static void hotkey_mask_warn_incomplete_mask(void)
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002231{
2232 /* log only what the user can fix... */
2233 const u32 wantedmask = hotkey_driver_mask &
2234 ~(hotkey_acpi_mask | hotkey_source_mask) &
2235 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2236
2237 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002238 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002239}
2240
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002241/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002242 * Set the firmware mask when supported
2243 *
2244 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2245 *
2246 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2247 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002248 * Call with hotkey_mutex held
2249 */
2250static int hotkey_mask_set(u32 mask)
2251{
2252 int i;
2253 int rc = 0;
2254
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002255 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002256
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002257 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002258 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002259 if (!acpi_evalf(hkey_handle,
2260 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002261 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002262 rc = -EIO;
2263 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002264 }
2265 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002266 }
2267
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002268 /*
2269 * We *must* make an inconditional call to hotkey_mask_get to
2270 * refresh hotkey_acpi_mask and update hotkey_user_mask
2271 *
2272 * Take the opportunity to also log when we cannot _enable_
2273 * a given event.
2274 */
2275 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002276 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002277 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002278 }
2279
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002280 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2281 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002282
2283 return rc;
2284}
2285
2286/*
2287 * Sets hotkey_user_mask and tries to set the firmware mask
2288 *
2289 * Call with hotkey_mutex held
2290 */
2291static int hotkey_user_mask_set(const u32 mask)
2292{
2293 int rc;
2294
2295 /* Give people a chance to notice they are doing something that
2296 * is bound to go boom on their users sooner or later */
2297 if (!tp_warned.hotkey_mask_ff &&
2298 (mask == 0xffff || mask == 0xffffff ||
2299 mask == 0xffffffff)) {
2300 tp_warned.hotkey_mask_ff = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002301 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2302 mask);
2303 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 -03002304 }
2305
2306 /* Try to enable what the user asked for, plus whatever we need.
2307 * this syncs everything but won't enable bits in hotkey_user_mask */
2308 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2309
2310 /* Enable the available bits in hotkey_user_mask */
2311 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2312
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002313 return rc;
2314}
2315
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002316/*
2317 * Sets the driver hotkey mask.
2318 *
2319 * Can be called even if the hotkey subdriver is inactive
2320 */
2321static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2322{
2323 int rc;
2324
2325 /* Do the right thing if hotkey_init has not been called yet */
2326 if (!tp_features.hotkey) {
2327 hotkey_driver_mask = mask;
2328 return 0;
2329 }
2330
2331 mutex_lock(&hotkey_mutex);
2332
2333 HOTKEY_CONFIG_CRITICAL_START
2334 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002335#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002336 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002337#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002338 HOTKEY_CONFIG_CRITICAL_END
2339
2340 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2341 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002342 hotkey_poll_setup(true);
2343
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002344 mutex_unlock(&hotkey_mutex);
2345
2346 return rc;
2347}
2348
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002349static int hotkey_status_get(int *status)
2350{
2351 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2352 return -EIO;
2353
2354 return 0;
2355}
2356
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002357static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002358{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002359 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002360 return -EIO;
2361
2362 return 0;
2363}
2364
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002365static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002366{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002367 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002368
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002369 if (tp_features.hotkey_tablet &&
2370 !hotkey_get_tablet_mode(&state)) {
2371 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002372
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002373 input_report_switch(tpacpi_inputdev,
2374 SW_TABLET_MODE, !!state);
2375 input_sync(tpacpi_inputdev);
2376
2377 mutex_unlock(&tpacpi_inputdev_send_mutex);
2378 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002379}
2380
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002381/* Do NOT call without validating scancode first */
2382static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002383{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002384 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002385
2386 if (keycode != KEY_RESERVED) {
2387 mutex_lock(&tpacpi_inputdev_send_mutex);
2388
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002389 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002390 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002391 input_sync(tpacpi_inputdev);
2392
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002393 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002394 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002395 input_sync(tpacpi_inputdev);
2396
2397 mutex_unlock(&tpacpi_inputdev_send_mutex);
2398 }
2399}
2400
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002401/* Do NOT call without validating scancode first */
2402static void tpacpi_input_send_key_masked(const unsigned int scancode)
2403{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002404 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002405 if (hotkey_user_mask & (1 << scancode))
2406 tpacpi_input_send_key(scancode);
2407}
2408
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002409#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2410static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2411
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002412/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002413static void tpacpi_hotkey_send_key(unsigned int scancode)
2414{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002415 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002416}
2417
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002418static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002419{
2420 u8 d;
2421
2422 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2423 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2424 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2425 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2426 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2427 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2428 }
Hans de Goedec685e202017-02-09 16:44:13 +01002429 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002430 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2431 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2432 }
2433 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2434 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2435 n->displayexp_toggle =
2436 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2437 }
2438 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2439 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2440 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2441 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2442 n->brightness_toggle =
2443 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2444 }
2445 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2446 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2447 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2448 >> TP_NVRAM_POS_LEVEL_VOLUME;
2449 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2450 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2451 }
2452}
2453
Behan Webstera4d44ba2014-02-12 21:58:46 +01002454#define TPACPI_COMPARE_KEY(__scancode, __member) \
2455do { \
2456 if ((event_mask & (1 << __scancode)) && \
2457 oldn->__member != newn->__member) \
2458 tpacpi_hotkey_send_key(__scancode); \
2459} while (0)
2460
2461#define TPACPI_MAY_SEND_KEY(__scancode) \
2462do { \
2463 if (event_mask & (1 << __scancode)) \
2464 tpacpi_hotkey_send_key(__scancode); \
2465} while (0)
2466
2467static void issue_volchange(const unsigned int oldvol,
2468 const unsigned int newvol,
2469 const u32 event_mask)
2470{
2471 unsigned int i = oldvol;
2472
2473 while (i > newvol) {
2474 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2475 i--;
2476 }
2477 while (i < newvol) {
2478 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2479 i++;
2480 }
2481}
2482
2483static void issue_brightnesschange(const unsigned int oldbrt,
2484 const unsigned int newbrt,
2485 const u32 event_mask)
2486{
2487 unsigned int i = oldbrt;
2488
2489 while (i > newbrt) {
2490 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2491 i--;
2492 }
2493 while (i < newbrt) {
2494 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2495 i++;
2496 }
2497}
2498
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002499static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2500 struct tp_nvram_state *newn,
2501 const u32 event_mask)
2502{
2503
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002504 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2505 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2506 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2507 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2508
2509 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2510
2511 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2512
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002513 /*
2514 * Handle volume
2515 *
2516 * This code is supposed to duplicate the IBM firmware behaviour:
2517 * - Pressing MUTE issues mute hotkey message, even when already mute
2518 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002519 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002520 * - The act of unmuting issues volume up/down notification,
2521 * depending which key was used to unmute
2522 *
2523 * We are constrained to what the NVRAM can tell us, which is not much
2524 * and certainly not enough if more than one volume hotkey was pressed
2525 * since the last poll cycle.
2526 *
2527 * Just to make our life interesting, some newer Lenovo ThinkPads have
2528 * bugs in the BIOS and may fail to update volume_toggle properly.
2529 */
2530 if (newn->mute) {
2531 /* muted */
2532 if (!oldn->mute ||
2533 oldn->volume_toggle != newn->volume_toggle ||
2534 oldn->volume_level != newn->volume_level) {
2535 /* recently muted, or repeated mute keypress, or
2536 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002537 issue_volchange(oldn->volume_level, newn->volume_level,
2538 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002539 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2540 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002541 } else {
2542 /* unmute */
2543 if (oldn->mute) {
2544 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002545 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002546 }
2547 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002548 issue_volchange(oldn->volume_level, newn->volume_level,
2549 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002550 } else if (oldn->volume_toggle != newn->volume_toggle) {
2551 /* repeated vol up/down keypress at end of scale ? */
2552 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002553 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002554 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2555 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002556 }
2557 }
2558
2559 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002560 if (oldn->brightness_level != newn->brightness_level) {
2561 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002562 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002563 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2564 /* repeated key presses that didn't change state */
2565 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002566 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002567 else if (newn->brightness_level >= bright_maxlvl
2568 && !tp_features.bright_unkfw)
2569 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002570 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002571
2572#undef TPACPI_COMPARE_KEY
2573#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002574}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002575
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002576/*
2577 * Polling driver
2578 *
2579 * We track all events in hotkey_source_mask all the time, since
2580 * most of them are edge-based. We only issue those requested by
2581 * hotkey_user_mask or hotkey_driver_mask, though.
2582 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002583static int hotkey_kthread(void *data)
2584{
2585 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002586 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002587 unsigned int si, so;
2588 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002589 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002590 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002591 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002592
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002593 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2594 goto exit;
2595
2596 set_freezable();
2597
2598 so = 0;
2599 si = 1;
2600 t = 0;
2601
2602 /* Initial state for compares */
2603 mutex_lock(&hotkey_thread_data_mutex);
2604 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002605 poll_mask = hotkey_source_mask;
2606 event_mask = hotkey_source_mask &
2607 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002608 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002609 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002610 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002611
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002612 while (!kthread_should_stop()) {
2613 if (t == 0) {
2614 if (likely(poll_freq))
2615 t = 1000/poll_freq;
2616 else
2617 t = 100; /* should never happen... */
2618 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002619 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002620 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002621 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002622
2623 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002624 continue;
2625
2626 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002627 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002628 /* forget old state on thaw or config change */
2629 si = so;
2630 t = 0;
2631 change_detector = hotkey_config_change;
2632 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002633 poll_mask = hotkey_source_mask;
2634 event_mask = hotkey_source_mask &
2635 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002636 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002637 mutex_unlock(&hotkey_thread_data_mutex);
2638
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002639 if (likely(poll_mask)) {
2640 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002641 if (likely(si != so)) {
2642 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002643 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002644 }
2645 }
2646
2647 so = si;
2648 si ^= 1;
2649 }
2650
2651exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002652 return 0;
2653}
2654
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002655/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002656static void hotkey_poll_stop_sync(void)
2657{
2658 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002659 kthread_stop(tpacpi_hotkey_task);
2660 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002661 }
2662}
2663
2664/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002665static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002666{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002667 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2668 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002669
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002670 if (hotkey_poll_freq > 0 &&
2671 (poll_driver_mask ||
2672 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002673 if (!tpacpi_hotkey_task) {
2674 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002675 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002676 if (IS_ERR(tpacpi_hotkey_task)) {
2677 tpacpi_hotkey_task = NULL;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002678 pr_err("could not create kernel thread for hotkey polling\n");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002679 }
2680 }
2681 } else {
2682 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002683 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002684 hotkey_poll_freq == 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002685 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 -07002686 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002687 }
2688 }
2689}
2690
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002691static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002692{
2693 mutex_lock(&hotkey_mutex);
2694 hotkey_poll_setup(may_warn);
2695 mutex_unlock(&hotkey_mutex);
2696}
2697
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002698/* call with hotkey_mutex held */
2699static void hotkey_poll_set_freq(unsigned int freq)
2700{
2701 if (!freq)
2702 hotkey_poll_stop_sync();
2703
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002704 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002705}
2706
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002707#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2708
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002709static void hotkey_poll_setup(const bool __unused)
2710{
2711}
2712
2713static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002714{
2715}
2716
2717#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2718
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002719static int hotkey_inputdev_open(struct input_dev *dev)
2720{
2721 switch (tpacpi_lifecycle) {
2722 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002723 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002724 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002725 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002726 case TPACPI_LIFE_EXITING:
2727 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002728 }
2729
2730 /* Should only happen if tpacpi_lifecycle is corrupt */
2731 BUG();
2732 return -EBUSY;
2733}
2734
2735static void hotkey_inputdev_close(struct input_dev *dev)
2736{
2737 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002738 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002739 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002740 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002741}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002742
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002743/* sysfs hotkey enable ------------------------------------------------- */
2744static ssize_t hotkey_enable_show(struct device *dev,
2745 struct device_attribute *attr,
2746 char *buf)
2747{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002748 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002749
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002750 printk_deprecated_attribute("hotkey_enable",
2751 "Hotkey reporting is always enabled");
2752
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002753 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002754 if (res)
2755 return res;
2756
2757 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2758}
2759
2760static ssize_t hotkey_enable_store(struct device *dev,
2761 struct device_attribute *attr,
2762 const char *buf, size_t count)
2763{
2764 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002765
2766 printk_deprecated_attribute("hotkey_enable",
2767 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002768
2769 if (parse_strtoul(buf, 1, &t))
2770 return -EINVAL;
2771
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002772 if (t == 0)
2773 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002774
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002775 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002776}
2777
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002778static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002779
2780/* sysfs hotkey mask --------------------------------------------------- */
2781static ssize_t hotkey_mask_show(struct device *dev,
2782 struct device_attribute *attr,
2783 char *buf)
2784{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002785 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002786}
2787
2788static ssize_t hotkey_mask_store(struct device *dev,
2789 struct device_attribute *attr,
2790 const char *buf, size_t count)
2791{
2792 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002793 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002794
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002795 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002796 return -EINVAL;
2797
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002798 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002799 return -ERESTARTSYS;
2800
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002801 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002802
2803#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002804 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002805#endif
2806
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002807 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002808
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002809 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2810
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002811 return (res) ? res : count;
2812}
2813
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002814static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002815
2816/* sysfs hotkey bios_enabled ------------------------------------------- */
2817static ssize_t hotkey_bios_enabled_show(struct device *dev,
2818 struct device_attribute *attr,
2819 char *buf)
2820{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002821 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002822}
2823
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002824static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002825
2826/* sysfs hotkey bios_mask ---------------------------------------------- */
2827static ssize_t hotkey_bios_mask_show(struct device *dev,
2828 struct device_attribute *attr,
2829 char *buf)
2830{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002831 printk_deprecated_attribute("hotkey_bios_mask",
2832 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002833 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002834}
2835
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002836static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002837
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002838/* sysfs hotkey all_mask ----------------------------------------------- */
2839static ssize_t hotkey_all_mask_show(struct device *dev,
2840 struct device_attribute *attr,
2841 char *buf)
2842{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002843 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2844 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002845}
2846
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002847static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002848
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002849/* sysfs hotkey all_mask ----------------------------------------------- */
2850static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2851 struct device_attribute *attr,
2852 char *buf)
2853{
2854 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2855 hotkey_adaptive_all_mask | hotkey_source_mask);
2856}
2857
2858static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2859
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002860/* sysfs hotkey recommended_mask --------------------------------------- */
2861static ssize_t hotkey_recommended_mask_show(struct device *dev,
2862 struct device_attribute *attr,
2863 char *buf)
2864{
2865 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002866 (hotkey_all_mask | hotkey_source_mask)
2867 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002868}
2869
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002870static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002871
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002872#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2873
2874/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2875static ssize_t hotkey_source_mask_show(struct device *dev,
2876 struct device_attribute *attr,
2877 char *buf)
2878{
2879 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2880}
2881
2882static ssize_t hotkey_source_mask_store(struct device *dev,
2883 struct device_attribute *attr,
2884 const char *buf, size_t count)
2885{
2886 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002887 u32 r_ev;
2888 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002889
2890 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2891 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2892 return -EINVAL;
2893
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002894 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002895 return -ERESTARTSYS;
2896
2897 HOTKEY_CONFIG_CRITICAL_START
2898 hotkey_source_mask = t;
2899 HOTKEY_CONFIG_CRITICAL_END
2900
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002901 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2902 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002903 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002904
2905 /* check if events needed by the driver got disabled */
2906 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2907 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002908
2909 mutex_unlock(&hotkey_mutex);
2910
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002911 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002912 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002913
2914 if (r_ev)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002915 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002916 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002917
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002918 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2919
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002920 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002921}
2922
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002923static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002924
2925/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2926static ssize_t hotkey_poll_freq_show(struct device *dev,
2927 struct device_attribute *attr,
2928 char *buf)
2929{
2930 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2931}
2932
2933static ssize_t hotkey_poll_freq_store(struct device *dev,
2934 struct device_attribute *attr,
2935 const char *buf, size_t count)
2936{
2937 unsigned long t;
2938
2939 if (parse_strtoul(buf, 25, &t))
2940 return -EINVAL;
2941
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002942 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002943 return -ERESTARTSYS;
2944
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002945 hotkey_poll_set_freq(t);
2946 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002947
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002948 mutex_unlock(&hotkey_mutex);
2949
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002950 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2951
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002952 return count;
2953}
2954
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002955static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002956
2957#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2958
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002959/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002960static ssize_t hotkey_radio_sw_show(struct device *dev,
2961 struct device_attribute *attr,
2962 char *buf)
2963{
Johannes Berg19d337d2009-06-02 13:01:37 +02002964 int res;
2965 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002966 if (res < 0)
2967 return res;
2968
Johannes Berg19d337d2009-06-02 13:01:37 +02002969 /* Opportunistic update */
2970 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2971
2972 return snprintf(buf, PAGE_SIZE, "%d\n",
2973 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002974}
2975
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002976static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002977
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002978static void hotkey_radio_sw_notify_change(void)
2979{
2980 if (tp_features.hotkey_wlsw)
2981 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2982 "hotkey_radio_sw");
2983}
2984
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002985/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2986static ssize_t hotkey_tablet_mode_show(struct device *dev,
2987 struct device_attribute *attr,
2988 char *buf)
2989{
2990 int res, s;
2991 res = hotkey_get_tablet_mode(&s);
2992 if (res < 0)
2993 return res;
2994
2995 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2996}
2997
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002998static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002999
3000static void hotkey_tablet_mode_notify_change(void)
3001{
3002 if (tp_features.hotkey_tablet)
3003 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3004 "hotkey_tablet_mode");
3005}
3006
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003007/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003008static ssize_t hotkey_wakeup_reason_show(struct device *dev,
3009 struct device_attribute *attr,
3010 char *buf)
3011{
3012 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
3013}
3014
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003015static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003016
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02003017static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003018{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003019 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3020 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003021}
3022
3023/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003024static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
3025 struct device_attribute *attr,
3026 char *buf)
3027{
3028 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
3029}
3030
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003031static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
3032 hotkey_wakeup_hotunplug_complete_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003033
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02003034static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003035{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003036 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3037 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003038}
3039
Bastien Nocerab790cee2015-03-02 14:45:27 +01003040/* sysfs adaptive kbd mode --------------------------------------------- */
3041
3042static int adaptive_keyboard_get_mode(void);
3043static int adaptive_keyboard_set_mode(int new_mode);
3044
3045enum ADAPTIVE_KEY_MODE {
3046 HOME_MODE,
3047 WEB_BROWSER_MODE,
3048 WEB_CONFERENCE_MODE,
3049 FUNCTION_MODE,
3050 LAYFLAT_MODE
3051};
3052
3053static ssize_t adaptive_kbd_mode_show(struct device *dev,
3054 struct device_attribute *attr,
3055 char *buf)
3056{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003057 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01003058
3059 current_mode = adaptive_keyboard_get_mode();
3060 if (current_mode < 0)
3061 return current_mode;
3062
3063 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
3064}
3065
3066static ssize_t adaptive_kbd_mode_store(struct device *dev,
3067 struct device_attribute *attr,
3068 const char *buf, size_t count)
3069{
3070 unsigned long t;
3071 int res;
3072
3073 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
3074 return -EINVAL;
3075
3076 res = adaptive_keyboard_set_mode(t);
3077 return (res < 0) ? res : count;
3078}
3079
3080static DEVICE_ATTR_RW(adaptive_kbd_mode);
3081
3082static struct attribute *adaptive_kbd_attributes[] = {
3083 &dev_attr_adaptive_kbd_mode.attr,
3084 NULL
3085};
3086
3087static const struct attribute_group adaptive_kbd_attr_group = {
3088 .attrs = adaptive_kbd_attributes,
3089};
3090
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003091/* --------------------------------------------------------------------- */
3092
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003093static struct attribute *hotkey_attributes[] __initdata = {
3094 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003095 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003096 &dev_attr_hotkey_bios_mask.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003097 &dev_attr_wakeup_reason.attr,
3098 &dev_attr_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003099 &dev_attr_hotkey_mask.attr,
3100 &dev_attr_hotkey_all_mask.attr,
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003101 &dev_attr_hotkey_adaptive_all_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003102 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003103#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003104 &dev_attr_hotkey_source_mask.attr,
3105 &dev_attr_hotkey_poll_freq.attr,
3106#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003107};
3108
Johannes Berg19d337d2009-06-02 13:01:37 +02003109/*
3110 * Sync both the hw and sw blocking state of all switches
3111 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003112static void tpacpi_send_radiosw_update(void)
3113{
3114 int wlsw;
3115
Johannes Berg19d337d2009-06-02 13:01:37 +02003116 /*
3117 * We must sync all rfkill controllers *before* issuing any
3118 * rfkill input events, or we will race the rfkill core input
3119 * handler.
3120 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003121 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003122 * for the above.
3123 *
3124 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3125 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003126
Johannes Berg19d337d2009-06-02 13:01:37 +02003127 wlsw = hotkey_get_wlsw();
3128
3129 /* Sync hw blocking state first if it is hw-blocked */
3130 if (wlsw == TPACPI_RFK_RADIO_OFF)
3131 tpacpi_rfk_update_hwblock_state(true);
3132
3133 /* Sync sw blocking state */
3134 tpacpi_rfk_update_swstate_all();
3135
3136 /* Sync hw blocking state last if it is hw-unblocked */
3137 if (wlsw == TPACPI_RFK_RADIO_ON)
3138 tpacpi_rfk_update_hwblock_state(false);
3139
3140 /* Issue rfkill input event for WLSW switch */
3141 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003142 mutex_lock(&tpacpi_inputdev_send_mutex);
3143
3144 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003145 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003146 input_sync(tpacpi_inputdev);
3147
3148 mutex_unlock(&tpacpi_inputdev_send_mutex);
3149 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003150
3151 /*
3152 * this can be unconditional, as we will poll state again
3153 * if userspace uses the notify to read data
3154 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003155 hotkey_radio_sw_notify_change();
3156}
3157
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003158static void hotkey_exit(void)
3159{
3160#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003161 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003162 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003163 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003164#endif
3165
3166 if (hotkey_dev_attributes)
3167 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3168
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003169 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003170 "restoring original HKEY status and mask\n");
3171 /* yes, there is a bitwise or below, we want the
3172 * functions to be called even if one of them fail */
3173 if (((tp_features.hotkey_mask &&
3174 hotkey_mask_set(hotkey_orig_mask)) |
3175 hotkey_status_set(false)) != 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003176 pr_err("failed to restore hot key mask to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003177}
3178
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003179static void __init hotkey_unmap(const unsigned int scancode)
3180{
3181 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3182 clear_bit(hotkey_keycode_map[scancode],
3183 tpacpi_inputdev->keybit);
3184 hotkey_keycode_map[scancode] = KEY_RESERVED;
3185 }
3186}
3187
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003188/*
3189 * HKEY quirks:
3190 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3191 */
3192
3193#define TPACPI_HK_Q_INIMASK 0x0001
3194
3195static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3196 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3197 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3198 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3199 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3200 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3201 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3202 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3203 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3204 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3205 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3206 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3207 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3208 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3209 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3210 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3211 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3212 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3213 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3214 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3215};
3216
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003217typedef u16 tpacpi_keymap_entry_t;
3218typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003219
Lyudeb3180022016-11-11 15:15:02 -05003220static int hotkey_init_tablet_mode(void)
3221{
Darren Hartcb2bf252016-12-14 14:45:48 -08003222 int in_tablet_mode = 0, res;
3223 char *type = NULL;
Lyudeb3180022016-11-11 15:15:02 -05003224
Benjamin Bergdda3ec02017-09-15 15:20:49 +02003225 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3226 int has_tablet_mode;
3227
3228 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3229 &has_tablet_mode);
3230 if (has_tablet_mode)
3231 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3232 type = "GMMS";
3233 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
Lyudeb03f4d42016-11-11 15:15:03 -05003234 /* For X41t, X60t, X61t Tablets... */
Lyudeb3180022016-11-11 15:15:02 -05003235 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3236 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3237 type = "MHKG";
3238 }
3239
3240 if (!tp_features.hotkey_tablet)
3241 return 0;
3242
3243 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3244 type, in_tablet_mode ? "tablet" : "laptop");
3245
3246 res = add_to_attr_set(hotkey_dev_attributes,
3247 &dev_attr_hotkey_tablet_mode.attr);
3248 if (res)
3249 return -1;
3250
3251 return in_tablet_mode;
3252}
3253
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003254static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003255{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003256 /* Requirements for changing the default keymaps:
3257 *
3258 * 1. Many of the keys are mapped to KEY_RESERVED for very
3259 * good reasons. Do not change them unless you have deep
3260 * knowledge on the IBM and Lenovo ThinkPad firmware for
3261 * the various ThinkPad models. The driver behaves
3262 * differently for KEY_RESERVED: such keys have their
3263 * hot key mask *unset* in mask_recommended, and also
3264 * in the initial hot key mask programmed into the
3265 * firmware at driver load time, which means the firm-
3266 * ware may react very differently if you change them to
3267 * something else;
3268 *
3269 * 2. You must be subscribed to the linux-thinkpad and
3270 * ibm-acpi-devel mailing lists, and you should read the
3271 * list archives since 2007 if you want to change the
3272 * keymaps. This requirement exists so that you will
3273 * know the past history of problems with the thinkpad-
3274 * acpi driver keymaps, and also that you will be
3275 * listening to any bug reports;
3276 *
3277 * 3. Do not send thinkpad-acpi specific patches directly to
3278 * for merging, *ever*. Send them to the linux-acpi
3279 * mailinglist for comments. Merging is to be done only
3280 * through acpi-test and the ACPI maintainer.
3281 *
3282 * If the above is too much to ask, don't change the keymap.
3283 * Ask the thinkpad-acpi maintainer to do it, instead.
3284 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003285
3286 enum keymap_index {
3287 TPACPI_KEYMAP_IBM_GENERIC = 0,
3288 TPACPI_KEYMAP_LENOVO_GENERIC,
3289 };
3290
3291 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3292 /* Generic keymap for IBM ThinkPads */
3293 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003294 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003295 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003296 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3297 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003298
3299 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003300 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3301 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3302 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003303
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003304 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003305 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003306 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003307
3308 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003309 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003310
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003311 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3312 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003313
3314 /* Volume: firmware always react to it and reprograms
3315 * the built-in *extra* mixer. Never map it to control
3316 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003317 KEY_RESERVED, /* 0x14: VOLUME UP */
3318 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3319 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003320
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003321 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003322
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003323 /* (assignments unknown, please report if found) */
3324 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3325 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003326
3327 /* No assignments, only used for Adaptive keyboards. */
3328 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3329 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3330 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3331 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3332 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Christian Kellner696c65232017-02-28 17:10:57 +01003333
3334 /* No assignment, used for newer Lenovo models */
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, KEY_UNKNOWN,
3339 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3340 KEY_UNKNOWN, KEY_UNKNOWN
3341
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003342 },
3343
3344 /* Generic keymap for Lenovo ThinkPads */
3345 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003346 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3347 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003348 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003349 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003350
3351 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003352 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3353 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3354 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003355
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003356 /* These should be enabled --only-- when ACPI video
3357 * is disabled (i.e. in "vendor" mode), and are handled
3358 * in a special way by the init code */
3359 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3360 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003361
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003362 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003363
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003364 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3365 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003366
3367 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3368 * react to it and reprograms the built-in *extra* mixer.
3369 * Never map it to control another mixer by default.
3370 *
3371 * T60?, T61, R60?, R61: firmware and EC tries to send
3372 * these over the regular keyboard, so these are no-ops,
3373 * but there are still weird bugs re. MUTE, so do not
3374 * change unless you get test reports from all Lenovo
3375 * models. May cause the BIOS to interfere with the
3376 * HDA mixer.
3377 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003378 KEY_RESERVED, /* 0x14: VOLUME UP */
3379 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3380 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003381
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003382 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003383
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003384 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003385 KEY_UNKNOWN, KEY_UNKNOWN,
3386
3387 /*
3388 * The mic mute button only sends 0x1a. It does not
3389 * automatically mute the mic or change the mute light.
3390 */
3391 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3392
3393 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003394 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003395
3396 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003397 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003398
3399 /*
3400 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3401 * The first item in this list is the Mute button which is
3402 * emitted with 0x103 through
3403 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3404 * symbol is held.
3405 * We'll need to offset those by 0x20.
3406 */
3407 KEY_RESERVED, /* Mute held, 0x103 */
3408 KEY_BRIGHTNESS_MIN, /* Backlight off */
3409 KEY_RESERVED, /* Clipping tool */
3410 KEY_RESERVED, /* Cloud */
3411 KEY_RESERVED,
3412 KEY_VOICECOMMAND, /* Voice */
3413 KEY_RESERVED,
3414 KEY_RESERVED, /* Gestures */
3415 KEY_RESERVED,
3416 KEY_RESERVED,
3417 KEY_RESERVED,
3418 KEY_CONFIG, /* Settings */
3419 KEY_RESERVED, /* New tab */
3420 KEY_REFRESH, /* Reload */
3421 KEY_BACK, /* Back */
3422 KEY_RESERVED, /* Microphone down */
3423 KEY_RESERVED, /* Microphone up */
3424 KEY_RESERVED, /* Microphone cancellation */
3425 KEY_RESERVED, /* Camera mode */
3426 KEY_RESERVED, /* Rotate display, 0x116 */
Christian Kellner696c65232017-02-28 17:10:57 +01003427
3428 /*
3429 * These are found in 2017 models (e.g. T470s, X270).
3430 * The lowest known value is 0x311, which according to
3431 * the manual should launch a user defined favorite
3432 * application.
3433 *
3434 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3435 * corresponding to 0x34.
3436 */
3437
3438 /* (assignments unknown, please report if found) */
3439 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3440 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3441 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3442 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3443 KEY_UNKNOWN,
3444
3445 KEY_FAVORITES, /* Favorite app, 0x311 */
3446 KEY_RESERVED, /* Clipping tool */
3447 KEY_RESERVED,
3448 KEY_BLUETOOTH, /* Bluetooth */
3449 KEY_KEYBOARD /* Keyboard, 0x315 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003450 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003451 };
3452
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003453 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3454 /* Generic maps (fallback) */
3455 {
3456 .vendor = PCI_VENDOR_ID_IBM,
3457 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3458 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3459 },
3460 {
3461 .vendor = PCI_VENDOR_ID_LENOVO,
3462 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3463 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3464 },
3465 };
3466
3467#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003468#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003469
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003470 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003471 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003472 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003473 bool radiosw_state = false;
3474 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003475
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003476 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003477 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003478
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003479 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3480 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003481
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003482 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003483 BUG_ON(tpacpi_inputdev->open != NULL ||
3484 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003485
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003486 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003487 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003488
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003489#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003490 mutex_init(&hotkey_thread_data_mutex);
3491#endif
3492
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003493 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003494 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003495
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003496 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3497 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003498 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003499
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003500 if (!tp_features.hotkey)
3501 return 1;
3502
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003503 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3504 ARRAY_SIZE(tpacpi_hotkey_qtable));
3505
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003506 tpacpi_disable_brightness_delay();
3507
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003508 /* MUST have enough space for all attributes to be added to
3509 * hotkey_dev_attributes */
3510 hotkey_dev_attributes = create_attr_set(
3511 ARRAY_SIZE(hotkey_attributes) + 2,
3512 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003513 if (!hotkey_dev_attributes)
3514 return -ENOMEM;
3515 res = add_many_to_attr_set(hotkey_dev_attributes,
3516 hotkey_attributes,
3517 ARRAY_SIZE(hotkey_attributes));
3518 if (res)
3519 goto err_exit;
3520
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003521 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003522 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3523 for HKEY interface version 0x100 */
3524 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003525 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3526 "firmware HKEY interface version: 0x%x\n",
3527 hkeyv);
3528
3529 switch (hkeyv >> 8) {
3530 case 1:
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003531 /*
3532 * MHKV 0x100 in A31, R40, R40e,
3533 * T4x, X31, and later
3534 */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003535
3536 /* Paranoia check AND init hotkey_all_mask */
3537 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3538 "MHKA", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003539 pr_err("missing MHKA handler, please report this to %s\n",
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003540 TPACPI_MAIL);
3541 /* Fallback: pre-init for FN+F3,F4,F12 */
3542 hotkey_all_mask = 0x080cU;
3543 } else {
3544 tp_features.hotkey_mask = 1;
3545 }
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003546 break;
3547
3548 case 2:
3549 /*
3550 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3551 */
3552
3553 /* Paranoia check AND init hotkey_all_mask */
3554 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3555 "MHKA", "dd", 1)) {
3556 pr_err("missing MHKA handler, please report this to %s\n",
3557 TPACPI_MAIL);
3558 /* Fallback: pre-init for FN+F3,F4,F12 */
3559 hotkey_all_mask = 0x080cU;
3560 } else {
3561 tp_features.hotkey_mask = 1;
3562 }
3563
3564 /*
3565 * Check if we have an adaptive keyboard, like on the
3566 * Lenovo Carbon X1 2014 (2nd Gen).
3567 */
3568 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3569 "MHKA", "dd", 2)) {
3570 if (hotkey_adaptive_all_mask != 0) {
3571 tp_features.has_adaptive_kbd = true;
3572 res = sysfs_create_group(
3573 &tpacpi_pdev->dev.kobj,
3574 &adaptive_kbd_attr_group);
3575 if (res)
3576 goto err_exit;
3577 }
3578 } else {
3579 tp_features.has_adaptive_kbd = false;
3580 hotkey_adaptive_all_mask = 0x0U;
3581 }
3582 break;
3583
3584 default:
3585 pr_err("unknown version of the HKEY interface: 0x%x\n",
3586 hkeyv);
3587 pr_err("please report this to %s\n", TPACPI_MAIL);
3588 break;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003589 }
3590 }
3591
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003592 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3593 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003594 str_supported(tp_features.hotkey_mask));
3595
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003596 /* Init hotkey_all_mask if not initialized yet */
3597 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3598 (quirks & TPACPI_HK_Q_INIMASK))
3599 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003600
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003601 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003602 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003603 /* hotkey_source_mask *must* be zero for
3604 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003605 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003606 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003607 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003608
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003609 hotkey_orig_mask = hotkey_acpi_mask;
3610 } else {
3611 hotkey_orig_mask = hotkey_all_mask;
3612 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003613 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003614
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003615#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3616 if (dbg_wlswemul) {
3617 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003618 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003619 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003620 } else
3621#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003622 /* Not all thinkpads have a hardware radio switch */
3623 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3624 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003625 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003626 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003627 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003628 }
3629 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003630 res = add_to_attr_set(hotkey_dev_attributes,
3631 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003632
Lyudeb3180022016-11-11 15:15:02 -05003633 res = hotkey_init_tablet_mode();
3634 if (res < 0)
3635 goto err_exit;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003636
Lyudeb3180022016-11-11 15:15:02 -05003637 tabletsw_state = res;
3638
3639 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3640 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003641 if (res)
3642 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003643
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003644 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003645 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3646 GFP_KERNEL);
3647 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003648 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003649 res = -ENOMEM;
3650 goto err_exit;
3651 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003652
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003653 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3654 ARRAY_SIZE(tpacpi_keymap_qtable));
3655 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3656 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3657 "using keymap number %lu\n", keymap_id);
3658
3659 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3660 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003661
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003662 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003663 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3664 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3665 tpacpi_inputdev->keycode = hotkey_keycode_map;
3666 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3667 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003668 input_set_capability(tpacpi_inputdev, EV_KEY,
3669 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003670 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003671 if (i < sizeof(hotkey_reserved_mask)*8)
3672 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003673 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003674 }
3675
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003676 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003677 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003678 input_report_switch(tpacpi_inputdev,
3679 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003680 }
3681 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003682 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003683 input_report_switch(tpacpi_inputdev,
3684 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003685 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003686
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003687 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003688 * userspace. tpacpi_detect_brightness_capabilities() must have
3689 * been called before this point */
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02003690 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003691 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3692 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003693
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003694 /* Disable brightness up/down on Lenovo thinkpads when
3695 * ACPI is handling them, otherwise it is plain impossible
3696 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003697 hotkey_reserved_mask |=
3698 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3699 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003700 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3701 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003702 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003703
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003704#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003705 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3706 & ~hotkey_all_mask
3707 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003708
3709 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3710 "hotkey source mask 0x%08x, polling freq %u\n",
3711 hotkey_source_mask, hotkey_poll_freq);
3712#endif
3713
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003714 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3715 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003716 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003717 if (res) {
3718 hotkey_exit();
3719 return res;
3720 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003721 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3722 | hotkey_driver_mask)
3723 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003724 if (res < 0 && res != -ENXIO) {
3725 hotkey_exit();
3726 return res;
3727 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003728 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3729 & ~hotkey_reserved_mask;
3730 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3731 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3732 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003733
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003734 tpacpi_inputdev->open = &hotkey_inputdev_open;
3735 tpacpi_inputdev->close = &hotkey_inputdev_close;
3736
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003737 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003738
3739 return 0;
3740
3741err_exit:
3742 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
Bastien Nocerab790cee2015-03-02 14:45:27 +01003743 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3744 &adaptive_kbd_attr_group);
3745
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003746 hotkey_dev_attributes = NULL;
3747
Jan van den Berg72a979f2014-09-17 00:01:08 +02003748 return (res < 0) ? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003749}
3750
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003751/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3752 * mode, Web conference mode, Function mode and Lay-flat mode.
3753 * We support Home mode and Function mode currently.
3754 *
3755 * Will consider support rest of modes in future.
3756 *
3757 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003758static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003759 HOME_MODE,
3760/* WEB_BROWSER_MODE = 2,
3761 WEB_CONFERENCE_MODE = 3, */
3762 FUNCTION_MODE
3763};
3764
3765#define DFR_CHANGE_ROW 0x101
3766#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003767#define FIRST_ADAPTIVE_KEY 0x103
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003768
3769/* press Fn key a while second, it will switch to Function Mode. Then
3770 * release Fn key, previous mode be restored.
3771 */
3772static bool adaptive_keyboard_mode_is_saved;
3773static int adaptive_keyboard_prev_mode;
3774
Bastien Noceraf74587f2015-03-02 14:45:22 +01003775static int adaptive_keyboard_get_mode(void)
3776{
3777 int mode = 0;
3778
3779 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3780 pr_err("Cannot read adaptive keyboard mode\n");
3781 return -EIO;
3782 }
3783
3784 return mode;
3785}
3786
3787static int adaptive_keyboard_set_mode(int new_mode)
3788{
3789 if (new_mode < 0 ||
3790 new_mode > LAYFLAT_MODE)
3791 return -EINVAL;
3792
3793 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3794 pr_err("Cannot set adaptive keyboard mode\n");
3795 return -EIO;
3796 }
3797
3798 return 0;
3799}
3800
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003801static int adaptive_keyboard_get_next_mode(int mode)
3802{
3803 size_t i;
3804 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3805
3806 for (i = 0; i <= max_mode; i++) {
3807 if (adaptive_keyboard_modes[i] == mode)
3808 break;
3809 }
3810
3811 if (i >= max_mode)
3812 i = 0;
3813 else
3814 i++;
3815
3816 return adaptive_keyboard_modes[i];
3817}
3818
3819static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3820{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003821 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003822 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003823 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003824
3825 switch (scancode) {
3826 case DFR_CHANGE_ROW:
3827 if (adaptive_keyboard_mode_is_saved) {
3828 new_mode = adaptive_keyboard_prev_mode;
3829 adaptive_keyboard_mode_is_saved = false;
3830 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003831 current_mode = adaptive_keyboard_get_mode();
3832 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003833 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003834 new_mode = adaptive_keyboard_get_next_mode(
3835 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003836 }
3837
Bastien Noceraf74587f2015-03-02 14:45:22 +01003838 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003839 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003840
3841 return true;
3842
3843 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003844 current_mode = adaptive_keyboard_get_mode();
3845 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003846 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003847
Bastien Noceraf74587f2015-03-02 14:45:22 +01003848 adaptive_keyboard_prev_mode = current_mode;
3849 adaptive_keyboard_mode_is_saved = true;
3850
3851 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3852 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003853 return true;
3854
3855 default:
Dan Carpenter741d98c2015-03-11 12:36:07 +03003856 if (scancode < FIRST_ADAPTIVE_KEY ||
Christian Kellner696c65232017-02-28 17:10:57 +01003857 scancode >= FIRST_ADAPTIVE_KEY +
3858 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3859 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
Bastien Nocera6a68d852015-03-02 14:45:31 +01003860 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003861 scancode);
Bastien Nocera6a68d852015-03-02 14:45:31 +01003862 return false;
3863 }
Christian Kellner149c8c72017-02-28 17:10:56 +01003864 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3865 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
Bastien Nocera6a68d852015-03-02 14:45:31 +01003866 if (keycode != KEY_RESERVED) {
3867 mutex_lock(&tpacpi_inputdev_send_mutex);
3868
3869 input_report_key(tpacpi_inputdev, keycode, 1);
3870 input_sync(tpacpi_inputdev);
3871
3872 input_report_key(tpacpi_inputdev, keycode, 0);
3873 input_sync(tpacpi_inputdev);
3874
3875 mutex_unlock(&tpacpi_inputdev_send_mutex);
3876 }
3877 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003878 }
3879}
3880
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003881static bool hotkey_notify_hotkey(const u32 hkey,
3882 bool *send_acpi_ev,
3883 bool *ignore_acpi_ev)
3884{
3885 /* 0x1000-0x1FFF: key presses */
3886 unsigned int scancode = hkey & 0xfff;
3887 *send_acpi_ev = true;
3888 *ignore_acpi_ev = false;
3889
Christian Kellner696c65232017-02-28 17:10:57 +01003890 /*
3891 * Original events are in the 0x10XX range, the adaptive keyboard
3892 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3893 * models, additional keys are emitted through 0x13XX.
3894 */
3895 switch ((hkey >> 8) & 0xf) {
3896 case 0:
3897 if (scancode > 0 &&
3898 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3899 /* HKEY event 0x1001 is scancode 0x00 */
3900 scancode--;
3901 if (!(hotkey_source_mask & (1 << scancode))) {
3902 tpacpi_input_send_key_masked(scancode);
3903 *send_acpi_ev = false;
3904 } else {
3905 *ignore_acpi_ev = true;
3906 }
3907 return true;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003908 }
Christian Kellner696c65232017-02-28 17:10:57 +01003909 break;
3910
3911 case 1:
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003912 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Christian Kellner696c65232017-02-28 17:10:57 +01003913
3914 case 3:
3915 /* Extended keycodes start at 0x300 and our offset into the map
3916 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3917 * will be positive, but might not be in the correct range.
3918 */
3919 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3920 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3921 scancode < TPACPI_HOTKEY_MAP_LEN) {
3922 tpacpi_input_send_key(scancode);
3923 return true;
3924 }
3925 break;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003926 }
Christian Kellner696c65232017-02-28 17:10:57 +01003927
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003928 return false;
3929}
3930
3931static bool hotkey_notify_wakeup(const u32 hkey,
3932 bool *send_acpi_ev,
3933 bool *ignore_acpi_ev)
3934{
3935 /* 0x2000-0x2FFF: Wakeup reason */
3936 *send_acpi_ev = true;
3937 *ignore_acpi_ev = false;
3938
3939 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003940 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3941 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003942 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3943 *ignore_acpi_ev = true;
3944 break;
3945
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003946 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3947 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003948 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3949 *ignore_acpi_ev = true;
3950 break;
3951
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003952 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3953 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003954 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003955 /* how to auto-heal: */
3956 /* 2313: woke up from S3, go to S4/S5 */
3957 /* 2413: woke up from S4, go to S5 */
3958 break;
3959
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003960 default:
3961 return false;
3962 }
3963
3964 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003965 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003966 hotkey_wakeup_reason_notify_change();
3967 }
3968 return true;
3969}
3970
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003971static bool hotkey_notify_dockevent(const u32 hkey,
3972 bool *send_acpi_ev,
3973 bool *ignore_acpi_ev)
3974{
3975 /* 0x4000-0x4FFF: dock-related events */
3976 *send_acpi_ev = true;
3977 *ignore_acpi_ev = false;
3978
3979 switch (hkey) {
3980 case TP_HKEY_EV_UNDOCK_ACK:
3981 /* ACPI undock operation completed after wakeup */
3982 hotkey_autosleep_ack = 1;
3983 pr_info("undocked\n");
3984 hotkey_wakeup_hotunplug_complete_notify_change();
3985 return true;
3986
3987 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3988 pr_info("docked into hotplug port replicator\n");
3989 return true;
3990 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3991 pr_info("undocked from hotplug port replicator\n");
3992 return true;
3993
3994 default:
3995 return false;
3996 }
3997}
3998
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003999static bool hotkey_notify_usrevent(const u32 hkey,
4000 bool *send_acpi_ev,
4001 bool *ignore_acpi_ev)
4002{
4003 /* 0x5000-0x5FFF: human interface helpers */
4004 *send_acpi_ev = true;
4005 *ignore_acpi_ev = false;
4006
4007 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004008 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
4009 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004010 return true;
4011
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004012 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
4013 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004014 tpacpi_input_send_tabletsw();
4015 hotkey_tablet_mode_notify_change();
4016 *send_acpi_ev = false;
4017 return true;
4018
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004019 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
4020 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
4021 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03004022 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004023 *ignore_acpi_ev = true;
4024 return true;
4025
4026 default:
4027 return false;
4028 }
4029}
4030
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004031static void thermal_dump_all_sensors(void);
4032
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004033static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004034 bool *send_acpi_ev,
4035 bool *ignore_acpi_ev)
4036{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004037 bool known = true;
4038
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004039 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004040 *send_acpi_ev = true;
4041 *ignore_acpi_ev = false;
4042
4043 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004044 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07004045 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02004046 /* recommended action: do nothing, we don't have
4047 * Lenovo ATM information */
4048 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004049 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07004050 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004051 /* recommended action: warn user through gui */
4052 break;
4053 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07004054 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004055 /* recommended action: immediate sleep/hibernate */
4056 break;
4057 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004058 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004059 /* recommended action: warn user through gui, that */
4060 /* some internal component is too hot */
4061 break;
4062 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004063 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004064 /* recommended action: immediate sleep/hibernate */
4065 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01004066 case TP_HKEY_EV_AC_CHANGED:
4067 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4068 * AC status changed; can be triggered by plugging or
4069 * unplugging AC adapter, docking or undocking. */
4070
4071 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004072
4073 case TP_HKEY_EV_KEY_NUMLOCK:
4074 case TP_HKEY_EV_KEY_FN:
Xavier Naveira67ab6242015-02-10 08:45:18 +01004075 case TP_HKEY_EV_KEY_FN_ESC:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004076 /* key press events, we just ignore them as long as the EC
4077 * is still reporting them in the normal keyboard stream */
4078 *send_acpi_ev = false;
4079 *ignore_acpi_ev = true;
4080 return true;
4081
Lyudeb03f4d42016-11-11 15:15:03 -05004082 case TP_HKEY_EV_TABLET_CHANGED:
4083 tpacpi_input_send_tabletsw();
4084 hotkey_tablet_mode_notify_change();
4085 *send_acpi_ev = false;
4086 break;
4087
David Herrmann587d8622018-01-12 12:04:45 +01004088 case TP_HKEY_EV_PALM_DETECTED:
4089 case TP_HKEY_EV_PALM_UNDETECTED:
4090 /* palm detected hovering the keyboard, forward to user-space
4091 * via netlink for consumption */
4092 return true;
4093
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004094 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004095 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004096 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004097 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004098
4099 thermal_dump_all_sensors();
4100
4101 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004102}
4103
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004104static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4105{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004106 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004107 bool send_acpi_ev;
4108 bool ignore_acpi_ev;
4109 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004110
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004111 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07004112 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004113 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004114 acpi_bus_generate_netlink_event(
4115 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004116 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004117 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004118 return;
4119 }
4120
4121 while (1) {
4122 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07004123 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004124 return;
4125 }
4126
4127 if (hkey == 0) {
4128 /* queue empty */
4129 return;
4130 }
4131
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004132 send_acpi_ev = true;
4133 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004134
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004135 switch (hkey >> 12) {
4136 case 1:
4137 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004138 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4139 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004140 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004141 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004142 /* 0x2000-0x2FFF: Wakeup reason */
4143 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4144 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004145 break;
4146 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004147 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004148 switch (hkey) {
4149 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004150 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004151 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004152 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004153 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004154 break;
4155 case TP_HKEY_EV_OPTDRV_EJ:
4156 /* FIXME: kick libata if SATA link offline */
4157 known_ev = true;
4158 break;
4159 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004160 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004161 }
4162 break;
4163 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03004164 /* 0x4000-0x4FFF: dock-related events */
4165 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4166 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004167 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004168 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02004169 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004170 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4171 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004172 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004173 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004174 /* 0x6000-0x6FFF: thermal alarms/notices and
4175 * keyboard events */
4176 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004177 &ignore_acpi_ev);
4178 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004179 case 7:
4180 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004181 if (tp_features.hotkey_wlsw &&
4182 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004183 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004184 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004185 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004186 break;
4187 }
4188 /* fallthrough to default */
4189 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004190 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004191 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004192 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07004193 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004194 pr_notice("please report the conditions when this event happened to %s\n",
4195 TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004196 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004197
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004198 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03004199 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004200 acpi_bus_generate_netlink_event(
4201 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004202 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004203 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004204 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004205 }
4206}
4207
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02004208static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004209{
4210 /* Do these on suspend, we get the events on early resume! */
4211 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4212 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08004213
4214 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004215 if (tp_features.has_adaptive_kbd) {
4216 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4217 "GTRW", "dd", 0)) {
4218 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004219 }
4220 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004221}
4222
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004223static void hotkey_resume(void)
4224{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03004225 tpacpi_disable_brightness_delay();
4226
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004227 if (hotkey_status_set(true) < 0 ||
4228 hotkey_mask_set(hotkey_acpi_mask) < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004229 pr_err("error while attempting to reset the event firmware interface\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004230
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004231 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02004232 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004233 hotkey_wakeup_reason_notify_change();
4234 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004235 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004236
4237 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004238 if (tp_features.has_adaptive_kbd) {
4239 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4240 adaptive_keyboard_prev_mode)) {
4241 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004242 }
4243 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004244}
4245
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004246/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004247static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004249 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004251 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004252 seq_printf(m, "status:\t\tnot supported\n");
4253 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004254 }
4255
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004256 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004257 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004258 res = hotkey_status_get(&status);
4259 if (!res)
4260 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004261 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004262 if (res)
4263 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004265 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004266 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004267 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4268 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004270 seq_printf(m, "mask:\t\tnot supported\n");
4271 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 }
4273
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275}
4276
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004277static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004278{
4279 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004280 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004281 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4282 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 +00004283}
4284
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004285static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004287 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004288 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004291 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 return -ENODEV;
4293
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004294 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004295 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004296
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004297 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004298
4299 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 while ((cmd = next_cmd(&buf))) {
4301 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004302 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004304 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004305 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03004307 mask = (hotkey_all_mask | hotkey_source_mask)
4308 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4310 /* mask set */
4311 } else if (sscanf(cmd, "%x", &mask) == 1) {
4312 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004313 } else {
4314 res = -EINVAL;
4315 goto errexit;
4316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004318
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004319 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004320 tpacpi_disclose_usertask("procfs hotkey",
4321 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004322 res = hotkey_user_mask_set(mask);
4323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004325errexit:
4326 mutex_unlock(&hotkey_mutex);
4327 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004328}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004330static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004331 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4332 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Hui Wanga3c42a42016-11-08 16:13:23 +08004333 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004334 {"", 0},
4335};
4336
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004337static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004338 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004339 .notify = hotkey_notify,
4340 .handle = &hkey_handle,
4341 .type = ACPI_DEVICE_NOTIFY,
4342};
4343
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004344static struct ibm_struct hotkey_driver_data = {
4345 .name = "hotkey",
4346 .read = hotkey_read,
4347 .write = hotkey_write,
4348 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004349 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004350 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004351 .acpi = &ibm_hotkey_acpidriver,
4352};
4353
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004354/*************************************************************************
4355 * Bluetooth subdriver
4356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004358enum {
4359 /* ACPI GBDC/SBDC bits */
4360 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4361 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004362 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004363 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004364};
4365
4366enum {
4367 /* ACPI \BLTH commands */
4368 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4369 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4370 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4371 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4372 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004373};
4374
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004375#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4376
Johannes Berg19d337d2009-06-02 13:01:37 +02004377static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004378{
4379 int status;
4380
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004381#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4382 if (dbg_bluetoothemul)
4383 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004384 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004385#endif
4386
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004387 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4388 return -EIO;
4389
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004390 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004391 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004392}
4393
Johannes Berg19d337d2009-06-02 13:01:37 +02004394static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004395{
4396 int status;
4397
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004398 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004399 "will attempt to %s bluetooth\n",
4400 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004401
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004402#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4403 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004404 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004405 return 0;
4406 }
4407#endif
4408
Johannes Berg19d337d2009-06-02 13:01:37 +02004409 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004410 status = TP_ACPI_BLUETOOTH_RADIOSSW
4411 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4412 else
4413 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004414
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004415 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4416 return -EIO;
4417
4418 return 0;
4419}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004420
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004421/* sysfs bluetooth enable ---------------------------------------------- */
4422static ssize_t bluetooth_enable_show(struct device *dev,
4423 struct device_attribute *attr,
4424 char *buf)
4425{
Johannes Berg19d337d2009-06-02 13:01:37 +02004426 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4427 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004428}
4429
4430static ssize_t bluetooth_enable_store(struct device *dev,
4431 struct device_attribute *attr,
4432 const char *buf, size_t count)
4433{
Johannes Berg19d337d2009-06-02 13:01:37 +02004434 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4435 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004436}
4437
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004438static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004439
4440/* --------------------------------------------------------------------- */
4441
4442static struct attribute *bluetooth_attributes[] = {
4443 &dev_attr_bluetooth_enable.attr,
4444 NULL
4445};
4446
4447static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004448 .attrs = bluetooth_attributes,
4449};
4450
Johannes Berg19d337d2009-06-02 13:01:37 +02004451static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4452 .get_status = bluetooth_get_status,
4453 .set_status = bluetooth_set_status,
4454};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004455
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004456static void bluetooth_shutdown(void)
4457{
4458 /* Order firmware to save current state to NVRAM */
4459 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4460 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004461 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004462 else
4463 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004464 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004465}
4466
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004467static void bluetooth_exit(void)
4468{
4469 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4470 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004471
4472 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4473
4474 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004475}
4476
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004477static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004479 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004480 int status = 0;
4481
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004482 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4483 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004484
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004485 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004486
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004487 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4488 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004489 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004490 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004492 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4493 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004494 str_supported(tp_features.bluetooth),
4495 status);
4496
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004497#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4498 if (dbg_bluetoothemul) {
4499 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004500 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004501 } else
4502#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004503 if (tp_features.bluetooth &&
4504 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4505 /* no bluetooth hardware present in system */
4506 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004507 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004508 "bluetooth hardware not installed\n");
4509 }
4510
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004511 if (!tp_features.bluetooth)
4512 return 1;
4513
Johannes Berg19d337d2009-06-02 13:01:37 +02004514 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4515 &bluetooth_tprfk_ops,
4516 RFKILL_TYPE_BLUETOOTH,
4517 TPACPI_RFK_BLUETOOTH_SW_NAME,
4518 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004519 if (res)
4520 return res;
4521
Johannes Berg19d337d2009-06-02 13:01:37 +02004522 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4523 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004524 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004525 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004526 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004527 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004528
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004529 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530}
4531
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004532/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004533static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004535 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536}
4537
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004538static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539{
Johannes Berg19d337d2009-06-02 13:01:37 +02004540 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541}
4542
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004543static struct ibm_struct bluetooth_driver_data = {
4544 .name = "bluetooth",
4545 .read = bluetooth_read,
4546 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004547 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004548 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004549};
4550
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004551/*************************************************************************
4552 * Wan subdriver
4553 */
4554
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004555enum {
4556 /* ACPI GWAN/SWAN bits */
4557 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4558 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004559 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004560 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004561};
4562
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004563#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4564
Johannes Berg19d337d2009-06-02 13:01:37 +02004565static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004566{
4567 int status;
4568
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004569#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4570 if (dbg_wwanemul)
4571 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004572 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004573#endif
4574
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004575 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4576 return -EIO;
4577
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004578 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004579 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004580}
4581
Johannes Berg19d337d2009-06-02 13:01:37 +02004582static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004583{
4584 int status;
4585
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004586 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004587 "will attempt to %s wwan\n",
4588 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004589
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004590#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4591 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004592 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004593 return 0;
4594 }
4595#endif
4596
Johannes Berg19d337d2009-06-02 13:01:37 +02004597 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004598 status = TP_ACPI_WANCARD_RADIOSSW
4599 | TP_ACPI_WANCARD_RESUMECTRL;
4600 else
4601 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004602
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004603 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4604 return -EIO;
4605
4606 return 0;
4607}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004608
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004609/* sysfs wan enable ---------------------------------------------------- */
4610static ssize_t wan_enable_show(struct device *dev,
4611 struct device_attribute *attr,
4612 char *buf)
4613{
Johannes Berg19d337d2009-06-02 13:01:37 +02004614 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4615 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004616}
4617
4618static ssize_t wan_enable_store(struct device *dev,
4619 struct device_attribute *attr,
4620 const char *buf, size_t count)
4621{
Johannes Berg19d337d2009-06-02 13:01:37 +02004622 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4623 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004624}
4625
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004626static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4627 wan_enable_show, wan_enable_store);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004628
4629/* --------------------------------------------------------------------- */
4630
4631static struct attribute *wan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004632 &dev_attr_wwan_enable.attr,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004633 NULL
4634};
4635
4636static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004637 .attrs = wan_attributes,
4638};
4639
Johannes Berg19d337d2009-06-02 13:01:37 +02004640static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4641 .get_status = wan_get_status,
4642 .set_status = wan_set_status,
4643};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004644
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004645static void wan_shutdown(void)
4646{
4647 /* Order firmware to save current state to NVRAM */
4648 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4649 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004650 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004651 else
4652 vdbg_printk(TPACPI_DBG_RFKILL,
4653 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004654}
4655
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004656static void wan_exit(void)
4657{
4658 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4659 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004660
4661 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4662
4663 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004664}
4665
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004666static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004667{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004668 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004669 int status = 0;
4670
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004671 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4672 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004673
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004674 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004675
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004676 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004677 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004678
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004679 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4680 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004681 str_supported(tp_features.wan),
4682 status);
4683
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004684#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4685 if (dbg_wwanemul) {
4686 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004687 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004688 } else
4689#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004690 if (tp_features.wan &&
4691 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4692 /* no wan hardware present in system */
4693 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004694 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004695 "wan hardware not installed\n");
4696 }
4697
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004698 if (!tp_features.wan)
4699 return 1;
4700
Johannes Berg19d337d2009-06-02 13:01:37 +02004701 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4702 &wan_tprfk_ops,
4703 RFKILL_TYPE_WWAN,
4704 TPACPI_RFK_WWAN_SW_NAME,
4705 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004706 if (res)
4707 return res;
4708
Johannes Berg19d337d2009-06-02 13:01:37 +02004709 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4710 &wan_attr_group);
4711
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004712 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004713 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004714 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004715 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004716
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004717 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004718}
4719
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004720/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004721static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004722{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004723 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004724}
4725
4726static int wan_write(char *buf)
4727{
Johannes Berg19d337d2009-06-02 13:01:37 +02004728 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004729}
4730
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004731static struct ibm_struct wan_driver_data = {
4732 .name = "wan",
4733 .read = wan_read,
4734 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004735 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004736 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004737};
4738
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004739/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004740 * UWB subdriver
4741 */
4742
4743enum {
4744 /* ACPI GUWB/SUWB bits */
4745 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4746 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4747};
4748
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004749#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4750
Johannes Berg19d337d2009-06-02 13:01:37 +02004751static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004752{
4753 int status;
4754
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004755#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4756 if (dbg_uwbemul)
4757 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004758 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004759#endif
4760
4761 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4762 return -EIO;
4763
4764 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004765 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004766}
4767
Johannes Berg19d337d2009-06-02 13:01:37 +02004768static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004769{
4770 int status;
4771
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004772 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004773 "will attempt to %s UWB\n",
4774 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004775
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004776#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4777 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004778 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004779 return 0;
4780 }
4781#endif
4782
Johannes Berg19d337d2009-06-02 13:01:37 +02004783 if (state == TPACPI_RFK_RADIO_ON)
4784 status = TP_ACPI_UWB_RADIOSSW;
4785 else
4786 status = 0;
4787
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004788 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4789 return -EIO;
4790
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004791 return 0;
4792}
4793
4794/* --------------------------------------------------------------------- */
4795
Johannes Berg19d337d2009-06-02 13:01:37 +02004796static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4797 .get_status = uwb_get_status,
4798 .set_status = uwb_set_status,
4799};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004800
4801static void uwb_exit(void)
4802{
Johannes Berg19d337d2009-06-02 13:01:37 +02004803 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004804}
4805
4806static int __init uwb_init(struct ibm_init_struct *iibm)
4807{
4808 int res;
4809 int status = 0;
4810
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004811 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4812 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004813
4814 TPACPI_ACPIHANDLE_INIT(hkey);
4815
4816 tp_features.uwb = hkey_handle &&
4817 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4818
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004819 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4820 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004821 str_supported(tp_features.uwb),
4822 status);
4823
4824#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4825 if (dbg_uwbemul) {
4826 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004827 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004828 } else
4829#endif
4830 if (tp_features.uwb &&
4831 !(status & TP_ACPI_UWB_HWPRESENT)) {
4832 /* no uwb hardware present in system */
4833 tp_features.uwb = 0;
4834 dbg_printk(TPACPI_DBG_INIT,
4835 "uwb hardware not installed\n");
4836 }
4837
4838 if (!tp_features.uwb)
4839 return 1;
4840
4841 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004842 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004843 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004844 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004845 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004846 return res;
4847}
4848
4849static struct ibm_struct uwb_driver_data = {
4850 .name = "uwb",
4851 .exit = uwb_exit,
4852 .flags.experimental = 1,
4853};
4854
4855/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004856 * Video subdriver
4857 */
4858
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004859#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4860
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004861enum video_access_mode {
4862 TPACPI_VIDEO_NONE = 0,
4863 TPACPI_VIDEO_570, /* 570 */
4864 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4865 TPACPI_VIDEO_NEW, /* all others */
4866};
4867
4868enum { /* video status flags, based on VIDEO_570 */
4869 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4870 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4871 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4872};
4873
4874enum { /* TPACPI_VIDEO_570 constants */
4875 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4876 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4877 * video_status_flags */
4878 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4879 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4880};
4881
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004882static enum video_access_mode video_supported;
4883static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004884
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004885static int video_autosw_get(void);
4886static int video_autosw_set(int enable);
4887
Joe Perches112a6ee2011-04-04 10:06:24 -07004888TPACPI_HANDLE(vid, root,
4889 "\\_SB.PCI.AGP.VGA", /* 570 */
4890 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4891 "\\_SB.PCI0.VID0", /* 770e */
4892 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4893 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4894 "\\_SB.PCI0.AGP.VID", /* all others */
4895 ); /* R30, R31 */
4896
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004897TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004898
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004899static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004901 int ivga;
4902
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004903 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4904
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004905 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004906 if (tpacpi_is_ibm())
4907 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004908
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004909 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4910 /* G41, assume IVGA doesn't change */
4911 vid_handle = vid2_handle;
4912
4913 if (!vid_handle)
4914 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004915 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004916 else if (tpacpi_is_ibm() &&
4917 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004918 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004919 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004920 else if (tpacpi_is_ibm() &&
4921 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004922 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004923 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004924 else
4925 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004926 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004928 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4929 str_supported(video_supported != TPACPI_VIDEO_NONE),
4930 video_supported);
4931
Jan van den Berg72a979f2014-09-17 00:01:08 +02004932 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933}
4934
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004935static void video_exit(void)
4936{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004937 dbg_printk(TPACPI_DBG_EXIT,
4938 "restoring original video autoswitch mode\n");
4939 if (video_autosw_set(video_orig_autosw))
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004940 pr_err("error while trying to restore original video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004941}
4942
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004943static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944{
4945 int status = 0;
4946 int i;
4947
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004948 switch (video_supported) {
4949 case TPACPI_VIDEO_570:
4950 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4951 TP_ACPI_VIDEO_570_PHSCMD))
4952 return -EIO;
4953 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4954 break;
4955 case TPACPI_VIDEO_770:
4956 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4957 return -EIO;
4958 if (i)
4959 status |= TP_ACPI_VIDEO_S_LCD;
4960 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4961 return -EIO;
4962 if (i)
4963 status |= TP_ACPI_VIDEO_S_CRT;
4964 break;
4965 case TPACPI_VIDEO_NEW:
4966 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4967 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4968 return -EIO;
4969 if (i)
4970 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004972 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4973 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4974 return -EIO;
4975 if (i)
4976 status |= TP_ACPI_VIDEO_S_LCD;
4977 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4978 return -EIO;
4979 if (i)
4980 status |= TP_ACPI_VIDEO_S_DVI;
4981 break;
4982 default:
4983 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
4986 return status;
4987}
4988
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004989static int video_outputsw_set(int status)
4990{
4991 int autosw;
4992 int res = 0;
4993
4994 switch (video_supported) {
4995 case TPACPI_VIDEO_570:
4996 res = acpi_evalf(NULL, NULL,
4997 "\\_SB.PHS2", "vdd",
4998 TP_ACPI_VIDEO_570_PHS2CMD,
4999 status | TP_ACPI_VIDEO_570_PHS2SET);
5000 break;
5001 case TPACPI_VIDEO_770:
5002 autosw = video_autosw_get();
5003 if (autosw < 0)
5004 return autosw;
5005
5006 res = video_autosw_set(1);
5007 if (res)
5008 return res;
5009 res = acpi_evalf(vid_handle, NULL,
5010 "ASWT", "vdd", status * 0x100, 0);
5011 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07005012 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005013 return -EIO;
5014 }
5015 break;
5016 case TPACPI_VIDEO_NEW:
5017 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005018 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005019 break;
5020 default:
5021 return -ENOSYS;
5022 }
5023
Jan van den Berg72a979f2014-09-17 00:01:08 +02005024 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005025}
5026
5027static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005029 int autosw = 0;
5030
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005031 switch (video_supported) {
5032 case TPACPI_VIDEO_570:
5033 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5034 return -EIO;
5035 break;
5036 case TPACPI_VIDEO_770:
5037 case TPACPI_VIDEO_NEW:
5038 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5039 return -EIO;
5040 break;
5041 default:
5042 return -ENOSYS;
5043 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005044
5045 return autosw & 1;
5046}
5047
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005048static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005049{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005050 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005051 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005052 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005053}
5054
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005055static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005056{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005057 int autosw = video_autosw_get();
5058 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005059
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005060 if (autosw < 0)
5061 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005062
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005063 switch (video_supported) {
5064 case TPACPI_VIDEO_570:
5065 res = video_autosw_set(1);
5066 if (res)
5067 return res;
5068 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5069 break;
5070 case TPACPI_VIDEO_770:
5071 case TPACPI_VIDEO_NEW:
5072 res = video_autosw_set(1);
5073 if (res)
5074 return res;
5075 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5076 break;
5077 default:
5078 return -ENOSYS;
5079 }
5080 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07005081 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005082 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005083 }
5084
Jan van den Berg72a979f2014-09-17 00:01:08 +02005085 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005086}
5087
5088static int video_expand_toggle(void)
5089{
5090 switch (video_supported) {
5091 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005092 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005093 0 : -EIO;
5094 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005095 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005096 0 : -EIO;
5097 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005098 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005099 0 : -EIO;
5100 default:
5101 return -ENOSYS;
5102 }
5103 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005104}
5105
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005106static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005107{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005108 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005109
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005110 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005111 seq_printf(m, "status:\t\tnot supported\n");
5112 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005113 }
5114
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005115 /* Even reads can crash X.org, so... */
5116 if (!capable(CAP_SYS_ADMIN))
5117 return -EPERM;
5118
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005119 status = video_outputsw_get();
5120 if (status < 0)
5121 return status;
5122
5123 autosw = video_autosw_get();
5124 if (autosw < 0)
5125 return autosw;
5126
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005127 seq_printf(m, "status:\t\tsupported\n");
5128 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5129 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005130 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005131 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5132 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5133 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5134 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005135 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005136 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5137 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5138 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005139
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005140 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005141}
5142
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005143static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144{
5145 char *cmd;
5146 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005147 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005149 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005150 return -ENODEV;
5151
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005152 /* Even reads can crash X.org, let alone writes... */
5153 if (!capable(CAP_SYS_ADMIN))
5154 return -EPERM;
5155
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005156 enable = 0;
5157 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158
5159 while ((cmd = next_cmd(&buf))) {
5160 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005161 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005163 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005165 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005167 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005168 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005169 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005170 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005171 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005172 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005173 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005175 res = video_autosw_set(1);
5176 if (res)
5177 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005179 res = video_autosw_set(0);
5180 if (res)
5181 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005183 res = video_outputsw_cycle();
5184 if (res)
5185 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005187 res = video_expand_toggle();
5188 if (res)
5189 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 } else
5191 return -EINVAL;
5192 }
5193
5194 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005195 status = video_outputsw_get();
5196 if (status < 0)
5197 return status;
5198 res = video_outputsw_set((status & ~disable) | enable);
5199 if (res)
5200 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 }
5202
5203 return 0;
5204}
5205
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005206static struct ibm_struct video_driver_data = {
5207 .name = "video",
5208 .read = video_read,
5209 .write = video_write,
5210 .exit = video_exit,
5211};
5212
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005213#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5214
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005215/*************************************************************************
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005216 * Keyboard backlight subdriver
5217 */
5218
Hans de Goedec685e202017-02-09 16:44:13 +01005219static enum led_brightness kbdlight_brightness;
5220static DEFINE_MUTEX(kbdlight_mutex);
5221
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005222static int kbdlight_set_level(int level)
5223{
Hans de Goedec685e202017-02-09 16:44:13 +01005224 int ret = 0;
5225
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005226 if (!hkey_handle)
5227 return -ENXIO;
5228
Hans de Goedec685e202017-02-09 16:44:13 +01005229 mutex_lock(&kbdlight_mutex);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005230
Hans de Goedec685e202017-02-09 16:44:13 +01005231 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5232 ret = -EIO;
5233 else
5234 kbdlight_brightness = level;
5235
5236 mutex_unlock(&kbdlight_mutex);
5237
5238 return ret;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005239}
5240
5241static int kbdlight_get_level(void)
5242{
5243 int status = 0;
5244
5245 if (!hkey_handle)
5246 return -ENXIO;
5247
5248 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5249 return -EIO;
5250
5251 if (status < 0)
5252 return status;
5253
5254 return status & 0x3;
5255}
5256
5257static bool kbdlight_is_supported(void)
5258{
5259 int status = 0;
5260
5261 if (!hkey_handle)
5262 return false;
5263
5264 if (!acpi_has_method(hkey_handle, "MLCG")) {
5265 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5266 return false;
5267 }
5268
5269 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5270 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5271 return false;
5272 }
5273
5274 if (status < 0) {
5275 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5276 return false;
5277 }
5278
5279 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5280 /*
5281 * Guessed test for keyboard backlight:
5282 *
5283 * Machines with backlight keyboard return:
5284 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5285 * b110100010010000000XX - ThinkPad x230
5286 * b010100000010000000XX - ThinkPad x240
5287 * b010100000010000000XX - ThinkPad W541
5288 * (XX is current backlight level)
5289 *
5290 * Machines without backlight keyboard return:
5291 * b10100001000000000000 - ThinkPad x230
5292 * b10110001000000000000 - ThinkPad E430
5293 * b00000000000000000000 - ThinkPad E450
5294 *
5295 * Candidate BITs for detection test (XOR):
5296 * b01000000001000000000
5297 * ^
5298 */
5299 return status & BIT(9);
5300}
5301
Hans de Goede86ec0c22017-02-09 16:44:12 +01005302static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005303 enum led_brightness brightness)
5304{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005305 return kbdlight_set_level(brightness);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005306}
5307
5308static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5309{
5310 int level;
5311
5312 level = kbdlight_get_level();
5313 if (level < 0)
5314 return 0;
5315
5316 return level;
5317}
5318
5319static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5320 .led_classdev = {
5321 .name = "tpacpi::kbd_backlight",
5322 .max_brightness = 2,
Hans de Goedec685e202017-02-09 16:44:13 +01005323 .flags = LED_BRIGHT_HW_CHANGED,
Hans de Goede86ec0c22017-02-09 16:44:12 +01005324 .brightness_set_blocking = &kbdlight_sysfs_set,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005325 .brightness_get = &kbdlight_sysfs_get,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005326 }
5327};
5328
5329static int __init kbdlight_init(struct ibm_init_struct *iibm)
5330{
5331 int rc;
5332
5333 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5334
5335 TPACPI_ACPIHANDLE_INIT(hkey);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005336
5337 if (!kbdlight_is_supported()) {
5338 tp_features.kbdlight = 0;
5339 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5340 return 1;
5341 }
5342
Hans de Goedec685e202017-02-09 16:44:13 +01005343 kbdlight_brightness = kbdlight_sysfs_get(NULL);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005344 tp_features.kbdlight = 1;
5345
5346 rc = led_classdev_register(&tpacpi_pdev->dev,
5347 &tpacpi_led_kbdlight.led_classdev);
5348 if (rc < 0) {
5349 tp_features.kbdlight = 0;
5350 return rc;
5351 }
5352
Hans de Goedec685e202017-02-09 16:44:13 +01005353 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5354 TP_ACPI_HKEY_KBD_LIGHT_MASK);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005355 return 0;
5356}
5357
5358static void kbdlight_exit(void)
5359{
5360 if (tp_features.kbdlight)
5361 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005362}
5363
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005364static int kbdlight_set_level_and_update(int level)
5365{
5366 int ret;
5367 struct led_classdev *led_cdev;
5368
5369 ret = kbdlight_set_level(level);
5370 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5371
5372 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5373 led_cdev->brightness = level;
5374
5375 return ret;
5376}
5377
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005378static int kbdlight_read(struct seq_file *m)
5379{
5380 int level;
5381
5382 if (!tp_features.kbdlight) {
5383 seq_printf(m, "status:\t\tnot supported\n");
5384 } else {
5385 level = kbdlight_get_level();
5386 if (level < 0)
5387 seq_printf(m, "status:\t\terror %d\n", level);
5388 else
5389 seq_printf(m, "status:\t\t%d\n", level);
5390 seq_printf(m, "commands:\t0, 1, 2\n");
5391 }
5392
5393 return 0;
5394}
5395
5396static int kbdlight_write(char *buf)
5397{
5398 char *cmd;
5399 int level = -1;
5400
5401 if (!tp_features.kbdlight)
5402 return -ENODEV;
5403
5404 while ((cmd = next_cmd(&buf))) {
5405 if (strlencmp(cmd, "0") == 0)
5406 level = 0;
5407 else if (strlencmp(cmd, "1") == 0)
5408 level = 1;
5409 else if (strlencmp(cmd, "2") == 0)
5410 level = 2;
5411 else
5412 return -EINVAL;
5413 }
5414
5415 if (level == -1)
5416 return -EINVAL;
5417
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005418 return kbdlight_set_level_and_update(level);
5419}
5420
5421static void kbdlight_suspend(void)
5422{
5423 struct led_classdev *led_cdev;
5424
5425 if (!tp_features.kbdlight)
5426 return;
5427
5428 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5429 led_update_brightness(led_cdev);
5430 led_classdev_suspend(led_cdev);
5431}
5432
5433static void kbdlight_resume(void)
5434{
5435 if (!tp_features.kbdlight)
5436 return;
5437
5438 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005439}
5440
5441static struct ibm_struct kbdlight_driver_data = {
5442 .name = "kbdlight",
5443 .read = kbdlight_read,
5444 .write = kbdlight_write,
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005445 .suspend = kbdlight_suspend,
5446 .resume = kbdlight_resume,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005447 .exit = kbdlight_exit,
5448};
5449
5450/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005451 * Light (thinklight) subdriver
5452 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005454TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5455TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005456
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005457static int light_get_status(void)
5458{
5459 int status = 0;
5460
5461 if (tp_features.light_status) {
5462 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5463 return -EIO;
5464 return (!!status);
5465 }
5466
5467 return -ENXIO;
5468}
5469
5470static int light_set_status(int status)
5471{
5472 int rc;
5473
5474 if (tp_features.light) {
5475 if (cmos_handle) {
5476 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005477 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005478 TP_CMOS_THINKLIGHT_ON :
5479 TP_CMOS_THINKLIGHT_OFF);
5480 } else {
5481 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005482 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005483 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005484 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005485 }
5486
5487 return -ENXIO;
5488}
5489
Hans de Goede86ec0c22017-02-09 16:44:12 +01005490static int light_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005491 enum led_brightness brightness)
5492{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005493 return light_set_status((brightness != LED_OFF) ?
5494 TPACPI_LED_ON : TPACPI_LED_OFF);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005495}
5496
5497static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5498{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005499 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005500}
5501
5502static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5503 .led_classdev = {
5504 .name = "tpacpi::thinklight",
Hans de Goede86ec0c22017-02-09 16:44:12 +01005505 .brightness_set_blocking = &light_sysfs_set,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005506 .brightness_get = &light_sysfs_get,
5507 }
5508};
5509
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005510static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005512 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005513
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005514 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5515
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005516 if (tpacpi_is_ibm()) {
5517 TPACPI_ACPIHANDLE_INIT(ledb);
5518 TPACPI_ACPIHANDLE_INIT(lght);
5519 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005520 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005521
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005522 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005523 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005524
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005525 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005526 /* light status not supported on
5527 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005528 tp_features.light_status =
5529 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005531 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5532 str_supported(tp_features.light),
5533 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005534
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005535 if (!tp_features.light)
5536 return 1;
5537
5538 rc = led_classdev_register(&tpacpi_pdev->dev,
5539 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005540
5541 if (rc < 0) {
5542 tp_features.light = 0;
5543 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005544 } else {
5545 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005546 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005547
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005548 return rc;
5549}
5550
5551static void light_exit(void)
5552{
5553 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554}
5555
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005556static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005558 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005560 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005561 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005562 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005563 seq_printf(m, "status:\t\tunknown\n");
5564 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005565 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005566 status = light_get_status();
5567 if (status < 0)
5568 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005569 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5570 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574}
5575
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005576static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005579 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005580
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005581 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005582 return -ENODEV;
5583
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 while ((cmd = next_cmd(&buf))) {
5585 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005586 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005588 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 } else
5590 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 }
5592
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005593 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594}
5595
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005596static struct ibm_struct light_driver_data = {
5597 .name = "light",
5598 .read = light_read,
5599 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005600 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005601};
5602
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005603/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005604 * CMOS subdriver
5605 */
5606
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005607/* sysfs cmos_command -------------------------------------------------- */
5608static ssize_t cmos_command_store(struct device *dev,
5609 struct device_attribute *attr,
5610 const char *buf, size_t count)
5611{
5612 unsigned long cmos_cmd;
5613 int res;
5614
5615 if (parse_strtoul(buf, 21, &cmos_cmd))
5616 return -EINVAL;
5617
5618 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005619 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005620}
5621
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01005622static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005623
5624/* --------------------------------------------------------------------- */
5625
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005626static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005627{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005628 int res;
5629
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005630 vdbg_printk(TPACPI_DBG_INIT,
5631 "initializing cmos commands subdriver\n");
5632
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005633 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005634
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005635 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5636 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005637
5638 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5639 if (res)
5640 return res;
5641
Jan van den Berg72a979f2014-09-17 00:01:08 +02005642 return (cmos_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005643}
5644
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005645static void cmos_exit(void)
5646{
5647 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5648}
5649
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005650static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005652 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5653 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005655 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005657 seq_printf(m, "status:\t\tsupported\n");
5658 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 }
5660
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662}
5663
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005664static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665{
5666 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005667 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668
5669 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005670 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5671 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 /* cmos_cmd set */
5673 } else
5674 return -EINVAL;
5675
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005676 res = issue_thinkpad_cmos_command(cmos_cmd);
5677 if (res)
5678 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 }
5680
5681 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005682}
5683
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005684static struct ibm_struct cmos_driver_data = {
5685 .name = "cmos",
5686 .read = cmos_read,
5687 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005688 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005689};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005690
5691/*************************************************************************
5692 * LED subdriver
5693 */
5694
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005695enum led_access_mode {
5696 TPACPI_LED_NONE = 0,
5697 TPACPI_LED_570, /* 570 */
5698 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5699 TPACPI_LED_NEW, /* all others */
5700};
5701
5702enum { /* For TPACPI_LED_OLD */
5703 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5704 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5705 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5706};
5707
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005708static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005709
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005710static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005711
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005712#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005713static struct tpacpi_led_classdev *tpacpi_leds;
5714static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005715static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005716 /* there's a limit of 19 chars + NULL before 2.6.26 */
5717 "tpacpi::power",
5718 "tpacpi:orange:batt",
5719 "tpacpi:green:batt",
5720 "tpacpi::dock_active",
5721 "tpacpi::bay_active",
5722 "tpacpi::dock_batt",
5723 "tpacpi::unknown_led",
5724 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005725 "tpacpi::dock_status1",
5726 "tpacpi::dock_status2",
5727 "tpacpi::unknown_led2",
5728 "tpacpi::unknown_led3",
5729 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005730};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005731#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005732
5733static inline bool tpacpi_is_led_restricted(const unsigned int led)
5734{
5735#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5736 return false;
5737#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005738 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005739#endif
5740}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005741
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005742static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005743{
5744 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005745 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005746
5747 switch (led_supported) {
5748 case TPACPI_LED_570:
5749 if (!acpi_evalf(ec_handle,
5750 &status, "GLED", "dd", 1 << led))
5751 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005752 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005753 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005754 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005755 TPACPI_LED_ON :
5756 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005757 tpacpi_led_state_cache[led] = led_s;
5758 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005759 default:
5760 return -ENXIO;
5761 }
5762
5763 /* not reached */
5764}
5765
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005766static int led_set_status(const unsigned int led,
5767 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005768{
5769 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005770 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5771 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005772
5773 int rc = 0;
5774
5775 switch (led_supported) {
5776 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005777 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005778 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005779 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005780 if (unlikely(tpacpi_is_led_restricted(led)))
5781 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005782 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5783 (1 << led), led_sled_arg1[ledstatus]))
5784 rc = -EIO;
5785 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005786 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005787 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005788 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005789 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005790 if (unlikely(tpacpi_is_led_restricted(led)))
5791 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005792 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5793 if (rc >= 0)
5794 rc = ec_write(TPACPI_LED_EC_HLBL,
5795 (ledstatus == TPACPI_LED_BLINK) << led);
5796 if (rc >= 0)
5797 rc = ec_write(TPACPI_LED_EC_HLCL,
5798 (ledstatus != TPACPI_LED_OFF) << led);
5799 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005800 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005801 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005802 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5803 return -EINVAL;
5804 if (unlikely(tpacpi_is_led_restricted(led)))
5805 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005806 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5807 led, led_led_arg1[ledstatus]))
5808 rc = -EIO;
5809 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005810 default:
5811 rc = -ENXIO;
5812 }
5813
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005814 if (!rc)
5815 tpacpi_led_state_cache[led] = ledstatus;
5816
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005817 return rc;
5818}
5819
Hans de Goede86ec0c22017-02-09 16:44:12 +01005820static int led_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005821 enum led_brightness brightness)
5822{
5823 struct tpacpi_led_classdev *data = container_of(led_cdev,
5824 struct tpacpi_led_classdev, led_classdev);
Hans de Goede86ec0c22017-02-09 16:44:12 +01005825 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005826
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005827 if (brightness == LED_OFF)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005828 new_state = TPACPI_LED_OFF;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005829 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005830 new_state = TPACPI_LED_ON;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005831 else
Hans de Goede86ec0c22017-02-09 16:44:12 +01005832 new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005833
Hans de Goede86ec0c22017-02-09 16:44:12 +01005834 return led_set_status(data->led, new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005835}
5836
5837static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5838 unsigned long *delay_on, unsigned long *delay_off)
5839{
5840 struct tpacpi_led_classdev *data = container_of(led_cdev,
5841 struct tpacpi_led_classdev, led_classdev);
5842
5843 /* Can we choose the flash rate? */
5844 if (*delay_on == 0 && *delay_off == 0) {
5845 /* yes. set them to the hardware blink rate (1 Hz) */
5846 *delay_on = 500; /* ms */
5847 *delay_off = 500; /* ms */
5848 } else if ((*delay_on != 500) || (*delay_off != 500))
5849 return -EINVAL;
5850
Hans de Goede86ec0c22017-02-09 16:44:12 +01005851 return led_set_status(data->led, TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005852}
5853
5854static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5855{
5856 int rc;
5857
5858 struct tpacpi_led_classdev *data = container_of(led_cdev,
5859 struct tpacpi_led_classdev, led_classdev);
5860
5861 rc = led_get_status(data->led);
5862
5863 if (rc == TPACPI_LED_OFF || rc < 0)
5864 rc = LED_OFF; /* no error handling in led class :( */
5865 else
5866 rc = LED_FULL;
5867
5868 return rc;
5869}
5870
5871static void led_exit(void)
5872{
5873 unsigned int i;
5874
5875 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5876 if (tpacpi_leds[i].led_classdev.name)
5877 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5878 }
5879
5880 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005881}
5882
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005883static int __init tpacpi_init_led(unsigned int led)
5884{
5885 int rc;
5886
5887 tpacpi_leds[led].led = led;
5888
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005889 /* LEDs with no name don't get registered */
5890 if (!tpacpi_led_names[led])
5891 return 0;
5892
Hans de Goede86ec0c22017-02-09 16:44:12 +01005893 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005894 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5895 if (led_supported == TPACPI_LED_570)
5896 tpacpi_leds[led].led_classdev.brightness_get =
5897 &led_sysfs_get;
5898
5899 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5900
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005901 rc = led_classdev_register(&tpacpi_pdev->dev,
5902 &tpacpi_leds[led].led_classdev);
5903 if (rc < 0)
5904 tpacpi_leds[led].led_classdev.name = NULL;
5905
5906 return rc;
5907}
5908
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005909static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5910 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5911 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5912 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5913
5914 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5915 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5916 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5917 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5918 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5919 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5920 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5921 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5922
5923 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5924 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5925 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5926 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5927 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5928
5929 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5930 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5931 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5932 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5933
5934 /* (1) - may have excess leds enabled on MSB */
5935
5936 /* Defaults (order matters, keep last, don't reorder!) */
5937 { /* Lenovo */
5938 .vendor = PCI_VENDOR_ID_LENOVO,
5939 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5940 .quirks = 0x1fffU,
5941 },
5942 { /* IBM ThinkPads with no EC version string */
5943 .vendor = PCI_VENDOR_ID_IBM,
5944 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5945 .quirks = 0x00ffU,
5946 },
5947 { /* IBM ThinkPads with EC version string */
5948 .vendor = PCI_VENDOR_ID_IBM,
5949 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5950 .quirks = 0x00bfU,
5951 },
5952};
5953
5954#undef TPACPI_LEDQ_IBM
5955#undef TPACPI_LEDQ_LNV
5956
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005957static enum led_access_mode __init led_init_detect_mode(void)
5958{
5959 acpi_status status;
5960
5961 if (tpacpi_is_ibm()) {
5962 /* 570 */
5963 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5964 if (ACPI_SUCCESS(status))
5965 return TPACPI_LED_570;
5966
5967 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5968 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5969 if (ACPI_SUCCESS(status))
5970 return TPACPI_LED_OLD;
5971 }
5972
5973 /* most others */
5974 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5975 if (ACPI_SUCCESS(status))
5976 return TPACPI_LED_NEW;
5977
5978 /* R30, R31, and unknown firmwares */
5979 led_handle = NULL;
5980 return TPACPI_LED_NONE;
5981}
5982
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005983static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005984{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005985 unsigned int i;
5986 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005987 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005988
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005989 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5990
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005991 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005992
Adam Leefcb44e12013-06-07 16:20:08 +08005993 if (led_supported != TPACPI_LED_NONE) {
5994 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5995 ARRAY_SIZE(led_useful_qtable));
5996
5997 if (!useful_leds) {
5998 led_handle = NULL;
5999 led_supported = TPACPI_LED_NONE;
6000 }
6001 }
6002
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006003 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
6004 str_supported(led_supported), led_supported);
6005
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006006 if (led_supported == TPACPI_LED_NONE)
6007 return 1;
6008
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006009 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
6010 GFP_KERNEL);
6011 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07006012 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006013 return -ENOMEM;
6014 }
6015
6016 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08006017 tpacpi_leds[i].led = -1;
6018
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006019 if (!tpacpi_is_led_restricted(i) &&
6020 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006021 rc = tpacpi_init_led(i);
6022 if (rc < 0) {
6023 led_exit();
6024 return rc;
6025 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006026 }
6027 }
6028
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006029#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006030 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00006031#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03006032 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006033}
6034
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006035#define str_led_status(s) \
6036 ((s) == TPACPI_LED_OFF ? "off" : \
6037 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006038
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006039static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006041 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006042 seq_printf(m, "status:\t\tnot supported\n");
6043 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006044 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006045 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006046
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006047 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006048 /* 570 */
6049 int i, status;
6050 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006051 status = led_get_status(i);
6052 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006053 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006054 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006055 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006056 }
6057 }
6058
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006059 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062}
6063
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006064static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065{
6066 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006067 int led, rc;
6068 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006069
6070 if (!led_supported)
6071 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
6073 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08006074 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 return -EINVAL;
6076
Adam Leeedf2d772013-06-08 16:51:15 +08006077 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
6078 tpacpi_leds[led].led < 0)
6079 return -ENODEV;
6080
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006081 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006082 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006084 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006085 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006086 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006087 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006088 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006089 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006090
6091 rc = led_set_status(led, s);
6092 if (rc < 0)
6093 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 }
6095
6096 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006097}
6098
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006099static struct ibm_struct led_driver_data = {
6100 .name = "led",
6101 .read = led_read,
6102 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006103 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006104};
6105
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006106/*************************************************************************
6107 * Beep subdriver
6108 */
6109
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006110TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006111
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006112#define TPACPI_BEEP_Q1 0x0001
6113
6114static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6115 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6116 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6117};
6118
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006119static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006120{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006121 unsigned long quirks;
6122
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006123 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6124
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006125 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006126
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006127 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6128 str_supported(beep_handle != NULL));
6129
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006130 quirks = tpacpi_check_quirks(beep_quirk_table,
6131 ARRAY_SIZE(beep_quirk_table));
6132
6133 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6134
Jan van den Berg72a979f2014-09-17 00:01:08 +02006135 return (beep_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006136}
6137
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006138static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006140 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006141 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006142 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006143 seq_printf(m, "status:\t\tsupported\n");
6144 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006147 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148}
6149
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006150static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151{
6152 char *cmd;
6153 int beep_cmd;
6154
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006155 if (!beep_handle)
6156 return -ENODEV;
6157
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006159 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6160 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 /* beep_cmd set */
6162 } else
6163 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006164 if (tp_features.beep_needs_two_args) {
6165 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6166 beep_cmd, 0))
6167 return -EIO;
6168 } else {
6169 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6170 beep_cmd))
6171 return -EIO;
6172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 }
6174
6175 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006176}
6177
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006178static struct ibm_struct beep_driver_data = {
6179 .name = "beep",
6180 .read = beep_read,
6181 .write = beep_write,
6182};
6183
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006184/*************************************************************************
6185 * Thermal subdriver
6186 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006187
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006188enum thermal_access_mode {
6189 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6190 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6191 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6192 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6193 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6194};
6195
6196enum { /* TPACPI_THERMAL_TPEC_* */
6197 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6198 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6199 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006200
6201 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006202};
6203
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006204
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006205#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6206struct ibm_thermal_sensors_struct {
6207 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6208};
6209
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006210static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006211
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006212/* idx is zero-based */
6213static int thermal_get_sensor(int idx, s32 *value)
6214{
6215 int t;
6216 s8 tmp;
6217 char tmpi[5];
6218
6219 t = TP_EC_THERMAL_TMP0;
6220
6221 switch (thermal_read_mode) {
6222#if TPACPI_MAX_THERMAL_SENSORS >= 16
6223 case TPACPI_THERMAL_TPEC_16:
6224 if (idx >= 8 && idx <= 15) {
6225 t = TP_EC_THERMAL_TMP8;
6226 idx -= 8;
6227 }
6228 /* fallthrough */
6229#endif
6230 case TPACPI_THERMAL_TPEC_8:
6231 if (idx <= 7) {
6232 if (!acpi_ec_read(t + idx, &tmp))
6233 return -EIO;
6234 *value = tmp * 1000;
6235 return 0;
6236 }
6237 break;
6238
6239 case TPACPI_THERMAL_ACPI_UPDT:
6240 if (idx <= 7) {
6241 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6242 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6243 return -EIO;
6244 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6245 return -EIO;
6246 *value = (t - 2732) * 100;
6247 return 0;
6248 }
6249 break;
6250
6251 case TPACPI_THERMAL_ACPI_TMP07:
6252 if (idx <= 7) {
6253 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6254 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6255 return -EIO;
6256 if (t > 127 || t < -127)
6257 t = TP_EC_THERMAL_TMP_NA;
6258 *value = t * 1000;
6259 return 0;
6260 }
6261 break;
6262
6263 case TPACPI_THERMAL_NONE:
6264 default:
6265 return -ENOSYS;
6266 }
6267
6268 return -EINVAL;
6269}
6270
6271static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6272{
6273 int res, i;
6274 int n;
6275
6276 n = 8;
6277 i = 0;
6278
6279 if (!s)
6280 return -EINVAL;
6281
6282 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6283 n = 16;
6284
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006285 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006286 res = thermal_get_sensor(i, &s->temp[i]);
6287 if (res)
6288 return res;
6289 }
6290
6291 return n;
6292}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006293
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006294static void thermal_dump_all_sensors(void)
6295{
6296 int n, i;
6297 struct ibm_thermal_sensors_struct t;
6298
6299 n = thermal_get_sensors(&t);
6300 if (n <= 0)
6301 return;
6302
Joe Perches0978e012011-04-04 10:06:25 -07006303 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006304
6305 for (i = 0; i < n; i++) {
6306 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07006307 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006308 else
Joe Perches0978e012011-04-04 10:06:25 -07006309 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006310 }
6311
Joe Perches0978e012011-04-04 10:06:25 -07006312 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006313}
6314
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006315/* sysfs temp##_input -------------------------------------------------- */
6316
6317static ssize_t thermal_temp_input_show(struct device *dev,
6318 struct device_attribute *attr,
6319 char *buf)
6320{
6321 struct sensor_device_attribute *sensor_attr =
6322 to_sensor_dev_attr(attr);
6323 int idx = sensor_attr->index;
6324 s32 value;
6325 int res;
6326
6327 res = thermal_get_sensor(idx, &value);
6328 if (res)
6329 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006330 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006331 return -ENXIO;
6332
6333 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6334}
6335
6336#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006337 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6338 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006339
6340static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6341 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6342 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6343 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6344 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6345 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6346 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6347 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6348 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6349 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6350 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6351 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6352 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6353 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6354 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6355 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6356 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6357};
6358
6359#define THERMAL_ATTRS(X) \
6360 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6361
6362static struct attribute *thermal_temp_input_attr[] = {
6363 THERMAL_ATTRS(8),
6364 THERMAL_ATTRS(9),
6365 THERMAL_ATTRS(10),
6366 THERMAL_ATTRS(11),
6367 THERMAL_ATTRS(12),
6368 THERMAL_ATTRS(13),
6369 THERMAL_ATTRS(14),
6370 THERMAL_ATTRS(15),
6371 THERMAL_ATTRS(0),
6372 THERMAL_ATTRS(1),
6373 THERMAL_ATTRS(2),
6374 THERMAL_ATTRS(3),
6375 THERMAL_ATTRS(4),
6376 THERMAL_ATTRS(5),
6377 THERMAL_ATTRS(6),
6378 THERMAL_ATTRS(7),
6379 NULL
6380};
6381
6382static const struct attribute_group thermal_temp_input16_group = {
6383 .attrs = thermal_temp_input_attr
6384};
6385
6386static const struct attribute_group thermal_temp_input8_group = {
6387 .attrs = &thermal_temp_input_attr[8]
6388};
6389
6390#undef THERMAL_SENSOR_ATTR_TEMP
6391#undef THERMAL_ATTRS
6392
6393/* --------------------------------------------------------------------- */
6394
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006395static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006396{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006397 u8 t, ta1, ta2;
6398 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006399 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006400 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006401
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006402 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6403
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006404 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006405
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03006406 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006407 /*
6408 * Direct EC access mode: sensors at registers
6409 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6410 * non-implemented, thermal sensors return 0x80 when
6411 * not available
6412 */
6413
6414 ta1 = ta2 = 0;
6415 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006416 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006417 ta1 |= t;
6418 } else {
6419 ta1 = 0;
6420 break;
6421 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006422 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006423 ta2 |= t;
6424 } else {
6425 ta1 = 0;
6426 break;
6427 }
6428 }
6429 if (ta1 == 0) {
6430 /* This is sheer paranoia, but we handle it anyway */
6431 if (acpi_tmp7) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006432 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006433 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006434 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006435 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006436 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006437 }
6438 } else {
6439 thermal_read_mode =
6440 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006441 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006442 }
6443 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006444 if (tpacpi_is_ibm() &&
6445 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006446 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006447 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006448 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006449 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006450 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006451 }
6452 } else {
6453 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006454 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006455 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006456
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006457 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6458 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6459 thermal_read_mode);
6460
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006461 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006462 case TPACPI_THERMAL_TPEC_16:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006463 res = sysfs_create_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006464 &thermal_temp_input16_group);
6465 if (res)
6466 return res;
6467 break;
6468 case TPACPI_THERMAL_TPEC_8:
6469 case TPACPI_THERMAL_ACPI_TMP07:
6470 case TPACPI_THERMAL_ACPI_UPDT:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006471 res = sysfs_create_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006472 &thermal_temp_input8_group);
6473 if (res)
6474 return res;
6475 break;
6476 case TPACPI_THERMAL_NONE:
6477 default:
6478 return 1;
6479 }
6480
6481 return 0;
6482}
6483
6484static void thermal_exit(void)
6485{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006486 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006487 case TPACPI_THERMAL_TPEC_16:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006488 sysfs_remove_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006489 &thermal_temp_input16_group);
6490 break;
6491 case TPACPI_THERMAL_TPEC_8:
6492 case TPACPI_THERMAL_ACPI_TMP07:
6493 case TPACPI_THERMAL_ACPI_UPDT:
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07006494 sysfs_remove_group(&tpacpi_hwmon->kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08006495 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006496 break;
6497 case TPACPI_THERMAL_NONE:
6498 default:
6499 break;
6500 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006501}
6502
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006503static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006504{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006505 int n, i;
6506 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006507
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006508 n = thermal_get_sensors(&t);
6509 if (unlikely(n < 0))
6510 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006511
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006512 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006513
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006514 if (n > 0) {
6515 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006516 seq_printf(m, "%d ", t.temp[i] / 1000);
6517 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006518 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006519 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006520
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006521 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006522}
6523
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006524static struct ibm_struct thermal_driver_data = {
6525 .name = "thermal",
6526 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006527 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006528};
6529
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006530/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006531 * Backlight/brightness subdriver
6532 */
Holger Macht8acb0252006-10-20 14:30:28 -07006533
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006534#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6535
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006536/*
6537 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6538 * CMOS NVRAM byte 0x5E, bits 0-3.
6539 *
6540 * EC HBRV (0x31) has the following layout
6541 * Bit 7: unknown function
6542 * Bit 6: unknown function
6543 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6544 * Bit 4: must be set to zero to avoid problems
6545 * Bit 3-0: backlight brightness level
6546 *
6547 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006548 *
6549 * WARNING: The X61 has been verified to use HBRV for something else, so
6550 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6551 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006552 */
6553
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006554enum {
6555 TP_EC_BACKLIGHT = 0x31,
6556
6557 /* TP_EC_BACKLIGHT bitmasks */
6558 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6559 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6560 TP_EC_BACKLIGHT_MAPSW = 0x20,
6561};
6562
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006563enum tpacpi_brightness_access_mode {
6564 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6565 TPACPI_BRGHT_MODE_EC, /* EC control */
6566 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6567 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6568 TPACPI_BRGHT_MODE_MAX
6569};
6570
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006571static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006572
6573static enum tpacpi_brightness_access_mode brightness_mode =
6574 TPACPI_BRGHT_MODE_MAX;
6575
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006576static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6577
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006578static struct mutex brightness_mutex;
6579
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006580/* NVRAM brightness access,
6581 * call with brightness_mutex held! */
6582static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006583{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006584 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006585
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006586 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6587 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6588 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006589 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006590
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006591 return lnvram;
6592}
6593
6594static void tpacpi_brightness_checkpoint_nvram(void)
6595{
6596 u8 lec = 0;
6597 u8 b_nvram;
6598
6599 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6600 return;
6601
6602 vdbg_printk(TPACPI_DBG_BRGHT,
6603 "trying to checkpoint backlight level to NVRAM...\n");
6604
6605 if (mutex_lock_killable(&brightness_mutex) < 0)
6606 return;
6607
6608 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6609 goto unlock;
6610 lec &= TP_EC_BACKLIGHT_LVLMSK;
6611 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6612
6613 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6614 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6615 /* NVRAM needs update */
6616 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6617 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6618 b_nvram |= lec;
6619 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6620 dbg_printk(TPACPI_DBG_BRGHT,
6621 "updated NVRAM backlight level to %u (0x%02x)\n",
6622 (unsigned int) lec, (unsigned int) b_nvram);
6623 } else
6624 vdbg_printk(TPACPI_DBG_BRGHT,
6625 "NVRAM backlight level already is %u (0x%02x)\n",
6626 (unsigned int) lec, (unsigned int) b_nvram);
6627
6628unlock:
6629 mutex_unlock(&brightness_mutex);
6630}
6631
6632
6633/* call with brightness_mutex held! */
6634static int tpacpi_brightness_get_raw(int *status)
6635{
6636 u8 lec = 0;
6637
6638 switch (brightness_mode) {
6639 case TPACPI_BRGHT_MODE_UCMS_STEP:
6640 *status = tpacpi_brightness_nvram_get();
6641 return 0;
6642 case TPACPI_BRGHT_MODE_EC:
6643 case TPACPI_BRGHT_MODE_ECNVRAM:
6644 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006645 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006646 *status = lec;
6647 return 0;
6648 default:
6649 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006650 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006651}
6652
6653/* call with brightness_mutex held! */
6654/* do NOT call with illegal backlight level value */
6655static int tpacpi_brightness_set_ec(unsigned int value)
6656{
6657 u8 lec = 0;
6658
6659 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6660 return -EIO;
6661
6662 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6663 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6664 (value & TP_EC_BACKLIGHT_LVLMSK))))
6665 return -EIO;
6666
6667 return 0;
6668}
6669
6670/* call with brightness_mutex held! */
6671static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6672{
6673 int cmos_cmd, inc;
6674 unsigned int current_value, i;
6675
6676 current_value = tpacpi_brightness_nvram_get();
6677
6678 if (value == current_value)
6679 return 0;
6680
6681 cmos_cmd = (value > current_value) ?
6682 TP_CMOS_BRIGHTNESS_UP :
6683 TP_CMOS_BRIGHTNESS_DOWN;
6684 inc = (value > current_value) ? 1 : -1;
6685
6686 for (i = current_value; i != value; i += inc)
6687 if (issue_thinkpad_cmos_command(cmos_cmd))
6688 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006689
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006690 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006691}
6692
6693/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006694static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006695{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006696 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006697
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006698 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006699 return -EINVAL;
6700
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006701 vdbg_printk(TPACPI_DBG_BRGHT,
6702 "set backlight level to %d\n", value);
6703
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006704 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006705 if (res < 0)
6706 return res;
6707
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006708 switch (brightness_mode) {
6709 case TPACPI_BRGHT_MODE_EC:
6710 case TPACPI_BRGHT_MODE_ECNVRAM:
6711 res = tpacpi_brightness_set_ec(value);
6712 break;
6713 case TPACPI_BRGHT_MODE_UCMS_STEP:
6714 res = tpacpi_brightness_set_ucmsstep(value);
6715 break;
6716 default:
6717 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006718 }
6719
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006720 mutex_unlock(&brightness_mutex);
6721 return res;
6722}
6723
6724/* sysfs backlight class ----------------------------------------------- */
6725
6726static int brightness_update_status(struct backlight_device *bd)
6727{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006728 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006729 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6730 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006731 bd->props.brightness : 0;
6732
6733 dbg_printk(TPACPI_DBG_BRGHT,
6734 "backlight: attempt to set level to %d\n",
6735 level);
6736
6737 /* it is the backlight class's job (caller) to handle
6738 * EINTR and other errors properly */
6739 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006740}
Holger Macht8acb0252006-10-20 14:30:28 -07006741
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006742static int brightness_get(struct backlight_device *bd)
6743{
6744 int status, res;
6745
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006746 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006747 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006748 return 0;
6749
6750 res = tpacpi_brightness_get_raw(&status);
6751
6752 mutex_unlock(&brightness_mutex);
6753
6754 if (res < 0)
6755 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006756
6757 return status & TP_EC_BACKLIGHT_LVLMSK;
6758}
6759
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006760static void tpacpi_brightness_notify_change(void)
6761{
6762 backlight_force_update(ibm_backlight_device,
6763 BACKLIGHT_UPDATE_HOTKEY);
6764}
6765
Lionel Debrouxacc24722010-11-16 14:14:02 +01006766static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006767 .get_brightness = brightness_get,
6768 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006769};
6770
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006771/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006772
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006773/*
6774 * Call _BCL method of video device. On some ThinkPads this will
6775 * switch the firmware to the ACPI brightness control mode.
6776 */
6777
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006778static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6779{
6780 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6781 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006782 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006783 int rc;
6784
Aaron Lu46445b62013-10-11 21:27:46 +08006785 if (acpi_bus_get_device(handle, &device))
6786 return 0;
6787
6788 rc = 0;
6789 list_for_each_entry(child, &device->children, node) {
6790 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6791 NULL, &buffer);
6792 if (ACPI_FAILURE(status))
6793 continue;
6794
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006795 obj = (union acpi_object *)buffer.pointer;
6796 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006797 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006798 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006799 rc = 0;
6800 } else {
6801 rc = obj->package.count;
6802 }
Aaron Lu46445b62013-10-11 21:27:46 +08006803 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006804 }
6805
6806 kfree(buffer.pointer);
6807 return rc;
6808}
6809
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006810
6811/*
6812 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6813 */
6814static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6815{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006816 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006817 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006818
Aaron Lu46445b62013-10-11 21:27:46 +08006819 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006820 if (video_device)
6821 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006822
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006823 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006824
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006825 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006826}
6827
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006828/*
6829 * These are only useful for models that have only one possibility
6830 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6831 * these quirks.
6832 */
6833#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6834#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6835#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6836
6837static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6838 /* Models with ATI GPUs known to require ECNVRAM mode */
6839 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6840
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006841 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006842 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006843 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006844 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006845 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6846
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006847 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006848 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006849 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6850 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006851
6852 /* Models with Intel GMA900 */
6853 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6854 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6855 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6856};
6857
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006858/*
6859 * Returns < 0 for error, otherwise sets tp_features.bright_*
6860 * and bright_maxlvl.
6861 */
6862static void __init tpacpi_detect_brightness_capabilities(void)
6863{
6864 unsigned int b;
6865
6866 vdbg_printk(TPACPI_DBG_INIT,
6867 "detecting firmware brightness interface capabilities\n");
6868
6869 /* we could run a quirks check here (same table used by
6870 * brightness_init) if needed */
6871
6872 /*
6873 * We always attempt to detect acpi support, so as to switch
6874 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6875 * going to publish a backlight interface
6876 */
6877 b = tpacpi_check_std_acpi_brightness_support();
6878 switch (b) {
6879 case 16:
6880 bright_maxlvl = 15;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006881 break;
6882 case 8:
6883 case 0:
6884 bright_maxlvl = 7;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006885 break;
6886 default:
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006887 tp_features.bright_unkfw = 1;
6888 bright_maxlvl = b - 1;
6889 }
Eric Curtin15c75622016-01-30 16:55:59 +00006890 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006891}
6892
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006893static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006894{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006895 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006896 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006897 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006898
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006899 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6900
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006901 mutex_init(&brightness_mutex);
6902
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006903 quirks = tpacpi_check_quirks(brightness_quirk_table,
6904 ARRAY_SIZE(brightness_quirk_table));
6905
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006906 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006907
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006908 /* if it is unknown, we don't handle it: it wouldn't be safe */
6909 if (tp_features.bright_unkfw)
6910 return 1;
6911
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006912 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006913 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006914 "brightness support disabled by module parameter\n");
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006915 return 1;
6916 }
6917
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02006918 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006919 if (brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006920 pr_info("Standard ACPI backlight interface available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006921 return 1;
6922 } else if (brightness_enable == 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006923 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 -03006924 return 1;
6925 }
6926 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006927 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006928 }
6929
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006930 /*
6931 * Check for module parameter bogosity, note that we
6932 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6933 * able to detect "unspecified"
6934 */
6935 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006936 return -EINVAL;
6937
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006938 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6939 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6940 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006941 if (quirks & TPACPI_BRGHT_Q_EC)
6942 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6943 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006944 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6945
6946 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006947 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006948 brightness_mode);
6949 }
6950
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006951 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006952 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006953 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6954 brightness_mode == TPACPI_BRGHT_MODE_EC))
6955 return -EINVAL;
6956
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006957 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006958 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006959
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006960 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006961 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006962 props.max_brightness = bright_maxlvl;
6963 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006964 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6965 NULL, NULL,
6966 &ibm_backlight_data,
6967 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006968 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006969 int rc = PTR_ERR(ibm_backlight_device);
6970 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006971 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006972 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006973 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006974 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6975 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006976
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006977 if (quirks & TPACPI_BRGHT_Q_ASK) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006978 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
6979 brightness_mode);
6980 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
6981 TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006982 }
6983
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006984 /* Added by mistake in early 2007. Probably useless, but it could
6985 * be working around some unknown firmware problem where the value
6986 * read at startup doesn't match the real hardware state... so leave
6987 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006988 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006989
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006990 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006991 "brightness: registering brightness hotkeys as change notification\n");
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006992 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6993 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006994 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006995 return 0;
6996}
6997
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006998static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006999{
7000 tpacpi_brightness_checkpoint_nvram();
7001}
7002
7003static void brightness_shutdown(void)
7004{
7005 tpacpi_brightness_checkpoint_nvram();
7006}
7007
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007008static void brightness_exit(void)
7009{
7010 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007011 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007012 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007013 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007014 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007015
7016 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02007017}
7018
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007019static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007020{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007021 int level;
7022
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007023 level = brightness_get(NULL);
7024 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007025 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007026 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007027 seq_printf(m, "level:\t\t%d\n", level);
7028 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007029 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7030 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007031 }
7032
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007033 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007034}
7035
7036static int brightness_write(char *buf)
7037{
7038 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007039 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007040 char *cmd;
7041
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007042 level = brightness_get(NULL);
7043 if (level < 0)
7044 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007045
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007046 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007047 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007048 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007049 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007050 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007051 if (level > 0)
7052 level--;
7053 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007054 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007055 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007056 } else
7057 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007058 }
7059
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007060 tpacpi_disclose_usertask("procfs brightness",
7061 "set level to %d\n", level);
7062
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007063 /*
7064 * Now we know what the final level should be, so we try to set it.
7065 * Doing it this way makes the syscall restartable in case of EINTR
7066 */
7067 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007068 if (!rc && ibm_backlight_device)
7069 backlight_force_update(ibm_backlight_device,
7070 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02007071 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007072}
7073
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007074static struct ibm_struct brightness_driver_data = {
7075 .name = "brightness",
7076 .read = brightness_read,
7077 .write = brightness_write,
7078 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007079 .suspend = brightness_suspend,
7080 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007081};
7082
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007083/*************************************************************************
7084 * Volume subdriver
7085 */
7086
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007087/*
7088 * IBM ThinkPads have a simple volume controller with MUTE gating.
7089 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7090 *
7091 * Since the *61 series (and probably also the later *60 series), Lenovo
7092 * ThinkPads only implement the MUTE gate.
7093 *
7094 * EC register 0x30
7095 * Bit 6: MUTE (1 mutes sound)
7096 * Bit 3-0: Volume
7097 * Other bits should be zero as far as we know.
7098 *
7099 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7100 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7101 * such as bit 7 which is used to detect repeated presses of MUTE,
7102 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007103 *
7104 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7105 * in response to user input. Unfortunately, this rarely works well.
7106 * The laptop changes the state of its internal MUTE gate and, on some
7107 * models, sends KEY_MUTE, causing any user code that responds to the
7108 * mute button to get confused. The hardware MUTE gate is also
7109 * unnecessary, since user code can handle the mute button without
7110 * kernel or EC help.
7111 *
7112 * To avoid confusing userspace, we simply disable all EC-based mute
7113 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007114 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007115
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007116#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7117
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007118#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7119#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7120#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7121
Takashi Iwaicab66612013-10-24 16:06:32 +02007122#if SNDRV_CARDS <= 32
7123#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7124#else
7125#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7126#endif
7127static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007128static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307129static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007130
7131struct tpacpi_alsa_data {
7132 struct snd_card *card;
7133 struct snd_ctl_elem_id *ctl_mute_id;
7134 struct snd_ctl_elem_id *ctl_vol_id;
7135};
7136
7137static struct snd_card *alsa_card;
7138
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007139enum {
7140 TP_EC_AUDIO = 0x30,
7141
7142 /* TP_EC_AUDIO bits */
7143 TP_EC_AUDIO_MUTESW = 6,
7144
7145 /* TP_EC_AUDIO bitmasks */
7146 TP_EC_AUDIO_LVL_MSK = 0x0F,
7147 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7148
7149 /* Maximum volume */
7150 TP_EC_VOLUME_MAX = 14,
7151};
7152
7153enum tpacpi_volume_access_mode {
7154 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7155 TPACPI_VOL_MODE_EC, /* Pure EC control */
7156 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7157 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7158 TPACPI_VOL_MODE_MAX
7159};
7160
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007161enum tpacpi_volume_capabilities {
7162 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7163 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7164 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7165 TPACPI_VOL_CAP_MAX
7166};
7167
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007168enum tpacpi_mute_btn_mode {
7169 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7170 /* We don't know what mode 1 is. */
7171 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7172 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7173};
7174
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007175static enum tpacpi_volume_access_mode volume_mode =
7176 TPACPI_VOL_MODE_MAX;
7177
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007178static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307179static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007180static bool software_mute_requested = true;
7181static bool software_mute_active;
7182static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007183
7184/*
7185 * Used to syncronize writers to TP_EC_AUDIO and
7186 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7187 */
7188static struct mutex volume_mutex;
7189
7190static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007191{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007192 u8 lec = 0;
7193 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007194 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007195
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007196 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7197 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007198 if (!volume_control_allowed)
7199 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007200 if (software_mute_active)
7201 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007202
7203 vdbg_printk(TPACPI_DBG_MIXER,
7204 "trying to checkpoint mixer state to NVRAM...\n");
7205
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007206 if (tp_features.mixer_no_level_control)
7207 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7208 else
7209 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7210
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007211 if (mutex_lock_killable(&volume_mutex) < 0)
7212 return;
7213
7214 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7215 goto unlock;
7216 lec &= ec_mask;
7217 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7218
7219 if (lec != (b_nvram & ec_mask)) {
7220 /* NVRAM needs update */
7221 b_nvram &= ~ec_mask;
7222 b_nvram |= lec;
7223 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7224 dbg_printk(TPACPI_DBG_MIXER,
7225 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7226 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007227 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007228 vdbg_printk(TPACPI_DBG_MIXER,
7229 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7230 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007231 }
7232
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007233unlock:
7234 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007235}
7236
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007237static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007238{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007239 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007240
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007241 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7242 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007243
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007244 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007245
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007246 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007247
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007248 return 0;
7249}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007250
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007251static int volume_get_status(u8 *status)
7252{
7253 return volume_get_status_ec(status);
7254}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007255
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007256static int volume_set_status_ec(const u8 status)
7257{
7258 if (!acpi_ec_write(TP_EC_AUDIO, status))
7259 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007260
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007261 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7262
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007263 /*
7264 * On X200s, and possibly on others, it can take a while for
7265 * reads to become correct.
7266 */
7267 msleep(1);
7268
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007269 return 0;
7270}
7271
7272static int volume_set_status(const u8 status)
7273{
7274 return volume_set_status_ec(status);
7275}
7276
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007277/* returns < 0 on error, 0 on no change, 1 on change */
7278static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007279{
7280 int rc;
7281 u8 s, n;
7282
7283 if (mutex_lock_killable(&volume_mutex) < 0)
7284 return -EINTR;
7285
7286 rc = volume_get_status_ec(&s);
7287 if (rc)
7288 goto unlock;
7289
7290 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7291 s & ~TP_EC_AUDIO_MUTESW_MSK;
7292
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007293 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007294 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007295 if (!rc)
7296 rc = 1;
7297 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007298
7299unlock:
7300 mutex_unlock(&volume_mutex);
7301 return rc;
7302}
7303
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007304static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007305{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007306 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007307 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007308 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007309}
7310
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007311static int volume_set_mute(const bool mute)
7312{
7313 int rc;
7314
7315 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7316 (mute) ? "" : "un");
7317
7318 rc = __volume_set_mute_ec(mute);
7319 return (rc < 0) ? rc : 0;
7320}
7321
7322/* returns < 0 on error, 0 on no change, 1 on change */
7323static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007324{
7325 int rc;
7326 u8 s, n;
7327
7328 if (vol > TP_EC_VOLUME_MAX)
7329 return -EINVAL;
7330
7331 if (mutex_lock_killable(&volume_mutex) < 0)
7332 return -EINTR;
7333
7334 rc = volume_get_status_ec(&s);
7335 if (rc)
7336 goto unlock;
7337
7338 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7339
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007340 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007341 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007342 if (!rc)
7343 rc = 1;
7344 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007345
7346unlock:
7347 mutex_unlock(&volume_mutex);
7348 return rc;
7349}
7350
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007351static int volume_set_software_mute(bool startup)
7352{
7353 int result;
7354
7355 if (!tpacpi_is_lenovo())
7356 return -ENODEV;
7357
7358 if (startup) {
7359 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7360 "HAUM", "qd"))
7361 return -EIO;
7362
7363 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7364 "Initial HAUM setting was %d\n",
7365 software_mute_orig_mode);
7366 }
7367
7368 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7369 (int)TP_EC_MUTE_BTN_NONE))
7370 return -EIO;
7371
7372 if (result != TP_EC_MUTE_BTN_NONE)
7373 pr_warn("Unexpected SAUM result %d\n",
7374 result);
7375
7376 /*
7377 * In software mute mode, the standard codec controls take
7378 * precendence, so we unmute the ThinkPad HW switch at
7379 * startup. Just on case there are SAUM-capable ThinkPads
7380 * with level controls, set max HW volume as well.
7381 */
7382 if (tp_features.mixer_no_level_control)
7383 result = volume_set_mute(false);
7384 else
7385 result = volume_set_status(TP_EC_VOLUME_MAX);
7386
7387 if (result != 0)
7388 pr_warn("Failed to unmute the HW mute switch\n");
7389
7390 return 0;
7391}
7392
7393static void volume_exit_software_mute(void)
7394{
7395 int r;
7396
7397 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7398 || r != software_mute_orig_mode)
7399 pr_warn("Failed to restore mute mode\n");
7400}
7401
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007402static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007403{
7404 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007405 "ALSA: trying to set volume level to %hu\n", vol);
7406 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007407}
7408
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007409static void volume_alsa_notify_change(void)
7410{
7411 struct tpacpi_alsa_data *d;
7412
7413 if (alsa_card && alsa_card->private_data) {
7414 d = alsa_card->private_data;
7415 if (d->ctl_mute_id)
7416 snd_ctl_notify(alsa_card,
7417 SNDRV_CTL_EVENT_MASK_VALUE,
7418 d->ctl_mute_id);
7419 if (d->ctl_vol_id)
7420 snd_ctl_notify(alsa_card,
7421 SNDRV_CTL_EVENT_MASK_VALUE,
7422 d->ctl_vol_id);
7423 }
7424}
7425
7426static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7427 struct snd_ctl_elem_info *uinfo)
7428{
7429 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7430 uinfo->count = 1;
7431 uinfo->value.integer.min = 0;
7432 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7433 return 0;
7434}
7435
7436static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7437 struct snd_ctl_elem_value *ucontrol)
7438{
7439 u8 s;
7440 int rc;
7441
7442 rc = volume_get_status(&s);
7443 if (rc < 0)
7444 return rc;
7445
7446 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7447 return 0;
7448}
7449
7450static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7451 struct snd_ctl_elem_value *ucontrol)
7452{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007453 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7454 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007455 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007456}
7457
7458#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7459
7460static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7461 struct snd_ctl_elem_value *ucontrol)
7462{
7463 u8 s;
7464 int rc;
7465
7466 rc = volume_get_status(&s);
7467 if (rc < 0)
7468 return rc;
7469
7470 ucontrol->value.integer.value[0] =
7471 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7472 return 0;
7473}
7474
7475static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7476 struct snd_ctl_elem_value *ucontrol)
7477{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007478 tpacpi_disclose_usertask("ALSA", "%smute\n",
7479 ucontrol->value.integer.value[0] ?
7480 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007481 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007482}
7483
Mathias Krausec3aa4722014-07-16 19:43:17 +02007484static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007485 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7486 .name = "Console Playback Volume",
7487 .index = 0,
7488 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7489 .info = volume_alsa_vol_info,
7490 .get = volume_alsa_vol_get,
7491};
7492
Mathias Krausec3aa4722014-07-16 19:43:17 +02007493static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007494 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7495 .name = "Console Playback Switch",
7496 .index = 0,
7497 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7498 .info = volume_alsa_mute_info,
7499 .get = volume_alsa_mute_get,
7500};
7501
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007502static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007503{
7504 tpacpi_volume_checkpoint_nvram();
7505}
7506
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007507static void volume_resume(void)
7508{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007509 if (software_mute_active) {
7510 if (volume_set_software_mute(false) < 0)
7511 pr_warn("Failed to restore software mute\n");
7512 } else {
7513 volume_alsa_notify_change();
7514 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007515}
7516
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007517static void volume_shutdown(void)
7518{
7519 tpacpi_volume_checkpoint_nvram();
7520}
7521
7522static void volume_exit(void)
7523{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007524 if (alsa_card) {
7525 snd_card_free(alsa_card);
7526 alsa_card = NULL;
7527 }
7528
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007529 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007530
7531 if (software_mute_active)
7532 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007533}
7534
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007535static int __init volume_create_alsa_mixer(void)
7536{
7537 struct snd_card *card;
7538 struct tpacpi_alsa_data *data;
7539 struct snd_kcontrol *ctl_vol;
7540 struct snd_kcontrol *ctl_mute;
7541 int rc;
7542
Takashi Iwai89235e52014-01-29 15:01:27 +01007543 rc = snd_card_new(&tpacpi_pdev->dev,
7544 alsa_index, alsa_id, THIS_MODULE,
7545 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007546 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007547 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007548 return 1;
7549 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007550
7551 BUG_ON(!card->private_data);
7552 data = card->private_data;
7553 data->card = card;
7554
7555 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7556 sizeof(card->driver));
7557 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7558 sizeof(card->shortname));
7559 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7560 (thinkpad_id.ec_version_str) ?
7561 thinkpad_id.ec_version_str : "(unknown)");
7562 snprintf(card->longname, sizeof(card->longname),
7563 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7564 (thinkpad_id.ec_version_str) ?
7565 thinkpad_id.ec_version_str : "unknown");
7566
7567 if (volume_control_allowed) {
7568 volume_alsa_control_vol.put = volume_alsa_vol_put;
7569 volume_alsa_control_vol.access =
7570 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7571
7572 volume_alsa_control_mute.put = volume_alsa_mute_put;
7573 volume_alsa_control_mute.access =
7574 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7575 }
7576
7577 if (!tp_features.mixer_no_level_control) {
7578 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7579 rc = snd_ctl_add(card, ctl_vol);
7580 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007581 pr_err("Failed to create ALSA volume control: %d\n",
7582 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007583 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007584 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007585 data->ctl_vol_id = &ctl_vol->id;
7586 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007587
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007588 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7589 rc = snd_ctl_add(card, ctl_mute);
7590 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007591 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007592 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007593 }
7594 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007595
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007596 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007597 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007598 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007599 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007600 }
7601
7602 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007603 return 0;
7604
7605err_exit:
7606 snd_card_free(card);
7607 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007608}
7609
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007610#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7611#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7612
7613static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7614 /* Whitelist volume level on all IBM by default */
7615 { .vendor = PCI_VENDOR_ID_IBM,
7616 .bios = TPACPI_MATCH_ANY,
7617 .ec = TPACPI_MATCH_ANY,
7618 .quirks = TPACPI_VOL_Q_LEVEL },
7619
7620 /* Lenovo models with volume control (needs confirmation) */
7621 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7622 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7623 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7624 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7625 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7626 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7627 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7628
7629 /* Whitelist mute-only on all Lenovo by default */
7630 { .vendor = PCI_VENDOR_ID_LENOVO,
7631 .bios = TPACPI_MATCH_ANY,
7632 .ec = TPACPI_MATCH_ANY,
7633 .quirks = TPACPI_VOL_Q_MUTEONLY }
7634};
7635
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007636static int __init volume_init(struct ibm_init_struct *iibm)
7637{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007638 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007639 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007640
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007641 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7642
7643 mutex_init(&volume_mutex);
7644
7645 /*
7646 * Check for module parameter bogosity, note that we
7647 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7648 * able to detect "unspecified"
7649 */
7650 if (volume_mode > TPACPI_VOL_MODE_MAX)
7651 return -EINVAL;
7652
7653 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007654 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7655 TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007656 return 1;
7657 }
7658
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007659 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7660 return -EINVAL;
7661
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007662 /*
7663 * The ALSA mixer is our primary interface.
7664 * When disabled, don't install the subdriver at all
7665 */
7666 if (!alsa_enable) {
7667 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007668 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007669 return 1;
7670 }
7671
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007672 quirks = tpacpi_check_quirks(volume_quirk_table,
7673 ARRAY_SIZE(volume_quirk_table));
7674
7675 switch (volume_capabilities) {
7676 case TPACPI_VOL_CAP_AUTO:
7677 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7678 tp_features.mixer_no_level_control = 1;
7679 else if (quirks & TPACPI_VOL_Q_LEVEL)
7680 tp_features.mixer_no_level_control = 0;
7681 else
7682 return 1; /* no mixer */
7683 break;
7684 case TPACPI_VOL_CAP_VOLMUTE:
7685 tp_features.mixer_no_level_control = 0;
7686 break;
7687 case TPACPI_VOL_CAP_MUTEONLY:
7688 tp_features.mixer_no_level_control = 1;
7689 break;
7690 default:
7691 return 1;
7692 }
7693
7694 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7695 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7696 "using user-supplied volume_capabilities=%d\n",
7697 volume_capabilities);
7698
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007699 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7700 volume_mode == TPACPI_VOL_MODE_MAX) {
7701 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7702
7703 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7704 "driver auto-selected volume_mode=%d\n",
7705 volume_mode);
7706 } else {
7707 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7708 "using user-supplied volume_mode=%d\n",
7709 volume_mode);
7710 }
7711
7712 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007713 "mute is supported, volume control is %s\n",
7714 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007715
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007716 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7717 software_mute_active = true;
7718 } else {
7719 rc = volume_create_alsa_mixer();
7720 if (rc) {
7721 pr_err("Could not create the ALSA mixer interface\n");
7722 return rc;
7723 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007724
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007725 pr_info("Console audio control enabled, mode: %s\n",
7726 (volume_control_allowed) ?
7727 "override (read/write)" :
7728 "monitor (read only)");
7729 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007730
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007731 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7732 "registering volume hotkeys as change notification\n");
7733 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7734 | TP_ACPI_HKEY_VOLUP_MASK
7735 | TP_ACPI_HKEY_VOLDWN_MASK
7736 | TP_ACPI_HKEY_MUTE_MASK);
7737
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007738 return 0;
7739}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007740
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007741static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007742{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007743 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007744
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007745 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007746 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007747 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007748 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007749 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007750 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007751 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007752 status & TP_EC_AUDIO_LVL_MSK);
7753
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007754 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007755 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007756
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007757 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007758 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007759 if (!tp_features.mixer_no_level_control) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007760 seq_printf(m, "commands:\tup, down\n");
7761 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7762 TP_EC_VOLUME_MAX);
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007763 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007764 }
7765 }
7766
7767 return 0;
7768}
7769
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007770static int volume_write(char *buf)
7771{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007772 u8 s;
7773 u8 new_level, new_mute;
7774 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007775 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007776 int rc;
7777
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007778 /*
7779 * We do allow volume control at driver startup, so that the
7780 * user can set initial state through the volume=... parameter hack.
7781 */
7782 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7783 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7784 tp_warned.volume_ctrl_forbidden = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007785 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7786 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007787 }
7788 return -EPERM;
7789 }
7790
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007791 rc = volume_get_status(&s);
7792 if (rc < 0)
7793 return rc;
7794
7795 new_level = s & TP_EC_AUDIO_LVL_MSK;
7796 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007797
7798 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007799 if (!tp_features.mixer_no_level_control) {
7800 if (strlencmp(cmd, "up") == 0) {
7801 if (new_mute)
7802 new_mute = 0;
7803 else if (new_level < TP_EC_VOLUME_MAX)
7804 new_level++;
7805 continue;
7806 } else if (strlencmp(cmd, "down") == 0) {
7807 if (new_mute)
7808 new_mute = 0;
7809 else if (new_level > 0)
7810 new_level--;
7811 continue;
7812 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7813 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7814 new_level = l;
7815 continue;
7816 }
7817 }
7818 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007819 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007820 else if (strlencmp(cmd, "unmute") == 0)
7821 new_mute = 0;
7822 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007823 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007824 }
7825
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007826 if (tp_features.mixer_no_level_control) {
7827 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7828 new_mute ? "" : "un");
7829 rc = volume_set_mute(!!new_mute);
7830 } else {
7831 tpacpi_disclose_usertask("procfs volume",
7832 "%smute and set level to %d\n",
7833 new_mute ? "" : "un", new_level);
7834 rc = volume_set_status(new_mute | new_level);
7835 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007836 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007837
7838 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007839}
7840
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007841static struct ibm_struct volume_driver_data = {
7842 .name = "volume",
7843 .read = volume_read,
7844 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007845 .exit = volume_exit,
7846 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007847 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007848 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007849};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007850
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007851#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7852
7853#define alsa_card NULL
7854
Tobias Klauserbb55a2e2016-11-25 14:18:07 +01007855static inline void volume_alsa_notify_change(void)
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007856{
7857}
7858
7859static int __init volume_init(struct ibm_init_struct *iibm)
7860{
Joe Perches0978e012011-04-04 10:06:25 -07007861 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007862
7863 return 1;
7864}
7865
7866static struct ibm_struct volume_driver_data = {
7867 .name = "volume",
7868};
7869
7870#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7871
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007872/*************************************************************************
7873 * Fan subdriver
7874 */
7875
7876/*
7877 * FAN ACCESS MODES
7878 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007879 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007880 * ACPI GFAN method: returns fan level
7881 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007882 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007883 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007884 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007885 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007886 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7887 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007888 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7889 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007890 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007891 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007892 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7893 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007894 *
7895 * Fan speed changes of any sort (including those caused by the
7896 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007897 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007898 * limited.
7899 *
7900 * Reading is not available if GFAN exists.
7901 * Writing is not available if SFAN exists.
7902 *
7903 * Bits
7904 * 7 automatic mode engaged;
7905 * (default operation mode of the ThinkPad)
7906 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007907 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007908 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007909 * the tachometer while the fan controller ramps up
7910 * the speed (which can take up to a few *minutes*).
7911 * Speeds up fan to 100% duty-cycle, which is far above
7912 * the standard RPM levels. It is not impossible that
7913 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007914 * 5-3 unused in some models. Extra bits for fan level
7915 * in others, but still useless as all values above
7916 * 7 map to the same speed as level 7 in these models.
7917 * 2-0 fan level (0..7 usually)
7918 * 0x00 = stop
7919 * 0x07 = max (set when temperatures critical)
7920 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007921 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007922 *
7923 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007924 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007925 * does so, its initial value is meaningless (0x07).
7926 *
7927 * For firmware bugs, refer to:
7928 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7929 *
7930 * ----
7931 *
7932 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7933 * Main fan tachometer reading (in RPM)
7934 *
7935 * This register is present on all ThinkPads with a new-style EC, and
7936 * it is known not to be present on the A21m/e, and T22, as there is
7937 * something else in offset 0x84 according to the ACPI DSDT. Other
7938 * ThinkPads from this same time period (and earlier) probably lack the
7939 * tachometer as well.
7940 *
Nick Andrew877d0312009-01-26 11:06:57 +01007941 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007942 * was never fixed by IBM to report the EC firmware version string
7943 * probably support the tachometer (like the early X models), so
7944 * detecting it is quite hard. We need more data to know for sure.
7945 *
7946 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7947 * might result.
7948 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007949 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7950 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007951 *
7952 * For firmware bugs, refer to:
7953 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7954 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007955 * ----
7956 *
7957 * ThinkPad EC register 0x31 bit 0 (only on select models)
7958 *
7959 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7960 * show the speed of the main fan. When bit 0 of EC register 0x31
7961 * is one, the tachometer registers show the speed of the auxiliary
7962 * fan.
7963 *
7964 * Fan control seems to affect both fans, regardless of the state
7965 * of this bit.
7966 *
7967 * So far, only the firmware for the X60/X61 non-tablet versions
7968 * seem to support this (firmware TP-7M).
7969 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007970 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007971 * ThinkPad X31, X40, X41. Not available in the X60.
7972 *
7973 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7974 * high speed. ACPI DSDT seems to map these three speeds to levels
7975 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7976 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7977 *
7978 * The speeds are stored on handles
7979 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7980 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007981 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007982 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7983 *
7984 * ACPI DSDT switches which set is in use depending on various
7985 * factors.
7986 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007987 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007988 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7989 * but the ACPI tables just mention level 7.
7990 */
7991
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007992enum { /* Fan control constants */
7993 fan_status_offset = 0x2f, /* EC register 0x2f */
7994 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7995 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007996 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7997 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007998
7999 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
8000 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
8001
8002 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
8003};
8004
8005enum fan_status_access_mode {
8006 TPACPI_FAN_NONE = 0, /* No fan status or control */
8007 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
8008 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8009};
8010
8011enum fan_control_access_mode {
8012 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8013 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8014 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8015 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8016};
8017
8018enum fan_control_commands {
8019 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8020 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8021 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8022 * and also watchdog cmd */
8023};
8024
Rusty Russell90ab5ee2012-01-13 09:32:20 +10308025static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008026
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008027static enum fan_status_access_mode fan_status_access_mode;
8028static enum fan_control_access_mode fan_control_access_mode;
8029static enum fan_control_commands fan_control_commands;
8030
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008031static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008032static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008033static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008034static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008035
8036static struct mutex fan_mutex;
8037
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008038static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05008039static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008040
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008041TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8042TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008043 "\\FSPD", /* 600e/x, 770e, 770x */
8044 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008045TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008046 "JFNS", /* 770x-JL */
8047 ); /* all others */
8048
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008049/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008050 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8051 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8052 * be in auto mode (0x80).
8053 *
8054 * This is corrected by any write to HFSP either by the driver, or
8055 * by the firmware.
8056 *
8057 * We assume 0x07 really means auto mode while this quirk is active,
8058 * as this is far more likely than the ThinkPad being in level 7,
8059 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008060 *
8061 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8062 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008063 */
8064
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008065static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008066{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008067 if (fan_control_initial_status == 0x07) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008068 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 -03008069 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008070 }
8071}
8072
8073static void fan_quirk1_handle(u8 *fan_status)
8074{
8075 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8076 if (*fan_status != fan_control_initial_status) {
8077 /* something changed the HFSP regisnter since
8078 * driver init time, so it is not undefined
8079 * anymore */
8080 tp_features.fan_ctrl_status_undef = 0;
8081 } else {
8082 /* Return most likely status. In fact, it
8083 * might be the only possible status */
8084 *fan_status = TP_EC_FAN_AUTO;
8085 }
8086 }
8087}
8088
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008089/* Select main fan on X60/X61, NOOP on others */
8090static bool fan_select_fan1(void)
8091{
8092 if (tp_features.second_fan) {
8093 u8 val;
8094
8095 if (ec_read(fan_select_offset, &val) < 0)
8096 return false;
8097 val &= 0xFEU;
8098 if (ec_write(fan_select_offset, val) < 0)
8099 return false;
8100 }
8101 return true;
8102}
8103
8104/* Select secondary fan on X60/X61 */
8105static bool fan_select_fan2(void)
8106{
8107 u8 val;
8108
8109 if (!tp_features.second_fan)
8110 return false;
8111
8112 if (ec_read(fan_select_offset, &val) < 0)
8113 return false;
8114 val |= 0x01U;
8115 if (ec_write(fan_select_offset, val) < 0)
8116 return false;
8117
8118 return true;
8119}
8120
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008121/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008122 * Call with fan_mutex held
8123 */
8124static void fan_update_desired_level(u8 status)
8125{
8126 if ((status &
8127 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8128 if (status > 7)
8129 fan_control_desired_level = 7;
8130 else
8131 fan_control_desired_level = status;
8132 }
8133}
8134
8135static int fan_get_status(u8 *status)
8136{
8137 u8 s;
8138
8139 /* TODO:
8140 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8141
8142 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07008143 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008144 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07008145 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008146
Dan Carpentereceeb432012-09-01 12:54:07 -07008147 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008148 return -EIO;
8149
8150 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07008151 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008152
8153 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07008154 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008155 case TPACPI_FAN_RD_TPEC:
8156 /* all except 570, 600e/x, 770e, 770x */
8157 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8158 return -EIO;
8159
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008160 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008161 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008162 fan_quirk1_handle(status);
8163 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008164
8165 break;
8166
8167 default:
8168 return -ENXIO;
8169 }
8170
8171 return 0;
8172}
8173
8174static int fan_get_status_safe(u8 *status)
8175{
8176 int rc;
8177 u8 s;
8178
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008179 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008180 return -ERESTARTSYS;
8181 rc = fan_get_status(&s);
8182 if (!rc)
8183 fan_update_desired_level(s);
8184 mutex_unlock(&fan_mutex);
8185
Dan Carpentera7718362016-04-15 17:46:34 +03008186 if (rc)
8187 return rc;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008188 if (status)
8189 *status = s;
8190
Dan Carpentera7718362016-04-15 17:46:34 +03008191 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008192}
8193
8194static int fan_get_speed(unsigned int *speed)
8195{
8196 u8 hi, lo;
8197
8198 switch (fan_status_access_mode) {
8199 case TPACPI_FAN_RD_TPEC:
8200 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008201 if (unlikely(!fan_select_fan1()))
8202 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008203 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8204 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8205 return -EIO;
8206
8207 if (likely(speed))
8208 *speed = (hi << 8) | lo;
8209
8210 break;
8211
8212 default:
8213 return -ENXIO;
8214 }
8215
8216 return 0;
8217}
8218
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008219static int fan2_get_speed(unsigned int *speed)
8220{
8221 u8 hi, lo;
8222 bool rc;
8223
8224 switch (fan_status_access_mode) {
8225 case TPACPI_FAN_RD_TPEC:
8226 /* all except 570, 600e/x, 770e, 770x */
8227 if (unlikely(!fan_select_fan2()))
8228 return -EIO;
8229 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8230 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8231 fan_select_fan1(); /* play it safe */
8232 if (rc)
8233 return -EIO;
8234
8235 if (likely(speed))
8236 *speed = (hi << 8) | lo;
8237
8238 break;
8239
8240 default:
8241 return -ENXIO;
8242 }
8243
8244 return 0;
8245}
8246
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008247static int fan_set_level(int level)
8248{
8249 if (!fan_control_allowed)
8250 return -EPERM;
8251
8252 switch (fan_control_access_mode) {
8253 case TPACPI_FAN_WR_ACPI_SFAN:
8254 if (level >= 0 && level <= 7) {
8255 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8256 return -EIO;
8257 } else
8258 return -EINVAL;
8259 break;
8260
8261 case TPACPI_FAN_WR_ACPI_FANS:
8262 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008263 if (!(level & TP_EC_FAN_AUTO) &&
8264 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008265 ((level < 0) || (level > 7)))
8266 return -EINVAL;
8267
8268 /* safety net should the EC not support AUTO
8269 * or FULLSPEED mode bits and just ignore them */
8270 if (level & TP_EC_FAN_FULLSPEED)
8271 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01008272 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008273 level |= 4; /* safety min speed 4 */
8274
8275 if (!acpi_ec_write(fan_status_offset, level))
8276 return -EIO;
8277 else
8278 tp_features.fan_ctrl_status_undef = 0;
8279 break;
8280
8281 default:
8282 return -ENXIO;
8283 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008284
8285 vdbg_printk(TPACPI_DBG_FAN,
8286 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008287 return 0;
8288}
8289
8290static int fan_set_level_safe(int level)
8291{
8292 int rc;
8293
8294 if (!fan_control_allowed)
8295 return -EPERM;
8296
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008297 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008298 return -ERESTARTSYS;
8299
8300 if (level == TPACPI_FAN_LAST_LEVEL)
8301 level = fan_control_desired_level;
8302
8303 rc = fan_set_level(level);
8304 if (!rc)
8305 fan_update_desired_level(level);
8306
8307 mutex_unlock(&fan_mutex);
8308 return rc;
8309}
8310
8311static int fan_set_enable(void)
8312{
8313 u8 s;
8314 int rc;
8315
8316 if (!fan_control_allowed)
8317 return -EPERM;
8318
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008319 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008320 return -ERESTARTSYS;
8321
8322 switch (fan_control_access_mode) {
8323 case TPACPI_FAN_WR_ACPI_FANS:
8324 case TPACPI_FAN_WR_TPEC:
8325 rc = fan_get_status(&s);
8326 if (rc < 0)
8327 break;
8328
8329 /* Don't go out of emergency fan mode */
8330 if (s != 7) {
8331 s &= 0x07;
8332 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8333 }
8334
8335 if (!acpi_ec_write(fan_status_offset, s))
8336 rc = -EIO;
8337 else {
8338 tp_features.fan_ctrl_status_undef = 0;
8339 rc = 0;
8340 }
8341 break;
8342
8343 case TPACPI_FAN_WR_ACPI_SFAN:
8344 rc = fan_get_status(&s);
8345 if (rc < 0)
8346 break;
8347
8348 s &= 0x07;
8349
8350 /* Set fan to at least level 4 */
8351 s |= 4;
8352
8353 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008354 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008355 else
8356 rc = 0;
8357 break;
8358
8359 default:
8360 rc = -ENXIO;
8361 }
8362
8363 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008364
8365 if (!rc)
8366 vdbg_printk(TPACPI_DBG_FAN,
8367 "fan control: set fan control register to 0x%02x\n",
8368 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008369 return rc;
8370}
8371
8372static int fan_set_disable(void)
8373{
8374 int rc;
8375
8376 if (!fan_control_allowed)
8377 return -EPERM;
8378
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008379 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008380 return -ERESTARTSYS;
8381
8382 rc = 0;
8383 switch (fan_control_access_mode) {
8384 case TPACPI_FAN_WR_ACPI_FANS:
8385 case TPACPI_FAN_WR_TPEC:
8386 if (!acpi_ec_write(fan_status_offset, 0x00))
8387 rc = -EIO;
8388 else {
8389 fan_control_desired_level = 0;
8390 tp_features.fan_ctrl_status_undef = 0;
8391 }
8392 break;
8393
8394 case TPACPI_FAN_WR_ACPI_SFAN:
8395 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8396 rc = -EIO;
8397 else
8398 fan_control_desired_level = 0;
8399 break;
8400
8401 default:
8402 rc = -ENXIO;
8403 }
8404
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008405 if (!rc)
8406 vdbg_printk(TPACPI_DBG_FAN,
8407 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008408
8409 mutex_unlock(&fan_mutex);
8410 return rc;
8411}
8412
8413static int fan_set_speed(int speed)
8414{
8415 int rc;
8416
8417 if (!fan_control_allowed)
8418 return -EPERM;
8419
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008420 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008421 return -ERESTARTSYS;
8422
8423 rc = 0;
8424 switch (fan_control_access_mode) {
8425 case TPACPI_FAN_WR_ACPI_FANS:
8426 if (speed >= 0 && speed <= 65535) {
8427 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8428 speed, speed, speed))
8429 rc = -EIO;
8430 } else
8431 rc = -EINVAL;
8432 break;
8433
8434 default:
8435 rc = -ENXIO;
8436 }
8437
8438 mutex_unlock(&fan_mutex);
8439 return rc;
8440}
8441
8442static void fan_watchdog_reset(void)
8443{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008444 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8445 return;
8446
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008447 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008448 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8449 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8450 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8451 else
8452 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008453}
8454
8455static void fan_watchdog_fire(struct work_struct *ignored)
8456{
8457 int rc;
8458
8459 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8460 return;
8461
Joe Perches0978e012011-04-04 10:06:25 -07008462 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008463 rc = fan_set_enable();
8464 if (rc < 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008465 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8466 rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008467 /* reschedule for later */
8468 fan_watchdog_reset();
8469 }
8470}
8471
8472/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008473 * SYSFS fan layout: hwmon compatible (device)
8474 *
8475 * pwm*_enable:
8476 * 0: "disengaged" mode
8477 * 1: manual mode
8478 * 2: native EC "auto" mode (recommended, hardware default)
8479 *
8480 * pwm*: set speed in manual mode, ignored otherwise.
8481 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8482 * interpolation.
8483 *
8484 * fan*_input: tachometer reading, RPM
8485 *
8486 *
8487 * SYSFS fan layout: extensions
8488 *
8489 * fan_watchdog (driver):
8490 * fan watchdog interval in seconds, 0 disables (default), max 120
8491 */
8492
8493/* sysfs fan pwm1_enable ----------------------------------------------- */
8494static ssize_t fan_pwm1_enable_show(struct device *dev,
8495 struct device_attribute *attr,
8496 char *buf)
8497{
8498 int res, mode;
8499 u8 status;
8500
8501 res = fan_get_status_safe(&status);
8502 if (res)
8503 return res;
8504
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008505 if (status & TP_EC_FAN_FULLSPEED) {
8506 mode = 0;
8507 } else if (status & TP_EC_FAN_AUTO) {
8508 mode = 2;
8509 } else
8510 mode = 1;
8511
8512 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8513}
8514
8515static ssize_t fan_pwm1_enable_store(struct device *dev,
8516 struct device_attribute *attr,
8517 const char *buf, size_t count)
8518{
8519 unsigned long t;
8520 int res, level;
8521
8522 if (parse_strtoul(buf, 2, &t))
8523 return -EINVAL;
8524
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008525 tpacpi_disclose_usertask("hwmon pwm1_enable",
8526 "set fan mode to %lu\n", t);
8527
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008528 switch (t) {
8529 case 0:
8530 level = TP_EC_FAN_FULLSPEED;
8531 break;
8532 case 1:
8533 level = TPACPI_FAN_LAST_LEVEL;
8534 break;
8535 case 2:
8536 level = TP_EC_FAN_AUTO;
8537 break;
8538 case 3:
8539 /* reserved for software-controlled auto mode */
8540 return -ENOSYS;
8541 default:
8542 return -EINVAL;
8543 }
8544
8545 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008546 if (res == -ENXIO)
8547 return -EINVAL;
8548 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008549 return res;
8550
8551 fan_watchdog_reset();
8552
8553 return count;
8554}
8555
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008556static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8557 fan_pwm1_enable_show, fan_pwm1_enable_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008558
8559/* sysfs fan pwm1 ------------------------------------------------------ */
8560static ssize_t fan_pwm1_show(struct device *dev,
8561 struct device_attribute *attr,
8562 char *buf)
8563{
8564 int res;
8565 u8 status;
8566
8567 res = fan_get_status_safe(&status);
8568 if (res)
8569 return res;
8570
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008571 if ((status &
8572 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8573 status = fan_control_desired_level;
8574
8575 if (status > 7)
8576 status = 7;
8577
8578 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8579}
8580
8581static ssize_t fan_pwm1_store(struct device *dev,
8582 struct device_attribute *attr,
8583 const char *buf, size_t count)
8584{
8585 unsigned long s;
8586 int rc;
8587 u8 status, newlevel;
8588
8589 if (parse_strtoul(buf, 255, &s))
8590 return -EINVAL;
8591
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008592 tpacpi_disclose_usertask("hwmon pwm1",
8593 "set fan speed to %lu\n", s);
8594
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008595 /* scale down from 0-255 to 0-7 */
8596 newlevel = (s >> 5) & 0x07;
8597
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008598 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008599 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008600
8601 rc = fan_get_status(&status);
8602 if (!rc && (status &
8603 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8604 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008605 if (rc == -ENXIO)
8606 rc = -EINVAL;
8607 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008608 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008609 fan_watchdog_reset();
8610 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008611 }
8612
8613 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008614 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008615}
8616
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008617static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008618
8619/* sysfs fan fan1_input ------------------------------------------------ */
8620static ssize_t fan_fan1_input_show(struct device *dev,
8621 struct device_attribute *attr,
8622 char *buf)
8623{
8624 int res;
8625 unsigned int speed;
8626
8627 res = fan_get_speed(&speed);
8628 if (res < 0)
8629 return res;
8630
8631 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8632}
8633
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008634static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008635
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008636/* sysfs fan fan2_input ------------------------------------------------ */
8637static ssize_t fan_fan2_input_show(struct device *dev,
8638 struct device_attribute *attr,
8639 char *buf)
8640{
8641 int res;
8642 unsigned int speed;
8643
8644 res = fan2_get_speed(&speed);
8645 if (res < 0)
8646 return res;
8647
8648 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8649}
8650
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008651static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008652
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008653/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008654static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008655{
8656 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8657}
8658
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008659static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8660 size_t count)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008661{
8662 unsigned long t;
8663
8664 if (parse_strtoul(buf, 120, &t))
8665 return -EINVAL;
8666
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008667 if (!fan_control_allowed)
8668 return -EPERM;
8669
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008670 fan_watchdog_maxinterval = t;
8671 fan_watchdog_reset();
8672
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008673 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8674
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008675 return count;
8676}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008677static DRIVER_ATTR_RW(fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008678
8679/* --------------------------------------------------------------------- */
8680static struct attribute *fan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008681 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8682 &dev_attr_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008683 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008684 NULL
8685};
8686
8687static const struct attribute_group fan_attr_group = {
8688 .attrs = fan_attributes,
8689};
8690
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008691#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8692#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008693
8694#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8695 { .vendor = PCI_VENDOR_ID_IBM, \
8696 .bios = TPACPI_MATCH_ANY, \
8697 .ec = TPID(__id1, __id2), \
8698 .quirks = __quirks }
8699
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008700#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8701 { .vendor = PCI_VENDOR_ID_LENOVO, \
8702 .bios = TPACPI_MATCH_ANY, \
8703 .ec = TPID(__id1, __id2), \
8704 .quirks = __quirks }
8705
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008706static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8707 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8708 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8709 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8710 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008711 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008712};
8713
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008714#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008715#undef TPACPI_FAN_QI
8716
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008717static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008718{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008719 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008720 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008721
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008722 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8723 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008724
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008725 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008726 fan_status_access_mode = TPACPI_FAN_NONE;
8727 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008728 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008729 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008730 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008731 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008732 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008733
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008734 if (tpacpi_is_ibm()) {
8735 TPACPI_ACPIHANDLE_INIT(fans);
8736 TPACPI_ACPIHANDLE_INIT(gfan);
8737 TPACPI_ACPIHANDLE_INIT(sfan);
8738 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008739
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008740 quirks = tpacpi_check_quirks(fan_quirk_table,
8741 ARRAY_SIZE(fan_quirk_table));
8742
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008743 if (gfan_handle) {
8744 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008745 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008746 } else {
8747 /* all other ThinkPads: note that even old-style
8748 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008749 if (likely(acpi_ec_read(fan_status_offset,
8750 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008751 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008752 if (quirks & TPACPI_FAN_Q1)
8753 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008754 if (quirks & TPACPI_FAN_2FAN) {
8755 tp_features.second_fan = 1;
8756 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8757 "secondary fan support enabled\n");
8758 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008759 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008760 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008761 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008762 }
8763 }
8764
8765 if (sfan_handle) {
8766 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008767 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008768 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008769 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008770 } else {
8771 if (!gfan_handle) {
8772 /* gfan without sfan means no fan control */
8773 /* all other models implement TP EC 0x2f control */
8774
8775 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008776 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008777 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008778 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008779 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008780 TPACPI_FAN_CMD_SPEED |
8781 TPACPI_FAN_CMD_LEVEL |
8782 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008783 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008784 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008785 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008786 TPACPI_FAN_CMD_LEVEL |
8787 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008788 }
8789 }
8790 }
8791
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008792 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8793 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008794 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8795 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8796 fan_status_access_mode, fan_control_access_mode);
8797
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008798 /* fan control master switch */
8799 if (!fan_control_allowed) {
8800 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8801 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008802 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008803 "fan control features disabled by parameter\n");
8804 }
8805
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008806 /* update fan_control_desired_level */
8807 if (fan_status_access_mode != TPACPI_FAN_NONE)
8808 fan_get_status_safe(NULL);
8809
8810 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8811 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008812 if (tp_features.second_fan) {
8813 /* attach second fan tachometer */
8814 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008815 &dev_attr_fan2_input.attr;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008816 }
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008817 rc = sysfs_create_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008818 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008819 if (rc < 0)
8820 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008821
8822 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8823 &driver_attr_fan_watchdog);
8824 if (rc < 0) {
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008825 sysfs_remove_group(&tpacpi_hwmon->kobj,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008826 &fan_attr_group);
8827 return rc;
8828 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008829 return 0;
8830 } else
8831 return 1;
8832}
8833
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008834static void fan_exit(void)
8835{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008836 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008837 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008838
8839 /* FIXME: can we really do this unconditionally? */
Stanislav Fomichev6b99e352017-06-20 20:45:13 -07008840 sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008841 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8842 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008843
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008844 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008845 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008846}
8847
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008848static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008849{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008850 int rc;
8851
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008852 if (!fan_control_allowed)
8853 return;
8854
8855 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008856 fan_control_resume_level = 0;
8857 rc = fan_get_status_safe(&fan_control_resume_level);
8858 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008859 pr_notice("failed to read fan level for later restore during resume: %d\n",
8860 rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008861
8862 /* if it is undefined, don't attempt to restore it.
8863 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008864 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008865 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008866}
8867
8868static void fan_resume(void)
8869{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008870 u8 current_level = 7;
8871 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008872 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008873
8874 /* DSDT *always* updates status on resume */
8875 tp_features.fan_ctrl_status_undef = 0;
8876
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008877 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008878 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008879 (fan_get_status_safe(&current_level) < 0))
8880 return;
8881
8882 switch (fan_control_access_mode) {
8883 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008884 /* never decrease fan level */
8885 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008886 break;
8887 case TPACPI_FAN_WR_ACPI_FANS:
8888 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008889 /* never decrease fan level, scale is:
8890 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8891 *
8892 * We expect the firmware to set either 7 or AUTO, but we
8893 * handle FULLSPEED out of paranoia.
8894 *
8895 * So, we can safely only restore FULLSPEED or 7, anything
8896 * else could slow the fan. Restoring AUTO is useless, at
8897 * best that's exactly what the DSDT already set (it is the
8898 * slower it uses).
8899 *
8900 * Always keep in mind that the DSDT *will* have set the
8901 * fans to what the vendor supposes is the best level. We
8902 * muck with it only to speed the fan up.
8903 */
8904 if (fan_control_resume_level != 7 &&
8905 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8906 return;
8907 else
8908 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8909 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008910 break;
8911 default:
8912 return;
8913 }
8914 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008915 pr_notice("restoring fan level to 0x%02x\n",
8916 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008917 rc = fan_set_level_safe(fan_control_resume_level);
8918 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008919 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008920 }
8921}
8922
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008923static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008924{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008925 int rc;
8926 u8 status;
8927 unsigned int speed = 0;
8928
8929 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008930 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008931 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008932 rc = fan_get_status_safe(&status);
8933 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008934 return rc;
8935
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008936 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008937 "level:\t\t%d\n",
8938 (status != 0) ? "enabled" : "disabled", status);
8939 break;
8940
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008941 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008942 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008943 rc = fan_get_status_safe(&status);
8944 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008945 return rc;
8946
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008947 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008948 (status != 0) ? "enabled" : "disabled");
8949
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008950 rc = fan_get_speed(&speed);
8951 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008952 return rc;
8953
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008954 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008955
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008956 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008957 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008958 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008959 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008960 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008961 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008962 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008963 break;
8964
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008965 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008966 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008967 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008968 }
8969
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008970 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008971 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008972
8973 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008974 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008975 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008976 break;
8977
8978 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008979 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008980 break;
8981 }
8982 }
8983
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008984 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008985 seq_printf(m, "commands:\tenable, disable\n"
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008986 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008987
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008988 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008989 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008990
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008991 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008992}
8993
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008994static int fan_write_cmd_level(const char *cmd, int *rc)
8995{
8996 int level;
8997
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008998 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008999 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03009000 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009001 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009002 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02009003 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009004 return 0;
9005
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009006 *rc = fan_set_level_safe(level);
9007 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009008 pr_err("level command accepted for unsupported access mode %d\n",
9009 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009010 else if (!*rc)
9011 tpacpi_disclose_usertask("procfs fan",
9012 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009013
9014 return 1;
9015}
9016
9017static int fan_write_cmd_enable(const char *cmd, int *rc)
9018{
9019 if (strlencmp(cmd, "enable") != 0)
9020 return 0;
9021
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009022 *rc = fan_set_enable();
9023 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009024 pr_err("enable command accepted for unsupported access mode %d\n",
9025 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009026 else if (!*rc)
9027 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009028
9029 return 1;
9030}
9031
9032static int fan_write_cmd_disable(const char *cmd, int *rc)
9033{
9034 if (strlencmp(cmd, "disable") != 0)
9035 return 0;
9036
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009037 *rc = fan_set_disable();
9038 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009039 pr_err("disable command accepted for unsupported access mode %d\n",
9040 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009041 else if (!*rc)
9042 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009043
9044 return 1;
9045}
9046
9047static int fan_write_cmd_speed(const char *cmd, int *rc)
9048{
9049 int speed;
9050
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02009051 /* TODO:
9052 * Support speed <low> <medium> <high> ? */
9053
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009054 if (sscanf(cmd, "speed %d", &speed) != 1)
9055 return 0;
9056
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009057 *rc = fan_set_speed(speed);
9058 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009059 pr_err("speed command accepted for unsupported access mode %d\n",
9060 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009061 else if (!*rc)
9062 tpacpi_disclose_usertask("procfs fan",
9063 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009064
9065 return 1;
9066}
9067
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009068static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9069{
9070 int interval;
9071
9072 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9073 return 0;
9074
9075 if (interval < 0 || interval > 120)
9076 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009077 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009078 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009079 tpacpi_disclose_usertask("procfs fan",
9080 "set watchdog timer to %d\n",
9081 interval);
9082 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009083
9084 return 1;
9085}
9086
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009087static int fan_write(char *buf)
9088{
9089 char *cmd;
9090 int rc = 0;
9091
9092 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009093 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009094 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009095 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009096 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009097 fan_write_cmd_disable(cmd, &rc) ||
9098 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009099 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009100 fan_write_cmd_speed(cmd, &rc))
9101 )
9102 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009103 else if (!rc)
9104 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009105 }
9106
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009107 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009108}
9109
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009110static struct ibm_struct fan_driver_data = {
9111 .name = "fan",
9112 .read = fan_read,
9113 .write = fan_write,
9114 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03009115 .suspend = fan_suspend,
9116 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009117};
9118
David Henningsson420f9732013-10-16 23:10:31 +02009119/*************************************************************************
9120 * Mute LED subdriver
9121 */
9122
9123
9124struct tp_led_table {
9125 acpi_string name;
9126 int on_value;
9127 int off_value;
9128 int state;
9129};
9130
9131static struct tp_led_table led_tables[] = {
9132 [TPACPI_LED_MUTE] = {
9133 .name = "SSMS",
9134 .on_value = 1,
9135 .off_value = 0,
9136 },
9137 [TPACPI_LED_MICMUTE] = {
9138 .name = "MMTS",
9139 .on_value = 2,
9140 .off_value = 0,
9141 },
9142};
9143
9144static int mute_led_on_off(struct tp_led_table *t, bool state)
9145{
9146 acpi_handle temp;
9147 int output;
9148
Axel Lin3526eca2016-09-19 09:33:51 +08009149 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009150 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9151 return -EIO;
9152 }
9153
9154 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9155 state ? t->on_value : t->off_value))
9156 return -EIO;
9157
9158 t->state = state;
9159 return state;
9160}
9161
9162int tpacpi_led_set(int whichled, bool on)
9163{
9164 struct tp_led_table *t;
9165
9166 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9167 return -EINVAL;
9168
9169 t = &led_tables[whichled];
9170 if (t->state < 0 || t->state == on)
9171 return t->state;
9172 return mute_led_on_off(t, on);
9173}
9174EXPORT_SYMBOL_GPL(tpacpi_led_set);
9175
9176static int mute_led_init(struct ibm_init_struct *iibm)
9177{
9178 acpi_handle temp;
9179 int i;
9180
9181 for (i = 0; i < TPACPI_LED_MAX; i++) {
9182 struct tp_led_table *t = &led_tables[i];
9183 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9184 mute_led_on_off(t, false);
9185 else
9186 t->state = -ENODEV;
9187 }
9188 return 0;
9189}
9190
9191static void mute_led_exit(void)
9192{
9193 int i;
9194
9195 for (i = 0; i < TPACPI_LED_MAX; i++)
9196 tpacpi_led_set(i, false);
9197}
9198
Takashi Iwai119f4492014-02-12 16:32:45 +01009199static void mute_led_resume(void)
9200{
9201 int i;
9202
9203 for (i = 0; i < TPACPI_LED_MAX; i++) {
9204 struct tp_led_table *t = &led_tables[i];
9205 if (t->state >= 0)
9206 mute_led_on_off(t, t->state);
9207 }
9208}
9209
David Henningsson420f9732013-10-16 23:10:31 +02009210static struct ibm_struct mute_led_driver_data = {
9211 .name = "mute_led",
9212 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01009213 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02009214};
9215
Ognjen Galic2801b962018-02-07 15:58:44 +01009216/*
9217 * Battery Wear Control Driver
9218 * Contact: Ognjen Galic <smclt30p@gmail.com>
9219 */
9220
9221/* Metadata */
9222
9223#define GET_START "BCTG"
9224#define SET_START "BCCS"
9225#define GET_STOP "BCSG"
9226#define SET_STOP "BCSS"
9227
9228#define START_ATTR "charge_start_threshold"
9229#define STOP_ATTR "charge_stop_threshold"
9230
9231enum {
9232 BAT_ANY = 0,
9233 BAT_PRIMARY = 1,
9234 BAT_SECONDARY = 2
9235};
9236
9237enum {
9238 /* Error condition bit */
9239 METHOD_ERR = BIT(31),
9240};
9241
9242enum {
9243 /* This is used in the get/set helpers */
9244 THRESHOLD_START,
9245 THRESHOLD_STOP,
9246};
9247
9248struct tpacpi_battery_data {
9249 int charge_start;
9250 int start_support;
9251 int charge_stop;
9252 int stop_support;
9253};
9254
9255struct tpacpi_battery_driver_data {
9256 struct tpacpi_battery_data batteries[3];
9257 int individual_addressing;
9258};
9259
9260static struct tpacpi_battery_driver_data battery_info;
9261
9262/* ACPI helpers/functions/probes */
9263
9264/**
9265 * This evaluates a ACPI method call specific to the battery
9266 * ACPI extension. The specifics are that an error is marked
9267 * in the 32rd bit of the response, so we just check that here.
9268 */
9269static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9270{
9271 int response;
9272
9273 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9274 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9275 return AE_ERROR;
9276 }
9277 if (response & METHOD_ERR) {
9278 acpi_handle_err(hkey_handle,
9279 "%s evaluated but flagged as error", method);
9280 return AE_ERROR;
9281 }
9282 *ret = response;
9283 return AE_OK;
9284}
9285
9286static int tpacpi_battery_get(int what, int battery, int *ret)
9287{
9288 switch (what) {
9289 case THRESHOLD_START:
9290 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9291 return -ENODEV;
9292
9293 /* The value is in the low 8 bits of the response */
9294 *ret = *ret & 0xFF;
9295 return 0;
9296 case THRESHOLD_STOP:
9297 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9298 return -ENODEV;
9299 /* Value is in lower 8 bits */
9300 *ret = *ret & 0xFF;
9301 /*
9302 * On the stop value, if we return 0 that
9303 * does not make any sense. 0 means Default, which
9304 * means that charging stops at 100%, so we return
9305 * that.
9306 */
9307 if (*ret == 0)
9308 *ret = 100;
9309 return 0;
9310 default:
9311 pr_crit("wrong parameter: %d", what);
9312 return -EINVAL;
9313 }
9314}
9315
9316static int tpacpi_battery_set(int what, int battery, int value)
9317{
9318 int param, ret;
9319 /* The first 8 bits are the value of the threshold */
9320 param = value;
9321 /* The battery ID is in bits 8-9, 2 bits */
9322 param |= battery << 8;
9323
9324 switch (what) {
9325 case THRESHOLD_START:
9326 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9327 pr_err("failed to set charge threshold on battery %d",
9328 battery);
9329 return -ENODEV;
9330 }
9331 return 0;
9332 case THRESHOLD_STOP:
9333 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9334 pr_err("failed to set stop threshold: %d", battery);
9335 return -ENODEV;
9336 }
9337 return 0;
9338 default:
9339 pr_crit("wrong parameter: %d", what);
9340 return -EINVAL;
9341 }
9342}
9343
9344static int tpacpi_battery_probe(int battery)
9345{
9346 int ret = 0;
9347
9348 memset(&battery_info, 0, sizeof(struct tpacpi_battery_driver_data));
9349 /*
9350 * 1) Get the current start threshold
9351 * 2) Check for support
9352 * 3) Get the current stop threshold
9353 * 4) Check for support
9354 */
9355 if (acpi_has_method(hkey_handle, GET_START)) {
9356 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9357 pr_err("Error probing battery %d\n", battery);
9358 return -ENODEV;
9359 }
9360 /* Individual addressing is in bit 9 */
9361 if (ret & BIT(9))
9362 battery_info.individual_addressing = true;
9363 /* Support is marked in bit 8 */
9364 if (ret & BIT(8))
9365 battery_info.batteries[battery].start_support = 1;
9366 else
9367 return -ENODEV;
9368 if (tpacpi_battery_get(THRESHOLD_START, battery,
9369 &battery_info.batteries[battery].charge_start)) {
9370 pr_err("Error probing battery %d\n", battery);
9371 return -ENODEV;
9372 }
9373 }
9374 if (acpi_has_method(hkey_handle, GET_STOP)) {
9375 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9376 pr_err("Error probing battery stop; %d\n", battery);
9377 return -ENODEV;
9378 }
9379 /* Support is marked in bit 8 */
9380 if (ret & BIT(8))
9381 battery_info.batteries[battery].stop_support = 1;
9382 else
9383 return -ENODEV;
9384 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9385 &battery_info.batteries[battery].charge_stop)) {
9386 pr_err("Error probing battery stop: %d\n", battery);
9387 return -ENODEV;
9388 }
9389 }
9390 pr_info("battery %d registered (start %d, stop %d)",
9391 battery,
9392 battery_info.batteries[battery].charge_start,
9393 battery_info.batteries[battery].charge_stop);
9394
9395 return 0;
9396}
9397
9398/* General helper functions */
9399
9400static int tpacpi_battery_get_id(const char *battery_name)
9401{
9402
9403 if (strcmp(battery_name, "BAT0") == 0)
9404 return BAT_PRIMARY;
9405 if (strcmp(battery_name, "BAT1") == 0)
9406 return BAT_SECONDARY;
9407 /*
9408 * If for some reason the battery is not BAT0 nor is it
9409 * BAT1, we will assume it's the default, first battery,
9410 * AKA primary.
9411 */
9412 pr_warn("unknown battery %s, assuming primary", battery_name);
9413 return BAT_PRIMARY;
9414}
9415
9416/* sysfs interface */
9417
9418static ssize_t tpacpi_battery_store(int what,
9419 struct device *dev,
9420 const char *buf, size_t count)
9421{
9422 struct power_supply *supply = to_power_supply(dev);
9423 unsigned long value;
9424 int battery, rval;
9425 /*
9426 * Some systems have support for more than
9427 * one battery. If that is the case,
9428 * tpacpi_battery_probe marked that addressing
9429 * them individually is supported, so we do that
9430 * based on the device struct.
9431 *
9432 * On systems that are not supported, we assume
9433 * the primary as most of the ACPI calls fail
9434 * with "Any Battery" as the parameter.
9435 */
9436 if (battery_info.individual_addressing)
9437 /* BAT_PRIMARY or BAT_SECONDARY */
9438 battery = tpacpi_battery_get_id(supply->desc->name);
9439 else
9440 battery = BAT_PRIMARY;
9441
9442 rval = kstrtoul(buf, 10, &value);
9443 if (rval)
9444 return rval;
9445
9446 switch (what) {
9447 case THRESHOLD_START:
9448 if (!battery_info.batteries[battery].start_support)
9449 return -ENODEV;
9450 /* valid values are [0, 99] */
9451 if (value < 0 || value > 99)
9452 return -EINVAL;
9453 if (value > battery_info.batteries[battery].charge_stop)
9454 return -EINVAL;
9455 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9456 return -ENODEV;
9457 battery_info.batteries[battery].charge_start = value;
9458 return count;
9459
9460 case THRESHOLD_STOP:
9461 if (!battery_info.batteries[battery].stop_support)
9462 return -ENODEV;
9463 /* valid values are [1, 100] */
9464 if (value < 1 || value > 100)
9465 return -EINVAL;
9466 if (value < battery_info.batteries[battery].charge_start)
9467 return -EINVAL;
9468 battery_info.batteries[battery].charge_stop = value;
9469 /*
9470 * When 100 is passed to stop, we need to flip
9471 * it to 0 as that the EC understands that as
9472 * "Default", which will charge to 100%
9473 */
9474 if (value == 100)
9475 value = 0;
9476 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9477 return -EINVAL;
9478 return count;
9479 default:
9480 pr_crit("Wrong parameter: %d", what);
9481 return -EINVAL;
9482 }
9483 return count;
9484}
9485
9486static ssize_t tpacpi_battery_show(int what,
9487 struct device *dev,
9488 char *buf)
9489{
9490 struct power_supply *supply = to_power_supply(dev);
9491 int ret, battery;
9492 /*
9493 * Some systems have support for more than
9494 * one battery. If that is the case,
9495 * tpacpi_battery_probe marked that addressing
9496 * them individually is supported, so we;
9497 * based on the device struct.
9498 *
9499 * On systems that are not supported, we assume
9500 * the primary as most of the ACPI calls fail
9501 * with "Any Battery" as the parameter.
9502 */
9503 if (battery_info.individual_addressing)
9504 /* BAT_PRIMARY or BAT_SECONDARY */
9505 battery = tpacpi_battery_get_id(supply->desc->name);
9506 else
9507 battery = BAT_PRIMARY;
9508 if (tpacpi_battery_get(what, battery, &ret))
9509 return -ENODEV;
9510 return sprintf(buf, "%d\n", ret);
9511}
9512
9513static ssize_t charge_start_threshold_show(struct device *device,
9514 struct device_attribute *attr,
9515 char *buf)
9516{
9517 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9518}
9519
9520static ssize_t charge_stop_threshold_show(struct device *device,
9521 struct device_attribute *attr,
9522 char *buf)
9523{
9524 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9525}
9526
9527static ssize_t charge_start_threshold_store(struct device *dev,
9528 struct device_attribute *attr,
9529 const char *buf, size_t count)
9530{
9531 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9532}
9533
9534static ssize_t charge_stop_threshold_store(struct device *dev,
9535 struct device_attribute *attr,
9536 const char *buf, size_t count)
9537{
9538 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9539}
9540
9541static DEVICE_ATTR_RW(charge_start_threshold);
9542static DEVICE_ATTR_RW(charge_stop_threshold);
9543
9544static struct attribute *tpacpi_battery_attrs[] = {
9545 &dev_attr_charge_start_threshold.attr,
9546 &dev_attr_charge_stop_threshold.attr,
9547 NULL,
9548};
9549
9550ATTRIBUTE_GROUPS(tpacpi_battery);
9551
9552/* ACPI battery hooking */
9553
9554static int tpacpi_battery_add(struct power_supply *battery)
9555{
9556 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9557
9558 if (tpacpi_battery_probe(batteryid))
9559 return -ENODEV;
9560 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9561 return -ENODEV;
9562 return 0;
9563}
9564
9565static int tpacpi_battery_remove(struct power_supply *battery)
9566{
9567 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9568 return 0;
9569}
9570
9571static struct acpi_battery_hook battery_hook = {
9572 .add_battery = tpacpi_battery_add,
9573 .remove_battery = tpacpi_battery_remove,
9574 .name = "ThinkPad Battery Extension",
9575};
9576
9577/* Subdriver init/exit */
9578
9579static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9580{
9581 battery_hook_register(&battery_hook);
9582 return 0;
9583}
9584
9585static void tpacpi_battery_exit(void)
9586{
9587 battery_hook_unregister(&battery_hook);
9588}
9589
9590static struct ibm_struct battery_driver_data = {
9591 .name = "battery",
9592 .exit = tpacpi_battery_exit,
9593};
9594
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009595/****************************************************************************
9596 ****************************************************************************
9597 *
9598 * Infrastructure
9599 *
9600 ****************************************************************************
9601 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07009602
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009603/*
9604 * HKEY event callout for other subdrivers go here
9605 * (yes, it is ugly, but it is quick, safe, and gets the job done
9606 */
9607static void tpacpi_driver_event(const unsigned int hkey_event)
9608{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00009609 if (ibm_backlight_device) {
9610 switch (hkey_event) {
9611 case TP_HKEY_EV_BRGHT_UP:
9612 case TP_HKEY_EV_BRGHT_DOWN:
9613 tpacpi_brightness_notify_change();
9614 }
9615 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009616 if (alsa_card) {
9617 switch (hkey_event) {
9618 case TP_HKEY_EV_VOL_UP:
9619 case TP_HKEY_EV_VOL_DOWN:
9620 case TP_HKEY_EV_VOL_MUTE:
9621 volume_alsa_notify_change();
9622 }
9623 }
Hans de Goedec685e202017-02-09 16:44:13 +01009624 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9625 enum led_brightness brightness;
9626
9627 mutex_lock(&kbdlight_mutex);
9628
9629 /*
9630 * Check the brightness actually changed, setting the brightness
9631 * through kbdlight_set_level() also triggers this event.
9632 */
9633 brightness = kbdlight_sysfs_get(NULL);
9634 if (kbdlight_brightness != brightness) {
9635 kbdlight_brightness = brightness;
9636 led_classdev_notify_brightness_hw_changed(
9637 &tpacpi_led_kbdlight.led_classdev, brightness);
9638 }
9639
9640 mutex_unlock(&kbdlight_mutex);
9641 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009642}
9643
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009644static void hotkey_driver_event(const unsigned int scancode)
9645{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03009646 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009647}
9648
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009649/* --------------------------------------------------------------------- */
9650
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009651/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03009652static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009653
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009654/*
9655 * Module and infrastructure proble, init and exit handling
9656 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009657
Rusty Russell90ab5ee2012-01-13 09:32:20 +10309658static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009659
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009660#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009661static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009662{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009663 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009664
Jan van den Berg72a979f2014-09-17 00:01:08 +02009665 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009666}
9667#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9668
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009669static void ibm_exit(struct ibm_struct *ibm)
9670{
9671 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9672
9673 list_del_init(&ibm->all_drivers);
9674
9675 if (ibm->flags.acpi_notify_installed) {
9676 dbg_printk(TPACPI_DBG_EXIT,
9677 "%s: acpi_remove_notify_handler\n", ibm->name);
9678 BUG_ON(!ibm->acpi);
9679 acpi_remove_notify_handler(*ibm->acpi->handle,
9680 ibm->acpi->type,
9681 dispatch_acpi_notify);
9682 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009683 }
9684
9685 if (ibm->flags.proc_created) {
9686 dbg_printk(TPACPI_DBG_EXIT,
9687 "%s: remove_proc_entry\n", ibm->name);
9688 remove_proc_entry(ibm->name, proc_dir);
9689 ibm->flags.proc_created = 0;
9690 }
9691
9692 if (ibm->flags.acpi_driver_registered) {
9693 dbg_printk(TPACPI_DBG_EXIT,
9694 "%s: acpi_bus_unregister_driver\n", ibm->name);
9695 BUG_ON(!ibm->acpi);
9696 acpi_bus_unregister_driver(ibm->acpi->driver);
9697 kfree(ibm->acpi->driver);
9698 ibm->acpi->driver = NULL;
9699 ibm->flags.acpi_driver_registered = 0;
9700 }
9701
9702 if (ibm->flags.init_called && ibm->exit) {
9703 ibm->exit();
9704 ibm->flags.init_called = 0;
9705 }
9706
9707 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9708}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009709
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009710static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009711{
9712 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009713 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009714 struct proc_dir_entry *entry;
9715
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009716 BUG_ON(ibm == NULL);
9717
9718 INIT_LIST_HEAD(&ibm->all_drivers);
9719
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009720 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009721 return 0;
9722
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009723 dbg_printk(TPACPI_DBG_INIT,
9724 "probing for %s\n", ibm->name);
9725
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009726 if (iibm->init) {
9727 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009728 if (ret > 0)
9729 return 0; /* probe failed */
9730 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009731 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009732
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009733 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009734 }
9735
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009736 if (ibm->acpi) {
9737 if (ibm->acpi->hid) {
9738 ret = register_tpacpi_subdriver(ibm);
9739 if (ret)
9740 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009741 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009742
9743 if (ibm->acpi->notify) {
9744 ret = setup_acpi_notify(ibm);
9745 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07009746 pr_notice("disabling subdriver %s\n",
9747 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009748 ret = 0;
9749 goto err_out;
9750 }
9751 if (ret < 0)
9752 goto err_out;
9753 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009754 }
9755
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009756 dbg_printk(TPACPI_DBG_INIT,
9757 "%s installed\n", ibm->name);
9758
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009759 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04009760 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009761
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009762 if (!mode)
9763 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009764 if (ibm->write)
9765 mode |= S_IWUSR;
9766 entry = proc_create_data(ibm->name, mode, proc_dir,
9767 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009768 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07009769 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009770 ret = -ENODEV;
9771 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009772 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009773 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009775
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009776 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9777
Linus Torvalds1da177e2005-04-16 15:20:36 -07009778 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009779
9780err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009781 dbg_printk(TPACPI_DBG_INIT,
9782 "%s: at error exit path with result %d\n",
9783 ibm->name, ret);
9784
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009785 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +02009786 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009787}
9788
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009789/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009791static bool __pure __init tpacpi_is_fw_digit(const char c)
9792{
9793 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9794}
9795
Jan van den Berg72a979f2014-09-17 00:01:08 +02009796static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009797 const char t)
9798{
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009799 /*
9800 * Most models: xxyTkkWW (#.##c)
9801 * Ancient 570/600 and -SL lacks (#.##c)
9802 */
9803 if (s && strlen(s) >= 8 &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009804 tpacpi_is_fw_digit(s[0]) &&
9805 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009806 s[2] == t &&
9807 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009808 tpacpi_is_fw_digit(s[4]) &&
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009809 tpacpi_is_fw_digit(s[5]))
9810 return true;
9811
9812 /* New models: xxxyTkkW (#.##c); T550 and some others */
9813 return s && strlen(s) >= 8 &&
9814 tpacpi_is_fw_digit(s[0]) &&
9815 tpacpi_is_fw_digit(s[1]) &&
9816 tpacpi_is_fw_digit(s[2]) &&
9817 s[3] == t &&
9818 (s[4] == 'T' || s[4] == 'N') &&
9819 tpacpi_is_fw_digit(s[5]) &&
9820 tpacpi_is_fw_digit(s[6]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009821}
9822
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009823/* returns 0 - probe ok, or < 0 - probe error.
9824 * Probe ok doesn't mean thinkpad found.
9825 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9826static int __must_check __init get_thinkpad_model_data(
9827 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009828{
Jeff Garzik18552562007-10-03 15:15:40 -04009829 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009830 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009831 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009832
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009833 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009834 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009835
9836 memset(tp, 0, sizeof(*tp));
9837
9838 if (dmi_name_in_vendors("IBM"))
9839 tp->vendor = PCI_VENDOR_ID_IBM;
9840 else if (dmi_name_in_vendors("LENOVO"))
9841 tp->vendor = PCI_VENDOR_ID_LENOVO;
9842 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009843 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009844
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009845 s = dmi_get_system_info(DMI_BIOS_VERSION);
9846 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9847 if (s && !tp->bios_version_str)
9848 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009849
9850 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009851 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9852 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009853 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009854
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009855 tp->bios_model = tp->bios_version_str[0]
9856 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009857 tp->bios_release = (tp->bios_version_str[4] << 8)
9858 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009859
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009860 /*
9861 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9862 * X32 or newer, all Z series; Some models must have an
9863 * up-to-date BIOS or they will not be detected.
9864 *
9865 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9866 */
9867 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009868 if (sscanf(dev->name,
9869 "IBM ThinkPad Embedded Controller -[%17c",
9870 ec_fw_string) == 1) {
9871 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9872 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009873
9874 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009875 if (!tp->ec_version_str)
9876 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009877
9878 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9879 tp->ec_model = ec_fw_string[0]
9880 | (ec_fw_string[1] << 8);
9881 tp->ec_release = (ec_fw_string[4] << 8)
9882 | ec_fw_string[5];
9883 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009884 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
Joe Perches0978e012011-04-04 10:06:25 -07009885 ec_fw_string);
9886 pr_notice("please report this to %s\n",
9887 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009888 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009889 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009890 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009891 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009892
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009893 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009894 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009895 tp->model_str = kstrdup(s, GFP_KERNEL);
9896 if (!tp->model_str)
9897 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009898 } else {
9899 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009900 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009901 tp->model_str = kstrdup(s, GFP_KERNEL);
9902 if (!tp->model_str)
9903 return -ENOMEM;
9904 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009905 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03009906
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009907 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9908 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9909 if (s && !tp->nummodel_str)
9910 return -ENOMEM;
9911
9912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009913}
9914
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009915static int __init probe_for_thinkpad(void)
9916{
9917 int is_thinkpad;
9918
9919 if (acpi_disabled)
9920 return -ENODEV;
9921
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009922 /* It would be dangerous to run the driver in this case */
9923 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9924 return -ENODEV;
9925
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009926 /*
9927 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009928 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009929 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009930 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9931 (thinkpad_id.ec_model != 0) ||
9932 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009933
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03009934 /* The EC handler is required */
9935 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009936 if (!ec_handle) {
9937 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07009938 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009939 return -ENODEV;
9940 }
9941
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009942 if (!is_thinkpad && !force_load)
9943 return -ENODEV;
9944
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009945 return 0;
9946}
9947
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009948static void __init thinkpad_acpi_init_banner(void)
9949{
Joe Perches0978e012011-04-04 10:06:25 -07009950 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9951 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009952
Joe Perches0978e012011-04-04 10:06:25 -07009953 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009954 (thinkpad_id.bios_version_str) ?
9955 thinkpad_id.bios_version_str : "unknown",
9956 (thinkpad_id.ec_version_str) ?
9957 thinkpad_id.ec_version_str : "unknown");
9958
9959 BUG_ON(!thinkpad_id.vendor);
9960
9961 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07009962 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009963 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9964 "IBM" : ((thinkpad_id.vendor ==
9965 PCI_VENDOR_ID_LENOVO) ?
9966 "Lenovo" : "Unknown vendor"),
9967 thinkpad_id.model_str,
9968 (thinkpad_id.nummodel_str) ?
9969 thinkpad_id.nummodel_str : "unknown");
9970}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009971
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009972/* Module init, exit, parameters */
9973
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009974static struct ibm_init_struct ibms_init[] __initdata = {
9975 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009976 .data = &thinkpad_acpi_driver_data,
9977 },
9978 {
9979 .init = hotkey_init,
9980 .data = &hotkey_driver_data,
9981 },
9982 {
9983 .init = bluetooth_init,
9984 .data = &bluetooth_driver_data,
9985 },
9986 {
9987 .init = wan_init,
9988 .data = &wan_driver_data,
9989 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009990 {
9991 .init = uwb_init,
9992 .data = &uwb_driver_data,
9993 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009994#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009995 {
9996 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009997 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009998 .data = &video_driver_data,
9999 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -020010000#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010001 {
Pali Rohárbb28f3d52015-12-30 23:27:41 +010010002 .init = kbdlight_init,
10003 .data = &kbdlight_driver_data,
10004 },
10005 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010006 .init = light_init,
10007 .data = &light_driver_data,
10008 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010009 {
10010 .init = cmos_init,
10011 .data = &cmos_driver_data,
10012 },
10013 {
10014 .init = led_init,
10015 .data = &led_driver_data,
10016 },
10017 {
10018 .init = beep_init,
10019 .data = &beep_driver_data,
10020 },
10021 {
10022 .init = thermal_init,
10023 .data = &thermal_driver_data,
10024 },
10025 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010026 .init = brightness_init,
10027 .data = &brightness_driver_data,
10028 },
10029 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020010030 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010031 .data = &volume_driver_data,
10032 },
10033 {
10034 .init = fan_init,
10035 .data = &fan_driver_data,
10036 },
David Henningsson420f9732013-10-16 23:10:31 +020010037 {
10038 .init = mute_led_init,
10039 .data = &mute_led_driver_data,
10040 },
Ognjen Galic2801b962018-02-07 15:58:44 +010010041 {
10042 .init = tpacpi_battery_init,
10043 .data = &battery_driver_data,
10044 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010045};
10046
Kees Cooke4dca7b2017-10-17 19:04:42 -070010047static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010048{
10049 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010050 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010051
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -020010052 if (!kp || !kp->name || !val)
10053 return -EINVAL;
10054
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010055 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10056 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -020010057 WARN_ON(ibm == NULL);
10058
10059 if (!ibm || !ibm->name)
10060 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010061
10062 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
10063 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010064 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010065 strcpy(ibms_init[i].param, val);
10066 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010067 return 0;
10068 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010069 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010070
10071 return -EINVAL;
10072}
10073
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010074module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010075MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010076 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010077
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -030010078module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010079MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -030010080
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010081module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010082MODULE_PARM_DESC(force_load,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010083 "Attempts to load the driver even on a mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -030010084
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010085module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010086MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010087 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -030010088
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010089module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010090MODULE_PARM_DESC(brightness_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010091 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -030010092
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010093module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010094MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010095 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -020010096
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -020010097#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020010098module_param_named(volume_mode, volume_mode, uint, 0444);
10099MODULE_PARM_DESC(volume_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010100 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -020010101
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020010102module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
10103MODULE_PARM_DESC(volume_capabilities,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010104 "Selects the mixer capabilites: 0=auto, 1=volume and mute, 2=mute only");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020010105
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -020010106module_param_named(volume_control, volume_control_allowed, bool, 0444);
10107MODULE_PARM_DESC(volume_control,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010108 "Enables software override for the console audio control when true");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -020010109
Andy Lutomirski9a417ec2014-10-17 17:04:29 -070010110module_param_named(software_mute, software_mute_requested, bool, 0444);
10111MODULE_PARM_DESC(software_mute,
10112 "Request full software mute control");
10113
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020010114/* ALSA module API parameters */
10115module_param_named(index, alsa_index, int, 0444);
10116MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
10117module_param_named(id, alsa_id, charp, 0444);
10118MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
10119module_param_named(enable, alsa_enable, bool, 0444);
10120MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -020010121#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020010122
Andy Shevchenko9b4bbbd2017-05-09 17:17:21 +030010123/* The module parameter can't be read back, that's why 0 is used here */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010124#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010125 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Andy Shevchenko5cac62a2017-05-09 17:17:20 +030010126 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010127
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010128TPACPI_PARAM(hotkey);
10129TPACPI_PARAM(bluetooth);
10130TPACPI_PARAM(video);
10131TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010132TPACPI_PARAM(cmos);
10133TPACPI_PARAM(led);
10134TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010135TPACPI_PARAM(brightness);
10136TPACPI_PARAM(volume);
10137TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030010138
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010139#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010140module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010141MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
10142module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
10143MODULE_PARM_DESC(wlsw_state,
10144 "Initial state of the emulated WLSW switch");
10145
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010146module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010147MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
10148module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
10149MODULE_PARM_DESC(bluetooth_state,
10150 "Initial state of the emulated bluetooth switch");
10151
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010152module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010153MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
10154module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
10155MODULE_PARM_DESC(wwan_state,
10156 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020010157
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +000010158module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -020010159MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
10160module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
10161MODULE_PARM_DESC(uwb_state,
10162 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -020010163#endif
10164
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010165static void thinkpad_acpi_module_exit(void)
10166{
10167 struct ibm_struct *ibm, *itmp;
10168
10169 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
10170
10171 list_for_each_entry_safe_reverse(ibm, itmp,
10172 &tpacpi_all_drivers,
10173 all_drivers) {
10174 ibm_exit(ibm);
10175 }
10176
10177 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
10178
10179 if (tpacpi_inputdev) {
10180 if (tp_features.input_device_registered)
10181 input_unregister_device(tpacpi_inputdev);
10182 else
10183 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +100010184 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010185 }
10186
10187 if (tpacpi_hwmon)
10188 hwmon_device_unregister(tpacpi_hwmon);
10189
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010190 if (tpacpi_sensors_pdev)
10191 platform_device_unregister(tpacpi_sensors_pdev);
10192 if (tpacpi_pdev)
10193 platform_device_unregister(tpacpi_pdev);
10194
10195 if (tp_features.sensors_pdrv_attrs_registered)
10196 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
10197 if (tp_features.platform_drv_attrs_registered)
10198 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
10199
10200 if (tp_features.sensors_pdrv_registered)
10201 platform_driver_unregister(&tpacpi_hwmon_pdriver);
10202
10203 if (tp_features.platform_drv_registered)
10204 platform_driver_unregister(&tpacpi_pdriver);
10205
10206 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010207 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010208
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -030010209 if (tpacpi_wq)
10210 destroy_workqueue(tpacpi_wq);
10211
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010212 kfree(thinkpad_id.bios_version_str);
10213 kfree(thinkpad_id.ec_version_str);
10214 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +100010215 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -020010216}
10217
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -020010218
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010219static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010220{
10221 int ret, i;
10222
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -030010223 tpacpi_lifecycle = TPACPI_LIFE_INIT;
10224
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010225 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010226
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010227 ret = get_thinkpad_model_data(&thinkpad_id);
10228 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010229 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -030010230 thinkpad_acpi_module_exit();
10231 return ret;
10232 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010233 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010234 if (ret) {
10235 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -030010236 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010238
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010239 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -030010240
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -030010241 thinkpad_acpi_init_banner();
10242 tpacpi_check_outdated_fw();
10243
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010244 TPACPI_ACPIHANDLE_INIT(ecrd);
10245 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010246
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -030010247 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
10248 if (!tpacpi_wq) {
10249 thinkpad_acpi_module_exit();
10250 return -ENOMEM;
10251 }
10252
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010253 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010254 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -070010255 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010256 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010257 return -ENODEV;
10258 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -040010259
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010260 ret = platform_driver_register(&tpacpi_pdriver);
10261 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010262 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010263 thinkpad_acpi_module_exit();
10264 return ret;
10265 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -030010266 tp_features.platform_drv_registered = 1;
10267
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010268 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
10269 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010270 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010271 thinkpad_acpi_module_exit();
10272 return ret;
10273 }
10274 tp_features.sensors_pdrv_registered = 1;
10275
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010276 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -030010277 if (!ret) {
10278 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010279 ret = tpacpi_create_driver_attributes(
10280 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -030010281 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010282 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -070010283 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010284 thinkpad_acpi_module_exit();
10285 return ret;
10286 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -030010287 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030010288
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010289
10290 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010291 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010292 NULL, 0);
10293 if (IS_ERR(tpacpi_pdev)) {
10294 ret = PTR_ERR(tpacpi_pdev);
10295 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070010296 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010297 thinkpad_acpi_module_exit();
10298 return ret;
10299 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010300 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -020010301 TPACPI_HWMON_DRVR_NAME,
10302 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010303 if (IS_ERR(tpacpi_sensors_pdev)) {
10304 ret = PTR_ERR(tpacpi_sensors_pdev);
10305 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070010306 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010307 thinkpad_acpi_module_exit();
10308 return ret;
10309 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030010310 tp_features.sensors_pdev_attrs_registered = 1;
Stanislav Fomichev6b99e352017-06-20 20:45:13 -070010311 tpacpi_hwmon = hwmon_device_register_with_groups(
10312 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
10313
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010314 if (IS_ERR(tpacpi_hwmon)) {
10315 ret = PTR_ERR(tpacpi_hwmon);
10316 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -070010317 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -030010318 thinkpad_acpi_module_exit();
10319 return ret;
10320 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -030010321 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010322 tpacpi_inputdev = input_allocate_device();
10323 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010324 thinkpad_acpi_module_exit();
10325 return -ENOMEM;
10326 } else {
10327 /* Prepare input device, but don't register */
10328 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010329 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010330 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -030010331 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010332 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
10333 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +000010334 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010335 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -030010336
10337 /* Init subdriver dependencies */
10338 tpacpi_detect_brightness_capabilities();
10339
10340 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -030010341 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10342 ret = ibm_init(&ibms_init[i]);
10343 if (ret >= 0 && *ibms_init[i].param)
10344 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010345 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010346 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -070010347 return ret;
10348 }
10349 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -030010350
10351 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
10352
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010353 ret = input_register_device(tpacpi_inputdev);
10354 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -070010355 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -030010356 thinkpad_acpi_module_exit();
10357 return ret;
10358 } else {
10359 tp_features.input_device_registered = 1;
10360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010361
10362 return 0;
10363}
10364
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -030010365MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
10366
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010367/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +000010368 * This will autoload the driver in almost every ThinkPad
10369 * in widespread use.
10370 *
10371 * Only _VERY_ old models, like the 240, 240x and 570 lack
10372 * the HKEY event interface.
10373 */
10374MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
10375
10376/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010377 * DMI matching for module autoloading
10378 *
10379 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
10380 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
10381 *
10382 * Only models listed in thinkwiki will be supported, so add yours
10383 * if it is not there yet.
10384 */
10385#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +010010386 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010387
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010388/* Ancient thinkpad BIOSes have to be identified by
10389 * BIOS type or model number, and there are far less
10390 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +000010391IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010392
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +000010393MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
10394MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020010395MODULE_DESCRIPTION(TPACPI_DESC);
10396MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -020010397MODULE_LICENSE("GPL");
10398
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010399module_init(thinkpad_acpi_module_init);
10400module_exit(thinkpad_acpi_module_exit);