blob: b22573131e53806c9e6064c7233d193f586e3ed4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Joe Perches0978e012011-04-04 10:06:25 -070024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
David Henningsson420f9732013-10-16 23:10:31 +020026#define TPACPI_VERSION "0.25"
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020027#define TPACPI_SYSFS_VERSION 0x020700
Borislav Deianov78f81cc2005-08-17 00:00:00 -040028
29/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020031 * 2007-10-20 changelog trimmed down
32 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030033 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020035 *
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030039 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040040 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020042 *
43 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040044 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
51
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020052#include <linux/kernel.h>
53#include <linux/module.h>
54#include <linux/init.h>
55#include <linux/types.h>
56#include <linux/string.h>
57#include <linux/list.h>
58#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000059#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020060#include <linux/kthread.h>
61#include <linux/freezer.h>
62#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020064#include <linux/nvram.h>
65#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020066#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020067#include <linux/sysfs.h>
68#include <linux/backlight.h>
69#include <linux/fb.h>
70#include <linux/platform_device.h>
71#include <linux/hwmon.h>
72#include <linux/hwmon-sysfs.h>
73#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030074#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030075#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020076#include <linux/dmi.h>
77#include <linux/jiffies.h>
78#include <linux/workqueue.h>
Lv Zheng8b484632013-12-03 08:49:16 +080079#include <linux/acpi.h>
80#include <linux/pci_ids.h>
81#include <linux/thinkpad_acpi.h>
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020082#include <sound/core.h>
83#include <sound/control.h>
84#include <sound/initval.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080085#include <linux/uaccess.h>
Hans de Goedeb33c6ce2015-06-16 16:28:10 +020086#include <acpi/video.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020087
88/* ThinkPad CMOS commands */
89#define TP_CMOS_VOLUME_DOWN 0
90#define TP_CMOS_VOLUME_UP 1
91#define TP_CMOS_VOLUME_MUTE 2
92#define TP_CMOS_BRIGHTNESS_UP 4
93#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030094#define TP_CMOS_THINKLIGHT_ON 12
95#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020096
97/* NVRAM Addresses */
98enum tp_nvram_addr {
99 TP_NVRAM_ADDR_HK2 = 0x57,
100 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
101 TP_NVRAM_ADDR_VIDEO = 0x59,
102 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
103 TP_NVRAM_ADDR_MIXER = 0x60,
104};
105
106/* NVRAM bit masks */
107enum {
108 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
109 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
110 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
111 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
112 TP_NVRAM_MASK_THINKLIGHT = 0x10,
113 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
114 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
115 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
116 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
117 TP_NVRAM_MASK_MUTE = 0x40,
118 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
119 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
120 TP_NVRAM_POS_LEVEL_VOLUME = 0,
121};
122
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300123/* Misc NVRAM-related */
124enum {
125 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
126};
127
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200128/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400129#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
130#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Hui Wanga3c42a42016-11-08 16:13:23 +0800131#define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300132#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200133
134/* Input IDs */
135#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
136#define TPACPI_HKEY_INPUT_VERSION 0x4101
137
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200138/* ACPI \WGSV commands */
139enum {
140 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
141 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
142 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
143 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
144};
145
146/* TP_ACPI_WGSV_GET_STATE bits */
147enum {
148 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
149 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
150 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
151 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
152 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
153 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
154 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
155 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
156 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
157 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
158};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200159
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300160/* HKEY events */
161enum tpacpi_hkey_event_t {
162 /* Hotkey-related */
163 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
164 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
165 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
Hans de Goedec685e202017-02-09 16:44:13 +0100166 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300167 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
168 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
169 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
170
171 /* Reasons for waking up from S3/S4 */
172 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
173 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
174 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
175 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
176 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
177 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
178
179 /* Auto-sleep after eject request */
180 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
181 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
182
183 /* Misc bay events */
184 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -0300185 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
186 or port replicator */
187 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
188 dock or port replicator */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300189
190 /* User-interface events */
191 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
192 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
193 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
194 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
Lyudeb03f4d42016-11-11 15:15:03 -0500195 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
196 * enter/leave tablet mode
197 */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300198 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
199 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
200 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
201
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300202 /* Key-related user-interface events */
203 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
204 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
Xavier Naveira67ab6242015-02-10 08:45:18 +0100205 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300206
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300207 /* Thermal events */
208 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
209 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
210 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
211 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
212 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
213
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100214 /* AC-related events */
215 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300216
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300217 /* Misc */
218 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
219};
220
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200221/****************************************************************************
222 * Main driver
223 */
224
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200225#define TPACPI_NAME "thinkpad"
226#define TPACPI_DESC "ThinkPad ACPI Extras"
227#define TPACPI_FILE TPACPI_NAME "_acpi"
228#define TPACPI_URL "http://ibm-acpi.sf.net/"
229#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200230
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200231#define TPACPI_PROC_DIR "ibm"
232#define TPACPI_ACPI_EVENT_PREFIX "ibm"
233#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300234#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200235#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200236
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300237#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300238#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300239
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200240#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200241
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000242/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200243#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000244#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200245#define TPACPI_DBG_INIT 0x0001
246#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000247#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000248#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000249#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000250#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200251#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200252
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200253#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
254#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
255#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200256
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200257
258/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200259 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200260 */
261
262struct ibm_struct;
263
264struct tp_acpi_drv_struct {
265 const struct acpi_device_id *hid;
266 struct acpi_driver *driver;
267
268 void (*notify) (struct ibm_struct *, u32);
269 acpi_handle *handle;
270 u32 type;
271 struct acpi_device *device;
272};
273
274struct ibm_struct {
275 char *name;
276
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200277 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200278 int (*write) (char *);
279 void (*exit) (void);
280 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200281 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200282 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200283
284 struct list_head all_drivers;
285
286 struct tp_acpi_drv_struct *acpi;
287
288 struct {
289 u8 acpi_driver_registered:1;
290 u8 acpi_notify_installed:1;
291 u8 proc_created:1;
292 u8 init_called:1;
293 u8 experimental:1;
294 } flags;
295};
296
297struct ibm_init_struct {
298 char param[32];
299
300 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400301 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200302 struct ibm_struct *data;
303};
304
305static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200306 u32 bluetooth:1;
307 u32 hotkey:1;
308 u32 hotkey_mask:1;
309 u32 hotkey_wlsw:1;
Lyudeb3180022016-11-11 15:15:02 -0500310 enum {
311 TP_HOTKEY_TABLET_NONE = 0,
312 TP_HOTKEY_TABLET_USES_MHKG,
Lyudeb03f4d42016-11-11 15:15:03 -0500313 /* X1 Yoga 2016, seen on BIOS N1FET44W */
314 TP_HOTKEY_TABLET_USES_CMMD,
Lyudeb3180022016-11-11 15:15:02 -0500315 } hotkey_tablet;
Pali Rohárbb28f3d52015-12-30 23:27:41 +0100316 u32 kbdlight:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200317 u32 light:1;
318 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300319 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300320 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200321 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200322 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200323 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300324 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300325 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200326 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200327 u32 input_device_registered:1;
328 u32 platform_drv_registered:1;
329 u32 platform_drv_attrs_registered:1;
330 u32 sensors_pdrv_registered:1;
331 u32 sensors_pdrv_attrs_registered:1;
332 u32 sensors_pdev_attrs_registered:1;
333 u32 hotkey_poll_active:1;
Bastien Noceraf23a5bc2015-03-02 14:45:16 +0100334 u32 has_adaptive_kbd:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200335} tp_features;
336
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300337static struct {
338 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200339 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300340} tp_warned;
341
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200342struct thinkpad_id_data {
343 unsigned int vendor; /* ThinkPad vendor:
344 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
345
346 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
347 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
348
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300349 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200350 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300351 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
352 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200353
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300354 char *model_str; /* ThinkPad T43 */
355 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200356};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200357static struct thinkpad_id_data thinkpad_id;
358
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300359static enum {
360 TPACPI_LIFE_INIT = 0,
361 TPACPI_LIFE_RUNNING,
362 TPACPI_LIFE_EXITING,
363} tpacpi_lifecycle;
364
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200365static int experimental;
366static u32 dbg_level;
367
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300368static struct workqueue_struct *tpacpi_wq;
369
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000370enum led_status_t {
371 TPACPI_LED_OFF = 0,
372 TPACPI_LED_ON,
373 TPACPI_LED_BLINK,
374};
375
Hans de Goede86ec0c22017-02-09 16:44:12 +0100376/* tpacpi LED class */
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300377struct tpacpi_led_classdev {
378 struct led_classdev led_classdev;
Adam Leeedf2d772013-06-08 16:51:15 +0800379 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300380};
381
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300382/* brightness level capabilities */
383static unsigned int bright_maxlvl; /* 0 = unknown */
384
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200385#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
386static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030387static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200388static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030389static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200390static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030391static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200392static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030393static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200394#endif
395
396
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000397/*************************************************************************
398 * Debugging helpers
399 */
400
Joe Perches0978e012011-04-04 10:06:25 -0700401#define dbg_printk(a_dbg_level, format, arg...) \
402do { \
403 if (dbg_level & (a_dbg_level)) \
404 printk(KERN_DEBUG pr_fmt("%s: " format), \
405 __func__, ##arg); \
406} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000407
408#ifdef CONFIG_THINKPAD_ACPI_DEBUG
409#define vdbg_printk dbg_printk
410static const char *str_supported(int is_supported);
411#else
Joe Perches0978e012011-04-04 10:06:25 -0700412static inline const char *str_supported(int is_supported) { return ""; }
413#define vdbg_printk(a_dbg_level, format, arg...) \
Joe Perchesefd85cf2015-08-26 11:13:38 -0700414 do { if (0) no_printk(format, ##arg); } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000415#endif
416
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000417static void tpacpi_log_usertask(const char * const what)
418{
Joe Perches0978e012011-04-04 10:06:25 -0700419 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
420 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000421}
422
Joe Perches0978e012011-04-04 10:06:25 -0700423#define tpacpi_disclose_usertask(what, format, arg...) \
424do { \
425 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
426 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
427 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
428 what, task_tgid_vnr(current), ## arg); \
429 } \
430} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000431
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300432/*
433 * Quirk handling helpers
434 *
435 * ThinkPad IDs and versions seen in the field so far
436 * are two-characters from the set [0-9A-Z], i.e. base 36.
437 *
438 * We use values well outside that range as specials.
439 */
440
441#define TPACPI_MATCH_ANY 0xffffU
442#define TPACPI_MATCH_UNKNOWN 0U
443
444/* TPID('1', 'Y') == 0x5931 */
445#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
446
447#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_IBM, \
449 .bios = TPID(__id1, __id2), \
450 .ec = TPACPI_MATCH_ANY, \
451 .quirks = (__quirk) }
452
453#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
454 { .vendor = PCI_VENDOR_ID_LENOVO, \
455 .bios = TPID(__id1, __id2), \
456 .ec = TPACPI_MATCH_ANY, \
457 .quirks = (__quirk) }
458
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200459#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
460 { .vendor = PCI_VENDOR_ID_LENOVO, \
461 .bios = TPACPI_MATCH_ANY, \
462 .ec = TPID(__id1, __id2), \
463 .quirks = (__quirk) }
464
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300465struct tpacpi_quirk {
466 unsigned int vendor;
467 u16 bios;
468 u16 ec;
469 unsigned long quirks;
470};
471
472/**
473 * tpacpi_check_quirks() - search BIOS/EC version on a list
474 * @qlist: array of &struct tpacpi_quirk
475 * @qlist_size: number of elements in @qlist
476 *
477 * Iterates over a quirks list until one is found that matches the
478 * ThinkPad's vendor, BIOS and EC model.
479 *
480 * Returns 0 if nothing matches, otherwise returns the quirks field of
481 * the matching &struct tpacpi_quirk entry.
482 *
483 * The match criteria is: vendor, ec and bios much match.
484 */
485static unsigned long __init tpacpi_check_quirks(
486 const struct tpacpi_quirk *qlist,
487 unsigned int qlist_size)
488{
489 while (qlist_size) {
490 if ((qlist->vendor == thinkpad_id.vendor ||
491 qlist->vendor == TPACPI_MATCH_ANY) &&
492 (qlist->bios == thinkpad_id.bios_model ||
493 qlist->bios == TPACPI_MATCH_ANY) &&
494 (qlist->ec == thinkpad_id.ec_model ||
495 qlist->ec == TPACPI_MATCH_ANY))
496 return qlist->quirks;
497
498 qlist_size--;
499 qlist++;
500 }
501 return 0;
502}
503
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300504static inline bool __pure __init tpacpi_is_lenovo(void)
505{
506 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
507}
508
509static inline bool __pure __init tpacpi_is_ibm(void)
510{
511 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
512}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300513
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300514/****************************************************************************
515 ****************************************************************************
516 *
517 * ACPI Helpers and device model
518 *
519 ****************************************************************************
520 ****************************************************************************/
521
522/*************************************************************************
523 * ACPI basic handles
524 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300526static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300527static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200529#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700531 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300532 &parent##_handle; \
533 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200535TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
536TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200538TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
539 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400540 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
541 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300542 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400543
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200544TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400545 "^HKEY", /* R30, R31 */
546 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300547 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400548
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300549/*************************************************************************
550 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200551 */
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700554 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400557 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200558 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400559 struct acpi_buffer result, *resultp;
560 union acpi_object out_obj;
561 acpi_status status;
562 va_list ap;
563 char res_type;
564 int success;
565 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700568 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return 0;
570 }
571
572 if (*fmt == 'q') {
573 quiet = 1;
574 fmt++;
575 } else
576 quiet = 0;
577
578 res_type = *(fmt++);
579
580 params.count = 0;
581 params.pointer = &in_objs[0];
582
583 va_start(ap, fmt);
584 while (*fmt) {
585 char c = *(fmt++);
586 switch (c) {
587 case 'd': /* int */
588 in_objs[params.count].integer.value = va_arg(ap, int);
589 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
590 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400591 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300593 pr_err("acpi_evalf() called with invalid format character '%c'\n",
594 c);
Jesper Juhl21365852010-12-24 19:56:28 +0100595 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return 0;
597 }
598 }
599 va_end(ap);
600
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400601 if (res_type != 'v') {
602 result.length = sizeof(out_obj);
603 result.pointer = &out_obj;
604 resultp = &result;
605 } else
606 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400608 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400611 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300612 success = (status == AE_OK &&
613 out_obj.type == ACPI_TYPE_INTEGER);
614 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700615 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400617 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 success = status == AE_OK;
619 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400620 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300622 pr_err("acpi_evalf() called with invalid format character '%c'\n",
623 res_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return 0;
625 }
626
627 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700628 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300629 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 return success;
632}
633
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200634static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300635{
636 int v;
637
638 if (ecrd_handle) {
639 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
640 return 0;
641 *p = v;
642 } else {
643 if (ec_read(i, p) < 0)
644 return 0;
645 }
646
647 return 1;
648}
649
650static int acpi_ec_write(int i, u8 v)
651{
652 if (ecwr_handle) {
653 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
654 return 0;
655 } else {
656 if (ec_write(i, v) < 0)
657 return 0;
658 }
659
660 return 1;
661}
662
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300663static int issue_thinkpad_cmos_command(int cmos_cmd)
664{
665 if (!cmos_handle)
666 return -ENXIO;
667
668 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
669 return -EIO;
670
671 return 0;
672}
673
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300674/*************************************************************************
675 * ACPI device model
676 */
677
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200678#define TPACPI_ACPIHANDLE_INIT(object) \
679 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300680 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200681
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300682static void __init drv_acpi_handle_init(const char *name,
683 acpi_handle *handle, const acpi_handle parent,
684 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300685{
686 int i;
687 acpi_status status;
688
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300689 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
690 name);
691
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300692 for (i = 0; i < num_paths; i++) {
693 status = acpi_get_handle(parent, paths[i], handle);
694 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300695 dbg_printk(TPACPI_DBG_INIT,
696 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300697 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300698 return;
699 }
700 }
701
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300702 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
703 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300704 *handle = NULL;
705}
706
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300707static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
708 u32 level, void *context, void **return_value)
709{
Aaron Lu46445b62013-10-11 21:27:46 +0800710 struct acpi_device *dev;
711 if (!strcmp(context, "video")) {
712 if (acpi_bus_get_device(handle, &dev))
713 return AE_OK;
714 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
715 return AE_OK;
716 }
717
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300718 *(acpi_handle *)return_value = handle;
719
720 return AE_CTRL_TERMINATE;
721}
722
723static void __init tpacpi_acpi_handle_locate(const char *name,
724 const char *hid,
725 acpi_handle *handle)
726{
727 acpi_status status;
728 acpi_handle device_found;
729
Aaron Lu46445b62013-10-11 21:27:46 +0800730 BUG_ON(!name || !handle);
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300731 vdbg_printk(TPACPI_DBG_INIT,
732 "trying to locate ACPI handle for %s, using HID %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +0800733 name, hid ? hid : "NULL");
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300734
735 memset(&device_found, 0, sizeof(device_found));
736 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
737 (void *)name, &device_found);
738
739 *handle = NULL;
740
741 if (ACPI_SUCCESS(status)) {
742 *handle = device_found;
743 dbg_printk(TPACPI_DBG_INIT,
744 "Found ACPI handle for %s\n", name);
745 } else {
746 vdbg_printk(TPACPI_DBG_INIT,
747 "Could not locate an ACPI handle for %s: %s\n",
748 name, acpi_format_exception(status));
749 }
750}
751
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300752static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300753{
754 struct ibm_struct *ibm = data;
755
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300756 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
757 return;
758
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300759 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300760 return;
761
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300762 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300763}
764
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300765static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300766{
767 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300768 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300769
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300770 BUG_ON(!ibm->acpi);
771
772 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300773 return 0;
774
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300775 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300776 "setting up ACPI notify for %s\n", ibm->name);
777
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300778 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
779 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700780 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300781 return -ENODEV;
782 }
783
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700784 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300785 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200786 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300787 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300788
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300789 status = acpi_install_notify_handler(*ibm->acpi->handle,
790 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300791 if (ACPI_FAILURE(status)) {
792 if (status == AE_ALREADY_EXISTS) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +0300793 pr_notice("another device driver is already handling %s events\n",
794 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300795 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700796 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300797 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300798 }
799 return -ENODEV;
800 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300801 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300802 return 0;
803}
804
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300805static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300806{
807 return 0;
808}
809
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300810static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300811{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300812 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300813
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300814 dbg_printk(TPACPI_DBG_INIT,
815 "registering %s as an ACPI driver\n", ibm->name);
816
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300817 BUG_ON(!ibm->acpi);
818
819 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
820 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700821 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300822 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300823 }
824
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200825 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300826 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200827
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300828 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300829
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300830 rc = acpi_bus_register_driver(ibm->acpi->driver);
831 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700832 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200833 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300834 kfree(ibm->acpi->driver);
835 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300836 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300837 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300838
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300839 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300840}
841
842
843/****************************************************************************
844 ****************************************************************************
845 *
846 * Procfs Helpers
847 *
848 ****************************************************************************
849 ****************************************************************************/
850
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200851static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300852{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200853 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300854
855 if (!ibm || !ibm->read)
856 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200857 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300858}
859
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200860static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300861{
Al Virod9dda782013-03-31 18:16:14 -0400862 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200863}
864
865static ssize_t dispatch_proc_write(struct file *file,
866 const char __user *userbuf,
867 size_t count, loff_t *pos)
868{
Al Virod9dda782013-03-31 18:16:14 -0400869 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300870 char *kernbuf;
871 int ret;
872
873 if (!ibm || !ibm->write)
874 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300875 if (count > PAGE_SIZE - 2)
876 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300877
878 kernbuf = kmalloc(count + 2, GFP_KERNEL);
879 if (!kernbuf)
880 return -ENOMEM;
881
882 if (copy_from_user(kernbuf, userbuf, count)) {
883 kfree(kernbuf);
884 return -EFAULT;
885 }
886
887 kernbuf[count] = 0;
888 strcat(kernbuf, ",");
889 ret = ibm->write(kernbuf);
890 if (ret == 0)
891 ret = count;
892
893 kfree(kernbuf);
894
895 return ret;
896}
897
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200898static const struct file_operations dispatch_proc_fops = {
899 .owner = THIS_MODULE,
900 .open = dispatch_proc_open,
901 .read = seq_read,
902 .llseek = seq_lseek,
903 .release = single_release,
904 .write = dispatch_proc_write,
905};
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static char *next_cmd(char **cmds)
908{
909 char *start = *cmds;
910 char *end;
911
912 while ((end = strchr(start, ',')) && end == start)
913 start = end + 1;
914
915 if (!end)
916 return NULL;
917
918 *end = 0;
919 *cmds = end + 1;
920 return start;
921}
922
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300923
924/****************************************************************************
925 ****************************************************************************
926 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300927 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300928 *
929 ****************************************************************************
930 ****************************************************************************/
931
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300932static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300933static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700934static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300935static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300936static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200937static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300938
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200939#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200940static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200941{
942 struct ibm_struct *ibm, *itmp;
943
944 list_for_each_entry_safe(ibm, itmp,
945 &tpacpi_all_drivers,
946 all_drivers) {
947 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200948 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200949 }
950
951 return 0;
952}
953
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200954static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300955{
956 struct ibm_struct *ibm, *itmp;
957
958 list_for_each_entry_safe(ibm, itmp,
959 &tpacpi_all_drivers,
960 all_drivers) {
961 if (ibm->resume)
962 (ibm->resume)();
963 }
964
965 return 0;
966}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200967#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300968
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200969static SIMPLE_DEV_PM_OPS(tpacpi_pm,
970 tpacpi_suspend_handler, tpacpi_resume_handler);
971
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200972static void tpacpi_shutdown_handler(struct platform_device *pdev)
973{
974 struct ibm_struct *ibm, *itmp;
975
976 list_for_each_entry_safe(ibm, itmp,
977 &tpacpi_all_drivers,
978 all_drivers) {
979 if (ibm->shutdown)
980 (ibm->shutdown)();
981 }
982}
983
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300984static struct platform_driver tpacpi_pdriver = {
985 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200986 .name = TPACPI_DRVR_NAME,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200987 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300988 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200989 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300990};
991
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300992static struct platform_driver tpacpi_hwmon_pdriver = {
993 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200994 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300995 },
996};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300997
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300998/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300999 * sysfs support helpers
1000 */
1001
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001002struct attribute_set {
1003 unsigned int members, max_members;
1004 struct attribute_group group;
1005};
1006
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001007struct attribute_set_obj {
1008 struct attribute_set s;
1009 struct attribute *a;
1010} __attribute__((packed));
1011
1012static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001013 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001014{
1015 struct attribute_set_obj *sobj;
1016
1017 if (max_members == 0)
1018 return NULL;
1019
1020 /* Allocates space for implicit NULL at the end too */
1021 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1022 max_members * sizeof(struct attribute *),
1023 GFP_KERNEL);
1024 if (!sobj)
1025 return NULL;
1026 sobj->s.max_members = max_members;
1027 sobj->s.group.attrs = &sobj->a;
1028 sobj->s.group.name = name;
1029
1030 return &sobj->s;
1031}
1032
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001033#define destroy_attr_set(_set) \
1034 kfree(_set);
1035
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001036/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001037static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001038{
1039 if (!s || !attr)
1040 return -EINVAL;
1041
1042 if (s->members >= s->max_members)
1043 return -ENOMEM;
1044
1045 s->group.attrs[s->members] = attr;
1046 s->members++;
1047
1048 return 0;
1049}
1050
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001051static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001052 struct attribute **attr,
1053 unsigned int count)
1054{
1055 int i, res;
1056
1057 for (i = 0; i < count; i++) {
1058 res = add_to_attr_set(s, attr[i]);
1059 if (res)
1060 return res;
1061 }
1062
1063 return 0;
1064}
1065
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001066static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001067{
1068 sysfs_remove_group(kobj, &s->group);
1069 destroy_attr_set(s);
1070}
1071
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001072#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1073 sysfs_create_group(_kobj, &_attr_set->group)
1074
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001075static int parse_strtoul(const char *buf,
1076 unsigned long max, unsigned long *value)
1077{
1078 char *endp;
1079
André Goddard Rosae7d28602009-12-14 18:01:06 -08001080 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1081 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001082 if (*endp || *value > max)
1083 return -EINVAL;
1084
1085 return 0;
1086}
1087
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001088static void tpacpi_disable_brightness_delay(void)
1089{
1090 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001091 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001092}
1093
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001094static void printk_deprecated_attribute(const char * const what,
1095 const char * const details)
1096{
1097 tpacpi_log_usertask("deprecated sysfs attribute");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001098 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001099 what, details);
1100}
1101
Johannes Berg19d337d2009-06-02 13:01:37 +02001102/*************************************************************************
1103 * rfkill and radio control support helpers
1104 */
1105
1106/*
1107 * ThinkPad-ACPI firmware handling model:
1108 *
1109 * WLSW (master wireless switch) is event-driven, and is common to all
1110 * firmware-controlled radios. It cannot be controlled, just monitored,
1111 * as expected. It overrides all radio state in firmware
1112 *
1113 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1114 * (TODO: verify how WLSW interacts with the returned radio state).
1115 *
1116 * The only time there are shadow radio state changes, is when
1117 * masked-off hotkeys are used.
1118 */
1119
1120/*
1121 * Internal driver API for radio state:
1122 *
1123 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1124 * bool: true means radio blocked (off)
1125 */
1126enum tpacpi_rfkill_state {
1127 TPACPI_RFK_RADIO_OFF = 0,
1128 TPACPI_RFK_RADIO_ON
1129};
1130
1131/* rfkill switches */
1132enum tpacpi_rfk_id {
1133 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1134 TPACPI_RFK_WWAN_SW_ID,
1135 TPACPI_RFK_UWB_SW_ID,
1136 TPACPI_RFK_SW_MAX
1137};
1138
1139static const char *tpacpi_rfkill_names[] = {
1140 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1141 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1142 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1143 [TPACPI_RFK_SW_MAX] = NULL
1144};
1145
1146/* ThinkPad-ACPI rfkill subdriver */
1147struct tpacpi_rfk {
1148 struct rfkill *rfkill;
1149 enum tpacpi_rfk_id id;
1150 const struct tpacpi_rfk_ops *ops;
1151};
1152
1153struct tpacpi_rfk_ops {
1154 /* firmware interface */
1155 int (*get_status)(void);
1156 int (*set_status)(const enum tpacpi_rfkill_state);
1157};
1158
1159static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1160
1161/* Query FW and update rfkill sw state for a given rfkill switch */
1162static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1163{
1164 int status;
1165
1166 if (!tp_rfk)
1167 return -ENODEV;
1168
1169 status = (tp_rfk->ops->get_status)();
1170 if (status < 0)
1171 return status;
1172
1173 rfkill_set_sw_state(tp_rfk->rfkill,
1174 (status == TPACPI_RFK_RADIO_OFF));
1175
1176 return status;
1177}
1178
1179/* Query FW and update rfkill sw state for all rfkill switches */
1180static void tpacpi_rfk_update_swstate_all(void)
1181{
1182 unsigned int i;
1183
1184 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1185 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1186}
1187
1188/*
1189 * Sync the HW-blocking state of all rfkill switches,
1190 * do notice it causes the rfkill core to schedule uevents
1191 */
1192static void tpacpi_rfk_update_hwblock_state(bool blocked)
1193{
1194 unsigned int i;
1195 struct tpacpi_rfk *tp_rfk;
1196
1197 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1198 tp_rfk = tpacpi_rfkill_switches[i];
1199 if (tp_rfk) {
1200 if (rfkill_set_hw_state(tp_rfk->rfkill,
1201 blocked)) {
1202 /* ignore -- we track sw block */
1203 }
1204 }
1205 }
1206}
1207
1208/* Call to get the WLSW state from the firmware */
1209static int hotkey_get_wlsw(void);
1210
1211/* Call to query WLSW state and update all rfkill switches */
1212static bool tpacpi_rfk_check_hwblock_state(void)
1213{
1214 int res = hotkey_get_wlsw();
1215 int hw_blocked;
1216
1217 /* When unknown or unsupported, we have to assume it is unblocked */
1218 if (res < 0)
1219 return false;
1220
1221 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1222 tpacpi_rfk_update_hwblock_state(hw_blocked);
1223
1224 return hw_blocked;
1225}
1226
1227static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1228{
1229 struct tpacpi_rfk *tp_rfk = data;
1230 int res;
1231
1232 dbg_printk(TPACPI_DBG_RFKILL,
1233 "request to change radio state to %s\n",
1234 blocked ? "blocked" : "unblocked");
1235
1236 /* try to set radio state */
1237 res = (tp_rfk->ops->set_status)(blocked ?
1238 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1239
1240 /* and update the rfkill core with whatever the FW really did */
1241 tpacpi_rfk_update_swstate(tp_rfk);
1242
1243 return (res < 0) ? res : 0;
1244}
1245
1246static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1247 .set_block = tpacpi_rfk_hook_set_block,
1248};
1249
1250static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1251 const struct tpacpi_rfk_ops *tp_rfkops,
1252 const enum rfkill_type rfktype,
1253 const char *name,
1254 const bool set_default)
1255{
1256 struct tpacpi_rfk *atp_rfk;
1257 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001258 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001259 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001260 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001261
1262 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1263
Johannes Berg19d337d2009-06-02 13:01:37 +02001264 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1265 if (atp_rfk)
1266 atp_rfk->rfkill = rfkill_alloc(name,
1267 &tpacpi_pdev->dev,
1268 rfktype,
1269 &tpacpi_rfk_rfkill_ops,
1270 atp_rfk);
1271 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001272 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001273 kfree(atp_rfk);
1274 return -ENOMEM;
1275 }
1276
1277 atp_rfk->id = id;
1278 atp_rfk->ops = tp_rfkops;
1279
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001280 sw_status = (tp_rfkops->get_status)();
1281 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001282 pr_err("failed to read initial state for %s, error %d\n",
1283 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001284 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001285 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001286 if (set_default) {
1287 /* try to keep the initial state, since we ask the
1288 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001289 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001290 }
1291 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001292 hw_state = tpacpi_rfk_check_hwblock_state();
1293 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001294
1295 res = rfkill_register(atp_rfk->rfkill);
1296 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001297 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001298 rfkill_destroy(atp_rfk->rfkill);
1299 kfree(atp_rfk);
1300 return res;
1301 }
1302
1303 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001304
Joe Perches0978e012011-04-04 10:06:25 -07001305 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001306 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001307 return 0;
1308}
1309
1310static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1311{
1312 struct tpacpi_rfk *tp_rfk;
1313
1314 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1315
1316 tp_rfk = tpacpi_rfkill_switches[id];
1317 if (tp_rfk) {
1318 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001319 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001320 tpacpi_rfkill_switches[id] = NULL;
1321 kfree(tp_rfk);
1322 }
1323}
1324
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001325static void printk_deprecated_rfkill_attribute(const char * const what)
1326{
1327 printk_deprecated_attribute(what,
1328 "Please switch to generic rfkill before year 2010");
1329}
1330
Johannes Berg19d337d2009-06-02 13:01:37 +02001331/* sysfs <radio> enable ------------------------------------------------ */
1332static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1333 struct device_attribute *attr,
1334 char *buf)
1335{
1336 int status;
1337
1338 printk_deprecated_rfkill_attribute(attr->attr.name);
1339
1340 /* This is in the ABI... */
1341 if (tpacpi_rfk_check_hwblock_state()) {
1342 status = TPACPI_RFK_RADIO_OFF;
1343 } else {
1344 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1345 if (status < 0)
1346 return status;
1347 }
1348
1349 return snprintf(buf, PAGE_SIZE, "%d\n",
1350 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1351}
1352
1353static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1354 struct device_attribute *attr,
1355 const char *buf, size_t count)
1356{
1357 unsigned long t;
1358 int res;
1359
1360 printk_deprecated_rfkill_attribute(attr->attr.name);
1361
1362 if (parse_strtoul(buf, 1, &t))
1363 return -EINVAL;
1364
1365 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1366
1367 /* This is in the ABI... */
1368 if (tpacpi_rfk_check_hwblock_state() && !!t)
1369 return -EPERM;
1370
1371 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1372 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1373 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1374
1375 return (res < 0) ? res : count;
1376}
1377
1378/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001379static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001380{
Johannes Berg19d337d2009-06-02 13:01:37 +02001381 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001382 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001383 else {
1384 int status;
1385
1386 /* This is in the ABI... */
1387 if (tpacpi_rfk_check_hwblock_state()) {
1388 status = TPACPI_RFK_RADIO_OFF;
1389 } else {
1390 status = tpacpi_rfk_update_swstate(
1391 tpacpi_rfkill_switches[id]);
1392 if (status < 0)
1393 return status;
1394 }
1395
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001396 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001397 (status == TPACPI_RFK_RADIO_ON) ?
1398 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001399 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001400 }
1401
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001402 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001403}
1404
1405static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1406{
1407 char *cmd;
1408 int status = -1;
1409 int res = 0;
1410
1411 if (id >= TPACPI_RFK_SW_MAX)
1412 return -ENODEV;
1413
1414 while ((cmd = next_cmd(&buf))) {
1415 if (strlencmp(cmd, "enable") == 0)
1416 status = TPACPI_RFK_RADIO_ON;
1417 else if (strlencmp(cmd, "disable") == 0)
1418 status = TPACPI_RFK_RADIO_OFF;
1419 else
1420 return -EINVAL;
1421 }
1422
1423 if (status != -1) {
1424 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1425 (status == TPACPI_RFK_RADIO_ON) ?
1426 "enable" : "disable",
1427 tpacpi_rfkill_names[id]);
1428 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1429 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1430 }
1431
1432 return res;
1433}
1434
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001435/*************************************************************************
1436 * thinkpad-acpi driver attributes
1437 */
1438
1439/* interface_version --------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001440static ssize_t interface_version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001441{
1442 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1443}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001444static DRIVER_ATTR_RO(interface_version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001445
1446/* debug_level --------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001447static ssize_t debug_level_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001448{
1449 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1450}
1451
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001452static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1453 size_t count)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001454{
1455 unsigned long t;
1456
1457 if (parse_strtoul(buf, 0xffff, &t))
1458 return -EINVAL;
1459
1460 dbg_level = t;
1461
1462 return count;
1463}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001464static DRIVER_ATTR_RW(debug_level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001465
1466/* version ------------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001467static ssize_t version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001468{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001469 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1470 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001471}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001472static DRIVER_ATTR_RO(version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001473
1474/* --------------------------------------------------------------------- */
1475
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001476#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1477
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001478/* wlsw_emulstate ------------------------------------------------------ */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001479static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001480{
1481 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1482}
1483
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001484static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1485 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001486{
1487 unsigned long t;
1488
1489 if (parse_strtoul(buf, 1, &t))
1490 return -EINVAL;
1491
Johannes Berg19d337d2009-06-02 13:01:37 +02001492 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001493 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001494 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1495 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001496
1497 return count;
1498}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001499static DRIVER_ATTR_RW(wlsw_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001500
1501/* bluetooth_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001502static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001503{
1504 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1505}
1506
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001507static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1508 const char *buf, size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001509{
1510 unsigned long t;
1511
1512 if (parse_strtoul(buf, 1, &t))
1513 return -EINVAL;
1514
1515 tpacpi_bluetooth_emulstate = !!t;
1516
1517 return count;
1518}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001519static DRIVER_ATTR_RW(bluetooth_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001520
1521/* wwan_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001522static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001523{
1524 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1525}
1526
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001527static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1528 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001529{
1530 unsigned long t;
1531
1532 if (parse_strtoul(buf, 1, &t))
1533 return -EINVAL;
1534
1535 tpacpi_wwan_emulstate = !!t;
1536
1537 return count;
1538}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001539static DRIVER_ATTR_RW(wwan_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001540
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001541/* uwb_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001542static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001543{
1544 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1545}
1546
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001547static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1548 size_t count)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001549{
1550 unsigned long t;
1551
1552 if (parse_strtoul(buf, 1, &t))
1553 return -EINVAL;
1554
1555 tpacpi_uwb_emulstate = !!t;
1556
1557 return count;
1558}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001559static DRIVER_ATTR_RW(uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001560#endif
1561
1562/* --------------------------------------------------------------------- */
1563
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001564static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001565 &driver_attr_debug_level, &driver_attr_version,
1566 &driver_attr_interface_version,
1567};
1568
1569static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1570{
1571 int i, res;
1572
1573 i = 0;
1574 res = 0;
1575 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1576 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1577 i++;
1578 }
1579
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001580#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1581 if (!res && dbg_wlswemul)
1582 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1583 if (!res && dbg_bluetoothemul)
1584 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1585 if (!res && dbg_wwanemul)
1586 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001587 if (!res && dbg_uwbemul)
1588 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001589#endif
1590
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001591 return res;
1592}
1593
1594static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1595{
1596 int i;
1597
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001598 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001599 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001600
1601#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1602 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1603 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1604 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001605 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001606#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001607}
1608
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001609/*************************************************************************
1610 * Firmware Data
1611 */
1612
1613/*
1614 * Table of recommended minimum BIOS versions
1615 *
1616 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001617 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001618 * bugs and bad ACPI behaviour on older versions
1619 *
1620 * 2. BIOS or EC fw with known bugs that trigger on Linux
1621 *
1622 * 3. BIOS with known reduced functionality in older versions
1623 *
1624 * We recommend the latest BIOS and EC version.
1625 * We only support the latest BIOS and EC fw version as a rule.
1626 *
1627 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1628 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001629 *
1630 * WARNING: we use this table also to detect that the machine is
1631 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001632 */
1633
1634#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1635 { .vendor = (__v), \
1636 .bios = TPID(__id1, __id2), \
1637 .ec = TPACPI_MATCH_ANY, \
1638 .quirks = TPACPI_MATCH_ANY << 16 \
1639 | (__bv1) << 8 | (__bv2) }
1640
1641#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001642 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001643 { .vendor = (__v), \
1644 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001645 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001646 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1647 | (__bv1) << 8 | (__bv2) }
1648
1649#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1650 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1651
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001652/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001653#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1654 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001655 __bv1, __bv2, TPID(__id1, __id2), \
1656 __ev1, __ev2), \
1657 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1658 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1659 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001660
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001661/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001662#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1663 __eid1, __eid2, __ev1, __ev2) \
1664 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001665 __bv1, __bv2, TPID(__eid1, __eid2), \
1666 __ev1, __ev2), \
1667 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1668 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1669 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001670
1671#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1672 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1673
1674#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1675 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001676 __bv1, __bv2, TPID(__id1, __id2), \
1677 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001678
1679#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1680 __eid1, __eid2, __ev1, __ev2) \
1681 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001682 __bv1, __bv2, TPID(__eid1, __eid2), \
1683 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001684
1685static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1686 /* Numeric models ------------------ */
1687 /* FW MODEL BIOS VERS */
1688 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1689 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1690 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1691 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1692 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1693 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1694 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1695 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1696 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1697
1698 /* A-series ------------------------- */
1699 /* FW MODEL BIOS VERS EC VERS */
1700 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1701 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1702 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1703 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1704 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1705 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1706 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1707 TPV_QI0('1', '3', '2', '0'), /* A22m */
1708 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1709 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1710 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1711
1712 /* G-series ------------------------- */
1713 /* FW MODEL BIOS VERS */
1714 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1715 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1716
1717 /* R-series, T-series --------------- */
1718 /* FW MODEL BIOS VERS EC VERS */
1719 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1720 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1721 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1722 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1723 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1724 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1725 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1726 T40/p, T41/p, T42/p (1) */
1727 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1728 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1729 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1730 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1731
1732 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1733 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1734 TPV_QI0('1', '6', '3', '2'), /* T22 */
1735 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1736 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1737 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1738
1739 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1740 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001741 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001742
1743 /* BIOS FW BIOS VERS EC FW EC VERS */
1744 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1745 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1746
1747 /* X-series ------------------------- */
1748 /* FW MODEL BIOS VERS EC VERS */
1749 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1750 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1751 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1752 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1753 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1754 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1755 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1756
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001757 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1758 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001759
1760 /* (0) - older versions lack DMI EC fw string and functionality */
1761 /* (1) - older versions known to lack functionality */
1762};
1763
1764#undef TPV_QL1
1765#undef TPV_QL0
1766#undef TPV_QI2
1767#undef TPV_QI1
1768#undef TPV_QI0
1769#undef TPV_Q_X
1770#undef TPV_Q
1771
1772static void __init tpacpi_check_outdated_fw(void)
1773{
1774 unsigned long fwvers;
1775 u16 ec_version, bios_version;
1776
1777 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1778 ARRAY_SIZE(tpacpi_bios_version_qtable));
1779
1780 if (!fwvers)
1781 return;
1782
1783 bios_version = fwvers & 0xffffU;
1784 ec_version = (fwvers >> 16) & 0xffffU;
1785
1786 /* note that unknown versions are set to 0x0000 and we use that */
1787 if ((bios_version > thinkpad_id.bios_release) ||
1788 (ec_version > thinkpad_id.ec_release &&
1789 ec_version != TPACPI_MATCH_ANY)) {
1790 /*
1791 * The changelogs would let us track down the exact
1792 * reason, but it is just too much of a pain to track
1793 * it. We only list BIOSes that are either really
1794 * broken, or really stable to begin with, so it is
1795 * best if the user upgrades the firmware anyway.
1796 */
Joe Perches0978e012011-04-04 10:06:25 -07001797 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001798 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 -03001799 }
1800}
1801
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001802static bool __init tpacpi_is_fw_known(void)
1803{
1804 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1805 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1806}
1807
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001808/****************************************************************************
1809 ****************************************************************************
1810 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001811 * Subdrivers
1812 *
1813 ****************************************************************************
1814 ****************************************************************************/
1815
1816/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001817 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001818 */
1819
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001820static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001822 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1823 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001827static struct ibm_struct thinkpad_acpi_driver_data = {
1828 .name = "driver",
1829 .read = thinkpad_acpi_driver_read,
1830};
1831
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001832/*************************************************************************
1833 * Hotkey subdriver
1834 */
1835
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001836/*
1837 * ThinkPad firmware event model
1838 *
1839 * The ThinkPad firmware has two main event interfaces: normal ACPI
1840 * notifications (which follow the ACPI standard), and a private event
1841 * interface.
1842 *
1843 * The private event interface also issues events for the hotkeys. As
1844 * the driver gained features, the event handling code ended up being
1845 * built around the hotkey subdriver. This will need to be refactored
1846 * to a more formal event API eventually.
1847 *
1848 * Some "hotkeys" are actually supposed to be used as event reports,
1849 * such as "brightness has changed", "volume has changed", depending on
1850 * the ThinkPad model and how the firmware is operating.
1851 *
1852 * Unlike other classes, hotkey-class events have mask/unmask control on
1853 * non-ancient firmware. However, how it behaves changes a lot with the
1854 * firmware model and version.
1855 */
1856
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001857enum { /* hot key scan codes (derived from ACPI DSDT) */
1858 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1859 TP_ACPI_HOTKEYSCAN_FNF2,
1860 TP_ACPI_HOTKEYSCAN_FNF3,
1861 TP_ACPI_HOTKEYSCAN_FNF4,
1862 TP_ACPI_HOTKEYSCAN_FNF5,
1863 TP_ACPI_HOTKEYSCAN_FNF6,
1864 TP_ACPI_HOTKEYSCAN_FNF7,
1865 TP_ACPI_HOTKEYSCAN_FNF8,
1866 TP_ACPI_HOTKEYSCAN_FNF9,
1867 TP_ACPI_HOTKEYSCAN_FNF10,
1868 TP_ACPI_HOTKEYSCAN_FNF11,
1869 TP_ACPI_HOTKEYSCAN_FNF12,
1870 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1871 TP_ACPI_HOTKEYSCAN_FNINSERT,
1872 TP_ACPI_HOTKEYSCAN_FNDELETE,
1873 TP_ACPI_HOTKEYSCAN_FNHOME,
1874 TP_ACPI_HOTKEYSCAN_FNEND,
1875 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1876 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1877 TP_ACPI_HOTKEYSCAN_FNSPACE,
1878 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1879 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1880 TP_ACPI_HOTKEYSCAN_MUTE,
1881 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001882 TP_ACPI_HOTKEYSCAN_UNK1,
1883 TP_ACPI_HOTKEYSCAN_UNK2,
1884 TP_ACPI_HOTKEYSCAN_UNK3,
1885 TP_ACPI_HOTKEYSCAN_UNK4,
1886 TP_ACPI_HOTKEYSCAN_UNK5,
1887 TP_ACPI_HOTKEYSCAN_UNK6,
1888 TP_ACPI_HOTKEYSCAN_UNK7,
1889 TP_ACPI_HOTKEYSCAN_UNK8,
1890
Christian Kellner149c8c72017-02-28 17:10:56 +01001891 /* Adaptive keyboard keycodes */
1892 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1893 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
Bastien Nocera6a68d852015-03-02 14:45:31 +01001894 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1895 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1896 TP_ACPI_HOTKEYSCAN_CLOUD,
1897 TP_ACPI_HOTKEYSCAN_UNK9,
1898 TP_ACPI_HOTKEYSCAN_VOICE,
1899 TP_ACPI_HOTKEYSCAN_UNK10,
1900 TP_ACPI_HOTKEYSCAN_GESTURES,
1901 TP_ACPI_HOTKEYSCAN_UNK11,
1902 TP_ACPI_HOTKEYSCAN_UNK12,
1903 TP_ACPI_HOTKEYSCAN_UNK13,
1904 TP_ACPI_HOTKEYSCAN_CONFIG,
1905 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1906 TP_ACPI_HOTKEYSCAN_RELOAD,
1907 TP_ACPI_HOTKEYSCAN_BACK,
1908 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1909 TP_ACPI_HOTKEYSCAN_MIC_UP,
1910 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1911 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1912 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1913
Christian Kellner696c65232017-02-28 17:10:57 +01001914 /* Lenovo extended keymap, starting at 0x1300 */
1915 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1916 /* first new observed key (star, favorites) is 0x1311 */
1917 TP_ACPI_HOTKEYSCAN_STAR = 69,
1918 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1919 TP_ACPI_HOTKEYSCAN_UNK25,
1920 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1921 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1922
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001923 /* Hotkey keymap size */
1924 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001925};
1926
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001927enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001928 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1929 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1930};
1931
1932enum { /* Positions of some of the keys in hotkey masks */
1933 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1934 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1935 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1936 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1937 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
Hans de Goedec685e202017-02-09 16:44:13 +01001938 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001939 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1940 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1941 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1942 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1943 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1944};
1945
1946enum { /* NVRAM to ACPI HKEY group map */
1947 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1948 TP_ACPI_HKEY_ZOOM_MASK |
1949 TP_ACPI_HKEY_DISPSWTCH_MASK |
1950 TP_ACPI_HKEY_HIBERNATE_MASK,
1951 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1952 TP_ACPI_HKEY_BRGHTDWN_MASK,
1953 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1954 TP_ACPI_HKEY_VOLDWN_MASK |
1955 TP_ACPI_HKEY_MUTE_MASK,
1956};
1957
1958#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1959struct tp_nvram_state {
1960 u16 thinkpad_toggle:1;
1961 u16 zoom_toggle:1;
1962 u16 display_toggle:1;
1963 u16 thinklight_toggle:1;
1964 u16 hibernate_toggle:1;
1965 u16 displayexp_toggle:1;
1966 u16 display_state:1;
1967 u16 brightness_toggle:1;
1968 u16 volume_toggle:1;
1969 u16 mute:1;
1970
1971 u8 brightness_level;
1972 u8 volume_level;
1973};
1974
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001975/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001976static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001977
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001978/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001979 * Acquire mutex to write poller control variables as an
1980 * atomic block.
1981 *
1982 * Increment hotkey_config_change when changing them if you
1983 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001984 *
1985 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1986 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001987static struct mutex hotkey_thread_data_mutex;
1988static unsigned int hotkey_config_change;
1989
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001990/*
1991 * hotkey poller control variables
1992 *
1993 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001994 *
1995 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1996 * should be used only when the changes need to be taken as
1997 * a block, OR when one needs to force the kthread to forget
1998 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001999 */
2000static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2001static unsigned int hotkey_poll_freq = 10; /* Hz */
2002
2003#define HOTKEY_CONFIG_CRITICAL_START \
2004 do { \
2005 mutex_lock(&hotkey_thread_data_mutex); \
2006 hotkey_config_change++; \
2007 } while (0);
2008#define HOTKEY_CONFIG_CRITICAL_END \
2009 mutex_unlock(&hotkey_thread_data_mutex);
2010
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002011#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2012
2013#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002014#define HOTKEY_CONFIG_CRITICAL_START
2015#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002016
2017#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2018
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002019static struct mutex hotkey_mutex;
2020
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002021static enum { /* Reasons for waking up */
2022 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2023 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2024 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2025} hotkey_wakeup_reason;
2026
2027static int hotkey_autosleep_ack;
2028
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002029static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2030static u32 hotkey_all_mask; /* all events supported in fw */
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002031static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002032static u32 hotkey_reserved_mask; /* events better left disabled */
2033static u32 hotkey_driver_mask; /* events needed by the driver */
2034static u32 hotkey_user_mask; /* events visible to userspace */
2035static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002036
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002037static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002038
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002039static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002040
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002041static void tpacpi_driver_event(const unsigned int hkey_event);
2042static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002043static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002044
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002045/* HKEY.MHKG() return bits */
2046#define TP_HOTKEY_TABLET_MASK (1 << 3)
Lyudeb03f4d42016-11-11 15:15:03 -05002047/* ThinkPad X1 Yoga (2016) */
2048#define TP_EC_CMMD_TABLET_MODE 0x6
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002049
Johannes Berg19d337d2009-06-02 13:01:37 +02002050static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002051{
Johannes Berg19d337d2009-06-02 13:01:37 +02002052 int status;
2053
2054 if (!tp_features.hotkey_wlsw)
2055 return -ENODEV;
2056
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002057#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002058 if (dbg_wlswemul)
2059 return (tpacpi_wlsw_emulstate) ?
2060 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002061#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002062
2063 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002064 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002065
2066 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002067}
2068
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002069static int hotkey_get_tablet_mode(int *status)
2070{
2071 int s;
2072
Lyudeb03f4d42016-11-11 15:15:03 -05002073 switch (tp_features.hotkey_tablet) {
2074 case TP_HOTKEY_TABLET_USES_MHKG:
2075 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2076 return -EIO;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002077
Lyudeb03f4d42016-11-11 15:15:03 -05002078 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2079 break;
2080 case TP_HOTKEY_TABLET_USES_CMMD:
2081 if (!acpi_evalf(ec_handle, &s, "CMMD", "d"))
2082 return -EIO;
2083
2084 *status = (s == TP_EC_CMMD_TABLET_MODE);
2085 break;
2086 default:
2087 break;
2088 }
2089
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002090 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002091}
2092
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002093/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002094 * Reads current event mask from firmware, and updates
2095 * hotkey_acpi_mask accordingly. Also resets any bits
2096 * from hotkey_user_mask that are unavailable to be
2097 * delivered (shadow requirement of the userspace ABI).
2098 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002099 * Call with hotkey_mutex held
2100 */
2101static int hotkey_mask_get(void)
2102{
2103 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002104 u32 m = 0;
2105
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002106 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002107 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002108
2109 hotkey_acpi_mask = m;
2110 } else {
2111 /* no mask support doesn't mean no event support... */
2112 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002113 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002114
2115 /* sync userspace-visible mask */
2116 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002117
2118 return 0;
2119}
2120
Jean Delvaredf6dd1b2015-04-27 09:45:06 +02002121static void hotkey_mask_warn_incomplete_mask(void)
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002122{
2123 /* log only what the user can fix... */
2124 const u32 wantedmask = hotkey_driver_mask &
2125 ~(hotkey_acpi_mask | hotkey_source_mask) &
2126 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2127
2128 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002129 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002130}
2131
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002132/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002133 * Set the firmware mask when supported
2134 *
2135 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2136 *
2137 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2138 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002139 * Call with hotkey_mutex held
2140 */
2141static int hotkey_mask_set(u32 mask)
2142{
2143 int i;
2144 int rc = 0;
2145
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002146 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002147
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002148 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002149 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002150 if (!acpi_evalf(hkey_handle,
2151 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002152 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002153 rc = -EIO;
2154 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002155 }
2156 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002157 }
2158
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002159 /*
2160 * We *must* make an inconditional call to hotkey_mask_get to
2161 * refresh hotkey_acpi_mask and update hotkey_user_mask
2162 *
2163 * Take the opportunity to also log when we cannot _enable_
2164 * a given event.
2165 */
2166 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002167 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002168 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002169 }
2170
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002171 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2172 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002173
2174 return rc;
2175}
2176
2177/*
2178 * Sets hotkey_user_mask and tries to set the firmware mask
2179 *
2180 * Call with hotkey_mutex held
2181 */
2182static int hotkey_user_mask_set(const u32 mask)
2183{
2184 int rc;
2185
2186 /* Give people a chance to notice they are doing something that
2187 * is bound to go boom on their users sooner or later */
2188 if (!tp_warned.hotkey_mask_ff &&
2189 (mask == 0xffff || mask == 0xffffff ||
2190 mask == 0xffffffff)) {
2191 tp_warned.hotkey_mask_ff = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002192 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2193 mask);
2194 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 -03002195 }
2196
2197 /* Try to enable what the user asked for, plus whatever we need.
2198 * this syncs everything but won't enable bits in hotkey_user_mask */
2199 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2200
2201 /* Enable the available bits in hotkey_user_mask */
2202 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2203
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002204 return rc;
2205}
2206
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002207/*
2208 * Sets the driver hotkey mask.
2209 *
2210 * Can be called even if the hotkey subdriver is inactive
2211 */
2212static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2213{
2214 int rc;
2215
2216 /* Do the right thing if hotkey_init has not been called yet */
2217 if (!tp_features.hotkey) {
2218 hotkey_driver_mask = mask;
2219 return 0;
2220 }
2221
2222 mutex_lock(&hotkey_mutex);
2223
2224 HOTKEY_CONFIG_CRITICAL_START
2225 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002226#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002227 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002228#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002229 HOTKEY_CONFIG_CRITICAL_END
2230
2231 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2232 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002233 hotkey_poll_setup(true);
2234
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002235 mutex_unlock(&hotkey_mutex);
2236
2237 return rc;
2238}
2239
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002240static int hotkey_status_get(int *status)
2241{
2242 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2243 return -EIO;
2244
2245 return 0;
2246}
2247
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002248static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002249{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002250 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002251 return -EIO;
2252
2253 return 0;
2254}
2255
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002256static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002257{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002258 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002259
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002260 if (tp_features.hotkey_tablet &&
2261 !hotkey_get_tablet_mode(&state)) {
2262 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002263
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002264 input_report_switch(tpacpi_inputdev,
2265 SW_TABLET_MODE, !!state);
2266 input_sync(tpacpi_inputdev);
2267
2268 mutex_unlock(&tpacpi_inputdev_send_mutex);
2269 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002270}
2271
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002272/* Do NOT call without validating scancode first */
2273static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002274{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002275 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002276
2277 if (keycode != KEY_RESERVED) {
2278 mutex_lock(&tpacpi_inputdev_send_mutex);
2279
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002280 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002281 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002282 input_sync(tpacpi_inputdev);
2283
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002284 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002285 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002286 input_sync(tpacpi_inputdev);
2287
2288 mutex_unlock(&tpacpi_inputdev_send_mutex);
2289 }
2290}
2291
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002292/* Do NOT call without validating scancode first */
2293static void tpacpi_input_send_key_masked(const unsigned int scancode)
2294{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002295 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002296 if (hotkey_user_mask & (1 << scancode))
2297 tpacpi_input_send_key(scancode);
2298}
2299
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002300#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2301static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2302
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002303/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002304static void tpacpi_hotkey_send_key(unsigned int scancode)
2305{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002306 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002307}
2308
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002309static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002310{
2311 u8 d;
2312
2313 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2314 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2315 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2316 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2317 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2318 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2319 }
Hans de Goedec685e202017-02-09 16:44:13 +01002320 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002321 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2322 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2323 }
2324 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2325 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2326 n->displayexp_toggle =
2327 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2328 }
2329 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2330 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2331 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2332 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2333 n->brightness_toggle =
2334 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2335 }
2336 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2337 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2338 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2339 >> TP_NVRAM_POS_LEVEL_VOLUME;
2340 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2341 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2342 }
2343}
2344
Behan Webstera4d44ba2014-02-12 21:58:46 +01002345#define TPACPI_COMPARE_KEY(__scancode, __member) \
2346do { \
2347 if ((event_mask & (1 << __scancode)) && \
2348 oldn->__member != newn->__member) \
2349 tpacpi_hotkey_send_key(__scancode); \
2350} while (0)
2351
2352#define TPACPI_MAY_SEND_KEY(__scancode) \
2353do { \
2354 if (event_mask & (1 << __scancode)) \
2355 tpacpi_hotkey_send_key(__scancode); \
2356} while (0)
2357
2358static void issue_volchange(const unsigned int oldvol,
2359 const unsigned int newvol,
2360 const u32 event_mask)
2361{
2362 unsigned int i = oldvol;
2363
2364 while (i > newvol) {
2365 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2366 i--;
2367 }
2368 while (i < newvol) {
2369 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2370 i++;
2371 }
2372}
2373
2374static void issue_brightnesschange(const unsigned int oldbrt,
2375 const unsigned int newbrt,
2376 const u32 event_mask)
2377{
2378 unsigned int i = oldbrt;
2379
2380 while (i > newbrt) {
2381 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2382 i--;
2383 }
2384 while (i < newbrt) {
2385 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2386 i++;
2387 }
2388}
2389
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002390static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2391 struct tp_nvram_state *newn,
2392 const u32 event_mask)
2393{
2394
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002395 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2396 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2397 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2398 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2399
2400 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2401
2402 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2403
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002404 /*
2405 * Handle volume
2406 *
2407 * This code is supposed to duplicate the IBM firmware behaviour:
2408 * - Pressing MUTE issues mute hotkey message, even when already mute
2409 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002410 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002411 * - The act of unmuting issues volume up/down notification,
2412 * depending which key was used to unmute
2413 *
2414 * We are constrained to what the NVRAM can tell us, which is not much
2415 * and certainly not enough if more than one volume hotkey was pressed
2416 * since the last poll cycle.
2417 *
2418 * Just to make our life interesting, some newer Lenovo ThinkPads have
2419 * bugs in the BIOS and may fail to update volume_toggle properly.
2420 */
2421 if (newn->mute) {
2422 /* muted */
2423 if (!oldn->mute ||
2424 oldn->volume_toggle != newn->volume_toggle ||
2425 oldn->volume_level != newn->volume_level) {
2426 /* recently muted, or repeated mute keypress, or
2427 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002428 issue_volchange(oldn->volume_level, newn->volume_level,
2429 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002430 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2431 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002432 } else {
2433 /* unmute */
2434 if (oldn->mute) {
2435 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002437 }
2438 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002439 issue_volchange(oldn->volume_level, newn->volume_level,
2440 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002441 } else if (oldn->volume_toggle != newn->volume_toggle) {
2442 /* repeated vol up/down keypress at end of scale ? */
2443 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002444 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002445 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2446 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002447 }
2448 }
2449
2450 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002451 if (oldn->brightness_level != newn->brightness_level) {
2452 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002453 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002454 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2455 /* repeated key presses that didn't change state */
2456 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002457 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002458 else if (newn->brightness_level >= bright_maxlvl
2459 && !tp_features.bright_unkfw)
2460 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002461 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002462
2463#undef TPACPI_COMPARE_KEY
2464#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002465}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002466
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002467/*
2468 * Polling driver
2469 *
2470 * We track all events in hotkey_source_mask all the time, since
2471 * most of them are edge-based. We only issue those requested by
2472 * hotkey_user_mask or hotkey_driver_mask, though.
2473 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002474static int hotkey_kthread(void *data)
2475{
2476 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002477 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002478 unsigned int si, so;
2479 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002480 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002481 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002482 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002483
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002484 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2485 goto exit;
2486
2487 set_freezable();
2488
2489 so = 0;
2490 si = 1;
2491 t = 0;
2492
2493 /* Initial state for compares */
2494 mutex_lock(&hotkey_thread_data_mutex);
2495 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002496 poll_mask = hotkey_source_mask;
2497 event_mask = hotkey_source_mask &
2498 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002499 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002500 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002501 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002502
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002503 while (!kthread_should_stop()) {
2504 if (t == 0) {
2505 if (likely(poll_freq))
2506 t = 1000/poll_freq;
2507 else
2508 t = 100; /* should never happen... */
2509 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002510 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002511 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002512 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002513
2514 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002515 continue;
2516
2517 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002518 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002519 /* forget old state on thaw or config change */
2520 si = so;
2521 t = 0;
2522 change_detector = hotkey_config_change;
2523 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002524 poll_mask = hotkey_source_mask;
2525 event_mask = hotkey_source_mask &
2526 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002527 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002528 mutex_unlock(&hotkey_thread_data_mutex);
2529
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002530 if (likely(poll_mask)) {
2531 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002532 if (likely(si != so)) {
2533 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002534 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002535 }
2536 }
2537
2538 so = si;
2539 si ^= 1;
2540 }
2541
2542exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002543 return 0;
2544}
2545
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002546/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002547static void hotkey_poll_stop_sync(void)
2548{
2549 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002550 kthread_stop(tpacpi_hotkey_task);
2551 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002552 }
2553}
2554
2555/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002556static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002557{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002558 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2559 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002560
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002561 if (hotkey_poll_freq > 0 &&
2562 (poll_driver_mask ||
2563 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002564 if (!tpacpi_hotkey_task) {
2565 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002566 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002567 if (IS_ERR(tpacpi_hotkey_task)) {
2568 tpacpi_hotkey_task = NULL;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002569 pr_err("could not create kernel thread for hotkey polling\n");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002570 }
2571 }
2572 } else {
2573 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002574 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002575 hotkey_poll_freq == 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002576 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 -07002577 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002578 }
2579 }
2580}
2581
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002582static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002583{
2584 mutex_lock(&hotkey_mutex);
2585 hotkey_poll_setup(may_warn);
2586 mutex_unlock(&hotkey_mutex);
2587}
2588
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002589/* call with hotkey_mutex held */
2590static void hotkey_poll_set_freq(unsigned int freq)
2591{
2592 if (!freq)
2593 hotkey_poll_stop_sync();
2594
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002595 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002596}
2597
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002598#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2599
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002600static void hotkey_poll_setup(const bool __unused)
2601{
2602}
2603
2604static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002605{
2606}
2607
2608#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2609
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002610static int hotkey_inputdev_open(struct input_dev *dev)
2611{
2612 switch (tpacpi_lifecycle) {
2613 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002614 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002615 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002616 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002617 case TPACPI_LIFE_EXITING:
2618 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002619 }
2620
2621 /* Should only happen if tpacpi_lifecycle is corrupt */
2622 BUG();
2623 return -EBUSY;
2624}
2625
2626static void hotkey_inputdev_close(struct input_dev *dev)
2627{
2628 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002629 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002630 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002631 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002632}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002633
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002634/* sysfs hotkey enable ------------------------------------------------- */
2635static ssize_t hotkey_enable_show(struct device *dev,
2636 struct device_attribute *attr,
2637 char *buf)
2638{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002639 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002640
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002641 printk_deprecated_attribute("hotkey_enable",
2642 "Hotkey reporting is always enabled");
2643
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002644 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002645 if (res)
2646 return res;
2647
2648 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2649}
2650
2651static ssize_t hotkey_enable_store(struct device *dev,
2652 struct device_attribute *attr,
2653 const char *buf, size_t count)
2654{
2655 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002656
2657 printk_deprecated_attribute("hotkey_enable",
2658 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002659
2660 if (parse_strtoul(buf, 1, &t))
2661 return -EINVAL;
2662
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002663 if (t == 0)
2664 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002665
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002666 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002667}
2668
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002669static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002670
2671/* sysfs hotkey mask --------------------------------------------------- */
2672static ssize_t hotkey_mask_show(struct device *dev,
2673 struct device_attribute *attr,
2674 char *buf)
2675{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002676 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002677}
2678
2679static ssize_t hotkey_mask_store(struct device *dev,
2680 struct device_attribute *attr,
2681 const char *buf, size_t count)
2682{
2683 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002684 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002685
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002686 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002687 return -EINVAL;
2688
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002689 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002690 return -ERESTARTSYS;
2691
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002692 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002693
2694#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002695 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002696#endif
2697
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002698 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002699
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002700 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2701
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002702 return (res) ? res : count;
2703}
2704
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002705static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002706
2707/* sysfs hotkey bios_enabled ------------------------------------------- */
2708static ssize_t hotkey_bios_enabled_show(struct device *dev,
2709 struct device_attribute *attr,
2710 char *buf)
2711{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002712 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002713}
2714
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002715static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002716
2717/* sysfs hotkey bios_mask ---------------------------------------------- */
2718static ssize_t hotkey_bios_mask_show(struct device *dev,
2719 struct device_attribute *attr,
2720 char *buf)
2721{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002722 printk_deprecated_attribute("hotkey_bios_mask",
2723 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002724 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002725}
2726
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002727static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002728
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002729/* sysfs hotkey all_mask ----------------------------------------------- */
2730static ssize_t hotkey_all_mask_show(struct device *dev,
2731 struct device_attribute *attr,
2732 char *buf)
2733{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002734 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2735 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002736}
2737
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002738static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002739
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002740/* sysfs hotkey all_mask ----------------------------------------------- */
2741static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2742 struct device_attribute *attr,
2743 char *buf)
2744{
2745 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2746 hotkey_adaptive_all_mask | hotkey_source_mask);
2747}
2748
2749static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2750
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002751/* sysfs hotkey recommended_mask --------------------------------------- */
2752static ssize_t hotkey_recommended_mask_show(struct device *dev,
2753 struct device_attribute *attr,
2754 char *buf)
2755{
2756 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002757 (hotkey_all_mask | hotkey_source_mask)
2758 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002759}
2760
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002761static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002762
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002763#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2764
2765/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2766static ssize_t hotkey_source_mask_show(struct device *dev,
2767 struct device_attribute *attr,
2768 char *buf)
2769{
2770 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2771}
2772
2773static ssize_t hotkey_source_mask_store(struct device *dev,
2774 struct device_attribute *attr,
2775 const char *buf, size_t count)
2776{
2777 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002778 u32 r_ev;
2779 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002780
2781 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2782 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2783 return -EINVAL;
2784
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002785 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002786 return -ERESTARTSYS;
2787
2788 HOTKEY_CONFIG_CRITICAL_START
2789 hotkey_source_mask = t;
2790 HOTKEY_CONFIG_CRITICAL_END
2791
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002792 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2793 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002794 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002795
2796 /* check if events needed by the driver got disabled */
2797 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2798 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002799
2800 mutex_unlock(&hotkey_mutex);
2801
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002802 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002803 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002804
2805 if (r_ev)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002806 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002807 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002808
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002809 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2810
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002811 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002812}
2813
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002814static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002815
2816/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2817static ssize_t hotkey_poll_freq_show(struct device *dev,
2818 struct device_attribute *attr,
2819 char *buf)
2820{
2821 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2822}
2823
2824static ssize_t hotkey_poll_freq_store(struct device *dev,
2825 struct device_attribute *attr,
2826 const char *buf, size_t count)
2827{
2828 unsigned long t;
2829
2830 if (parse_strtoul(buf, 25, &t))
2831 return -EINVAL;
2832
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002833 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002834 return -ERESTARTSYS;
2835
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002836 hotkey_poll_set_freq(t);
2837 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002838
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002839 mutex_unlock(&hotkey_mutex);
2840
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002841 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2842
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002843 return count;
2844}
2845
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002846static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002847
2848#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2849
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002850/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002851static ssize_t hotkey_radio_sw_show(struct device *dev,
2852 struct device_attribute *attr,
2853 char *buf)
2854{
Johannes Berg19d337d2009-06-02 13:01:37 +02002855 int res;
2856 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002857 if (res < 0)
2858 return res;
2859
Johannes Berg19d337d2009-06-02 13:01:37 +02002860 /* Opportunistic update */
2861 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2862
2863 return snprintf(buf, PAGE_SIZE, "%d\n",
2864 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002865}
2866
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002867static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002868
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002869static void hotkey_radio_sw_notify_change(void)
2870{
2871 if (tp_features.hotkey_wlsw)
2872 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2873 "hotkey_radio_sw");
2874}
2875
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002876/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2877static ssize_t hotkey_tablet_mode_show(struct device *dev,
2878 struct device_attribute *attr,
2879 char *buf)
2880{
2881 int res, s;
2882 res = hotkey_get_tablet_mode(&s);
2883 if (res < 0)
2884 return res;
2885
2886 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2887}
2888
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002889static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002890
2891static void hotkey_tablet_mode_notify_change(void)
2892{
2893 if (tp_features.hotkey_tablet)
2894 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2895 "hotkey_tablet_mode");
2896}
2897
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002898/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002899static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2900 struct device_attribute *attr,
2901 char *buf)
2902{
2903 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2904}
2905
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002906static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002907
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002908static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002909{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002910 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2911 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002912}
2913
2914/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002915static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2916 struct device_attribute *attr,
2917 char *buf)
2918{
2919 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2920}
2921
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002922static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2923 hotkey_wakeup_hotunplug_complete_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002924
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002925static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002926{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002927 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2928 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002929}
2930
Bastien Nocerab790cee2015-03-02 14:45:27 +01002931/* sysfs adaptive kbd mode --------------------------------------------- */
2932
2933static int adaptive_keyboard_get_mode(void);
2934static int adaptive_keyboard_set_mode(int new_mode);
2935
2936enum ADAPTIVE_KEY_MODE {
2937 HOME_MODE,
2938 WEB_BROWSER_MODE,
2939 WEB_CONFERENCE_MODE,
2940 FUNCTION_MODE,
2941 LAYFLAT_MODE
2942};
2943
2944static ssize_t adaptive_kbd_mode_show(struct device *dev,
2945 struct device_attribute *attr,
2946 char *buf)
2947{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03002948 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01002949
2950 current_mode = adaptive_keyboard_get_mode();
2951 if (current_mode < 0)
2952 return current_mode;
2953
2954 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
2955}
2956
2957static ssize_t adaptive_kbd_mode_store(struct device *dev,
2958 struct device_attribute *attr,
2959 const char *buf, size_t count)
2960{
2961 unsigned long t;
2962 int res;
2963
2964 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2965 return -EINVAL;
2966
2967 res = adaptive_keyboard_set_mode(t);
2968 return (res < 0) ? res : count;
2969}
2970
2971static DEVICE_ATTR_RW(adaptive_kbd_mode);
2972
2973static struct attribute *adaptive_kbd_attributes[] = {
2974 &dev_attr_adaptive_kbd_mode.attr,
2975 NULL
2976};
2977
2978static const struct attribute_group adaptive_kbd_attr_group = {
2979 .attrs = adaptive_kbd_attributes,
2980};
2981
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002982/* --------------------------------------------------------------------- */
2983
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002984static struct attribute *hotkey_attributes[] __initdata = {
2985 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002986 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002987 &dev_attr_hotkey_bios_mask.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002988 &dev_attr_wakeup_reason.attr,
2989 &dev_attr_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002990 &dev_attr_hotkey_mask.attr,
2991 &dev_attr_hotkey_all_mask.attr,
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002992 &dev_attr_hotkey_adaptive_all_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002993 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002994#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002995 &dev_attr_hotkey_source_mask.attr,
2996 &dev_attr_hotkey_poll_freq.attr,
2997#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002998};
2999
Johannes Berg19d337d2009-06-02 13:01:37 +02003000/*
3001 * Sync both the hw and sw blocking state of all switches
3002 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003003static void tpacpi_send_radiosw_update(void)
3004{
3005 int wlsw;
3006
Johannes Berg19d337d2009-06-02 13:01:37 +02003007 /*
3008 * We must sync all rfkill controllers *before* issuing any
3009 * rfkill input events, or we will race the rfkill core input
3010 * handler.
3011 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003012 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003013 * for the above.
3014 *
3015 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3016 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003017
Johannes Berg19d337d2009-06-02 13:01:37 +02003018 wlsw = hotkey_get_wlsw();
3019
3020 /* Sync hw blocking state first if it is hw-blocked */
3021 if (wlsw == TPACPI_RFK_RADIO_OFF)
3022 tpacpi_rfk_update_hwblock_state(true);
3023
3024 /* Sync sw blocking state */
3025 tpacpi_rfk_update_swstate_all();
3026
3027 /* Sync hw blocking state last if it is hw-unblocked */
3028 if (wlsw == TPACPI_RFK_RADIO_ON)
3029 tpacpi_rfk_update_hwblock_state(false);
3030
3031 /* Issue rfkill input event for WLSW switch */
3032 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003033 mutex_lock(&tpacpi_inputdev_send_mutex);
3034
3035 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003036 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003037 input_sync(tpacpi_inputdev);
3038
3039 mutex_unlock(&tpacpi_inputdev_send_mutex);
3040 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003041
3042 /*
3043 * this can be unconditional, as we will poll state again
3044 * if userspace uses the notify to read data
3045 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003046 hotkey_radio_sw_notify_change();
3047}
3048
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003049static void hotkey_exit(void)
3050{
3051#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003052 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003053 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003054 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003055#endif
3056
3057 if (hotkey_dev_attributes)
3058 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3059
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003060 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003061 "restoring original HKEY status and mask\n");
3062 /* yes, there is a bitwise or below, we want the
3063 * functions to be called even if one of them fail */
3064 if (((tp_features.hotkey_mask &&
3065 hotkey_mask_set(hotkey_orig_mask)) |
3066 hotkey_status_set(false)) != 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003067 pr_err("failed to restore hot key mask to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003068}
3069
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003070static void __init hotkey_unmap(const unsigned int scancode)
3071{
3072 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3073 clear_bit(hotkey_keycode_map[scancode],
3074 tpacpi_inputdev->keybit);
3075 hotkey_keycode_map[scancode] = KEY_RESERVED;
3076 }
3077}
3078
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003079/*
3080 * HKEY quirks:
3081 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3082 */
3083
3084#define TPACPI_HK_Q_INIMASK 0x0001
3085
3086static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3087 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3088 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3089 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3090 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3091 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3092 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3093 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3094 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3095 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3096 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3097 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3098 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3099 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3100 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3101 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3102 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3103 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3104 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3105 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3106};
3107
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003108typedef u16 tpacpi_keymap_entry_t;
3109typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003110
Lyudeb3180022016-11-11 15:15:02 -05003111static int hotkey_init_tablet_mode(void)
3112{
Darren Hartcb2bf252016-12-14 14:45:48 -08003113 int in_tablet_mode = 0, res;
3114 char *type = NULL;
Lyudeb3180022016-11-11 15:15:02 -05003115
Lyudeb3180022016-11-11 15:15:02 -05003116 if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
Lyudeb03f4d42016-11-11 15:15:03 -05003117 /* For X41t, X60t, X61t Tablets... */
Lyudeb3180022016-11-11 15:15:02 -05003118 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3119 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3120 type = "MHKG";
Lyudeb03f4d42016-11-11 15:15:03 -05003121 } else if (acpi_evalf(ec_handle, &res, "CMMD", "qd")) {
3122 /* For X1 Yoga (2016) */
3123 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_CMMD;
3124 in_tablet_mode = res == TP_EC_CMMD_TABLET_MODE;
3125 type = "CMMD";
Lyudeb3180022016-11-11 15:15:02 -05003126 }
3127
3128 if (!tp_features.hotkey_tablet)
3129 return 0;
3130
3131 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3132 type, in_tablet_mode ? "tablet" : "laptop");
3133
3134 res = add_to_attr_set(hotkey_dev_attributes,
3135 &dev_attr_hotkey_tablet_mode.attr);
3136 if (res)
3137 return -1;
3138
3139 return in_tablet_mode;
3140}
3141
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003142static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003143{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003144 /* Requirements for changing the default keymaps:
3145 *
3146 * 1. Many of the keys are mapped to KEY_RESERVED for very
3147 * good reasons. Do not change them unless you have deep
3148 * knowledge on the IBM and Lenovo ThinkPad firmware for
3149 * the various ThinkPad models. The driver behaves
3150 * differently for KEY_RESERVED: such keys have their
3151 * hot key mask *unset* in mask_recommended, and also
3152 * in the initial hot key mask programmed into the
3153 * firmware at driver load time, which means the firm-
3154 * ware may react very differently if you change them to
3155 * something else;
3156 *
3157 * 2. You must be subscribed to the linux-thinkpad and
3158 * ibm-acpi-devel mailing lists, and you should read the
3159 * list archives since 2007 if you want to change the
3160 * keymaps. This requirement exists so that you will
3161 * know the past history of problems with the thinkpad-
3162 * acpi driver keymaps, and also that you will be
3163 * listening to any bug reports;
3164 *
3165 * 3. Do not send thinkpad-acpi specific patches directly to
3166 * for merging, *ever*. Send them to the linux-acpi
3167 * mailinglist for comments. Merging is to be done only
3168 * through acpi-test and the ACPI maintainer.
3169 *
3170 * If the above is too much to ask, don't change the keymap.
3171 * Ask the thinkpad-acpi maintainer to do it, instead.
3172 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003173
3174 enum keymap_index {
3175 TPACPI_KEYMAP_IBM_GENERIC = 0,
3176 TPACPI_KEYMAP_LENOVO_GENERIC,
3177 };
3178
3179 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3180 /* Generic keymap for IBM ThinkPads */
3181 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003182 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003183 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003184 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3185 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003186
3187 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003188 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3189 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3190 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003191
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003192 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003193 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003194 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003195
3196 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003197 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003198
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003199 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3200 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003201
3202 /* Volume: firmware always react to it and reprograms
3203 * the built-in *extra* mixer. Never map it to control
3204 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003205 KEY_RESERVED, /* 0x14: VOLUME UP */
3206 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3207 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003208
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003209 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003210
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003211 /* (assignments unknown, please report if found) */
3212 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3213 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003214
3215 /* No assignments, only used for Adaptive keyboards. */
3216 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3217 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3218 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3219 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3220 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Christian Kellner696c65232017-02-28 17:10:57 +01003221
3222 /* No assignment, used for newer Lenovo models */
3223 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3224 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3225 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3226 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3227 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3228 KEY_UNKNOWN, KEY_UNKNOWN
3229
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003230 },
3231
3232 /* Generic keymap for Lenovo ThinkPads */
3233 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003234 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3235 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003236 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003237 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003238
3239 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003240 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3241 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3242 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003243
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003244 /* These should be enabled --only-- when ACPI video
3245 * is disabled (i.e. in "vendor" mode), and are handled
3246 * in a special way by the init code */
3247 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3248 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003249
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003250 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003251
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003252 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3253 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003254
3255 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3256 * react to it and reprograms the built-in *extra* mixer.
3257 * Never map it to control another mixer by default.
3258 *
3259 * T60?, T61, R60?, R61: firmware and EC tries to send
3260 * these over the regular keyboard, so these are no-ops,
3261 * but there are still weird bugs re. MUTE, so do not
3262 * change unless you get test reports from all Lenovo
3263 * models. May cause the BIOS to interfere with the
3264 * HDA mixer.
3265 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003266 KEY_RESERVED, /* 0x14: VOLUME UP */
3267 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3268 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003269
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003270 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003271
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003272 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003273 KEY_UNKNOWN, KEY_UNKNOWN,
3274
3275 /*
3276 * The mic mute button only sends 0x1a. It does not
3277 * automatically mute the mic or change the mute light.
3278 */
3279 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3280
3281 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003282 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003283
3284 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003285 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003286
3287 /*
3288 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3289 * The first item in this list is the Mute button which is
3290 * emitted with 0x103 through
3291 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3292 * symbol is held.
3293 * We'll need to offset those by 0x20.
3294 */
3295 KEY_RESERVED, /* Mute held, 0x103 */
3296 KEY_BRIGHTNESS_MIN, /* Backlight off */
3297 KEY_RESERVED, /* Clipping tool */
3298 KEY_RESERVED, /* Cloud */
3299 KEY_RESERVED,
3300 KEY_VOICECOMMAND, /* Voice */
3301 KEY_RESERVED,
3302 KEY_RESERVED, /* Gestures */
3303 KEY_RESERVED,
3304 KEY_RESERVED,
3305 KEY_RESERVED,
3306 KEY_CONFIG, /* Settings */
3307 KEY_RESERVED, /* New tab */
3308 KEY_REFRESH, /* Reload */
3309 KEY_BACK, /* Back */
3310 KEY_RESERVED, /* Microphone down */
3311 KEY_RESERVED, /* Microphone up */
3312 KEY_RESERVED, /* Microphone cancellation */
3313 KEY_RESERVED, /* Camera mode */
3314 KEY_RESERVED, /* Rotate display, 0x116 */
Christian Kellner696c65232017-02-28 17:10:57 +01003315
3316 /*
3317 * These are found in 2017 models (e.g. T470s, X270).
3318 * The lowest known value is 0x311, which according to
3319 * the manual should launch a user defined favorite
3320 * application.
3321 *
3322 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3323 * corresponding to 0x34.
3324 */
3325
3326 /* (assignments unknown, please report if found) */
3327 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
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,
3332
3333 KEY_FAVORITES, /* Favorite app, 0x311 */
3334 KEY_RESERVED, /* Clipping tool */
3335 KEY_RESERVED,
3336 KEY_BLUETOOTH, /* Bluetooth */
3337 KEY_KEYBOARD /* Keyboard, 0x315 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003338 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003339 };
3340
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003341 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3342 /* Generic maps (fallback) */
3343 {
3344 .vendor = PCI_VENDOR_ID_IBM,
3345 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3346 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3347 },
3348 {
3349 .vendor = PCI_VENDOR_ID_LENOVO,
3350 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3351 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3352 },
3353 };
3354
3355#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003356#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003357
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003358 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003359 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003360 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003361 bool radiosw_state = false;
3362 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003363
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003364 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003365 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003366
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003367 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3368 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003369
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003370 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003371 BUG_ON(tpacpi_inputdev->open != NULL ||
3372 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003373
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003374 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003375 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003376
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003377#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003378 mutex_init(&hotkey_thread_data_mutex);
3379#endif
3380
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003381 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003382 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003383
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003384 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3385 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003386 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003387
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003388 if (!tp_features.hotkey)
3389 return 1;
3390
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003391 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3392 ARRAY_SIZE(tpacpi_hotkey_qtable));
3393
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003394 tpacpi_disable_brightness_delay();
3395
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003396 /* MUST have enough space for all attributes to be added to
3397 * hotkey_dev_attributes */
3398 hotkey_dev_attributes = create_attr_set(
3399 ARRAY_SIZE(hotkey_attributes) + 2,
3400 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003401 if (!hotkey_dev_attributes)
3402 return -ENOMEM;
3403 res = add_many_to_attr_set(hotkey_dev_attributes,
3404 hotkey_attributes,
3405 ARRAY_SIZE(hotkey_attributes));
3406 if (res)
3407 goto err_exit;
3408
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003409 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003410 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3411 for HKEY interface version 0x100 */
3412 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003413 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3414 "firmware HKEY interface version: 0x%x\n",
3415 hkeyv);
3416
3417 switch (hkeyv >> 8) {
3418 case 1:
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003419 /*
3420 * MHKV 0x100 in A31, R40, R40e,
3421 * T4x, X31, and later
3422 */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003423
3424 /* Paranoia check AND init hotkey_all_mask */
3425 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3426 "MHKA", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003427 pr_err("missing MHKA handler, please report this to %s\n",
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003428 TPACPI_MAIL);
3429 /* Fallback: pre-init for FN+F3,F4,F12 */
3430 hotkey_all_mask = 0x080cU;
3431 } else {
3432 tp_features.hotkey_mask = 1;
3433 }
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003434 break;
3435
3436 case 2:
3437 /*
3438 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3439 */
3440
3441 /* Paranoia check AND init hotkey_all_mask */
3442 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3443 "MHKA", "dd", 1)) {
3444 pr_err("missing MHKA handler, please report this to %s\n",
3445 TPACPI_MAIL);
3446 /* Fallback: pre-init for FN+F3,F4,F12 */
3447 hotkey_all_mask = 0x080cU;
3448 } else {
3449 tp_features.hotkey_mask = 1;
3450 }
3451
3452 /*
3453 * Check if we have an adaptive keyboard, like on the
3454 * Lenovo Carbon X1 2014 (2nd Gen).
3455 */
3456 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3457 "MHKA", "dd", 2)) {
3458 if (hotkey_adaptive_all_mask != 0) {
3459 tp_features.has_adaptive_kbd = true;
3460 res = sysfs_create_group(
3461 &tpacpi_pdev->dev.kobj,
3462 &adaptive_kbd_attr_group);
3463 if (res)
3464 goto err_exit;
3465 }
3466 } else {
3467 tp_features.has_adaptive_kbd = false;
3468 hotkey_adaptive_all_mask = 0x0U;
3469 }
3470 break;
3471
3472 default:
3473 pr_err("unknown version of the HKEY interface: 0x%x\n",
3474 hkeyv);
3475 pr_err("please report this to %s\n", TPACPI_MAIL);
3476 break;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003477 }
3478 }
3479
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003480 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3481 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003482 str_supported(tp_features.hotkey_mask));
3483
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003484 /* Init hotkey_all_mask if not initialized yet */
3485 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3486 (quirks & TPACPI_HK_Q_INIMASK))
3487 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003488
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003489 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003490 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003491 /* hotkey_source_mask *must* be zero for
3492 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003493 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003494 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003495 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003496
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003497 hotkey_orig_mask = hotkey_acpi_mask;
3498 } else {
3499 hotkey_orig_mask = hotkey_all_mask;
3500 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003501 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003502
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003503#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3504 if (dbg_wlswemul) {
3505 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003506 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003507 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003508 } else
3509#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003510 /* Not all thinkpads have a hardware radio switch */
3511 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3512 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003513 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003514 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003515 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003516 }
3517 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003518 res = add_to_attr_set(hotkey_dev_attributes,
3519 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003520
Lyudeb3180022016-11-11 15:15:02 -05003521 res = hotkey_init_tablet_mode();
3522 if (res < 0)
3523 goto err_exit;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003524
Lyudeb3180022016-11-11 15:15:02 -05003525 tabletsw_state = res;
3526
3527 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3528 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003529 if (res)
3530 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003531
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003532 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003533 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3534 GFP_KERNEL);
3535 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003536 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003537 res = -ENOMEM;
3538 goto err_exit;
3539 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003540
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003541 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3542 ARRAY_SIZE(tpacpi_keymap_qtable));
3543 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3544 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3545 "using keymap number %lu\n", keymap_id);
3546
3547 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3548 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003549
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003550 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003551 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3552 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3553 tpacpi_inputdev->keycode = hotkey_keycode_map;
3554 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3555 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003556 input_set_capability(tpacpi_inputdev, EV_KEY,
3557 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003558 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003559 if (i < sizeof(hotkey_reserved_mask)*8)
3560 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003561 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003562 }
3563
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003564 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003565 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003566 input_report_switch(tpacpi_inputdev,
3567 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003568 }
3569 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003570 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003571 input_report_switch(tpacpi_inputdev,
3572 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003573 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003574
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003575 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003576 * userspace. tpacpi_detect_brightness_capabilities() must have
3577 * been called before this point */
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02003578 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003579 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3580 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003581
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003582 /* Disable brightness up/down on Lenovo thinkpads when
3583 * ACPI is handling them, otherwise it is plain impossible
3584 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003585 hotkey_reserved_mask |=
3586 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3587 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003588 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3589 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003590 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003591
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003592#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003593 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3594 & ~hotkey_all_mask
3595 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003596
3597 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3598 "hotkey source mask 0x%08x, polling freq %u\n",
3599 hotkey_source_mask, hotkey_poll_freq);
3600#endif
3601
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003602 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3603 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003604 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003605 if (res) {
3606 hotkey_exit();
3607 return res;
3608 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003609 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3610 | hotkey_driver_mask)
3611 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003612 if (res < 0 && res != -ENXIO) {
3613 hotkey_exit();
3614 return res;
3615 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003616 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3617 & ~hotkey_reserved_mask;
3618 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3619 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3620 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003621
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003622 tpacpi_inputdev->open = &hotkey_inputdev_open;
3623 tpacpi_inputdev->close = &hotkey_inputdev_close;
3624
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003625 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003626
3627 return 0;
3628
3629err_exit:
3630 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
Bastien Nocerab790cee2015-03-02 14:45:27 +01003631 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3632 &adaptive_kbd_attr_group);
3633
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003634 hotkey_dev_attributes = NULL;
3635
Jan van den Berg72a979f2014-09-17 00:01:08 +02003636 return (res < 0) ? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003637}
3638
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003639/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3640 * mode, Web conference mode, Function mode and Lay-flat mode.
3641 * We support Home mode and Function mode currently.
3642 *
3643 * Will consider support rest of modes in future.
3644 *
3645 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003646static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003647 HOME_MODE,
3648/* WEB_BROWSER_MODE = 2,
3649 WEB_CONFERENCE_MODE = 3, */
3650 FUNCTION_MODE
3651};
3652
3653#define DFR_CHANGE_ROW 0x101
3654#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003655#define FIRST_ADAPTIVE_KEY 0x103
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003656
3657/* press Fn key a while second, it will switch to Function Mode. Then
3658 * release Fn key, previous mode be restored.
3659 */
3660static bool adaptive_keyboard_mode_is_saved;
3661static int adaptive_keyboard_prev_mode;
3662
Bastien Noceraf74587f2015-03-02 14:45:22 +01003663static int adaptive_keyboard_get_mode(void)
3664{
3665 int mode = 0;
3666
3667 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3668 pr_err("Cannot read adaptive keyboard mode\n");
3669 return -EIO;
3670 }
3671
3672 return mode;
3673}
3674
3675static int adaptive_keyboard_set_mode(int new_mode)
3676{
3677 if (new_mode < 0 ||
3678 new_mode > LAYFLAT_MODE)
3679 return -EINVAL;
3680
3681 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3682 pr_err("Cannot set adaptive keyboard mode\n");
3683 return -EIO;
3684 }
3685
3686 return 0;
3687}
3688
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003689static int adaptive_keyboard_get_next_mode(int mode)
3690{
3691 size_t i;
3692 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3693
3694 for (i = 0; i <= max_mode; i++) {
3695 if (adaptive_keyboard_modes[i] == mode)
3696 break;
3697 }
3698
3699 if (i >= max_mode)
3700 i = 0;
3701 else
3702 i++;
3703
3704 return adaptive_keyboard_modes[i];
3705}
3706
3707static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3708{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003709 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003710 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003711 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003712
3713 switch (scancode) {
3714 case DFR_CHANGE_ROW:
3715 if (adaptive_keyboard_mode_is_saved) {
3716 new_mode = adaptive_keyboard_prev_mode;
3717 adaptive_keyboard_mode_is_saved = false;
3718 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003719 current_mode = adaptive_keyboard_get_mode();
3720 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003721 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003722 new_mode = adaptive_keyboard_get_next_mode(
3723 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003724 }
3725
Bastien Noceraf74587f2015-03-02 14:45:22 +01003726 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003727 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003728
3729 return true;
3730
3731 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003732 current_mode = adaptive_keyboard_get_mode();
3733 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003734 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003735
Bastien Noceraf74587f2015-03-02 14:45:22 +01003736 adaptive_keyboard_prev_mode = current_mode;
3737 adaptive_keyboard_mode_is_saved = true;
3738
3739 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3740 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003741 return true;
3742
3743 default:
Dan Carpenter741d98c2015-03-11 12:36:07 +03003744 if (scancode < FIRST_ADAPTIVE_KEY ||
Christian Kellner696c65232017-02-28 17:10:57 +01003745 scancode >= FIRST_ADAPTIVE_KEY +
3746 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3747 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
Bastien Nocera6a68d852015-03-02 14:45:31 +01003748 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003749 scancode);
Bastien Nocera6a68d852015-03-02 14:45:31 +01003750 return false;
3751 }
Christian Kellner149c8c72017-02-28 17:10:56 +01003752 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3753 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
Bastien Nocera6a68d852015-03-02 14:45:31 +01003754 if (keycode != KEY_RESERVED) {
3755 mutex_lock(&tpacpi_inputdev_send_mutex);
3756
3757 input_report_key(tpacpi_inputdev, keycode, 1);
3758 input_sync(tpacpi_inputdev);
3759
3760 input_report_key(tpacpi_inputdev, keycode, 0);
3761 input_sync(tpacpi_inputdev);
3762
3763 mutex_unlock(&tpacpi_inputdev_send_mutex);
3764 }
3765 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003766 }
3767}
3768
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003769static bool hotkey_notify_hotkey(const u32 hkey,
3770 bool *send_acpi_ev,
3771 bool *ignore_acpi_ev)
3772{
3773 /* 0x1000-0x1FFF: key presses */
3774 unsigned int scancode = hkey & 0xfff;
3775 *send_acpi_ev = true;
3776 *ignore_acpi_ev = false;
3777
Christian Kellner696c65232017-02-28 17:10:57 +01003778 /*
3779 * Original events are in the 0x10XX range, the adaptive keyboard
3780 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3781 * models, additional keys are emitted through 0x13XX.
3782 */
3783 switch ((hkey >> 8) & 0xf) {
3784 case 0:
3785 if (scancode > 0 &&
3786 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3787 /* HKEY event 0x1001 is scancode 0x00 */
3788 scancode--;
3789 if (!(hotkey_source_mask & (1 << scancode))) {
3790 tpacpi_input_send_key_masked(scancode);
3791 *send_acpi_ev = false;
3792 } else {
3793 *ignore_acpi_ev = true;
3794 }
3795 return true;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003796 }
Christian Kellner696c65232017-02-28 17:10:57 +01003797 break;
3798
3799 case 1:
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003800 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Christian Kellner696c65232017-02-28 17:10:57 +01003801
3802 case 3:
3803 /* Extended keycodes start at 0x300 and our offset into the map
3804 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3805 * will be positive, but might not be in the correct range.
3806 */
3807 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3808 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3809 scancode < TPACPI_HOTKEY_MAP_LEN) {
3810 tpacpi_input_send_key(scancode);
3811 return true;
3812 }
3813 break;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003814 }
Christian Kellner696c65232017-02-28 17:10:57 +01003815
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003816 return false;
3817}
3818
3819static bool hotkey_notify_wakeup(const u32 hkey,
3820 bool *send_acpi_ev,
3821 bool *ignore_acpi_ev)
3822{
3823 /* 0x2000-0x2FFF: Wakeup reason */
3824 *send_acpi_ev = true;
3825 *ignore_acpi_ev = false;
3826
3827 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003828 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3829 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003830 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3831 *ignore_acpi_ev = true;
3832 break;
3833
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003834 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3835 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003836 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3837 *ignore_acpi_ev = true;
3838 break;
3839
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003840 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3841 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003842 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003843 /* how to auto-heal: */
3844 /* 2313: woke up from S3, go to S4/S5 */
3845 /* 2413: woke up from S4, go to S5 */
3846 break;
3847
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003848 default:
3849 return false;
3850 }
3851
3852 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003853 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003854 hotkey_wakeup_reason_notify_change();
3855 }
3856 return true;
3857}
3858
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003859static bool hotkey_notify_dockevent(const u32 hkey,
3860 bool *send_acpi_ev,
3861 bool *ignore_acpi_ev)
3862{
3863 /* 0x4000-0x4FFF: dock-related events */
3864 *send_acpi_ev = true;
3865 *ignore_acpi_ev = false;
3866
3867 switch (hkey) {
3868 case TP_HKEY_EV_UNDOCK_ACK:
3869 /* ACPI undock operation completed after wakeup */
3870 hotkey_autosleep_ack = 1;
3871 pr_info("undocked\n");
3872 hotkey_wakeup_hotunplug_complete_notify_change();
3873 return true;
3874
3875 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3876 pr_info("docked into hotplug port replicator\n");
3877 return true;
3878 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3879 pr_info("undocked from hotplug port replicator\n");
3880 return true;
3881
3882 default:
3883 return false;
3884 }
3885}
3886
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003887static bool hotkey_notify_usrevent(const u32 hkey,
3888 bool *send_acpi_ev,
3889 bool *ignore_acpi_ev)
3890{
3891 /* 0x5000-0x5FFF: human interface helpers */
3892 *send_acpi_ev = true;
3893 *ignore_acpi_ev = false;
3894
3895 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003896 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3897 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003898 return true;
3899
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003900 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3901 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003902 tpacpi_input_send_tabletsw();
3903 hotkey_tablet_mode_notify_change();
3904 *send_acpi_ev = false;
3905 return true;
3906
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003907 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3908 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3909 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003910 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003911 *ignore_acpi_ev = true;
3912 return true;
3913
3914 default:
3915 return false;
3916 }
3917}
3918
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003919static void thermal_dump_all_sensors(void);
3920
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003921static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003922 bool *send_acpi_ev,
3923 bool *ignore_acpi_ev)
3924{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003925 bool known = true;
3926
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003927 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003928 *send_acpi_ev = true;
3929 *ignore_acpi_ev = false;
3930
3931 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003932 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003933 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003934 /* recommended action: do nothing, we don't have
3935 * Lenovo ATM information */
3936 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003937 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003938 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003939 /* recommended action: warn user through gui */
3940 break;
3941 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003942 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003943 /* recommended action: immediate sleep/hibernate */
3944 break;
3945 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003946 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003947 /* recommended action: warn user through gui, that */
3948 /* some internal component is too hot */
3949 break;
3950 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003951 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003952 /* recommended action: immediate sleep/hibernate */
3953 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01003954 case TP_HKEY_EV_AC_CHANGED:
3955 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3956 * AC status changed; can be triggered by plugging or
3957 * unplugging AC adapter, docking or undocking. */
3958
3959 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003960
3961 case TP_HKEY_EV_KEY_NUMLOCK:
3962 case TP_HKEY_EV_KEY_FN:
Xavier Naveira67ab6242015-02-10 08:45:18 +01003963 case TP_HKEY_EV_KEY_FN_ESC:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003964 /* key press events, we just ignore them as long as the EC
3965 * is still reporting them in the normal keyboard stream */
3966 *send_acpi_ev = false;
3967 *ignore_acpi_ev = true;
3968 return true;
3969
Lyudeb03f4d42016-11-11 15:15:03 -05003970 case TP_HKEY_EV_TABLET_CHANGED:
3971 tpacpi_input_send_tabletsw();
3972 hotkey_tablet_mode_notify_change();
3973 *send_acpi_ev = false;
3974 break;
3975
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003976 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003977 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003978 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003979 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003980
3981 thermal_dump_all_sensors();
3982
3983 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003984}
3985
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003986static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3987{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003988 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003989 bool send_acpi_ev;
3990 bool ignore_acpi_ev;
3991 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003992
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003993 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07003994 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003995 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003996 acpi_bus_generate_netlink_event(
3997 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003998 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003999 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004000 return;
4001 }
4002
4003 while (1) {
4004 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07004005 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004006 return;
4007 }
4008
4009 if (hkey == 0) {
4010 /* queue empty */
4011 return;
4012 }
4013
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004014 send_acpi_ev = true;
4015 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004016
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004017 switch (hkey >> 12) {
4018 case 1:
4019 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004020 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4021 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004022 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004023 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004024 /* 0x2000-0x2FFF: Wakeup reason */
4025 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4026 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004027 break;
4028 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004029 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004030 switch (hkey) {
4031 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004032 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004033 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004034 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004035 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004036 break;
4037 case TP_HKEY_EV_OPTDRV_EJ:
4038 /* FIXME: kick libata if SATA link offline */
4039 known_ev = true;
4040 break;
4041 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004042 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004043 }
4044 break;
4045 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03004046 /* 0x4000-0x4FFF: dock-related events */
4047 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4048 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004049 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004050 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02004051 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004052 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4053 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004054 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004055 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004056 /* 0x6000-0x6FFF: thermal alarms/notices and
4057 * keyboard events */
4058 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004059 &ignore_acpi_ev);
4060 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004061 case 7:
4062 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004063 if (tp_features.hotkey_wlsw &&
4064 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004065 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004066 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004067 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004068 break;
4069 }
4070 /* fallthrough to default */
4071 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004072 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004073 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004074 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07004075 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004076 pr_notice("please report the conditions when this event happened to %s\n",
4077 TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004078 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004079
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004080 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03004081 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004082 acpi_bus_generate_netlink_event(
4083 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004084 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004085 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004086 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004087 }
4088}
4089
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02004090static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004091{
4092 /* Do these on suspend, we get the events on early resume! */
4093 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4094 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08004095
4096 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004097 if (tp_features.has_adaptive_kbd) {
4098 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4099 "GTRW", "dd", 0)) {
4100 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004101 }
4102 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004103}
4104
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004105static void hotkey_resume(void)
4106{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03004107 tpacpi_disable_brightness_delay();
4108
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004109 if (hotkey_status_set(true) < 0 ||
4110 hotkey_mask_set(hotkey_acpi_mask) < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004111 pr_err("error while attempting to reset the event firmware interface\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004112
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004113 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02004114 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004115 hotkey_wakeup_reason_notify_change();
4116 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004117 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004118
4119 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004120 if (tp_features.has_adaptive_kbd) {
4121 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4122 adaptive_keyboard_prev_mode)) {
4123 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004124 }
4125 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004126}
4127
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004128/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004129static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004131 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004133 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004134 seq_printf(m, "status:\t\tnot supported\n");
4135 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004136 }
4137
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004138 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004139 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004140 res = hotkey_status_get(&status);
4141 if (!res)
4142 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004143 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004144 if (res)
4145 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004147 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004148 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004149 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4150 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004152 seq_printf(m, "mask:\t\tnot supported\n");
4153 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 }
4155
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004156 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157}
4158
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004159static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004160{
4161 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004162 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004163 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4164 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 +00004165}
4166
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004167static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004169 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004170 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004173 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 return -ENODEV;
4175
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004176 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004177 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004178
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004179 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004180
4181 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 while ((cmd = next_cmd(&buf))) {
4183 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004184 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004186 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004187 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03004189 mask = (hotkey_all_mask | hotkey_source_mask)
4190 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4192 /* mask set */
4193 } else if (sscanf(cmd, "%x", &mask) == 1) {
4194 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004195 } else {
4196 res = -EINVAL;
4197 goto errexit;
4198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004200
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004201 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004202 tpacpi_disclose_usertask("procfs hotkey",
4203 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004204 res = hotkey_user_mask_set(mask);
4205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004207errexit:
4208 mutex_unlock(&hotkey_mutex);
4209 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004210}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004212static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004213 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4214 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Hui Wanga3c42a42016-11-08 16:13:23 +08004215 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004216 {"", 0},
4217};
4218
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004219static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004220 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004221 .notify = hotkey_notify,
4222 .handle = &hkey_handle,
4223 .type = ACPI_DEVICE_NOTIFY,
4224};
4225
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004226static struct ibm_struct hotkey_driver_data = {
4227 .name = "hotkey",
4228 .read = hotkey_read,
4229 .write = hotkey_write,
4230 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004231 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004232 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004233 .acpi = &ibm_hotkey_acpidriver,
4234};
4235
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004236/*************************************************************************
4237 * Bluetooth subdriver
4238 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004240enum {
4241 /* ACPI GBDC/SBDC bits */
4242 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4243 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004244 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004245 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004246};
4247
4248enum {
4249 /* ACPI \BLTH commands */
4250 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4251 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4252 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4253 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4254 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004255};
4256
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004257#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4258
Johannes Berg19d337d2009-06-02 13:01:37 +02004259static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004260{
4261 int status;
4262
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004263#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4264 if (dbg_bluetoothemul)
4265 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004266 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004267#endif
4268
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004269 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4270 return -EIO;
4271
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004272 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004273 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004274}
4275
Johannes Berg19d337d2009-06-02 13:01:37 +02004276static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004277{
4278 int status;
4279
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004280 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004281 "will attempt to %s bluetooth\n",
4282 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004283
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004284#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4285 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004286 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004287 return 0;
4288 }
4289#endif
4290
Johannes Berg19d337d2009-06-02 13:01:37 +02004291 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004292 status = TP_ACPI_BLUETOOTH_RADIOSSW
4293 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4294 else
4295 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004296
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004297 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4298 return -EIO;
4299
4300 return 0;
4301}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004302
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004303/* sysfs bluetooth enable ---------------------------------------------- */
4304static ssize_t bluetooth_enable_show(struct device *dev,
4305 struct device_attribute *attr,
4306 char *buf)
4307{
Johannes Berg19d337d2009-06-02 13:01:37 +02004308 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4309 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004310}
4311
4312static ssize_t bluetooth_enable_store(struct device *dev,
4313 struct device_attribute *attr,
4314 const char *buf, size_t count)
4315{
Johannes Berg19d337d2009-06-02 13:01:37 +02004316 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4317 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004318}
4319
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004320static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004321
4322/* --------------------------------------------------------------------- */
4323
4324static struct attribute *bluetooth_attributes[] = {
4325 &dev_attr_bluetooth_enable.attr,
4326 NULL
4327};
4328
4329static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004330 .attrs = bluetooth_attributes,
4331};
4332
Johannes Berg19d337d2009-06-02 13:01:37 +02004333static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4334 .get_status = bluetooth_get_status,
4335 .set_status = bluetooth_set_status,
4336};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004337
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004338static void bluetooth_shutdown(void)
4339{
4340 /* Order firmware to save current state to NVRAM */
4341 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4342 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004343 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004344 else
4345 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004346 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004347}
4348
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004349static void bluetooth_exit(void)
4350{
4351 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4352 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004353
4354 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4355
4356 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004357}
4358
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004359static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004361 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004362 int status = 0;
4363
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004364 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4365 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004366
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004367 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004368
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004369 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4370 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004371 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004372 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004374 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4375 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004376 str_supported(tp_features.bluetooth),
4377 status);
4378
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004379#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4380 if (dbg_bluetoothemul) {
4381 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004382 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004383 } else
4384#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004385 if (tp_features.bluetooth &&
4386 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4387 /* no bluetooth hardware present in system */
4388 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004389 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004390 "bluetooth hardware not installed\n");
4391 }
4392
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004393 if (!tp_features.bluetooth)
4394 return 1;
4395
Johannes Berg19d337d2009-06-02 13:01:37 +02004396 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4397 &bluetooth_tprfk_ops,
4398 RFKILL_TYPE_BLUETOOTH,
4399 TPACPI_RFK_BLUETOOTH_SW_NAME,
4400 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004401 if (res)
4402 return res;
4403
Johannes Berg19d337d2009-06-02 13:01:37 +02004404 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4405 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004406 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004407 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004408 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004409 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004410
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412}
4413
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004414/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004415static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004417 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418}
4419
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004420static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421{
Johannes Berg19d337d2009-06-02 13:01:37 +02004422 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423}
4424
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004425static struct ibm_struct bluetooth_driver_data = {
4426 .name = "bluetooth",
4427 .read = bluetooth_read,
4428 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004429 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004430 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004431};
4432
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004433/*************************************************************************
4434 * Wan subdriver
4435 */
4436
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004437enum {
4438 /* ACPI GWAN/SWAN bits */
4439 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4440 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004441 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004442 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004443};
4444
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004445#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4446
Johannes Berg19d337d2009-06-02 13:01:37 +02004447static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004448{
4449 int status;
4450
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004451#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4452 if (dbg_wwanemul)
4453 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004454 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004455#endif
4456
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004457 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4458 return -EIO;
4459
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004460 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004461 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004462}
4463
Johannes Berg19d337d2009-06-02 13:01:37 +02004464static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004465{
4466 int status;
4467
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004468 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004469 "will attempt to %s wwan\n",
4470 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004471
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004472#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4473 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004474 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004475 return 0;
4476 }
4477#endif
4478
Johannes Berg19d337d2009-06-02 13:01:37 +02004479 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004480 status = TP_ACPI_WANCARD_RADIOSSW
4481 | TP_ACPI_WANCARD_RESUMECTRL;
4482 else
4483 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004484
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004485 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4486 return -EIO;
4487
4488 return 0;
4489}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004490
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004491/* sysfs wan enable ---------------------------------------------------- */
4492static ssize_t wan_enable_show(struct device *dev,
4493 struct device_attribute *attr,
4494 char *buf)
4495{
Johannes Berg19d337d2009-06-02 13:01:37 +02004496 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4497 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004498}
4499
4500static ssize_t wan_enable_store(struct device *dev,
4501 struct device_attribute *attr,
4502 const char *buf, size_t count)
4503{
Johannes Berg19d337d2009-06-02 13:01:37 +02004504 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4505 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004506}
4507
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004508static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4509 wan_enable_show, wan_enable_store);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004510
4511/* --------------------------------------------------------------------- */
4512
4513static struct attribute *wan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004514 &dev_attr_wwan_enable.attr,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004515 NULL
4516};
4517
4518static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004519 .attrs = wan_attributes,
4520};
4521
Johannes Berg19d337d2009-06-02 13:01:37 +02004522static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4523 .get_status = wan_get_status,
4524 .set_status = wan_set_status,
4525};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004526
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004527static void wan_shutdown(void)
4528{
4529 /* Order firmware to save current state to NVRAM */
4530 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4531 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004532 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004533 else
4534 vdbg_printk(TPACPI_DBG_RFKILL,
4535 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004536}
4537
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004538static void wan_exit(void)
4539{
4540 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4541 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004542
4543 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4544
4545 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004546}
4547
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004548static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004549{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004550 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004551 int status = 0;
4552
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004553 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4554 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004555
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004556 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004557
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004558 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004559 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004560
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004561 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4562 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004563 str_supported(tp_features.wan),
4564 status);
4565
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004566#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4567 if (dbg_wwanemul) {
4568 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004569 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004570 } else
4571#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004572 if (tp_features.wan &&
4573 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4574 /* no wan hardware present in system */
4575 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004576 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004577 "wan hardware not installed\n");
4578 }
4579
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004580 if (!tp_features.wan)
4581 return 1;
4582
Johannes Berg19d337d2009-06-02 13:01:37 +02004583 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4584 &wan_tprfk_ops,
4585 RFKILL_TYPE_WWAN,
4586 TPACPI_RFK_WWAN_SW_NAME,
4587 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004588 if (res)
4589 return res;
4590
Johannes Berg19d337d2009-06-02 13:01:37 +02004591 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4592 &wan_attr_group);
4593
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004594 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004595 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004596 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004597 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004598
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004599 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004600}
4601
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004602/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004603static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004604{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004605 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004606}
4607
4608static int wan_write(char *buf)
4609{
Johannes Berg19d337d2009-06-02 13:01:37 +02004610 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004611}
4612
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004613static struct ibm_struct wan_driver_data = {
4614 .name = "wan",
4615 .read = wan_read,
4616 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004617 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004618 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004619};
4620
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004621/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004622 * UWB subdriver
4623 */
4624
4625enum {
4626 /* ACPI GUWB/SUWB bits */
4627 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4628 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4629};
4630
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004631#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4632
Johannes Berg19d337d2009-06-02 13:01:37 +02004633static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004634{
4635 int status;
4636
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004637#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4638 if (dbg_uwbemul)
4639 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004640 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004641#endif
4642
4643 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4644 return -EIO;
4645
4646 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004647 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004648}
4649
Johannes Berg19d337d2009-06-02 13:01:37 +02004650static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004651{
4652 int status;
4653
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004654 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004655 "will attempt to %s UWB\n",
4656 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004657
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004658#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4659 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004660 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004661 return 0;
4662 }
4663#endif
4664
Johannes Berg19d337d2009-06-02 13:01:37 +02004665 if (state == TPACPI_RFK_RADIO_ON)
4666 status = TP_ACPI_UWB_RADIOSSW;
4667 else
4668 status = 0;
4669
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004670 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4671 return -EIO;
4672
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004673 return 0;
4674}
4675
4676/* --------------------------------------------------------------------- */
4677
Johannes Berg19d337d2009-06-02 13:01:37 +02004678static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4679 .get_status = uwb_get_status,
4680 .set_status = uwb_set_status,
4681};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004682
4683static void uwb_exit(void)
4684{
Johannes Berg19d337d2009-06-02 13:01:37 +02004685 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004686}
4687
4688static int __init uwb_init(struct ibm_init_struct *iibm)
4689{
4690 int res;
4691 int status = 0;
4692
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004693 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4694 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004695
4696 TPACPI_ACPIHANDLE_INIT(hkey);
4697
4698 tp_features.uwb = hkey_handle &&
4699 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4700
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004701 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4702 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004703 str_supported(tp_features.uwb),
4704 status);
4705
4706#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4707 if (dbg_uwbemul) {
4708 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004709 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004710 } else
4711#endif
4712 if (tp_features.uwb &&
4713 !(status & TP_ACPI_UWB_HWPRESENT)) {
4714 /* no uwb hardware present in system */
4715 tp_features.uwb = 0;
4716 dbg_printk(TPACPI_DBG_INIT,
4717 "uwb hardware not installed\n");
4718 }
4719
4720 if (!tp_features.uwb)
4721 return 1;
4722
4723 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004724 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004725 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004726 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004727 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004728 return res;
4729}
4730
4731static struct ibm_struct uwb_driver_data = {
4732 .name = "uwb",
4733 .exit = uwb_exit,
4734 .flags.experimental = 1,
4735};
4736
4737/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004738 * Video subdriver
4739 */
4740
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004741#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4742
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004743enum video_access_mode {
4744 TPACPI_VIDEO_NONE = 0,
4745 TPACPI_VIDEO_570, /* 570 */
4746 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4747 TPACPI_VIDEO_NEW, /* all others */
4748};
4749
4750enum { /* video status flags, based on VIDEO_570 */
4751 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4752 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4753 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4754};
4755
4756enum { /* TPACPI_VIDEO_570 constants */
4757 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4758 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4759 * video_status_flags */
4760 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4761 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4762};
4763
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004764static enum video_access_mode video_supported;
4765static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004766
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004767static int video_autosw_get(void);
4768static int video_autosw_set(int enable);
4769
Joe Perches112a6ee2011-04-04 10:06:24 -07004770TPACPI_HANDLE(vid, root,
4771 "\\_SB.PCI.AGP.VGA", /* 570 */
4772 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4773 "\\_SB.PCI0.VID0", /* 770e */
4774 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4775 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4776 "\\_SB.PCI0.AGP.VID", /* all others */
4777 ); /* R30, R31 */
4778
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004779TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004780
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004781static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004783 int ivga;
4784
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004785 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4786
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004787 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004788 if (tpacpi_is_ibm())
4789 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004790
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004791 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4792 /* G41, assume IVGA doesn't change */
4793 vid_handle = vid2_handle;
4794
4795 if (!vid_handle)
4796 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004797 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004798 else if (tpacpi_is_ibm() &&
4799 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004800 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004801 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004802 else if (tpacpi_is_ibm() &&
4803 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004804 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004805 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004806 else
4807 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004808 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004810 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4811 str_supported(video_supported != TPACPI_VIDEO_NONE),
4812 video_supported);
4813
Jan van den Berg72a979f2014-09-17 00:01:08 +02004814 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815}
4816
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004817static void video_exit(void)
4818{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004819 dbg_printk(TPACPI_DBG_EXIT,
4820 "restoring original video autoswitch mode\n");
4821 if (video_autosw_set(video_orig_autosw))
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004822 pr_err("error while trying to restore original video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004823}
4824
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004825static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826{
4827 int status = 0;
4828 int i;
4829
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004830 switch (video_supported) {
4831 case TPACPI_VIDEO_570:
4832 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4833 TP_ACPI_VIDEO_570_PHSCMD))
4834 return -EIO;
4835 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4836 break;
4837 case TPACPI_VIDEO_770:
4838 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4839 return -EIO;
4840 if (i)
4841 status |= TP_ACPI_VIDEO_S_LCD;
4842 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4843 return -EIO;
4844 if (i)
4845 status |= TP_ACPI_VIDEO_S_CRT;
4846 break;
4847 case TPACPI_VIDEO_NEW:
4848 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4849 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4850 return -EIO;
4851 if (i)
4852 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004854 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4855 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4856 return -EIO;
4857 if (i)
4858 status |= TP_ACPI_VIDEO_S_LCD;
4859 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4860 return -EIO;
4861 if (i)
4862 status |= TP_ACPI_VIDEO_S_DVI;
4863 break;
4864 default:
4865 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
4868 return status;
4869}
4870
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004871static int video_outputsw_set(int status)
4872{
4873 int autosw;
4874 int res = 0;
4875
4876 switch (video_supported) {
4877 case TPACPI_VIDEO_570:
4878 res = acpi_evalf(NULL, NULL,
4879 "\\_SB.PHS2", "vdd",
4880 TP_ACPI_VIDEO_570_PHS2CMD,
4881 status | TP_ACPI_VIDEO_570_PHS2SET);
4882 break;
4883 case TPACPI_VIDEO_770:
4884 autosw = video_autosw_get();
4885 if (autosw < 0)
4886 return autosw;
4887
4888 res = video_autosw_set(1);
4889 if (res)
4890 return res;
4891 res = acpi_evalf(vid_handle, NULL,
4892 "ASWT", "vdd", status * 0x100, 0);
4893 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004894 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004895 return -EIO;
4896 }
4897 break;
4898 case TPACPI_VIDEO_NEW:
4899 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004900 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004901 break;
4902 default:
4903 return -ENOSYS;
4904 }
4905
Jan van den Berg72a979f2014-09-17 00:01:08 +02004906 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004907}
4908
4909static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004911 int autosw = 0;
4912
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004913 switch (video_supported) {
4914 case TPACPI_VIDEO_570:
4915 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4916 return -EIO;
4917 break;
4918 case TPACPI_VIDEO_770:
4919 case TPACPI_VIDEO_NEW:
4920 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4921 return -EIO;
4922 break;
4923 default:
4924 return -ENOSYS;
4925 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004926
4927 return autosw & 1;
4928}
4929
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004930static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004931{
Jan van den Berg72a979f2014-09-17 00:01:08 +02004932 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004933 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004934 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004935}
4936
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004937static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004938{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004939 int autosw = video_autosw_get();
4940 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004941
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004942 if (autosw < 0)
4943 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004944
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004945 switch (video_supported) {
4946 case TPACPI_VIDEO_570:
4947 res = video_autosw_set(1);
4948 if (res)
4949 return res;
4950 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4951 break;
4952 case TPACPI_VIDEO_770:
4953 case TPACPI_VIDEO_NEW:
4954 res = video_autosw_set(1);
4955 if (res)
4956 return res;
4957 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4958 break;
4959 default:
4960 return -ENOSYS;
4961 }
4962 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004963 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004964 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004965 }
4966
Jan van den Berg72a979f2014-09-17 00:01:08 +02004967 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004968}
4969
4970static int video_expand_toggle(void)
4971{
4972 switch (video_supported) {
4973 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004974 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004975 0 : -EIO;
4976 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004977 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004978 0 : -EIO;
4979 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004980 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004981 0 : -EIO;
4982 default:
4983 return -ENOSYS;
4984 }
4985 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004986}
4987
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004988static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004989{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004990 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004991
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004992 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004993 seq_printf(m, "status:\t\tnot supported\n");
4994 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004995 }
4996
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004997 /* Even reads can crash X.org, so... */
4998 if (!capable(CAP_SYS_ADMIN))
4999 return -EPERM;
5000
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005001 status = video_outputsw_get();
5002 if (status < 0)
5003 return status;
5004
5005 autosw = video_autosw_get();
5006 if (autosw < 0)
5007 return autosw;
5008
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005009 seq_printf(m, "status:\t\tsupported\n");
5010 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5011 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005012 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005013 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5014 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5015 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5016 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005017 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005018 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5019 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5020 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005021
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005022 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005023}
5024
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005025static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026{
5027 char *cmd;
5028 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005029 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005031 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005032 return -ENODEV;
5033
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005034 /* Even reads can crash X.org, let alone writes... */
5035 if (!capable(CAP_SYS_ADMIN))
5036 return -EPERM;
5037
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005038 enable = 0;
5039 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
5041 while ((cmd = next_cmd(&buf))) {
5042 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005043 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005045 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005047 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005049 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005050 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005051 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005052 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005053 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005054 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005055 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005057 res = video_autosw_set(1);
5058 if (res)
5059 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005061 res = video_autosw_set(0);
5062 if (res)
5063 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005065 res = video_outputsw_cycle();
5066 if (res)
5067 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005069 res = video_expand_toggle();
5070 if (res)
5071 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 } else
5073 return -EINVAL;
5074 }
5075
5076 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005077 status = video_outputsw_get();
5078 if (status < 0)
5079 return status;
5080 res = video_outputsw_set((status & ~disable) | enable);
5081 if (res)
5082 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 }
5084
5085 return 0;
5086}
5087
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005088static struct ibm_struct video_driver_data = {
5089 .name = "video",
5090 .read = video_read,
5091 .write = video_write,
5092 .exit = video_exit,
5093};
5094
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005095#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5096
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005097/*************************************************************************
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005098 * Keyboard backlight subdriver
5099 */
5100
Hans de Goedec685e202017-02-09 16:44:13 +01005101static enum led_brightness kbdlight_brightness;
5102static DEFINE_MUTEX(kbdlight_mutex);
5103
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005104static int kbdlight_set_level(int level)
5105{
Hans de Goedec685e202017-02-09 16:44:13 +01005106 int ret = 0;
5107
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005108 if (!hkey_handle)
5109 return -ENXIO;
5110
Hans de Goedec685e202017-02-09 16:44:13 +01005111 mutex_lock(&kbdlight_mutex);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005112
Hans de Goedec685e202017-02-09 16:44:13 +01005113 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5114 ret = -EIO;
5115 else
5116 kbdlight_brightness = level;
5117
5118 mutex_unlock(&kbdlight_mutex);
5119
5120 return ret;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005121}
5122
5123static int kbdlight_get_level(void)
5124{
5125 int status = 0;
5126
5127 if (!hkey_handle)
5128 return -ENXIO;
5129
5130 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5131 return -EIO;
5132
5133 if (status < 0)
5134 return status;
5135
5136 return status & 0x3;
5137}
5138
5139static bool kbdlight_is_supported(void)
5140{
5141 int status = 0;
5142
5143 if (!hkey_handle)
5144 return false;
5145
5146 if (!acpi_has_method(hkey_handle, "MLCG")) {
5147 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5148 return false;
5149 }
5150
5151 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5152 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5153 return false;
5154 }
5155
5156 if (status < 0) {
5157 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5158 return false;
5159 }
5160
5161 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5162 /*
5163 * Guessed test for keyboard backlight:
5164 *
5165 * Machines with backlight keyboard return:
5166 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5167 * b110100010010000000XX - ThinkPad x230
5168 * b010100000010000000XX - ThinkPad x240
5169 * b010100000010000000XX - ThinkPad W541
5170 * (XX is current backlight level)
5171 *
5172 * Machines without backlight keyboard return:
5173 * b10100001000000000000 - ThinkPad x230
5174 * b10110001000000000000 - ThinkPad E430
5175 * b00000000000000000000 - ThinkPad E450
5176 *
5177 * Candidate BITs for detection test (XOR):
5178 * b01000000001000000000
5179 * ^
5180 */
5181 return status & BIT(9);
5182}
5183
Hans de Goede86ec0c22017-02-09 16:44:12 +01005184static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005185 enum led_brightness brightness)
5186{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005187 return kbdlight_set_level(brightness);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005188}
5189
5190static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5191{
5192 int level;
5193
5194 level = kbdlight_get_level();
5195 if (level < 0)
5196 return 0;
5197
5198 return level;
5199}
5200
5201static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5202 .led_classdev = {
5203 .name = "tpacpi::kbd_backlight",
5204 .max_brightness = 2,
Hans de Goedec685e202017-02-09 16:44:13 +01005205 .flags = LED_BRIGHT_HW_CHANGED,
Hans de Goede86ec0c22017-02-09 16:44:12 +01005206 .brightness_set_blocking = &kbdlight_sysfs_set,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005207 .brightness_get = &kbdlight_sysfs_get,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005208 }
5209};
5210
5211static int __init kbdlight_init(struct ibm_init_struct *iibm)
5212{
5213 int rc;
5214
5215 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5216
5217 TPACPI_ACPIHANDLE_INIT(hkey);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005218
5219 if (!kbdlight_is_supported()) {
5220 tp_features.kbdlight = 0;
5221 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5222 return 1;
5223 }
5224
Hans de Goedec685e202017-02-09 16:44:13 +01005225 kbdlight_brightness = kbdlight_sysfs_get(NULL);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005226 tp_features.kbdlight = 1;
5227
5228 rc = led_classdev_register(&tpacpi_pdev->dev,
5229 &tpacpi_led_kbdlight.led_classdev);
5230 if (rc < 0) {
5231 tp_features.kbdlight = 0;
5232 return rc;
5233 }
5234
Hans de Goedec685e202017-02-09 16:44:13 +01005235 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5236 TP_ACPI_HKEY_KBD_LIGHT_MASK);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005237 return 0;
5238}
5239
5240static void kbdlight_exit(void)
5241{
5242 if (tp_features.kbdlight)
5243 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005244}
5245
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005246static int kbdlight_set_level_and_update(int level)
5247{
5248 int ret;
5249 struct led_classdev *led_cdev;
5250
5251 ret = kbdlight_set_level(level);
5252 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5253
5254 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5255 led_cdev->brightness = level;
5256
5257 return ret;
5258}
5259
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005260static int kbdlight_read(struct seq_file *m)
5261{
5262 int level;
5263
5264 if (!tp_features.kbdlight) {
5265 seq_printf(m, "status:\t\tnot supported\n");
5266 } else {
5267 level = kbdlight_get_level();
5268 if (level < 0)
5269 seq_printf(m, "status:\t\terror %d\n", level);
5270 else
5271 seq_printf(m, "status:\t\t%d\n", level);
5272 seq_printf(m, "commands:\t0, 1, 2\n");
5273 }
5274
5275 return 0;
5276}
5277
5278static int kbdlight_write(char *buf)
5279{
5280 char *cmd;
5281 int level = -1;
5282
5283 if (!tp_features.kbdlight)
5284 return -ENODEV;
5285
5286 while ((cmd = next_cmd(&buf))) {
5287 if (strlencmp(cmd, "0") == 0)
5288 level = 0;
5289 else if (strlencmp(cmd, "1") == 0)
5290 level = 1;
5291 else if (strlencmp(cmd, "2") == 0)
5292 level = 2;
5293 else
5294 return -EINVAL;
5295 }
5296
5297 if (level == -1)
5298 return -EINVAL;
5299
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005300 return kbdlight_set_level_and_update(level);
5301}
5302
5303static void kbdlight_suspend(void)
5304{
5305 struct led_classdev *led_cdev;
5306
5307 if (!tp_features.kbdlight)
5308 return;
5309
5310 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5311 led_update_brightness(led_cdev);
5312 led_classdev_suspend(led_cdev);
5313}
5314
5315static void kbdlight_resume(void)
5316{
5317 if (!tp_features.kbdlight)
5318 return;
5319
5320 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005321}
5322
5323static struct ibm_struct kbdlight_driver_data = {
5324 .name = "kbdlight",
5325 .read = kbdlight_read,
5326 .write = kbdlight_write,
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005327 .suspend = kbdlight_suspend,
5328 .resume = kbdlight_resume,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005329 .exit = kbdlight_exit,
5330};
5331
5332/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005333 * Light (thinklight) subdriver
5334 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005336TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5337TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005338
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005339static int light_get_status(void)
5340{
5341 int status = 0;
5342
5343 if (tp_features.light_status) {
5344 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5345 return -EIO;
5346 return (!!status);
5347 }
5348
5349 return -ENXIO;
5350}
5351
5352static int light_set_status(int status)
5353{
5354 int rc;
5355
5356 if (tp_features.light) {
5357 if (cmos_handle) {
5358 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005359 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005360 TP_CMOS_THINKLIGHT_ON :
5361 TP_CMOS_THINKLIGHT_OFF);
5362 } else {
5363 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005364 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005365 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005366 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005367 }
5368
5369 return -ENXIO;
5370}
5371
Hans de Goede86ec0c22017-02-09 16:44:12 +01005372static int light_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005373 enum led_brightness brightness)
5374{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005375 return light_set_status((brightness != LED_OFF) ?
5376 TPACPI_LED_ON : TPACPI_LED_OFF);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005377}
5378
5379static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5380{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005381 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005382}
5383
5384static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5385 .led_classdev = {
5386 .name = "tpacpi::thinklight",
Hans de Goede86ec0c22017-02-09 16:44:12 +01005387 .brightness_set_blocking = &light_sysfs_set,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005388 .brightness_get = &light_sysfs_get,
5389 }
5390};
5391
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005392static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005394 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005395
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005396 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5397
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005398 if (tpacpi_is_ibm()) {
5399 TPACPI_ACPIHANDLE_INIT(ledb);
5400 TPACPI_ACPIHANDLE_INIT(lght);
5401 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005402 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005403
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005404 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005405 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005406
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005407 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005408 /* light status not supported on
5409 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005410 tp_features.light_status =
5411 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005413 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5414 str_supported(tp_features.light),
5415 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005416
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005417 if (!tp_features.light)
5418 return 1;
5419
5420 rc = led_classdev_register(&tpacpi_pdev->dev,
5421 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005422
5423 if (rc < 0) {
5424 tp_features.light = 0;
5425 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005426 } else {
5427 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005428 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005429
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005430 return rc;
5431}
5432
5433static void light_exit(void)
5434{
5435 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436}
5437
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005438static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005440 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005442 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005443 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005444 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005445 seq_printf(m, "status:\t\tunknown\n");
5446 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005447 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005448 status = light_get_status();
5449 if (status < 0)
5450 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005451 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5452 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456}
5457
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005458static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005461 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005462
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005463 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005464 return -ENODEV;
5465
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 while ((cmd = next_cmd(&buf))) {
5467 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005468 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005470 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 } else
5472 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 }
5474
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005475 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476}
5477
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005478static struct ibm_struct light_driver_data = {
5479 .name = "light",
5480 .read = light_read,
5481 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005482 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005483};
5484
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005485/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005486 * CMOS subdriver
5487 */
5488
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005489/* sysfs cmos_command -------------------------------------------------- */
5490static ssize_t cmos_command_store(struct device *dev,
5491 struct device_attribute *attr,
5492 const char *buf, size_t count)
5493{
5494 unsigned long cmos_cmd;
5495 int res;
5496
5497 if (parse_strtoul(buf, 21, &cmos_cmd))
5498 return -EINVAL;
5499
5500 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005501 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005502}
5503
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01005504static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005505
5506/* --------------------------------------------------------------------- */
5507
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005508static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005509{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005510 int res;
5511
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005512 vdbg_printk(TPACPI_DBG_INIT,
5513 "initializing cmos commands subdriver\n");
5514
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005515 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005516
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005517 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5518 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005519
5520 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5521 if (res)
5522 return res;
5523
Jan van den Berg72a979f2014-09-17 00:01:08 +02005524 return (cmos_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005525}
5526
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005527static void cmos_exit(void)
5528{
5529 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5530}
5531
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005532static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005534 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5535 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005537 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005539 seq_printf(m, "status:\t\tsupported\n");
5540 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 }
5542
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544}
5545
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005546static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547{
5548 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005549 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
5551 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005552 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5553 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 /* cmos_cmd set */
5555 } else
5556 return -EINVAL;
5557
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005558 res = issue_thinkpad_cmos_command(cmos_cmd);
5559 if (res)
5560 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 }
5562
5563 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005564}
5565
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005566static struct ibm_struct cmos_driver_data = {
5567 .name = "cmos",
5568 .read = cmos_read,
5569 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005570 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005571};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005572
5573/*************************************************************************
5574 * LED subdriver
5575 */
5576
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005577enum led_access_mode {
5578 TPACPI_LED_NONE = 0,
5579 TPACPI_LED_570, /* 570 */
5580 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5581 TPACPI_LED_NEW, /* all others */
5582};
5583
5584enum { /* For TPACPI_LED_OLD */
5585 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5586 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5587 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5588};
5589
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005590static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005591
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005592static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005593
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005594#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005595static struct tpacpi_led_classdev *tpacpi_leds;
5596static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005597static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005598 /* there's a limit of 19 chars + NULL before 2.6.26 */
5599 "tpacpi::power",
5600 "tpacpi:orange:batt",
5601 "tpacpi:green:batt",
5602 "tpacpi::dock_active",
5603 "tpacpi::bay_active",
5604 "tpacpi::dock_batt",
5605 "tpacpi::unknown_led",
5606 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005607 "tpacpi::dock_status1",
5608 "tpacpi::dock_status2",
5609 "tpacpi::unknown_led2",
5610 "tpacpi::unknown_led3",
5611 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005612};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005613#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005614
5615static inline bool tpacpi_is_led_restricted(const unsigned int led)
5616{
5617#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5618 return false;
5619#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005620 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005621#endif
5622}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005623
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005624static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005625{
5626 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005627 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005628
5629 switch (led_supported) {
5630 case TPACPI_LED_570:
5631 if (!acpi_evalf(ec_handle,
5632 &status, "GLED", "dd", 1 << led))
5633 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005634 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005635 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005636 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005637 TPACPI_LED_ON :
5638 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005639 tpacpi_led_state_cache[led] = led_s;
5640 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005641 default:
5642 return -ENXIO;
5643 }
5644
5645 /* not reached */
5646}
5647
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005648static int led_set_status(const unsigned int led,
5649 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005650{
5651 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005652 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5653 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005654
5655 int rc = 0;
5656
5657 switch (led_supported) {
5658 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005659 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005660 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005661 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005662 if (unlikely(tpacpi_is_led_restricted(led)))
5663 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005664 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5665 (1 << led), led_sled_arg1[ledstatus]))
5666 rc = -EIO;
5667 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005668 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005669 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005670 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005671 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005672 if (unlikely(tpacpi_is_led_restricted(led)))
5673 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005674 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5675 if (rc >= 0)
5676 rc = ec_write(TPACPI_LED_EC_HLBL,
5677 (ledstatus == TPACPI_LED_BLINK) << led);
5678 if (rc >= 0)
5679 rc = ec_write(TPACPI_LED_EC_HLCL,
5680 (ledstatus != TPACPI_LED_OFF) << led);
5681 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005682 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005683 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005684 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5685 return -EINVAL;
5686 if (unlikely(tpacpi_is_led_restricted(led)))
5687 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005688 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5689 led, led_led_arg1[ledstatus]))
5690 rc = -EIO;
5691 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005692 default:
5693 rc = -ENXIO;
5694 }
5695
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005696 if (!rc)
5697 tpacpi_led_state_cache[led] = ledstatus;
5698
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005699 return rc;
5700}
5701
Hans de Goede86ec0c22017-02-09 16:44:12 +01005702static int led_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005703 enum led_brightness brightness)
5704{
5705 struct tpacpi_led_classdev *data = container_of(led_cdev,
5706 struct tpacpi_led_classdev, led_classdev);
Hans de Goede86ec0c22017-02-09 16:44:12 +01005707 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005708
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005709 if (brightness == LED_OFF)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005710 new_state = TPACPI_LED_OFF;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005711 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005712 new_state = TPACPI_LED_ON;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005713 else
Hans de Goede86ec0c22017-02-09 16:44:12 +01005714 new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005715
Hans de Goede86ec0c22017-02-09 16:44:12 +01005716 return led_set_status(data->led, new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005717}
5718
5719static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5720 unsigned long *delay_on, unsigned long *delay_off)
5721{
5722 struct tpacpi_led_classdev *data = container_of(led_cdev,
5723 struct tpacpi_led_classdev, led_classdev);
5724
5725 /* Can we choose the flash rate? */
5726 if (*delay_on == 0 && *delay_off == 0) {
5727 /* yes. set them to the hardware blink rate (1 Hz) */
5728 *delay_on = 500; /* ms */
5729 *delay_off = 500; /* ms */
5730 } else if ((*delay_on != 500) || (*delay_off != 500))
5731 return -EINVAL;
5732
Hans de Goede86ec0c22017-02-09 16:44:12 +01005733 return led_set_status(data->led, TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005734}
5735
5736static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5737{
5738 int rc;
5739
5740 struct tpacpi_led_classdev *data = container_of(led_cdev,
5741 struct tpacpi_led_classdev, led_classdev);
5742
5743 rc = led_get_status(data->led);
5744
5745 if (rc == TPACPI_LED_OFF || rc < 0)
5746 rc = LED_OFF; /* no error handling in led class :( */
5747 else
5748 rc = LED_FULL;
5749
5750 return rc;
5751}
5752
5753static void led_exit(void)
5754{
5755 unsigned int i;
5756
5757 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5758 if (tpacpi_leds[i].led_classdev.name)
5759 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5760 }
5761
5762 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005763}
5764
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005765static int __init tpacpi_init_led(unsigned int led)
5766{
5767 int rc;
5768
5769 tpacpi_leds[led].led = led;
5770
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005771 /* LEDs with no name don't get registered */
5772 if (!tpacpi_led_names[led])
5773 return 0;
5774
Hans de Goede86ec0c22017-02-09 16:44:12 +01005775 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005776 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5777 if (led_supported == TPACPI_LED_570)
5778 tpacpi_leds[led].led_classdev.brightness_get =
5779 &led_sysfs_get;
5780
5781 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5782
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005783 rc = led_classdev_register(&tpacpi_pdev->dev,
5784 &tpacpi_leds[led].led_classdev);
5785 if (rc < 0)
5786 tpacpi_leds[led].led_classdev.name = NULL;
5787
5788 return rc;
5789}
5790
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005791static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5792 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5793 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5794 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5795
5796 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5797 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5798 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5799 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5800 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5801 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5802 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5803 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5804
5805 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5806 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5807 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5808 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5809 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5810
5811 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5812 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5813 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5814 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5815
5816 /* (1) - may have excess leds enabled on MSB */
5817
5818 /* Defaults (order matters, keep last, don't reorder!) */
5819 { /* Lenovo */
5820 .vendor = PCI_VENDOR_ID_LENOVO,
5821 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5822 .quirks = 0x1fffU,
5823 },
5824 { /* IBM ThinkPads with no EC version string */
5825 .vendor = PCI_VENDOR_ID_IBM,
5826 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5827 .quirks = 0x00ffU,
5828 },
5829 { /* IBM ThinkPads with EC version string */
5830 .vendor = PCI_VENDOR_ID_IBM,
5831 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5832 .quirks = 0x00bfU,
5833 },
5834};
5835
5836#undef TPACPI_LEDQ_IBM
5837#undef TPACPI_LEDQ_LNV
5838
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005839static enum led_access_mode __init led_init_detect_mode(void)
5840{
5841 acpi_status status;
5842
5843 if (tpacpi_is_ibm()) {
5844 /* 570 */
5845 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5846 if (ACPI_SUCCESS(status))
5847 return TPACPI_LED_570;
5848
5849 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5850 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5851 if (ACPI_SUCCESS(status))
5852 return TPACPI_LED_OLD;
5853 }
5854
5855 /* most others */
5856 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5857 if (ACPI_SUCCESS(status))
5858 return TPACPI_LED_NEW;
5859
5860 /* R30, R31, and unknown firmwares */
5861 led_handle = NULL;
5862 return TPACPI_LED_NONE;
5863}
5864
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005865static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005866{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005867 unsigned int i;
5868 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005869 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005870
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005871 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5872
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005873 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005874
Adam Leefcb44e12013-06-07 16:20:08 +08005875 if (led_supported != TPACPI_LED_NONE) {
5876 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5877 ARRAY_SIZE(led_useful_qtable));
5878
5879 if (!useful_leds) {
5880 led_handle = NULL;
5881 led_supported = TPACPI_LED_NONE;
5882 }
5883 }
5884
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005885 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5886 str_supported(led_supported), led_supported);
5887
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005888 if (led_supported == TPACPI_LED_NONE)
5889 return 1;
5890
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005891 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5892 GFP_KERNEL);
5893 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005894 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005895 return -ENOMEM;
5896 }
5897
5898 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08005899 tpacpi_leds[i].led = -1;
5900
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005901 if (!tpacpi_is_led_restricted(i) &&
5902 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005903 rc = tpacpi_init_led(i);
5904 if (rc < 0) {
5905 led_exit();
5906 return rc;
5907 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005908 }
5909 }
5910
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005911#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03005912 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005913#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005914 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005915}
5916
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005917#define str_led_status(s) \
5918 ((s) == TPACPI_LED_OFF ? "off" : \
5919 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005920
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005921static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005923 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005924 seq_printf(m, "status:\t\tnot supported\n");
5925 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005926 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005927 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005928
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005929 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005930 /* 570 */
5931 int i, status;
5932 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005933 status = led_get_status(i);
5934 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005935 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005936 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005937 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005938 }
5939 }
5940
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03005941 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944}
5945
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005946static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947{
5948 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005949 int led, rc;
5950 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005951
5952 if (!led_supported)
5953 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
5955 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08005956 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 return -EINVAL;
5958
Adam Leeedf2d772013-06-08 16:51:15 +08005959 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5960 tpacpi_leds[led].led < 0)
5961 return -ENODEV;
5962
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005963 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005964 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005966 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005967 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005968 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005969 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005970 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005971 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005972
5973 rc = led_set_status(led, s);
5974 if (rc < 0)
5975 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 }
5977
5978 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005979}
5980
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005981static struct ibm_struct led_driver_data = {
5982 .name = "led",
5983 .read = led_read,
5984 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005985 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005986};
5987
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005988/*************************************************************************
5989 * Beep subdriver
5990 */
5991
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005992TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005993
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005994#define TPACPI_BEEP_Q1 0x0001
5995
5996static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5997 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5998 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5999};
6000
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006001static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006002{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006003 unsigned long quirks;
6004
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006005 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6006
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006007 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006008
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006009 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6010 str_supported(beep_handle != NULL));
6011
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006012 quirks = tpacpi_check_quirks(beep_quirk_table,
6013 ARRAY_SIZE(beep_quirk_table));
6014
6015 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6016
Jan van den Berg72a979f2014-09-17 00:01:08 +02006017 return (beep_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006018}
6019
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006020static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006022 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006023 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006024 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006025 seq_printf(m, "status:\t\tsupported\n");
6026 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030}
6031
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006032static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033{
6034 char *cmd;
6035 int beep_cmd;
6036
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006037 if (!beep_handle)
6038 return -ENODEV;
6039
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006041 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6042 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 /* beep_cmd set */
6044 } else
6045 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006046 if (tp_features.beep_needs_two_args) {
6047 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6048 beep_cmd, 0))
6049 return -EIO;
6050 } else {
6051 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6052 beep_cmd))
6053 return -EIO;
6054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 }
6056
6057 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006058}
6059
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006060static struct ibm_struct beep_driver_data = {
6061 .name = "beep",
6062 .read = beep_read,
6063 .write = beep_write,
6064};
6065
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006066/*************************************************************************
6067 * Thermal subdriver
6068 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006069
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006070enum thermal_access_mode {
6071 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6072 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6073 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6074 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6075 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6076};
6077
6078enum { /* TPACPI_THERMAL_TPEC_* */
6079 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6080 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6081 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006082
6083 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006084};
6085
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006086
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006087#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6088struct ibm_thermal_sensors_struct {
6089 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6090};
6091
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006092static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006093
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006094/* idx is zero-based */
6095static int thermal_get_sensor(int idx, s32 *value)
6096{
6097 int t;
6098 s8 tmp;
6099 char tmpi[5];
6100
6101 t = TP_EC_THERMAL_TMP0;
6102
6103 switch (thermal_read_mode) {
6104#if TPACPI_MAX_THERMAL_SENSORS >= 16
6105 case TPACPI_THERMAL_TPEC_16:
6106 if (idx >= 8 && idx <= 15) {
6107 t = TP_EC_THERMAL_TMP8;
6108 idx -= 8;
6109 }
6110 /* fallthrough */
6111#endif
6112 case TPACPI_THERMAL_TPEC_8:
6113 if (idx <= 7) {
6114 if (!acpi_ec_read(t + idx, &tmp))
6115 return -EIO;
6116 *value = tmp * 1000;
6117 return 0;
6118 }
6119 break;
6120
6121 case TPACPI_THERMAL_ACPI_UPDT:
6122 if (idx <= 7) {
6123 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6124 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6125 return -EIO;
6126 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6127 return -EIO;
6128 *value = (t - 2732) * 100;
6129 return 0;
6130 }
6131 break;
6132
6133 case TPACPI_THERMAL_ACPI_TMP07:
6134 if (idx <= 7) {
6135 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6136 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6137 return -EIO;
6138 if (t > 127 || t < -127)
6139 t = TP_EC_THERMAL_TMP_NA;
6140 *value = t * 1000;
6141 return 0;
6142 }
6143 break;
6144
6145 case TPACPI_THERMAL_NONE:
6146 default:
6147 return -ENOSYS;
6148 }
6149
6150 return -EINVAL;
6151}
6152
6153static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6154{
6155 int res, i;
6156 int n;
6157
6158 n = 8;
6159 i = 0;
6160
6161 if (!s)
6162 return -EINVAL;
6163
6164 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6165 n = 16;
6166
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006167 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006168 res = thermal_get_sensor(i, &s->temp[i]);
6169 if (res)
6170 return res;
6171 }
6172
6173 return n;
6174}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006175
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006176static void thermal_dump_all_sensors(void)
6177{
6178 int n, i;
6179 struct ibm_thermal_sensors_struct t;
6180
6181 n = thermal_get_sensors(&t);
6182 if (n <= 0)
6183 return;
6184
Joe Perches0978e012011-04-04 10:06:25 -07006185 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006186
6187 for (i = 0; i < n; i++) {
6188 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07006189 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006190 else
Joe Perches0978e012011-04-04 10:06:25 -07006191 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006192 }
6193
Joe Perches0978e012011-04-04 10:06:25 -07006194 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006195}
6196
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006197/* sysfs temp##_input -------------------------------------------------- */
6198
6199static ssize_t thermal_temp_input_show(struct device *dev,
6200 struct device_attribute *attr,
6201 char *buf)
6202{
6203 struct sensor_device_attribute *sensor_attr =
6204 to_sensor_dev_attr(attr);
6205 int idx = sensor_attr->index;
6206 s32 value;
6207 int res;
6208
6209 res = thermal_get_sensor(idx, &value);
6210 if (res)
6211 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006212 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006213 return -ENXIO;
6214
6215 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6216}
6217
6218#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006219 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6220 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006221
6222static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6223 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6224 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6225 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6226 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6227 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6228 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6229 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6230 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6231 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6232 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6233 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6234 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6235 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6236 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6237 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6238 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6239};
6240
6241#define THERMAL_ATTRS(X) \
6242 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6243
6244static struct attribute *thermal_temp_input_attr[] = {
6245 THERMAL_ATTRS(8),
6246 THERMAL_ATTRS(9),
6247 THERMAL_ATTRS(10),
6248 THERMAL_ATTRS(11),
6249 THERMAL_ATTRS(12),
6250 THERMAL_ATTRS(13),
6251 THERMAL_ATTRS(14),
6252 THERMAL_ATTRS(15),
6253 THERMAL_ATTRS(0),
6254 THERMAL_ATTRS(1),
6255 THERMAL_ATTRS(2),
6256 THERMAL_ATTRS(3),
6257 THERMAL_ATTRS(4),
6258 THERMAL_ATTRS(5),
6259 THERMAL_ATTRS(6),
6260 THERMAL_ATTRS(7),
6261 NULL
6262};
6263
6264static const struct attribute_group thermal_temp_input16_group = {
6265 .attrs = thermal_temp_input_attr
6266};
6267
6268static const struct attribute_group thermal_temp_input8_group = {
6269 .attrs = &thermal_temp_input_attr[8]
6270};
6271
6272#undef THERMAL_SENSOR_ATTR_TEMP
6273#undef THERMAL_ATTRS
6274
6275/* --------------------------------------------------------------------- */
6276
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006277static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006278{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006279 u8 t, ta1, ta2;
6280 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006281 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006282 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006283
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006284 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6285
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006286 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006287
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03006288 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006289 /*
6290 * Direct EC access mode: sensors at registers
6291 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6292 * non-implemented, thermal sensors return 0x80 when
6293 * not available
6294 */
6295
6296 ta1 = ta2 = 0;
6297 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006298 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006299 ta1 |= t;
6300 } else {
6301 ta1 = 0;
6302 break;
6303 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006304 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006305 ta2 |= t;
6306 } else {
6307 ta1 = 0;
6308 break;
6309 }
6310 }
6311 if (ta1 == 0) {
6312 /* This is sheer paranoia, but we handle it anyway */
6313 if (acpi_tmp7) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006314 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006315 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006316 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006317 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006318 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006319 }
6320 } else {
6321 thermal_read_mode =
6322 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006323 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006324 }
6325 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006326 if (tpacpi_is_ibm() &&
6327 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006328 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006329 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006330 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006331 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006332 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006333 }
6334 } else {
6335 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006336 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006337 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006338
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006339 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6340 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6341 thermal_read_mode);
6342
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006343 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006344 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006345 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006346 &thermal_temp_input16_group);
6347 if (res)
6348 return res;
6349 break;
6350 case TPACPI_THERMAL_TPEC_8:
6351 case TPACPI_THERMAL_ACPI_TMP07:
6352 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006353 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006354 &thermal_temp_input8_group);
6355 if (res)
6356 return res;
6357 break;
6358 case TPACPI_THERMAL_NONE:
6359 default:
6360 return 1;
6361 }
6362
6363 return 0;
6364}
6365
6366static void thermal_exit(void)
6367{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006368 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006369 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006370 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006371 &thermal_temp_input16_group);
6372 break;
6373 case TPACPI_THERMAL_TPEC_8:
6374 case TPACPI_THERMAL_ACPI_TMP07:
6375 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006376 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08006377 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006378 break;
6379 case TPACPI_THERMAL_NONE:
6380 default:
6381 break;
6382 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006383}
6384
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006385static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006386{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006387 int n, i;
6388 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006389
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006390 n = thermal_get_sensors(&t);
6391 if (unlikely(n < 0))
6392 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006393
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006394 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006395
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006396 if (n > 0) {
6397 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006398 seq_printf(m, "%d ", t.temp[i] / 1000);
6399 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006400 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006401 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006402
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006403 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006404}
6405
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006406static struct ibm_struct thermal_driver_data = {
6407 .name = "thermal",
6408 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006409 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006410};
6411
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006412/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006413 * Backlight/brightness subdriver
6414 */
Holger Macht8acb0252006-10-20 14:30:28 -07006415
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006416#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6417
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006418/*
6419 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6420 * CMOS NVRAM byte 0x5E, bits 0-3.
6421 *
6422 * EC HBRV (0x31) has the following layout
6423 * Bit 7: unknown function
6424 * Bit 6: unknown function
6425 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6426 * Bit 4: must be set to zero to avoid problems
6427 * Bit 3-0: backlight brightness level
6428 *
6429 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006430 *
6431 * WARNING: The X61 has been verified to use HBRV for something else, so
6432 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6433 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006434 */
6435
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006436enum {
6437 TP_EC_BACKLIGHT = 0x31,
6438
6439 /* TP_EC_BACKLIGHT bitmasks */
6440 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6441 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6442 TP_EC_BACKLIGHT_MAPSW = 0x20,
6443};
6444
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006445enum tpacpi_brightness_access_mode {
6446 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6447 TPACPI_BRGHT_MODE_EC, /* EC control */
6448 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6449 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6450 TPACPI_BRGHT_MODE_MAX
6451};
6452
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006453static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006454
6455static enum tpacpi_brightness_access_mode brightness_mode =
6456 TPACPI_BRGHT_MODE_MAX;
6457
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006458static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6459
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006460static struct mutex brightness_mutex;
6461
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006462/* NVRAM brightness access,
6463 * call with brightness_mutex held! */
6464static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006465{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006466 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006467
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006468 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6469 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6470 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006471 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006472
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006473 return lnvram;
6474}
6475
6476static void tpacpi_brightness_checkpoint_nvram(void)
6477{
6478 u8 lec = 0;
6479 u8 b_nvram;
6480
6481 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6482 return;
6483
6484 vdbg_printk(TPACPI_DBG_BRGHT,
6485 "trying to checkpoint backlight level to NVRAM...\n");
6486
6487 if (mutex_lock_killable(&brightness_mutex) < 0)
6488 return;
6489
6490 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6491 goto unlock;
6492 lec &= TP_EC_BACKLIGHT_LVLMSK;
6493 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6494
6495 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6496 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6497 /* NVRAM needs update */
6498 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6499 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6500 b_nvram |= lec;
6501 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6502 dbg_printk(TPACPI_DBG_BRGHT,
6503 "updated NVRAM backlight level to %u (0x%02x)\n",
6504 (unsigned int) lec, (unsigned int) b_nvram);
6505 } else
6506 vdbg_printk(TPACPI_DBG_BRGHT,
6507 "NVRAM backlight level already is %u (0x%02x)\n",
6508 (unsigned int) lec, (unsigned int) b_nvram);
6509
6510unlock:
6511 mutex_unlock(&brightness_mutex);
6512}
6513
6514
6515/* call with brightness_mutex held! */
6516static int tpacpi_brightness_get_raw(int *status)
6517{
6518 u8 lec = 0;
6519
6520 switch (brightness_mode) {
6521 case TPACPI_BRGHT_MODE_UCMS_STEP:
6522 *status = tpacpi_brightness_nvram_get();
6523 return 0;
6524 case TPACPI_BRGHT_MODE_EC:
6525 case TPACPI_BRGHT_MODE_ECNVRAM:
6526 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006527 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006528 *status = lec;
6529 return 0;
6530 default:
6531 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006532 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006533}
6534
6535/* call with brightness_mutex held! */
6536/* do NOT call with illegal backlight level value */
6537static int tpacpi_brightness_set_ec(unsigned int value)
6538{
6539 u8 lec = 0;
6540
6541 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6542 return -EIO;
6543
6544 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6545 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6546 (value & TP_EC_BACKLIGHT_LVLMSK))))
6547 return -EIO;
6548
6549 return 0;
6550}
6551
6552/* call with brightness_mutex held! */
6553static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6554{
6555 int cmos_cmd, inc;
6556 unsigned int current_value, i;
6557
6558 current_value = tpacpi_brightness_nvram_get();
6559
6560 if (value == current_value)
6561 return 0;
6562
6563 cmos_cmd = (value > current_value) ?
6564 TP_CMOS_BRIGHTNESS_UP :
6565 TP_CMOS_BRIGHTNESS_DOWN;
6566 inc = (value > current_value) ? 1 : -1;
6567
6568 for (i = current_value; i != value; i += inc)
6569 if (issue_thinkpad_cmos_command(cmos_cmd))
6570 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006571
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006572 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006573}
6574
6575/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006576static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006577{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006578 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006579
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006580 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006581 return -EINVAL;
6582
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006583 vdbg_printk(TPACPI_DBG_BRGHT,
6584 "set backlight level to %d\n", value);
6585
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006586 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006587 if (res < 0)
6588 return res;
6589
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006590 switch (brightness_mode) {
6591 case TPACPI_BRGHT_MODE_EC:
6592 case TPACPI_BRGHT_MODE_ECNVRAM:
6593 res = tpacpi_brightness_set_ec(value);
6594 break;
6595 case TPACPI_BRGHT_MODE_UCMS_STEP:
6596 res = tpacpi_brightness_set_ucmsstep(value);
6597 break;
6598 default:
6599 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006600 }
6601
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006602 mutex_unlock(&brightness_mutex);
6603 return res;
6604}
6605
6606/* sysfs backlight class ----------------------------------------------- */
6607
6608static int brightness_update_status(struct backlight_device *bd)
6609{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006610 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006611 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6612 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006613 bd->props.brightness : 0;
6614
6615 dbg_printk(TPACPI_DBG_BRGHT,
6616 "backlight: attempt to set level to %d\n",
6617 level);
6618
6619 /* it is the backlight class's job (caller) to handle
6620 * EINTR and other errors properly */
6621 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006622}
Holger Macht8acb0252006-10-20 14:30:28 -07006623
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006624static int brightness_get(struct backlight_device *bd)
6625{
6626 int status, res;
6627
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006628 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006629 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006630 return 0;
6631
6632 res = tpacpi_brightness_get_raw(&status);
6633
6634 mutex_unlock(&brightness_mutex);
6635
6636 if (res < 0)
6637 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006638
6639 return status & TP_EC_BACKLIGHT_LVLMSK;
6640}
6641
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006642static void tpacpi_brightness_notify_change(void)
6643{
6644 backlight_force_update(ibm_backlight_device,
6645 BACKLIGHT_UPDATE_HOTKEY);
6646}
6647
Lionel Debrouxacc24722010-11-16 14:14:02 +01006648static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006649 .get_brightness = brightness_get,
6650 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006651};
6652
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006653/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006654
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006655/*
6656 * Call _BCL method of video device. On some ThinkPads this will
6657 * switch the firmware to the ACPI brightness control mode.
6658 */
6659
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006660static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6661{
6662 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6663 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006664 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006665 int rc;
6666
Aaron Lu46445b62013-10-11 21:27:46 +08006667 if (acpi_bus_get_device(handle, &device))
6668 return 0;
6669
6670 rc = 0;
6671 list_for_each_entry(child, &device->children, node) {
6672 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6673 NULL, &buffer);
6674 if (ACPI_FAILURE(status))
6675 continue;
6676
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006677 obj = (union acpi_object *)buffer.pointer;
6678 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006679 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006680 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006681 rc = 0;
6682 } else {
6683 rc = obj->package.count;
6684 }
Aaron Lu46445b62013-10-11 21:27:46 +08006685 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006686 }
6687
6688 kfree(buffer.pointer);
6689 return rc;
6690}
6691
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006692
6693/*
6694 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6695 */
6696static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6697{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006698 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006699 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006700
Aaron Lu46445b62013-10-11 21:27:46 +08006701 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006702 if (video_device)
6703 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006704
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006705 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006706
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006707 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006708}
6709
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006710/*
6711 * These are only useful for models that have only one possibility
6712 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6713 * these quirks.
6714 */
6715#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6716#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6717#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6718
6719static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6720 /* Models with ATI GPUs known to require ECNVRAM mode */
6721 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6722
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006723 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006724 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006725 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006726 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006727 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6728
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006729 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006730 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006731 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6732 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006733
6734 /* Models with Intel GMA900 */
6735 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6736 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6737 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6738};
6739
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006740/*
6741 * Returns < 0 for error, otherwise sets tp_features.bright_*
6742 * and bright_maxlvl.
6743 */
6744static void __init tpacpi_detect_brightness_capabilities(void)
6745{
6746 unsigned int b;
6747
6748 vdbg_printk(TPACPI_DBG_INIT,
6749 "detecting firmware brightness interface capabilities\n");
6750
6751 /* we could run a quirks check here (same table used by
6752 * brightness_init) if needed */
6753
6754 /*
6755 * We always attempt to detect acpi support, so as to switch
6756 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6757 * going to publish a backlight interface
6758 */
6759 b = tpacpi_check_std_acpi_brightness_support();
6760 switch (b) {
6761 case 16:
6762 bright_maxlvl = 15;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006763 break;
6764 case 8:
6765 case 0:
6766 bright_maxlvl = 7;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006767 break;
6768 default:
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006769 tp_features.bright_unkfw = 1;
6770 bright_maxlvl = b - 1;
6771 }
Eric Curtin15c75622016-01-30 16:55:59 +00006772 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006773}
6774
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006775static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006776{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006777 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006778 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006779 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006780
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006781 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6782
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006783 mutex_init(&brightness_mutex);
6784
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006785 quirks = tpacpi_check_quirks(brightness_quirk_table,
6786 ARRAY_SIZE(brightness_quirk_table));
6787
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006788 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006789
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006790 /* if it is unknown, we don't handle it: it wouldn't be safe */
6791 if (tp_features.bright_unkfw)
6792 return 1;
6793
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006794 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006795 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006796 "brightness support disabled by module parameter\n");
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006797 return 1;
6798 }
6799
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02006800 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006801 if (brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006802 pr_info("Standard ACPI backlight interface available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006803 return 1;
6804 } else if (brightness_enable == 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006805 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 -03006806 return 1;
6807 }
6808 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006809 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006810 }
6811
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006812 /*
6813 * Check for module parameter bogosity, note that we
6814 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6815 * able to detect "unspecified"
6816 */
6817 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006818 return -EINVAL;
6819
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006820 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6821 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6822 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006823 if (quirks & TPACPI_BRGHT_Q_EC)
6824 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6825 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006826 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6827
6828 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006829 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006830 brightness_mode);
6831 }
6832
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006833 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006834 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006835 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6836 brightness_mode == TPACPI_BRGHT_MODE_EC))
6837 return -EINVAL;
6838
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006839 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006840 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006841
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006842 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006843 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006844 props.max_brightness = bright_maxlvl;
6845 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006846 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6847 NULL, NULL,
6848 &ibm_backlight_data,
6849 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006850 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006851 int rc = PTR_ERR(ibm_backlight_device);
6852 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006853 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006854 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006855 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006856 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6857 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006858
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006859 if (quirks & TPACPI_BRGHT_Q_ASK) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006860 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
6861 brightness_mode);
6862 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
6863 TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006864 }
6865
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006866 /* Added by mistake in early 2007. Probably useless, but it could
6867 * be working around some unknown firmware problem where the value
6868 * read at startup doesn't match the real hardware state... so leave
6869 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006870 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006871
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006872 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006873 "brightness: registering brightness hotkeys as change notification\n");
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006874 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6875 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006876 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006877 return 0;
6878}
6879
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006880static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006881{
6882 tpacpi_brightness_checkpoint_nvram();
6883}
6884
6885static void brightness_shutdown(void)
6886{
6887 tpacpi_brightness_checkpoint_nvram();
6888}
6889
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006890static void brightness_exit(void)
6891{
6892 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006893 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006894 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006895 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006896 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006897
6898 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006899}
6900
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006901static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006902{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006903 int level;
6904
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006905 level = brightness_get(NULL);
6906 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006907 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006908 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006909 seq_printf(m, "level:\t\t%d\n", level);
6910 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006911 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6912 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006913 }
6914
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006915 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006916}
6917
6918static int brightness_write(char *buf)
6919{
6920 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006921 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006922 char *cmd;
6923
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006924 level = brightness_get(NULL);
6925 if (level < 0)
6926 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006927
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006928 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006929 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006930 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006931 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006932 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006933 if (level > 0)
6934 level--;
6935 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006936 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006937 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006938 } else
6939 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006940 }
6941
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006942 tpacpi_disclose_usertask("procfs brightness",
6943 "set level to %d\n", level);
6944
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006945 /*
6946 * Now we know what the final level should be, so we try to set it.
6947 * Doing it this way makes the syscall restartable in case of EINTR
6948 */
6949 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006950 if (!rc && ibm_backlight_device)
6951 backlight_force_update(ibm_backlight_device,
6952 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02006953 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006954}
6955
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006956static struct ibm_struct brightness_driver_data = {
6957 .name = "brightness",
6958 .read = brightness_read,
6959 .write = brightness_write,
6960 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006961 .suspend = brightness_suspend,
6962 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006963};
6964
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006965/*************************************************************************
6966 * Volume subdriver
6967 */
6968
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006969/*
6970 * IBM ThinkPads have a simple volume controller with MUTE gating.
6971 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6972 *
6973 * Since the *61 series (and probably also the later *60 series), Lenovo
6974 * ThinkPads only implement the MUTE gate.
6975 *
6976 * EC register 0x30
6977 * Bit 6: MUTE (1 mutes sound)
6978 * Bit 3-0: Volume
6979 * Other bits should be zero as far as we know.
6980 *
6981 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6982 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6983 * such as bit 7 which is used to detect repeated presses of MUTE,
6984 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07006985 *
6986 * On newer Lenovo ThinkPads, the EC can automatically change the volume
6987 * in response to user input. Unfortunately, this rarely works well.
6988 * The laptop changes the state of its internal MUTE gate and, on some
6989 * models, sends KEY_MUTE, causing any user code that responds to the
6990 * mute button to get confused. The hardware MUTE gate is also
6991 * unnecessary, since user code can handle the mute button without
6992 * kernel or EC help.
6993 *
6994 * To avoid confusing userspace, we simply disable all EC-based mute
6995 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006996 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006997
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006998#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6999
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007000#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7001#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7002#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7003
Takashi Iwaicab66612013-10-24 16:06:32 +02007004#if SNDRV_CARDS <= 32
7005#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7006#else
7007#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7008#endif
7009static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007010static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307011static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007012
7013struct tpacpi_alsa_data {
7014 struct snd_card *card;
7015 struct snd_ctl_elem_id *ctl_mute_id;
7016 struct snd_ctl_elem_id *ctl_vol_id;
7017};
7018
7019static struct snd_card *alsa_card;
7020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007021enum {
7022 TP_EC_AUDIO = 0x30,
7023
7024 /* TP_EC_AUDIO bits */
7025 TP_EC_AUDIO_MUTESW = 6,
7026
7027 /* TP_EC_AUDIO bitmasks */
7028 TP_EC_AUDIO_LVL_MSK = 0x0F,
7029 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7030
7031 /* Maximum volume */
7032 TP_EC_VOLUME_MAX = 14,
7033};
7034
7035enum tpacpi_volume_access_mode {
7036 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7037 TPACPI_VOL_MODE_EC, /* Pure EC control */
7038 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7039 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7040 TPACPI_VOL_MODE_MAX
7041};
7042
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007043enum tpacpi_volume_capabilities {
7044 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7045 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7046 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7047 TPACPI_VOL_CAP_MAX
7048};
7049
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007050enum tpacpi_mute_btn_mode {
7051 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7052 /* We don't know what mode 1 is. */
7053 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7054 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7055};
7056
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007057static enum tpacpi_volume_access_mode volume_mode =
7058 TPACPI_VOL_MODE_MAX;
7059
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007060static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307061static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007062static bool software_mute_requested = true;
7063static bool software_mute_active;
7064static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007065
7066/*
7067 * Used to syncronize writers to TP_EC_AUDIO and
7068 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7069 */
7070static struct mutex volume_mutex;
7071
7072static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007073{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007074 u8 lec = 0;
7075 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007076 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007077
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007078 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7079 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007080 if (!volume_control_allowed)
7081 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007082 if (software_mute_active)
7083 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007084
7085 vdbg_printk(TPACPI_DBG_MIXER,
7086 "trying to checkpoint mixer state to NVRAM...\n");
7087
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007088 if (tp_features.mixer_no_level_control)
7089 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7090 else
7091 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7092
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007093 if (mutex_lock_killable(&volume_mutex) < 0)
7094 return;
7095
7096 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7097 goto unlock;
7098 lec &= ec_mask;
7099 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7100
7101 if (lec != (b_nvram & ec_mask)) {
7102 /* NVRAM needs update */
7103 b_nvram &= ~ec_mask;
7104 b_nvram |= lec;
7105 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7106 dbg_printk(TPACPI_DBG_MIXER,
7107 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7108 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007109 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007110 vdbg_printk(TPACPI_DBG_MIXER,
7111 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7112 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007113 }
7114
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007115unlock:
7116 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007117}
7118
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007119static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007120{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007121 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007122
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007123 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7124 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007125
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007126 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007127
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007128 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007129
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007130 return 0;
7131}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007132
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007133static int volume_get_status(u8 *status)
7134{
7135 return volume_get_status_ec(status);
7136}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007137
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007138static int volume_set_status_ec(const u8 status)
7139{
7140 if (!acpi_ec_write(TP_EC_AUDIO, status))
7141 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007142
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007143 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7144
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007145 /*
7146 * On X200s, and possibly on others, it can take a while for
7147 * reads to become correct.
7148 */
7149 msleep(1);
7150
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007151 return 0;
7152}
7153
7154static int volume_set_status(const u8 status)
7155{
7156 return volume_set_status_ec(status);
7157}
7158
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007159/* returns < 0 on error, 0 on no change, 1 on change */
7160static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007161{
7162 int rc;
7163 u8 s, n;
7164
7165 if (mutex_lock_killable(&volume_mutex) < 0)
7166 return -EINTR;
7167
7168 rc = volume_get_status_ec(&s);
7169 if (rc)
7170 goto unlock;
7171
7172 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7173 s & ~TP_EC_AUDIO_MUTESW_MSK;
7174
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007175 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007176 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007177 if (!rc)
7178 rc = 1;
7179 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007180
7181unlock:
7182 mutex_unlock(&volume_mutex);
7183 return rc;
7184}
7185
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007186static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007187{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007188 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007189 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007190 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007191}
7192
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007193static int volume_set_mute(const bool mute)
7194{
7195 int rc;
7196
7197 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7198 (mute) ? "" : "un");
7199
7200 rc = __volume_set_mute_ec(mute);
7201 return (rc < 0) ? rc : 0;
7202}
7203
7204/* returns < 0 on error, 0 on no change, 1 on change */
7205static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007206{
7207 int rc;
7208 u8 s, n;
7209
7210 if (vol > TP_EC_VOLUME_MAX)
7211 return -EINVAL;
7212
7213 if (mutex_lock_killable(&volume_mutex) < 0)
7214 return -EINTR;
7215
7216 rc = volume_get_status_ec(&s);
7217 if (rc)
7218 goto unlock;
7219
7220 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7221
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007222 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007223 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007224 if (!rc)
7225 rc = 1;
7226 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007227
7228unlock:
7229 mutex_unlock(&volume_mutex);
7230 return rc;
7231}
7232
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007233static int volume_set_software_mute(bool startup)
7234{
7235 int result;
7236
7237 if (!tpacpi_is_lenovo())
7238 return -ENODEV;
7239
7240 if (startup) {
7241 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7242 "HAUM", "qd"))
7243 return -EIO;
7244
7245 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7246 "Initial HAUM setting was %d\n",
7247 software_mute_orig_mode);
7248 }
7249
7250 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7251 (int)TP_EC_MUTE_BTN_NONE))
7252 return -EIO;
7253
7254 if (result != TP_EC_MUTE_BTN_NONE)
7255 pr_warn("Unexpected SAUM result %d\n",
7256 result);
7257
7258 /*
7259 * In software mute mode, the standard codec controls take
7260 * precendence, so we unmute the ThinkPad HW switch at
7261 * startup. Just on case there are SAUM-capable ThinkPads
7262 * with level controls, set max HW volume as well.
7263 */
7264 if (tp_features.mixer_no_level_control)
7265 result = volume_set_mute(false);
7266 else
7267 result = volume_set_status(TP_EC_VOLUME_MAX);
7268
7269 if (result != 0)
7270 pr_warn("Failed to unmute the HW mute switch\n");
7271
7272 return 0;
7273}
7274
7275static void volume_exit_software_mute(void)
7276{
7277 int r;
7278
7279 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7280 || r != software_mute_orig_mode)
7281 pr_warn("Failed to restore mute mode\n");
7282}
7283
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007284static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007285{
7286 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007287 "ALSA: trying to set volume level to %hu\n", vol);
7288 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007289}
7290
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007291static void volume_alsa_notify_change(void)
7292{
7293 struct tpacpi_alsa_data *d;
7294
7295 if (alsa_card && alsa_card->private_data) {
7296 d = alsa_card->private_data;
7297 if (d->ctl_mute_id)
7298 snd_ctl_notify(alsa_card,
7299 SNDRV_CTL_EVENT_MASK_VALUE,
7300 d->ctl_mute_id);
7301 if (d->ctl_vol_id)
7302 snd_ctl_notify(alsa_card,
7303 SNDRV_CTL_EVENT_MASK_VALUE,
7304 d->ctl_vol_id);
7305 }
7306}
7307
7308static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7309 struct snd_ctl_elem_info *uinfo)
7310{
7311 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7312 uinfo->count = 1;
7313 uinfo->value.integer.min = 0;
7314 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7315 return 0;
7316}
7317
7318static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7319 struct snd_ctl_elem_value *ucontrol)
7320{
7321 u8 s;
7322 int rc;
7323
7324 rc = volume_get_status(&s);
7325 if (rc < 0)
7326 return rc;
7327
7328 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7329 return 0;
7330}
7331
7332static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7333 struct snd_ctl_elem_value *ucontrol)
7334{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007335 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7336 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007337 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007338}
7339
7340#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7341
7342static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7343 struct snd_ctl_elem_value *ucontrol)
7344{
7345 u8 s;
7346 int rc;
7347
7348 rc = volume_get_status(&s);
7349 if (rc < 0)
7350 return rc;
7351
7352 ucontrol->value.integer.value[0] =
7353 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7354 return 0;
7355}
7356
7357static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7358 struct snd_ctl_elem_value *ucontrol)
7359{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007360 tpacpi_disclose_usertask("ALSA", "%smute\n",
7361 ucontrol->value.integer.value[0] ?
7362 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007363 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007364}
7365
Mathias Krausec3aa4722014-07-16 19:43:17 +02007366static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007367 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7368 .name = "Console Playback Volume",
7369 .index = 0,
7370 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7371 .info = volume_alsa_vol_info,
7372 .get = volume_alsa_vol_get,
7373};
7374
Mathias Krausec3aa4722014-07-16 19:43:17 +02007375static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007376 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7377 .name = "Console Playback Switch",
7378 .index = 0,
7379 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7380 .info = volume_alsa_mute_info,
7381 .get = volume_alsa_mute_get,
7382};
7383
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007384static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007385{
7386 tpacpi_volume_checkpoint_nvram();
7387}
7388
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007389static void volume_resume(void)
7390{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007391 if (software_mute_active) {
7392 if (volume_set_software_mute(false) < 0)
7393 pr_warn("Failed to restore software mute\n");
7394 } else {
7395 volume_alsa_notify_change();
7396 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007397}
7398
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007399static void volume_shutdown(void)
7400{
7401 tpacpi_volume_checkpoint_nvram();
7402}
7403
7404static void volume_exit(void)
7405{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007406 if (alsa_card) {
7407 snd_card_free(alsa_card);
7408 alsa_card = NULL;
7409 }
7410
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007411 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007412
7413 if (software_mute_active)
7414 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007415}
7416
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007417static int __init volume_create_alsa_mixer(void)
7418{
7419 struct snd_card *card;
7420 struct tpacpi_alsa_data *data;
7421 struct snd_kcontrol *ctl_vol;
7422 struct snd_kcontrol *ctl_mute;
7423 int rc;
7424
Takashi Iwai89235e52014-01-29 15:01:27 +01007425 rc = snd_card_new(&tpacpi_pdev->dev,
7426 alsa_index, alsa_id, THIS_MODULE,
7427 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007428 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007429 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007430 return 1;
7431 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007432
7433 BUG_ON(!card->private_data);
7434 data = card->private_data;
7435 data->card = card;
7436
7437 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7438 sizeof(card->driver));
7439 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7440 sizeof(card->shortname));
7441 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7442 (thinkpad_id.ec_version_str) ?
7443 thinkpad_id.ec_version_str : "(unknown)");
7444 snprintf(card->longname, sizeof(card->longname),
7445 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7446 (thinkpad_id.ec_version_str) ?
7447 thinkpad_id.ec_version_str : "unknown");
7448
7449 if (volume_control_allowed) {
7450 volume_alsa_control_vol.put = volume_alsa_vol_put;
7451 volume_alsa_control_vol.access =
7452 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7453
7454 volume_alsa_control_mute.put = volume_alsa_mute_put;
7455 volume_alsa_control_mute.access =
7456 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7457 }
7458
7459 if (!tp_features.mixer_no_level_control) {
7460 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7461 rc = snd_ctl_add(card, ctl_vol);
7462 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007463 pr_err("Failed to create ALSA volume control: %d\n",
7464 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007465 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007466 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007467 data->ctl_vol_id = &ctl_vol->id;
7468 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007469
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007470 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7471 rc = snd_ctl_add(card, ctl_mute);
7472 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007473 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007474 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007475 }
7476 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007477
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007478 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007479 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007480 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007481 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007482 }
7483
7484 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007485 return 0;
7486
7487err_exit:
7488 snd_card_free(card);
7489 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007490}
7491
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007492#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7493#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7494
7495static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7496 /* Whitelist volume level on all IBM by default */
7497 { .vendor = PCI_VENDOR_ID_IBM,
7498 .bios = TPACPI_MATCH_ANY,
7499 .ec = TPACPI_MATCH_ANY,
7500 .quirks = TPACPI_VOL_Q_LEVEL },
7501
7502 /* Lenovo models with volume control (needs confirmation) */
7503 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7504 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7505 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7506 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7507 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7508 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7509 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7510
7511 /* Whitelist mute-only on all Lenovo by default */
7512 { .vendor = PCI_VENDOR_ID_LENOVO,
7513 .bios = TPACPI_MATCH_ANY,
7514 .ec = TPACPI_MATCH_ANY,
7515 .quirks = TPACPI_VOL_Q_MUTEONLY }
7516};
7517
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007518static int __init volume_init(struct ibm_init_struct *iibm)
7519{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007520 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007521 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007522
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007523 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7524
7525 mutex_init(&volume_mutex);
7526
7527 /*
7528 * Check for module parameter bogosity, note that we
7529 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7530 * able to detect "unspecified"
7531 */
7532 if (volume_mode > TPACPI_VOL_MODE_MAX)
7533 return -EINVAL;
7534
7535 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007536 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7537 TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007538 return 1;
7539 }
7540
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007541 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7542 return -EINVAL;
7543
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007544 /*
7545 * The ALSA mixer is our primary interface.
7546 * When disabled, don't install the subdriver at all
7547 */
7548 if (!alsa_enable) {
7549 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007550 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007551 return 1;
7552 }
7553
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007554 quirks = tpacpi_check_quirks(volume_quirk_table,
7555 ARRAY_SIZE(volume_quirk_table));
7556
7557 switch (volume_capabilities) {
7558 case TPACPI_VOL_CAP_AUTO:
7559 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7560 tp_features.mixer_no_level_control = 1;
7561 else if (quirks & TPACPI_VOL_Q_LEVEL)
7562 tp_features.mixer_no_level_control = 0;
7563 else
7564 return 1; /* no mixer */
7565 break;
7566 case TPACPI_VOL_CAP_VOLMUTE:
7567 tp_features.mixer_no_level_control = 0;
7568 break;
7569 case TPACPI_VOL_CAP_MUTEONLY:
7570 tp_features.mixer_no_level_control = 1;
7571 break;
7572 default:
7573 return 1;
7574 }
7575
7576 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7577 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7578 "using user-supplied volume_capabilities=%d\n",
7579 volume_capabilities);
7580
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007581 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7582 volume_mode == TPACPI_VOL_MODE_MAX) {
7583 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7584
7585 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7586 "driver auto-selected volume_mode=%d\n",
7587 volume_mode);
7588 } else {
7589 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7590 "using user-supplied volume_mode=%d\n",
7591 volume_mode);
7592 }
7593
7594 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007595 "mute is supported, volume control is %s\n",
7596 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007597
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007598 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7599 software_mute_active = true;
7600 } else {
7601 rc = volume_create_alsa_mixer();
7602 if (rc) {
7603 pr_err("Could not create the ALSA mixer interface\n");
7604 return rc;
7605 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007606
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007607 pr_info("Console audio control enabled, mode: %s\n",
7608 (volume_control_allowed) ?
7609 "override (read/write)" :
7610 "monitor (read only)");
7611 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007612
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007613 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7614 "registering volume hotkeys as change notification\n");
7615 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7616 | TP_ACPI_HKEY_VOLUP_MASK
7617 | TP_ACPI_HKEY_VOLDWN_MASK
7618 | TP_ACPI_HKEY_MUTE_MASK);
7619
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007620 return 0;
7621}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007622
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007623static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007624{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007625 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007626
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007627 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007628 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007629 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007630 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007631 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007632 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007633 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007634 status & TP_EC_AUDIO_LVL_MSK);
7635
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007636 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007637 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007638
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007639 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007640 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007641 if (!tp_features.mixer_no_level_control) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007642 seq_printf(m, "commands:\tup, down\n");
7643 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7644 TP_EC_VOLUME_MAX);
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007645 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007646 }
7647 }
7648
7649 return 0;
7650}
7651
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007652static int volume_write(char *buf)
7653{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007654 u8 s;
7655 u8 new_level, new_mute;
7656 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007657 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007658 int rc;
7659
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007660 /*
7661 * We do allow volume control at driver startup, so that the
7662 * user can set initial state through the volume=... parameter hack.
7663 */
7664 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7665 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7666 tp_warned.volume_ctrl_forbidden = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007667 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7668 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007669 }
7670 return -EPERM;
7671 }
7672
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007673 rc = volume_get_status(&s);
7674 if (rc < 0)
7675 return rc;
7676
7677 new_level = s & TP_EC_AUDIO_LVL_MSK;
7678 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007679
7680 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007681 if (!tp_features.mixer_no_level_control) {
7682 if (strlencmp(cmd, "up") == 0) {
7683 if (new_mute)
7684 new_mute = 0;
7685 else if (new_level < TP_EC_VOLUME_MAX)
7686 new_level++;
7687 continue;
7688 } else if (strlencmp(cmd, "down") == 0) {
7689 if (new_mute)
7690 new_mute = 0;
7691 else if (new_level > 0)
7692 new_level--;
7693 continue;
7694 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7695 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7696 new_level = l;
7697 continue;
7698 }
7699 }
7700 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007701 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007702 else if (strlencmp(cmd, "unmute") == 0)
7703 new_mute = 0;
7704 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007705 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007706 }
7707
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007708 if (tp_features.mixer_no_level_control) {
7709 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7710 new_mute ? "" : "un");
7711 rc = volume_set_mute(!!new_mute);
7712 } else {
7713 tpacpi_disclose_usertask("procfs volume",
7714 "%smute and set level to %d\n",
7715 new_mute ? "" : "un", new_level);
7716 rc = volume_set_status(new_mute | new_level);
7717 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007718 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007719
7720 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007721}
7722
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007723static struct ibm_struct volume_driver_data = {
7724 .name = "volume",
7725 .read = volume_read,
7726 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007727 .exit = volume_exit,
7728 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007729 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007730 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007731};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007732
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007733#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7734
7735#define alsa_card NULL
7736
Tobias Klauserbb55a2e2016-11-25 14:18:07 +01007737static inline void volume_alsa_notify_change(void)
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007738{
7739}
7740
7741static int __init volume_init(struct ibm_init_struct *iibm)
7742{
Joe Perches0978e012011-04-04 10:06:25 -07007743 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007744
7745 return 1;
7746}
7747
7748static struct ibm_struct volume_driver_data = {
7749 .name = "volume",
7750};
7751
7752#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7753
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007754/*************************************************************************
7755 * Fan subdriver
7756 */
7757
7758/*
7759 * FAN ACCESS MODES
7760 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007761 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007762 * ACPI GFAN method: returns fan level
7763 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007764 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007765 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007766 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007767 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007768 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7769 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007770 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7771 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007772 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007773 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007774 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7775 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007776 *
7777 * Fan speed changes of any sort (including those caused by the
7778 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007779 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007780 * limited.
7781 *
7782 * Reading is not available if GFAN exists.
7783 * Writing is not available if SFAN exists.
7784 *
7785 * Bits
7786 * 7 automatic mode engaged;
7787 * (default operation mode of the ThinkPad)
7788 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007789 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007790 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007791 * the tachometer while the fan controller ramps up
7792 * the speed (which can take up to a few *minutes*).
7793 * Speeds up fan to 100% duty-cycle, which is far above
7794 * the standard RPM levels. It is not impossible that
7795 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007796 * 5-3 unused in some models. Extra bits for fan level
7797 * in others, but still useless as all values above
7798 * 7 map to the same speed as level 7 in these models.
7799 * 2-0 fan level (0..7 usually)
7800 * 0x00 = stop
7801 * 0x07 = max (set when temperatures critical)
7802 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007803 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007804 *
7805 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007806 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007807 * does so, its initial value is meaningless (0x07).
7808 *
7809 * For firmware bugs, refer to:
7810 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7811 *
7812 * ----
7813 *
7814 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7815 * Main fan tachometer reading (in RPM)
7816 *
7817 * This register is present on all ThinkPads with a new-style EC, and
7818 * it is known not to be present on the A21m/e, and T22, as there is
7819 * something else in offset 0x84 according to the ACPI DSDT. Other
7820 * ThinkPads from this same time period (and earlier) probably lack the
7821 * tachometer as well.
7822 *
Nick Andrew877d0312009-01-26 11:06:57 +01007823 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007824 * was never fixed by IBM to report the EC firmware version string
7825 * probably support the tachometer (like the early X models), so
7826 * detecting it is quite hard. We need more data to know for sure.
7827 *
7828 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7829 * might result.
7830 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007831 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7832 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007833 *
7834 * For firmware bugs, refer to:
7835 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7836 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007837 * ----
7838 *
7839 * ThinkPad EC register 0x31 bit 0 (only on select models)
7840 *
7841 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7842 * show the speed of the main fan. When bit 0 of EC register 0x31
7843 * is one, the tachometer registers show the speed of the auxiliary
7844 * fan.
7845 *
7846 * Fan control seems to affect both fans, regardless of the state
7847 * of this bit.
7848 *
7849 * So far, only the firmware for the X60/X61 non-tablet versions
7850 * seem to support this (firmware TP-7M).
7851 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007852 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007853 * ThinkPad X31, X40, X41. Not available in the X60.
7854 *
7855 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7856 * high speed. ACPI DSDT seems to map these three speeds to levels
7857 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7858 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7859 *
7860 * The speeds are stored on handles
7861 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7862 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007863 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007864 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7865 *
7866 * ACPI DSDT switches which set is in use depending on various
7867 * factors.
7868 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007869 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007870 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7871 * but the ACPI tables just mention level 7.
7872 */
7873
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007874enum { /* Fan control constants */
7875 fan_status_offset = 0x2f, /* EC register 0x2f */
7876 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7877 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007878 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7879 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007880
7881 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7882 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7883
7884 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7885};
7886
7887enum fan_status_access_mode {
7888 TPACPI_FAN_NONE = 0, /* No fan status or control */
7889 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7890 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7891};
7892
7893enum fan_control_access_mode {
7894 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7895 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7896 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7897 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7898};
7899
7900enum fan_control_commands {
7901 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7902 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7903 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7904 * and also watchdog cmd */
7905};
7906
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307907static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007908
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007909static enum fan_status_access_mode fan_status_access_mode;
7910static enum fan_control_access_mode fan_control_access_mode;
7911static enum fan_control_commands fan_control_commands;
7912
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007913static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007914static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007915static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007916static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007917
7918static struct mutex fan_mutex;
7919
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007920static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007921static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007922
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007923TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7924TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007925 "\\FSPD", /* 600e/x, 770e, 770x */
7926 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007927TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007928 "JFNS", /* 770x-JL */
7929 ); /* all others */
7930
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007931/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007932 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7933 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7934 * be in auto mode (0x80).
7935 *
7936 * This is corrected by any write to HFSP either by the driver, or
7937 * by the firmware.
7938 *
7939 * We assume 0x07 really means auto mode while this quirk is active,
7940 * as this is far more likely than the ThinkPad being in level 7,
7941 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007942 *
7943 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7944 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007945 */
7946
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007947static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007948{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007949 if (fan_control_initial_status == 0x07) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007950 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 -03007951 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007952 }
7953}
7954
7955static void fan_quirk1_handle(u8 *fan_status)
7956{
7957 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7958 if (*fan_status != fan_control_initial_status) {
7959 /* something changed the HFSP regisnter since
7960 * driver init time, so it is not undefined
7961 * anymore */
7962 tp_features.fan_ctrl_status_undef = 0;
7963 } else {
7964 /* Return most likely status. In fact, it
7965 * might be the only possible status */
7966 *fan_status = TP_EC_FAN_AUTO;
7967 }
7968 }
7969}
7970
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007971/* Select main fan on X60/X61, NOOP on others */
7972static bool fan_select_fan1(void)
7973{
7974 if (tp_features.second_fan) {
7975 u8 val;
7976
7977 if (ec_read(fan_select_offset, &val) < 0)
7978 return false;
7979 val &= 0xFEU;
7980 if (ec_write(fan_select_offset, val) < 0)
7981 return false;
7982 }
7983 return true;
7984}
7985
7986/* Select secondary fan on X60/X61 */
7987static bool fan_select_fan2(void)
7988{
7989 u8 val;
7990
7991 if (!tp_features.second_fan)
7992 return false;
7993
7994 if (ec_read(fan_select_offset, &val) < 0)
7995 return false;
7996 val |= 0x01U;
7997 if (ec_write(fan_select_offset, val) < 0)
7998 return false;
7999
8000 return true;
8001}
8002
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008003/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008004 * Call with fan_mutex held
8005 */
8006static void fan_update_desired_level(u8 status)
8007{
8008 if ((status &
8009 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8010 if (status > 7)
8011 fan_control_desired_level = 7;
8012 else
8013 fan_control_desired_level = status;
8014 }
8015}
8016
8017static int fan_get_status(u8 *status)
8018{
8019 u8 s;
8020
8021 /* TODO:
8022 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8023
8024 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07008025 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008026 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07008027 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008028
Dan Carpentereceeb432012-09-01 12:54:07 -07008029 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008030 return -EIO;
8031
8032 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07008033 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008034
8035 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07008036 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008037 case TPACPI_FAN_RD_TPEC:
8038 /* all except 570, 600e/x, 770e, 770x */
8039 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8040 return -EIO;
8041
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008042 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008043 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008044 fan_quirk1_handle(status);
8045 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008046
8047 break;
8048
8049 default:
8050 return -ENXIO;
8051 }
8052
8053 return 0;
8054}
8055
8056static int fan_get_status_safe(u8 *status)
8057{
8058 int rc;
8059 u8 s;
8060
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008061 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008062 return -ERESTARTSYS;
8063 rc = fan_get_status(&s);
8064 if (!rc)
8065 fan_update_desired_level(s);
8066 mutex_unlock(&fan_mutex);
8067
Dan Carpentera7718362016-04-15 17:46:34 +03008068 if (rc)
8069 return rc;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008070 if (status)
8071 *status = s;
8072
Dan Carpentera7718362016-04-15 17:46:34 +03008073 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008074}
8075
8076static int fan_get_speed(unsigned int *speed)
8077{
8078 u8 hi, lo;
8079
8080 switch (fan_status_access_mode) {
8081 case TPACPI_FAN_RD_TPEC:
8082 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008083 if (unlikely(!fan_select_fan1()))
8084 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008085 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8086 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8087 return -EIO;
8088
8089 if (likely(speed))
8090 *speed = (hi << 8) | lo;
8091
8092 break;
8093
8094 default:
8095 return -ENXIO;
8096 }
8097
8098 return 0;
8099}
8100
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008101static int fan2_get_speed(unsigned int *speed)
8102{
8103 u8 hi, lo;
8104 bool rc;
8105
8106 switch (fan_status_access_mode) {
8107 case TPACPI_FAN_RD_TPEC:
8108 /* all except 570, 600e/x, 770e, 770x */
8109 if (unlikely(!fan_select_fan2()))
8110 return -EIO;
8111 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8112 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8113 fan_select_fan1(); /* play it safe */
8114 if (rc)
8115 return -EIO;
8116
8117 if (likely(speed))
8118 *speed = (hi << 8) | lo;
8119
8120 break;
8121
8122 default:
8123 return -ENXIO;
8124 }
8125
8126 return 0;
8127}
8128
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008129static int fan_set_level(int level)
8130{
8131 if (!fan_control_allowed)
8132 return -EPERM;
8133
8134 switch (fan_control_access_mode) {
8135 case TPACPI_FAN_WR_ACPI_SFAN:
8136 if (level >= 0 && level <= 7) {
8137 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8138 return -EIO;
8139 } else
8140 return -EINVAL;
8141 break;
8142
8143 case TPACPI_FAN_WR_ACPI_FANS:
8144 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008145 if (!(level & TP_EC_FAN_AUTO) &&
8146 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008147 ((level < 0) || (level > 7)))
8148 return -EINVAL;
8149
8150 /* safety net should the EC not support AUTO
8151 * or FULLSPEED mode bits and just ignore them */
8152 if (level & TP_EC_FAN_FULLSPEED)
8153 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01008154 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008155 level |= 4; /* safety min speed 4 */
8156
8157 if (!acpi_ec_write(fan_status_offset, level))
8158 return -EIO;
8159 else
8160 tp_features.fan_ctrl_status_undef = 0;
8161 break;
8162
8163 default:
8164 return -ENXIO;
8165 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008166
8167 vdbg_printk(TPACPI_DBG_FAN,
8168 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008169 return 0;
8170}
8171
8172static int fan_set_level_safe(int level)
8173{
8174 int rc;
8175
8176 if (!fan_control_allowed)
8177 return -EPERM;
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
8182 if (level == TPACPI_FAN_LAST_LEVEL)
8183 level = fan_control_desired_level;
8184
8185 rc = fan_set_level(level);
8186 if (!rc)
8187 fan_update_desired_level(level);
8188
8189 mutex_unlock(&fan_mutex);
8190 return rc;
8191}
8192
8193static int fan_set_enable(void)
8194{
8195 u8 s;
8196 int rc;
8197
8198 if (!fan_control_allowed)
8199 return -EPERM;
8200
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008201 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008202 return -ERESTARTSYS;
8203
8204 switch (fan_control_access_mode) {
8205 case TPACPI_FAN_WR_ACPI_FANS:
8206 case TPACPI_FAN_WR_TPEC:
8207 rc = fan_get_status(&s);
8208 if (rc < 0)
8209 break;
8210
8211 /* Don't go out of emergency fan mode */
8212 if (s != 7) {
8213 s &= 0x07;
8214 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8215 }
8216
8217 if (!acpi_ec_write(fan_status_offset, s))
8218 rc = -EIO;
8219 else {
8220 tp_features.fan_ctrl_status_undef = 0;
8221 rc = 0;
8222 }
8223 break;
8224
8225 case TPACPI_FAN_WR_ACPI_SFAN:
8226 rc = fan_get_status(&s);
8227 if (rc < 0)
8228 break;
8229
8230 s &= 0x07;
8231
8232 /* Set fan to at least level 4 */
8233 s |= 4;
8234
8235 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008236 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008237 else
8238 rc = 0;
8239 break;
8240
8241 default:
8242 rc = -ENXIO;
8243 }
8244
8245 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008246
8247 if (!rc)
8248 vdbg_printk(TPACPI_DBG_FAN,
8249 "fan control: set fan control register to 0x%02x\n",
8250 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008251 return rc;
8252}
8253
8254static int fan_set_disable(void)
8255{
8256 int rc;
8257
8258 if (!fan_control_allowed)
8259 return -EPERM;
8260
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008261 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008262 return -ERESTARTSYS;
8263
8264 rc = 0;
8265 switch (fan_control_access_mode) {
8266 case TPACPI_FAN_WR_ACPI_FANS:
8267 case TPACPI_FAN_WR_TPEC:
8268 if (!acpi_ec_write(fan_status_offset, 0x00))
8269 rc = -EIO;
8270 else {
8271 fan_control_desired_level = 0;
8272 tp_features.fan_ctrl_status_undef = 0;
8273 }
8274 break;
8275
8276 case TPACPI_FAN_WR_ACPI_SFAN:
8277 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8278 rc = -EIO;
8279 else
8280 fan_control_desired_level = 0;
8281 break;
8282
8283 default:
8284 rc = -ENXIO;
8285 }
8286
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008287 if (!rc)
8288 vdbg_printk(TPACPI_DBG_FAN,
8289 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008290
8291 mutex_unlock(&fan_mutex);
8292 return rc;
8293}
8294
8295static int fan_set_speed(int speed)
8296{
8297 int rc;
8298
8299 if (!fan_control_allowed)
8300 return -EPERM;
8301
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008302 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008303 return -ERESTARTSYS;
8304
8305 rc = 0;
8306 switch (fan_control_access_mode) {
8307 case TPACPI_FAN_WR_ACPI_FANS:
8308 if (speed >= 0 && speed <= 65535) {
8309 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8310 speed, speed, speed))
8311 rc = -EIO;
8312 } else
8313 rc = -EINVAL;
8314 break;
8315
8316 default:
8317 rc = -ENXIO;
8318 }
8319
8320 mutex_unlock(&fan_mutex);
8321 return rc;
8322}
8323
8324static void fan_watchdog_reset(void)
8325{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008326 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8327 return;
8328
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008329 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008330 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8331 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8332 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8333 else
8334 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008335}
8336
8337static void fan_watchdog_fire(struct work_struct *ignored)
8338{
8339 int rc;
8340
8341 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8342 return;
8343
Joe Perches0978e012011-04-04 10:06:25 -07008344 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008345 rc = fan_set_enable();
8346 if (rc < 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008347 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8348 rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008349 /* reschedule for later */
8350 fan_watchdog_reset();
8351 }
8352}
8353
8354/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008355 * SYSFS fan layout: hwmon compatible (device)
8356 *
8357 * pwm*_enable:
8358 * 0: "disengaged" mode
8359 * 1: manual mode
8360 * 2: native EC "auto" mode (recommended, hardware default)
8361 *
8362 * pwm*: set speed in manual mode, ignored otherwise.
8363 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8364 * interpolation.
8365 *
8366 * fan*_input: tachometer reading, RPM
8367 *
8368 *
8369 * SYSFS fan layout: extensions
8370 *
8371 * fan_watchdog (driver):
8372 * fan watchdog interval in seconds, 0 disables (default), max 120
8373 */
8374
8375/* sysfs fan pwm1_enable ----------------------------------------------- */
8376static ssize_t fan_pwm1_enable_show(struct device *dev,
8377 struct device_attribute *attr,
8378 char *buf)
8379{
8380 int res, mode;
8381 u8 status;
8382
8383 res = fan_get_status_safe(&status);
8384 if (res)
8385 return res;
8386
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008387 if (status & TP_EC_FAN_FULLSPEED) {
8388 mode = 0;
8389 } else if (status & TP_EC_FAN_AUTO) {
8390 mode = 2;
8391 } else
8392 mode = 1;
8393
8394 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8395}
8396
8397static ssize_t fan_pwm1_enable_store(struct device *dev,
8398 struct device_attribute *attr,
8399 const char *buf, size_t count)
8400{
8401 unsigned long t;
8402 int res, level;
8403
8404 if (parse_strtoul(buf, 2, &t))
8405 return -EINVAL;
8406
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008407 tpacpi_disclose_usertask("hwmon pwm1_enable",
8408 "set fan mode to %lu\n", t);
8409
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008410 switch (t) {
8411 case 0:
8412 level = TP_EC_FAN_FULLSPEED;
8413 break;
8414 case 1:
8415 level = TPACPI_FAN_LAST_LEVEL;
8416 break;
8417 case 2:
8418 level = TP_EC_FAN_AUTO;
8419 break;
8420 case 3:
8421 /* reserved for software-controlled auto mode */
8422 return -ENOSYS;
8423 default:
8424 return -EINVAL;
8425 }
8426
8427 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008428 if (res == -ENXIO)
8429 return -EINVAL;
8430 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008431 return res;
8432
8433 fan_watchdog_reset();
8434
8435 return count;
8436}
8437
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008438static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8439 fan_pwm1_enable_show, fan_pwm1_enable_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008440
8441/* sysfs fan pwm1 ------------------------------------------------------ */
8442static ssize_t fan_pwm1_show(struct device *dev,
8443 struct device_attribute *attr,
8444 char *buf)
8445{
8446 int res;
8447 u8 status;
8448
8449 res = fan_get_status_safe(&status);
8450 if (res)
8451 return res;
8452
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008453 if ((status &
8454 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8455 status = fan_control_desired_level;
8456
8457 if (status > 7)
8458 status = 7;
8459
8460 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8461}
8462
8463static ssize_t fan_pwm1_store(struct device *dev,
8464 struct device_attribute *attr,
8465 const char *buf, size_t count)
8466{
8467 unsigned long s;
8468 int rc;
8469 u8 status, newlevel;
8470
8471 if (parse_strtoul(buf, 255, &s))
8472 return -EINVAL;
8473
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008474 tpacpi_disclose_usertask("hwmon pwm1",
8475 "set fan speed to %lu\n", s);
8476
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008477 /* scale down from 0-255 to 0-7 */
8478 newlevel = (s >> 5) & 0x07;
8479
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008480 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008481 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008482
8483 rc = fan_get_status(&status);
8484 if (!rc && (status &
8485 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8486 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008487 if (rc == -ENXIO)
8488 rc = -EINVAL;
8489 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008490 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008491 fan_watchdog_reset();
8492 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008493 }
8494
8495 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008496 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008497}
8498
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008499static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008500
8501/* sysfs fan fan1_input ------------------------------------------------ */
8502static ssize_t fan_fan1_input_show(struct device *dev,
8503 struct device_attribute *attr,
8504 char *buf)
8505{
8506 int res;
8507 unsigned int speed;
8508
8509 res = fan_get_speed(&speed);
8510 if (res < 0)
8511 return res;
8512
8513 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8514}
8515
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008516static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008517
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008518/* sysfs fan fan2_input ------------------------------------------------ */
8519static ssize_t fan_fan2_input_show(struct device *dev,
8520 struct device_attribute *attr,
8521 char *buf)
8522{
8523 int res;
8524 unsigned int speed;
8525
8526 res = fan2_get_speed(&speed);
8527 if (res < 0)
8528 return res;
8529
8530 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8531}
8532
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008533static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008534
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008535/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008536static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008537{
8538 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8539}
8540
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008541static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8542 size_t count)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008543{
8544 unsigned long t;
8545
8546 if (parse_strtoul(buf, 120, &t))
8547 return -EINVAL;
8548
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008549 if (!fan_control_allowed)
8550 return -EPERM;
8551
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008552 fan_watchdog_maxinterval = t;
8553 fan_watchdog_reset();
8554
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008555 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8556
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008557 return count;
8558}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008559static DRIVER_ATTR_RW(fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008560
8561/* --------------------------------------------------------------------- */
8562static struct attribute *fan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008563 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8564 &dev_attr_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008565 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008566 NULL
8567};
8568
8569static const struct attribute_group fan_attr_group = {
8570 .attrs = fan_attributes,
8571};
8572
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008573#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8574#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008575
8576#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8577 { .vendor = PCI_VENDOR_ID_IBM, \
8578 .bios = TPACPI_MATCH_ANY, \
8579 .ec = TPID(__id1, __id2), \
8580 .quirks = __quirks }
8581
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008582#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8583 { .vendor = PCI_VENDOR_ID_LENOVO, \
8584 .bios = TPACPI_MATCH_ANY, \
8585 .ec = TPID(__id1, __id2), \
8586 .quirks = __quirks }
8587
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008588static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8589 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8590 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8591 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8592 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008593 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008594};
8595
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008596#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008597#undef TPACPI_FAN_QI
8598
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008599static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008600{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008601 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008602 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008603
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008604 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8605 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008606
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008607 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008608 fan_status_access_mode = TPACPI_FAN_NONE;
8609 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008610 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008611 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008612 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008613 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008614 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008615
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008616 if (tpacpi_is_ibm()) {
8617 TPACPI_ACPIHANDLE_INIT(fans);
8618 TPACPI_ACPIHANDLE_INIT(gfan);
8619 TPACPI_ACPIHANDLE_INIT(sfan);
8620 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008621
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008622 quirks = tpacpi_check_quirks(fan_quirk_table,
8623 ARRAY_SIZE(fan_quirk_table));
8624
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008625 if (gfan_handle) {
8626 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008627 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008628 } else {
8629 /* all other ThinkPads: note that even old-style
8630 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008631 if (likely(acpi_ec_read(fan_status_offset,
8632 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008633 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008634 if (quirks & TPACPI_FAN_Q1)
8635 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008636 if (quirks & TPACPI_FAN_2FAN) {
8637 tp_features.second_fan = 1;
8638 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8639 "secondary fan support enabled\n");
8640 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008641 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008642 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008643 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008644 }
8645 }
8646
8647 if (sfan_handle) {
8648 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008649 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008650 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008651 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008652 } else {
8653 if (!gfan_handle) {
8654 /* gfan without sfan means no fan control */
8655 /* all other models implement TP EC 0x2f control */
8656
8657 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008658 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008659 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008660 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008661 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008662 TPACPI_FAN_CMD_SPEED |
8663 TPACPI_FAN_CMD_LEVEL |
8664 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008665 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008666 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008667 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008668 TPACPI_FAN_CMD_LEVEL |
8669 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008670 }
8671 }
8672 }
8673
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008674 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8675 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008676 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8677 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8678 fan_status_access_mode, fan_control_access_mode);
8679
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008680 /* fan control master switch */
8681 if (!fan_control_allowed) {
8682 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8683 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008684 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008685 "fan control features disabled by parameter\n");
8686 }
8687
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008688 /* update fan_control_desired_level */
8689 if (fan_status_access_mode != TPACPI_FAN_NONE)
8690 fan_get_status_safe(NULL);
8691
8692 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8693 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008694 if (tp_features.second_fan) {
8695 /* attach second fan tachometer */
8696 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008697 &dev_attr_fan2_input.attr;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008698 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008699 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008700 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008701 if (rc < 0)
8702 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008703
8704 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8705 &driver_attr_fan_watchdog);
8706 if (rc < 0) {
8707 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8708 &fan_attr_group);
8709 return rc;
8710 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008711 return 0;
8712 } else
8713 return 1;
8714}
8715
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008716static void fan_exit(void)
8717{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008718 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008719 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008720
8721 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008722 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008723 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8724 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008725
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008726 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008727 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008728}
8729
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008730static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008731{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008732 int rc;
8733
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008734 if (!fan_control_allowed)
8735 return;
8736
8737 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008738 fan_control_resume_level = 0;
8739 rc = fan_get_status_safe(&fan_control_resume_level);
8740 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008741 pr_notice("failed to read fan level for later restore during resume: %d\n",
8742 rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008743
8744 /* if it is undefined, don't attempt to restore it.
8745 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008746 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008747 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008748}
8749
8750static void fan_resume(void)
8751{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008752 u8 current_level = 7;
8753 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008754 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008755
8756 /* DSDT *always* updates status on resume */
8757 tp_features.fan_ctrl_status_undef = 0;
8758
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008759 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008760 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008761 (fan_get_status_safe(&current_level) < 0))
8762 return;
8763
8764 switch (fan_control_access_mode) {
8765 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008766 /* never decrease fan level */
8767 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008768 break;
8769 case TPACPI_FAN_WR_ACPI_FANS:
8770 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008771 /* never decrease fan level, scale is:
8772 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8773 *
8774 * We expect the firmware to set either 7 or AUTO, but we
8775 * handle FULLSPEED out of paranoia.
8776 *
8777 * So, we can safely only restore FULLSPEED or 7, anything
8778 * else could slow the fan. Restoring AUTO is useless, at
8779 * best that's exactly what the DSDT already set (it is the
8780 * slower it uses).
8781 *
8782 * Always keep in mind that the DSDT *will* have set the
8783 * fans to what the vendor supposes is the best level. We
8784 * muck with it only to speed the fan up.
8785 */
8786 if (fan_control_resume_level != 7 &&
8787 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8788 return;
8789 else
8790 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8791 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008792 break;
8793 default:
8794 return;
8795 }
8796 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008797 pr_notice("restoring fan level to 0x%02x\n",
8798 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008799 rc = fan_set_level_safe(fan_control_resume_level);
8800 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008801 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008802 }
8803}
8804
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008805static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008806{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008807 int rc;
8808 u8 status;
8809 unsigned int speed = 0;
8810
8811 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008812 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008813 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008814 rc = fan_get_status_safe(&status);
8815 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008816 return rc;
8817
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008818 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008819 "level:\t\t%d\n",
8820 (status != 0) ? "enabled" : "disabled", status);
8821 break;
8822
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008823 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008824 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008825 rc = fan_get_status_safe(&status);
8826 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008827 return rc;
8828
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008829 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008830 (status != 0) ? "enabled" : "disabled");
8831
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008832 rc = fan_get_speed(&speed);
8833 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008834 return rc;
8835
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008836 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008837
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008838 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008839 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008840 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008841 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008842 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008843 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008844 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008845 break;
8846
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008847 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008848 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008849 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008850 }
8851
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008852 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008853 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008854
8855 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008856 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008857 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008858 break;
8859
8860 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008861 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008862 break;
8863 }
8864 }
8865
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008866 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008867 seq_printf(m, "commands:\tenable, disable\n"
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008868 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008869
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008870 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008871 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008872
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008873 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008874}
8875
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008876static int fan_write_cmd_level(const char *cmd, int *rc)
8877{
8878 int level;
8879
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008880 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008881 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008882 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008883 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008884 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008885 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008886 return 0;
8887
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008888 *rc = fan_set_level_safe(level);
8889 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008890 pr_err("level command accepted for unsupported access mode %d\n",
8891 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008892 else if (!*rc)
8893 tpacpi_disclose_usertask("procfs fan",
8894 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008895
8896 return 1;
8897}
8898
8899static int fan_write_cmd_enable(const char *cmd, int *rc)
8900{
8901 if (strlencmp(cmd, "enable") != 0)
8902 return 0;
8903
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008904 *rc = fan_set_enable();
8905 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008906 pr_err("enable command accepted for unsupported access mode %d\n",
8907 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008908 else if (!*rc)
8909 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008910
8911 return 1;
8912}
8913
8914static int fan_write_cmd_disable(const char *cmd, int *rc)
8915{
8916 if (strlencmp(cmd, "disable") != 0)
8917 return 0;
8918
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008919 *rc = fan_set_disable();
8920 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008921 pr_err("disable command accepted for unsupported access mode %d\n",
8922 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008923 else if (!*rc)
8924 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008925
8926 return 1;
8927}
8928
8929static int fan_write_cmd_speed(const char *cmd, int *rc)
8930{
8931 int speed;
8932
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008933 /* TODO:
8934 * Support speed <low> <medium> <high> ? */
8935
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008936 if (sscanf(cmd, "speed %d", &speed) != 1)
8937 return 0;
8938
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008939 *rc = fan_set_speed(speed);
8940 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008941 pr_err("speed command accepted for unsupported access mode %d\n",
8942 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008943 else if (!*rc)
8944 tpacpi_disclose_usertask("procfs fan",
8945 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008946
8947 return 1;
8948}
8949
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008950static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8951{
8952 int interval;
8953
8954 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8955 return 0;
8956
8957 if (interval < 0 || interval > 120)
8958 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008959 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008960 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008961 tpacpi_disclose_usertask("procfs fan",
8962 "set watchdog timer to %d\n",
8963 interval);
8964 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008965
8966 return 1;
8967}
8968
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008969static int fan_write(char *buf)
8970{
8971 char *cmd;
8972 int rc = 0;
8973
8974 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008975 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008976 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008977 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008978 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008979 fan_write_cmd_disable(cmd, &rc) ||
8980 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008981 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008982 fan_write_cmd_speed(cmd, &rc))
8983 )
8984 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008985 else if (!rc)
8986 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008987 }
8988
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008989 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008990}
8991
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008992static struct ibm_struct fan_driver_data = {
8993 .name = "fan",
8994 .read = fan_read,
8995 .write = fan_write,
8996 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008997 .suspend = fan_suspend,
8998 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008999};
9000
David Henningsson420f9732013-10-16 23:10:31 +02009001/*************************************************************************
9002 * Mute LED subdriver
9003 */
9004
9005
9006struct tp_led_table {
9007 acpi_string name;
9008 int on_value;
9009 int off_value;
9010 int state;
9011};
9012
9013static struct tp_led_table led_tables[] = {
9014 [TPACPI_LED_MUTE] = {
9015 .name = "SSMS",
9016 .on_value = 1,
9017 .off_value = 0,
9018 },
9019 [TPACPI_LED_MICMUTE] = {
9020 .name = "MMTS",
9021 .on_value = 2,
9022 .off_value = 0,
9023 },
9024};
9025
9026static int mute_led_on_off(struct tp_led_table *t, bool state)
9027{
9028 acpi_handle temp;
9029 int output;
9030
Axel Lin3526eca2016-09-19 09:33:51 +08009031 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009032 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9033 return -EIO;
9034 }
9035
9036 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9037 state ? t->on_value : t->off_value))
9038 return -EIO;
9039
9040 t->state = state;
9041 return state;
9042}
9043
9044int tpacpi_led_set(int whichled, bool on)
9045{
9046 struct tp_led_table *t;
9047
9048 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9049 return -EINVAL;
9050
9051 t = &led_tables[whichled];
9052 if (t->state < 0 || t->state == on)
9053 return t->state;
9054 return mute_led_on_off(t, on);
9055}
9056EXPORT_SYMBOL_GPL(tpacpi_led_set);
9057
9058static int mute_led_init(struct ibm_init_struct *iibm)
9059{
9060 acpi_handle temp;
9061 int i;
9062
9063 for (i = 0; i < TPACPI_LED_MAX; i++) {
9064 struct tp_led_table *t = &led_tables[i];
9065 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9066 mute_led_on_off(t, false);
9067 else
9068 t->state = -ENODEV;
9069 }
9070 return 0;
9071}
9072
9073static void mute_led_exit(void)
9074{
9075 int i;
9076
9077 for (i = 0; i < TPACPI_LED_MAX; i++)
9078 tpacpi_led_set(i, false);
9079}
9080
Takashi Iwai119f4492014-02-12 16:32:45 +01009081static void mute_led_resume(void)
9082{
9083 int i;
9084
9085 for (i = 0; i < TPACPI_LED_MAX; i++) {
9086 struct tp_led_table *t = &led_tables[i];
9087 if (t->state >= 0)
9088 mute_led_on_off(t, t->state);
9089 }
9090}
9091
David Henningsson420f9732013-10-16 23:10:31 +02009092static struct ibm_struct mute_led_driver_data = {
9093 .name = "mute_led",
9094 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01009095 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02009096};
9097
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009098/****************************************************************************
9099 ****************************************************************************
9100 *
9101 * Infrastructure
9102 *
9103 ****************************************************************************
9104 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07009105
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009106/*
9107 * HKEY event callout for other subdrivers go here
9108 * (yes, it is ugly, but it is quick, safe, and gets the job done
9109 */
9110static void tpacpi_driver_event(const unsigned int hkey_event)
9111{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00009112 if (ibm_backlight_device) {
9113 switch (hkey_event) {
9114 case TP_HKEY_EV_BRGHT_UP:
9115 case TP_HKEY_EV_BRGHT_DOWN:
9116 tpacpi_brightness_notify_change();
9117 }
9118 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009119 if (alsa_card) {
9120 switch (hkey_event) {
9121 case TP_HKEY_EV_VOL_UP:
9122 case TP_HKEY_EV_VOL_DOWN:
9123 case TP_HKEY_EV_VOL_MUTE:
9124 volume_alsa_notify_change();
9125 }
9126 }
Hans de Goedec685e202017-02-09 16:44:13 +01009127 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9128 enum led_brightness brightness;
9129
9130 mutex_lock(&kbdlight_mutex);
9131
9132 /*
9133 * Check the brightness actually changed, setting the brightness
9134 * through kbdlight_set_level() also triggers this event.
9135 */
9136 brightness = kbdlight_sysfs_get(NULL);
9137 if (kbdlight_brightness != brightness) {
9138 kbdlight_brightness = brightness;
9139 led_classdev_notify_brightness_hw_changed(
9140 &tpacpi_led_kbdlight.led_classdev, brightness);
9141 }
9142
9143 mutex_unlock(&kbdlight_mutex);
9144 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009145}
9146
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009147static void hotkey_driver_event(const unsigned int scancode)
9148{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03009149 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009150}
9151
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009152/* sysfs name ---------------------------------------------------------- */
9153static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
9154 struct device_attribute *attr,
9155 char *buf)
9156{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009157 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009158}
9159
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009160static DEVICE_ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009161
9162/* --------------------------------------------------------------------- */
9163
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009164/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03009165static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009166
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009167/*
9168 * Module and infrastructure proble, init and exit handling
9169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009170
Rusty Russell90ab5ee2012-01-13 09:32:20 +10309171static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009172
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009173#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009174static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009175{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009176 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009177
Jan van den Berg72a979f2014-09-17 00:01:08 +02009178 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009179}
9180#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9181
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009182static void ibm_exit(struct ibm_struct *ibm)
9183{
9184 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9185
9186 list_del_init(&ibm->all_drivers);
9187
9188 if (ibm->flags.acpi_notify_installed) {
9189 dbg_printk(TPACPI_DBG_EXIT,
9190 "%s: acpi_remove_notify_handler\n", ibm->name);
9191 BUG_ON(!ibm->acpi);
9192 acpi_remove_notify_handler(*ibm->acpi->handle,
9193 ibm->acpi->type,
9194 dispatch_acpi_notify);
9195 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009196 }
9197
9198 if (ibm->flags.proc_created) {
9199 dbg_printk(TPACPI_DBG_EXIT,
9200 "%s: remove_proc_entry\n", ibm->name);
9201 remove_proc_entry(ibm->name, proc_dir);
9202 ibm->flags.proc_created = 0;
9203 }
9204
9205 if (ibm->flags.acpi_driver_registered) {
9206 dbg_printk(TPACPI_DBG_EXIT,
9207 "%s: acpi_bus_unregister_driver\n", ibm->name);
9208 BUG_ON(!ibm->acpi);
9209 acpi_bus_unregister_driver(ibm->acpi->driver);
9210 kfree(ibm->acpi->driver);
9211 ibm->acpi->driver = NULL;
9212 ibm->flags.acpi_driver_registered = 0;
9213 }
9214
9215 if (ibm->flags.init_called && ibm->exit) {
9216 ibm->exit();
9217 ibm->flags.init_called = 0;
9218 }
9219
9220 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9221}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009222
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009223static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009224{
9225 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009226 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009227 struct proc_dir_entry *entry;
9228
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009229 BUG_ON(ibm == NULL);
9230
9231 INIT_LIST_HEAD(&ibm->all_drivers);
9232
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009233 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009234 return 0;
9235
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009236 dbg_printk(TPACPI_DBG_INIT,
9237 "probing for %s\n", ibm->name);
9238
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009239 if (iibm->init) {
9240 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009241 if (ret > 0)
9242 return 0; /* probe failed */
9243 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009244 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009245
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009246 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009247 }
9248
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009249 if (ibm->acpi) {
9250 if (ibm->acpi->hid) {
9251 ret = register_tpacpi_subdriver(ibm);
9252 if (ret)
9253 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009254 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009255
9256 if (ibm->acpi->notify) {
9257 ret = setup_acpi_notify(ibm);
9258 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07009259 pr_notice("disabling subdriver %s\n",
9260 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009261 ret = 0;
9262 goto err_out;
9263 }
9264 if (ret < 0)
9265 goto err_out;
9266 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009267 }
9268
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009269 dbg_printk(TPACPI_DBG_INIT,
9270 "%s installed\n", ibm->name);
9271
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009272 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04009273 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009274
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009275 if (!mode)
9276 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009277 if (ibm->write)
9278 mode |= S_IWUSR;
9279 entry = proc_create_data(ibm->name, mode, proc_dir,
9280 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009281 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07009282 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009283 ret = -ENODEV;
9284 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009285 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009286 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009288
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009289 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9290
Linus Torvalds1da177e2005-04-16 15:20:36 -07009291 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009292
9293err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009294 dbg_printk(TPACPI_DBG_INIT,
9295 "%s: at error exit path with result %d\n",
9296 ibm->name, ret);
9297
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009298 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +02009299 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009300}
9301
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009302/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009303
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009304static bool __pure __init tpacpi_is_fw_digit(const char c)
9305{
9306 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9307}
9308
Jan van den Berg72a979f2014-09-17 00:01:08 +02009309static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009310 const char t)
9311{
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009312 /*
9313 * Most models: xxyTkkWW (#.##c)
9314 * Ancient 570/600 and -SL lacks (#.##c)
9315 */
9316 if (s && strlen(s) >= 8 &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009317 tpacpi_is_fw_digit(s[0]) &&
9318 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009319 s[2] == t &&
9320 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009321 tpacpi_is_fw_digit(s[4]) &&
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009322 tpacpi_is_fw_digit(s[5]))
9323 return true;
9324
9325 /* New models: xxxyTkkW (#.##c); T550 and some others */
9326 return s && strlen(s) >= 8 &&
9327 tpacpi_is_fw_digit(s[0]) &&
9328 tpacpi_is_fw_digit(s[1]) &&
9329 tpacpi_is_fw_digit(s[2]) &&
9330 s[3] == t &&
9331 (s[4] == 'T' || s[4] == 'N') &&
9332 tpacpi_is_fw_digit(s[5]) &&
9333 tpacpi_is_fw_digit(s[6]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009334}
9335
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009336/* returns 0 - probe ok, or < 0 - probe error.
9337 * Probe ok doesn't mean thinkpad found.
9338 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9339static int __must_check __init get_thinkpad_model_data(
9340 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009341{
Jeff Garzik18552562007-10-03 15:15:40 -04009342 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009343 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009344 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009345
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009346 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009347 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009348
9349 memset(tp, 0, sizeof(*tp));
9350
9351 if (dmi_name_in_vendors("IBM"))
9352 tp->vendor = PCI_VENDOR_ID_IBM;
9353 else if (dmi_name_in_vendors("LENOVO"))
9354 tp->vendor = PCI_VENDOR_ID_LENOVO;
9355 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009356 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009357
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009358 s = dmi_get_system_info(DMI_BIOS_VERSION);
9359 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9360 if (s && !tp->bios_version_str)
9361 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009362
9363 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009364 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9365 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009366 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009367
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009368 tp->bios_model = tp->bios_version_str[0]
9369 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009370 tp->bios_release = (tp->bios_version_str[4] << 8)
9371 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009372
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009373 /*
9374 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9375 * X32 or newer, all Z series; Some models must have an
9376 * up-to-date BIOS or they will not be detected.
9377 *
9378 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9379 */
9380 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009381 if (sscanf(dev->name,
9382 "IBM ThinkPad Embedded Controller -[%17c",
9383 ec_fw_string) == 1) {
9384 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9385 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009386
9387 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009388 if (!tp->ec_version_str)
9389 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009390
9391 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9392 tp->ec_model = ec_fw_string[0]
9393 | (ec_fw_string[1] << 8);
9394 tp->ec_release = (ec_fw_string[4] << 8)
9395 | ec_fw_string[5];
9396 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009397 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
Joe Perches0978e012011-04-04 10:06:25 -07009398 ec_fw_string);
9399 pr_notice("please report this to %s\n",
9400 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009401 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009402 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009403 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009404 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009405
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009406 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009407 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009408 tp->model_str = kstrdup(s, GFP_KERNEL);
9409 if (!tp->model_str)
9410 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009411 } else {
9412 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009413 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009414 tp->model_str = kstrdup(s, GFP_KERNEL);
9415 if (!tp->model_str)
9416 return -ENOMEM;
9417 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009418 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03009419
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009420 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9421 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9422 if (s && !tp->nummodel_str)
9423 return -ENOMEM;
9424
9425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009426}
9427
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009428static int __init probe_for_thinkpad(void)
9429{
9430 int is_thinkpad;
9431
9432 if (acpi_disabled)
9433 return -ENODEV;
9434
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009435 /* It would be dangerous to run the driver in this case */
9436 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9437 return -ENODEV;
9438
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009439 /*
9440 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009441 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009442 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009443 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9444 (thinkpad_id.ec_model != 0) ||
9445 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009446
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03009447 /* The EC handler is required */
9448 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009449 if (!ec_handle) {
9450 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07009451 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009452 return -ENODEV;
9453 }
9454
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009455 if (!is_thinkpad && !force_load)
9456 return -ENODEV;
9457
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009458 return 0;
9459}
9460
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009461static void __init thinkpad_acpi_init_banner(void)
9462{
Joe Perches0978e012011-04-04 10:06:25 -07009463 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9464 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009465
Joe Perches0978e012011-04-04 10:06:25 -07009466 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009467 (thinkpad_id.bios_version_str) ?
9468 thinkpad_id.bios_version_str : "unknown",
9469 (thinkpad_id.ec_version_str) ?
9470 thinkpad_id.ec_version_str : "unknown");
9471
9472 BUG_ON(!thinkpad_id.vendor);
9473
9474 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07009475 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009476 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9477 "IBM" : ((thinkpad_id.vendor ==
9478 PCI_VENDOR_ID_LENOVO) ?
9479 "Lenovo" : "Unknown vendor"),
9480 thinkpad_id.model_str,
9481 (thinkpad_id.nummodel_str) ?
9482 thinkpad_id.nummodel_str : "unknown");
9483}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009484
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009485/* Module init, exit, parameters */
9486
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009487static struct ibm_init_struct ibms_init[] __initdata = {
9488 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009489 .data = &thinkpad_acpi_driver_data,
9490 },
9491 {
9492 .init = hotkey_init,
9493 .data = &hotkey_driver_data,
9494 },
9495 {
9496 .init = bluetooth_init,
9497 .data = &bluetooth_driver_data,
9498 },
9499 {
9500 .init = wan_init,
9501 .data = &wan_driver_data,
9502 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009503 {
9504 .init = uwb_init,
9505 .data = &uwb_driver_data,
9506 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009507#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009508 {
9509 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009510 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009511 .data = &video_driver_data,
9512 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009513#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009514 {
Pali Rohárbb28f3d52015-12-30 23:27:41 +01009515 .init = kbdlight_init,
9516 .data = &kbdlight_driver_data,
9517 },
9518 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009519 .init = light_init,
9520 .data = &light_driver_data,
9521 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009522 {
9523 .init = cmos_init,
9524 .data = &cmos_driver_data,
9525 },
9526 {
9527 .init = led_init,
9528 .data = &led_driver_data,
9529 },
9530 {
9531 .init = beep_init,
9532 .data = &beep_driver_data,
9533 },
9534 {
9535 .init = thermal_init,
9536 .data = &thermal_driver_data,
9537 },
9538 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009539 .init = brightness_init,
9540 .data = &brightness_driver_data,
9541 },
9542 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009543 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009544 .data = &volume_driver_data,
9545 },
9546 {
9547 .init = fan_init,
9548 .data = &fan_driver_data,
9549 },
David Henningsson420f9732013-10-16 23:10:31 +02009550 {
9551 .init = mute_led_init,
9552 .data = &mute_led_driver_data,
9553 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009554};
9555
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009556static int __init set_ibm_param(const char *val, struct kernel_param *kp)
9557{
9558 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009559 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009560
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009561 if (!kp || !kp->name || !val)
9562 return -EINVAL;
9563
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009564 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9565 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009566 WARN_ON(ibm == NULL);
9567
9568 if (!ibm || !ibm->name)
9569 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009570
9571 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9572 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009573 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009574 strcpy(ibms_init[i].param, val);
9575 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009576 return 0;
9577 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009578 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009579
9580 return -EINVAL;
9581}
9582
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009583module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009584MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009585 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009586
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009587module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009588MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009589
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009590module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009591MODULE_PARM_DESC(force_load,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009592 "Attempts to load the driver even on a mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009593
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009594module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009595MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009596 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03009597
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009598module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009599MODULE_PARM_DESC(brightness_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009600 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03009601
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009602module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009603MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009604 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02009605
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009606#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009607module_param_named(volume_mode, volume_mode, uint, 0444);
9608MODULE_PARM_DESC(volume_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009609 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009610
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02009611module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9612MODULE_PARM_DESC(volume_capabilities,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009613 "Selects the mixer capabilites: 0=auto, 1=volume and mute, 2=mute only");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02009614
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02009615module_param_named(volume_control, volume_control_allowed, bool, 0444);
9616MODULE_PARM_DESC(volume_control,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009617 "Enables software override for the console audio control when true");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02009618
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07009619module_param_named(software_mute, software_mute_requested, bool, 0444);
9620MODULE_PARM_DESC(software_mute,
9621 "Request full software mute control");
9622
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009623/* ALSA module API parameters */
9624module_param_named(index, alsa_index, int, 0444);
9625MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9626module_param_named(id, alsa_id, charp, 0444);
9627MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9628module_param_named(enable, alsa_enable, bool, 0444);
9629MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009630#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009631
Andy Shevchenko9b4bbbd2017-05-09 17:17:21 +03009632/* The module parameter can't be read back, that's why 0 is used here */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009633#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009634 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009635 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009636
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009637TPACPI_PARAM(hotkey);
9638TPACPI_PARAM(bluetooth);
9639TPACPI_PARAM(video);
9640TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009641TPACPI_PARAM(cmos);
9642TPACPI_PARAM(led);
9643TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009644TPACPI_PARAM(brightness);
9645TPACPI_PARAM(volume);
9646TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009647
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009648#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009649module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009650MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9651module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9652MODULE_PARM_DESC(wlsw_state,
9653 "Initial state of the emulated WLSW switch");
9654
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009655module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009656MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9657module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9658MODULE_PARM_DESC(bluetooth_state,
9659 "Initial state of the emulated bluetooth switch");
9660
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009661module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009662MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9663module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9664MODULE_PARM_DESC(wwan_state,
9665 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009666
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009667module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009668MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9669module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9670MODULE_PARM_DESC(uwb_state,
9671 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009672#endif
9673
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009674static void thinkpad_acpi_module_exit(void)
9675{
9676 struct ibm_struct *ibm, *itmp;
9677
9678 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9679
9680 list_for_each_entry_safe_reverse(ibm, itmp,
9681 &tpacpi_all_drivers,
9682 all_drivers) {
9683 ibm_exit(ibm);
9684 }
9685
9686 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9687
9688 if (tpacpi_inputdev) {
9689 if (tp_features.input_device_registered)
9690 input_unregister_device(tpacpi_inputdev);
9691 else
9692 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +10009693 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009694 }
9695
9696 if (tpacpi_hwmon)
9697 hwmon_device_unregister(tpacpi_hwmon);
9698
9699 if (tp_features.sensors_pdev_attrs_registered)
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009700 device_remove_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009701 if (tpacpi_sensors_pdev)
9702 platform_device_unregister(tpacpi_sensors_pdev);
9703 if (tpacpi_pdev)
9704 platform_device_unregister(tpacpi_pdev);
9705
9706 if (tp_features.sensors_pdrv_attrs_registered)
9707 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9708 if (tp_features.platform_drv_attrs_registered)
9709 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9710
9711 if (tp_features.sensors_pdrv_registered)
9712 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9713
9714 if (tp_features.platform_drv_registered)
9715 platform_driver_unregister(&tpacpi_pdriver);
9716
9717 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009718 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009719
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009720 if (tpacpi_wq)
9721 destroy_workqueue(tpacpi_wq);
9722
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009723 kfree(thinkpad_id.bios_version_str);
9724 kfree(thinkpad_id.ec_version_str);
9725 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +10009726 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009727}
9728
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009729
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009730static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009731{
9732 int ret, i;
9733
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009734 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9735
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009736 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009737
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009738 ret = get_thinkpad_model_data(&thinkpad_id);
9739 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009740 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009741 thinkpad_acpi_module_exit();
9742 return ret;
9743 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009744 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009745 if (ret) {
9746 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009747 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009749
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009750 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009751
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009752 thinkpad_acpi_init_banner();
9753 tpacpi_check_outdated_fw();
9754
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009755 TPACPI_ACPIHANDLE_INIT(ecrd);
9756 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009757
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009758 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9759 if (!tpacpi_wq) {
9760 thinkpad_acpi_module_exit();
9761 return -ENOMEM;
9762 }
9763
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009764 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009765 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07009766 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009767 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009768 return -ENODEV;
9769 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009770
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009771 ret = platform_driver_register(&tpacpi_pdriver);
9772 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009773 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009774 thinkpad_acpi_module_exit();
9775 return ret;
9776 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009777 tp_features.platform_drv_registered = 1;
9778
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009779 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9780 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009781 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009782 thinkpad_acpi_module_exit();
9783 return ret;
9784 }
9785 tp_features.sensors_pdrv_registered = 1;
9786
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009787 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009788 if (!ret) {
9789 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009790 ret = tpacpi_create_driver_attributes(
9791 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009792 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009793 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009794 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009795 thinkpad_acpi_module_exit();
9796 return ret;
9797 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009798 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009799
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009800
9801 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009802 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009803 NULL, 0);
9804 if (IS_ERR(tpacpi_pdev)) {
9805 ret = PTR_ERR(tpacpi_pdev);
9806 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009807 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009808 thinkpad_acpi_module_exit();
9809 return ret;
9810 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009811 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009812 TPACPI_HWMON_DRVR_NAME,
9813 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009814 if (IS_ERR(tpacpi_sensors_pdev)) {
9815 ret = PTR_ERR(tpacpi_sensors_pdev);
9816 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009817 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009818 thinkpad_acpi_module_exit();
9819 return ret;
9820 }
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009821 ret = device_create_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009822 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009823 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009824 thinkpad_acpi_module_exit();
9825 return ret;
9826 }
9827 tp_features.sensors_pdev_attrs_registered = 1;
9828 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009829 if (IS_ERR(tpacpi_hwmon)) {
9830 ret = PTR_ERR(tpacpi_hwmon);
9831 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009832 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009833 thinkpad_acpi_module_exit();
9834 return ret;
9835 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009836 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009837 tpacpi_inputdev = input_allocate_device();
9838 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009839 thinkpad_acpi_module_exit();
9840 return -ENOMEM;
9841 } else {
9842 /* Prepare input device, but don't register */
9843 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009844 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009845 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009846 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009847 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9848 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009849 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009850 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009851
9852 /* Init subdriver dependencies */
9853 tpacpi_detect_brightness_capabilities();
9854
9855 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009856 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9857 ret = ibm_init(&ibms_init[i]);
9858 if (ret >= 0 && *ibms_init[i].param)
9859 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009860 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009861 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009862 return ret;
9863 }
9864 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009865
9866 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9867
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009868 ret = input_register_device(tpacpi_inputdev);
9869 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009870 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009871 thinkpad_acpi_module_exit();
9872 return ret;
9873 } else {
9874 tp_features.input_device_registered = 1;
9875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009876
9877 return 0;
9878}
9879
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009880MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9881
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009882/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009883 * This will autoload the driver in almost every ThinkPad
9884 * in widespread use.
9885 *
9886 * Only _VERY_ old models, like the 240, 240x and 570 lack
9887 * the HKEY event interface.
9888 */
9889MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9890
9891/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009892 * DMI matching for module autoloading
9893 *
9894 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9895 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9896 *
9897 * Only models listed in thinkwiki will be supported, so add yours
9898 * if it is not there yet.
9899 */
9900#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009901 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009902
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009903/* Ancient thinkpad BIOSes have to be identified by
9904 * BIOS type or model number, and there are far less
9905 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009906IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009907
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009908MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9909MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009910MODULE_DESCRIPTION(TPACPI_DESC);
9911MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009912MODULE_LICENSE("GPL");
9913
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009914module_init(thinkpad_acpi_module_init);
9915module_exit(thinkpad_acpi_module_exit);