blob: cab115bece153c0df3b1f99e45bd8e46231d8a51 [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 --------------------------------------------------- */
1440static ssize_t tpacpi_driver_interface_version_show(
1441 struct device_driver *drv,
1442 char *buf)
1443{
1444 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1445}
1446
1447static DRIVER_ATTR(interface_version, S_IRUGO,
1448 tpacpi_driver_interface_version_show, NULL);
1449
1450/* debug_level --------------------------------------------------------- */
1451static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1452 char *buf)
1453{
1454 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1455}
1456
1457static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1458 const char *buf, size_t count)
1459{
1460 unsigned long t;
1461
1462 if (parse_strtoul(buf, 0xffff, &t))
1463 return -EINVAL;
1464
1465 dbg_level = t;
1466
1467 return count;
1468}
1469
1470static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1471 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1472
1473/* version ------------------------------------------------------------- */
1474static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1475 char *buf)
1476{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001477 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1478 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001479}
1480
1481static DRIVER_ATTR(version, S_IRUGO,
1482 tpacpi_driver_version_show, NULL);
1483
1484/* --------------------------------------------------------------------- */
1485
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001486#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1487
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001488/* wlsw_emulstate ------------------------------------------------------ */
1489static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1490 char *buf)
1491{
1492 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1493}
1494
1495static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1496 const char *buf, size_t count)
1497{
1498 unsigned long t;
1499
1500 if (parse_strtoul(buf, 1, &t))
1501 return -EINVAL;
1502
Johannes Berg19d337d2009-06-02 13:01:37 +02001503 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001504 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001505 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1506 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001507
1508 return count;
1509}
1510
1511static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1512 tpacpi_driver_wlsw_emulstate_show,
1513 tpacpi_driver_wlsw_emulstate_store);
1514
1515/* bluetooth_emulstate ------------------------------------------------- */
1516static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1517 struct device_driver *drv,
1518 char *buf)
1519{
1520 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1521}
1522
1523static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1524 struct device_driver *drv,
1525 const char *buf, size_t count)
1526{
1527 unsigned long t;
1528
1529 if (parse_strtoul(buf, 1, &t))
1530 return -EINVAL;
1531
1532 tpacpi_bluetooth_emulstate = !!t;
1533
1534 return count;
1535}
1536
1537static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1538 tpacpi_driver_bluetooth_emulstate_show,
1539 tpacpi_driver_bluetooth_emulstate_store);
1540
1541/* wwan_emulstate ------------------------------------------------- */
1542static ssize_t tpacpi_driver_wwan_emulstate_show(
1543 struct device_driver *drv,
1544 char *buf)
1545{
1546 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1547}
1548
1549static ssize_t tpacpi_driver_wwan_emulstate_store(
1550 struct device_driver *drv,
1551 const char *buf, size_t count)
1552{
1553 unsigned long t;
1554
1555 if (parse_strtoul(buf, 1, &t))
1556 return -EINVAL;
1557
1558 tpacpi_wwan_emulstate = !!t;
1559
1560 return count;
1561}
1562
1563static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1564 tpacpi_driver_wwan_emulstate_show,
1565 tpacpi_driver_wwan_emulstate_store);
1566
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001567/* uwb_emulstate ------------------------------------------------- */
1568static ssize_t tpacpi_driver_uwb_emulstate_show(
1569 struct device_driver *drv,
1570 char *buf)
1571{
1572 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1573}
1574
1575static ssize_t tpacpi_driver_uwb_emulstate_store(
1576 struct device_driver *drv,
1577 const char *buf, size_t count)
1578{
1579 unsigned long t;
1580
1581 if (parse_strtoul(buf, 1, &t))
1582 return -EINVAL;
1583
1584 tpacpi_uwb_emulstate = !!t;
1585
1586 return count;
1587}
1588
1589static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1590 tpacpi_driver_uwb_emulstate_show,
1591 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001592#endif
1593
1594/* --------------------------------------------------------------------- */
1595
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001596static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001597 &driver_attr_debug_level, &driver_attr_version,
1598 &driver_attr_interface_version,
1599};
1600
1601static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1602{
1603 int i, res;
1604
1605 i = 0;
1606 res = 0;
1607 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1608 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1609 i++;
1610 }
1611
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001612#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1613 if (!res && dbg_wlswemul)
1614 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1615 if (!res && dbg_bluetoothemul)
1616 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1617 if (!res && dbg_wwanemul)
1618 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001619 if (!res && dbg_uwbemul)
1620 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001621#endif
1622
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001623 return res;
1624}
1625
1626static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1627{
1628 int i;
1629
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001630 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001631 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001632
1633#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1634 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1635 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1636 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001637 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001638#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001639}
1640
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001641/*************************************************************************
1642 * Firmware Data
1643 */
1644
1645/*
1646 * Table of recommended minimum BIOS versions
1647 *
1648 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001649 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001650 * bugs and bad ACPI behaviour on older versions
1651 *
1652 * 2. BIOS or EC fw with known bugs that trigger on Linux
1653 *
1654 * 3. BIOS with known reduced functionality in older versions
1655 *
1656 * We recommend the latest BIOS and EC version.
1657 * We only support the latest BIOS and EC fw version as a rule.
1658 *
1659 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1660 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001661 *
1662 * WARNING: we use this table also to detect that the machine is
1663 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001664 */
1665
1666#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1667 { .vendor = (__v), \
1668 .bios = TPID(__id1, __id2), \
1669 .ec = TPACPI_MATCH_ANY, \
1670 .quirks = TPACPI_MATCH_ANY << 16 \
1671 | (__bv1) << 8 | (__bv2) }
1672
1673#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001674 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001675 { .vendor = (__v), \
1676 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001677 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001678 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1679 | (__bv1) << 8 | (__bv2) }
1680
1681#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1682 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1683
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001684/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001685#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1686 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001687 __bv1, __bv2, TPID(__id1, __id2), \
1688 __ev1, __ev2), \
1689 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1690 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1691 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001692
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001693/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001694#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1695 __eid1, __eid2, __ev1, __ev2) \
1696 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001697 __bv1, __bv2, TPID(__eid1, __eid2), \
1698 __ev1, __ev2), \
1699 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1700 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1701 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001702
1703#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1704 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1705
1706#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1707 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001708 __bv1, __bv2, TPID(__id1, __id2), \
1709 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001710
1711#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1712 __eid1, __eid2, __ev1, __ev2) \
1713 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001714 __bv1, __bv2, TPID(__eid1, __eid2), \
1715 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001716
1717static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1718 /* Numeric models ------------------ */
1719 /* FW MODEL BIOS VERS */
1720 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1721 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1722 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1723 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1724 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1725 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1726 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1727 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1728 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1729
1730 /* A-series ------------------------- */
1731 /* FW MODEL BIOS VERS EC VERS */
1732 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1733 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1734 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1735 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1736 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1737 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1738 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1739 TPV_QI0('1', '3', '2', '0'), /* A22m */
1740 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1741 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1742 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1743
1744 /* G-series ------------------------- */
1745 /* FW MODEL BIOS VERS */
1746 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1747 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1748
1749 /* R-series, T-series --------------- */
1750 /* FW MODEL BIOS VERS EC VERS */
1751 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1752 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1753 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1754 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1755 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1756 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1757 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1758 T40/p, T41/p, T42/p (1) */
1759 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1760 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1761 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1762 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1763
1764 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1765 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1766 TPV_QI0('1', '6', '3', '2'), /* T22 */
1767 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1768 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1769 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1770
1771 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1772 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001773 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001774
1775 /* BIOS FW BIOS VERS EC FW EC VERS */
1776 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1777 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1778
1779 /* X-series ------------------------- */
1780 /* FW MODEL BIOS VERS EC VERS */
1781 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1782 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1783 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1784 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1785 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1786 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1787 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1788
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001789 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1790 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001791
1792 /* (0) - older versions lack DMI EC fw string and functionality */
1793 /* (1) - older versions known to lack functionality */
1794};
1795
1796#undef TPV_QL1
1797#undef TPV_QL0
1798#undef TPV_QI2
1799#undef TPV_QI1
1800#undef TPV_QI0
1801#undef TPV_Q_X
1802#undef TPV_Q
1803
1804static void __init tpacpi_check_outdated_fw(void)
1805{
1806 unsigned long fwvers;
1807 u16 ec_version, bios_version;
1808
1809 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1810 ARRAY_SIZE(tpacpi_bios_version_qtable));
1811
1812 if (!fwvers)
1813 return;
1814
1815 bios_version = fwvers & 0xffffU;
1816 ec_version = (fwvers >> 16) & 0xffffU;
1817
1818 /* note that unknown versions are set to 0x0000 and we use that */
1819 if ((bios_version > thinkpad_id.bios_release) ||
1820 (ec_version > thinkpad_id.ec_release &&
1821 ec_version != TPACPI_MATCH_ANY)) {
1822 /*
1823 * The changelogs would let us track down the exact
1824 * reason, but it is just too much of a pain to track
1825 * it. We only list BIOSes that are either really
1826 * broken, or really stable to begin with, so it is
1827 * best if the user upgrades the firmware anyway.
1828 */
Joe Perches0978e012011-04-04 10:06:25 -07001829 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03001830 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 -03001831 }
1832}
1833
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001834static bool __init tpacpi_is_fw_known(void)
1835{
1836 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1837 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1838}
1839
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001840/****************************************************************************
1841 ****************************************************************************
1842 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001843 * Subdrivers
1844 *
1845 ****************************************************************************
1846 ****************************************************************************/
1847
1848/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001849 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001850 */
1851
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001852static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001854 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1855 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
1858
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001859static struct ibm_struct thinkpad_acpi_driver_data = {
1860 .name = "driver",
1861 .read = thinkpad_acpi_driver_read,
1862};
1863
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001864/*************************************************************************
1865 * Hotkey subdriver
1866 */
1867
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001868/*
1869 * ThinkPad firmware event model
1870 *
1871 * The ThinkPad firmware has two main event interfaces: normal ACPI
1872 * notifications (which follow the ACPI standard), and a private event
1873 * interface.
1874 *
1875 * The private event interface also issues events for the hotkeys. As
1876 * the driver gained features, the event handling code ended up being
1877 * built around the hotkey subdriver. This will need to be refactored
1878 * to a more formal event API eventually.
1879 *
1880 * Some "hotkeys" are actually supposed to be used as event reports,
1881 * such as "brightness has changed", "volume has changed", depending on
1882 * the ThinkPad model and how the firmware is operating.
1883 *
1884 * Unlike other classes, hotkey-class events have mask/unmask control on
1885 * non-ancient firmware. However, how it behaves changes a lot with the
1886 * firmware model and version.
1887 */
1888
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001889enum { /* hot key scan codes (derived from ACPI DSDT) */
1890 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1891 TP_ACPI_HOTKEYSCAN_FNF2,
1892 TP_ACPI_HOTKEYSCAN_FNF3,
1893 TP_ACPI_HOTKEYSCAN_FNF4,
1894 TP_ACPI_HOTKEYSCAN_FNF5,
1895 TP_ACPI_HOTKEYSCAN_FNF6,
1896 TP_ACPI_HOTKEYSCAN_FNF7,
1897 TP_ACPI_HOTKEYSCAN_FNF8,
1898 TP_ACPI_HOTKEYSCAN_FNF9,
1899 TP_ACPI_HOTKEYSCAN_FNF10,
1900 TP_ACPI_HOTKEYSCAN_FNF11,
1901 TP_ACPI_HOTKEYSCAN_FNF12,
1902 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1903 TP_ACPI_HOTKEYSCAN_FNINSERT,
1904 TP_ACPI_HOTKEYSCAN_FNDELETE,
1905 TP_ACPI_HOTKEYSCAN_FNHOME,
1906 TP_ACPI_HOTKEYSCAN_FNEND,
1907 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1908 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1909 TP_ACPI_HOTKEYSCAN_FNSPACE,
1910 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1911 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1912 TP_ACPI_HOTKEYSCAN_MUTE,
1913 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001914 TP_ACPI_HOTKEYSCAN_UNK1,
1915 TP_ACPI_HOTKEYSCAN_UNK2,
1916 TP_ACPI_HOTKEYSCAN_UNK3,
1917 TP_ACPI_HOTKEYSCAN_UNK4,
1918 TP_ACPI_HOTKEYSCAN_UNK5,
1919 TP_ACPI_HOTKEYSCAN_UNK6,
1920 TP_ACPI_HOTKEYSCAN_UNK7,
1921 TP_ACPI_HOTKEYSCAN_UNK8,
1922
Christian Kellner149c8c72017-02-28 17:10:56 +01001923 /* Adaptive keyboard keycodes */
1924 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1925 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
Bastien Nocera6a68d852015-03-02 14:45:31 +01001926 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1927 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1928 TP_ACPI_HOTKEYSCAN_CLOUD,
1929 TP_ACPI_HOTKEYSCAN_UNK9,
1930 TP_ACPI_HOTKEYSCAN_VOICE,
1931 TP_ACPI_HOTKEYSCAN_UNK10,
1932 TP_ACPI_HOTKEYSCAN_GESTURES,
1933 TP_ACPI_HOTKEYSCAN_UNK11,
1934 TP_ACPI_HOTKEYSCAN_UNK12,
1935 TP_ACPI_HOTKEYSCAN_UNK13,
1936 TP_ACPI_HOTKEYSCAN_CONFIG,
1937 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1938 TP_ACPI_HOTKEYSCAN_RELOAD,
1939 TP_ACPI_HOTKEYSCAN_BACK,
1940 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1941 TP_ACPI_HOTKEYSCAN_MIC_UP,
1942 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1943 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1944 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1945
Christian Kellner696c65232017-02-28 17:10:57 +01001946 /* Lenovo extended keymap, starting at 0x1300 */
1947 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1948 /* first new observed key (star, favorites) is 0x1311 */
1949 TP_ACPI_HOTKEYSCAN_STAR = 69,
1950 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1951 TP_ACPI_HOTKEYSCAN_UNK25,
1952 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1953 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1954
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001955 /* Hotkey keymap size */
1956 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001957};
1958
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001959enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001960 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1961 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1962};
1963
1964enum { /* Positions of some of the keys in hotkey masks */
1965 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1966 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1967 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1968 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1969 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
Hans de Goedec685e202017-02-09 16:44:13 +01001970 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001971 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1972 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1973 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1974 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1975 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1976};
1977
1978enum { /* NVRAM to ACPI HKEY group map */
1979 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1980 TP_ACPI_HKEY_ZOOM_MASK |
1981 TP_ACPI_HKEY_DISPSWTCH_MASK |
1982 TP_ACPI_HKEY_HIBERNATE_MASK,
1983 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1984 TP_ACPI_HKEY_BRGHTDWN_MASK,
1985 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1986 TP_ACPI_HKEY_VOLDWN_MASK |
1987 TP_ACPI_HKEY_MUTE_MASK,
1988};
1989
1990#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1991struct tp_nvram_state {
1992 u16 thinkpad_toggle:1;
1993 u16 zoom_toggle:1;
1994 u16 display_toggle:1;
1995 u16 thinklight_toggle:1;
1996 u16 hibernate_toggle:1;
1997 u16 displayexp_toggle:1;
1998 u16 display_state:1;
1999 u16 brightness_toggle:1;
2000 u16 volume_toggle:1;
2001 u16 mute:1;
2002
2003 u8 brightness_level;
2004 u8 volume_level;
2005};
2006
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002007/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002008static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002009
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002010/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002011 * Acquire mutex to write poller control variables as an
2012 * atomic block.
2013 *
2014 * Increment hotkey_config_change when changing them if you
2015 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002016 *
2017 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2018 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002019static struct mutex hotkey_thread_data_mutex;
2020static unsigned int hotkey_config_change;
2021
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002022/*
2023 * hotkey poller control variables
2024 *
2025 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002026 *
2027 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2028 * should be used only when the changes need to be taken as
2029 * a block, OR when one needs to force the kthread to forget
2030 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002031 */
2032static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2033static unsigned int hotkey_poll_freq = 10; /* Hz */
2034
2035#define HOTKEY_CONFIG_CRITICAL_START \
2036 do { \
2037 mutex_lock(&hotkey_thread_data_mutex); \
2038 hotkey_config_change++; \
2039 } while (0);
2040#define HOTKEY_CONFIG_CRITICAL_END \
2041 mutex_unlock(&hotkey_thread_data_mutex);
2042
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002043#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2044
2045#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002046#define HOTKEY_CONFIG_CRITICAL_START
2047#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002048
2049#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2050
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002051static struct mutex hotkey_mutex;
2052
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002053static enum { /* Reasons for waking up */
2054 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2055 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2056 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2057} hotkey_wakeup_reason;
2058
2059static int hotkey_autosleep_ack;
2060
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002061static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2062static u32 hotkey_all_mask; /* all events supported in fw */
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002063static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002064static u32 hotkey_reserved_mask; /* events better left disabled */
2065static u32 hotkey_driver_mask; /* events needed by the driver */
2066static u32 hotkey_user_mask; /* events visible to userspace */
2067static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002068
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002069static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002070
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002071static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002072
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002073static void tpacpi_driver_event(const unsigned int hkey_event);
2074static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002075static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002076
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002077/* HKEY.MHKG() return bits */
2078#define TP_HOTKEY_TABLET_MASK (1 << 3)
Lyudeb03f4d42016-11-11 15:15:03 -05002079/* ThinkPad X1 Yoga (2016) */
2080#define TP_EC_CMMD_TABLET_MODE 0x6
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002081
Johannes Berg19d337d2009-06-02 13:01:37 +02002082static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002083{
Johannes Berg19d337d2009-06-02 13:01:37 +02002084 int status;
2085
2086 if (!tp_features.hotkey_wlsw)
2087 return -ENODEV;
2088
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002089#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002090 if (dbg_wlswemul)
2091 return (tpacpi_wlsw_emulstate) ?
2092 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002093#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002094
2095 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002096 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002097
2098 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002099}
2100
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002101static int hotkey_get_tablet_mode(int *status)
2102{
2103 int s;
2104
Lyudeb03f4d42016-11-11 15:15:03 -05002105 switch (tp_features.hotkey_tablet) {
2106 case TP_HOTKEY_TABLET_USES_MHKG:
2107 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2108 return -EIO;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002109
Lyudeb03f4d42016-11-11 15:15:03 -05002110 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2111 break;
2112 case TP_HOTKEY_TABLET_USES_CMMD:
2113 if (!acpi_evalf(ec_handle, &s, "CMMD", "d"))
2114 return -EIO;
2115
2116 *status = (s == TP_EC_CMMD_TABLET_MODE);
2117 break;
2118 default:
2119 break;
2120 }
2121
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002122 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002123}
2124
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002125/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002126 * Reads current event mask from firmware, and updates
2127 * hotkey_acpi_mask accordingly. Also resets any bits
2128 * from hotkey_user_mask that are unavailable to be
2129 * delivered (shadow requirement of the userspace ABI).
2130 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002131 * Call with hotkey_mutex held
2132 */
2133static int hotkey_mask_get(void)
2134{
2135 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002136 u32 m = 0;
2137
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002138 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002139 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002140
2141 hotkey_acpi_mask = m;
2142 } else {
2143 /* no mask support doesn't mean no event support... */
2144 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002145 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002146
2147 /* sync userspace-visible mask */
2148 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002149
2150 return 0;
2151}
2152
Jean Delvaredf6dd1b2015-04-27 09:45:06 +02002153static void hotkey_mask_warn_incomplete_mask(void)
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002154{
2155 /* log only what the user can fix... */
2156 const u32 wantedmask = hotkey_driver_mask &
2157 ~(hotkey_acpi_mask | hotkey_source_mask) &
2158 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2159
2160 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002161 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002162}
2163
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002164/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002165 * Set the firmware mask when supported
2166 *
2167 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2168 *
2169 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2170 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002171 * Call with hotkey_mutex held
2172 */
2173static int hotkey_mask_set(u32 mask)
2174{
2175 int i;
2176 int rc = 0;
2177
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002178 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002179
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002180 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002181 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002182 if (!acpi_evalf(hkey_handle,
2183 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002184 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002185 rc = -EIO;
2186 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002187 }
2188 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002189 }
2190
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002191 /*
2192 * We *must* make an inconditional call to hotkey_mask_get to
2193 * refresh hotkey_acpi_mask and update hotkey_user_mask
2194 *
2195 * Take the opportunity to also log when we cannot _enable_
2196 * a given event.
2197 */
2198 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002199 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002200 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002201 }
2202
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002203 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2204 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002205
2206 return rc;
2207}
2208
2209/*
2210 * Sets hotkey_user_mask and tries to set the firmware mask
2211 *
2212 * Call with hotkey_mutex held
2213 */
2214static int hotkey_user_mask_set(const u32 mask)
2215{
2216 int rc;
2217
2218 /* Give people a chance to notice they are doing something that
2219 * is bound to go boom on their users sooner or later */
2220 if (!tp_warned.hotkey_mask_ff &&
2221 (mask == 0xffff || mask == 0xffffff ||
2222 mask == 0xffffffff)) {
2223 tp_warned.hotkey_mask_ff = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002224 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2225 mask);
2226 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 -03002227 }
2228
2229 /* Try to enable what the user asked for, plus whatever we need.
2230 * this syncs everything but won't enable bits in hotkey_user_mask */
2231 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2232
2233 /* Enable the available bits in hotkey_user_mask */
2234 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2235
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002236 return rc;
2237}
2238
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002239/*
2240 * Sets the driver hotkey mask.
2241 *
2242 * Can be called even if the hotkey subdriver is inactive
2243 */
2244static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2245{
2246 int rc;
2247
2248 /* Do the right thing if hotkey_init has not been called yet */
2249 if (!tp_features.hotkey) {
2250 hotkey_driver_mask = mask;
2251 return 0;
2252 }
2253
2254 mutex_lock(&hotkey_mutex);
2255
2256 HOTKEY_CONFIG_CRITICAL_START
2257 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002258#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002259 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002260#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002261 HOTKEY_CONFIG_CRITICAL_END
2262
2263 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2264 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002265 hotkey_poll_setup(true);
2266
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002267 mutex_unlock(&hotkey_mutex);
2268
2269 return rc;
2270}
2271
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002272static int hotkey_status_get(int *status)
2273{
2274 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2275 return -EIO;
2276
2277 return 0;
2278}
2279
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002280static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002281{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002282 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002283 return -EIO;
2284
2285 return 0;
2286}
2287
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002288static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002289{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002290 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002291
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002292 if (tp_features.hotkey_tablet &&
2293 !hotkey_get_tablet_mode(&state)) {
2294 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002295
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002296 input_report_switch(tpacpi_inputdev,
2297 SW_TABLET_MODE, !!state);
2298 input_sync(tpacpi_inputdev);
2299
2300 mutex_unlock(&tpacpi_inputdev_send_mutex);
2301 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002302}
2303
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002304/* Do NOT call without validating scancode first */
2305static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002306{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002307 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002308
2309 if (keycode != KEY_RESERVED) {
2310 mutex_lock(&tpacpi_inputdev_send_mutex);
2311
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002312 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002313 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002314 input_sync(tpacpi_inputdev);
2315
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002316 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002317 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002318 input_sync(tpacpi_inputdev);
2319
2320 mutex_unlock(&tpacpi_inputdev_send_mutex);
2321 }
2322}
2323
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002324/* Do NOT call without validating scancode first */
2325static void tpacpi_input_send_key_masked(const unsigned int scancode)
2326{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002327 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002328 if (hotkey_user_mask & (1 << scancode))
2329 tpacpi_input_send_key(scancode);
2330}
2331
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002332#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2333static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2334
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002335/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002336static void tpacpi_hotkey_send_key(unsigned int scancode)
2337{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002338 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002339}
2340
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002341static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002342{
2343 u8 d;
2344
2345 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2346 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2347 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2348 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2349 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2350 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2351 }
Hans de Goedec685e202017-02-09 16:44:13 +01002352 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002353 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2354 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2355 }
2356 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2357 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2358 n->displayexp_toggle =
2359 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2360 }
2361 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2362 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2363 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2364 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2365 n->brightness_toggle =
2366 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2367 }
2368 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2369 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2370 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2371 >> TP_NVRAM_POS_LEVEL_VOLUME;
2372 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2373 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2374 }
2375}
2376
Behan Webstera4d44ba2014-02-12 21:58:46 +01002377#define TPACPI_COMPARE_KEY(__scancode, __member) \
2378do { \
2379 if ((event_mask & (1 << __scancode)) && \
2380 oldn->__member != newn->__member) \
2381 tpacpi_hotkey_send_key(__scancode); \
2382} while (0)
2383
2384#define TPACPI_MAY_SEND_KEY(__scancode) \
2385do { \
2386 if (event_mask & (1 << __scancode)) \
2387 tpacpi_hotkey_send_key(__scancode); \
2388} while (0)
2389
2390static void issue_volchange(const unsigned int oldvol,
2391 const unsigned int newvol,
2392 const u32 event_mask)
2393{
2394 unsigned int i = oldvol;
2395
2396 while (i > newvol) {
2397 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2398 i--;
2399 }
2400 while (i < newvol) {
2401 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2402 i++;
2403 }
2404}
2405
2406static void issue_brightnesschange(const unsigned int oldbrt,
2407 const unsigned int newbrt,
2408 const u32 event_mask)
2409{
2410 unsigned int i = oldbrt;
2411
2412 while (i > newbrt) {
2413 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2414 i--;
2415 }
2416 while (i < newbrt) {
2417 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2418 i++;
2419 }
2420}
2421
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002422static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2423 struct tp_nvram_state *newn,
2424 const u32 event_mask)
2425{
2426
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002427 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2428 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2429 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2430 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2431
2432 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2433
2434 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2435
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002436 /*
2437 * Handle volume
2438 *
2439 * This code is supposed to duplicate the IBM firmware behaviour:
2440 * - Pressing MUTE issues mute hotkey message, even when already mute
2441 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002442 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002443 * - The act of unmuting issues volume up/down notification,
2444 * depending which key was used to unmute
2445 *
2446 * We are constrained to what the NVRAM can tell us, which is not much
2447 * and certainly not enough if more than one volume hotkey was pressed
2448 * since the last poll cycle.
2449 *
2450 * Just to make our life interesting, some newer Lenovo ThinkPads have
2451 * bugs in the BIOS and may fail to update volume_toggle properly.
2452 */
2453 if (newn->mute) {
2454 /* muted */
2455 if (!oldn->mute ||
2456 oldn->volume_toggle != newn->volume_toggle ||
2457 oldn->volume_level != newn->volume_level) {
2458 /* recently muted, or repeated mute keypress, or
2459 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002460 issue_volchange(oldn->volume_level, newn->volume_level,
2461 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002462 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2463 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002464 } else {
2465 /* unmute */
2466 if (oldn->mute) {
2467 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002468 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002469 }
2470 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002471 issue_volchange(oldn->volume_level, newn->volume_level,
2472 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002473 } else if (oldn->volume_toggle != newn->volume_toggle) {
2474 /* repeated vol up/down keypress at end of scale ? */
2475 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002476 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002477 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2478 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002479 }
2480 }
2481
2482 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002483 if (oldn->brightness_level != newn->brightness_level) {
2484 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002485 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002486 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2487 /* repeated key presses that didn't change state */
2488 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002489 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002490 else if (newn->brightness_level >= bright_maxlvl
2491 && !tp_features.bright_unkfw)
2492 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002493 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002494
2495#undef TPACPI_COMPARE_KEY
2496#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002497}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002498
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002499/*
2500 * Polling driver
2501 *
2502 * We track all events in hotkey_source_mask all the time, since
2503 * most of them are edge-based. We only issue those requested by
2504 * hotkey_user_mask or hotkey_driver_mask, though.
2505 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002506static int hotkey_kthread(void *data)
2507{
2508 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002509 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002510 unsigned int si, so;
2511 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002512 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002513 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002514 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002515
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002516 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2517 goto exit;
2518
2519 set_freezable();
2520
2521 so = 0;
2522 si = 1;
2523 t = 0;
2524
2525 /* Initial state for compares */
2526 mutex_lock(&hotkey_thread_data_mutex);
2527 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002528 poll_mask = hotkey_source_mask;
2529 event_mask = hotkey_source_mask &
2530 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002531 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002532 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002533 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002534
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002535 while (!kthread_should_stop()) {
2536 if (t == 0) {
2537 if (likely(poll_freq))
2538 t = 1000/poll_freq;
2539 else
2540 t = 100; /* should never happen... */
2541 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002542 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002543 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002544 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002545
2546 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002547 continue;
2548
2549 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002550 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002551 /* forget old state on thaw or config change */
2552 si = so;
2553 t = 0;
2554 change_detector = hotkey_config_change;
2555 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002556 poll_mask = hotkey_source_mask;
2557 event_mask = hotkey_source_mask &
2558 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002559 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002560 mutex_unlock(&hotkey_thread_data_mutex);
2561
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002562 if (likely(poll_mask)) {
2563 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002564 if (likely(si != so)) {
2565 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002566 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002567 }
2568 }
2569
2570 so = si;
2571 si ^= 1;
2572 }
2573
2574exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002575 return 0;
2576}
2577
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002578/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002579static void hotkey_poll_stop_sync(void)
2580{
2581 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002582 kthread_stop(tpacpi_hotkey_task);
2583 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002584 }
2585}
2586
2587/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002588static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002589{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002590 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2591 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002592
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002593 if (hotkey_poll_freq > 0 &&
2594 (poll_driver_mask ||
2595 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002596 if (!tpacpi_hotkey_task) {
2597 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002598 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002599 if (IS_ERR(tpacpi_hotkey_task)) {
2600 tpacpi_hotkey_task = NULL;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002601 pr_err("could not create kernel thread for hotkey polling\n");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002602 }
2603 }
2604 } else {
2605 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002606 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002607 hotkey_poll_freq == 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002608 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 -07002609 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002610 }
2611 }
2612}
2613
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002614static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002615{
2616 mutex_lock(&hotkey_mutex);
2617 hotkey_poll_setup(may_warn);
2618 mutex_unlock(&hotkey_mutex);
2619}
2620
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002621/* call with hotkey_mutex held */
2622static void hotkey_poll_set_freq(unsigned int freq)
2623{
2624 if (!freq)
2625 hotkey_poll_stop_sync();
2626
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002627 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002628}
2629
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002630#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2631
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002632static void hotkey_poll_setup(const bool __unused)
2633{
2634}
2635
2636static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002637{
2638}
2639
2640#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2641
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002642static int hotkey_inputdev_open(struct input_dev *dev)
2643{
2644 switch (tpacpi_lifecycle) {
2645 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002646 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002647 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002648 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002649 case TPACPI_LIFE_EXITING:
2650 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002651 }
2652
2653 /* Should only happen if tpacpi_lifecycle is corrupt */
2654 BUG();
2655 return -EBUSY;
2656}
2657
2658static void hotkey_inputdev_close(struct input_dev *dev)
2659{
2660 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002661 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002662 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002663 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002664}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002665
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002666/* sysfs hotkey enable ------------------------------------------------- */
2667static ssize_t hotkey_enable_show(struct device *dev,
2668 struct device_attribute *attr,
2669 char *buf)
2670{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002671 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002672
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002673 printk_deprecated_attribute("hotkey_enable",
2674 "Hotkey reporting is always enabled");
2675
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002676 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002677 if (res)
2678 return res;
2679
2680 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2681}
2682
2683static ssize_t hotkey_enable_store(struct device *dev,
2684 struct device_attribute *attr,
2685 const char *buf, size_t count)
2686{
2687 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002688
2689 printk_deprecated_attribute("hotkey_enable",
2690 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002691
2692 if (parse_strtoul(buf, 1, &t))
2693 return -EINVAL;
2694
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002695 if (t == 0)
2696 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002697
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002698 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002699}
2700
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002701static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002702
2703/* sysfs hotkey mask --------------------------------------------------- */
2704static ssize_t hotkey_mask_show(struct device *dev,
2705 struct device_attribute *attr,
2706 char *buf)
2707{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002708 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002709}
2710
2711static ssize_t hotkey_mask_store(struct device *dev,
2712 struct device_attribute *attr,
2713 const char *buf, size_t count)
2714{
2715 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002716 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002717
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002718 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002719 return -EINVAL;
2720
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002721 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002722 return -ERESTARTSYS;
2723
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002724 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002725
2726#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002727 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002728#endif
2729
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002730 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002731
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002732 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2733
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002734 return (res) ? res : count;
2735}
2736
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002737static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002738
2739/* sysfs hotkey bios_enabled ------------------------------------------- */
2740static ssize_t hotkey_bios_enabled_show(struct device *dev,
2741 struct device_attribute *attr,
2742 char *buf)
2743{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002744 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002745}
2746
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002747static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002748
2749/* sysfs hotkey bios_mask ---------------------------------------------- */
2750static ssize_t hotkey_bios_mask_show(struct device *dev,
2751 struct device_attribute *attr,
2752 char *buf)
2753{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002754 printk_deprecated_attribute("hotkey_bios_mask",
2755 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002756 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002757}
2758
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002759static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002760
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002761/* sysfs hotkey all_mask ----------------------------------------------- */
2762static ssize_t hotkey_all_mask_show(struct device *dev,
2763 struct device_attribute *attr,
2764 char *buf)
2765{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002766 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2767 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002768}
2769
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002770static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002771
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002772/* sysfs hotkey all_mask ----------------------------------------------- */
2773static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2774 struct device_attribute *attr,
2775 char *buf)
2776{
2777 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2778 hotkey_adaptive_all_mask | hotkey_source_mask);
2779}
2780
2781static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2782
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002783/* sysfs hotkey recommended_mask --------------------------------------- */
2784static ssize_t hotkey_recommended_mask_show(struct device *dev,
2785 struct device_attribute *attr,
2786 char *buf)
2787{
2788 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002789 (hotkey_all_mask | hotkey_source_mask)
2790 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002791}
2792
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002793static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002794
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002795#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2796
2797/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2798static ssize_t hotkey_source_mask_show(struct device *dev,
2799 struct device_attribute *attr,
2800 char *buf)
2801{
2802 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2803}
2804
2805static ssize_t hotkey_source_mask_store(struct device *dev,
2806 struct device_attribute *attr,
2807 const char *buf, size_t count)
2808{
2809 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002810 u32 r_ev;
2811 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002812
2813 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2814 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2815 return -EINVAL;
2816
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002817 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002818 return -ERESTARTSYS;
2819
2820 HOTKEY_CONFIG_CRITICAL_START
2821 hotkey_source_mask = t;
2822 HOTKEY_CONFIG_CRITICAL_END
2823
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002824 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2825 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002826 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002827
2828 /* check if events needed by the driver got disabled */
2829 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2830 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002831
2832 mutex_unlock(&hotkey_mutex);
2833
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002834 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002835 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002836
2837 if (r_ev)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03002838 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
Joe Perches0978e012011-04-04 10:06:25 -07002839 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002840
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002841 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2842
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002843 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002844}
2845
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002846static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002847
2848/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2849static ssize_t hotkey_poll_freq_show(struct device *dev,
2850 struct device_attribute *attr,
2851 char *buf)
2852{
2853 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2854}
2855
2856static ssize_t hotkey_poll_freq_store(struct device *dev,
2857 struct device_attribute *attr,
2858 const char *buf, size_t count)
2859{
2860 unsigned long t;
2861
2862 if (parse_strtoul(buf, 25, &t))
2863 return -EINVAL;
2864
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002865 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002866 return -ERESTARTSYS;
2867
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002868 hotkey_poll_set_freq(t);
2869 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002870
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002871 mutex_unlock(&hotkey_mutex);
2872
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002873 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2874
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002875 return count;
2876}
2877
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002878static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002879
2880#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2881
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002882/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002883static ssize_t hotkey_radio_sw_show(struct device *dev,
2884 struct device_attribute *attr,
2885 char *buf)
2886{
Johannes Berg19d337d2009-06-02 13:01:37 +02002887 int res;
2888 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002889 if (res < 0)
2890 return res;
2891
Johannes Berg19d337d2009-06-02 13:01:37 +02002892 /* Opportunistic update */
2893 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2894
2895 return snprintf(buf, PAGE_SIZE, "%d\n",
2896 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002897}
2898
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002899static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002900
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002901static void hotkey_radio_sw_notify_change(void)
2902{
2903 if (tp_features.hotkey_wlsw)
2904 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2905 "hotkey_radio_sw");
2906}
2907
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002908/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2909static ssize_t hotkey_tablet_mode_show(struct device *dev,
2910 struct device_attribute *attr,
2911 char *buf)
2912{
2913 int res, s;
2914 res = hotkey_get_tablet_mode(&s);
2915 if (res < 0)
2916 return res;
2917
2918 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2919}
2920
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002921static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002922
2923static void hotkey_tablet_mode_notify_change(void)
2924{
2925 if (tp_features.hotkey_tablet)
2926 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2927 "hotkey_tablet_mode");
2928}
2929
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002930/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002931static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2932 struct device_attribute *attr,
2933 char *buf)
2934{
2935 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2936}
2937
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002938static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002939
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002940static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002941{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002942 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2943 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002944}
2945
2946/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002947static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2948 struct device_attribute *attr,
2949 char *buf)
2950{
2951 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2952}
2953
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002954static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2955 hotkey_wakeup_hotunplug_complete_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002956
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002957static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002958{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002959 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2960 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002961}
2962
Bastien Nocerab790cee2015-03-02 14:45:27 +01002963/* sysfs adaptive kbd mode --------------------------------------------- */
2964
2965static int adaptive_keyboard_get_mode(void);
2966static int adaptive_keyboard_set_mode(int new_mode);
2967
2968enum ADAPTIVE_KEY_MODE {
2969 HOME_MODE,
2970 WEB_BROWSER_MODE,
2971 WEB_CONFERENCE_MODE,
2972 FUNCTION_MODE,
2973 LAYFLAT_MODE
2974};
2975
2976static ssize_t adaptive_kbd_mode_show(struct device *dev,
2977 struct device_attribute *attr,
2978 char *buf)
2979{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03002980 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01002981
2982 current_mode = adaptive_keyboard_get_mode();
2983 if (current_mode < 0)
2984 return current_mode;
2985
2986 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
2987}
2988
2989static ssize_t adaptive_kbd_mode_store(struct device *dev,
2990 struct device_attribute *attr,
2991 const char *buf, size_t count)
2992{
2993 unsigned long t;
2994 int res;
2995
2996 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2997 return -EINVAL;
2998
2999 res = adaptive_keyboard_set_mode(t);
3000 return (res < 0) ? res : count;
3001}
3002
3003static DEVICE_ATTR_RW(adaptive_kbd_mode);
3004
3005static struct attribute *adaptive_kbd_attributes[] = {
3006 &dev_attr_adaptive_kbd_mode.attr,
3007 NULL
3008};
3009
3010static const struct attribute_group adaptive_kbd_attr_group = {
3011 .attrs = adaptive_kbd_attributes,
3012};
3013
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003014/* --------------------------------------------------------------------- */
3015
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003016static struct attribute *hotkey_attributes[] __initdata = {
3017 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003018 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003019 &dev_attr_hotkey_bios_mask.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003020 &dev_attr_wakeup_reason.attr,
3021 &dev_attr_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003022 &dev_attr_hotkey_mask.attr,
3023 &dev_attr_hotkey_all_mask.attr,
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003024 &dev_attr_hotkey_adaptive_all_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003025 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003026#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003027 &dev_attr_hotkey_source_mask.attr,
3028 &dev_attr_hotkey_poll_freq.attr,
3029#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003030};
3031
Johannes Berg19d337d2009-06-02 13:01:37 +02003032/*
3033 * Sync both the hw and sw blocking state of all switches
3034 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003035static void tpacpi_send_radiosw_update(void)
3036{
3037 int wlsw;
3038
Johannes Berg19d337d2009-06-02 13:01:37 +02003039 /*
3040 * We must sync all rfkill controllers *before* issuing any
3041 * rfkill input events, or we will race the rfkill core input
3042 * handler.
3043 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003044 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003045 * for the above.
3046 *
3047 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3048 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003049
Johannes Berg19d337d2009-06-02 13:01:37 +02003050 wlsw = hotkey_get_wlsw();
3051
3052 /* Sync hw blocking state first if it is hw-blocked */
3053 if (wlsw == TPACPI_RFK_RADIO_OFF)
3054 tpacpi_rfk_update_hwblock_state(true);
3055
3056 /* Sync sw blocking state */
3057 tpacpi_rfk_update_swstate_all();
3058
3059 /* Sync hw blocking state last if it is hw-unblocked */
3060 if (wlsw == TPACPI_RFK_RADIO_ON)
3061 tpacpi_rfk_update_hwblock_state(false);
3062
3063 /* Issue rfkill input event for WLSW switch */
3064 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003065 mutex_lock(&tpacpi_inputdev_send_mutex);
3066
3067 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003068 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003069 input_sync(tpacpi_inputdev);
3070
3071 mutex_unlock(&tpacpi_inputdev_send_mutex);
3072 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003073
3074 /*
3075 * this can be unconditional, as we will poll state again
3076 * if userspace uses the notify to read data
3077 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003078 hotkey_radio_sw_notify_change();
3079}
3080
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003081static void hotkey_exit(void)
3082{
3083#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003084 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003085 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003086 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003087#endif
3088
3089 if (hotkey_dev_attributes)
3090 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3091
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003092 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003093 "restoring original HKEY status and mask\n");
3094 /* yes, there is a bitwise or below, we want the
3095 * functions to be called even if one of them fail */
3096 if (((tp_features.hotkey_mask &&
3097 hotkey_mask_set(hotkey_orig_mask)) |
3098 hotkey_status_set(false)) != 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003099 pr_err("failed to restore hot key mask to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003100}
3101
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003102static void __init hotkey_unmap(const unsigned int scancode)
3103{
3104 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3105 clear_bit(hotkey_keycode_map[scancode],
3106 tpacpi_inputdev->keybit);
3107 hotkey_keycode_map[scancode] = KEY_RESERVED;
3108 }
3109}
3110
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003111/*
3112 * HKEY quirks:
3113 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3114 */
3115
3116#define TPACPI_HK_Q_INIMASK 0x0001
3117
3118static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3119 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3120 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3121 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3122 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3123 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3124 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3125 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3126 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3127 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3128 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3129 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3130 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3131 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3132 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3133 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3134 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3135 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3136 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3137 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3138};
3139
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003140typedef u16 tpacpi_keymap_entry_t;
3141typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003142
Lyudeb3180022016-11-11 15:15:02 -05003143static int hotkey_init_tablet_mode(void)
3144{
Darren Hartcb2bf252016-12-14 14:45:48 -08003145 int in_tablet_mode = 0, res;
3146 char *type = NULL;
Lyudeb3180022016-11-11 15:15:02 -05003147
Lyudeb3180022016-11-11 15:15:02 -05003148 if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
Lyudeb03f4d42016-11-11 15:15:03 -05003149 /* For X41t, X60t, X61t Tablets... */
Lyudeb3180022016-11-11 15:15:02 -05003150 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3151 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3152 type = "MHKG";
Lyudeb03f4d42016-11-11 15:15:03 -05003153 } else if (acpi_evalf(ec_handle, &res, "CMMD", "qd")) {
3154 /* For X1 Yoga (2016) */
3155 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_CMMD;
3156 in_tablet_mode = res == TP_EC_CMMD_TABLET_MODE;
3157 type = "CMMD";
Lyudeb3180022016-11-11 15:15:02 -05003158 }
3159
3160 if (!tp_features.hotkey_tablet)
3161 return 0;
3162
3163 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3164 type, in_tablet_mode ? "tablet" : "laptop");
3165
3166 res = add_to_attr_set(hotkey_dev_attributes,
3167 &dev_attr_hotkey_tablet_mode.attr);
3168 if (res)
3169 return -1;
3170
3171 return in_tablet_mode;
3172}
3173
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003174static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003175{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003176 /* Requirements for changing the default keymaps:
3177 *
3178 * 1. Many of the keys are mapped to KEY_RESERVED for very
3179 * good reasons. Do not change them unless you have deep
3180 * knowledge on the IBM and Lenovo ThinkPad firmware for
3181 * the various ThinkPad models. The driver behaves
3182 * differently for KEY_RESERVED: such keys have their
3183 * hot key mask *unset* in mask_recommended, and also
3184 * in the initial hot key mask programmed into the
3185 * firmware at driver load time, which means the firm-
3186 * ware may react very differently if you change them to
3187 * something else;
3188 *
3189 * 2. You must be subscribed to the linux-thinkpad and
3190 * ibm-acpi-devel mailing lists, and you should read the
3191 * list archives since 2007 if you want to change the
3192 * keymaps. This requirement exists so that you will
3193 * know the past history of problems with the thinkpad-
3194 * acpi driver keymaps, and also that you will be
3195 * listening to any bug reports;
3196 *
3197 * 3. Do not send thinkpad-acpi specific patches directly to
3198 * for merging, *ever*. Send them to the linux-acpi
3199 * mailinglist for comments. Merging is to be done only
3200 * through acpi-test and the ACPI maintainer.
3201 *
3202 * If the above is too much to ask, don't change the keymap.
3203 * Ask the thinkpad-acpi maintainer to do it, instead.
3204 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003205
3206 enum keymap_index {
3207 TPACPI_KEYMAP_IBM_GENERIC = 0,
3208 TPACPI_KEYMAP_LENOVO_GENERIC,
3209 };
3210
3211 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3212 /* Generic keymap for IBM ThinkPads */
3213 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003214 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003215 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003216 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3217 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003218
3219 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003220 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3221 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3222 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003223
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003224 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003225 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003226 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003227
3228 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003229 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003230
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003231 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3232 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003233
3234 /* Volume: firmware always react to it and reprograms
3235 * the built-in *extra* mixer. Never map it to control
3236 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003237 KEY_RESERVED, /* 0x14: VOLUME UP */
3238 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3239 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003240
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003241 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003242
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003243 /* (assignments unknown, please report if found) */
3244 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3245 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003246
3247 /* No assignments, only used for Adaptive keyboards. */
3248 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3249 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3250 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3251 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3252 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Christian Kellner696c65232017-02-28 17:10:57 +01003253
3254 /* No assignment, used for newer Lenovo models */
3255 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3256 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3257 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3258 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3259 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3260 KEY_UNKNOWN, KEY_UNKNOWN
3261
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003262 },
3263
3264 /* Generic keymap for Lenovo ThinkPads */
3265 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003266 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3267 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003268 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003269 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003270
3271 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003272 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3273 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3274 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003275
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003276 /* These should be enabled --only-- when ACPI video
3277 * is disabled (i.e. in "vendor" mode), and are handled
3278 * in a special way by the init code */
3279 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3280 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003281
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003282 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003283
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003284 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3285 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003286
3287 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3288 * react to it and reprograms the built-in *extra* mixer.
3289 * Never map it to control another mixer by default.
3290 *
3291 * T60?, T61, R60?, R61: firmware and EC tries to send
3292 * these over the regular keyboard, so these are no-ops,
3293 * but there are still weird bugs re. MUTE, so do not
3294 * change unless you get test reports from all Lenovo
3295 * models. May cause the BIOS to interfere with the
3296 * HDA mixer.
3297 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003298 KEY_RESERVED, /* 0x14: VOLUME UP */
3299 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3300 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003301
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003302 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003303
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003304 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003305 KEY_UNKNOWN, KEY_UNKNOWN,
3306
3307 /*
3308 * The mic mute button only sends 0x1a. It does not
3309 * automatically mute the mic or change the mute light.
3310 */
3311 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3312
3313 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003314 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003315
3316 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003317 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003318
3319 /*
3320 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3321 * The first item in this list is the Mute button which is
3322 * emitted with 0x103 through
3323 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3324 * symbol is held.
3325 * We'll need to offset those by 0x20.
3326 */
3327 KEY_RESERVED, /* Mute held, 0x103 */
3328 KEY_BRIGHTNESS_MIN, /* Backlight off */
3329 KEY_RESERVED, /* Clipping tool */
3330 KEY_RESERVED, /* Cloud */
3331 KEY_RESERVED,
3332 KEY_VOICECOMMAND, /* Voice */
3333 KEY_RESERVED,
3334 KEY_RESERVED, /* Gestures */
3335 KEY_RESERVED,
3336 KEY_RESERVED,
3337 KEY_RESERVED,
3338 KEY_CONFIG, /* Settings */
3339 KEY_RESERVED, /* New tab */
3340 KEY_REFRESH, /* Reload */
3341 KEY_BACK, /* Back */
3342 KEY_RESERVED, /* Microphone down */
3343 KEY_RESERVED, /* Microphone up */
3344 KEY_RESERVED, /* Microphone cancellation */
3345 KEY_RESERVED, /* Camera mode */
3346 KEY_RESERVED, /* Rotate display, 0x116 */
Christian Kellner696c65232017-02-28 17:10:57 +01003347
3348 /*
3349 * These are found in 2017 models (e.g. T470s, X270).
3350 * The lowest known value is 0x311, which according to
3351 * the manual should launch a user defined favorite
3352 * application.
3353 *
3354 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3355 * corresponding to 0x34.
3356 */
3357
3358 /* (assignments unknown, please report if found) */
3359 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3360 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3361 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3362 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3363 KEY_UNKNOWN,
3364
3365 KEY_FAVORITES, /* Favorite app, 0x311 */
3366 KEY_RESERVED, /* Clipping tool */
3367 KEY_RESERVED,
3368 KEY_BLUETOOTH, /* Bluetooth */
3369 KEY_KEYBOARD /* Keyboard, 0x315 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003370 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003371 };
3372
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003373 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3374 /* Generic maps (fallback) */
3375 {
3376 .vendor = PCI_VENDOR_ID_IBM,
3377 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3378 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3379 },
3380 {
3381 .vendor = PCI_VENDOR_ID_LENOVO,
3382 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3383 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3384 },
3385 };
3386
3387#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003388#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003389
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003390 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003391 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003392 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003393 bool radiosw_state = false;
3394 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003395
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003396 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003397 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003398
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003399 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3400 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003401
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003402 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003403 BUG_ON(tpacpi_inputdev->open != NULL ||
3404 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003405
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003406 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003407 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003408
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003409#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003410 mutex_init(&hotkey_thread_data_mutex);
3411#endif
3412
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003413 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003414 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003415
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003416 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3417 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003418 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003419
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003420 if (!tp_features.hotkey)
3421 return 1;
3422
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003423 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3424 ARRAY_SIZE(tpacpi_hotkey_qtable));
3425
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003426 tpacpi_disable_brightness_delay();
3427
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003428 /* MUST have enough space for all attributes to be added to
3429 * hotkey_dev_attributes */
3430 hotkey_dev_attributes = create_attr_set(
3431 ARRAY_SIZE(hotkey_attributes) + 2,
3432 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003433 if (!hotkey_dev_attributes)
3434 return -ENOMEM;
3435 res = add_many_to_attr_set(hotkey_dev_attributes,
3436 hotkey_attributes,
3437 ARRAY_SIZE(hotkey_attributes));
3438 if (res)
3439 goto err_exit;
3440
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003441 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003442 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3443 for HKEY interface version 0x100 */
3444 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003445 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3446 "firmware HKEY interface version: 0x%x\n",
3447 hkeyv);
3448
3449 switch (hkeyv >> 8) {
3450 case 1:
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003451 /*
3452 * MHKV 0x100 in A31, R40, R40e,
3453 * T4x, X31, and later
3454 */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003455
3456 /* Paranoia check AND init hotkey_all_mask */
3457 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3458 "MHKA", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003459 pr_err("missing MHKA handler, please report this to %s\n",
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003460 TPACPI_MAIL);
3461 /* Fallback: pre-init for FN+F3,F4,F12 */
3462 hotkey_all_mask = 0x080cU;
3463 } else {
3464 tp_features.hotkey_mask = 1;
3465 }
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003466 break;
3467
3468 case 2:
3469 /*
3470 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3471 */
3472
3473 /* Paranoia check AND init hotkey_all_mask */
3474 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3475 "MHKA", "dd", 1)) {
3476 pr_err("missing MHKA handler, please report this to %s\n",
3477 TPACPI_MAIL);
3478 /* Fallback: pre-init for FN+F3,F4,F12 */
3479 hotkey_all_mask = 0x080cU;
3480 } else {
3481 tp_features.hotkey_mask = 1;
3482 }
3483
3484 /*
3485 * Check if we have an adaptive keyboard, like on the
3486 * Lenovo Carbon X1 2014 (2nd Gen).
3487 */
3488 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3489 "MHKA", "dd", 2)) {
3490 if (hotkey_adaptive_all_mask != 0) {
3491 tp_features.has_adaptive_kbd = true;
3492 res = sysfs_create_group(
3493 &tpacpi_pdev->dev.kobj,
3494 &adaptive_kbd_attr_group);
3495 if (res)
3496 goto err_exit;
3497 }
3498 } else {
3499 tp_features.has_adaptive_kbd = false;
3500 hotkey_adaptive_all_mask = 0x0U;
3501 }
3502 break;
3503
3504 default:
3505 pr_err("unknown version of the HKEY interface: 0x%x\n",
3506 hkeyv);
3507 pr_err("please report this to %s\n", TPACPI_MAIL);
3508 break;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003509 }
3510 }
3511
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003512 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3513 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003514 str_supported(tp_features.hotkey_mask));
3515
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003516 /* Init hotkey_all_mask if not initialized yet */
3517 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3518 (quirks & TPACPI_HK_Q_INIMASK))
3519 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003520
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003521 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003522 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003523 /* hotkey_source_mask *must* be zero for
3524 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003525 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003526 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003527 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003528
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003529 hotkey_orig_mask = hotkey_acpi_mask;
3530 } else {
3531 hotkey_orig_mask = hotkey_all_mask;
3532 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003533 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003534
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003535#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3536 if (dbg_wlswemul) {
3537 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003538 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003539 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003540 } else
3541#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003542 /* Not all thinkpads have a hardware radio switch */
3543 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3544 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003545 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003546 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003547 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003548 }
3549 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003550 res = add_to_attr_set(hotkey_dev_attributes,
3551 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003552
Lyudeb3180022016-11-11 15:15:02 -05003553 res = hotkey_init_tablet_mode();
3554 if (res < 0)
3555 goto err_exit;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003556
Lyudeb3180022016-11-11 15:15:02 -05003557 tabletsw_state = res;
3558
3559 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3560 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003561 if (res)
3562 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003563
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003564 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003565 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3566 GFP_KERNEL);
3567 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003568 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003569 res = -ENOMEM;
3570 goto err_exit;
3571 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003572
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003573 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3574 ARRAY_SIZE(tpacpi_keymap_qtable));
3575 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3576 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3577 "using keymap number %lu\n", keymap_id);
3578
3579 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3580 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003581
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003582 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003583 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3584 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3585 tpacpi_inputdev->keycode = hotkey_keycode_map;
3586 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3587 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003588 input_set_capability(tpacpi_inputdev, EV_KEY,
3589 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003590 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003591 if (i < sizeof(hotkey_reserved_mask)*8)
3592 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003593 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003594 }
3595
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003596 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003597 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003598 input_report_switch(tpacpi_inputdev,
3599 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003600 }
3601 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003602 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003603 input_report_switch(tpacpi_inputdev,
3604 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003605 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003606
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003607 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003608 * userspace. tpacpi_detect_brightness_capabilities() must have
3609 * been called before this point */
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02003610 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003611 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3612 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003613
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003614 /* Disable brightness up/down on Lenovo thinkpads when
3615 * ACPI is handling them, otherwise it is plain impossible
3616 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003617 hotkey_reserved_mask |=
3618 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3619 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003620 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3621 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003622 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003623
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003624#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003625 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3626 & ~hotkey_all_mask
3627 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003628
3629 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3630 "hotkey source mask 0x%08x, polling freq %u\n",
3631 hotkey_source_mask, hotkey_poll_freq);
3632#endif
3633
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003634 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3635 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003636 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003637 if (res) {
3638 hotkey_exit();
3639 return res;
3640 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003641 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3642 | hotkey_driver_mask)
3643 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003644 if (res < 0 && res != -ENXIO) {
3645 hotkey_exit();
3646 return res;
3647 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003648 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3649 & ~hotkey_reserved_mask;
3650 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3651 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3652 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003653
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003654 tpacpi_inputdev->open = &hotkey_inputdev_open;
3655 tpacpi_inputdev->close = &hotkey_inputdev_close;
3656
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003657 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003658
3659 return 0;
3660
3661err_exit:
3662 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
Bastien Nocerab790cee2015-03-02 14:45:27 +01003663 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3664 &adaptive_kbd_attr_group);
3665
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003666 hotkey_dev_attributes = NULL;
3667
Jan van den Berg72a979f2014-09-17 00:01:08 +02003668 return (res < 0) ? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003669}
3670
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003671/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3672 * mode, Web conference mode, Function mode and Lay-flat mode.
3673 * We support Home mode and Function mode currently.
3674 *
3675 * Will consider support rest of modes in future.
3676 *
3677 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003678static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003679 HOME_MODE,
3680/* WEB_BROWSER_MODE = 2,
3681 WEB_CONFERENCE_MODE = 3, */
3682 FUNCTION_MODE
3683};
3684
3685#define DFR_CHANGE_ROW 0x101
3686#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003687#define FIRST_ADAPTIVE_KEY 0x103
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003688
3689/* press Fn key a while second, it will switch to Function Mode. Then
3690 * release Fn key, previous mode be restored.
3691 */
3692static bool adaptive_keyboard_mode_is_saved;
3693static int adaptive_keyboard_prev_mode;
3694
Bastien Noceraf74587f2015-03-02 14:45:22 +01003695static int adaptive_keyboard_get_mode(void)
3696{
3697 int mode = 0;
3698
3699 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3700 pr_err("Cannot read adaptive keyboard mode\n");
3701 return -EIO;
3702 }
3703
3704 return mode;
3705}
3706
3707static int adaptive_keyboard_set_mode(int new_mode)
3708{
3709 if (new_mode < 0 ||
3710 new_mode > LAYFLAT_MODE)
3711 return -EINVAL;
3712
3713 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3714 pr_err("Cannot set adaptive keyboard mode\n");
3715 return -EIO;
3716 }
3717
3718 return 0;
3719}
3720
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003721static int adaptive_keyboard_get_next_mode(int mode)
3722{
3723 size_t i;
3724 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3725
3726 for (i = 0; i <= max_mode; i++) {
3727 if (adaptive_keyboard_modes[i] == mode)
3728 break;
3729 }
3730
3731 if (i >= max_mode)
3732 i = 0;
3733 else
3734 i++;
3735
3736 return adaptive_keyboard_modes[i];
3737}
3738
3739static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3740{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003741 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003742 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003743 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003744
3745 switch (scancode) {
3746 case DFR_CHANGE_ROW:
3747 if (adaptive_keyboard_mode_is_saved) {
3748 new_mode = adaptive_keyboard_prev_mode;
3749 adaptive_keyboard_mode_is_saved = false;
3750 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003751 current_mode = adaptive_keyboard_get_mode();
3752 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003753 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003754 new_mode = adaptive_keyboard_get_next_mode(
3755 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003756 }
3757
Bastien Noceraf74587f2015-03-02 14:45:22 +01003758 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003759 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003760
3761 return true;
3762
3763 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003764 current_mode = adaptive_keyboard_get_mode();
3765 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003766 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003767
Bastien Noceraf74587f2015-03-02 14:45:22 +01003768 adaptive_keyboard_prev_mode = current_mode;
3769 adaptive_keyboard_mode_is_saved = true;
3770
3771 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3772 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003773 return true;
3774
3775 default:
Dan Carpenter741d98c2015-03-11 12:36:07 +03003776 if (scancode < FIRST_ADAPTIVE_KEY ||
Christian Kellner696c65232017-02-28 17:10:57 +01003777 scancode >= FIRST_ADAPTIVE_KEY +
3778 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3779 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
Bastien Nocera6a68d852015-03-02 14:45:31 +01003780 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003781 scancode);
Bastien Nocera6a68d852015-03-02 14:45:31 +01003782 return false;
3783 }
Christian Kellner149c8c72017-02-28 17:10:56 +01003784 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3785 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
Bastien Nocera6a68d852015-03-02 14:45:31 +01003786 if (keycode != KEY_RESERVED) {
3787 mutex_lock(&tpacpi_inputdev_send_mutex);
3788
3789 input_report_key(tpacpi_inputdev, keycode, 1);
3790 input_sync(tpacpi_inputdev);
3791
3792 input_report_key(tpacpi_inputdev, keycode, 0);
3793 input_sync(tpacpi_inputdev);
3794
3795 mutex_unlock(&tpacpi_inputdev_send_mutex);
3796 }
3797 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003798 }
3799}
3800
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003801static bool hotkey_notify_hotkey(const u32 hkey,
3802 bool *send_acpi_ev,
3803 bool *ignore_acpi_ev)
3804{
3805 /* 0x1000-0x1FFF: key presses */
3806 unsigned int scancode = hkey & 0xfff;
3807 *send_acpi_ev = true;
3808 *ignore_acpi_ev = false;
3809
Christian Kellner696c65232017-02-28 17:10:57 +01003810 /*
3811 * Original events are in the 0x10XX range, the adaptive keyboard
3812 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3813 * models, additional keys are emitted through 0x13XX.
3814 */
3815 switch ((hkey >> 8) & 0xf) {
3816 case 0:
3817 if (scancode > 0 &&
3818 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3819 /* HKEY event 0x1001 is scancode 0x00 */
3820 scancode--;
3821 if (!(hotkey_source_mask & (1 << scancode))) {
3822 tpacpi_input_send_key_masked(scancode);
3823 *send_acpi_ev = false;
3824 } else {
3825 *ignore_acpi_ev = true;
3826 }
3827 return true;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003828 }
Christian Kellner696c65232017-02-28 17:10:57 +01003829 break;
3830
3831 case 1:
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003832 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Christian Kellner696c65232017-02-28 17:10:57 +01003833
3834 case 3:
3835 /* Extended keycodes start at 0x300 and our offset into the map
3836 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3837 * will be positive, but might not be in the correct range.
3838 */
3839 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3840 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3841 scancode < TPACPI_HOTKEY_MAP_LEN) {
3842 tpacpi_input_send_key(scancode);
3843 return true;
3844 }
3845 break;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003846 }
Christian Kellner696c65232017-02-28 17:10:57 +01003847
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003848 return false;
3849}
3850
3851static bool hotkey_notify_wakeup(const u32 hkey,
3852 bool *send_acpi_ev,
3853 bool *ignore_acpi_ev)
3854{
3855 /* 0x2000-0x2FFF: Wakeup reason */
3856 *send_acpi_ev = true;
3857 *ignore_acpi_ev = false;
3858
3859 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003860 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3861 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003862 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3863 *ignore_acpi_ev = true;
3864 break;
3865
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003866 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3867 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003868 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3869 *ignore_acpi_ev = true;
3870 break;
3871
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003872 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3873 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003874 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003875 /* how to auto-heal: */
3876 /* 2313: woke up from S3, go to S4/S5 */
3877 /* 2413: woke up from S4, go to S5 */
3878 break;
3879
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003880 default:
3881 return false;
3882 }
3883
3884 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003885 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003886 hotkey_wakeup_reason_notify_change();
3887 }
3888 return true;
3889}
3890
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003891static bool hotkey_notify_dockevent(const u32 hkey,
3892 bool *send_acpi_ev,
3893 bool *ignore_acpi_ev)
3894{
3895 /* 0x4000-0x4FFF: dock-related events */
3896 *send_acpi_ev = true;
3897 *ignore_acpi_ev = false;
3898
3899 switch (hkey) {
3900 case TP_HKEY_EV_UNDOCK_ACK:
3901 /* ACPI undock operation completed after wakeup */
3902 hotkey_autosleep_ack = 1;
3903 pr_info("undocked\n");
3904 hotkey_wakeup_hotunplug_complete_notify_change();
3905 return true;
3906
3907 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3908 pr_info("docked into hotplug port replicator\n");
3909 return true;
3910 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3911 pr_info("undocked from hotplug port replicator\n");
3912 return true;
3913
3914 default:
3915 return false;
3916 }
3917}
3918
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003919static bool hotkey_notify_usrevent(const u32 hkey,
3920 bool *send_acpi_ev,
3921 bool *ignore_acpi_ev)
3922{
3923 /* 0x5000-0x5FFF: human interface helpers */
3924 *send_acpi_ev = true;
3925 *ignore_acpi_ev = false;
3926
3927 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003928 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3929 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003930 return true;
3931
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003932 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3933 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003934 tpacpi_input_send_tabletsw();
3935 hotkey_tablet_mode_notify_change();
3936 *send_acpi_ev = false;
3937 return true;
3938
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003939 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3940 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3941 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003942 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003943 *ignore_acpi_ev = true;
3944 return true;
3945
3946 default:
3947 return false;
3948 }
3949}
3950
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003951static void thermal_dump_all_sensors(void);
3952
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003953static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003954 bool *send_acpi_ev,
3955 bool *ignore_acpi_ev)
3956{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003957 bool known = true;
3958
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003959 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003960 *send_acpi_ev = true;
3961 *ignore_acpi_ev = false;
3962
3963 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003964 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003965 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003966 /* recommended action: do nothing, we don't have
3967 * Lenovo ATM information */
3968 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003969 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003970 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003971 /* recommended action: warn user through gui */
3972 break;
3973 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003974 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003975 /* recommended action: immediate sleep/hibernate */
3976 break;
3977 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003978 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003979 /* recommended action: warn user through gui, that */
3980 /* some internal component is too hot */
3981 break;
3982 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03003983 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003984 /* recommended action: immediate sleep/hibernate */
3985 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01003986 case TP_HKEY_EV_AC_CHANGED:
3987 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3988 * AC status changed; can be triggered by plugging or
3989 * unplugging AC adapter, docking or undocking. */
3990
3991 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003992
3993 case TP_HKEY_EV_KEY_NUMLOCK:
3994 case TP_HKEY_EV_KEY_FN:
Xavier Naveira67ab6242015-02-10 08:45:18 +01003995 case TP_HKEY_EV_KEY_FN_ESC:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003996 /* key press events, we just ignore them as long as the EC
3997 * is still reporting them in the normal keyboard stream */
3998 *send_acpi_ev = false;
3999 *ignore_acpi_ev = true;
4000 return true;
4001
Lyudeb03f4d42016-11-11 15:15:03 -05004002 case TP_HKEY_EV_TABLET_CHANGED:
4003 tpacpi_input_send_tabletsw();
4004 hotkey_tablet_mode_notify_change();
4005 *send_acpi_ev = false;
4006 break;
4007
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004008 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004009 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004010 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004011 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004012
4013 thermal_dump_all_sensors();
4014
4015 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004016}
4017
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004018static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4019{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004020 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004021 bool send_acpi_ev;
4022 bool ignore_acpi_ev;
4023 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004024
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004025 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07004026 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004027 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004028 acpi_bus_generate_netlink_event(
4029 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004030 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004031 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004032 return;
4033 }
4034
4035 while (1) {
4036 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07004037 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004038 return;
4039 }
4040
4041 if (hkey == 0) {
4042 /* queue empty */
4043 return;
4044 }
4045
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004046 send_acpi_ev = true;
4047 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004048
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004049 switch (hkey >> 12) {
4050 case 1:
4051 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004052 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4053 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004054 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004055 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004056 /* 0x2000-0x2FFF: Wakeup reason */
4057 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4058 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004059 break;
4060 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004061 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004062 switch (hkey) {
4063 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004064 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004065 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004066 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004067 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004068 break;
4069 case TP_HKEY_EV_OPTDRV_EJ:
4070 /* FIXME: kick libata if SATA link offline */
4071 known_ev = true;
4072 break;
4073 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004074 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004075 }
4076 break;
4077 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03004078 /* 0x4000-0x4FFF: dock-related events */
4079 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4080 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004081 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004082 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02004083 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004084 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4085 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004086 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004087 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004088 /* 0x6000-0x6FFF: thermal alarms/notices and
4089 * keyboard events */
4090 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004091 &ignore_acpi_ev);
4092 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004093 case 7:
4094 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004095 if (tp_features.hotkey_wlsw &&
4096 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004097 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004098 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004099 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004100 break;
4101 }
4102 /* fallthrough to default */
4103 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004104 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004105 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004106 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07004107 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004108 pr_notice("please report the conditions when this event happened to %s\n",
4109 TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004110 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004111
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004112 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03004113 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004114 acpi_bus_generate_netlink_event(
4115 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004116 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004117 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004118 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004119 }
4120}
4121
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02004122static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004123{
4124 /* Do these on suspend, we get the events on early resume! */
4125 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4126 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08004127
4128 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004129 if (tp_features.has_adaptive_kbd) {
4130 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4131 "GTRW", "dd", 0)) {
4132 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004133 }
4134 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004135}
4136
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004137static void hotkey_resume(void)
4138{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03004139 tpacpi_disable_brightness_delay();
4140
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004141 if (hotkey_status_set(true) < 0 ||
4142 hotkey_mask_set(hotkey_acpi_mask) < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004143 pr_err("error while attempting to reset the event firmware interface\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004144
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004145 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02004146 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004147 hotkey_wakeup_reason_notify_change();
4148 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004149 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004150
4151 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004152 if (tp_features.has_adaptive_kbd) {
4153 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4154 adaptive_keyboard_prev_mode)) {
4155 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004156 }
4157 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004158}
4159
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004160/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004161static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004163 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004165 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004166 seq_printf(m, "status:\t\tnot supported\n");
4167 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004168 }
4169
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004170 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004171 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004172 res = hotkey_status_get(&status);
4173 if (!res)
4174 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004175 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004176 if (res)
4177 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004179 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004180 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004181 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4182 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004184 seq_printf(m, "mask:\t\tnot supported\n");
4185 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 }
4187
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189}
4190
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004191static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004192{
4193 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004194 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004195 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4196 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 +00004197}
4198
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004199static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004201 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004202 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004205 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 return -ENODEV;
4207
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004208 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004209 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004210
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004211 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004212
4213 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 while ((cmd = next_cmd(&buf))) {
4215 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004216 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004218 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004219 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03004221 mask = (hotkey_all_mask | hotkey_source_mask)
4222 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4224 /* mask set */
4225 } else if (sscanf(cmd, "%x", &mask) == 1) {
4226 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004227 } else {
4228 res = -EINVAL;
4229 goto errexit;
4230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004232
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004233 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004234 tpacpi_disclose_usertask("procfs hotkey",
4235 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004236 res = hotkey_user_mask_set(mask);
4237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004239errexit:
4240 mutex_unlock(&hotkey_mutex);
4241 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004242}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004244static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004245 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4246 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Hui Wanga3c42a42016-11-08 16:13:23 +08004247 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004248 {"", 0},
4249};
4250
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004251static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004252 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004253 .notify = hotkey_notify,
4254 .handle = &hkey_handle,
4255 .type = ACPI_DEVICE_NOTIFY,
4256};
4257
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004258static struct ibm_struct hotkey_driver_data = {
4259 .name = "hotkey",
4260 .read = hotkey_read,
4261 .write = hotkey_write,
4262 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004263 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004264 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004265 .acpi = &ibm_hotkey_acpidriver,
4266};
4267
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004268/*************************************************************************
4269 * Bluetooth subdriver
4270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004272enum {
4273 /* ACPI GBDC/SBDC bits */
4274 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4275 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004276 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004277 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004278};
4279
4280enum {
4281 /* ACPI \BLTH commands */
4282 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4283 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4284 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4285 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4286 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004287};
4288
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004289#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4290
Johannes Berg19d337d2009-06-02 13:01:37 +02004291static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004292{
4293 int status;
4294
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004295#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4296 if (dbg_bluetoothemul)
4297 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004298 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004299#endif
4300
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004301 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4302 return -EIO;
4303
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004304 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004305 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004306}
4307
Johannes Berg19d337d2009-06-02 13:01:37 +02004308static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004309{
4310 int status;
4311
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004312 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004313 "will attempt to %s bluetooth\n",
4314 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004315
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004316#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4317 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004318 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004319 return 0;
4320 }
4321#endif
4322
Johannes Berg19d337d2009-06-02 13:01:37 +02004323 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004324 status = TP_ACPI_BLUETOOTH_RADIOSSW
4325 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4326 else
4327 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004328
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004329 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4330 return -EIO;
4331
4332 return 0;
4333}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004334
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004335/* sysfs bluetooth enable ---------------------------------------------- */
4336static ssize_t bluetooth_enable_show(struct device *dev,
4337 struct device_attribute *attr,
4338 char *buf)
4339{
Johannes Berg19d337d2009-06-02 13:01:37 +02004340 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4341 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004342}
4343
4344static ssize_t bluetooth_enable_store(struct device *dev,
4345 struct device_attribute *attr,
4346 const char *buf, size_t count)
4347{
Johannes Berg19d337d2009-06-02 13:01:37 +02004348 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4349 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004350}
4351
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004352static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004353
4354/* --------------------------------------------------------------------- */
4355
4356static struct attribute *bluetooth_attributes[] = {
4357 &dev_attr_bluetooth_enable.attr,
4358 NULL
4359};
4360
4361static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004362 .attrs = bluetooth_attributes,
4363};
4364
Johannes Berg19d337d2009-06-02 13:01:37 +02004365static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4366 .get_status = bluetooth_get_status,
4367 .set_status = bluetooth_set_status,
4368};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004369
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004370static void bluetooth_shutdown(void)
4371{
4372 /* Order firmware to save current state to NVRAM */
4373 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4374 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004375 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004376 else
4377 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004378 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004379}
4380
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004381static void bluetooth_exit(void)
4382{
4383 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4384 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004385
4386 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4387
4388 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004389}
4390
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004391static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004393 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004394 int status = 0;
4395
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004396 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4397 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004398
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004399 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004400
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004401 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4402 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004403 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004404 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004406 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4407 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004408 str_supported(tp_features.bluetooth),
4409 status);
4410
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004411#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4412 if (dbg_bluetoothemul) {
4413 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004414 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004415 } else
4416#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004417 if (tp_features.bluetooth &&
4418 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4419 /* no bluetooth hardware present in system */
4420 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004421 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004422 "bluetooth hardware not installed\n");
4423 }
4424
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004425 if (!tp_features.bluetooth)
4426 return 1;
4427
Johannes Berg19d337d2009-06-02 13:01:37 +02004428 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4429 &bluetooth_tprfk_ops,
4430 RFKILL_TYPE_BLUETOOTH,
4431 TPACPI_RFK_BLUETOOTH_SW_NAME,
4432 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004433 if (res)
4434 return res;
4435
Johannes Berg19d337d2009-06-02 13:01:37 +02004436 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4437 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004438 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004439 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004440 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004441 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004442
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444}
4445
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004446/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004447static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004449 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450}
4451
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004452static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453{
Johannes Berg19d337d2009-06-02 13:01:37 +02004454 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455}
4456
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004457static struct ibm_struct bluetooth_driver_data = {
4458 .name = "bluetooth",
4459 .read = bluetooth_read,
4460 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004461 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004462 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004463};
4464
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004465/*************************************************************************
4466 * Wan subdriver
4467 */
4468
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004469enum {
4470 /* ACPI GWAN/SWAN bits */
4471 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4472 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004473 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004474 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004475};
4476
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004477#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4478
Johannes Berg19d337d2009-06-02 13:01:37 +02004479static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004480{
4481 int status;
4482
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004483#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4484 if (dbg_wwanemul)
4485 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004486 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004487#endif
4488
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004489 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4490 return -EIO;
4491
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004492 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004493 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004494}
4495
Johannes Berg19d337d2009-06-02 13:01:37 +02004496static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004497{
4498 int status;
4499
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004500 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004501 "will attempt to %s wwan\n",
4502 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004503
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004504#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4505 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004506 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004507 return 0;
4508 }
4509#endif
4510
Johannes Berg19d337d2009-06-02 13:01:37 +02004511 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004512 status = TP_ACPI_WANCARD_RADIOSSW
4513 | TP_ACPI_WANCARD_RESUMECTRL;
4514 else
4515 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004516
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004517 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4518 return -EIO;
4519
4520 return 0;
4521}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004522
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004523/* sysfs wan enable ---------------------------------------------------- */
4524static ssize_t wan_enable_show(struct device *dev,
4525 struct device_attribute *attr,
4526 char *buf)
4527{
Johannes Berg19d337d2009-06-02 13:01:37 +02004528 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4529 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004530}
4531
4532static ssize_t wan_enable_store(struct device *dev,
4533 struct device_attribute *attr,
4534 const char *buf, size_t count)
4535{
Johannes Berg19d337d2009-06-02 13:01:37 +02004536 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4537 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004538}
4539
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004540static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4541 wan_enable_show, wan_enable_store);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004542
4543/* --------------------------------------------------------------------- */
4544
4545static struct attribute *wan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004546 &dev_attr_wwan_enable.attr,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004547 NULL
4548};
4549
4550static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004551 .attrs = wan_attributes,
4552};
4553
Johannes Berg19d337d2009-06-02 13:01:37 +02004554static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4555 .get_status = wan_get_status,
4556 .set_status = wan_set_status,
4557};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004558
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004559static void wan_shutdown(void)
4560{
4561 /* Order firmware to save current state to NVRAM */
4562 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4563 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004564 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004565 else
4566 vdbg_printk(TPACPI_DBG_RFKILL,
4567 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004568}
4569
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004570static void wan_exit(void)
4571{
4572 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4573 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004574
4575 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4576
4577 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004578}
4579
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004580static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004581{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004582 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004583 int status = 0;
4584
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004585 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4586 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004587
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004588 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004589
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004590 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004591 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004592
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004593 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4594 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004595 str_supported(tp_features.wan),
4596 status);
4597
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004598#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4599 if (dbg_wwanemul) {
4600 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004601 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004602 } else
4603#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004604 if (tp_features.wan &&
4605 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4606 /* no wan hardware present in system */
4607 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004608 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004609 "wan hardware not installed\n");
4610 }
4611
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004612 if (!tp_features.wan)
4613 return 1;
4614
Johannes Berg19d337d2009-06-02 13:01:37 +02004615 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4616 &wan_tprfk_ops,
4617 RFKILL_TYPE_WWAN,
4618 TPACPI_RFK_WWAN_SW_NAME,
4619 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004620 if (res)
4621 return res;
4622
Johannes Berg19d337d2009-06-02 13:01:37 +02004623 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4624 &wan_attr_group);
4625
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004626 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004627 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004628 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004629 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004630
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004631 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004632}
4633
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004634/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004635static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004636{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004637 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004638}
4639
4640static int wan_write(char *buf)
4641{
Johannes Berg19d337d2009-06-02 13:01:37 +02004642 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004643}
4644
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004645static struct ibm_struct wan_driver_data = {
4646 .name = "wan",
4647 .read = wan_read,
4648 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004649 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004650 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004651};
4652
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004653/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004654 * UWB subdriver
4655 */
4656
4657enum {
4658 /* ACPI GUWB/SUWB bits */
4659 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4660 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4661};
4662
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004663#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4664
Johannes Berg19d337d2009-06-02 13:01:37 +02004665static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004666{
4667 int status;
4668
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004669#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4670 if (dbg_uwbemul)
4671 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004672 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004673#endif
4674
4675 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4676 return -EIO;
4677
4678 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004679 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004680}
4681
Johannes Berg19d337d2009-06-02 13:01:37 +02004682static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004683{
4684 int status;
4685
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004686 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004687 "will attempt to %s UWB\n",
4688 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004689
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004690#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4691 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004692 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004693 return 0;
4694 }
4695#endif
4696
Johannes Berg19d337d2009-06-02 13:01:37 +02004697 if (state == TPACPI_RFK_RADIO_ON)
4698 status = TP_ACPI_UWB_RADIOSSW;
4699 else
4700 status = 0;
4701
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004702 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4703 return -EIO;
4704
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004705 return 0;
4706}
4707
4708/* --------------------------------------------------------------------- */
4709
Johannes Berg19d337d2009-06-02 13:01:37 +02004710static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4711 .get_status = uwb_get_status,
4712 .set_status = uwb_set_status,
4713};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004714
4715static void uwb_exit(void)
4716{
Johannes Berg19d337d2009-06-02 13:01:37 +02004717 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004718}
4719
4720static int __init uwb_init(struct ibm_init_struct *iibm)
4721{
4722 int res;
4723 int status = 0;
4724
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004725 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4726 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004727
4728 TPACPI_ACPIHANDLE_INIT(hkey);
4729
4730 tp_features.uwb = hkey_handle &&
4731 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4732
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004733 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4734 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004735 str_supported(tp_features.uwb),
4736 status);
4737
4738#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4739 if (dbg_uwbemul) {
4740 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004741 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004742 } else
4743#endif
4744 if (tp_features.uwb &&
4745 !(status & TP_ACPI_UWB_HWPRESENT)) {
4746 /* no uwb hardware present in system */
4747 tp_features.uwb = 0;
4748 dbg_printk(TPACPI_DBG_INIT,
4749 "uwb hardware not installed\n");
4750 }
4751
4752 if (!tp_features.uwb)
4753 return 1;
4754
4755 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004756 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004757 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004758 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004759 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004760 return res;
4761}
4762
4763static struct ibm_struct uwb_driver_data = {
4764 .name = "uwb",
4765 .exit = uwb_exit,
4766 .flags.experimental = 1,
4767};
4768
4769/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004770 * Video subdriver
4771 */
4772
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004773#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4774
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004775enum video_access_mode {
4776 TPACPI_VIDEO_NONE = 0,
4777 TPACPI_VIDEO_570, /* 570 */
4778 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4779 TPACPI_VIDEO_NEW, /* all others */
4780};
4781
4782enum { /* video status flags, based on VIDEO_570 */
4783 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4784 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4785 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4786};
4787
4788enum { /* TPACPI_VIDEO_570 constants */
4789 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4790 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4791 * video_status_flags */
4792 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4793 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4794};
4795
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004796static enum video_access_mode video_supported;
4797static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004798
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004799static int video_autosw_get(void);
4800static int video_autosw_set(int enable);
4801
Joe Perches112a6ee2011-04-04 10:06:24 -07004802TPACPI_HANDLE(vid, root,
4803 "\\_SB.PCI.AGP.VGA", /* 570 */
4804 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4805 "\\_SB.PCI0.VID0", /* 770e */
4806 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4807 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4808 "\\_SB.PCI0.AGP.VID", /* all others */
4809 ); /* R30, R31 */
4810
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004811TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004812
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004813static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004815 int ivga;
4816
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004817 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4818
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004819 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004820 if (tpacpi_is_ibm())
4821 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004822
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004823 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4824 /* G41, assume IVGA doesn't change */
4825 vid_handle = vid2_handle;
4826
4827 if (!vid_handle)
4828 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004829 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004830 else if (tpacpi_is_ibm() &&
4831 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004832 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004833 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004834 else if (tpacpi_is_ibm() &&
4835 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004836 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004837 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004838 else
4839 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004840 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004842 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4843 str_supported(video_supported != TPACPI_VIDEO_NONE),
4844 video_supported);
4845
Jan van den Berg72a979f2014-09-17 00:01:08 +02004846 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847}
4848
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004849static void video_exit(void)
4850{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004851 dbg_printk(TPACPI_DBG_EXIT,
4852 "restoring original video autoswitch mode\n");
4853 if (video_autosw_set(video_orig_autosw))
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03004854 pr_err("error while trying to restore original video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004855}
4856
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004857static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858{
4859 int status = 0;
4860 int i;
4861
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004862 switch (video_supported) {
4863 case TPACPI_VIDEO_570:
4864 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4865 TP_ACPI_VIDEO_570_PHSCMD))
4866 return -EIO;
4867 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4868 break;
4869 case TPACPI_VIDEO_770:
4870 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4871 return -EIO;
4872 if (i)
4873 status |= TP_ACPI_VIDEO_S_LCD;
4874 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4875 return -EIO;
4876 if (i)
4877 status |= TP_ACPI_VIDEO_S_CRT;
4878 break;
4879 case TPACPI_VIDEO_NEW:
4880 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4881 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4882 return -EIO;
4883 if (i)
4884 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004886 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4887 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4888 return -EIO;
4889 if (i)
4890 status |= TP_ACPI_VIDEO_S_LCD;
4891 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4892 return -EIO;
4893 if (i)
4894 status |= TP_ACPI_VIDEO_S_DVI;
4895 break;
4896 default:
4897 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899
4900 return status;
4901}
4902
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004903static int video_outputsw_set(int status)
4904{
4905 int autosw;
4906 int res = 0;
4907
4908 switch (video_supported) {
4909 case TPACPI_VIDEO_570:
4910 res = acpi_evalf(NULL, NULL,
4911 "\\_SB.PHS2", "vdd",
4912 TP_ACPI_VIDEO_570_PHS2CMD,
4913 status | TP_ACPI_VIDEO_570_PHS2SET);
4914 break;
4915 case TPACPI_VIDEO_770:
4916 autosw = video_autosw_get();
4917 if (autosw < 0)
4918 return autosw;
4919
4920 res = video_autosw_set(1);
4921 if (res)
4922 return res;
4923 res = acpi_evalf(vid_handle, NULL,
4924 "ASWT", "vdd", status * 0x100, 0);
4925 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004926 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004927 return -EIO;
4928 }
4929 break;
4930 case TPACPI_VIDEO_NEW:
4931 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004932 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004933 break;
4934 default:
4935 return -ENOSYS;
4936 }
4937
Jan van den Berg72a979f2014-09-17 00:01:08 +02004938 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004939}
4940
4941static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004943 int autosw = 0;
4944
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004945 switch (video_supported) {
4946 case TPACPI_VIDEO_570:
4947 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4948 return -EIO;
4949 break;
4950 case TPACPI_VIDEO_770:
4951 case TPACPI_VIDEO_NEW:
4952 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4953 return -EIO;
4954 break;
4955 default:
4956 return -ENOSYS;
4957 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004958
4959 return autosw & 1;
4960}
4961
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004962static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004963{
Jan van den Berg72a979f2014-09-17 00:01:08 +02004964 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004965 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004966 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004967}
4968
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004969static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004970{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004971 int autosw = video_autosw_get();
4972 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004973
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004974 if (autosw < 0)
4975 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004976
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004977 switch (video_supported) {
4978 case TPACPI_VIDEO_570:
4979 res = video_autosw_set(1);
4980 if (res)
4981 return res;
4982 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4983 break;
4984 case TPACPI_VIDEO_770:
4985 case TPACPI_VIDEO_NEW:
4986 res = video_autosw_set(1);
4987 if (res)
4988 return res;
4989 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4990 break;
4991 default:
4992 return -ENOSYS;
4993 }
4994 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004995 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004996 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004997 }
4998
Jan van den Berg72a979f2014-09-17 00:01:08 +02004999 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005000}
5001
5002static int video_expand_toggle(void)
5003{
5004 switch (video_supported) {
5005 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005006 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005007 0 : -EIO;
5008 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005009 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005010 0 : -EIO;
5011 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005012 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005013 0 : -EIO;
5014 default:
5015 return -ENOSYS;
5016 }
5017 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005018}
5019
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005020static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005021{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005022 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005023
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005024 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005025 seq_printf(m, "status:\t\tnot supported\n");
5026 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005027 }
5028
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005029 /* Even reads can crash X.org, so... */
5030 if (!capable(CAP_SYS_ADMIN))
5031 return -EPERM;
5032
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005033 status = video_outputsw_get();
5034 if (status < 0)
5035 return status;
5036
5037 autosw = video_autosw_get();
5038 if (autosw < 0)
5039 return autosw;
5040
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005041 seq_printf(m, "status:\t\tsupported\n");
5042 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5043 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005044 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005045 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5046 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5047 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5048 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005049 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005050 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5051 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5052 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005053
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005054 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005055}
5056
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005057static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058{
5059 char *cmd;
5060 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005061 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005063 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005064 return -ENODEV;
5065
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005066 /* Even reads can crash X.org, let alone writes... */
5067 if (!capable(CAP_SYS_ADMIN))
5068 return -EPERM;
5069
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005070 enable = 0;
5071 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
5073 while ((cmd = next_cmd(&buf))) {
5074 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005075 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005077 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005079 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005081 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005082 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005083 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005084 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005085 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005086 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005087 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005089 res = video_autosw_set(1);
5090 if (res)
5091 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005093 res = video_autosw_set(0);
5094 if (res)
5095 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005097 res = video_outputsw_cycle();
5098 if (res)
5099 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005101 res = video_expand_toggle();
5102 if (res)
5103 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 } else
5105 return -EINVAL;
5106 }
5107
5108 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005109 status = video_outputsw_get();
5110 if (status < 0)
5111 return status;
5112 res = video_outputsw_set((status & ~disable) | enable);
5113 if (res)
5114 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 }
5116
5117 return 0;
5118}
5119
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005120static struct ibm_struct video_driver_data = {
5121 .name = "video",
5122 .read = video_read,
5123 .write = video_write,
5124 .exit = video_exit,
5125};
5126
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005127#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5128
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005129/*************************************************************************
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005130 * Keyboard backlight subdriver
5131 */
5132
Hans de Goedec685e202017-02-09 16:44:13 +01005133static enum led_brightness kbdlight_brightness;
5134static DEFINE_MUTEX(kbdlight_mutex);
5135
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005136static int kbdlight_set_level(int level)
5137{
Hans de Goedec685e202017-02-09 16:44:13 +01005138 int ret = 0;
5139
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005140 if (!hkey_handle)
5141 return -ENXIO;
5142
Hans de Goedec685e202017-02-09 16:44:13 +01005143 mutex_lock(&kbdlight_mutex);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005144
Hans de Goedec685e202017-02-09 16:44:13 +01005145 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5146 ret = -EIO;
5147 else
5148 kbdlight_brightness = level;
5149
5150 mutex_unlock(&kbdlight_mutex);
5151
5152 return ret;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005153}
5154
5155static int kbdlight_get_level(void)
5156{
5157 int status = 0;
5158
5159 if (!hkey_handle)
5160 return -ENXIO;
5161
5162 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5163 return -EIO;
5164
5165 if (status < 0)
5166 return status;
5167
5168 return status & 0x3;
5169}
5170
5171static bool kbdlight_is_supported(void)
5172{
5173 int status = 0;
5174
5175 if (!hkey_handle)
5176 return false;
5177
5178 if (!acpi_has_method(hkey_handle, "MLCG")) {
5179 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5180 return false;
5181 }
5182
5183 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5184 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5185 return false;
5186 }
5187
5188 if (status < 0) {
5189 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5190 return false;
5191 }
5192
5193 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5194 /*
5195 * Guessed test for keyboard backlight:
5196 *
5197 * Machines with backlight keyboard return:
5198 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5199 * b110100010010000000XX - ThinkPad x230
5200 * b010100000010000000XX - ThinkPad x240
5201 * b010100000010000000XX - ThinkPad W541
5202 * (XX is current backlight level)
5203 *
5204 * Machines without backlight keyboard return:
5205 * b10100001000000000000 - ThinkPad x230
5206 * b10110001000000000000 - ThinkPad E430
5207 * b00000000000000000000 - ThinkPad E450
5208 *
5209 * Candidate BITs for detection test (XOR):
5210 * b01000000001000000000
5211 * ^
5212 */
5213 return status & BIT(9);
5214}
5215
Hans de Goede86ec0c22017-02-09 16:44:12 +01005216static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005217 enum led_brightness brightness)
5218{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005219 return kbdlight_set_level(brightness);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005220}
5221
5222static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5223{
5224 int level;
5225
5226 level = kbdlight_get_level();
5227 if (level < 0)
5228 return 0;
5229
5230 return level;
5231}
5232
5233static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5234 .led_classdev = {
5235 .name = "tpacpi::kbd_backlight",
5236 .max_brightness = 2,
Hans de Goedec685e202017-02-09 16:44:13 +01005237 .flags = LED_BRIGHT_HW_CHANGED,
Hans de Goede86ec0c22017-02-09 16:44:12 +01005238 .brightness_set_blocking = &kbdlight_sysfs_set,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005239 .brightness_get = &kbdlight_sysfs_get,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005240 }
5241};
5242
5243static int __init kbdlight_init(struct ibm_init_struct *iibm)
5244{
5245 int rc;
5246
5247 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5248
5249 TPACPI_ACPIHANDLE_INIT(hkey);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005250
5251 if (!kbdlight_is_supported()) {
5252 tp_features.kbdlight = 0;
5253 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5254 return 1;
5255 }
5256
Hans de Goedec685e202017-02-09 16:44:13 +01005257 kbdlight_brightness = kbdlight_sysfs_get(NULL);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005258 tp_features.kbdlight = 1;
5259
5260 rc = led_classdev_register(&tpacpi_pdev->dev,
5261 &tpacpi_led_kbdlight.led_classdev);
5262 if (rc < 0) {
5263 tp_features.kbdlight = 0;
5264 return rc;
5265 }
5266
Hans de Goedec685e202017-02-09 16:44:13 +01005267 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5268 TP_ACPI_HKEY_KBD_LIGHT_MASK);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005269 return 0;
5270}
5271
5272static void kbdlight_exit(void)
5273{
5274 if (tp_features.kbdlight)
5275 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005276}
5277
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005278static int kbdlight_set_level_and_update(int level)
5279{
5280 int ret;
5281 struct led_classdev *led_cdev;
5282
5283 ret = kbdlight_set_level(level);
5284 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5285
5286 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5287 led_cdev->brightness = level;
5288
5289 return ret;
5290}
5291
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005292static int kbdlight_read(struct seq_file *m)
5293{
5294 int level;
5295
5296 if (!tp_features.kbdlight) {
5297 seq_printf(m, "status:\t\tnot supported\n");
5298 } else {
5299 level = kbdlight_get_level();
5300 if (level < 0)
5301 seq_printf(m, "status:\t\terror %d\n", level);
5302 else
5303 seq_printf(m, "status:\t\t%d\n", level);
5304 seq_printf(m, "commands:\t0, 1, 2\n");
5305 }
5306
5307 return 0;
5308}
5309
5310static int kbdlight_write(char *buf)
5311{
5312 char *cmd;
5313 int level = -1;
5314
5315 if (!tp_features.kbdlight)
5316 return -ENODEV;
5317
5318 while ((cmd = next_cmd(&buf))) {
5319 if (strlencmp(cmd, "0") == 0)
5320 level = 0;
5321 else if (strlencmp(cmd, "1") == 0)
5322 level = 1;
5323 else if (strlencmp(cmd, "2") == 0)
5324 level = 2;
5325 else
5326 return -EINVAL;
5327 }
5328
5329 if (level == -1)
5330 return -EINVAL;
5331
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005332 return kbdlight_set_level_and_update(level);
5333}
5334
5335static void kbdlight_suspend(void)
5336{
5337 struct led_classdev *led_cdev;
5338
5339 if (!tp_features.kbdlight)
5340 return;
5341
5342 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5343 led_update_brightness(led_cdev);
5344 led_classdev_suspend(led_cdev);
5345}
5346
5347static void kbdlight_resume(void)
5348{
5349 if (!tp_features.kbdlight)
5350 return;
5351
5352 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005353}
5354
5355static struct ibm_struct kbdlight_driver_data = {
5356 .name = "kbdlight",
5357 .read = kbdlight_read,
5358 .write = kbdlight_write,
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005359 .suspend = kbdlight_suspend,
5360 .resume = kbdlight_resume,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005361 .exit = kbdlight_exit,
5362};
5363
5364/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005365 * Light (thinklight) subdriver
5366 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005368TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5369TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005370
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005371static int light_get_status(void)
5372{
5373 int status = 0;
5374
5375 if (tp_features.light_status) {
5376 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5377 return -EIO;
5378 return (!!status);
5379 }
5380
5381 return -ENXIO;
5382}
5383
5384static int light_set_status(int status)
5385{
5386 int rc;
5387
5388 if (tp_features.light) {
5389 if (cmos_handle) {
5390 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005391 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005392 TP_CMOS_THINKLIGHT_ON :
5393 TP_CMOS_THINKLIGHT_OFF);
5394 } else {
5395 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005396 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005397 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005398 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005399 }
5400
5401 return -ENXIO;
5402}
5403
Hans de Goede86ec0c22017-02-09 16:44:12 +01005404static int light_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005405 enum led_brightness brightness)
5406{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005407 return light_set_status((brightness != LED_OFF) ?
5408 TPACPI_LED_ON : TPACPI_LED_OFF);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005409}
5410
5411static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5412{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005413 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005414}
5415
5416static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5417 .led_classdev = {
5418 .name = "tpacpi::thinklight",
Hans de Goede86ec0c22017-02-09 16:44:12 +01005419 .brightness_set_blocking = &light_sysfs_set,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005420 .brightness_get = &light_sysfs_get,
5421 }
5422};
5423
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005424static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005426 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005427
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005428 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5429
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005430 if (tpacpi_is_ibm()) {
5431 TPACPI_ACPIHANDLE_INIT(ledb);
5432 TPACPI_ACPIHANDLE_INIT(lght);
5433 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005434 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005435
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005436 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005437 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005438
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005439 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005440 /* light status not supported on
5441 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005442 tp_features.light_status =
5443 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005445 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5446 str_supported(tp_features.light),
5447 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005448
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005449 if (!tp_features.light)
5450 return 1;
5451
5452 rc = led_classdev_register(&tpacpi_pdev->dev,
5453 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005454
5455 if (rc < 0) {
5456 tp_features.light = 0;
5457 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005458 } else {
5459 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005460 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005461
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005462 return rc;
5463}
5464
5465static void light_exit(void)
5466{
5467 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468}
5469
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005470static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005472 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005474 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005475 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005476 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005477 seq_printf(m, "status:\t\tunknown\n");
5478 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005479 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005480 status = light_get_status();
5481 if (status < 0)
5482 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005483 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5484 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488}
5489
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005490static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005493 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005494
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005495 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005496 return -ENODEV;
5497
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 while ((cmd = next_cmd(&buf))) {
5499 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005500 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005502 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 } else
5504 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 }
5506
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005507 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508}
5509
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005510static struct ibm_struct light_driver_data = {
5511 .name = "light",
5512 .read = light_read,
5513 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005514 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005515};
5516
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005517/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005518 * CMOS subdriver
5519 */
5520
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005521/* sysfs cmos_command -------------------------------------------------- */
5522static ssize_t cmos_command_store(struct device *dev,
5523 struct device_attribute *attr,
5524 const char *buf, size_t count)
5525{
5526 unsigned long cmos_cmd;
5527 int res;
5528
5529 if (parse_strtoul(buf, 21, &cmos_cmd))
5530 return -EINVAL;
5531
5532 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005533 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005534}
5535
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01005536static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005537
5538/* --------------------------------------------------------------------- */
5539
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005540static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005541{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005542 int res;
5543
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005544 vdbg_printk(TPACPI_DBG_INIT,
5545 "initializing cmos commands subdriver\n");
5546
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005547 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005548
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005549 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5550 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005551
5552 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5553 if (res)
5554 return res;
5555
Jan van den Berg72a979f2014-09-17 00:01:08 +02005556 return (cmos_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005557}
5558
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005559static void cmos_exit(void)
5560{
5561 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5562}
5563
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005564static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005566 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5567 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005569 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005571 seq_printf(m, "status:\t\tsupported\n");
5572 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 }
5574
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576}
5577
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005578static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579{
5580 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005581 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
5583 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005584 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5585 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 /* cmos_cmd set */
5587 } else
5588 return -EINVAL;
5589
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005590 res = issue_thinkpad_cmos_command(cmos_cmd);
5591 if (res)
5592 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 }
5594
5595 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005596}
5597
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005598static struct ibm_struct cmos_driver_data = {
5599 .name = "cmos",
5600 .read = cmos_read,
5601 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005602 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005603};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005604
5605/*************************************************************************
5606 * LED subdriver
5607 */
5608
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005609enum led_access_mode {
5610 TPACPI_LED_NONE = 0,
5611 TPACPI_LED_570, /* 570 */
5612 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5613 TPACPI_LED_NEW, /* all others */
5614};
5615
5616enum { /* For TPACPI_LED_OLD */
5617 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5618 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5619 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5620};
5621
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005622static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005623
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005624static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005625
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005626#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005627static struct tpacpi_led_classdev *tpacpi_leds;
5628static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005629static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005630 /* there's a limit of 19 chars + NULL before 2.6.26 */
5631 "tpacpi::power",
5632 "tpacpi:orange:batt",
5633 "tpacpi:green:batt",
5634 "tpacpi::dock_active",
5635 "tpacpi::bay_active",
5636 "tpacpi::dock_batt",
5637 "tpacpi::unknown_led",
5638 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005639 "tpacpi::dock_status1",
5640 "tpacpi::dock_status2",
5641 "tpacpi::unknown_led2",
5642 "tpacpi::unknown_led3",
5643 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005644};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005645#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005646
5647static inline bool tpacpi_is_led_restricted(const unsigned int led)
5648{
5649#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5650 return false;
5651#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005652 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005653#endif
5654}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005655
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005656static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005657{
5658 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005659 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005660
5661 switch (led_supported) {
5662 case TPACPI_LED_570:
5663 if (!acpi_evalf(ec_handle,
5664 &status, "GLED", "dd", 1 << led))
5665 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005666 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005667 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005668 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005669 TPACPI_LED_ON :
5670 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005671 tpacpi_led_state_cache[led] = led_s;
5672 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005673 default:
5674 return -ENXIO;
5675 }
5676
5677 /* not reached */
5678}
5679
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005680static int led_set_status(const unsigned int led,
5681 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005682{
5683 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005684 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5685 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005686
5687 int rc = 0;
5688
5689 switch (led_supported) {
5690 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005691 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005692 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005693 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005694 if (unlikely(tpacpi_is_led_restricted(led)))
5695 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005696 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5697 (1 << led), led_sled_arg1[ledstatus]))
5698 rc = -EIO;
5699 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005700 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005701 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005702 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005703 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005704 if (unlikely(tpacpi_is_led_restricted(led)))
5705 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005706 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5707 if (rc >= 0)
5708 rc = ec_write(TPACPI_LED_EC_HLBL,
5709 (ledstatus == TPACPI_LED_BLINK) << led);
5710 if (rc >= 0)
5711 rc = ec_write(TPACPI_LED_EC_HLCL,
5712 (ledstatus != TPACPI_LED_OFF) << led);
5713 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005714 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005715 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005716 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5717 return -EINVAL;
5718 if (unlikely(tpacpi_is_led_restricted(led)))
5719 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005720 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5721 led, led_led_arg1[ledstatus]))
5722 rc = -EIO;
5723 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005724 default:
5725 rc = -ENXIO;
5726 }
5727
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005728 if (!rc)
5729 tpacpi_led_state_cache[led] = ledstatus;
5730
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005731 return rc;
5732}
5733
Hans de Goede86ec0c22017-02-09 16:44:12 +01005734static int led_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005735 enum led_brightness brightness)
5736{
5737 struct tpacpi_led_classdev *data = container_of(led_cdev,
5738 struct tpacpi_led_classdev, led_classdev);
Hans de Goede86ec0c22017-02-09 16:44:12 +01005739 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005740
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005741 if (brightness == LED_OFF)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005742 new_state = TPACPI_LED_OFF;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005743 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005744 new_state = TPACPI_LED_ON;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005745 else
Hans de Goede86ec0c22017-02-09 16:44:12 +01005746 new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005747
Hans de Goede86ec0c22017-02-09 16:44:12 +01005748 return led_set_status(data->led, new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005749}
5750
5751static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5752 unsigned long *delay_on, unsigned long *delay_off)
5753{
5754 struct tpacpi_led_classdev *data = container_of(led_cdev,
5755 struct tpacpi_led_classdev, led_classdev);
5756
5757 /* Can we choose the flash rate? */
5758 if (*delay_on == 0 && *delay_off == 0) {
5759 /* yes. set them to the hardware blink rate (1 Hz) */
5760 *delay_on = 500; /* ms */
5761 *delay_off = 500; /* ms */
5762 } else if ((*delay_on != 500) || (*delay_off != 500))
5763 return -EINVAL;
5764
Hans de Goede86ec0c22017-02-09 16:44:12 +01005765 return led_set_status(data->led, TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005766}
5767
5768static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5769{
5770 int rc;
5771
5772 struct tpacpi_led_classdev *data = container_of(led_cdev,
5773 struct tpacpi_led_classdev, led_classdev);
5774
5775 rc = led_get_status(data->led);
5776
5777 if (rc == TPACPI_LED_OFF || rc < 0)
5778 rc = LED_OFF; /* no error handling in led class :( */
5779 else
5780 rc = LED_FULL;
5781
5782 return rc;
5783}
5784
5785static void led_exit(void)
5786{
5787 unsigned int i;
5788
5789 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5790 if (tpacpi_leds[i].led_classdev.name)
5791 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5792 }
5793
5794 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005795}
5796
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005797static int __init tpacpi_init_led(unsigned int led)
5798{
5799 int rc;
5800
5801 tpacpi_leds[led].led = led;
5802
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005803 /* LEDs with no name don't get registered */
5804 if (!tpacpi_led_names[led])
5805 return 0;
5806
Hans de Goede86ec0c22017-02-09 16:44:12 +01005807 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005808 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5809 if (led_supported == TPACPI_LED_570)
5810 tpacpi_leds[led].led_classdev.brightness_get =
5811 &led_sysfs_get;
5812
5813 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5814
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005815 rc = led_classdev_register(&tpacpi_pdev->dev,
5816 &tpacpi_leds[led].led_classdev);
5817 if (rc < 0)
5818 tpacpi_leds[led].led_classdev.name = NULL;
5819
5820 return rc;
5821}
5822
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005823static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5824 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5825 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5826 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5827
5828 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5829 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5830 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5831 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5832 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5833 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5834 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5835 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5836
5837 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5838 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5839 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5840 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5841 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5842
5843 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5844 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5845 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5846 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5847
5848 /* (1) - may have excess leds enabled on MSB */
5849
5850 /* Defaults (order matters, keep last, don't reorder!) */
5851 { /* Lenovo */
5852 .vendor = PCI_VENDOR_ID_LENOVO,
5853 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5854 .quirks = 0x1fffU,
5855 },
5856 { /* IBM ThinkPads with no EC version string */
5857 .vendor = PCI_VENDOR_ID_IBM,
5858 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5859 .quirks = 0x00ffU,
5860 },
5861 { /* IBM ThinkPads with EC version string */
5862 .vendor = PCI_VENDOR_ID_IBM,
5863 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5864 .quirks = 0x00bfU,
5865 },
5866};
5867
5868#undef TPACPI_LEDQ_IBM
5869#undef TPACPI_LEDQ_LNV
5870
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005871static enum led_access_mode __init led_init_detect_mode(void)
5872{
5873 acpi_status status;
5874
5875 if (tpacpi_is_ibm()) {
5876 /* 570 */
5877 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5878 if (ACPI_SUCCESS(status))
5879 return TPACPI_LED_570;
5880
5881 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5882 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5883 if (ACPI_SUCCESS(status))
5884 return TPACPI_LED_OLD;
5885 }
5886
5887 /* most others */
5888 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5889 if (ACPI_SUCCESS(status))
5890 return TPACPI_LED_NEW;
5891
5892 /* R30, R31, and unknown firmwares */
5893 led_handle = NULL;
5894 return TPACPI_LED_NONE;
5895}
5896
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005897static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005898{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005899 unsigned int i;
5900 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005901 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005902
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005903 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5904
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005905 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005906
Adam Leefcb44e12013-06-07 16:20:08 +08005907 if (led_supported != TPACPI_LED_NONE) {
5908 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5909 ARRAY_SIZE(led_useful_qtable));
5910
5911 if (!useful_leds) {
5912 led_handle = NULL;
5913 led_supported = TPACPI_LED_NONE;
5914 }
5915 }
5916
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005917 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5918 str_supported(led_supported), led_supported);
5919
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005920 if (led_supported == TPACPI_LED_NONE)
5921 return 1;
5922
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005923 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5924 GFP_KERNEL);
5925 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005926 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005927 return -ENOMEM;
5928 }
5929
5930 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08005931 tpacpi_leds[i].led = -1;
5932
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005933 if (!tpacpi_is_led_restricted(i) &&
5934 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005935 rc = tpacpi_init_led(i);
5936 if (rc < 0) {
5937 led_exit();
5938 return rc;
5939 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005940 }
5941 }
5942
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005943#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03005944 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005945#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005946 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005947}
5948
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005949#define str_led_status(s) \
5950 ((s) == TPACPI_LED_OFF ? "off" : \
5951 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005952
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005953static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005955 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005956 seq_printf(m, "status:\t\tnot supported\n");
5957 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005958 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005959 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005960
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005961 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005962 /* 570 */
5963 int i, status;
5964 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005965 status = led_get_status(i);
5966 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005967 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005968 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005969 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005970 }
5971 }
5972
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03005973 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005975 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976}
5977
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005978static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979{
5980 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005981 int led, rc;
5982 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005983
5984 if (!led_supported)
5985 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986
5987 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08005988 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 return -EINVAL;
5990
Adam Leeedf2d772013-06-08 16:51:15 +08005991 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5992 tpacpi_leds[led].led < 0)
5993 return -ENODEV;
5994
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005995 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005996 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005998 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005999 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006000 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006001 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006002 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006003 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006004
6005 rc = led_set_status(led, s);
6006 if (rc < 0)
6007 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 }
6009
6010 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006011}
6012
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006013static struct ibm_struct led_driver_data = {
6014 .name = "led",
6015 .read = led_read,
6016 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006017 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006018};
6019
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006020/*************************************************************************
6021 * Beep subdriver
6022 */
6023
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006024TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006025
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006026#define TPACPI_BEEP_Q1 0x0001
6027
6028static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6029 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6030 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6031};
6032
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006033static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006034{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006035 unsigned long quirks;
6036
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006037 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6038
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006039 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006040
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006041 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6042 str_supported(beep_handle != NULL));
6043
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006044 quirks = tpacpi_check_quirks(beep_quirk_table,
6045 ARRAY_SIZE(beep_quirk_table));
6046
6047 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6048
Jan van den Berg72a979f2014-09-17 00:01:08 +02006049 return (beep_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006050}
6051
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006052static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006054 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006055 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006056 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006057 seq_printf(m, "status:\t\tsupported\n");
6058 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062}
6063
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006064static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065{
6066 char *cmd;
6067 int beep_cmd;
6068
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006069 if (!beep_handle)
6070 return -ENODEV;
6071
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006073 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6074 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 /* beep_cmd set */
6076 } else
6077 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006078 if (tp_features.beep_needs_two_args) {
6079 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6080 beep_cmd, 0))
6081 return -EIO;
6082 } else {
6083 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6084 beep_cmd))
6085 return -EIO;
6086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 }
6088
6089 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006090}
6091
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006092static struct ibm_struct beep_driver_data = {
6093 .name = "beep",
6094 .read = beep_read,
6095 .write = beep_write,
6096};
6097
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006098/*************************************************************************
6099 * Thermal subdriver
6100 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006101
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006102enum thermal_access_mode {
6103 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6104 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6105 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6106 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6107 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6108};
6109
6110enum { /* TPACPI_THERMAL_TPEC_* */
6111 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6112 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6113 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006114
6115 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006116};
6117
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006118
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006119#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6120struct ibm_thermal_sensors_struct {
6121 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6122};
6123
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006124static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006125
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006126/* idx is zero-based */
6127static int thermal_get_sensor(int idx, s32 *value)
6128{
6129 int t;
6130 s8 tmp;
6131 char tmpi[5];
6132
6133 t = TP_EC_THERMAL_TMP0;
6134
6135 switch (thermal_read_mode) {
6136#if TPACPI_MAX_THERMAL_SENSORS >= 16
6137 case TPACPI_THERMAL_TPEC_16:
6138 if (idx >= 8 && idx <= 15) {
6139 t = TP_EC_THERMAL_TMP8;
6140 idx -= 8;
6141 }
6142 /* fallthrough */
6143#endif
6144 case TPACPI_THERMAL_TPEC_8:
6145 if (idx <= 7) {
6146 if (!acpi_ec_read(t + idx, &tmp))
6147 return -EIO;
6148 *value = tmp * 1000;
6149 return 0;
6150 }
6151 break;
6152
6153 case TPACPI_THERMAL_ACPI_UPDT:
6154 if (idx <= 7) {
6155 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6156 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6157 return -EIO;
6158 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6159 return -EIO;
6160 *value = (t - 2732) * 100;
6161 return 0;
6162 }
6163 break;
6164
6165 case TPACPI_THERMAL_ACPI_TMP07:
6166 if (idx <= 7) {
6167 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6168 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6169 return -EIO;
6170 if (t > 127 || t < -127)
6171 t = TP_EC_THERMAL_TMP_NA;
6172 *value = t * 1000;
6173 return 0;
6174 }
6175 break;
6176
6177 case TPACPI_THERMAL_NONE:
6178 default:
6179 return -ENOSYS;
6180 }
6181
6182 return -EINVAL;
6183}
6184
6185static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6186{
6187 int res, i;
6188 int n;
6189
6190 n = 8;
6191 i = 0;
6192
6193 if (!s)
6194 return -EINVAL;
6195
6196 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6197 n = 16;
6198
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006199 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006200 res = thermal_get_sensor(i, &s->temp[i]);
6201 if (res)
6202 return res;
6203 }
6204
6205 return n;
6206}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006207
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006208static void thermal_dump_all_sensors(void)
6209{
6210 int n, i;
6211 struct ibm_thermal_sensors_struct t;
6212
6213 n = thermal_get_sensors(&t);
6214 if (n <= 0)
6215 return;
6216
Joe Perches0978e012011-04-04 10:06:25 -07006217 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006218
6219 for (i = 0; i < n; i++) {
6220 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07006221 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006222 else
Joe Perches0978e012011-04-04 10:06:25 -07006223 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006224 }
6225
Joe Perches0978e012011-04-04 10:06:25 -07006226 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006227}
6228
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006229/* sysfs temp##_input -------------------------------------------------- */
6230
6231static ssize_t thermal_temp_input_show(struct device *dev,
6232 struct device_attribute *attr,
6233 char *buf)
6234{
6235 struct sensor_device_attribute *sensor_attr =
6236 to_sensor_dev_attr(attr);
6237 int idx = sensor_attr->index;
6238 s32 value;
6239 int res;
6240
6241 res = thermal_get_sensor(idx, &value);
6242 if (res)
6243 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006244 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006245 return -ENXIO;
6246
6247 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6248}
6249
6250#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006251 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6252 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006253
6254static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6255 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6256 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6257 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6258 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6259 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6260 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6261 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6262 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6263 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6264 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6265 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6266 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6267 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6268 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6269 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6270 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6271};
6272
6273#define THERMAL_ATTRS(X) \
6274 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6275
6276static struct attribute *thermal_temp_input_attr[] = {
6277 THERMAL_ATTRS(8),
6278 THERMAL_ATTRS(9),
6279 THERMAL_ATTRS(10),
6280 THERMAL_ATTRS(11),
6281 THERMAL_ATTRS(12),
6282 THERMAL_ATTRS(13),
6283 THERMAL_ATTRS(14),
6284 THERMAL_ATTRS(15),
6285 THERMAL_ATTRS(0),
6286 THERMAL_ATTRS(1),
6287 THERMAL_ATTRS(2),
6288 THERMAL_ATTRS(3),
6289 THERMAL_ATTRS(4),
6290 THERMAL_ATTRS(5),
6291 THERMAL_ATTRS(6),
6292 THERMAL_ATTRS(7),
6293 NULL
6294};
6295
6296static const struct attribute_group thermal_temp_input16_group = {
6297 .attrs = thermal_temp_input_attr
6298};
6299
6300static const struct attribute_group thermal_temp_input8_group = {
6301 .attrs = &thermal_temp_input_attr[8]
6302};
6303
6304#undef THERMAL_SENSOR_ATTR_TEMP
6305#undef THERMAL_ATTRS
6306
6307/* --------------------------------------------------------------------- */
6308
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006309static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006310{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006311 u8 t, ta1, ta2;
6312 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006313 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006314 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006315
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006316 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6317
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006318 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006319
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03006320 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006321 /*
6322 * Direct EC access mode: sensors at registers
6323 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6324 * non-implemented, thermal sensors return 0x80 when
6325 * not available
6326 */
6327
6328 ta1 = ta2 = 0;
6329 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006330 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006331 ta1 |= t;
6332 } else {
6333 ta1 = 0;
6334 break;
6335 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006336 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006337 ta2 |= t;
6338 } else {
6339 ta1 = 0;
6340 break;
6341 }
6342 }
6343 if (ta1 == 0) {
6344 /* This is sheer paranoia, but we handle it anyway */
6345 if (acpi_tmp7) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006346 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006347 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006348 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006349 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006350 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006351 }
6352 } else {
6353 thermal_read_mode =
6354 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006355 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006356 }
6357 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006358 if (tpacpi_is_ibm() &&
6359 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006360 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006361 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006362 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006363 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006364 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006365 }
6366 } else {
6367 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006368 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006369 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006370
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006371 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6372 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6373 thermal_read_mode);
6374
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006375 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006376 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006377 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006378 &thermal_temp_input16_group);
6379 if (res)
6380 return res;
6381 break;
6382 case TPACPI_THERMAL_TPEC_8:
6383 case TPACPI_THERMAL_ACPI_TMP07:
6384 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006385 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006386 &thermal_temp_input8_group);
6387 if (res)
6388 return res;
6389 break;
6390 case TPACPI_THERMAL_NONE:
6391 default:
6392 return 1;
6393 }
6394
6395 return 0;
6396}
6397
6398static void thermal_exit(void)
6399{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006400 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006401 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006402 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006403 &thermal_temp_input16_group);
6404 break;
6405 case TPACPI_THERMAL_TPEC_8:
6406 case TPACPI_THERMAL_ACPI_TMP07:
6407 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006408 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08006409 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006410 break;
6411 case TPACPI_THERMAL_NONE:
6412 default:
6413 break;
6414 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006415}
6416
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006417static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006418{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006419 int n, i;
6420 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006421
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006422 n = thermal_get_sensors(&t);
6423 if (unlikely(n < 0))
6424 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006425
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006426 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006427
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006428 if (n > 0) {
6429 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006430 seq_printf(m, "%d ", t.temp[i] / 1000);
6431 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006432 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006433 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006434
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006435 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006436}
6437
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006438static struct ibm_struct thermal_driver_data = {
6439 .name = "thermal",
6440 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006441 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006442};
6443
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006444/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006445 * Backlight/brightness subdriver
6446 */
Holger Macht8acb0252006-10-20 14:30:28 -07006447
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006448#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6449
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006450/*
6451 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6452 * CMOS NVRAM byte 0x5E, bits 0-3.
6453 *
6454 * EC HBRV (0x31) has the following layout
6455 * Bit 7: unknown function
6456 * Bit 6: unknown function
6457 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6458 * Bit 4: must be set to zero to avoid problems
6459 * Bit 3-0: backlight brightness level
6460 *
6461 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006462 *
6463 * WARNING: The X61 has been verified to use HBRV for something else, so
6464 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6465 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006466 */
6467
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006468enum {
6469 TP_EC_BACKLIGHT = 0x31,
6470
6471 /* TP_EC_BACKLIGHT bitmasks */
6472 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6473 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6474 TP_EC_BACKLIGHT_MAPSW = 0x20,
6475};
6476
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006477enum tpacpi_brightness_access_mode {
6478 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6479 TPACPI_BRGHT_MODE_EC, /* EC control */
6480 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6481 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6482 TPACPI_BRGHT_MODE_MAX
6483};
6484
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006485static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006486
6487static enum tpacpi_brightness_access_mode brightness_mode =
6488 TPACPI_BRGHT_MODE_MAX;
6489
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006490static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6491
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006492static struct mutex brightness_mutex;
6493
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006494/* NVRAM brightness access,
6495 * call with brightness_mutex held! */
6496static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006497{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006498 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006499
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006500 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6501 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6502 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006503 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006504
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006505 return lnvram;
6506}
6507
6508static void tpacpi_brightness_checkpoint_nvram(void)
6509{
6510 u8 lec = 0;
6511 u8 b_nvram;
6512
6513 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6514 return;
6515
6516 vdbg_printk(TPACPI_DBG_BRGHT,
6517 "trying to checkpoint backlight level to NVRAM...\n");
6518
6519 if (mutex_lock_killable(&brightness_mutex) < 0)
6520 return;
6521
6522 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6523 goto unlock;
6524 lec &= TP_EC_BACKLIGHT_LVLMSK;
6525 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6526
6527 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6528 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6529 /* NVRAM needs update */
6530 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6531 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6532 b_nvram |= lec;
6533 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6534 dbg_printk(TPACPI_DBG_BRGHT,
6535 "updated NVRAM backlight level to %u (0x%02x)\n",
6536 (unsigned int) lec, (unsigned int) b_nvram);
6537 } else
6538 vdbg_printk(TPACPI_DBG_BRGHT,
6539 "NVRAM backlight level already is %u (0x%02x)\n",
6540 (unsigned int) lec, (unsigned int) b_nvram);
6541
6542unlock:
6543 mutex_unlock(&brightness_mutex);
6544}
6545
6546
6547/* call with brightness_mutex held! */
6548static int tpacpi_brightness_get_raw(int *status)
6549{
6550 u8 lec = 0;
6551
6552 switch (brightness_mode) {
6553 case TPACPI_BRGHT_MODE_UCMS_STEP:
6554 *status = tpacpi_brightness_nvram_get();
6555 return 0;
6556 case TPACPI_BRGHT_MODE_EC:
6557 case TPACPI_BRGHT_MODE_ECNVRAM:
6558 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006559 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006560 *status = lec;
6561 return 0;
6562 default:
6563 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006564 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006565}
6566
6567/* call with brightness_mutex held! */
6568/* do NOT call with illegal backlight level value */
6569static int tpacpi_brightness_set_ec(unsigned int value)
6570{
6571 u8 lec = 0;
6572
6573 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6574 return -EIO;
6575
6576 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6577 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6578 (value & TP_EC_BACKLIGHT_LVLMSK))))
6579 return -EIO;
6580
6581 return 0;
6582}
6583
6584/* call with brightness_mutex held! */
6585static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6586{
6587 int cmos_cmd, inc;
6588 unsigned int current_value, i;
6589
6590 current_value = tpacpi_brightness_nvram_get();
6591
6592 if (value == current_value)
6593 return 0;
6594
6595 cmos_cmd = (value > current_value) ?
6596 TP_CMOS_BRIGHTNESS_UP :
6597 TP_CMOS_BRIGHTNESS_DOWN;
6598 inc = (value > current_value) ? 1 : -1;
6599
6600 for (i = current_value; i != value; i += inc)
6601 if (issue_thinkpad_cmos_command(cmos_cmd))
6602 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006603
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006604 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006605}
6606
6607/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006608static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006609{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006610 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006611
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006612 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006613 return -EINVAL;
6614
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006615 vdbg_printk(TPACPI_DBG_BRGHT,
6616 "set backlight level to %d\n", value);
6617
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006618 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006619 if (res < 0)
6620 return res;
6621
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006622 switch (brightness_mode) {
6623 case TPACPI_BRGHT_MODE_EC:
6624 case TPACPI_BRGHT_MODE_ECNVRAM:
6625 res = tpacpi_brightness_set_ec(value);
6626 break;
6627 case TPACPI_BRGHT_MODE_UCMS_STEP:
6628 res = tpacpi_brightness_set_ucmsstep(value);
6629 break;
6630 default:
6631 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006632 }
6633
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006634 mutex_unlock(&brightness_mutex);
6635 return res;
6636}
6637
6638/* sysfs backlight class ----------------------------------------------- */
6639
6640static int brightness_update_status(struct backlight_device *bd)
6641{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006642 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006643 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6644 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006645 bd->props.brightness : 0;
6646
6647 dbg_printk(TPACPI_DBG_BRGHT,
6648 "backlight: attempt to set level to %d\n",
6649 level);
6650
6651 /* it is the backlight class's job (caller) to handle
6652 * EINTR and other errors properly */
6653 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006654}
Holger Macht8acb0252006-10-20 14:30:28 -07006655
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006656static int brightness_get(struct backlight_device *bd)
6657{
6658 int status, res;
6659
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006660 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006661 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006662 return 0;
6663
6664 res = tpacpi_brightness_get_raw(&status);
6665
6666 mutex_unlock(&brightness_mutex);
6667
6668 if (res < 0)
6669 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006670
6671 return status & TP_EC_BACKLIGHT_LVLMSK;
6672}
6673
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006674static void tpacpi_brightness_notify_change(void)
6675{
6676 backlight_force_update(ibm_backlight_device,
6677 BACKLIGHT_UPDATE_HOTKEY);
6678}
6679
Lionel Debrouxacc24722010-11-16 14:14:02 +01006680static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006681 .get_brightness = brightness_get,
6682 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006683};
6684
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006685/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006686
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006687/*
6688 * Call _BCL method of video device. On some ThinkPads this will
6689 * switch the firmware to the ACPI brightness control mode.
6690 */
6691
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006692static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6693{
6694 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6695 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006696 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006697 int rc;
6698
Aaron Lu46445b62013-10-11 21:27:46 +08006699 if (acpi_bus_get_device(handle, &device))
6700 return 0;
6701
6702 rc = 0;
6703 list_for_each_entry(child, &device->children, node) {
6704 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6705 NULL, &buffer);
6706 if (ACPI_FAILURE(status))
6707 continue;
6708
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006709 obj = (union acpi_object *)buffer.pointer;
6710 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006711 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006712 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006713 rc = 0;
6714 } else {
6715 rc = obj->package.count;
6716 }
Aaron Lu46445b62013-10-11 21:27:46 +08006717 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006718 }
6719
6720 kfree(buffer.pointer);
6721 return rc;
6722}
6723
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006724
6725/*
6726 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6727 */
6728static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6729{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006730 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006731 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006732
Aaron Lu46445b62013-10-11 21:27:46 +08006733 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006734 if (video_device)
6735 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006736
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006737 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006738
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006739 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006740}
6741
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006742/*
6743 * These are only useful for models that have only one possibility
6744 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6745 * these quirks.
6746 */
6747#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6748#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6749#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6750
6751static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6752 /* Models with ATI GPUs known to require ECNVRAM mode */
6753 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6754
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006755 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006756 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006757 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006758 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006759 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6760
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006761 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006762 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006763 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6764 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006765
6766 /* Models with Intel GMA900 */
6767 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6768 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6769 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6770};
6771
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006772/*
6773 * Returns < 0 for error, otherwise sets tp_features.bright_*
6774 * and bright_maxlvl.
6775 */
6776static void __init tpacpi_detect_brightness_capabilities(void)
6777{
6778 unsigned int b;
6779
6780 vdbg_printk(TPACPI_DBG_INIT,
6781 "detecting firmware brightness interface capabilities\n");
6782
6783 /* we could run a quirks check here (same table used by
6784 * brightness_init) if needed */
6785
6786 /*
6787 * We always attempt to detect acpi support, so as to switch
6788 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6789 * going to publish a backlight interface
6790 */
6791 b = tpacpi_check_std_acpi_brightness_support();
6792 switch (b) {
6793 case 16:
6794 bright_maxlvl = 15;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006795 break;
6796 case 8:
6797 case 0:
6798 bright_maxlvl = 7;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006799 break;
6800 default:
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006801 tp_features.bright_unkfw = 1;
6802 bright_maxlvl = b - 1;
6803 }
Eric Curtin15c75622016-01-30 16:55:59 +00006804 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006805}
6806
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006807static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006808{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006809 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006810 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006811 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006812
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006813 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6814
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006815 mutex_init(&brightness_mutex);
6816
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006817 quirks = tpacpi_check_quirks(brightness_quirk_table,
6818 ARRAY_SIZE(brightness_quirk_table));
6819
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006820 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006821
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006822 /* if it is unknown, we don't handle it: it wouldn't be safe */
6823 if (tp_features.bright_unkfw)
6824 return 1;
6825
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006826 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006827 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006828 "brightness support disabled by module parameter\n");
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006829 return 1;
6830 }
6831
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02006832 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006833 if (brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006834 pr_info("Standard ACPI backlight interface available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006835 return 1;
6836 } else if (brightness_enable == 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006837 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 -03006838 return 1;
6839 }
6840 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006841 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006842 }
6843
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006844 /*
6845 * Check for module parameter bogosity, note that we
6846 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6847 * able to detect "unspecified"
6848 */
6849 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006850 return -EINVAL;
6851
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006852 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6853 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6854 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006855 if (quirks & TPACPI_BRGHT_Q_EC)
6856 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6857 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006858 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6859
6860 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006861 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006862 brightness_mode);
6863 }
6864
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006865 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006866 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006867 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6868 brightness_mode == TPACPI_BRGHT_MODE_EC))
6869 return -EINVAL;
6870
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006871 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006872 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006873
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006874 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006875 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006876 props.max_brightness = bright_maxlvl;
6877 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006878 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6879 NULL, NULL,
6880 &ibm_backlight_data,
6881 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006882 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006883 int rc = PTR_ERR(ibm_backlight_device);
6884 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006885 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006886 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006887 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006888 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6889 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006890
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006891 if (quirks & TPACPI_BRGHT_Q_ASK) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006892 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
6893 brightness_mode);
6894 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
6895 TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006896 }
6897
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006898 /* Added by mistake in early 2007. Probably useless, but it could
6899 * be working around some unknown firmware problem where the value
6900 * read at startup doesn't match the real hardware state... so leave
6901 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006902 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006903
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006904 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03006905 "brightness: registering brightness hotkeys as change notification\n");
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006906 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6907 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006908 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006909 return 0;
6910}
6911
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006912static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006913{
6914 tpacpi_brightness_checkpoint_nvram();
6915}
6916
6917static void brightness_shutdown(void)
6918{
6919 tpacpi_brightness_checkpoint_nvram();
6920}
6921
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006922static void brightness_exit(void)
6923{
6924 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006925 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006926 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006927 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006928 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006929
6930 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006931}
6932
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006933static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006934{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006935 int level;
6936
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006937 level = brightness_get(NULL);
6938 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006939 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006940 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006941 seq_printf(m, "level:\t\t%d\n", level);
6942 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006943 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6944 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006945 }
6946
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006947 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006948}
6949
6950static int brightness_write(char *buf)
6951{
6952 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006953 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006954 char *cmd;
6955
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006956 level = brightness_get(NULL);
6957 if (level < 0)
6958 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006959
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006960 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006961 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006962 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006963 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006964 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006965 if (level > 0)
6966 level--;
6967 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006968 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006969 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006970 } else
6971 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006972 }
6973
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006974 tpacpi_disclose_usertask("procfs brightness",
6975 "set level to %d\n", level);
6976
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006977 /*
6978 * Now we know what the final level should be, so we try to set it.
6979 * Doing it this way makes the syscall restartable in case of EINTR
6980 */
6981 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006982 if (!rc && ibm_backlight_device)
6983 backlight_force_update(ibm_backlight_device,
6984 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02006985 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006986}
6987
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006988static struct ibm_struct brightness_driver_data = {
6989 .name = "brightness",
6990 .read = brightness_read,
6991 .write = brightness_write,
6992 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006993 .suspend = brightness_suspend,
6994 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006995};
6996
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006997/*************************************************************************
6998 * Volume subdriver
6999 */
7000
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007001/*
7002 * IBM ThinkPads have a simple volume controller with MUTE gating.
7003 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7004 *
7005 * Since the *61 series (and probably also the later *60 series), Lenovo
7006 * ThinkPads only implement the MUTE gate.
7007 *
7008 * EC register 0x30
7009 * Bit 6: MUTE (1 mutes sound)
7010 * Bit 3-0: Volume
7011 * Other bits should be zero as far as we know.
7012 *
7013 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7014 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7015 * such as bit 7 which is used to detect repeated presses of MUTE,
7016 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007017 *
7018 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7019 * in response to user input. Unfortunately, this rarely works well.
7020 * The laptop changes the state of its internal MUTE gate and, on some
7021 * models, sends KEY_MUTE, causing any user code that responds to the
7022 * mute button to get confused. The hardware MUTE gate is also
7023 * unnecessary, since user code can handle the mute button without
7024 * kernel or EC help.
7025 *
7026 * To avoid confusing userspace, we simply disable all EC-based mute
7027 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007028 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007029
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007030#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7031
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007032#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7033#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7034#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7035
Takashi Iwaicab66612013-10-24 16:06:32 +02007036#if SNDRV_CARDS <= 32
7037#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7038#else
7039#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7040#endif
7041static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007042static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307043static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007044
7045struct tpacpi_alsa_data {
7046 struct snd_card *card;
7047 struct snd_ctl_elem_id *ctl_mute_id;
7048 struct snd_ctl_elem_id *ctl_vol_id;
7049};
7050
7051static struct snd_card *alsa_card;
7052
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007053enum {
7054 TP_EC_AUDIO = 0x30,
7055
7056 /* TP_EC_AUDIO bits */
7057 TP_EC_AUDIO_MUTESW = 6,
7058
7059 /* TP_EC_AUDIO bitmasks */
7060 TP_EC_AUDIO_LVL_MSK = 0x0F,
7061 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7062
7063 /* Maximum volume */
7064 TP_EC_VOLUME_MAX = 14,
7065};
7066
7067enum tpacpi_volume_access_mode {
7068 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7069 TPACPI_VOL_MODE_EC, /* Pure EC control */
7070 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7071 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7072 TPACPI_VOL_MODE_MAX
7073};
7074
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007075enum tpacpi_volume_capabilities {
7076 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7077 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7078 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7079 TPACPI_VOL_CAP_MAX
7080};
7081
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007082enum tpacpi_mute_btn_mode {
7083 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7084 /* We don't know what mode 1 is. */
7085 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7086 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7087};
7088
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007089static enum tpacpi_volume_access_mode volume_mode =
7090 TPACPI_VOL_MODE_MAX;
7091
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007092static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307093static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007094static bool software_mute_requested = true;
7095static bool software_mute_active;
7096static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007097
7098/*
7099 * Used to syncronize writers to TP_EC_AUDIO and
7100 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7101 */
7102static struct mutex volume_mutex;
7103
7104static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007105{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007106 u8 lec = 0;
7107 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007108 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007109
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007110 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7111 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007112 if (!volume_control_allowed)
7113 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007114 if (software_mute_active)
7115 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007116
7117 vdbg_printk(TPACPI_DBG_MIXER,
7118 "trying to checkpoint mixer state to NVRAM...\n");
7119
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007120 if (tp_features.mixer_no_level_control)
7121 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7122 else
7123 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7124
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007125 if (mutex_lock_killable(&volume_mutex) < 0)
7126 return;
7127
7128 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7129 goto unlock;
7130 lec &= ec_mask;
7131 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7132
7133 if (lec != (b_nvram & ec_mask)) {
7134 /* NVRAM needs update */
7135 b_nvram &= ~ec_mask;
7136 b_nvram |= lec;
7137 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7138 dbg_printk(TPACPI_DBG_MIXER,
7139 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7140 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007141 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007142 vdbg_printk(TPACPI_DBG_MIXER,
7143 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7144 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007145 }
7146
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007147unlock:
7148 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007149}
7150
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007151static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007152{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007153 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007154
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007155 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7156 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007157
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007158 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007159
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007160 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007161
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007162 return 0;
7163}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007164
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007165static int volume_get_status(u8 *status)
7166{
7167 return volume_get_status_ec(status);
7168}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007169
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007170static int volume_set_status_ec(const u8 status)
7171{
7172 if (!acpi_ec_write(TP_EC_AUDIO, status))
7173 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007174
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007175 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7176
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007177 /*
7178 * On X200s, and possibly on others, it can take a while for
7179 * reads to become correct.
7180 */
7181 msleep(1);
7182
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007183 return 0;
7184}
7185
7186static int volume_set_status(const u8 status)
7187{
7188 return volume_set_status_ec(status);
7189}
7190
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007191/* returns < 0 on error, 0 on no change, 1 on change */
7192static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007193{
7194 int rc;
7195 u8 s, n;
7196
7197 if (mutex_lock_killable(&volume_mutex) < 0)
7198 return -EINTR;
7199
7200 rc = volume_get_status_ec(&s);
7201 if (rc)
7202 goto unlock;
7203
7204 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7205 s & ~TP_EC_AUDIO_MUTESW_MSK;
7206
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007207 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007208 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007209 if (!rc)
7210 rc = 1;
7211 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007212
7213unlock:
7214 mutex_unlock(&volume_mutex);
7215 return rc;
7216}
7217
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007218static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007219{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007220 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007221 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007222 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007223}
7224
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007225static int volume_set_mute(const bool mute)
7226{
7227 int rc;
7228
7229 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7230 (mute) ? "" : "un");
7231
7232 rc = __volume_set_mute_ec(mute);
7233 return (rc < 0) ? rc : 0;
7234}
7235
7236/* returns < 0 on error, 0 on no change, 1 on change */
7237static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007238{
7239 int rc;
7240 u8 s, n;
7241
7242 if (vol > TP_EC_VOLUME_MAX)
7243 return -EINVAL;
7244
7245 if (mutex_lock_killable(&volume_mutex) < 0)
7246 return -EINTR;
7247
7248 rc = volume_get_status_ec(&s);
7249 if (rc)
7250 goto unlock;
7251
7252 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7253
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007254 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007255 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007256 if (!rc)
7257 rc = 1;
7258 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007259
7260unlock:
7261 mutex_unlock(&volume_mutex);
7262 return rc;
7263}
7264
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007265static int volume_set_software_mute(bool startup)
7266{
7267 int result;
7268
7269 if (!tpacpi_is_lenovo())
7270 return -ENODEV;
7271
7272 if (startup) {
7273 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7274 "HAUM", "qd"))
7275 return -EIO;
7276
7277 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7278 "Initial HAUM setting was %d\n",
7279 software_mute_orig_mode);
7280 }
7281
7282 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7283 (int)TP_EC_MUTE_BTN_NONE))
7284 return -EIO;
7285
7286 if (result != TP_EC_MUTE_BTN_NONE)
7287 pr_warn("Unexpected SAUM result %d\n",
7288 result);
7289
7290 /*
7291 * In software mute mode, the standard codec controls take
7292 * precendence, so we unmute the ThinkPad HW switch at
7293 * startup. Just on case there are SAUM-capable ThinkPads
7294 * with level controls, set max HW volume as well.
7295 */
7296 if (tp_features.mixer_no_level_control)
7297 result = volume_set_mute(false);
7298 else
7299 result = volume_set_status(TP_EC_VOLUME_MAX);
7300
7301 if (result != 0)
7302 pr_warn("Failed to unmute the HW mute switch\n");
7303
7304 return 0;
7305}
7306
7307static void volume_exit_software_mute(void)
7308{
7309 int r;
7310
7311 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7312 || r != software_mute_orig_mode)
7313 pr_warn("Failed to restore mute mode\n");
7314}
7315
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007316static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007317{
7318 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007319 "ALSA: trying to set volume level to %hu\n", vol);
7320 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007321}
7322
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007323static void volume_alsa_notify_change(void)
7324{
7325 struct tpacpi_alsa_data *d;
7326
7327 if (alsa_card && alsa_card->private_data) {
7328 d = alsa_card->private_data;
7329 if (d->ctl_mute_id)
7330 snd_ctl_notify(alsa_card,
7331 SNDRV_CTL_EVENT_MASK_VALUE,
7332 d->ctl_mute_id);
7333 if (d->ctl_vol_id)
7334 snd_ctl_notify(alsa_card,
7335 SNDRV_CTL_EVENT_MASK_VALUE,
7336 d->ctl_vol_id);
7337 }
7338}
7339
7340static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7341 struct snd_ctl_elem_info *uinfo)
7342{
7343 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7344 uinfo->count = 1;
7345 uinfo->value.integer.min = 0;
7346 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7347 return 0;
7348}
7349
7350static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7351 struct snd_ctl_elem_value *ucontrol)
7352{
7353 u8 s;
7354 int rc;
7355
7356 rc = volume_get_status(&s);
7357 if (rc < 0)
7358 return rc;
7359
7360 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7361 return 0;
7362}
7363
7364static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7365 struct snd_ctl_elem_value *ucontrol)
7366{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007367 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7368 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007369 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007370}
7371
7372#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7373
7374static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7375 struct snd_ctl_elem_value *ucontrol)
7376{
7377 u8 s;
7378 int rc;
7379
7380 rc = volume_get_status(&s);
7381 if (rc < 0)
7382 return rc;
7383
7384 ucontrol->value.integer.value[0] =
7385 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7386 return 0;
7387}
7388
7389static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7390 struct snd_ctl_elem_value *ucontrol)
7391{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007392 tpacpi_disclose_usertask("ALSA", "%smute\n",
7393 ucontrol->value.integer.value[0] ?
7394 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007395 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007396}
7397
Mathias Krausec3aa4722014-07-16 19:43:17 +02007398static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007399 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7400 .name = "Console Playback Volume",
7401 .index = 0,
7402 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7403 .info = volume_alsa_vol_info,
7404 .get = volume_alsa_vol_get,
7405};
7406
Mathias Krausec3aa4722014-07-16 19:43:17 +02007407static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007408 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7409 .name = "Console Playback Switch",
7410 .index = 0,
7411 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7412 .info = volume_alsa_mute_info,
7413 .get = volume_alsa_mute_get,
7414};
7415
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007416static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007417{
7418 tpacpi_volume_checkpoint_nvram();
7419}
7420
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007421static void volume_resume(void)
7422{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007423 if (software_mute_active) {
7424 if (volume_set_software_mute(false) < 0)
7425 pr_warn("Failed to restore software mute\n");
7426 } else {
7427 volume_alsa_notify_change();
7428 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007429}
7430
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007431static void volume_shutdown(void)
7432{
7433 tpacpi_volume_checkpoint_nvram();
7434}
7435
7436static void volume_exit(void)
7437{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007438 if (alsa_card) {
7439 snd_card_free(alsa_card);
7440 alsa_card = NULL;
7441 }
7442
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007443 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007444
7445 if (software_mute_active)
7446 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007447}
7448
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007449static int __init volume_create_alsa_mixer(void)
7450{
7451 struct snd_card *card;
7452 struct tpacpi_alsa_data *data;
7453 struct snd_kcontrol *ctl_vol;
7454 struct snd_kcontrol *ctl_mute;
7455 int rc;
7456
Takashi Iwai89235e52014-01-29 15:01:27 +01007457 rc = snd_card_new(&tpacpi_pdev->dev,
7458 alsa_index, alsa_id, THIS_MODULE,
7459 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007460 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007461 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007462 return 1;
7463 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007464
7465 BUG_ON(!card->private_data);
7466 data = card->private_data;
7467 data->card = card;
7468
7469 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7470 sizeof(card->driver));
7471 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7472 sizeof(card->shortname));
7473 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7474 (thinkpad_id.ec_version_str) ?
7475 thinkpad_id.ec_version_str : "(unknown)");
7476 snprintf(card->longname, sizeof(card->longname),
7477 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7478 (thinkpad_id.ec_version_str) ?
7479 thinkpad_id.ec_version_str : "unknown");
7480
7481 if (volume_control_allowed) {
7482 volume_alsa_control_vol.put = volume_alsa_vol_put;
7483 volume_alsa_control_vol.access =
7484 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7485
7486 volume_alsa_control_mute.put = volume_alsa_mute_put;
7487 volume_alsa_control_mute.access =
7488 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7489 }
7490
7491 if (!tp_features.mixer_no_level_control) {
7492 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7493 rc = snd_ctl_add(card, ctl_vol);
7494 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007495 pr_err("Failed to create ALSA volume control: %d\n",
7496 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007497 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007498 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007499 data->ctl_vol_id = &ctl_vol->id;
7500 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007501
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007502 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7503 rc = snd_ctl_add(card, ctl_mute);
7504 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007505 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007506 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007507 }
7508 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007509
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007510 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007511 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007512 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007513 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007514 }
7515
7516 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007517 return 0;
7518
7519err_exit:
7520 snd_card_free(card);
7521 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007522}
7523
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007524#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7525#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7526
7527static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7528 /* Whitelist volume level on all IBM by default */
7529 { .vendor = PCI_VENDOR_ID_IBM,
7530 .bios = TPACPI_MATCH_ANY,
7531 .ec = TPACPI_MATCH_ANY,
7532 .quirks = TPACPI_VOL_Q_LEVEL },
7533
7534 /* Lenovo models with volume control (needs confirmation) */
7535 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7536 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7537 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7538 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7539 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7540 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7541 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7542
7543 /* Whitelist mute-only on all Lenovo by default */
7544 { .vendor = PCI_VENDOR_ID_LENOVO,
7545 .bios = TPACPI_MATCH_ANY,
7546 .ec = TPACPI_MATCH_ANY,
7547 .quirks = TPACPI_VOL_Q_MUTEONLY }
7548};
7549
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007550static int __init volume_init(struct ibm_init_struct *iibm)
7551{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007552 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007553 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007554
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007555 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7556
7557 mutex_init(&volume_mutex);
7558
7559 /*
7560 * Check for module parameter bogosity, note that we
7561 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7562 * able to detect "unspecified"
7563 */
7564 if (volume_mode > TPACPI_VOL_MODE_MAX)
7565 return -EINVAL;
7566
7567 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007568 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7569 TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007570 return 1;
7571 }
7572
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007573 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7574 return -EINVAL;
7575
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007576 /*
7577 * The ALSA mixer is our primary interface.
7578 * When disabled, don't install the subdriver at all
7579 */
7580 if (!alsa_enable) {
7581 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007582 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007583 return 1;
7584 }
7585
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007586 quirks = tpacpi_check_quirks(volume_quirk_table,
7587 ARRAY_SIZE(volume_quirk_table));
7588
7589 switch (volume_capabilities) {
7590 case TPACPI_VOL_CAP_AUTO:
7591 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7592 tp_features.mixer_no_level_control = 1;
7593 else if (quirks & TPACPI_VOL_Q_LEVEL)
7594 tp_features.mixer_no_level_control = 0;
7595 else
7596 return 1; /* no mixer */
7597 break;
7598 case TPACPI_VOL_CAP_VOLMUTE:
7599 tp_features.mixer_no_level_control = 0;
7600 break;
7601 case TPACPI_VOL_CAP_MUTEONLY:
7602 tp_features.mixer_no_level_control = 1;
7603 break;
7604 default:
7605 return 1;
7606 }
7607
7608 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7609 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7610 "using user-supplied volume_capabilities=%d\n",
7611 volume_capabilities);
7612
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007613 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7614 volume_mode == TPACPI_VOL_MODE_MAX) {
7615 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7616
7617 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7618 "driver auto-selected volume_mode=%d\n",
7619 volume_mode);
7620 } else {
7621 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7622 "using user-supplied volume_mode=%d\n",
7623 volume_mode);
7624 }
7625
7626 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007627 "mute is supported, volume control is %s\n",
7628 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007629
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007630 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7631 software_mute_active = true;
7632 } else {
7633 rc = volume_create_alsa_mixer();
7634 if (rc) {
7635 pr_err("Could not create the ALSA mixer interface\n");
7636 return rc;
7637 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007638
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007639 pr_info("Console audio control enabled, mode: %s\n",
7640 (volume_control_allowed) ?
7641 "override (read/write)" :
7642 "monitor (read only)");
7643 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007644
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007645 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7646 "registering volume hotkeys as change notification\n");
7647 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7648 | TP_ACPI_HKEY_VOLUP_MASK
7649 | TP_ACPI_HKEY_VOLDWN_MASK
7650 | TP_ACPI_HKEY_MUTE_MASK);
7651
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007652 return 0;
7653}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007654
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007655static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007656{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007657 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007658
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007659 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007660 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007661 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007662 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007663 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007664 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007665 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007666 status & TP_EC_AUDIO_LVL_MSK);
7667
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007668 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007669 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007670
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007671 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007672 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007673 if (!tp_features.mixer_no_level_control) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007674 seq_printf(m, "commands:\tup, down\n");
7675 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7676 TP_EC_VOLUME_MAX);
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007677 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007678 }
7679 }
7680
7681 return 0;
7682}
7683
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007684static int volume_write(char *buf)
7685{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007686 u8 s;
7687 u8 new_level, new_mute;
7688 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007689 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007690 int rc;
7691
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007692 /*
7693 * We do allow volume control at driver startup, so that the
7694 * user can set initial state through the volume=... parameter hack.
7695 */
7696 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7697 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7698 tp_warned.volume_ctrl_forbidden = 1;
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007699 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7700 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007701 }
7702 return -EPERM;
7703 }
7704
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007705 rc = volume_get_status(&s);
7706 if (rc < 0)
7707 return rc;
7708
7709 new_level = s & TP_EC_AUDIO_LVL_MSK;
7710 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007711
7712 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007713 if (!tp_features.mixer_no_level_control) {
7714 if (strlencmp(cmd, "up") == 0) {
7715 if (new_mute)
7716 new_mute = 0;
7717 else if (new_level < TP_EC_VOLUME_MAX)
7718 new_level++;
7719 continue;
7720 } else if (strlencmp(cmd, "down") == 0) {
7721 if (new_mute)
7722 new_mute = 0;
7723 else if (new_level > 0)
7724 new_level--;
7725 continue;
7726 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7727 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7728 new_level = l;
7729 continue;
7730 }
7731 }
7732 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007733 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007734 else if (strlencmp(cmd, "unmute") == 0)
7735 new_mute = 0;
7736 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007737 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007738 }
7739
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007740 if (tp_features.mixer_no_level_control) {
7741 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7742 new_mute ? "" : "un");
7743 rc = volume_set_mute(!!new_mute);
7744 } else {
7745 tpacpi_disclose_usertask("procfs volume",
7746 "%smute and set level to %d\n",
7747 new_mute ? "" : "un", new_level);
7748 rc = volume_set_status(new_mute | new_level);
7749 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007750 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007751
7752 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007753}
7754
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007755static struct ibm_struct volume_driver_data = {
7756 .name = "volume",
7757 .read = volume_read,
7758 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007759 .exit = volume_exit,
7760 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007761 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007762 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007763};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007764
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007765#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7766
7767#define alsa_card NULL
7768
Tobias Klauserbb55a2e2016-11-25 14:18:07 +01007769static inline void volume_alsa_notify_change(void)
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007770{
7771}
7772
7773static int __init volume_init(struct ibm_init_struct *iibm)
7774{
Joe Perches0978e012011-04-04 10:06:25 -07007775 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007776
7777 return 1;
7778}
7779
7780static struct ibm_struct volume_driver_data = {
7781 .name = "volume",
7782};
7783
7784#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7785
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007786/*************************************************************************
7787 * Fan subdriver
7788 */
7789
7790/*
7791 * FAN ACCESS MODES
7792 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007793 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007794 * ACPI GFAN method: returns fan level
7795 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007796 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007797 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007798 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007799 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007800 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7801 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007802 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7803 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007804 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007805 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007806 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7807 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007808 *
7809 * Fan speed changes of any sort (including those caused by the
7810 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007811 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007812 * limited.
7813 *
7814 * Reading is not available if GFAN exists.
7815 * Writing is not available if SFAN exists.
7816 *
7817 * Bits
7818 * 7 automatic mode engaged;
7819 * (default operation mode of the ThinkPad)
7820 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007821 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007822 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007823 * the tachometer while the fan controller ramps up
7824 * the speed (which can take up to a few *minutes*).
7825 * Speeds up fan to 100% duty-cycle, which is far above
7826 * the standard RPM levels. It is not impossible that
7827 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007828 * 5-3 unused in some models. Extra bits for fan level
7829 * in others, but still useless as all values above
7830 * 7 map to the same speed as level 7 in these models.
7831 * 2-0 fan level (0..7 usually)
7832 * 0x00 = stop
7833 * 0x07 = max (set when temperatures critical)
7834 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007835 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007836 *
7837 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007838 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007839 * does so, its initial value is meaningless (0x07).
7840 *
7841 * For firmware bugs, refer to:
7842 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7843 *
7844 * ----
7845 *
7846 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7847 * Main fan tachometer reading (in RPM)
7848 *
7849 * This register is present on all ThinkPads with a new-style EC, and
7850 * it is known not to be present on the A21m/e, and T22, as there is
7851 * something else in offset 0x84 according to the ACPI DSDT. Other
7852 * ThinkPads from this same time period (and earlier) probably lack the
7853 * tachometer as well.
7854 *
Nick Andrew877d0312009-01-26 11:06:57 +01007855 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007856 * was never fixed by IBM to report the EC firmware version string
7857 * probably support the tachometer (like the early X models), so
7858 * detecting it is quite hard. We need more data to know for sure.
7859 *
7860 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7861 * might result.
7862 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007863 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7864 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007865 *
7866 * For firmware bugs, refer to:
7867 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7868 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007869 * ----
7870 *
7871 * ThinkPad EC register 0x31 bit 0 (only on select models)
7872 *
7873 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7874 * show the speed of the main fan. When bit 0 of EC register 0x31
7875 * is one, the tachometer registers show the speed of the auxiliary
7876 * fan.
7877 *
7878 * Fan control seems to affect both fans, regardless of the state
7879 * of this bit.
7880 *
7881 * So far, only the firmware for the X60/X61 non-tablet versions
7882 * seem to support this (firmware TP-7M).
7883 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007884 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007885 * ThinkPad X31, X40, X41. Not available in the X60.
7886 *
7887 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7888 * high speed. ACPI DSDT seems to map these three speeds to levels
7889 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7890 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7891 *
7892 * The speeds are stored on handles
7893 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7894 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007895 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007896 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7897 *
7898 * ACPI DSDT switches which set is in use depending on various
7899 * factors.
7900 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007901 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007902 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7903 * but the ACPI tables just mention level 7.
7904 */
7905
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007906enum { /* Fan control constants */
7907 fan_status_offset = 0x2f, /* EC register 0x2f */
7908 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7909 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007910 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7911 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007912
7913 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7914 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7915
7916 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7917};
7918
7919enum fan_status_access_mode {
7920 TPACPI_FAN_NONE = 0, /* No fan status or control */
7921 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7922 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7923};
7924
7925enum fan_control_access_mode {
7926 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7927 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7928 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7929 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7930};
7931
7932enum fan_control_commands {
7933 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7934 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7935 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7936 * and also watchdog cmd */
7937};
7938
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307939static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007940
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007941static enum fan_status_access_mode fan_status_access_mode;
7942static enum fan_control_access_mode fan_control_access_mode;
7943static enum fan_control_commands fan_control_commands;
7944
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007945static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007946static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007947static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007948static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007949
7950static struct mutex fan_mutex;
7951
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007952static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007953static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007954
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007955TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7956TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007957 "\\FSPD", /* 600e/x, 770e, 770x */
7958 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007959TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007960 "JFNS", /* 770x-JL */
7961 ); /* all others */
7962
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007963/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007964 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7965 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7966 * be in auto mode (0x80).
7967 *
7968 * This is corrected by any write to HFSP either by the driver, or
7969 * by the firmware.
7970 *
7971 * We assume 0x07 really means auto mode while this quirk is active,
7972 * as this is far more likely than the ThinkPad being in level 7,
7973 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007974 *
7975 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7976 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007977 */
7978
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007979static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007980{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007981 if (fan_control_initial_status == 0x07) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03007982 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 -03007983 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007984 }
7985}
7986
7987static void fan_quirk1_handle(u8 *fan_status)
7988{
7989 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7990 if (*fan_status != fan_control_initial_status) {
7991 /* something changed the HFSP regisnter since
7992 * driver init time, so it is not undefined
7993 * anymore */
7994 tp_features.fan_ctrl_status_undef = 0;
7995 } else {
7996 /* Return most likely status. In fact, it
7997 * might be the only possible status */
7998 *fan_status = TP_EC_FAN_AUTO;
7999 }
8000 }
8001}
8002
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008003/* Select main fan on X60/X61, NOOP on others */
8004static bool fan_select_fan1(void)
8005{
8006 if (tp_features.second_fan) {
8007 u8 val;
8008
8009 if (ec_read(fan_select_offset, &val) < 0)
8010 return false;
8011 val &= 0xFEU;
8012 if (ec_write(fan_select_offset, val) < 0)
8013 return false;
8014 }
8015 return true;
8016}
8017
8018/* Select secondary fan on X60/X61 */
8019static bool fan_select_fan2(void)
8020{
8021 u8 val;
8022
8023 if (!tp_features.second_fan)
8024 return false;
8025
8026 if (ec_read(fan_select_offset, &val) < 0)
8027 return false;
8028 val |= 0x01U;
8029 if (ec_write(fan_select_offset, val) < 0)
8030 return false;
8031
8032 return true;
8033}
8034
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008035/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008036 * Call with fan_mutex held
8037 */
8038static void fan_update_desired_level(u8 status)
8039{
8040 if ((status &
8041 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8042 if (status > 7)
8043 fan_control_desired_level = 7;
8044 else
8045 fan_control_desired_level = status;
8046 }
8047}
8048
8049static int fan_get_status(u8 *status)
8050{
8051 u8 s;
8052
8053 /* TODO:
8054 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8055
8056 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07008057 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008058 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07008059 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008060
Dan Carpentereceeb432012-09-01 12:54:07 -07008061 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008062 return -EIO;
8063
8064 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07008065 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008066
8067 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07008068 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008069 case TPACPI_FAN_RD_TPEC:
8070 /* all except 570, 600e/x, 770e, 770x */
8071 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8072 return -EIO;
8073
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008074 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008075 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008076 fan_quirk1_handle(status);
8077 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008078
8079 break;
8080
8081 default:
8082 return -ENXIO;
8083 }
8084
8085 return 0;
8086}
8087
8088static int fan_get_status_safe(u8 *status)
8089{
8090 int rc;
8091 u8 s;
8092
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008093 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008094 return -ERESTARTSYS;
8095 rc = fan_get_status(&s);
8096 if (!rc)
8097 fan_update_desired_level(s);
8098 mutex_unlock(&fan_mutex);
8099
Dan Carpentera7718362016-04-15 17:46:34 +03008100 if (rc)
8101 return rc;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008102 if (status)
8103 *status = s;
8104
Dan Carpentera7718362016-04-15 17:46:34 +03008105 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008106}
8107
8108static int fan_get_speed(unsigned int *speed)
8109{
8110 u8 hi, lo;
8111
8112 switch (fan_status_access_mode) {
8113 case TPACPI_FAN_RD_TPEC:
8114 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008115 if (unlikely(!fan_select_fan1()))
8116 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008117 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8118 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8119 return -EIO;
8120
8121 if (likely(speed))
8122 *speed = (hi << 8) | lo;
8123
8124 break;
8125
8126 default:
8127 return -ENXIO;
8128 }
8129
8130 return 0;
8131}
8132
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008133static int fan2_get_speed(unsigned int *speed)
8134{
8135 u8 hi, lo;
8136 bool rc;
8137
8138 switch (fan_status_access_mode) {
8139 case TPACPI_FAN_RD_TPEC:
8140 /* all except 570, 600e/x, 770e, 770x */
8141 if (unlikely(!fan_select_fan2()))
8142 return -EIO;
8143 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8144 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8145 fan_select_fan1(); /* play it safe */
8146 if (rc)
8147 return -EIO;
8148
8149 if (likely(speed))
8150 *speed = (hi << 8) | lo;
8151
8152 break;
8153
8154 default:
8155 return -ENXIO;
8156 }
8157
8158 return 0;
8159}
8160
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008161static int fan_set_level(int level)
8162{
8163 if (!fan_control_allowed)
8164 return -EPERM;
8165
8166 switch (fan_control_access_mode) {
8167 case TPACPI_FAN_WR_ACPI_SFAN:
8168 if (level >= 0 && level <= 7) {
8169 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8170 return -EIO;
8171 } else
8172 return -EINVAL;
8173 break;
8174
8175 case TPACPI_FAN_WR_ACPI_FANS:
8176 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008177 if (!(level & TP_EC_FAN_AUTO) &&
8178 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008179 ((level < 0) || (level > 7)))
8180 return -EINVAL;
8181
8182 /* safety net should the EC not support AUTO
8183 * or FULLSPEED mode bits and just ignore them */
8184 if (level & TP_EC_FAN_FULLSPEED)
8185 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01008186 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008187 level |= 4; /* safety min speed 4 */
8188
8189 if (!acpi_ec_write(fan_status_offset, level))
8190 return -EIO;
8191 else
8192 tp_features.fan_ctrl_status_undef = 0;
8193 break;
8194
8195 default:
8196 return -ENXIO;
8197 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008198
8199 vdbg_printk(TPACPI_DBG_FAN,
8200 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008201 return 0;
8202}
8203
8204static int fan_set_level_safe(int level)
8205{
8206 int rc;
8207
8208 if (!fan_control_allowed)
8209 return -EPERM;
8210
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008211 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008212 return -ERESTARTSYS;
8213
8214 if (level == TPACPI_FAN_LAST_LEVEL)
8215 level = fan_control_desired_level;
8216
8217 rc = fan_set_level(level);
8218 if (!rc)
8219 fan_update_desired_level(level);
8220
8221 mutex_unlock(&fan_mutex);
8222 return rc;
8223}
8224
8225static int fan_set_enable(void)
8226{
8227 u8 s;
8228 int rc;
8229
8230 if (!fan_control_allowed)
8231 return -EPERM;
8232
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008233 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008234 return -ERESTARTSYS;
8235
8236 switch (fan_control_access_mode) {
8237 case TPACPI_FAN_WR_ACPI_FANS:
8238 case TPACPI_FAN_WR_TPEC:
8239 rc = fan_get_status(&s);
8240 if (rc < 0)
8241 break;
8242
8243 /* Don't go out of emergency fan mode */
8244 if (s != 7) {
8245 s &= 0x07;
8246 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8247 }
8248
8249 if (!acpi_ec_write(fan_status_offset, s))
8250 rc = -EIO;
8251 else {
8252 tp_features.fan_ctrl_status_undef = 0;
8253 rc = 0;
8254 }
8255 break;
8256
8257 case TPACPI_FAN_WR_ACPI_SFAN:
8258 rc = fan_get_status(&s);
8259 if (rc < 0)
8260 break;
8261
8262 s &= 0x07;
8263
8264 /* Set fan to at least level 4 */
8265 s |= 4;
8266
8267 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008268 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008269 else
8270 rc = 0;
8271 break;
8272
8273 default:
8274 rc = -ENXIO;
8275 }
8276
8277 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008278
8279 if (!rc)
8280 vdbg_printk(TPACPI_DBG_FAN,
8281 "fan control: set fan control register to 0x%02x\n",
8282 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008283 return rc;
8284}
8285
8286static int fan_set_disable(void)
8287{
8288 int rc;
8289
8290 if (!fan_control_allowed)
8291 return -EPERM;
8292
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008293 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008294 return -ERESTARTSYS;
8295
8296 rc = 0;
8297 switch (fan_control_access_mode) {
8298 case TPACPI_FAN_WR_ACPI_FANS:
8299 case TPACPI_FAN_WR_TPEC:
8300 if (!acpi_ec_write(fan_status_offset, 0x00))
8301 rc = -EIO;
8302 else {
8303 fan_control_desired_level = 0;
8304 tp_features.fan_ctrl_status_undef = 0;
8305 }
8306 break;
8307
8308 case TPACPI_FAN_WR_ACPI_SFAN:
8309 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8310 rc = -EIO;
8311 else
8312 fan_control_desired_level = 0;
8313 break;
8314
8315 default:
8316 rc = -ENXIO;
8317 }
8318
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008319 if (!rc)
8320 vdbg_printk(TPACPI_DBG_FAN,
8321 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008322
8323 mutex_unlock(&fan_mutex);
8324 return rc;
8325}
8326
8327static int fan_set_speed(int speed)
8328{
8329 int rc;
8330
8331 if (!fan_control_allowed)
8332 return -EPERM;
8333
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008334 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008335 return -ERESTARTSYS;
8336
8337 rc = 0;
8338 switch (fan_control_access_mode) {
8339 case TPACPI_FAN_WR_ACPI_FANS:
8340 if (speed >= 0 && speed <= 65535) {
8341 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8342 speed, speed, speed))
8343 rc = -EIO;
8344 } else
8345 rc = -EINVAL;
8346 break;
8347
8348 default:
8349 rc = -ENXIO;
8350 }
8351
8352 mutex_unlock(&fan_mutex);
8353 return rc;
8354}
8355
8356static void fan_watchdog_reset(void)
8357{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008358 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8359 return;
8360
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008361 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008362 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8363 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8364 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8365 else
8366 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008367}
8368
8369static void fan_watchdog_fire(struct work_struct *ignored)
8370{
8371 int rc;
8372
8373 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8374 return;
8375
Joe Perches0978e012011-04-04 10:06:25 -07008376 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008377 rc = fan_set_enable();
8378 if (rc < 0) {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008379 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8380 rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008381 /* reschedule for later */
8382 fan_watchdog_reset();
8383 }
8384}
8385
8386/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008387 * SYSFS fan layout: hwmon compatible (device)
8388 *
8389 * pwm*_enable:
8390 * 0: "disengaged" mode
8391 * 1: manual mode
8392 * 2: native EC "auto" mode (recommended, hardware default)
8393 *
8394 * pwm*: set speed in manual mode, ignored otherwise.
8395 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8396 * interpolation.
8397 *
8398 * fan*_input: tachometer reading, RPM
8399 *
8400 *
8401 * SYSFS fan layout: extensions
8402 *
8403 * fan_watchdog (driver):
8404 * fan watchdog interval in seconds, 0 disables (default), max 120
8405 */
8406
8407/* sysfs fan pwm1_enable ----------------------------------------------- */
8408static ssize_t fan_pwm1_enable_show(struct device *dev,
8409 struct device_attribute *attr,
8410 char *buf)
8411{
8412 int res, mode;
8413 u8 status;
8414
8415 res = fan_get_status_safe(&status);
8416 if (res)
8417 return res;
8418
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008419 if (status & TP_EC_FAN_FULLSPEED) {
8420 mode = 0;
8421 } else if (status & TP_EC_FAN_AUTO) {
8422 mode = 2;
8423 } else
8424 mode = 1;
8425
8426 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8427}
8428
8429static ssize_t fan_pwm1_enable_store(struct device *dev,
8430 struct device_attribute *attr,
8431 const char *buf, size_t count)
8432{
8433 unsigned long t;
8434 int res, level;
8435
8436 if (parse_strtoul(buf, 2, &t))
8437 return -EINVAL;
8438
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008439 tpacpi_disclose_usertask("hwmon pwm1_enable",
8440 "set fan mode to %lu\n", t);
8441
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008442 switch (t) {
8443 case 0:
8444 level = TP_EC_FAN_FULLSPEED;
8445 break;
8446 case 1:
8447 level = TPACPI_FAN_LAST_LEVEL;
8448 break;
8449 case 2:
8450 level = TP_EC_FAN_AUTO;
8451 break;
8452 case 3:
8453 /* reserved for software-controlled auto mode */
8454 return -ENOSYS;
8455 default:
8456 return -EINVAL;
8457 }
8458
8459 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008460 if (res == -ENXIO)
8461 return -EINVAL;
8462 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008463 return res;
8464
8465 fan_watchdog_reset();
8466
8467 return count;
8468}
8469
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008470static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8471 fan_pwm1_enable_show, fan_pwm1_enable_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008472
8473/* sysfs fan pwm1 ------------------------------------------------------ */
8474static ssize_t fan_pwm1_show(struct device *dev,
8475 struct device_attribute *attr,
8476 char *buf)
8477{
8478 int res;
8479 u8 status;
8480
8481 res = fan_get_status_safe(&status);
8482 if (res)
8483 return res;
8484
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008485 if ((status &
8486 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8487 status = fan_control_desired_level;
8488
8489 if (status > 7)
8490 status = 7;
8491
8492 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8493}
8494
8495static ssize_t fan_pwm1_store(struct device *dev,
8496 struct device_attribute *attr,
8497 const char *buf, size_t count)
8498{
8499 unsigned long s;
8500 int rc;
8501 u8 status, newlevel;
8502
8503 if (parse_strtoul(buf, 255, &s))
8504 return -EINVAL;
8505
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008506 tpacpi_disclose_usertask("hwmon pwm1",
8507 "set fan speed to %lu\n", s);
8508
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008509 /* scale down from 0-255 to 0-7 */
8510 newlevel = (s >> 5) & 0x07;
8511
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008512 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008513 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008514
8515 rc = fan_get_status(&status);
8516 if (!rc && (status &
8517 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8518 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008519 if (rc == -ENXIO)
8520 rc = -EINVAL;
8521 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008522 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008523 fan_watchdog_reset();
8524 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008525 }
8526
8527 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008528 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008529}
8530
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008531static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008532
8533/* sysfs fan fan1_input ------------------------------------------------ */
8534static ssize_t fan_fan1_input_show(struct device *dev,
8535 struct device_attribute *attr,
8536 char *buf)
8537{
8538 int res;
8539 unsigned int speed;
8540
8541 res = fan_get_speed(&speed);
8542 if (res < 0)
8543 return res;
8544
8545 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8546}
8547
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008548static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008549
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008550/* sysfs fan fan2_input ------------------------------------------------ */
8551static ssize_t fan_fan2_input_show(struct device *dev,
8552 struct device_attribute *attr,
8553 char *buf)
8554{
8555 int res;
8556 unsigned int speed;
8557
8558 res = fan2_get_speed(&speed);
8559 if (res < 0)
8560 return res;
8561
8562 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8563}
8564
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008565static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008566
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008567/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008568static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
8569 char *buf)
8570{
8571 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8572}
8573
8574static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8575 const char *buf, size_t count)
8576{
8577 unsigned long t;
8578
8579 if (parse_strtoul(buf, 120, &t))
8580 return -EINVAL;
8581
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008582 if (!fan_control_allowed)
8583 return -EPERM;
8584
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008585 fan_watchdog_maxinterval = t;
8586 fan_watchdog_reset();
8587
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008588 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8589
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008590 return count;
8591}
8592
8593static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8594 fan_fan_watchdog_show, fan_fan_watchdog_store);
8595
8596/* --------------------------------------------------------------------- */
8597static struct attribute *fan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008598 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8599 &dev_attr_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008600 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008601 NULL
8602};
8603
8604static const struct attribute_group fan_attr_group = {
8605 .attrs = fan_attributes,
8606};
8607
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008608#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8609#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008610
8611#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8612 { .vendor = PCI_VENDOR_ID_IBM, \
8613 .bios = TPACPI_MATCH_ANY, \
8614 .ec = TPID(__id1, __id2), \
8615 .quirks = __quirks }
8616
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008617#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8618 { .vendor = PCI_VENDOR_ID_LENOVO, \
8619 .bios = TPACPI_MATCH_ANY, \
8620 .ec = TPID(__id1, __id2), \
8621 .quirks = __quirks }
8622
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008623static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8624 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8625 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8626 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8627 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008628 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008629};
8630
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008631#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008632#undef TPACPI_FAN_QI
8633
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008634static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008635{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008636 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008637 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008638
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008639 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8640 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008641
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008642 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008643 fan_status_access_mode = TPACPI_FAN_NONE;
8644 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008645 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008646 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008647 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008648 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008649 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008650
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008651 if (tpacpi_is_ibm()) {
8652 TPACPI_ACPIHANDLE_INIT(fans);
8653 TPACPI_ACPIHANDLE_INIT(gfan);
8654 TPACPI_ACPIHANDLE_INIT(sfan);
8655 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008656
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008657 quirks = tpacpi_check_quirks(fan_quirk_table,
8658 ARRAY_SIZE(fan_quirk_table));
8659
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008660 if (gfan_handle) {
8661 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008662 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008663 } else {
8664 /* all other ThinkPads: note that even old-style
8665 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008666 if (likely(acpi_ec_read(fan_status_offset,
8667 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008668 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008669 if (quirks & TPACPI_FAN_Q1)
8670 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008671 if (quirks & TPACPI_FAN_2FAN) {
8672 tp_features.second_fan = 1;
8673 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8674 "secondary fan support enabled\n");
8675 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008676 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008677 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008678 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008679 }
8680 }
8681
8682 if (sfan_handle) {
8683 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008684 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008685 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008686 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008687 } else {
8688 if (!gfan_handle) {
8689 /* gfan without sfan means no fan control */
8690 /* all other models implement TP EC 0x2f control */
8691
8692 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008693 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008694 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008695 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008696 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008697 TPACPI_FAN_CMD_SPEED |
8698 TPACPI_FAN_CMD_LEVEL |
8699 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008700 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008701 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008702 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008703 TPACPI_FAN_CMD_LEVEL |
8704 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008705 }
8706 }
8707 }
8708
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008709 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8710 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008711 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8712 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8713 fan_status_access_mode, fan_control_access_mode);
8714
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008715 /* fan control master switch */
8716 if (!fan_control_allowed) {
8717 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8718 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008719 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008720 "fan control features disabled by parameter\n");
8721 }
8722
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008723 /* update fan_control_desired_level */
8724 if (fan_status_access_mode != TPACPI_FAN_NONE)
8725 fan_get_status_safe(NULL);
8726
8727 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8728 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008729 if (tp_features.second_fan) {
8730 /* attach second fan tachometer */
8731 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008732 &dev_attr_fan2_input.attr;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008733 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008734 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008735 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008736 if (rc < 0)
8737 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008738
8739 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8740 &driver_attr_fan_watchdog);
8741 if (rc < 0) {
8742 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8743 &fan_attr_group);
8744 return rc;
8745 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008746 return 0;
8747 } else
8748 return 1;
8749}
8750
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008751static void fan_exit(void)
8752{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008753 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008754 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008755
8756 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008757 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008758 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8759 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008760
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008761 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008762 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008763}
8764
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008765static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008766{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008767 int rc;
8768
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008769 if (!fan_control_allowed)
8770 return;
8771
8772 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008773 fan_control_resume_level = 0;
8774 rc = fan_get_status_safe(&fan_control_resume_level);
8775 if (rc < 0)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008776 pr_notice("failed to read fan level for later restore during resume: %d\n",
8777 rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008778
8779 /* if it is undefined, don't attempt to restore it.
8780 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008781 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008782 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008783}
8784
8785static void fan_resume(void)
8786{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008787 u8 current_level = 7;
8788 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008789 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008790
8791 /* DSDT *always* updates status on resume */
8792 tp_features.fan_ctrl_status_undef = 0;
8793
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008794 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008795 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008796 (fan_get_status_safe(&current_level) < 0))
8797 return;
8798
8799 switch (fan_control_access_mode) {
8800 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008801 /* never decrease fan level */
8802 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008803 break;
8804 case TPACPI_FAN_WR_ACPI_FANS:
8805 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008806 /* never decrease fan level, scale is:
8807 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8808 *
8809 * We expect the firmware to set either 7 or AUTO, but we
8810 * handle FULLSPEED out of paranoia.
8811 *
8812 * So, we can safely only restore FULLSPEED or 7, anything
8813 * else could slow the fan. Restoring AUTO is useless, at
8814 * best that's exactly what the DSDT already set (it is the
8815 * slower it uses).
8816 *
8817 * Always keep in mind that the DSDT *will* have set the
8818 * fans to what the vendor supposes is the best level. We
8819 * muck with it only to speed the fan up.
8820 */
8821 if (fan_control_resume_level != 7 &&
8822 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8823 return;
8824 else
8825 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8826 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008827 break;
8828 default:
8829 return;
8830 }
8831 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008832 pr_notice("restoring fan level to 0x%02x\n",
8833 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008834 rc = fan_set_level_safe(fan_control_resume_level);
8835 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008836 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008837 }
8838}
8839
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008840static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008841{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008842 int rc;
8843 u8 status;
8844 unsigned int speed = 0;
8845
8846 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008847 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008848 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008849 rc = fan_get_status_safe(&status);
8850 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008851 return rc;
8852
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008853 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008854 "level:\t\t%d\n",
8855 (status != 0) ? "enabled" : "disabled", status);
8856 break;
8857
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008858 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008859 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008860 rc = fan_get_status_safe(&status);
8861 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008862 return rc;
8863
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008864 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008865 (status != 0) ? "enabled" : "disabled");
8866
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008867 rc = fan_get_speed(&speed);
8868 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008869 return rc;
8870
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008871 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008872
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008873 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008874 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008875 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008876 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008877 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008878 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008879 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008880 break;
8881
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008882 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008883 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008884 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008885 }
8886
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008887 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008888 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008889
8890 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008891 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008892 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008893 break;
8894
8895 default:
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008896 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008897 break;
8898 }
8899 }
8900
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008901 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008902 seq_printf(m, "commands:\tenable, disable\n"
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008903 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008904
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008905 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03008906 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008907
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008908 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008909}
8910
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008911static int fan_write_cmd_level(const char *cmd, int *rc)
8912{
8913 int level;
8914
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008915 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008916 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008917 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008918 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008919 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008920 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008921 return 0;
8922
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008923 *rc = fan_set_level_safe(level);
8924 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008925 pr_err("level command accepted for unsupported access mode %d\n",
8926 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008927 else if (!*rc)
8928 tpacpi_disclose_usertask("procfs fan",
8929 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008930
8931 return 1;
8932}
8933
8934static int fan_write_cmd_enable(const char *cmd, int *rc)
8935{
8936 if (strlencmp(cmd, "enable") != 0)
8937 return 0;
8938
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008939 *rc = fan_set_enable();
8940 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008941 pr_err("enable 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", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008945
8946 return 1;
8947}
8948
8949static int fan_write_cmd_disable(const char *cmd, int *rc)
8950{
8951 if (strlencmp(cmd, "disable") != 0)
8952 return 0;
8953
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008954 *rc = fan_set_disable();
8955 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008956 pr_err("disable command accepted for unsupported access mode %d\n",
8957 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008958 else if (!*rc)
8959 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008960
8961 return 1;
8962}
8963
8964static int fan_write_cmd_speed(const char *cmd, int *rc)
8965{
8966 int speed;
8967
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008968 /* TODO:
8969 * Support speed <low> <medium> <high> ? */
8970
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008971 if (sscanf(cmd, "speed %d", &speed) != 1)
8972 return 0;
8973
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008974 *rc = fan_set_speed(speed);
8975 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008976 pr_err("speed command accepted for unsupported access mode %d\n",
8977 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008978 else if (!*rc)
8979 tpacpi_disclose_usertask("procfs fan",
8980 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008981
8982 return 1;
8983}
8984
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008985static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8986{
8987 int interval;
8988
8989 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8990 return 0;
8991
8992 if (interval < 0 || interval > 120)
8993 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008994 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008995 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008996 tpacpi_disclose_usertask("procfs fan",
8997 "set watchdog timer to %d\n",
8998 interval);
8999 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009000
9001 return 1;
9002}
9003
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009004static int fan_write(char *buf)
9005{
9006 char *cmd;
9007 int rc = 0;
9008
9009 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009010 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009011 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009012 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009013 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009014 fan_write_cmd_disable(cmd, &rc) ||
9015 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009016 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009017 fan_write_cmd_speed(cmd, &rc))
9018 )
9019 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009020 else if (!rc)
9021 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009022 }
9023
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009024 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009025}
9026
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009027static struct ibm_struct fan_driver_data = {
9028 .name = "fan",
9029 .read = fan_read,
9030 .write = fan_write,
9031 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03009032 .suspend = fan_suspend,
9033 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009034};
9035
David Henningsson420f9732013-10-16 23:10:31 +02009036/*************************************************************************
9037 * Mute LED subdriver
9038 */
9039
9040
9041struct tp_led_table {
9042 acpi_string name;
9043 int on_value;
9044 int off_value;
9045 int state;
9046};
9047
9048static struct tp_led_table led_tables[] = {
9049 [TPACPI_LED_MUTE] = {
9050 .name = "SSMS",
9051 .on_value = 1,
9052 .off_value = 0,
9053 },
9054 [TPACPI_LED_MICMUTE] = {
9055 .name = "MMTS",
9056 .on_value = 2,
9057 .off_value = 0,
9058 },
9059};
9060
9061static int mute_led_on_off(struct tp_led_table *t, bool state)
9062{
9063 acpi_handle temp;
9064 int output;
9065
Axel Lin3526eca2016-09-19 09:33:51 +08009066 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009067 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9068 return -EIO;
9069 }
9070
9071 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9072 state ? t->on_value : t->off_value))
9073 return -EIO;
9074
9075 t->state = state;
9076 return state;
9077}
9078
9079int tpacpi_led_set(int whichled, bool on)
9080{
9081 struct tp_led_table *t;
9082
9083 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9084 return -EINVAL;
9085
9086 t = &led_tables[whichled];
9087 if (t->state < 0 || t->state == on)
9088 return t->state;
9089 return mute_led_on_off(t, on);
9090}
9091EXPORT_SYMBOL_GPL(tpacpi_led_set);
9092
9093static int mute_led_init(struct ibm_init_struct *iibm)
9094{
9095 acpi_handle temp;
9096 int i;
9097
9098 for (i = 0; i < TPACPI_LED_MAX; i++) {
9099 struct tp_led_table *t = &led_tables[i];
9100 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9101 mute_led_on_off(t, false);
9102 else
9103 t->state = -ENODEV;
9104 }
9105 return 0;
9106}
9107
9108static void mute_led_exit(void)
9109{
9110 int i;
9111
9112 for (i = 0; i < TPACPI_LED_MAX; i++)
9113 tpacpi_led_set(i, false);
9114}
9115
Takashi Iwai119f4492014-02-12 16:32:45 +01009116static void mute_led_resume(void)
9117{
9118 int i;
9119
9120 for (i = 0; i < TPACPI_LED_MAX; i++) {
9121 struct tp_led_table *t = &led_tables[i];
9122 if (t->state >= 0)
9123 mute_led_on_off(t, t->state);
9124 }
9125}
9126
David Henningsson420f9732013-10-16 23:10:31 +02009127static struct ibm_struct mute_led_driver_data = {
9128 .name = "mute_led",
9129 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01009130 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02009131};
9132
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009133/****************************************************************************
9134 ****************************************************************************
9135 *
9136 * Infrastructure
9137 *
9138 ****************************************************************************
9139 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07009140
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009141/*
9142 * HKEY event callout for other subdrivers go here
9143 * (yes, it is ugly, but it is quick, safe, and gets the job done
9144 */
9145static void tpacpi_driver_event(const unsigned int hkey_event)
9146{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00009147 if (ibm_backlight_device) {
9148 switch (hkey_event) {
9149 case TP_HKEY_EV_BRGHT_UP:
9150 case TP_HKEY_EV_BRGHT_DOWN:
9151 tpacpi_brightness_notify_change();
9152 }
9153 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009154 if (alsa_card) {
9155 switch (hkey_event) {
9156 case TP_HKEY_EV_VOL_UP:
9157 case TP_HKEY_EV_VOL_DOWN:
9158 case TP_HKEY_EV_VOL_MUTE:
9159 volume_alsa_notify_change();
9160 }
9161 }
Hans de Goedec685e202017-02-09 16:44:13 +01009162 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9163 enum led_brightness brightness;
9164
9165 mutex_lock(&kbdlight_mutex);
9166
9167 /*
9168 * Check the brightness actually changed, setting the brightness
9169 * through kbdlight_set_level() also triggers this event.
9170 */
9171 brightness = kbdlight_sysfs_get(NULL);
9172 if (kbdlight_brightness != brightness) {
9173 kbdlight_brightness = brightness;
9174 led_classdev_notify_brightness_hw_changed(
9175 &tpacpi_led_kbdlight.led_classdev, brightness);
9176 }
9177
9178 mutex_unlock(&kbdlight_mutex);
9179 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009180}
9181
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009182static void hotkey_driver_event(const unsigned int scancode)
9183{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03009184 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009185}
9186
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009187/* sysfs name ---------------------------------------------------------- */
9188static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
9189 struct device_attribute *attr,
9190 char *buf)
9191{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009192 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009193}
9194
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009195static DEVICE_ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009196
9197/* --------------------------------------------------------------------- */
9198
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009199/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03009200static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009201
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009202/*
9203 * Module and infrastructure proble, init and exit handling
9204 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009205
Rusty Russell90ab5ee2012-01-13 09:32:20 +10309206static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009207
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009208#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009209static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009210{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009211 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009212
Jan van den Berg72a979f2014-09-17 00:01:08 +02009213 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009214}
9215#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9216
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009217static void ibm_exit(struct ibm_struct *ibm)
9218{
9219 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9220
9221 list_del_init(&ibm->all_drivers);
9222
9223 if (ibm->flags.acpi_notify_installed) {
9224 dbg_printk(TPACPI_DBG_EXIT,
9225 "%s: acpi_remove_notify_handler\n", ibm->name);
9226 BUG_ON(!ibm->acpi);
9227 acpi_remove_notify_handler(*ibm->acpi->handle,
9228 ibm->acpi->type,
9229 dispatch_acpi_notify);
9230 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009231 }
9232
9233 if (ibm->flags.proc_created) {
9234 dbg_printk(TPACPI_DBG_EXIT,
9235 "%s: remove_proc_entry\n", ibm->name);
9236 remove_proc_entry(ibm->name, proc_dir);
9237 ibm->flags.proc_created = 0;
9238 }
9239
9240 if (ibm->flags.acpi_driver_registered) {
9241 dbg_printk(TPACPI_DBG_EXIT,
9242 "%s: acpi_bus_unregister_driver\n", ibm->name);
9243 BUG_ON(!ibm->acpi);
9244 acpi_bus_unregister_driver(ibm->acpi->driver);
9245 kfree(ibm->acpi->driver);
9246 ibm->acpi->driver = NULL;
9247 ibm->flags.acpi_driver_registered = 0;
9248 }
9249
9250 if (ibm->flags.init_called && ibm->exit) {
9251 ibm->exit();
9252 ibm->flags.init_called = 0;
9253 }
9254
9255 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9256}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009257
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009258static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009259{
9260 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009261 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009262 struct proc_dir_entry *entry;
9263
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009264 BUG_ON(ibm == NULL);
9265
9266 INIT_LIST_HEAD(&ibm->all_drivers);
9267
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009268 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009269 return 0;
9270
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009271 dbg_printk(TPACPI_DBG_INIT,
9272 "probing for %s\n", ibm->name);
9273
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009274 if (iibm->init) {
9275 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009276 if (ret > 0)
9277 return 0; /* probe failed */
9278 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009279 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009280
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009281 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009282 }
9283
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009284 if (ibm->acpi) {
9285 if (ibm->acpi->hid) {
9286 ret = register_tpacpi_subdriver(ibm);
9287 if (ret)
9288 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009289 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009290
9291 if (ibm->acpi->notify) {
9292 ret = setup_acpi_notify(ibm);
9293 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07009294 pr_notice("disabling subdriver %s\n",
9295 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009296 ret = 0;
9297 goto err_out;
9298 }
9299 if (ret < 0)
9300 goto err_out;
9301 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009302 }
9303
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009304 dbg_printk(TPACPI_DBG_INIT,
9305 "%s installed\n", ibm->name);
9306
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009307 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04009308 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009309
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009310 if (!mode)
9311 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009312 if (ibm->write)
9313 mode |= S_IWUSR;
9314 entry = proc_create_data(ibm->name, mode, proc_dir,
9315 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009316 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07009317 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009318 ret = -ENODEV;
9319 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009320 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009321 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009324 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9325
Linus Torvalds1da177e2005-04-16 15:20:36 -07009326 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009327
9328err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009329 dbg_printk(TPACPI_DBG_INIT,
9330 "%s: at error exit path with result %d\n",
9331 ibm->name, ret);
9332
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009333 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +02009334 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335}
9336
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009337/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009339static bool __pure __init tpacpi_is_fw_digit(const char c)
9340{
9341 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9342}
9343
Jan van den Berg72a979f2014-09-17 00:01:08 +02009344static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009345 const char t)
9346{
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009347 /*
9348 * Most models: xxyTkkWW (#.##c)
9349 * Ancient 570/600 and -SL lacks (#.##c)
9350 */
9351 if (s && strlen(s) >= 8 &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009352 tpacpi_is_fw_digit(s[0]) &&
9353 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009354 s[2] == t &&
9355 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009356 tpacpi_is_fw_digit(s[4]) &&
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009357 tpacpi_is_fw_digit(s[5]))
9358 return true;
9359
9360 /* New models: xxxyTkkW (#.##c); T550 and some others */
9361 return s && strlen(s) >= 8 &&
9362 tpacpi_is_fw_digit(s[0]) &&
9363 tpacpi_is_fw_digit(s[1]) &&
9364 tpacpi_is_fw_digit(s[2]) &&
9365 s[3] == t &&
9366 (s[4] == 'T' || s[4] == 'N') &&
9367 tpacpi_is_fw_digit(s[5]) &&
9368 tpacpi_is_fw_digit(s[6]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009369}
9370
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009371/* returns 0 - probe ok, or < 0 - probe error.
9372 * Probe ok doesn't mean thinkpad found.
9373 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9374static int __must_check __init get_thinkpad_model_data(
9375 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009376{
Jeff Garzik18552562007-10-03 15:15:40 -04009377 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009378 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009379 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009380
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009381 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009382 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009383
9384 memset(tp, 0, sizeof(*tp));
9385
9386 if (dmi_name_in_vendors("IBM"))
9387 tp->vendor = PCI_VENDOR_ID_IBM;
9388 else if (dmi_name_in_vendors("LENOVO"))
9389 tp->vendor = PCI_VENDOR_ID_LENOVO;
9390 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009391 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009392
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009393 s = dmi_get_system_info(DMI_BIOS_VERSION);
9394 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9395 if (s && !tp->bios_version_str)
9396 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009397
9398 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009399 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9400 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009401 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009402
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009403 tp->bios_model = tp->bios_version_str[0]
9404 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009405 tp->bios_release = (tp->bios_version_str[4] << 8)
9406 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009407
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009408 /*
9409 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9410 * X32 or newer, all Z series; Some models must have an
9411 * up-to-date BIOS or they will not be detected.
9412 *
9413 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9414 */
9415 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009416 if (sscanf(dev->name,
9417 "IBM ThinkPad Embedded Controller -[%17c",
9418 ec_fw_string) == 1) {
9419 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9420 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009421
9422 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009423 if (!tp->ec_version_str)
9424 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009425
9426 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9427 tp->ec_model = ec_fw_string[0]
9428 | (ec_fw_string[1] << 8);
9429 tp->ec_release = (ec_fw_string[4] << 8)
9430 | ec_fw_string[5];
9431 } else {
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009432 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
Joe Perches0978e012011-04-04 10:06:25 -07009433 ec_fw_string);
9434 pr_notice("please report this to %s\n",
9435 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009436 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009437 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009438 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009439 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009440
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009441 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009442 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009443 tp->model_str = kstrdup(s, GFP_KERNEL);
9444 if (!tp->model_str)
9445 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009446 } else {
9447 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009448 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009449 tp->model_str = kstrdup(s, GFP_KERNEL);
9450 if (!tp->model_str)
9451 return -ENOMEM;
9452 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009453 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03009454
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009455 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9456 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9457 if (s && !tp->nummodel_str)
9458 return -ENOMEM;
9459
9460 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009461}
9462
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009463static int __init probe_for_thinkpad(void)
9464{
9465 int is_thinkpad;
9466
9467 if (acpi_disabled)
9468 return -ENODEV;
9469
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009470 /* It would be dangerous to run the driver in this case */
9471 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9472 return -ENODEV;
9473
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009474 /*
9475 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009476 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009477 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009478 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9479 (thinkpad_id.ec_model != 0) ||
9480 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009481
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03009482 /* The EC handler is required */
9483 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009484 if (!ec_handle) {
9485 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07009486 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009487 return -ENODEV;
9488 }
9489
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009490 if (!is_thinkpad && !force_load)
9491 return -ENODEV;
9492
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009493 return 0;
9494}
9495
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009496static void __init thinkpad_acpi_init_banner(void)
9497{
Joe Perches0978e012011-04-04 10:06:25 -07009498 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9499 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009500
Joe Perches0978e012011-04-04 10:06:25 -07009501 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009502 (thinkpad_id.bios_version_str) ?
9503 thinkpad_id.bios_version_str : "unknown",
9504 (thinkpad_id.ec_version_str) ?
9505 thinkpad_id.ec_version_str : "unknown");
9506
9507 BUG_ON(!thinkpad_id.vendor);
9508
9509 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07009510 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009511 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9512 "IBM" : ((thinkpad_id.vendor ==
9513 PCI_VENDOR_ID_LENOVO) ?
9514 "Lenovo" : "Unknown vendor"),
9515 thinkpad_id.model_str,
9516 (thinkpad_id.nummodel_str) ?
9517 thinkpad_id.nummodel_str : "unknown");
9518}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009519
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009520/* Module init, exit, parameters */
9521
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009522static struct ibm_init_struct ibms_init[] __initdata = {
9523 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009524 .data = &thinkpad_acpi_driver_data,
9525 },
9526 {
9527 .init = hotkey_init,
9528 .data = &hotkey_driver_data,
9529 },
9530 {
9531 .init = bluetooth_init,
9532 .data = &bluetooth_driver_data,
9533 },
9534 {
9535 .init = wan_init,
9536 .data = &wan_driver_data,
9537 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009538 {
9539 .init = uwb_init,
9540 .data = &uwb_driver_data,
9541 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009542#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009543 {
9544 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009545 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009546 .data = &video_driver_data,
9547 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009548#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009549 {
Pali Rohárbb28f3d52015-12-30 23:27:41 +01009550 .init = kbdlight_init,
9551 .data = &kbdlight_driver_data,
9552 },
9553 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009554 .init = light_init,
9555 .data = &light_driver_data,
9556 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009557 {
9558 .init = cmos_init,
9559 .data = &cmos_driver_data,
9560 },
9561 {
9562 .init = led_init,
9563 .data = &led_driver_data,
9564 },
9565 {
9566 .init = beep_init,
9567 .data = &beep_driver_data,
9568 },
9569 {
9570 .init = thermal_init,
9571 .data = &thermal_driver_data,
9572 },
9573 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009574 .init = brightness_init,
9575 .data = &brightness_driver_data,
9576 },
9577 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009578 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009579 .data = &volume_driver_data,
9580 },
9581 {
9582 .init = fan_init,
9583 .data = &fan_driver_data,
9584 },
David Henningsson420f9732013-10-16 23:10:31 +02009585 {
9586 .init = mute_led_init,
9587 .data = &mute_led_driver_data,
9588 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009589};
9590
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009591static int __init set_ibm_param(const char *val, struct kernel_param *kp)
9592{
9593 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009594 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009595
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009596 if (!kp || !kp->name || !val)
9597 return -EINVAL;
9598
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009599 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9600 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009601 WARN_ON(ibm == NULL);
9602
9603 if (!ibm || !ibm->name)
9604 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009605
9606 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9607 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009608 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009609 strcpy(ibms_init[i].param, val);
9610 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009611 return 0;
9612 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009613 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009614
9615 return -EINVAL;
9616}
9617
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009618module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009619MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009620 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009621
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009622module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009623MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009624
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009625module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009626MODULE_PARM_DESC(force_load,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009627 "Attempts to load the driver even on a mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009628
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009629module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009630MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009631 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03009632
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009633module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009634MODULE_PARM_DESC(brightness_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009635 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03009636
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009637module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009638MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009639 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02009640
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009641#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009642module_param_named(volume_mode, volume_mode, uint, 0444);
9643MODULE_PARM_DESC(volume_mode,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009644 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009645
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02009646module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9647MODULE_PARM_DESC(volume_capabilities,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009648 "Selects the mixer capabilites: 0=auto, 1=volume and mute, 2=mute only");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02009649
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02009650module_param_named(volume_control, volume_control_allowed, bool, 0444);
9651MODULE_PARM_DESC(volume_control,
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009652 "Enables software override for the console audio control when true");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02009653
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07009654module_param_named(software_mute, software_mute_requested, bool, 0444);
9655MODULE_PARM_DESC(software_mute,
9656 "Request full software mute control");
9657
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009658/* ALSA module API parameters */
9659module_param_named(index, alsa_index, int, 0444);
9660MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9661module_param_named(id, alsa_id, charp, 0444);
9662MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9663module_param_named(enable, alsa_enable, bool, 0444);
9664MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009665#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009666
Andy Shevchenko9b4bbbd2017-05-09 17:17:21 +03009667/* The module parameter can't be read back, that's why 0 is used here */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009668#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009669 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Andy Shevchenko5cac62a2017-05-09 17:17:20 +03009670 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009671
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009672TPACPI_PARAM(hotkey);
9673TPACPI_PARAM(bluetooth);
9674TPACPI_PARAM(video);
9675TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009676TPACPI_PARAM(cmos);
9677TPACPI_PARAM(led);
9678TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009679TPACPI_PARAM(brightness);
9680TPACPI_PARAM(volume);
9681TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009682
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009683#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009684module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009685MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9686module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9687MODULE_PARM_DESC(wlsw_state,
9688 "Initial state of the emulated WLSW switch");
9689
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009690module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009691MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9692module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9693MODULE_PARM_DESC(bluetooth_state,
9694 "Initial state of the emulated bluetooth switch");
9695
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009696module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009697MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9698module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9699MODULE_PARM_DESC(wwan_state,
9700 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009701
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009702module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009703MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9704module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9705MODULE_PARM_DESC(uwb_state,
9706 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009707#endif
9708
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009709static void thinkpad_acpi_module_exit(void)
9710{
9711 struct ibm_struct *ibm, *itmp;
9712
9713 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9714
9715 list_for_each_entry_safe_reverse(ibm, itmp,
9716 &tpacpi_all_drivers,
9717 all_drivers) {
9718 ibm_exit(ibm);
9719 }
9720
9721 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9722
9723 if (tpacpi_inputdev) {
9724 if (tp_features.input_device_registered)
9725 input_unregister_device(tpacpi_inputdev);
9726 else
9727 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +10009728 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009729 }
9730
9731 if (tpacpi_hwmon)
9732 hwmon_device_unregister(tpacpi_hwmon);
9733
9734 if (tp_features.sensors_pdev_attrs_registered)
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009735 device_remove_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009736 if (tpacpi_sensors_pdev)
9737 platform_device_unregister(tpacpi_sensors_pdev);
9738 if (tpacpi_pdev)
9739 platform_device_unregister(tpacpi_pdev);
9740
9741 if (tp_features.sensors_pdrv_attrs_registered)
9742 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9743 if (tp_features.platform_drv_attrs_registered)
9744 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9745
9746 if (tp_features.sensors_pdrv_registered)
9747 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9748
9749 if (tp_features.platform_drv_registered)
9750 platform_driver_unregister(&tpacpi_pdriver);
9751
9752 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009753 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009754
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009755 if (tpacpi_wq)
9756 destroy_workqueue(tpacpi_wq);
9757
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009758 kfree(thinkpad_id.bios_version_str);
9759 kfree(thinkpad_id.ec_version_str);
9760 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +10009761 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009762}
9763
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009764
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009765static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009766{
9767 int ret, i;
9768
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009769 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9770
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009771 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009772
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009773 ret = get_thinkpad_model_data(&thinkpad_id);
9774 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009775 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009776 thinkpad_acpi_module_exit();
9777 return ret;
9778 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009779 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009780 if (ret) {
9781 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009782 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009785 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009786
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009787 thinkpad_acpi_init_banner();
9788 tpacpi_check_outdated_fw();
9789
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009790 TPACPI_ACPIHANDLE_INIT(ecrd);
9791 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009793 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9794 if (!tpacpi_wq) {
9795 thinkpad_acpi_module_exit();
9796 return -ENOMEM;
9797 }
9798
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009799 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07009801 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009802 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803 return -ENODEV;
9804 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009805
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009806 ret = platform_driver_register(&tpacpi_pdriver);
9807 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009808 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009809 thinkpad_acpi_module_exit();
9810 return ret;
9811 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009812 tp_features.platform_drv_registered = 1;
9813
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009814 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9815 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009816 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009817 thinkpad_acpi_module_exit();
9818 return ret;
9819 }
9820 tp_features.sensors_pdrv_registered = 1;
9821
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009822 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009823 if (!ret) {
9824 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009825 ret = tpacpi_create_driver_attributes(
9826 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009827 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009828 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009829 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009830 thinkpad_acpi_module_exit();
9831 return ret;
9832 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009833 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009834
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009835
9836 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009837 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009838 NULL, 0);
9839 if (IS_ERR(tpacpi_pdev)) {
9840 ret = PTR_ERR(tpacpi_pdev);
9841 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009842 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009843 thinkpad_acpi_module_exit();
9844 return ret;
9845 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009846 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009847 TPACPI_HWMON_DRVR_NAME,
9848 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009849 if (IS_ERR(tpacpi_sensors_pdev)) {
9850 ret = PTR_ERR(tpacpi_sensors_pdev);
9851 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009852 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009853 thinkpad_acpi_module_exit();
9854 return ret;
9855 }
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009856 ret = device_create_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009857 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009858 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009859 thinkpad_acpi_module_exit();
9860 return ret;
9861 }
9862 tp_features.sensors_pdev_attrs_registered = 1;
9863 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009864 if (IS_ERR(tpacpi_hwmon)) {
9865 ret = PTR_ERR(tpacpi_hwmon);
9866 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009867 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009868 thinkpad_acpi_module_exit();
9869 return ret;
9870 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009871 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009872 tpacpi_inputdev = input_allocate_device();
9873 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009874 thinkpad_acpi_module_exit();
9875 return -ENOMEM;
9876 } else {
9877 /* Prepare input device, but don't register */
9878 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009879 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009880 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009881 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009882 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9883 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009884 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009885 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009886
9887 /* Init subdriver dependencies */
9888 tpacpi_detect_brightness_capabilities();
9889
9890 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009891 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9892 ret = ibm_init(&ibms_init[i]);
9893 if (ret >= 0 && *ibms_init[i].param)
9894 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009895 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009896 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009897 return ret;
9898 }
9899 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009900
9901 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9902
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009903 ret = input_register_device(tpacpi_inputdev);
9904 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009905 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009906 thinkpad_acpi_module_exit();
9907 return ret;
9908 } else {
9909 tp_features.input_device_registered = 1;
9910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009911
9912 return 0;
9913}
9914
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009915MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9916
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009917/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009918 * This will autoload the driver in almost every ThinkPad
9919 * in widespread use.
9920 *
9921 * Only _VERY_ old models, like the 240, 240x and 570 lack
9922 * the HKEY event interface.
9923 */
9924MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9925
9926/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009927 * DMI matching for module autoloading
9928 *
9929 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9930 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9931 *
9932 * Only models listed in thinkwiki will be supported, so add yours
9933 * if it is not there yet.
9934 */
9935#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009936 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009937
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009938/* Ancient thinkpad BIOSes have to be identified by
9939 * BIOS type or model number, and there are far less
9940 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009941IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009942
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009943MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9944MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009945MODULE_DESCRIPTION(TPACPI_DESC);
9946MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009947MODULE_LICENSE("GPL");
9948
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009949module_init(thinkpad_acpi_module_init);
9950module_exit(thinkpad_acpi_module_exit);