blob: f6861b551178a1a84096695fe12ea867112246fa [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 Holschuhbee4cd9b2009-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:
Joe Perches0978e012011-04-04 10:06:25 -0700593 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 "with invalid format character '%c'\n", 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:
Joe Perches0978e012011-04-04 10:06:25 -0700622 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 "with invalid format character '%c'\n", res_type);
624 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) {
Joe Perches0978e012011-04-04 10:06:25 -0700793 pr_notice("another device driver is already "
794 "handling %s events\n", 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 Holschuhd64c81c42008-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 Holschuhd64c81c42008-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");
Joe Perches0978e012011-04-04 10:06:25 -07001098 pr_warn("WARNING: sysfs attribute %s is deprecated and "
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001099 "will be removed. %s\n",
1100 what, details);
1101}
1102
Johannes Berg19d337d2009-06-02 13:01:37 +02001103/*************************************************************************
1104 * rfkill and radio control support helpers
1105 */
1106
1107/*
1108 * ThinkPad-ACPI firmware handling model:
1109 *
1110 * WLSW (master wireless switch) is event-driven, and is common to all
1111 * firmware-controlled radios. It cannot be controlled, just monitored,
1112 * as expected. It overrides all radio state in firmware
1113 *
1114 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1115 * (TODO: verify how WLSW interacts with the returned radio state).
1116 *
1117 * The only time there are shadow radio state changes, is when
1118 * masked-off hotkeys are used.
1119 */
1120
1121/*
1122 * Internal driver API for radio state:
1123 *
1124 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1125 * bool: true means radio blocked (off)
1126 */
1127enum tpacpi_rfkill_state {
1128 TPACPI_RFK_RADIO_OFF = 0,
1129 TPACPI_RFK_RADIO_ON
1130};
1131
1132/* rfkill switches */
1133enum tpacpi_rfk_id {
1134 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1135 TPACPI_RFK_WWAN_SW_ID,
1136 TPACPI_RFK_UWB_SW_ID,
1137 TPACPI_RFK_SW_MAX
1138};
1139
1140static const char *tpacpi_rfkill_names[] = {
1141 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1142 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1143 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1144 [TPACPI_RFK_SW_MAX] = NULL
1145};
1146
1147/* ThinkPad-ACPI rfkill subdriver */
1148struct tpacpi_rfk {
1149 struct rfkill *rfkill;
1150 enum tpacpi_rfk_id id;
1151 const struct tpacpi_rfk_ops *ops;
1152};
1153
1154struct tpacpi_rfk_ops {
1155 /* firmware interface */
1156 int (*get_status)(void);
1157 int (*set_status)(const enum tpacpi_rfkill_state);
1158};
1159
1160static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1161
1162/* Query FW and update rfkill sw state for a given rfkill switch */
1163static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1164{
1165 int status;
1166
1167 if (!tp_rfk)
1168 return -ENODEV;
1169
1170 status = (tp_rfk->ops->get_status)();
1171 if (status < 0)
1172 return status;
1173
1174 rfkill_set_sw_state(tp_rfk->rfkill,
1175 (status == TPACPI_RFK_RADIO_OFF));
1176
1177 return status;
1178}
1179
1180/* Query FW and update rfkill sw state for all rfkill switches */
1181static void tpacpi_rfk_update_swstate_all(void)
1182{
1183 unsigned int i;
1184
1185 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1186 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1187}
1188
1189/*
1190 * Sync the HW-blocking state of all rfkill switches,
1191 * do notice it causes the rfkill core to schedule uevents
1192 */
1193static void tpacpi_rfk_update_hwblock_state(bool blocked)
1194{
1195 unsigned int i;
1196 struct tpacpi_rfk *tp_rfk;
1197
1198 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1199 tp_rfk = tpacpi_rfkill_switches[i];
1200 if (tp_rfk) {
1201 if (rfkill_set_hw_state(tp_rfk->rfkill,
1202 blocked)) {
1203 /* ignore -- we track sw block */
1204 }
1205 }
1206 }
1207}
1208
1209/* Call to get the WLSW state from the firmware */
1210static int hotkey_get_wlsw(void);
1211
1212/* Call to query WLSW state and update all rfkill switches */
1213static bool tpacpi_rfk_check_hwblock_state(void)
1214{
1215 int res = hotkey_get_wlsw();
1216 int hw_blocked;
1217
1218 /* When unknown or unsupported, we have to assume it is unblocked */
1219 if (res < 0)
1220 return false;
1221
1222 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1223 tpacpi_rfk_update_hwblock_state(hw_blocked);
1224
1225 return hw_blocked;
1226}
1227
1228static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1229{
1230 struct tpacpi_rfk *tp_rfk = data;
1231 int res;
1232
1233 dbg_printk(TPACPI_DBG_RFKILL,
1234 "request to change radio state to %s\n",
1235 blocked ? "blocked" : "unblocked");
1236
1237 /* try to set radio state */
1238 res = (tp_rfk->ops->set_status)(blocked ?
1239 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1240
1241 /* and update the rfkill core with whatever the FW really did */
1242 tpacpi_rfk_update_swstate(tp_rfk);
1243
1244 return (res < 0) ? res : 0;
1245}
1246
1247static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1248 .set_block = tpacpi_rfk_hook_set_block,
1249};
1250
1251static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1252 const struct tpacpi_rfk_ops *tp_rfkops,
1253 const enum rfkill_type rfktype,
1254 const char *name,
1255 const bool set_default)
1256{
1257 struct tpacpi_rfk *atp_rfk;
1258 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001259 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001260 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001261 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001262
1263 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1264
Johannes Berg19d337d2009-06-02 13:01:37 +02001265 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1266 if (atp_rfk)
1267 atp_rfk->rfkill = rfkill_alloc(name,
1268 &tpacpi_pdev->dev,
1269 rfktype,
1270 &tpacpi_rfk_rfkill_ops,
1271 atp_rfk);
1272 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001273 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001274 kfree(atp_rfk);
1275 return -ENOMEM;
1276 }
1277
1278 atp_rfk->id = id;
1279 atp_rfk->ops = tp_rfkops;
1280
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001281 sw_status = (tp_rfkops->get_status)();
1282 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001283 pr_err("failed to read initial state for %s, error %d\n",
1284 name, sw_status);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001285 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001286 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001287 if (set_default) {
1288 /* try to keep the initial state, since we ask the
1289 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001290 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001291 }
1292 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001293 hw_state = tpacpi_rfk_check_hwblock_state();
1294 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001295
1296 res = rfkill_register(atp_rfk->rfkill);
1297 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001298 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001299 rfkill_destroy(atp_rfk->rfkill);
1300 kfree(atp_rfk);
1301 return res;
1302 }
1303
1304 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001305
Joe Perches0978e012011-04-04 10:06:25 -07001306 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001307 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001308 return 0;
1309}
1310
1311static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1312{
1313 struct tpacpi_rfk *tp_rfk;
1314
1315 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1316
1317 tp_rfk = tpacpi_rfkill_switches[id];
1318 if (tp_rfk) {
1319 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001320 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001321 tpacpi_rfkill_switches[id] = NULL;
1322 kfree(tp_rfk);
1323 }
1324}
1325
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001326static void printk_deprecated_rfkill_attribute(const char * const what)
1327{
1328 printk_deprecated_attribute(what,
1329 "Please switch to generic rfkill before year 2010");
1330}
1331
Johannes Berg19d337d2009-06-02 13:01:37 +02001332/* sysfs <radio> enable ------------------------------------------------ */
1333static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1334 struct device_attribute *attr,
1335 char *buf)
1336{
1337 int status;
1338
1339 printk_deprecated_rfkill_attribute(attr->attr.name);
1340
1341 /* This is in the ABI... */
1342 if (tpacpi_rfk_check_hwblock_state()) {
1343 status = TPACPI_RFK_RADIO_OFF;
1344 } else {
1345 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1346 if (status < 0)
1347 return status;
1348 }
1349
1350 return snprintf(buf, PAGE_SIZE, "%d\n",
1351 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1352}
1353
1354static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1355 struct device_attribute *attr,
1356 const char *buf, size_t count)
1357{
1358 unsigned long t;
1359 int res;
1360
1361 printk_deprecated_rfkill_attribute(attr->attr.name);
1362
1363 if (parse_strtoul(buf, 1, &t))
1364 return -EINVAL;
1365
1366 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1367
1368 /* This is in the ABI... */
1369 if (tpacpi_rfk_check_hwblock_state() && !!t)
1370 return -EPERM;
1371
1372 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1373 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1374 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1375
1376 return (res < 0) ? res : count;
1377}
1378
1379/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001380static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001381{
Johannes Berg19d337d2009-06-02 13:01:37 +02001382 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001383 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001384 else {
1385 int status;
1386
1387 /* This is in the ABI... */
1388 if (tpacpi_rfk_check_hwblock_state()) {
1389 status = TPACPI_RFK_RADIO_OFF;
1390 } else {
1391 status = tpacpi_rfk_update_swstate(
1392 tpacpi_rfkill_switches[id]);
1393 if (status < 0)
1394 return status;
1395 }
1396
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001397 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001398 (status == TPACPI_RFK_RADIO_ON) ?
1399 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001400 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001401 }
1402
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001403 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001404}
1405
1406static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1407{
1408 char *cmd;
1409 int status = -1;
1410 int res = 0;
1411
1412 if (id >= TPACPI_RFK_SW_MAX)
1413 return -ENODEV;
1414
1415 while ((cmd = next_cmd(&buf))) {
1416 if (strlencmp(cmd, "enable") == 0)
1417 status = TPACPI_RFK_RADIO_ON;
1418 else if (strlencmp(cmd, "disable") == 0)
1419 status = TPACPI_RFK_RADIO_OFF;
1420 else
1421 return -EINVAL;
1422 }
1423
1424 if (status != -1) {
1425 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1426 (status == TPACPI_RFK_RADIO_ON) ?
1427 "enable" : "disable",
1428 tpacpi_rfkill_names[id]);
1429 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1430 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1431 }
1432
1433 return res;
1434}
1435
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001436/*************************************************************************
1437 * thinkpad-acpi driver attributes
1438 */
1439
1440/* interface_version --------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001441static ssize_t interface_version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001442{
1443 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1444}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001445static DRIVER_ATTR_RO(interface_version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001446
1447/* debug_level --------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001448static ssize_t debug_level_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001449{
1450 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1451}
1452
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001453static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1454 size_t count)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001455{
1456 unsigned long t;
1457
1458 if (parse_strtoul(buf, 0xffff, &t))
1459 return -EINVAL;
1460
1461 dbg_level = t;
1462
1463 return count;
1464}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001465static DRIVER_ATTR_RW(debug_level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001466
1467/* version ------------------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001468static ssize_t version_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001469{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001470 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1471 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001472}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001473static DRIVER_ATTR_RO(version);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001474
1475/* --------------------------------------------------------------------- */
1476
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001477#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1478
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001479/* wlsw_emulstate ------------------------------------------------------ */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001480static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001481{
1482 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1483}
1484
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001485static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1486 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001487{
1488 unsigned long t;
1489
1490 if (parse_strtoul(buf, 1, &t))
1491 return -EINVAL;
1492
Johannes Berg19d337d2009-06-02 13:01:37 +02001493 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001494 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001495 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1496 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001497
1498 return count;
1499}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001500static DRIVER_ATTR_RW(wlsw_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001501
1502/* bluetooth_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001503static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001504{
1505 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1506}
1507
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001508static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1509 const char *buf, size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001510{
1511 unsigned long t;
1512
1513 if (parse_strtoul(buf, 1, &t))
1514 return -EINVAL;
1515
1516 tpacpi_bluetooth_emulstate = !!t;
1517
1518 return count;
1519}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001520static DRIVER_ATTR_RW(bluetooth_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001521
1522/* wwan_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001523static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001524{
1525 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1526}
1527
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001528static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1529 size_t count)
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001530{
1531 unsigned long t;
1532
1533 if (parse_strtoul(buf, 1, &t))
1534 return -EINVAL;
1535
1536 tpacpi_wwan_emulstate = !!t;
1537
1538 return count;
1539}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001540static DRIVER_ATTR_RW(wwan_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001541
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001542/* uwb_emulstate ------------------------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001543static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001544{
1545 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1546}
1547
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001548static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1549 size_t count)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001550{
1551 unsigned long t;
1552
1553 if (parse_strtoul(buf, 1, &t))
1554 return -EINVAL;
1555
1556 tpacpi_uwb_emulstate = !!t;
1557
1558 return count;
1559}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02001560static DRIVER_ATTR_RW(uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001561#endif
1562
1563/* --------------------------------------------------------------------- */
1564
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001565static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001566 &driver_attr_debug_level, &driver_attr_version,
1567 &driver_attr_interface_version,
1568};
1569
1570static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1571{
1572 int i, res;
1573
1574 i = 0;
1575 res = 0;
1576 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1577 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1578 i++;
1579 }
1580
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001581#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1582 if (!res && dbg_wlswemul)
1583 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1584 if (!res && dbg_bluetoothemul)
1585 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1586 if (!res && dbg_wwanemul)
1587 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001588 if (!res && dbg_uwbemul)
1589 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001590#endif
1591
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001592 return res;
1593}
1594
1595static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1596{
1597 int i;
1598
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001599 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001600 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001601
1602#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1603 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1604 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1605 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001606 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001607#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001608}
1609
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001610/*************************************************************************
1611 * Firmware Data
1612 */
1613
1614/*
1615 * Table of recommended minimum BIOS versions
1616 *
1617 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001618 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001619 * bugs and bad ACPI behaviour on older versions
1620 *
1621 * 2. BIOS or EC fw with known bugs that trigger on Linux
1622 *
1623 * 3. BIOS with known reduced functionality in older versions
1624 *
1625 * We recommend the latest BIOS and EC version.
1626 * We only support the latest BIOS and EC fw version as a rule.
1627 *
1628 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1629 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001630 *
1631 * WARNING: we use this table also to detect that the machine is
1632 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001633 */
1634
1635#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1636 { .vendor = (__v), \
1637 .bios = TPID(__id1, __id2), \
1638 .ec = TPACPI_MATCH_ANY, \
1639 .quirks = TPACPI_MATCH_ANY << 16 \
1640 | (__bv1) << 8 | (__bv2) }
1641
1642#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001643 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001644 { .vendor = (__v), \
1645 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001646 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001647 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1648 | (__bv1) << 8 | (__bv2) }
1649
1650#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1651 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1652
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001653/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001654#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1655 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001656 __bv1, __bv2, TPID(__id1, __id2), \
1657 __ev1, __ev2), \
1658 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1659 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1660 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001661
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001662/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001663#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1664 __eid1, __eid2, __ev1, __ev2) \
1665 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001666 __bv1, __bv2, TPID(__eid1, __eid2), \
1667 __ev1, __ev2), \
1668 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1669 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1670 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001671
1672#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1673 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1674
1675#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1676 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001677 __bv1, __bv2, TPID(__id1, __id2), \
1678 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001679
1680#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1681 __eid1, __eid2, __ev1, __ev2) \
1682 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001683 __bv1, __bv2, TPID(__eid1, __eid2), \
1684 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001685
1686static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1687 /* Numeric models ------------------ */
1688 /* FW MODEL BIOS VERS */
1689 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1690 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1691 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1692 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1693 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1694 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1695 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1696 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1697 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1698
1699 /* A-series ------------------------- */
1700 /* FW MODEL BIOS VERS EC VERS */
1701 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1702 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1703 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1704 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1705 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1706 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1707 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1708 TPV_QI0('1', '3', '2', '0'), /* A22m */
1709 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1710 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1711 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1712
1713 /* G-series ------------------------- */
1714 /* FW MODEL BIOS VERS */
1715 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1716 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1717
1718 /* R-series, T-series --------------- */
1719 /* FW MODEL BIOS VERS EC VERS */
1720 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1721 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1722 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1723 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1724 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1725 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1726 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1727 T40/p, T41/p, T42/p (1) */
1728 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1729 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1730 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1731 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1732
1733 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1734 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1735 TPV_QI0('1', '6', '3', '2'), /* T22 */
1736 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1737 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1738 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1739
1740 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1741 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001742 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001743
1744 /* BIOS FW BIOS VERS EC FW EC VERS */
1745 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1746 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1747
1748 /* X-series ------------------------- */
1749 /* FW MODEL BIOS VERS EC VERS */
1750 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1751 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1752 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1753 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1754 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1755 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1756 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1757
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001758 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1759 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001760
1761 /* (0) - older versions lack DMI EC fw string and functionality */
1762 /* (1) - older versions known to lack functionality */
1763};
1764
1765#undef TPV_QL1
1766#undef TPV_QL0
1767#undef TPV_QI2
1768#undef TPV_QI1
1769#undef TPV_QI0
1770#undef TPV_Q_X
1771#undef TPV_Q
1772
1773static void __init tpacpi_check_outdated_fw(void)
1774{
1775 unsigned long fwvers;
1776 u16 ec_version, bios_version;
1777
1778 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1779 ARRAY_SIZE(tpacpi_bios_version_qtable));
1780
1781 if (!fwvers)
1782 return;
1783
1784 bios_version = fwvers & 0xffffU;
1785 ec_version = (fwvers >> 16) & 0xffffU;
1786
1787 /* note that unknown versions are set to 0x0000 and we use that */
1788 if ((bios_version > thinkpad_id.bios_release) ||
1789 (ec_version > thinkpad_id.ec_release &&
1790 ec_version != TPACPI_MATCH_ANY)) {
1791 /*
1792 * The changelogs would let us track down the exact
1793 * reason, but it is just too much of a pain to track
1794 * it. We only list BIOSes that are either really
1795 * broken, or really stable to begin with, so it is
1796 * best if the user upgrades the firmware anyway.
1797 */
Joe Perches0978e012011-04-04 10:06:25 -07001798 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1799 pr_warn("WARNING: This firmware may be missing critical bug "
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001800 "fixes and/or important features\n");
1801 }
1802}
1803
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001804static bool __init tpacpi_is_fw_known(void)
1805{
1806 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1807 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1808}
1809
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001810/****************************************************************************
1811 ****************************************************************************
1812 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001813 * Subdrivers
1814 *
1815 ****************************************************************************
1816 ****************************************************************************/
1817
1818/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001819 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001820 */
1821
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001822static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001824 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1825 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001829static struct ibm_struct thinkpad_acpi_driver_data = {
1830 .name = "driver",
1831 .read = thinkpad_acpi_driver_read,
1832};
1833
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001834/*************************************************************************
1835 * Hotkey subdriver
1836 */
1837
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001838/*
1839 * ThinkPad firmware event model
1840 *
1841 * The ThinkPad firmware has two main event interfaces: normal ACPI
1842 * notifications (which follow the ACPI standard), and a private event
1843 * interface.
1844 *
1845 * The private event interface also issues events for the hotkeys. As
1846 * the driver gained features, the event handling code ended up being
1847 * built around the hotkey subdriver. This will need to be refactored
1848 * to a more formal event API eventually.
1849 *
1850 * Some "hotkeys" are actually supposed to be used as event reports,
1851 * such as "brightness has changed", "volume has changed", depending on
1852 * the ThinkPad model and how the firmware is operating.
1853 *
1854 * Unlike other classes, hotkey-class events have mask/unmask control on
1855 * non-ancient firmware. However, how it behaves changes a lot with the
1856 * firmware model and version.
1857 */
1858
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001859enum { /* hot key scan codes (derived from ACPI DSDT) */
1860 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1861 TP_ACPI_HOTKEYSCAN_FNF2,
1862 TP_ACPI_HOTKEYSCAN_FNF3,
1863 TP_ACPI_HOTKEYSCAN_FNF4,
1864 TP_ACPI_HOTKEYSCAN_FNF5,
1865 TP_ACPI_HOTKEYSCAN_FNF6,
1866 TP_ACPI_HOTKEYSCAN_FNF7,
1867 TP_ACPI_HOTKEYSCAN_FNF8,
1868 TP_ACPI_HOTKEYSCAN_FNF9,
1869 TP_ACPI_HOTKEYSCAN_FNF10,
1870 TP_ACPI_HOTKEYSCAN_FNF11,
1871 TP_ACPI_HOTKEYSCAN_FNF12,
1872 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1873 TP_ACPI_HOTKEYSCAN_FNINSERT,
1874 TP_ACPI_HOTKEYSCAN_FNDELETE,
1875 TP_ACPI_HOTKEYSCAN_FNHOME,
1876 TP_ACPI_HOTKEYSCAN_FNEND,
1877 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1878 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1879 TP_ACPI_HOTKEYSCAN_FNSPACE,
1880 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1881 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1882 TP_ACPI_HOTKEYSCAN_MUTE,
1883 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001884 TP_ACPI_HOTKEYSCAN_UNK1,
1885 TP_ACPI_HOTKEYSCAN_UNK2,
1886 TP_ACPI_HOTKEYSCAN_UNK3,
1887 TP_ACPI_HOTKEYSCAN_UNK4,
1888 TP_ACPI_HOTKEYSCAN_UNK5,
1889 TP_ACPI_HOTKEYSCAN_UNK6,
1890 TP_ACPI_HOTKEYSCAN_UNK7,
1891 TP_ACPI_HOTKEYSCAN_UNK8,
1892
Christian Kellner149c8c72017-02-28 17:10:56 +01001893 /* Adaptive keyboard keycodes */
1894 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1895 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
Bastien Nocera6a68d852015-03-02 14:45:31 +01001896 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1897 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1898 TP_ACPI_HOTKEYSCAN_CLOUD,
1899 TP_ACPI_HOTKEYSCAN_UNK9,
1900 TP_ACPI_HOTKEYSCAN_VOICE,
1901 TP_ACPI_HOTKEYSCAN_UNK10,
1902 TP_ACPI_HOTKEYSCAN_GESTURES,
1903 TP_ACPI_HOTKEYSCAN_UNK11,
1904 TP_ACPI_HOTKEYSCAN_UNK12,
1905 TP_ACPI_HOTKEYSCAN_UNK13,
1906 TP_ACPI_HOTKEYSCAN_CONFIG,
1907 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1908 TP_ACPI_HOTKEYSCAN_RELOAD,
1909 TP_ACPI_HOTKEYSCAN_BACK,
1910 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1911 TP_ACPI_HOTKEYSCAN_MIC_UP,
1912 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1913 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1914 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1915
Christian Kellner696c65232017-02-28 17:10:57 +01001916 /* Lenovo extended keymap, starting at 0x1300 */
1917 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1918 /* first new observed key (star, favorites) is 0x1311 */
1919 TP_ACPI_HOTKEYSCAN_STAR = 69,
1920 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1921 TP_ACPI_HOTKEYSCAN_UNK25,
1922 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1923 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1924
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001925 /* Hotkey keymap size */
1926 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001927};
1928
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001929enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001930 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1931 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1932};
1933
1934enum { /* Positions of some of the keys in hotkey masks */
1935 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1936 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1937 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1938 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1939 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
Hans de Goedec685e202017-02-09 16:44:13 +01001940 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001941 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1942 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1943 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1944 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1945 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1946};
1947
1948enum { /* NVRAM to ACPI HKEY group map */
1949 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1950 TP_ACPI_HKEY_ZOOM_MASK |
1951 TP_ACPI_HKEY_DISPSWTCH_MASK |
1952 TP_ACPI_HKEY_HIBERNATE_MASK,
1953 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1954 TP_ACPI_HKEY_BRGHTDWN_MASK,
1955 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1956 TP_ACPI_HKEY_VOLDWN_MASK |
1957 TP_ACPI_HKEY_MUTE_MASK,
1958};
1959
1960#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1961struct tp_nvram_state {
1962 u16 thinkpad_toggle:1;
1963 u16 zoom_toggle:1;
1964 u16 display_toggle:1;
1965 u16 thinklight_toggle:1;
1966 u16 hibernate_toggle:1;
1967 u16 displayexp_toggle:1;
1968 u16 display_state:1;
1969 u16 brightness_toggle:1;
1970 u16 volume_toggle:1;
1971 u16 mute:1;
1972
1973 u8 brightness_level;
1974 u8 volume_level;
1975};
1976
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001977/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001978static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001979
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001980/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001981 * Acquire mutex to write poller control variables as an
1982 * atomic block.
1983 *
1984 * Increment hotkey_config_change when changing them if you
1985 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001986 *
1987 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1988 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001989static struct mutex hotkey_thread_data_mutex;
1990static unsigned int hotkey_config_change;
1991
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001992/*
1993 * hotkey poller control variables
1994 *
1995 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001996 *
1997 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1998 * should be used only when the changes need to be taken as
1999 * a block, OR when one needs to force the kthread to forget
2000 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002001 */
2002static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2003static unsigned int hotkey_poll_freq = 10; /* Hz */
2004
2005#define HOTKEY_CONFIG_CRITICAL_START \
2006 do { \
2007 mutex_lock(&hotkey_thread_data_mutex); \
2008 hotkey_config_change++; \
2009 } while (0);
2010#define HOTKEY_CONFIG_CRITICAL_END \
2011 mutex_unlock(&hotkey_thread_data_mutex);
2012
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002013#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2014
2015#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002016#define HOTKEY_CONFIG_CRITICAL_START
2017#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002018
2019#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2020
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002021static struct mutex hotkey_mutex;
2022
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002023static enum { /* Reasons for waking up */
2024 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2025 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2026 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2027} hotkey_wakeup_reason;
2028
2029static int hotkey_autosleep_ack;
2030
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002031static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2032static u32 hotkey_all_mask; /* all events supported in fw */
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002033static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002034static u32 hotkey_reserved_mask; /* events better left disabled */
2035static u32 hotkey_driver_mask; /* events needed by the driver */
2036static u32 hotkey_user_mask; /* events visible to userspace */
2037static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002038
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002039static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002040
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002041static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002042
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002043static void tpacpi_driver_event(const unsigned int hkey_event);
2044static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002045static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002046
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002047/* HKEY.MHKG() return bits */
2048#define TP_HOTKEY_TABLET_MASK (1 << 3)
Lyudeb03f4d42016-11-11 15:15:03 -05002049/* ThinkPad X1 Yoga (2016) */
2050#define TP_EC_CMMD_TABLET_MODE 0x6
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002051
Johannes Berg19d337d2009-06-02 13:01:37 +02002052static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002053{
Johannes Berg19d337d2009-06-02 13:01:37 +02002054 int status;
2055
2056 if (!tp_features.hotkey_wlsw)
2057 return -ENODEV;
2058
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002059#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002060 if (dbg_wlswemul)
2061 return (tpacpi_wlsw_emulstate) ?
2062 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002063#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002064
2065 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002066 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002067
2068 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002069}
2070
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002071static int hotkey_get_tablet_mode(int *status)
2072{
2073 int s;
2074
Lyudeb03f4d42016-11-11 15:15:03 -05002075 switch (tp_features.hotkey_tablet) {
2076 case TP_HOTKEY_TABLET_USES_MHKG:
2077 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2078 return -EIO;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002079
Lyudeb03f4d42016-11-11 15:15:03 -05002080 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2081 break;
2082 case TP_HOTKEY_TABLET_USES_CMMD:
2083 if (!acpi_evalf(ec_handle, &s, "CMMD", "d"))
2084 return -EIO;
2085
2086 *status = (s == TP_EC_CMMD_TABLET_MODE);
2087 break;
2088 default:
2089 break;
2090 }
2091
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002092 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002093}
2094
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002095/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002096 * Reads current event mask from firmware, and updates
2097 * hotkey_acpi_mask accordingly. Also resets any bits
2098 * from hotkey_user_mask that are unavailable to be
2099 * delivered (shadow requirement of the userspace ABI).
2100 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002101 * Call with hotkey_mutex held
2102 */
2103static int hotkey_mask_get(void)
2104{
2105 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002106 u32 m = 0;
2107
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002108 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002109 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002110
2111 hotkey_acpi_mask = m;
2112 } else {
2113 /* no mask support doesn't mean no event support... */
2114 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002115 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002116
2117 /* sync userspace-visible mask */
2118 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002119
2120 return 0;
2121}
2122
Jean Delvaredf6dd1b2015-04-27 09:45:06 +02002123static void hotkey_mask_warn_incomplete_mask(void)
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002124{
2125 /* log only what the user can fix... */
2126 const u32 wantedmask = hotkey_driver_mask &
2127 ~(hotkey_acpi_mask | hotkey_source_mask) &
2128 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2129
2130 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002131 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002132}
2133
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002134/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002135 * Set the firmware mask when supported
2136 *
2137 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2138 *
2139 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2140 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002141 * Call with hotkey_mutex held
2142 */
2143static int hotkey_mask_set(u32 mask)
2144{
2145 int i;
2146 int rc = 0;
2147
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002148 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002149
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002150 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002151 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002152 if (!acpi_evalf(hkey_handle,
2153 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002154 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002155 rc = -EIO;
2156 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002157 }
2158 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002159 }
2160
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002161 /*
2162 * We *must* make an inconditional call to hotkey_mask_get to
2163 * refresh hotkey_acpi_mask and update hotkey_user_mask
2164 *
2165 * Take the opportunity to also log when we cannot _enable_
2166 * a given event.
2167 */
2168 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Joe Perches0978e012011-04-04 10:06:25 -07002169 pr_notice("asked for hotkey mask 0x%08x, but "
2170 "firmware forced it to 0x%08x\n",
2171 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002172 }
2173
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002174 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2175 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002176
2177 return rc;
2178}
2179
2180/*
2181 * Sets hotkey_user_mask and tries to set the firmware mask
2182 *
2183 * Call with hotkey_mutex held
2184 */
2185static int hotkey_user_mask_set(const u32 mask)
2186{
2187 int rc;
2188
2189 /* Give people a chance to notice they are doing something that
2190 * is bound to go boom on their users sooner or later */
2191 if (!tp_warned.hotkey_mask_ff &&
2192 (mask == 0xffff || mask == 0xffffff ||
2193 mask == 0xffffffff)) {
2194 tp_warned.hotkey_mask_ff = 1;
Joe Perches0978e012011-04-04 10:06:25 -07002195 pr_notice("setting the hotkey mask to 0x%08x is likely "
2196 "not the best way to go about it\n", mask);
2197 pr_notice("please consider using the driver defaults, "
2198 "and refer to up-to-date thinkpad-acpi "
2199 "documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002200 }
2201
2202 /* Try to enable what the user asked for, plus whatever we need.
2203 * this syncs everything but won't enable bits in hotkey_user_mask */
2204 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2205
2206 /* Enable the available bits in hotkey_user_mask */
2207 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2208
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002209 return rc;
2210}
2211
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002212/*
2213 * Sets the driver hotkey mask.
2214 *
2215 * Can be called even if the hotkey subdriver is inactive
2216 */
2217static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2218{
2219 int rc;
2220
2221 /* Do the right thing if hotkey_init has not been called yet */
2222 if (!tp_features.hotkey) {
2223 hotkey_driver_mask = mask;
2224 return 0;
2225 }
2226
2227 mutex_lock(&hotkey_mutex);
2228
2229 HOTKEY_CONFIG_CRITICAL_START
2230 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002231#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002232 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002233#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002234 HOTKEY_CONFIG_CRITICAL_END
2235
2236 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2237 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002238 hotkey_poll_setup(true);
2239
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002240 mutex_unlock(&hotkey_mutex);
2241
2242 return rc;
2243}
2244
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002245static int hotkey_status_get(int *status)
2246{
2247 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2248 return -EIO;
2249
2250 return 0;
2251}
2252
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002253static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002254{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002255 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002256 return -EIO;
2257
2258 return 0;
2259}
2260
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002261static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002262{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002263 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002264
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002265 if (tp_features.hotkey_tablet &&
2266 !hotkey_get_tablet_mode(&state)) {
2267 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002268
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002269 input_report_switch(tpacpi_inputdev,
2270 SW_TABLET_MODE, !!state);
2271 input_sync(tpacpi_inputdev);
2272
2273 mutex_unlock(&tpacpi_inputdev_send_mutex);
2274 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002275}
2276
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002277/* Do NOT call without validating scancode first */
2278static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002279{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002280 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002281
2282 if (keycode != KEY_RESERVED) {
2283 mutex_lock(&tpacpi_inputdev_send_mutex);
2284
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002285 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002286 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002287 input_sync(tpacpi_inputdev);
2288
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002289 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002290 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002291 input_sync(tpacpi_inputdev);
2292
2293 mutex_unlock(&tpacpi_inputdev_send_mutex);
2294 }
2295}
2296
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002297/* Do NOT call without validating scancode first */
2298static void tpacpi_input_send_key_masked(const unsigned int scancode)
2299{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002300 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002301 if (hotkey_user_mask & (1 << scancode))
2302 tpacpi_input_send_key(scancode);
2303}
2304
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002305#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2306static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2307
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002308/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002309static void tpacpi_hotkey_send_key(unsigned int scancode)
2310{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002311 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002312}
2313
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002314static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002315{
2316 u8 d;
2317
2318 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2319 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2320 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2321 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2322 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2323 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2324 }
Hans de Goedec685e202017-02-09 16:44:13 +01002325 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002326 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2327 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2328 }
2329 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2330 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2331 n->displayexp_toggle =
2332 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2333 }
2334 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2335 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2336 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2337 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2338 n->brightness_toggle =
2339 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2340 }
2341 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2342 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2343 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2344 >> TP_NVRAM_POS_LEVEL_VOLUME;
2345 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2346 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2347 }
2348}
2349
Behan Webstera4d44ba2014-02-12 21:58:46 +01002350#define TPACPI_COMPARE_KEY(__scancode, __member) \
2351do { \
2352 if ((event_mask & (1 << __scancode)) && \
2353 oldn->__member != newn->__member) \
2354 tpacpi_hotkey_send_key(__scancode); \
2355} while (0)
2356
2357#define TPACPI_MAY_SEND_KEY(__scancode) \
2358do { \
2359 if (event_mask & (1 << __scancode)) \
2360 tpacpi_hotkey_send_key(__scancode); \
2361} while (0)
2362
2363static void issue_volchange(const unsigned int oldvol,
2364 const unsigned int newvol,
2365 const u32 event_mask)
2366{
2367 unsigned int i = oldvol;
2368
2369 while (i > newvol) {
2370 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2371 i--;
2372 }
2373 while (i < newvol) {
2374 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2375 i++;
2376 }
2377}
2378
2379static void issue_brightnesschange(const unsigned int oldbrt,
2380 const unsigned int newbrt,
2381 const u32 event_mask)
2382{
2383 unsigned int i = oldbrt;
2384
2385 while (i > newbrt) {
2386 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2387 i--;
2388 }
2389 while (i < newbrt) {
2390 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2391 i++;
2392 }
2393}
2394
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002395static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2396 struct tp_nvram_state *newn,
2397 const u32 event_mask)
2398{
2399
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002400 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2401 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2402 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2403 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2404
2405 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2406
2407 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2408
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002409 /*
2410 * Handle volume
2411 *
2412 * This code is supposed to duplicate the IBM firmware behaviour:
2413 * - Pressing MUTE issues mute hotkey message, even when already mute
2414 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002415 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002416 * - The act of unmuting issues volume up/down notification,
2417 * depending which key was used to unmute
2418 *
2419 * We are constrained to what the NVRAM can tell us, which is not much
2420 * and certainly not enough if more than one volume hotkey was pressed
2421 * since the last poll cycle.
2422 *
2423 * Just to make our life interesting, some newer Lenovo ThinkPads have
2424 * bugs in the BIOS and may fail to update volume_toggle properly.
2425 */
2426 if (newn->mute) {
2427 /* muted */
2428 if (!oldn->mute ||
2429 oldn->volume_toggle != newn->volume_toggle ||
2430 oldn->volume_level != newn->volume_level) {
2431 /* recently muted, or repeated mute keypress, or
2432 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002433 issue_volchange(oldn->volume_level, newn->volume_level,
2434 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002435 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2436 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002437 } else {
2438 /* unmute */
2439 if (oldn->mute) {
2440 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002441 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002442 }
2443 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002444 issue_volchange(oldn->volume_level, newn->volume_level,
2445 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002446 } else if (oldn->volume_toggle != newn->volume_toggle) {
2447 /* repeated vol up/down keypress at end of scale ? */
2448 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002449 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002450 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2451 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002452 }
2453 }
2454
2455 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002456 if (oldn->brightness_level != newn->brightness_level) {
2457 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002458 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002459 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2460 /* repeated key presses that didn't change state */
2461 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002462 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002463 else if (newn->brightness_level >= bright_maxlvl
2464 && !tp_features.bright_unkfw)
2465 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002466 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002467
2468#undef TPACPI_COMPARE_KEY
2469#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002470}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002471
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002472/*
2473 * Polling driver
2474 *
2475 * We track all events in hotkey_source_mask all the time, since
2476 * most of them are edge-based. We only issue those requested by
2477 * hotkey_user_mask or hotkey_driver_mask, though.
2478 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002479static int hotkey_kthread(void *data)
2480{
2481 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002482 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002483 unsigned int si, so;
2484 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002485 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002486 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002487 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002488
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002489 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2490 goto exit;
2491
2492 set_freezable();
2493
2494 so = 0;
2495 si = 1;
2496 t = 0;
2497
2498 /* Initial state for compares */
2499 mutex_lock(&hotkey_thread_data_mutex);
2500 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002501 poll_mask = hotkey_source_mask;
2502 event_mask = hotkey_source_mask &
2503 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002504 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002505 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002506 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002507
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002508 while (!kthread_should_stop()) {
2509 if (t == 0) {
2510 if (likely(poll_freq))
2511 t = 1000/poll_freq;
2512 else
2513 t = 100; /* should never happen... */
2514 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002515 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002516 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002517 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002518
2519 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002520 continue;
2521
2522 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002523 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002524 /* forget old state on thaw or config change */
2525 si = so;
2526 t = 0;
2527 change_detector = hotkey_config_change;
2528 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002529 poll_mask = hotkey_source_mask;
2530 event_mask = hotkey_source_mask &
2531 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002532 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002533 mutex_unlock(&hotkey_thread_data_mutex);
2534
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002535 if (likely(poll_mask)) {
2536 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002537 if (likely(si != so)) {
2538 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002539 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002540 }
2541 }
2542
2543 so = si;
2544 si ^= 1;
2545 }
2546
2547exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002548 return 0;
2549}
2550
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002551/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002552static void hotkey_poll_stop_sync(void)
2553{
2554 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002555 kthread_stop(tpacpi_hotkey_task);
2556 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002557 }
2558}
2559
2560/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002561static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002562{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002563 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2564 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002565
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002566 if (hotkey_poll_freq > 0 &&
2567 (poll_driver_mask ||
2568 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002569 if (!tpacpi_hotkey_task) {
2570 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002571 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002572 if (IS_ERR(tpacpi_hotkey_task)) {
2573 tpacpi_hotkey_task = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07002574 pr_err("could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002575 "for hotkey polling\n");
2576 }
2577 }
2578 } else {
2579 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002580 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002581 hotkey_poll_freq == 0) {
Joe Perches0978e012011-04-04 10:06:25 -07002582 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2583 "require polling, which is currently "
2584 "disabled\n",
2585 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002586 }
2587 }
2588}
2589
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002590static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002591{
2592 mutex_lock(&hotkey_mutex);
2593 hotkey_poll_setup(may_warn);
2594 mutex_unlock(&hotkey_mutex);
2595}
2596
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002597/* call with hotkey_mutex held */
2598static void hotkey_poll_set_freq(unsigned int freq)
2599{
2600 if (!freq)
2601 hotkey_poll_stop_sync();
2602
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002603 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002604}
2605
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002606#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2607
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002608static void hotkey_poll_setup(const bool __unused)
2609{
2610}
2611
2612static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002613{
2614}
2615
2616#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2617
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002618static int hotkey_inputdev_open(struct input_dev *dev)
2619{
2620 switch (tpacpi_lifecycle) {
2621 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002622 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002623 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002624 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002625 case TPACPI_LIFE_EXITING:
2626 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002627 }
2628
2629 /* Should only happen if tpacpi_lifecycle is corrupt */
2630 BUG();
2631 return -EBUSY;
2632}
2633
2634static void hotkey_inputdev_close(struct input_dev *dev)
2635{
2636 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002637 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002638 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002639 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002640}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002641
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002642/* sysfs hotkey enable ------------------------------------------------- */
2643static ssize_t hotkey_enable_show(struct device *dev,
2644 struct device_attribute *attr,
2645 char *buf)
2646{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002647 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002648
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002649 printk_deprecated_attribute("hotkey_enable",
2650 "Hotkey reporting is always enabled");
2651
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002652 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002653 if (res)
2654 return res;
2655
2656 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2657}
2658
2659static ssize_t hotkey_enable_store(struct device *dev,
2660 struct device_attribute *attr,
2661 const char *buf, size_t count)
2662{
2663 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002664
2665 printk_deprecated_attribute("hotkey_enable",
2666 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002667
2668 if (parse_strtoul(buf, 1, &t))
2669 return -EINVAL;
2670
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002671 if (t == 0)
2672 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002673
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002674 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002675}
2676
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002677static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002678
2679/* sysfs hotkey mask --------------------------------------------------- */
2680static ssize_t hotkey_mask_show(struct device *dev,
2681 struct device_attribute *attr,
2682 char *buf)
2683{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002684 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002685}
2686
2687static ssize_t hotkey_mask_store(struct device *dev,
2688 struct device_attribute *attr,
2689 const char *buf, size_t count)
2690{
2691 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002692 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002693
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002694 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002695 return -EINVAL;
2696
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002697 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002698 return -ERESTARTSYS;
2699
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002700 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002701
2702#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002703 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002704#endif
2705
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002706 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002707
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002708 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2709
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002710 return (res) ? res : count;
2711}
2712
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002713static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002714
2715/* sysfs hotkey bios_enabled ------------------------------------------- */
2716static ssize_t hotkey_bios_enabled_show(struct device *dev,
2717 struct device_attribute *attr,
2718 char *buf)
2719{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002720 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002721}
2722
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002723static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002724
2725/* sysfs hotkey bios_mask ---------------------------------------------- */
2726static ssize_t hotkey_bios_mask_show(struct device *dev,
2727 struct device_attribute *attr,
2728 char *buf)
2729{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002730 printk_deprecated_attribute("hotkey_bios_mask",
2731 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002732 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002733}
2734
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002735static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002736
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002737/* sysfs hotkey all_mask ----------------------------------------------- */
2738static ssize_t hotkey_all_mask_show(struct device *dev,
2739 struct device_attribute *attr,
2740 char *buf)
2741{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002742 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2743 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002744}
2745
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002746static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002747
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002748/* sysfs hotkey all_mask ----------------------------------------------- */
2749static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2750 struct device_attribute *attr,
2751 char *buf)
2752{
2753 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2754 hotkey_adaptive_all_mask | hotkey_source_mask);
2755}
2756
2757static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2758
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002759/* sysfs hotkey recommended_mask --------------------------------------- */
2760static ssize_t hotkey_recommended_mask_show(struct device *dev,
2761 struct device_attribute *attr,
2762 char *buf)
2763{
2764 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002765 (hotkey_all_mask | hotkey_source_mask)
2766 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002767}
2768
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002769static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002770
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002771#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2772
2773/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2774static ssize_t hotkey_source_mask_show(struct device *dev,
2775 struct device_attribute *attr,
2776 char *buf)
2777{
2778 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2779}
2780
2781static ssize_t hotkey_source_mask_store(struct device *dev,
2782 struct device_attribute *attr,
2783 const char *buf, size_t count)
2784{
2785 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002786 u32 r_ev;
2787 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002788
2789 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2790 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2791 return -EINVAL;
2792
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002793 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002794 return -ERESTARTSYS;
2795
2796 HOTKEY_CONFIG_CRITICAL_START
2797 hotkey_source_mask = t;
2798 HOTKEY_CONFIG_CRITICAL_END
2799
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002800 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2801 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002802 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002803
2804 /* check if events needed by the driver got disabled */
2805 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2806 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002807
2808 mutex_unlock(&hotkey_mutex);
2809
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002810 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07002811 pr_err("hotkey_source_mask: "
2812 "failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002813
2814 if (r_ev)
Joe Perches0978e012011-04-04 10:06:25 -07002815 pr_notice("hotkey_source_mask: "
2816 "some important events were disabled: 0x%04x\n",
2817 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002818
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002819 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2820
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002821 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002822}
2823
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002824static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002825
2826/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2827static ssize_t hotkey_poll_freq_show(struct device *dev,
2828 struct device_attribute *attr,
2829 char *buf)
2830{
2831 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2832}
2833
2834static ssize_t hotkey_poll_freq_store(struct device *dev,
2835 struct device_attribute *attr,
2836 const char *buf, size_t count)
2837{
2838 unsigned long t;
2839
2840 if (parse_strtoul(buf, 25, &t))
2841 return -EINVAL;
2842
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002843 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002844 return -ERESTARTSYS;
2845
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002846 hotkey_poll_set_freq(t);
2847 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002848
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002849 mutex_unlock(&hotkey_mutex);
2850
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002851 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2852
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002853 return count;
2854}
2855
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002856static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002857
2858#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2859
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002860/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002861static ssize_t hotkey_radio_sw_show(struct device *dev,
2862 struct device_attribute *attr,
2863 char *buf)
2864{
Johannes Berg19d337d2009-06-02 13:01:37 +02002865 int res;
2866 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002867 if (res < 0)
2868 return res;
2869
Johannes Berg19d337d2009-06-02 13:01:37 +02002870 /* Opportunistic update */
2871 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2872
2873 return snprintf(buf, PAGE_SIZE, "%d\n",
2874 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002875}
2876
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002877static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002878
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002879static void hotkey_radio_sw_notify_change(void)
2880{
2881 if (tp_features.hotkey_wlsw)
2882 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2883 "hotkey_radio_sw");
2884}
2885
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002886/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2887static ssize_t hotkey_tablet_mode_show(struct device *dev,
2888 struct device_attribute *attr,
2889 char *buf)
2890{
2891 int res, s;
2892 res = hotkey_get_tablet_mode(&s);
2893 if (res < 0)
2894 return res;
2895
2896 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2897}
2898
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002899static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002900
2901static void hotkey_tablet_mode_notify_change(void)
2902{
2903 if (tp_features.hotkey_tablet)
2904 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2905 "hotkey_tablet_mode");
2906}
2907
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002908/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002909static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2910 struct device_attribute *attr,
2911 char *buf)
2912{
2913 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2914}
2915
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002916static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002917
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002918static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002919{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002920 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2921 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002922}
2923
2924/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002925static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2926 struct device_attribute *attr,
2927 char *buf)
2928{
2929 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2930}
2931
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002932static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2933 hotkey_wakeup_hotunplug_complete_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002934
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002935static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002936{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002937 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2938 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002939}
2940
Bastien Nocerab790cee2015-03-02 14:45:27 +01002941/* sysfs adaptive kbd mode --------------------------------------------- */
2942
2943static int adaptive_keyboard_get_mode(void);
2944static int adaptive_keyboard_set_mode(int new_mode);
2945
2946enum ADAPTIVE_KEY_MODE {
2947 HOME_MODE,
2948 WEB_BROWSER_MODE,
2949 WEB_CONFERENCE_MODE,
2950 FUNCTION_MODE,
2951 LAYFLAT_MODE
2952};
2953
2954static ssize_t adaptive_kbd_mode_show(struct device *dev,
2955 struct device_attribute *attr,
2956 char *buf)
2957{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03002958 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01002959
2960 current_mode = adaptive_keyboard_get_mode();
2961 if (current_mode < 0)
2962 return current_mode;
2963
2964 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
2965}
2966
2967static ssize_t adaptive_kbd_mode_store(struct device *dev,
2968 struct device_attribute *attr,
2969 const char *buf, size_t count)
2970{
2971 unsigned long t;
2972 int res;
2973
2974 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2975 return -EINVAL;
2976
2977 res = adaptive_keyboard_set_mode(t);
2978 return (res < 0) ? res : count;
2979}
2980
2981static DEVICE_ATTR_RW(adaptive_kbd_mode);
2982
2983static struct attribute *adaptive_kbd_attributes[] = {
2984 &dev_attr_adaptive_kbd_mode.attr,
2985 NULL
2986};
2987
2988static const struct attribute_group adaptive_kbd_attr_group = {
2989 .attrs = adaptive_kbd_attributes,
2990};
2991
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002992/* --------------------------------------------------------------------- */
2993
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002994static struct attribute *hotkey_attributes[] __initdata = {
2995 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002996 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002997 &dev_attr_hotkey_bios_mask.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002998 &dev_attr_wakeup_reason.attr,
2999 &dev_attr_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003000 &dev_attr_hotkey_mask.attr,
3001 &dev_attr_hotkey_all_mask.attr,
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003002 &dev_attr_hotkey_adaptive_all_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003003 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003004#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003005 &dev_attr_hotkey_source_mask.attr,
3006 &dev_attr_hotkey_poll_freq.attr,
3007#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003008};
3009
Johannes Berg19d337d2009-06-02 13:01:37 +02003010/*
3011 * Sync both the hw and sw blocking state of all switches
3012 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003013static void tpacpi_send_radiosw_update(void)
3014{
3015 int wlsw;
3016
Johannes Berg19d337d2009-06-02 13:01:37 +02003017 /*
3018 * We must sync all rfkill controllers *before* issuing any
3019 * rfkill input events, or we will race the rfkill core input
3020 * handler.
3021 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003022 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003023 * for the above.
3024 *
3025 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3026 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003027
Johannes Berg19d337d2009-06-02 13:01:37 +02003028 wlsw = hotkey_get_wlsw();
3029
3030 /* Sync hw blocking state first if it is hw-blocked */
3031 if (wlsw == TPACPI_RFK_RADIO_OFF)
3032 tpacpi_rfk_update_hwblock_state(true);
3033
3034 /* Sync sw blocking state */
3035 tpacpi_rfk_update_swstate_all();
3036
3037 /* Sync hw blocking state last if it is hw-unblocked */
3038 if (wlsw == TPACPI_RFK_RADIO_ON)
3039 tpacpi_rfk_update_hwblock_state(false);
3040
3041 /* Issue rfkill input event for WLSW switch */
3042 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003043 mutex_lock(&tpacpi_inputdev_send_mutex);
3044
3045 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003046 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003047 input_sync(tpacpi_inputdev);
3048
3049 mutex_unlock(&tpacpi_inputdev_send_mutex);
3050 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003051
3052 /*
3053 * this can be unconditional, as we will poll state again
3054 * if userspace uses the notify to read data
3055 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003056 hotkey_radio_sw_notify_change();
3057}
3058
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003059static void hotkey_exit(void)
3060{
3061#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003062 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003063 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003064 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003065#endif
3066
3067 if (hotkey_dev_attributes)
3068 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3069
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003070 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003071 "restoring original HKEY status and mask\n");
3072 /* yes, there is a bitwise or below, we want the
3073 * functions to be called even if one of them fail */
3074 if (((tp_features.hotkey_mask &&
3075 hotkey_mask_set(hotkey_orig_mask)) |
3076 hotkey_status_set(false)) != 0)
Joe Perches0978e012011-04-04 10:06:25 -07003077 pr_err("failed to restore hot key mask "
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003078 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003079}
3080
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003081static void __init hotkey_unmap(const unsigned int scancode)
3082{
3083 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3084 clear_bit(hotkey_keycode_map[scancode],
3085 tpacpi_inputdev->keybit);
3086 hotkey_keycode_map[scancode] = KEY_RESERVED;
3087 }
3088}
3089
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003090/*
3091 * HKEY quirks:
3092 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3093 */
3094
3095#define TPACPI_HK_Q_INIMASK 0x0001
3096
3097static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3098 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3099 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3100 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3101 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3102 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3103 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3104 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3105 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3106 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3107 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3108 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3109 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3110 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3111 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3112 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3113 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3114 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3115 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3116 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3117};
3118
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003119typedef u16 tpacpi_keymap_entry_t;
3120typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003121
Lyudeb3180022016-11-11 15:15:02 -05003122static int hotkey_init_tablet_mode(void)
3123{
Darren Hartcb2bf252016-12-14 14:45:48 -08003124 int in_tablet_mode = 0, res;
3125 char *type = NULL;
Lyudeb3180022016-11-11 15:15:02 -05003126
Lyudeb3180022016-11-11 15:15:02 -05003127 if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
Lyudeb03f4d42016-11-11 15:15:03 -05003128 /* For X41t, X60t, X61t Tablets... */
Lyudeb3180022016-11-11 15:15:02 -05003129 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3130 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3131 type = "MHKG";
Lyudeb03f4d42016-11-11 15:15:03 -05003132 } else if (acpi_evalf(ec_handle, &res, "CMMD", "qd")) {
3133 /* For X1 Yoga (2016) */
3134 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_CMMD;
3135 in_tablet_mode = res == TP_EC_CMMD_TABLET_MODE;
3136 type = "CMMD";
Lyudeb3180022016-11-11 15:15:02 -05003137 }
3138
3139 if (!tp_features.hotkey_tablet)
3140 return 0;
3141
3142 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3143 type, in_tablet_mode ? "tablet" : "laptop");
3144
3145 res = add_to_attr_set(hotkey_dev_attributes,
3146 &dev_attr_hotkey_tablet_mode.attr);
3147 if (res)
3148 return -1;
3149
3150 return in_tablet_mode;
3151}
3152
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003153static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003154{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003155 /* Requirements for changing the default keymaps:
3156 *
3157 * 1. Many of the keys are mapped to KEY_RESERVED for very
3158 * good reasons. Do not change them unless you have deep
3159 * knowledge on the IBM and Lenovo ThinkPad firmware for
3160 * the various ThinkPad models. The driver behaves
3161 * differently for KEY_RESERVED: such keys have their
3162 * hot key mask *unset* in mask_recommended, and also
3163 * in the initial hot key mask programmed into the
3164 * firmware at driver load time, which means the firm-
3165 * ware may react very differently if you change them to
3166 * something else;
3167 *
3168 * 2. You must be subscribed to the linux-thinkpad and
3169 * ibm-acpi-devel mailing lists, and you should read the
3170 * list archives since 2007 if you want to change the
3171 * keymaps. This requirement exists so that you will
3172 * know the past history of problems with the thinkpad-
3173 * acpi driver keymaps, and also that you will be
3174 * listening to any bug reports;
3175 *
3176 * 3. Do not send thinkpad-acpi specific patches directly to
3177 * for merging, *ever*. Send them to the linux-acpi
3178 * mailinglist for comments. Merging is to be done only
3179 * through acpi-test and the ACPI maintainer.
3180 *
3181 * If the above is too much to ask, don't change the keymap.
3182 * Ask the thinkpad-acpi maintainer to do it, instead.
3183 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003184
3185 enum keymap_index {
3186 TPACPI_KEYMAP_IBM_GENERIC = 0,
3187 TPACPI_KEYMAP_LENOVO_GENERIC,
3188 };
3189
3190 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3191 /* Generic keymap for IBM ThinkPads */
3192 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003193 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003194 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003195 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3196 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003197
3198 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003199 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3200 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3201 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003202
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003203 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003204 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003205 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003206
3207 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003208 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003209
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003210 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3211 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003212
3213 /* Volume: firmware always react to it and reprograms
3214 * the built-in *extra* mixer. Never map it to control
3215 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003216 KEY_RESERVED, /* 0x14: VOLUME UP */
3217 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3218 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003219
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003220 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003221
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003222 /* (assignments unknown, please report if found) */
3223 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3224 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003225
3226 /* No assignments, only used for Adaptive keyboards. */
3227 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3228 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3229 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3230 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3231 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Christian Kellner696c65232017-02-28 17:10:57 +01003232
3233 /* No assignment, used for newer Lenovo models */
3234 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3235 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3236 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3237 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3238 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3239 KEY_UNKNOWN, KEY_UNKNOWN
3240
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003241 },
3242
3243 /* Generic keymap for Lenovo ThinkPads */
3244 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003245 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3246 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003247 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003248 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003249
3250 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003251 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3252 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3253 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003254
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003255 /* These should be enabled --only-- when ACPI video
3256 * is disabled (i.e. in "vendor" mode), and are handled
3257 * in a special way by the init code */
3258 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3259 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003260
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003261 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003262
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003263 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3264 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003265
3266 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3267 * react to it and reprograms the built-in *extra* mixer.
3268 * Never map it to control another mixer by default.
3269 *
3270 * T60?, T61, R60?, R61: firmware and EC tries to send
3271 * these over the regular keyboard, so these are no-ops,
3272 * but there are still weird bugs re. MUTE, so do not
3273 * change unless you get test reports from all Lenovo
3274 * models. May cause the BIOS to interfere with the
3275 * HDA mixer.
3276 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003277 KEY_RESERVED, /* 0x14: VOLUME UP */
3278 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3279 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003280
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003281 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003282
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003283 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003284 KEY_UNKNOWN, KEY_UNKNOWN,
3285
3286 /*
3287 * The mic mute button only sends 0x1a. It does not
3288 * automatically mute the mic or change the mute light.
3289 */
3290 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3291
3292 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003293 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003294
3295 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003296 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003297
3298 /*
3299 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3300 * The first item in this list is the Mute button which is
3301 * emitted with 0x103 through
3302 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3303 * symbol is held.
3304 * We'll need to offset those by 0x20.
3305 */
3306 KEY_RESERVED, /* Mute held, 0x103 */
3307 KEY_BRIGHTNESS_MIN, /* Backlight off */
3308 KEY_RESERVED, /* Clipping tool */
3309 KEY_RESERVED, /* Cloud */
3310 KEY_RESERVED,
3311 KEY_VOICECOMMAND, /* Voice */
3312 KEY_RESERVED,
3313 KEY_RESERVED, /* Gestures */
3314 KEY_RESERVED,
3315 KEY_RESERVED,
3316 KEY_RESERVED,
3317 KEY_CONFIG, /* Settings */
3318 KEY_RESERVED, /* New tab */
3319 KEY_REFRESH, /* Reload */
3320 KEY_BACK, /* Back */
3321 KEY_RESERVED, /* Microphone down */
3322 KEY_RESERVED, /* Microphone up */
3323 KEY_RESERVED, /* Microphone cancellation */
3324 KEY_RESERVED, /* Camera mode */
3325 KEY_RESERVED, /* Rotate display, 0x116 */
Christian Kellner696c65232017-02-28 17:10:57 +01003326
3327 /*
3328 * These are found in 2017 models (e.g. T470s, X270).
3329 * The lowest known value is 0x311, which according to
3330 * the manual should launch a user defined favorite
3331 * application.
3332 *
3333 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3334 * corresponding to 0x34.
3335 */
3336
3337 /* (assignments unknown, please report if found) */
3338 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3339 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3340 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3341 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3342 KEY_UNKNOWN,
3343
3344 KEY_FAVORITES, /* Favorite app, 0x311 */
3345 KEY_RESERVED, /* Clipping tool */
3346 KEY_RESERVED,
3347 KEY_BLUETOOTH, /* Bluetooth */
3348 KEY_KEYBOARD /* Keyboard, 0x315 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003349 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003350 };
3351
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003352 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3353 /* Generic maps (fallback) */
3354 {
3355 .vendor = PCI_VENDOR_ID_IBM,
3356 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3357 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3358 },
3359 {
3360 .vendor = PCI_VENDOR_ID_LENOVO,
3361 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3362 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3363 },
3364 };
3365
3366#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003367#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003368
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003369 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003370 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003371 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003372 bool radiosw_state = false;
3373 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003374
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003375 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003376 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003377
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003378 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3379 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003380
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003381 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003382 BUG_ON(tpacpi_inputdev->open != NULL ||
3383 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003384
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003385 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003386 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003387
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003388#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003389 mutex_init(&hotkey_thread_data_mutex);
3390#endif
3391
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003392 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003393 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003394
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003395 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3396 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003397 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003398
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003399 if (!tp_features.hotkey)
3400 return 1;
3401
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003402 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3403 ARRAY_SIZE(tpacpi_hotkey_qtable));
3404
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003405 tpacpi_disable_brightness_delay();
3406
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003407 /* MUST have enough space for all attributes to be added to
3408 * hotkey_dev_attributes */
3409 hotkey_dev_attributes = create_attr_set(
3410 ARRAY_SIZE(hotkey_attributes) + 2,
3411 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003412 if (!hotkey_dev_attributes)
3413 return -ENOMEM;
3414 res = add_many_to_attr_set(hotkey_dev_attributes,
3415 hotkey_attributes,
3416 ARRAY_SIZE(hotkey_attributes));
3417 if (res)
3418 goto err_exit;
3419
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003420 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003421 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3422 for HKEY interface version 0x100 */
3423 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003424 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3425 "firmware HKEY interface version: 0x%x\n",
3426 hkeyv);
3427
3428 switch (hkeyv >> 8) {
3429 case 1:
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003430 /*
3431 * MHKV 0x100 in A31, R40, R40e,
3432 * T4x, X31, and later
3433 */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003434
3435 /* Paranoia check AND init hotkey_all_mask */
3436 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3437 "MHKA", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003438 pr_err("missing MHKA handler, please report this to %s\n",
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003439 TPACPI_MAIL);
3440 /* Fallback: pre-init for FN+F3,F4,F12 */
3441 hotkey_all_mask = 0x080cU;
3442 } else {
3443 tp_features.hotkey_mask = 1;
3444 }
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003445 break;
3446
3447 case 2:
3448 /*
3449 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3450 */
3451
3452 /* Paranoia check AND init hotkey_all_mask */
3453 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3454 "MHKA", "dd", 1)) {
3455 pr_err("missing MHKA handler, please report this to %s\n",
3456 TPACPI_MAIL);
3457 /* Fallback: pre-init for FN+F3,F4,F12 */
3458 hotkey_all_mask = 0x080cU;
3459 } else {
3460 tp_features.hotkey_mask = 1;
3461 }
3462
3463 /*
3464 * Check if we have an adaptive keyboard, like on the
3465 * Lenovo Carbon X1 2014 (2nd Gen).
3466 */
3467 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3468 "MHKA", "dd", 2)) {
3469 if (hotkey_adaptive_all_mask != 0) {
3470 tp_features.has_adaptive_kbd = true;
3471 res = sysfs_create_group(
3472 &tpacpi_pdev->dev.kobj,
3473 &adaptive_kbd_attr_group);
3474 if (res)
3475 goto err_exit;
3476 }
3477 } else {
3478 tp_features.has_adaptive_kbd = false;
3479 hotkey_adaptive_all_mask = 0x0U;
3480 }
3481 break;
3482
3483 default:
3484 pr_err("unknown version of the HKEY interface: 0x%x\n",
3485 hkeyv);
3486 pr_err("please report this to %s\n", TPACPI_MAIL);
3487 break;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003488 }
3489 }
3490
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003491 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3492 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003493 str_supported(tp_features.hotkey_mask));
3494
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003495 /* Init hotkey_all_mask if not initialized yet */
3496 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3497 (quirks & TPACPI_HK_Q_INIMASK))
3498 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003499
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003500 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003501 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003502 /* hotkey_source_mask *must* be zero for
3503 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003504 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003505 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003506 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003507
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003508 hotkey_orig_mask = hotkey_acpi_mask;
3509 } else {
3510 hotkey_orig_mask = hotkey_all_mask;
3511 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003512 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003513
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003514#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3515 if (dbg_wlswemul) {
3516 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003517 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003518 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003519 } else
3520#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003521 /* Not all thinkpads have a hardware radio switch */
3522 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3523 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003524 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003525 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003526 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003527 }
3528 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003529 res = add_to_attr_set(hotkey_dev_attributes,
3530 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003531
Lyudeb3180022016-11-11 15:15:02 -05003532 res = hotkey_init_tablet_mode();
3533 if (res < 0)
3534 goto err_exit;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003535
Lyudeb3180022016-11-11 15:15:02 -05003536 tabletsw_state = res;
3537
3538 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3539 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003540 if (res)
3541 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003542
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003543 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003544 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3545 GFP_KERNEL);
3546 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003547 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003548 res = -ENOMEM;
3549 goto err_exit;
3550 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003551
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003552 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3553 ARRAY_SIZE(tpacpi_keymap_qtable));
3554 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3555 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3556 "using keymap number %lu\n", keymap_id);
3557
3558 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3559 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003560
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003561 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003562 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3563 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3564 tpacpi_inputdev->keycode = hotkey_keycode_map;
3565 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3566 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003567 input_set_capability(tpacpi_inputdev, EV_KEY,
3568 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003569 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003570 if (i < sizeof(hotkey_reserved_mask)*8)
3571 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003572 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003573 }
3574
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003575 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003576 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003577 input_report_switch(tpacpi_inputdev,
3578 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003579 }
3580 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003581 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003582 input_report_switch(tpacpi_inputdev,
3583 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003584 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003585
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003586 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003587 * userspace. tpacpi_detect_brightness_capabilities() must have
3588 * been called before this point */
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02003589 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Joe Perches0978e012011-04-04 10:06:25 -07003590 pr_info("This ThinkPad has standard ACPI backlight "
3591 "brightness control, supported by the ACPI "
3592 "video driver\n");
3593 pr_notice("Disabling thinkpad-acpi brightness events "
3594 "by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003595
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003596 /* Disable brightness up/down on Lenovo thinkpads when
3597 * ACPI is handling them, otherwise it is plain impossible
3598 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003599 hotkey_reserved_mask |=
3600 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3601 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003602 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3603 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003604 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003605
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003606#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003607 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3608 & ~hotkey_all_mask
3609 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003610
3611 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3612 "hotkey source mask 0x%08x, polling freq %u\n",
3613 hotkey_source_mask, hotkey_poll_freq);
3614#endif
3615
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003616 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3617 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003618 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003619 if (res) {
3620 hotkey_exit();
3621 return res;
3622 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003623 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3624 | hotkey_driver_mask)
3625 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003626 if (res < 0 && res != -ENXIO) {
3627 hotkey_exit();
3628 return res;
3629 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003630 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3631 & ~hotkey_reserved_mask;
3632 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3633 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3634 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003635
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003636 tpacpi_inputdev->open = &hotkey_inputdev_open;
3637 tpacpi_inputdev->close = &hotkey_inputdev_close;
3638
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003639 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003640
3641 return 0;
3642
3643err_exit:
3644 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
Bastien Nocerab790cee2015-03-02 14:45:27 +01003645 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3646 &adaptive_kbd_attr_group);
3647
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003648 hotkey_dev_attributes = NULL;
3649
Jan van den Berg72a979f2014-09-17 00:01:08 +02003650 return (res < 0) ? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003651}
3652
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003653/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3654 * mode, Web conference mode, Function mode and Lay-flat mode.
3655 * We support Home mode and Function mode currently.
3656 *
3657 * Will consider support rest of modes in future.
3658 *
3659 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003660static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003661 HOME_MODE,
3662/* WEB_BROWSER_MODE = 2,
3663 WEB_CONFERENCE_MODE = 3, */
3664 FUNCTION_MODE
3665};
3666
3667#define DFR_CHANGE_ROW 0x101
3668#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003669#define FIRST_ADAPTIVE_KEY 0x103
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003670
3671/* press Fn key a while second, it will switch to Function Mode. Then
3672 * release Fn key, previous mode be restored.
3673 */
3674static bool adaptive_keyboard_mode_is_saved;
3675static int adaptive_keyboard_prev_mode;
3676
Bastien Noceraf74587f2015-03-02 14:45:22 +01003677static int adaptive_keyboard_get_mode(void)
3678{
3679 int mode = 0;
3680
3681 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3682 pr_err("Cannot read adaptive keyboard mode\n");
3683 return -EIO;
3684 }
3685
3686 return mode;
3687}
3688
3689static int adaptive_keyboard_set_mode(int new_mode)
3690{
3691 if (new_mode < 0 ||
3692 new_mode > LAYFLAT_MODE)
3693 return -EINVAL;
3694
3695 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3696 pr_err("Cannot set adaptive keyboard mode\n");
3697 return -EIO;
3698 }
3699
3700 return 0;
3701}
3702
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003703static int adaptive_keyboard_get_next_mode(int mode)
3704{
3705 size_t i;
3706 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3707
3708 for (i = 0; i <= max_mode; i++) {
3709 if (adaptive_keyboard_modes[i] == mode)
3710 break;
3711 }
3712
3713 if (i >= max_mode)
3714 i = 0;
3715 else
3716 i++;
3717
3718 return adaptive_keyboard_modes[i];
3719}
3720
3721static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3722{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003723 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003724 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003725 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003726
3727 switch (scancode) {
3728 case DFR_CHANGE_ROW:
3729 if (adaptive_keyboard_mode_is_saved) {
3730 new_mode = adaptive_keyboard_prev_mode;
3731 adaptive_keyboard_mode_is_saved = false;
3732 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003733 current_mode = adaptive_keyboard_get_mode();
3734 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003735 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003736 new_mode = adaptive_keyboard_get_next_mode(
3737 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003738 }
3739
Bastien Noceraf74587f2015-03-02 14:45:22 +01003740 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003741 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003742
3743 return true;
3744
3745 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003746 current_mode = adaptive_keyboard_get_mode();
3747 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003748 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003749
Bastien Noceraf74587f2015-03-02 14:45:22 +01003750 adaptive_keyboard_prev_mode = current_mode;
3751 adaptive_keyboard_mode_is_saved = true;
3752
3753 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3754 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003755 return true;
3756
3757 default:
Dan Carpenter741d98c2015-03-11 12:36:07 +03003758 if (scancode < FIRST_ADAPTIVE_KEY ||
Christian Kellner696c65232017-02-28 17:10:57 +01003759 scancode >= FIRST_ADAPTIVE_KEY +
3760 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3761 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
Bastien Nocera6a68d852015-03-02 14:45:31 +01003762 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3763 scancode);
3764 return false;
3765 }
Christian Kellner149c8c72017-02-28 17:10:56 +01003766 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3767 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
Bastien Nocera6a68d852015-03-02 14:45:31 +01003768 if (keycode != KEY_RESERVED) {
3769 mutex_lock(&tpacpi_inputdev_send_mutex);
3770
3771 input_report_key(tpacpi_inputdev, keycode, 1);
3772 input_sync(tpacpi_inputdev);
3773
3774 input_report_key(tpacpi_inputdev, keycode, 0);
3775 input_sync(tpacpi_inputdev);
3776
3777 mutex_unlock(&tpacpi_inputdev_send_mutex);
3778 }
3779 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003780 }
3781}
3782
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003783static bool hotkey_notify_hotkey(const u32 hkey,
3784 bool *send_acpi_ev,
3785 bool *ignore_acpi_ev)
3786{
3787 /* 0x1000-0x1FFF: key presses */
3788 unsigned int scancode = hkey & 0xfff;
3789 *send_acpi_ev = true;
3790 *ignore_acpi_ev = false;
3791
Christian Kellner696c65232017-02-28 17:10:57 +01003792 /*
3793 * Original events are in the 0x10XX range, the adaptive keyboard
3794 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3795 * models, additional keys are emitted through 0x13XX.
3796 */
3797 switch ((hkey >> 8) & 0xf) {
3798 case 0:
3799 if (scancode > 0 &&
3800 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3801 /* HKEY event 0x1001 is scancode 0x00 */
3802 scancode--;
3803 if (!(hotkey_source_mask & (1 << scancode))) {
3804 tpacpi_input_send_key_masked(scancode);
3805 *send_acpi_ev = false;
3806 } else {
3807 *ignore_acpi_ev = true;
3808 }
3809 return true;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003810 }
Christian Kellner696c65232017-02-28 17:10:57 +01003811 break;
3812
3813 case 1:
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003814 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Christian Kellner696c65232017-02-28 17:10:57 +01003815
3816 case 3:
3817 /* Extended keycodes start at 0x300 and our offset into the map
3818 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3819 * will be positive, but might not be in the correct range.
3820 */
3821 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3822 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3823 scancode < TPACPI_HOTKEY_MAP_LEN) {
3824 tpacpi_input_send_key(scancode);
3825 return true;
3826 }
3827 break;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003828 }
Christian Kellner696c65232017-02-28 17:10:57 +01003829
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003830 return false;
3831}
3832
3833static bool hotkey_notify_wakeup(const u32 hkey,
3834 bool *send_acpi_ev,
3835 bool *ignore_acpi_ev)
3836{
3837 /* 0x2000-0x2FFF: Wakeup reason */
3838 *send_acpi_ev = true;
3839 *ignore_acpi_ev = false;
3840
3841 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003842 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3843 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003844 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3845 *ignore_acpi_ev = true;
3846 break;
3847
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003848 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3849 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003850 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3851 *ignore_acpi_ev = true;
3852 break;
3853
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003854 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3855 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003856 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003857 /* how to auto-heal: */
3858 /* 2313: woke up from S3, go to S4/S5 */
3859 /* 2413: woke up from S4, go to S5 */
3860 break;
3861
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003862 default:
3863 return false;
3864 }
3865
3866 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003867 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003868 hotkey_wakeup_reason_notify_change();
3869 }
3870 return true;
3871}
3872
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003873static bool hotkey_notify_dockevent(const u32 hkey,
3874 bool *send_acpi_ev,
3875 bool *ignore_acpi_ev)
3876{
3877 /* 0x4000-0x4FFF: dock-related events */
3878 *send_acpi_ev = true;
3879 *ignore_acpi_ev = false;
3880
3881 switch (hkey) {
3882 case TP_HKEY_EV_UNDOCK_ACK:
3883 /* ACPI undock operation completed after wakeup */
3884 hotkey_autosleep_ack = 1;
3885 pr_info("undocked\n");
3886 hotkey_wakeup_hotunplug_complete_notify_change();
3887 return true;
3888
3889 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3890 pr_info("docked into hotplug port replicator\n");
3891 return true;
3892 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3893 pr_info("undocked from hotplug port replicator\n");
3894 return true;
3895
3896 default:
3897 return false;
3898 }
3899}
3900
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003901static bool hotkey_notify_usrevent(const u32 hkey,
3902 bool *send_acpi_ev,
3903 bool *ignore_acpi_ev)
3904{
3905 /* 0x5000-0x5FFF: human interface helpers */
3906 *send_acpi_ev = true;
3907 *ignore_acpi_ev = false;
3908
3909 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003910 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3911 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003912 return true;
3913
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003914 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3915 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003916 tpacpi_input_send_tabletsw();
3917 hotkey_tablet_mode_notify_change();
3918 *send_acpi_ev = false;
3919 return true;
3920
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003921 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3922 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3923 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003924 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003925 *ignore_acpi_ev = true;
3926 return true;
3927
3928 default:
3929 return false;
3930 }
3931}
3932
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003933static void thermal_dump_all_sensors(void);
3934
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003935static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003936 bool *send_acpi_ev,
3937 bool *ignore_acpi_ev)
3938{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003939 bool known = true;
3940
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003941 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003942 *send_acpi_ev = true;
3943 *ignore_acpi_ev = false;
3944
3945 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003946 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003947 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003948 /* recommended action: do nothing, we don't have
3949 * Lenovo ATM information */
3950 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003951 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003952 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003953 /* recommended action: warn user through gui */
3954 break;
3955 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003956 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003957 /* recommended action: immediate sleep/hibernate */
3958 break;
3959 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003960 pr_crit("THERMAL ALARM: "
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003961 "a sensor reports something is too hot!\n");
3962 /* recommended action: warn user through gui, that */
3963 /* some internal component is too hot */
3964 break;
3965 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003966 pr_alert("THERMAL EMERGENCY: "
3967 "a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003968 /* recommended action: immediate sleep/hibernate */
3969 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01003970 case TP_HKEY_EV_AC_CHANGED:
3971 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3972 * AC status changed; can be triggered by plugging or
3973 * unplugging AC adapter, docking or undocking. */
3974
3975 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003976
3977 case TP_HKEY_EV_KEY_NUMLOCK:
3978 case TP_HKEY_EV_KEY_FN:
Xavier Naveira67ab6242015-02-10 08:45:18 +01003979 case TP_HKEY_EV_KEY_FN_ESC:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003980 /* key press events, we just ignore them as long as the EC
3981 * is still reporting them in the normal keyboard stream */
3982 *send_acpi_ev = false;
3983 *ignore_acpi_ev = true;
3984 return true;
3985
Lyudeb03f4d42016-11-11 15:15:03 -05003986 case TP_HKEY_EV_TABLET_CHANGED:
3987 tpacpi_input_send_tabletsw();
3988 hotkey_tablet_mode_notify_change();
3989 *send_acpi_ev = false;
3990 break;
3991
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003992 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003993 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003994 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003995 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003996
3997 thermal_dump_all_sensors();
3998
3999 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004000}
4001
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004002static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4003{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004004 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004005 bool send_acpi_ev;
4006 bool ignore_acpi_ev;
4007 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004008
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004009 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07004010 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004011 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004012 acpi_bus_generate_netlink_event(
4013 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004014 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004015 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004016 return;
4017 }
4018
4019 while (1) {
4020 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07004021 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004022 return;
4023 }
4024
4025 if (hkey == 0) {
4026 /* queue empty */
4027 return;
4028 }
4029
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004030 send_acpi_ev = true;
4031 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004032
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004033 switch (hkey >> 12) {
4034 case 1:
4035 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004036 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4037 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004038 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004039 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004040 /* 0x2000-0x2FFF: Wakeup reason */
4041 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4042 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004043 break;
4044 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004045 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004046 switch (hkey) {
4047 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004048 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004049 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004050 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004051 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004052 break;
4053 case TP_HKEY_EV_OPTDRV_EJ:
4054 /* FIXME: kick libata if SATA link offline */
4055 known_ev = true;
4056 break;
4057 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004058 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004059 }
4060 break;
4061 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03004062 /* 0x4000-0x4FFF: dock-related events */
4063 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4064 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004065 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004066 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02004067 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004068 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4069 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004070 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004071 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004072 /* 0x6000-0x6FFF: thermal alarms/notices and
4073 * keyboard events */
4074 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004075 &ignore_acpi_ev);
4076 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004077 case 7:
4078 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004079 if (tp_features.hotkey_wlsw &&
4080 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004081 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004082 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004083 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004084 break;
4085 }
4086 /* fallthrough to default */
4087 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004088 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004089 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004090 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07004091 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4092 pr_notice("please report the conditions when this "
4093 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004094 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004095
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004096 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03004097 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004098 acpi_bus_generate_netlink_event(
4099 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004100 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004101 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004102 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004103 }
4104}
4105
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02004106static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004107{
4108 /* Do these on suspend, we get the events on early resume! */
4109 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4110 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08004111
4112 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004113 if (tp_features.has_adaptive_kbd) {
4114 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4115 "GTRW", "dd", 0)) {
4116 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004117 }
4118 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004119}
4120
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004121static void hotkey_resume(void)
4122{
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03004123 tpacpi_disable_brightness_delay();
4124
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004125 if (hotkey_status_set(true) < 0 ||
4126 hotkey_mask_set(hotkey_acpi_mask) < 0)
Joe Perches0978e012011-04-04 10:06:25 -07004127 pr_err("error while attempting to reset the event "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004128 "firmware interface\n");
4129
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004130 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02004131 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004132 hotkey_wakeup_reason_notify_change();
4133 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004134 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004135
4136 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004137 if (tp_features.has_adaptive_kbd) {
4138 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4139 adaptive_keyboard_prev_mode)) {
4140 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004141 }
4142 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004143}
4144
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004145/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004146static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004148 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004150 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004151 seq_printf(m, "status:\t\tnot supported\n");
4152 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004153 }
4154
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004155 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004156 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004157 res = hotkey_status_get(&status);
4158 if (!res)
4159 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004160 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004161 if (res)
4162 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004164 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004165 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004166 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4167 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004169 seq_printf(m, "mask:\t\tnot supported\n");
4170 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 }
4172
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174}
4175
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004176static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004177{
4178 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004179 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Joe Perches0978e012011-04-04 10:06:25 -07004180 pr_fmt("hotkey enable/disable functionality has been "
4181 "removed from the driver. "
4182 "Hotkeys are always enabled.\n")))
4183 pr_err("Please remove the hotkey=enable module "
4184 "parameter, it is deprecated. "
4185 "Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004186}
4187
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004188static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004190 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004191 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004194 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 return -ENODEV;
4196
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004197 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004198 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004199
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004200 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004201
4202 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 while ((cmd = next_cmd(&buf))) {
4204 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004205 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004207 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004208 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa462009-09-12 15:22:16 -03004210 mask = (hotkey_all_mask | hotkey_source_mask)
4211 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4213 /* mask set */
4214 } else if (sscanf(cmd, "%x", &mask) == 1) {
4215 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004216 } else {
4217 res = -EINVAL;
4218 goto errexit;
4219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004221
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004222 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004223 tpacpi_disclose_usertask("procfs hotkey",
4224 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004225 res = hotkey_user_mask_set(mask);
4226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004228errexit:
4229 mutex_unlock(&hotkey_mutex);
4230 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004231}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004233static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004234 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4235 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Hui Wanga3c42a42016-11-08 16:13:23 +08004236 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004237 {"", 0},
4238};
4239
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004240static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004241 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004242 .notify = hotkey_notify,
4243 .handle = &hkey_handle,
4244 .type = ACPI_DEVICE_NOTIFY,
4245};
4246
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004247static struct ibm_struct hotkey_driver_data = {
4248 .name = "hotkey",
4249 .read = hotkey_read,
4250 .write = hotkey_write,
4251 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004252 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004253 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004254 .acpi = &ibm_hotkey_acpidriver,
4255};
4256
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004257/*************************************************************************
4258 * Bluetooth subdriver
4259 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004261enum {
4262 /* ACPI GBDC/SBDC bits */
4263 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4264 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004265 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004266 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004267};
4268
4269enum {
4270 /* ACPI \BLTH commands */
4271 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4272 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4273 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4274 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4275 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004276};
4277
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004278#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4279
Johannes Berg19d337d2009-06-02 13:01:37 +02004280static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004281{
4282 int status;
4283
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004284#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4285 if (dbg_bluetoothemul)
4286 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004287 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004288#endif
4289
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004290 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4291 return -EIO;
4292
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004293 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004294 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004295}
4296
Johannes Berg19d337d2009-06-02 13:01:37 +02004297static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004298{
4299 int status;
4300
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004301 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004302 "will attempt to %s bluetooth\n",
4303 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004304
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004305#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4306 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004307 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004308 return 0;
4309 }
4310#endif
4311
Johannes Berg19d337d2009-06-02 13:01:37 +02004312 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004313 status = TP_ACPI_BLUETOOTH_RADIOSSW
4314 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4315 else
4316 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004317
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004318 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4319 return -EIO;
4320
4321 return 0;
4322}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004323
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004324/* sysfs bluetooth enable ---------------------------------------------- */
4325static ssize_t bluetooth_enable_show(struct device *dev,
4326 struct device_attribute *attr,
4327 char *buf)
4328{
Johannes Berg19d337d2009-06-02 13:01:37 +02004329 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4330 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004331}
4332
4333static ssize_t bluetooth_enable_store(struct device *dev,
4334 struct device_attribute *attr,
4335 const char *buf, size_t count)
4336{
Johannes Berg19d337d2009-06-02 13:01:37 +02004337 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4338 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004339}
4340
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004341static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004342
4343/* --------------------------------------------------------------------- */
4344
4345static struct attribute *bluetooth_attributes[] = {
4346 &dev_attr_bluetooth_enable.attr,
4347 NULL
4348};
4349
4350static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004351 .attrs = bluetooth_attributes,
4352};
4353
Johannes Berg19d337d2009-06-02 13:01:37 +02004354static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4355 .get_status = bluetooth_get_status,
4356 .set_status = bluetooth_set_status,
4357};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004358
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004359static void bluetooth_shutdown(void)
4360{
4361 /* Order firmware to save current state to NVRAM */
4362 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4363 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004364 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004365 else
4366 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004367 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004368}
4369
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004370static void bluetooth_exit(void)
4371{
4372 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4373 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004374
4375 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4376
4377 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004378}
4379
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004380static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004382 int res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004383 int status = 0;
4384
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004385 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4386 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004387
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004388 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004389
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004390 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4391 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004392 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004393 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004395 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4396 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004397 str_supported(tp_features.bluetooth),
4398 status);
4399
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004400#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4401 if (dbg_bluetoothemul) {
4402 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004403 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004404 } else
4405#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004406 if (tp_features.bluetooth &&
4407 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4408 /* no bluetooth hardware present in system */
4409 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004410 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004411 "bluetooth hardware not installed\n");
4412 }
4413
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004414 if (!tp_features.bluetooth)
4415 return 1;
4416
Johannes Berg19d337d2009-06-02 13:01:37 +02004417 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4418 &bluetooth_tprfk_ops,
4419 RFKILL_TYPE_BLUETOOTH,
4420 TPACPI_RFK_BLUETOOTH_SW_NAME,
4421 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004422 if (res)
4423 return res;
4424
Johannes Berg19d337d2009-06-02 13:01:37 +02004425 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4426 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004427 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004428 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004429 return res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004430 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004431
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433}
4434
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004435/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004436static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004438 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439}
4440
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004441static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442{
Johannes Berg19d337d2009-06-02 13:01:37 +02004443 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444}
4445
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004446static struct ibm_struct bluetooth_driver_data = {
4447 .name = "bluetooth",
4448 .read = bluetooth_read,
4449 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004450 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004451 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004452};
4453
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004454/*************************************************************************
4455 * Wan subdriver
4456 */
4457
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004458enum {
4459 /* ACPI GWAN/SWAN bits */
4460 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4461 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004462 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004463 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004464};
4465
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004466#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4467
Johannes Berg19d337d2009-06-02 13:01:37 +02004468static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004469{
4470 int status;
4471
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004472#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4473 if (dbg_wwanemul)
4474 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004475 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004476#endif
4477
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004478 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4479 return -EIO;
4480
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004481 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004482 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004483}
4484
Johannes Berg19d337d2009-06-02 13:01:37 +02004485static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004486{
4487 int status;
4488
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004489 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004490 "will attempt to %s wwan\n",
4491 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004492
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004493#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4494 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004495 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004496 return 0;
4497 }
4498#endif
4499
Johannes Berg19d337d2009-06-02 13:01:37 +02004500 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004501 status = TP_ACPI_WANCARD_RADIOSSW
4502 | TP_ACPI_WANCARD_RESUMECTRL;
4503 else
4504 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004505
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004506 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4507 return -EIO;
4508
4509 return 0;
4510}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004511
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004512/* sysfs wan enable ---------------------------------------------------- */
4513static ssize_t wan_enable_show(struct device *dev,
4514 struct device_attribute *attr,
4515 char *buf)
4516{
Johannes Berg19d337d2009-06-02 13:01:37 +02004517 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4518 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004519}
4520
4521static ssize_t wan_enable_store(struct device *dev,
4522 struct device_attribute *attr,
4523 const char *buf, size_t count)
4524{
Johannes Berg19d337d2009-06-02 13:01:37 +02004525 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4526 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004527}
4528
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004529static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4530 wan_enable_show, wan_enable_store);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004531
4532/* --------------------------------------------------------------------- */
4533
4534static struct attribute *wan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004535 &dev_attr_wwan_enable.attr,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004536 NULL
4537};
4538
4539static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004540 .attrs = wan_attributes,
4541};
4542
Johannes Berg19d337d2009-06-02 13:01:37 +02004543static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4544 .get_status = wan_get_status,
4545 .set_status = wan_set_status,
4546};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004547
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004548static void wan_shutdown(void)
4549{
4550 /* Order firmware to save current state to NVRAM */
4551 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4552 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004553 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004554 else
4555 vdbg_printk(TPACPI_DBG_RFKILL,
4556 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004557}
4558
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004559static void wan_exit(void)
4560{
4561 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4562 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004563
4564 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4565
4566 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004567}
4568
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004569static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004570{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004571 int res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004572 int status = 0;
4573
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004574 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4575 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004576
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004577 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004578
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004579 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004580 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004581
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004582 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4583 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004584 str_supported(tp_features.wan),
4585 status);
4586
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004587#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4588 if (dbg_wwanemul) {
4589 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004590 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004591 } else
4592#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004593 if (tp_features.wan &&
4594 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4595 /* no wan hardware present in system */
4596 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004597 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004598 "wan hardware not installed\n");
4599 }
4600
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004601 if (!tp_features.wan)
4602 return 1;
4603
Johannes Berg19d337d2009-06-02 13:01:37 +02004604 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4605 &wan_tprfk_ops,
4606 RFKILL_TYPE_WWAN,
4607 TPACPI_RFK_WWAN_SW_NAME,
4608 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004609 if (res)
4610 return res;
4611
Johannes Berg19d337d2009-06-02 13:01:37 +02004612 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4613 &wan_attr_group);
4614
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004615 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004616 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004617 return res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004618 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004619
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004620 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004621}
4622
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004623/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004624static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004625{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004626 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004627}
4628
4629static int wan_write(char *buf)
4630{
Johannes Berg19d337d2009-06-02 13:01:37 +02004631 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004632}
4633
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004634static struct ibm_struct wan_driver_data = {
4635 .name = "wan",
4636 .read = wan_read,
4637 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004638 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004639 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004640};
4641
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004642/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004643 * UWB subdriver
4644 */
4645
4646enum {
4647 /* ACPI GUWB/SUWB bits */
4648 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4649 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4650};
4651
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004652#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4653
Johannes Berg19d337d2009-06-02 13:01:37 +02004654static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004655{
4656 int status;
4657
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004658#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4659 if (dbg_uwbemul)
4660 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004661 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004662#endif
4663
4664 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4665 return -EIO;
4666
4667 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004668 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004669}
4670
Johannes Berg19d337d2009-06-02 13:01:37 +02004671static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004672{
4673 int status;
4674
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004675 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004676 "will attempt to %s UWB\n",
4677 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004678
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004679#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4680 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004681 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004682 return 0;
4683 }
4684#endif
4685
Johannes Berg19d337d2009-06-02 13:01:37 +02004686 if (state == TPACPI_RFK_RADIO_ON)
4687 status = TP_ACPI_UWB_RADIOSSW;
4688 else
4689 status = 0;
4690
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004691 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4692 return -EIO;
4693
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004694 return 0;
4695}
4696
4697/* --------------------------------------------------------------------- */
4698
Johannes Berg19d337d2009-06-02 13:01:37 +02004699static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4700 .get_status = uwb_get_status,
4701 .set_status = uwb_set_status,
4702};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004703
4704static void uwb_exit(void)
4705{
Johannes Berg19d337d2009-06-02 13:01:37 +02004706 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004707}
4708
4709static int __init uwb_init(struct ibm_init_struct *iibm)
4710{
4711 int res;
4712 int status = 0;
4713
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004714 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4715 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004716
4717 TPACPI_ACPIHANDLE_INIT(hkey);
4718
4719 tp_features.uwb = hkey_handle &&
4720 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4721
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004722 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4723 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004724 str_supported(tp_features.uwb),
4725 status);
4726
4727#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4728 if (dbg_uwbemul) {
4729 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004730 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004731 } else
4732#endif
4733 if (tp_features.uwb &&
4734 !(status & TP_ACPI_UWB_HWPRESENT)) {
4735 /* no uwb hardware present in system */
4736 tp_features.uwb = 0;
4737 dbg_printk(TPACPI_DBG_INIT,
4738 "uwb hardware not installed\n");
4739 }
4740
4741 if (!tp_features.uwb)
4742 return 1;
4743
4744 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004745 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004746 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004747 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004748 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004749 return res;
4750}
4751
4752static struct ibm_struct uwb_driver_data = {
4753 .name = "uwb",
4754 .exit = uwb_exit,
4755 .flags.experimental = 1,
4756};
4757
4758/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004759 * Video subdriver
4760 */
4761
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004762#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4763
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004764enum video_access_mode {
4765 TPACPI_VIDEO_NONE = 0,
4766 TPACPI_VIDEO_570, /* 570 */
4767 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4768 TPACPI_VIDEO_NEW, /* all others */
4769};
4770
4771enum { /* video status flags, based on VIDEO_570 */
4772 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4773 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4774 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4775};
4776
4777enum { /* TPACPI_VIDEO_570 constants */
4778 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4779 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4780 * video_status_flags */
4781 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4782 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4783};
4784
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004785static enum video_access_mode video_supported;
4786static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004787
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004788static int video_autosw_get(void);
4789static int video_autosw_set(int enable);
4790
Joe Perches112a6ee2011-04-04 10:06:24 -07004791TPACPI_HANDLE(vid, root,
4792 "\\_SB.PCI.AGP.VGA", /* 570 */
4793 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4794 "\\_SB.PCI0.VID0", /* 770e */
4795 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4796 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4797 "\\_SB.PCI0.AGP.VID", /* all others */
4798 ); /* R30, R31 */
4799
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004800TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004801
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004802static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004804 int ivga;
4805
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004806 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4807
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004808 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004809 if (tpacpi_is_ibm())
4810 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004811
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004812 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4813 /* G41, assume IVGA doesn't change */
4814 vid_handle = vid2_handle;
4815
4816 if (!vid_handle)
4817 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004818 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004819 else if (tpacpi_is_ibm() &&
4820 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004821 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004822 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004823 else if (tpacpi_is_ibm() &&
4824 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004825 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004826 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004827 else
4828 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004829 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004831 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4832 str_supported(video_supported != TPACPI_VIDEO_NONE),
4833 video_supported);
4834
Jan van den Berg72a979f2014-09-17 00:01:08 +02004835 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836}
4837
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004838static void video_exit(void)
4839{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004840 dbg_printk(TPACPI_DBG_EXIT,
4841 "restoring original video autoswitch mode\n");
4842 if (video_autosw_set(video_orig_autosw))
Joe Perches0978e012011-04-04 10:06:25 -07004843 pr_err("error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004844 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004845}
4846
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004847static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848{
4849 int status = 0;
4850 int i;
4851
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004852 switch (video_supported) {
4853 case TPACPI_VIDEO_570:
4854 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4855 TP_ACPI_VIDEO_570_PHSCMD))
4856 return -EIO;
4857 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4858 break;
4859 case TPACPI_VIDEO_770:
4860 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4861 return -EIO;
4862 if (i)
4863 status |= TP_ACPI_VIDEO_S_LCD;
4864 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4865 return -EIO;
4866 if (i)
4867 status |= TP_ACPI_VIDEO_S_CRT;
4868 break;
4869 case TPACPI_VIDEO_NEW:
4870 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4871 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4872 return -EIO;
4873 if (i)
4874 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004876 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4877 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4878 return -EIO;
4879 if (i)
4880 status |= TP_ACPI_VIDEO_S_LCD;
4881 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4882 return -EIO;
4883 if (i)
4884 status |= TP_ACPI_VIDEO_S_DVI;
4885 break;
4886 default:
4887 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
4890 return status;
4891}
4892
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004893static int video_outputsw_set(int status)
4894{
4895 int autosw;
4896 int res = 0;
4897
4898 switch (video_supported) {
4899 case TPACPI_VIDEO_570:
4900 res = acpi_evalf(NULL, NULL,
4901 "\\_SB.PHS2", "vdd",
4902 TP_ACPI_VIDEO_570_PHS2CMD,
4903 status | TP_ACPI_VIDEO_570_PHS2SET);
4904 break;
4905 case TPACPI_VIDEO_770:
4906 autosw = video_autosw_get();
4907 if (autosw < 0)
4908 return autosw;
4909
4910 res = video_autosw_set(1);
4911 if (res)
4912 return res;
4913 res = acpi_evalf(vid_handle, NULL,
4914 "ASWT", "vdd", status * 0x100, 0);
4915 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004916 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004917 return -EIO;
4918 }
4919 break;
4920 case TPACPI_VIDEO_NEW:
4921 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004922 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004923 break;
4924 default:
4925 return -ENOSYS;
4926 }
4927
Jan van den Berg72a979f2014-09-17 00:01:08 +02004928 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004929}
4930
4931static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004933 int autosw = 0;
4934
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004935 switch (video_supported) {
4936 case TPACPI_VIDEO_570:
4937 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4938 return -EIO;
4939 break;
4940 case TPACPI_VIDEO_770:
4941 case TPACPI_VIDEO_NEW:
4942 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4943 return -EIO;
4944 break;
4945 default:
4946 return -ENOSYS;
4947 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004948
4949 return autosw & 1;
4950}
4951
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004952static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004953{
Jan van den Berg72a979f2014-09-17 00:01:08 +02004954 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004955 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004956 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004957}
4958
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004959static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004960{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004961 int autosw = video_autosw_get();
4962 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004963
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004964 if (autosw < 0)
4965 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004966
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004967 switch (video_supported) {
4968 case TPACPI_VIDEO_570:
4969 res = video_autosw_set(1);
4970 if (res)
4971 return res;
4972 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4973 break;
4974 case TPACPI_VIDEO_770:
4975 case TPACPI_VIDEO_NEW:
4976 res = video_autosw_set(1);
4977 if (res)
4978 return res;
4979 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4980 break;
4981 default:
4982 return -ENOSYS;
4983 }
4984 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004985 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004986 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004987 }
4988
Jan van den Berg72a979f2014-09-17 00:01:08 +02004989 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004990}
4991
4992static int video_expand_toggle(void)
4993{
4994 switch (video_supported) {
4995 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004996 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004997 0 : -EIO;
4998 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004999 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005000 0 : -EIO;
5001 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005002 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005003 0 : -EIO;
5004 default:
5005 return -ENOSYS;
5006 }
5007 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005008}
5009
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005010static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005011{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005012 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005013
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005014 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005015 seq_printf(m, "status:\t\tnot supported\n");
5016 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005017 }
5018
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005019 /* Even reads can crash X.org, so... */
5020 if (!capable(CAP_SYS_ADMIN))
5021 return -EPERM;
5022
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005023 status = video_outputsw_get();
5024 if (status < 0)
5025 return status;
5026
5027 autosw = video_autosw_get();
5028 if (autosw < 0)
5029 return autosw;
5030
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005031 seq_printf(m, "status:\t\tsupported\n");
5032 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5033 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005034 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005035 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5036 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5037 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5038 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005039 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005040 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5041 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5042 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005043
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005044 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005045}
5046
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005047static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048{
5049 char *cmd;
5050 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005051 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005053 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005054 return -ENODEV;
5055
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005056 /* Even reads can crash X.org, let alone writes... */
5057 if (!capable(CAP_SYS_ADMIN))
5058 return -EPERM;
5059
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005060 enable = 0;
5061 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
5063 while ((cmd = next_cmd(&buf))) {
5064 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005065 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005067 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005069 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005071 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005072 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005073 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005074 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005075 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005076 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005077 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005079 res = video_autosw_set(1);
5080 if (res)
5081 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005083 res = video_autosw_set(0);
5084 if (res)
5085 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005087 res = video_outputsw_cycle();
5088 if (res)
5089 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005091 res = video_expand_toggle();
5092 if (res)
5093 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 } else
5095 return -EINVAL;
5096 }
5097
5098 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005099 status = video_outputsw_get();
5100 if (status < 0)
5101 return status;
5102 res = video_outputsw_set((status & ~disable) | enable);
5103 if (res)
5104 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 }
5106
5107 return 0;
5108}
5109
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005110static struct ibm_struct video_driver_data = {
5111 .name = "video",
5112 .read = video_read,
5113 .write = video_write,
5114 .exit = video_exit,
5115};
5116
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005117#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5118
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005119/*************************************************************************
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005120 * Keyboard backlight subdriver
5121 */
5122
Hans de Goedec685e202017-02-09 16:44:13 +01005123static enum led_brightness kbdlight_brightness;
5124static DEFINE_MUTEX(kbdlight_mutex);
5125
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005126static int kbdlight_set_level(int level)
5127{
Hans de Goedec685e202017-02-09 16:44:13 +01005128 int ret = 0;
5129
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005130 if (!hkey_handle)
5131 return -ENXIO;
5132
Hans de Goedec685e202017-02-09 16:44:13 +01005133 mutex_lock(&kbdlight_mutex);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005134
Hans de Goedec685e202017-02-09 16:44:13 +01005135 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5136 ret = -EIO;
5137 else
5138 kbdlight_brightness = level;
5139
5140 mutex_unlock(&kbdlight_mutex);
5141
5142 return ret;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005143}
5144
5145static int kbdlight_get_level(void)
5146{
5147 int status = 0;
5148
5149 if (!hkey_handle)
5150 return -ENXIO;
5151
5152 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5153 return -EIO;
5154
5155 if (status < 0)
5156 return status;
5157
5158 return status & 0x3;
5159}
5160
5161static bool kbdlight_is_supported(void)
5162{
5163 int status = 0;
5164
5165 if (!hkey_handle)
5166 return false;
5167
5168 if (!acpi_has_method(hkey_handle, "MLCG")) {
5169 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5170 return false;
5171 }
5172
5173 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5174 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5175 return false;
5176 }
5177
5178 if (status < 0) {
5179 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5180 return false;
5181 }
5182
5183 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5184 /*
5185 * Guessed test for keyboard backlight:
5186 *
5187 * Machines with backlight keyboard return:
5188 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5189 * b110100010010000000XX - ThinkPad x230
5190 * b010100000010000000XX - ThinkPad x240
5191 * b010100000010000000XX - ThinkPad W541
5192 * (XX is current backlight level)
5193 *
5194 * Machines without backlight keyboard return:
5195 * b10100001000000000000 - ThinkPad x230
5196 * b10110001000000000000 - ThinkPad E430
5197 * b00000000000000000000 - ThinkPad E450
5198 *
5199 * Candidate BITs for detection test (XOR):
5200 * b01000000001000000000
5201 * ^
5202 */
5203 return status & BIT(9);
5204}
5205
Hans de Goede86ec0c22017-02-09 16:44:12 +01005206static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005207 enum led_brightness brightness)
5208{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005209 return kbdlight_set_level(brightness);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005210}
5211
5212static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5213{
5214 int level;
5215
5216 level = kbdlight_get_level();
5217 if (level < 0)
5218 return 0;
5219
5220 return level;
5221}
5222
5223static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5224 .led_classdev = {
5225 .name = "tpacpi::kbd_backlight",
5226 .max_brightness = 2,
Hans de Goedec685e202017-02-09 16:44:13 +01005227 .flags = LED_BRIGHT_HW_CHANGED,
Hans de Goede86ec0c22017-02-09 16:44:12 +01005228 .brightness_set_blocking = &kbdlight_sysfs_set,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005229 .brightness_get = &kbdlight_sysfs_get,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005230 }
5231};
5232
5233static int __init kbdlight_init(struct ibm_init_struct *iibm)
5234{
5235 int rc;
5236
5237 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5238
5239 TPACPI_ACPIHANDLE_INIT(hkey);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005240
5241 if (!kbdlight_is_supported()) {
5242 tp_features.kbdlight = 0;
5243 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5244 return 1;
5245 }
5246
Hans de Goedec685e202017-02-09 16:44:13 +01005247 kbdlight_brightness = kbdlight_sysfs_get(NULL);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005248 tp_features.kbdlight = 1;
5249
5250 rc = led_classdev_register(&tpacpi_pdev->dev,
5251 &tpacpi_led_kbdlight.led_classdev);
5252 if (rc < 0) {
5253 tp_features.kbdlight = 0;
5254 return rc;
5255 }
5256
Hans de Goedec685e202017-02-09 16:44:13 +01005257 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5258 TP_ACPI_HKEY_KBD_LIGHT_MASK);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005259 return 0;
5260}
5261
5262static void kbdlight_exit(void)
5263{
5264 if (tp_features.kbdlight)
5265 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005266}
5267
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005268static int kbdlight_set_level_and_update(int level)
5269{
5270 int ret;
5271 struct led_classdev *led_cdev;
5272
5273 ret = kbdlight_set_level(level);
5274 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5275
5276 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5277 led_cdev->brightness = level;
5278
5279 return ret;
5280}
5281
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005282static int kbdlight_read(struct seq_file *m)
5283{
5284 int level;
5285
5286 if (!tp_features.kbdlight) {
5287 seq_printf(m, "status:\t\tnot supported\n");
5288 } else {
5289 level = kbdlight_get_level();
5290 if (level < 0)
5291 seq_printf(m, "status:\t\terror %d\n", level);
5292 else
5293 seq_printf(m, "status:\t\t%d\n", level);
5294 seq_printf(m, "commands:\t0, 1, 2\n");
5295 }
5296
5297 return 0;
5298}
5299
5300static int kbdlight_write(char *buf)
5301{
5302 char *cmd;
5303 int level = -1;
5304
5305 if (!tp_features.kbdlight)
5306 return -ENODEV;
5307
5308 while ((cmd = next_cmd(&buf))) {
5309 if (strlencmp(cmd, "0") == 0)
5310 level = 0;
5311 else if (strlencmp(cmd, "1") == 0)
5312 level = 1;
5313 else if (strlencmp(cmd, "2") == 0)
5314 level = 2;
5315 else
5316 return -EINVAL;
5317 }
5318
5319 if (level == -1)
5320 return -EINVAL;
5321
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005322 return kbdlight_set_level_and_update(level);
5323}
5324
5325static void kbdlight_suspend(void)
5326{
5327 struct led_classdev *led_cdev;
5328
5329 if (!tp_features.kbdlight)
5330 return;
5331
5332 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5333 led_update_brightness(led_cdev);
5334 led_classdev_suspend(led_cdev);
5335}
5336
5337static void kbdlight_resume(void)
5338{
5339 if (!tp_features.kbdlight)
5340 return;
5341
5342 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005343}
5344
5345static struct ibm_struct kbdlight_driver_data = {
5346 .name = "kbdlight",
5347 .read = kbdlight_read,
5348 .write = kbdlight_write,
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005349 .suspend = kbdlight_suspend,
5350 .resume = kbdlight_resume,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005351 .exit = kbdlight_exit,
5352};
5353
5354/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005355 * Light (thinklight) subdriver
5356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005358TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5359TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005360
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005361static int light_get_status(void)
5362{
5363 int status = 0;
5364
5365 if (tp_features.light_status) {
5366 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5367 return -EIO;
5368 return (!!status);
5369 }
5370
5371 return -ENXIO;
5372}
5373
5374static int light_set_status(int status)
5375{
5376 int rc;
5377
5378 if (tp_features.light) {
5379 if (cmos_handle) {
5380 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005381 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005382 TP_CMOS_THINKLIGHT_ON :
5383 TP_CMOS_THINKLIGHT_OFF);
5384 } else {
5385 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005386 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005387 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005388 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005389 }
5390
5391 return -ENXIO;
5392}
5393
Hans de Goede86ec0c22017-02-09 16:44:12 +01005394static int light_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005395 enum led_brightness brightness)
5396{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005397 return light_set_status((brightness != LED_OFF) ?
5398 TPACPI_LED_ON : TPACPI_LED_OFF);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005399}
5400
5401static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5402{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005403 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005404}
5405
5406static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5407 .led_classdev = {
5408 .name = "tpacpi::thinklight",
Hans de Goede86ec0c22017-02-09 16:44:12 +01005409 .brightness_set_blocking = &light_sysfs_set,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005410 .brightness_get = &light_sysfs_get,
5411 }
5412};
5413
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005414static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005416 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005417
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005418 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5419
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005420 if (tpacpi_is_ibm()) {
5421 TPACPI_ACPIHANDLE_INIT(ledb);
5422 TPACPI_ACPIHANDLE_INIT(lght);
5423 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005424 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005425
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005426 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005427 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005428
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005429 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005430 /* light status not supported on
5431 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005432 tp_features.light_status =
5433 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005435 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5436 str_supported(tp_features.light),
5437 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005438
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005439 if (!tp_features.light)
5440 return 1;
5441
5442 rc = led_classdev_register(&tpacpi_pdev->dev,
5443 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005444
5445 if (rc < 0) {
5446 tp_features.light = 0;
5447 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005448 } else {
5449 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005450 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005451
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005452 return rc;
5453}
5454
5455static void light_exit(void)
5456{
5457 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458}
5459
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005460static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005462 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005464 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005465 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005466 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005467 seq_printf(m, "status:\t\tunknown\n");
5468 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005469 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005470 status = light_get_status();
5471 if (status < 0)
5472 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005473 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5474 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478}
5479
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005480static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005483 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005484
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005485 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005486 return -ENODEV;
5487
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 while ((cmd = next_cmd(&buf))) {
5489 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005490 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005492 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 } else
5494 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 }
5496
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005497 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498}
5499
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005500static struct ibm_struct light_driver_data = {
5501 .name = "light",
5502 .read = light_read,
5503 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005504 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005505};
5506
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005507/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005508 * CMOS subdriver
5509 */
5510
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005511/* sysfs cmos_command -------------------------------------------------- */
5512static ssize_t cmos_command_store(struct device *dev,
5513 struct device_attribute *attr,
5514 const char *buf, size_t count)
5515{
5516 unsigned long cmos_cmd;
5517 int res;
5518
5519 if (parse_strtoul(buf, 21, &cmos_cmd))
5520 return -EINVAL;
5521
5522 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005523 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005524}
5525
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01005526static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005527
5528/* --------------------------------------------------------------------- */
5529
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005530static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005531{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005532 int res;
5533
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005534 vdbg_printk(TPACPI_DBG_INIT,
5535 "initializing cmos commands subdriver\n");
5536
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005537 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005538
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005539 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5540 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005541
5542 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5543 if (res)
5544 return res;
5545
Jan van den Berg72a979f2014-09-17 00:01:08 +02005546 return (cmos_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005547}
5548
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005549static void cmos_exit(void)
5550{
5551 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5552}
5553
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005554static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005556 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5557 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005559 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005561 seq_printf(m, "status:\t\tsupported\n");
5562 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 }
5564
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566}
5567
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005568static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569{
5570 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005571 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
5573 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005574 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5575 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 /* cmos_cmd set */
5577 } else
5578 return -EINVAL;
5579
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005580 res = issue_thinkpad_cmos_command(cmos_cmd);
5581 if (res)
5582 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 }
5584
5585 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005586}
5587
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005588static struct ibm_struct cmos_driver_data = {
5589 .name = "cmos",
5590 .read = cmos_read,
5591 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005592 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005593};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005594
5595/*************************************************************************
5596 * LED subdriver
5597 */
5598
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005599enum led_access_mode {
5600 TPACPI_LED_NONE = 0,
5601 TPACPI_LED_570, /* 570 */
5602 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5603 TPACPI_LED_NEW, /* all others */
5604};
5605
5606enum { /* For TPACPI_LED_OLD */
5607 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5608 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5609 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5610};
5611
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005612static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005613
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005614static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005615
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005616#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005617static struct tpacpi_led_classdev *tpacpi_leds;
5618static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005619static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005620 /* there's a limit of 19 chars + NULL before 2.6.26 */
5621 "tpacpi::power",
5622 "tpacpi:orange:batt",
5623 "tpacpi:green:batt",
5624 "tpacpi::dock_active",
5625 "tpacpi::bay_active",
5626 "tpacpi::dock_batt",
5627 "tpacpi::unknown_led",
5628 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005629 "tpacpi::dock_status1",
5630 "tpacpi::dock_status2",
5631 "tpacpi::unknown_led2",
5632 "tpacpi::unknown_led3",
5633 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005634};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005635#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005636
5637static inline bool tpacpi_is_led_restricted(const unsigned int led)
5638{
5639#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5640 return false;
5641#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005642 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005643#endif
5644}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005645
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005646static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005647{
5648 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005649 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005650
5651 switch (led_supported) {
5652 case TPACPI_LED_570:
5653 if (!acpi_evalf(ec_handle,
5654 &status, "GLED", "dd", 1 << led))
5655 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005656 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005657 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005658 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005659 TPACPI_LED_ON :
5660 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005661 tpacpi_led_state_cache[led] = led_s;
5662 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005663 default:
5664 return -ENXIO;
5665 }
5666
5667 /* not reached */
5668}
5669
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005670static int led_set_status(const unsigned int led,
5671 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005672{
5673 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005674 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5675 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005676
5677 int rc = 0;
5678
5679 switch (led_supported) {
5680 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005681 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005682 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005683 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005684 if (unlikely(tpacpi_is_led_restricted(led)))
5685 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005686 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5687 (1 << led), led_sled_arg1[ledstatus]))
5688 rc = -EIO;
5689 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005690 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005691 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
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 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5697 if (rc >= 0)
5698 rc = ec_write(TPACPI_LED_EC_HLBL,
5699 (ledstatus == TPACPI_LED_BLINK) << led);
5700 if (rc >= 0)
5701 rc = ec_write(TPACPI_LED_EC_HLCL,
5702 (ledstatus != TPACPI_LED_OFF) << led);
5703 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005704 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005705 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005706 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5707 return -EINVAL;
5708 if (unlikely(tpacpi_is_led_restricted(led)))
5709 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005710 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5711 led, led_led_arg1[ledstatus]))
5712 rc = -EIO;
5713 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005714 default:
5715 rc = -ENXIO;
5716 }
5717
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005718 if (!rc)
5719 tpacpi_led_state_cache[led] = ledstatus;
5720
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005721 return rc;
5722}
5723
Hans de Goede86ec0c22017-02-09 16:44:12 +01005724static int led_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005725 enum led_brightness brightness)
5726{
5727 struct tpacpi_led_classdev *data = container_of(led_cdev,
5728 struct tpacpi_led_classdev, led_classdev);
Hans de Goede86ec0c22017-02-09 16:44:12 +01005729 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005730
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005731 if (brightness == LED_OFF)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005732 new_state = TPACPI_LED_OFF;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005733 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005734 new_state = TPACPI_LED_ON;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005735 else
Hans de Goede86ec0c22017-02-09 16:44:12 +01005736 new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005737
Hans de Goede86ec0c22017-02-09 16:44:12 +01005738 return led_set_status(data->led, new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005739}
5740
5741static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5742 unsigned long *delay_on, unsigned long *delay_off)
5743{
5744 struct tpacpi_led_classdev *data = container_of(led_cdev,
5745 struct tpacpi_led_classdev, led_classdev);
5746
5747 /* Can we choose the flash rate? */
5748 if (*delay_on == 0 && *delay_off == 0) {
5749 /* yes. set them to the hardware blink rate (1 Hz) */
5750 *delay_on = 500; /* ms */
5751 *delay_off = 500; /* ms */
5752 } else if ((*delay_on != 500) || (*delay_off != 500))
5753 return -EINVAL;
5754
Hans de Goede86ec0c22017-02-09 16:44:12 +01005755 return led_set_status(data->led, TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005756}
5757
5758static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5759{
5760 int rc;
5761
5762 struct tpacpi_led_classdev *data = container_of(led_cdev,
5763 struct tpacpi_led_classdev, led_classdev);
5764
5765 rc = led_get_status(data->led);
5766
5767 if (rc == TPACPI_LED_OFF || rc < 0)
5768 rc = LED_OFF; /* no error handling in led class :( */
5769 else
5770 rc = LED_FULL;
5771
5772 return rc;
5773}
5774
5775static void led_exit(void)
5776{
5777 unsigned int i;
5778
5779 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5780 if (tpacpi_leds[i].led_classdev.name)
5781 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5782 }
5783
5784 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005785}
5786
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005787static int __init tpacpi_init_led(unsigned int led)
5788{
5789 int rc;
5790
5791 tpacpi_leds[led].led = led;
5792
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005793 /* LEDs with no name don't get registered */
5794 if (!tpacpi_led_names[led])
5795 return 0;
5796
Hans de Goede86ec0c22017-02-09 16:44:12 +01005797 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005798 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5799 if (led_supported == TPACPI_LED_570)
5800 tpacpi_leds[led].led_classdev.brightness_get =
5801 &led_sysfs_get;
5802
5803 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5804
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005805 rc = led_classdev_register(&tpacpi_pdev->dev,
5806 &tpacpi_leds[led].led_classdev);
5807 if (rc < 0)
5808 tpacpi_leds[led].led_classdev.name = NULL;
5809
5810 return rc;
5811}
5812
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005813static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5814 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5815 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5816 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5817
5818 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5819 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5820 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5821 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5822 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5823 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5824 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5825 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5826
5827 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5828 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5829 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5830 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5831 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5832
5833 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5834 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5835 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5836 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5837
5838 /* (1) - may have excess leds enabled on MSB */
5839
5840 /* Defaults (order matters, keep last, don't reorder!) */
5841 { /* Lenovo */
5842 .vendor = PCI_VENDOR_ID_LENOVO,
5843 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5844 .quirks = 0x1fffU,
5845 },
5846 { /* IBM ThinkPads with no EC version string */
5847 .vendor = PCI_VENDOR_ID_IBM,
5848 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5849 .quirks = 0x00ffU,
5850 },
5851 { /* IBM ThinkPads with EC version string */
5852 .vendor = PCI_VENDOR_ID_IBM,
5853 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5854 .quirks = 0x00bfU,
5855 },
5856};
5857
5858#undef TPACPI_LEDQ_IBM
5859#undef TPACPI_LEDQ_LNV
5860
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005861static enum led_access_mode __init led_init_detect_mode(void)
5862{
5863 acpi_status status;
5864
5865 if (tpacpi_is_ibm()) {
5866 /* 570 */
5867 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5868 if (ACPI_SUCCESS(status))
5869 return TPACPI_LED_570;
5870
5871 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5872 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5873 if (ACPI_SUCCESS(status))
5874 return TPACPI_LED_OLD;
5875 }
5876
5877 /* most others */
5878 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5879 if (ACPI_SUCCESS(status))
5880 return TPACPI_LED_NEW;
5881
5882 /* R30, R31, and unknown firmwares */
5883 led_handle = NULL;
5884 return TPACPI_LED_NONE;
5885}
5886
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005887static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005888{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005889 unsigned int i;
5890 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005891 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005892
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005893 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5894
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005895 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005896
Adam Leefcb44e12013-06-07 16:20:08 +08005897 if (led_supported != TPACPI_LED_NONE) {
5898 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5899 ARRAY_SIZE(led_useful_qtable));
5900
5901 if (!useful_leds) {
5902 led_handle = NULL;
5903 led_supported = TPACPI_LED_NONE;
5904 }
5905 }
5906
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005907 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5908 str_supported(led_supported), led_supported);
5909
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005910 if (led_supported == TPACPI_LED_NONE)
5911 return 1;
5912
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005913 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5914 GFP_KERNEL);
5915 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005916 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005917 return -ENOMEM;
5918 }
5919
5920 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08005921 tpacpi_leds[i].led = -1;
5922
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005923 if (!tpacpi_is_led_restricted(i) &&
5924 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005925 rc = tpacpi_init_led(i);
5926 if (rc < 0) {
5927 led_exit();
5928 return rc;
5929 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005930 }
5931 }
5932
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005933#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Joe Perches0978e012011-04-04 10:06:25 -07005934 pr_notice("warning: userspace override of important "
5935 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005936#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005937 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005938}
5939
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005940#define str_led_status(s) \
5941 ((s) == TPACPI_LED_OFF ? "off" : \
5942 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005943
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005944static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005946 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005947 seq_printf(m, "status:\t\tnot supported\n");
5948 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005949 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005950 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005951
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005952 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005953 /* 570 */
5954 int i, status;
5955 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005956 status = led_get_status(i);
5957 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005958 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005959 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005960 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005961 }
5962 }
5963
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005964 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005965 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968}
5969
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005970static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971{
5972 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005973 int led, rc;
5974 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005975
5976 if (!led_supported)
5977 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978
5979 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08005980 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 return -EINVAL;
5982
Adam Leeedf2d772013-06-08 16:51:15 +08005983 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5984 tpacpi_leds[led].led < 0)
5985 return -ENODEV;
5986
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005987 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005988 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005990 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005991 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005992 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005993 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005994 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005995 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005996
5997 rc = led_set_status(led, s);
5998 if (rc < 0)
5999 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 }
6001
6002 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006003}
6004
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006005static struct ibm_struct led_driver_data = {
6006 .name = "led",
6007 .read = led_read,
6008 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006009 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006010};
6011
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006012/*************************************************************************
6013 * Beep subdriver
6014 */
6015
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006016TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006017
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006018#define TPACPI_BEEP_Q1 0x0001
6019
6020static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6021 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6022 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6023};
6024
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006025static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006026{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006027 unsigned long quirks;
6028
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006029 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6030
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006031 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006032
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006033 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6034 str_supported(beep_handle != NULL));
6035
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006036 quirks = tpacpi_check_quirks(beep_quirk_table,
6037 ARRAY_SIZE(beep_quirk_table));
6038
6039 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6040
Jan van den Berg72a979f2014-09-17 00:01:08 +02006041 return (beep_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006042}
6043
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006044static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006046 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006047 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006048 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006049 seq_printf(m, "status:\t\tsupported\n");
6050 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054}
6055
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006056static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057{
6058 char *cmd;
6059 int beep_cmd;
6060
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006061 if (!beep_handle)
6062 return -ENODEV;
6063
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006065 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6066 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 /* beep_cmd set */
6068 } else
6069 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006070 if (tp_features.beep_needs_two_args) {
6071 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6072 beep_cmd, 0))
6073 return -EIO;
6074 } else {
6075 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6076 beep_cmd))
6077 return -EIO;
6078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 }
6080
6081 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006082}
6083
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006084static struct ibm_struct beep_driver_data = {
6085 .name = "beep",
6086 .read = beep_read,
6087 .write = beep_write,
6088};
6089
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006090/*************************************************************************
6091 * Thermal subdriver
6092 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006093
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006094enum thermal_access_mode {
6095 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6096 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6097 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6098 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6099 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6100};
6101
6102enum { /* TPACPI_THERMAL_TPEC_* */
6103 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6104 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6105 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006106
6107 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006108};
6109
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006110
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006111#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6112struct ibm_thermal_sensors_struct {
6113 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6114};
6115
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006116static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006117
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006118/* idx is zero-based */
6119static int thermal_get_sensor(int idx, s32 *value)
6120{
6121 int t;
6122 s8 tmp;
6123 char tmpi[5];
6124
6125 t = TP_EC_THERMAL_TMP0;
6126
6127 switch (thermal_read_mode) {
6128#if TPACPI_MAX_THERMAL_SENSORS >= 16
6129 case TPACPI_THERMAL_TPEC_16:
6130 if (idx >= 8 && idx <= 15) {
6131 t = TP_EC_THERMAL_TMP8;
6132 idx -= 8;
6133 }
6134 /* fallthrough */
6135#endif
6136 case TPACPI_THERMAL_TPEC_8:
6137 if (idx <= 7) {
6138 if (!acpi_ec_read(t + idx, &tmp))
6139 return -EIO;
6140 *value = tmp * 1000;
6141 return 0;
6142 }
6143 break;
6144
6145 case TPACPI_THERMAL_ACPI_UPDT:
6146 if (idx <= 7) {
6147 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6148 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6149 return -EIO;
6150 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6151 return -EIO;
6152 *value = (t - 2732) * 100;
6153 return 0;
6154 }
6155 break;
6156
6157 case TPACPI_THERMAL_ACPI_TMP07:
6158 if (idx <= 7) {
6159 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6160 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6161 return -EIO;
6162 if (t > 127 || t < -127)
6163 t = TP_EC_THERMAL_TMP_NA;
6164 *value = t * 1000;
6165 return 0;
6166 }
6167 break;
6168
6169 case TPACPI_THERMAL_NONE:
6170 default:
6171 return -ENOSYS;
6172 }
6173
6174 return -EINVAL;
6175}
6176
6177static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6178{
6179 int res, i;
6180 int n;
6181
6182 n = 8;
6183 i = 0;
6184
6185 if (!s)
6186 return -EINVAL;
6187
6188 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6189 n = 16;
6190
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006191 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006192 res = thermal_get_sensor(i, &s->temp[i]);
6193 if (res)
6194 return res;
6195 }
6196
6197 return n;
6198}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006199
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006200static void thermal_dump_all_sensors(void)
6201{
6202 int n, i;
6203 struct ibm_thermal_sensors_struct t;
6204
6205 n = thermal_get_sensors(&t);
6206 if (n <= 0)
6207 return;
6208
Joe Perches0978e012011-04-04 10:06:25 -07006209 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006210
6211 for (i = 0; i < n; i++) {
6212 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07006213 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006214 else
Joe Perches0978e012011-04-04 10:06:25 -07006215 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006216 }
6217
Joe Perches0978e012011-04-04 10:06:25 -07006218 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006219}
6220
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006221/* sysfs temp##_input -------------------------------------------------- */
6222
6223static ssize_t thermal_temp_input_show(struct device *dev,
6224 struct device_attribute *attr,
6225 char *buf)
6226{
6227 struct sensor_device_attribute *sensor_attr =
6228 to_sensor_dev_attr(attr);
6229 int idx = sensor_attr->index;
6230 s32 value;
6231 int res;
6232
6233 res = thermal_get_sensor(idx, &value);
6234 if (res)
6235 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006236 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006237 return -ENXIO;
6238
6239 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6240}
6241
6242#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006243 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6244 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006245
6246static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6247 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6248 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6249 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6250 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6251 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6252 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6253 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6254 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6255 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6256 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6257 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6258 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6259 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6260 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6261 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6262 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6263};
6264
6265#define THERMAL_ATTRS(X) \
6266 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6267
6268static struct attribute *thermal_temp_input_attr[] = {
6269 THERMAL_ATTRS(8),
6270 THERMAL_ATTRS(9),
6271 THERMAL_ATTRS(10),
6272 THERMAL_ATTRS(11),
6273 THERMAL_ATTRS(12),
6274 THERMAL_ATTRS(13),
6275 THERMAL_ATTRS(14),
6276 THERMAL_ATTRS(15),
6277 THERMAL_ATTRS(0),
6278 THERMAL_ATTRS(1),
6279 THERMAL_ATTRS(2),
6280 THERMAL_ATTRS(3),
6281 THERMAL_ATTRS(4),
6282 THERMAL_ATTRS(5),
6283 THERMAL_ATTRS(6),
6284 THERMAL_ATTRS(7),
6285 NULL
6286};
6287
6288static const struct attribute_group thermal_temp_input16_group = {
6289 .attrs = thermal_temp_input_attr
6290};
6291
6292static const struct attribute_group thermal_temp_input8_group = {
6293 .attrs = &thermal_temp_input_attr[8]
6294};
6295
6296#undef THERMAL_SENSOR_ATTR_TEMP
6297#undef THERMAL_ATTRS
6298
6299/* --------------------------------------------------------------------- */
6300
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006301static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006302{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006303 u8 t, ta1, ta2;
6304 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006305 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006306 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006307
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006308 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6309
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006310 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006311
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03006312 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006313 /*
6314 * Direct EC access mode: sensors at registers
6315 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6316 * non-implemented, thermal sensors return 0x80 when
6317 * not available
6318 */
6319
6320 ta1 = ta2 = 0;
6321 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006322 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006323 ta1 |= t;
6324 } else {
6325 ta1 = 0;
6326 break;
6327 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006328 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006329 ta2 |= t;
6330 } else {
6331 ta1 = 0;
6332 break;
6333 }
6334 }
6335 if (ta1 == 0) {
6336 /* This is sheer paranoia, but we handle it anyway */
6337 if (acpi_tmp7) {
Joe Perches0978e012011-04-04 10:06:25 -07006338 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006339 "falling back to ACPI TMPx access "
6340 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006341 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006342 } else {
Joe Perches0978e012011-04-04 10:06:25 -07006343 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006344 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006345 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006346 }
6347 } else {
6348 thermal_read_mode =
6349 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006350 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006351 }
6352 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006353 if (tpacpi_is_ibm() &&
6354 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006355 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006356 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006357 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006358 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006359 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006360 }
6361 } else {
6362 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006363 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006364 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006365
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006366 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6367 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6368 thermal_read_mode);
6369
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006370 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006371 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006372 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006373 &thermal_temp_input16_group);
6374 if (res)
6375 return res;
6376 break;
6377 case TPACPI_THERMAL_TPEC_8:
6378 case TPACPI_THERMAL_ACPI_TMP07:
6379 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006380 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006381 &thermal_temp_input8_group);
6382 if (res)
6383 return res;
6384 break;
6385 case TPACPI_THERMAL_NONE:
6386 default:
6387 return 1;
6388 }
6389
6390 return 0;
6391}
6392
6393static void thermal_exit(void)
6394{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006395 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006396 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006397 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006398 &thermal_temp_input16_group);
6399 break;
6400 case TPACPI_THERMAL_TPEC_8:
6401 case TPACPI_THERMAL_ACPI_TMP07:
6402 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006403 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08006404 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006405 break;
6406 case TPACPI_THERMAL_NONE:
6407 default:
6408 break;
6409 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006410}
6411
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006412static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006413{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006414 int n, i;
6415 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006416
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006417 n = thermal_get_sensors(&t);
6418 if (unlikely(n < 0))
6419 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006420
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006421 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006422
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006423 if (n > 0) {
6424 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006425 seq_printf(m, "%d ", t.temp[i] / 1000);
6426 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006427 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006428 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006429
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006430 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006431}
6432
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006433static struct ibm_struct thermal_driver_data = {
6434 .name = "thermal",
6435 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006436 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006437};
6438
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006439/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006440 * Backlight/brightness subdriver
6441 */
Holger Macht8acb0252006-10-20 14:30:28 -07006442
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006443#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6444
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006445/*
6446 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6447 * CMOS NVRAM byte 0x5E, bits 0-3.
6448 *
6449 * EC HBRV (0x31) has the following layout
6450 * Bit 7: unknown function
6451 * Bit 6: unknown function
6452 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6453 * Bit 4: must be set to zero to avoid problems
6454 * Bit 3-0: backlight brightness level
6455 *
6456 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006457 *
6458 * WARNING: The X61 has been verified to use HBRV for something else, so
6459 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6460 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006461 */
6462
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006463enum {
6464 TP_EC_BACKLIGHT = 0x31,
6465
6466 /* TP_EC_BACKLIGHT bitmasks */
6467 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6468 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6469 TP_EC_BACKLIGHT_MAPSW = 0x20,
6470};
6471
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006472enum tpacpi_brightness_access_mode {
6473 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6474 TPACPI_BRGHT_MODE_EC, /* EC control */
6475 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6476 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6477 TPACPI_BRGHT_MODE_MAX
6478};
6479
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006480static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006481
6482static enum tpacpi_brightness_access_mode brightness_mode =
6483 TPACPI_BRGHT_MODE_MAX;
6484
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006485static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6486
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006487static struct mutex brightness_mutex;
6488
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006489/* NVRAM brightness access,
6490 * call with brightness_mutex held! */
6491static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006492{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006493 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006494
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006495 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6496 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6497 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006498 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006499
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006500 return lnvram;
6501}
6502
6503static void tpacpi_brightness_checkpoint_nvram(void)
6504{
6505 u8 lec = 0;
6506 u8 b_nvram;
6507
6508 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6509 return;
6510
6511 vdbg_printk(TPACPI_DBG_BRGHT,
6512 "trying to checkpoint backlight level to NVRAM...\n");
6513
6514 if (mutex_lock_killable(&brightness_mutex) < 0)
6515 return;
6516
6517 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6518 goto unlock;
6519 lec &= TP_EC_BACKLIGHT_LVLMSK;
6520 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6521
6522 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6523 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6524 /* NVRAM needs update */
6525 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6526 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6527 b_nvram |= lec;
6528 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6529 dbg_printk(TPACPI_DBG_BRGHT,
6530 "updated NVRAM backlight level to %u (0x%02x)\n",
6531 (unsigned int) lec, (unsigned int) b_nvram);
6532 } else
6533 vdbg_printk(TPACPI_DBG_BRGHT,
6534 "NVRAM backlight level already is %u (0x%02x)\n",
6535 (unsigned int) lec, (unsigned int) b_nvram);
6536
6537unlock:
6538 mutex_unlock(&brightness_mutex);
6539}
6540
6541
6542/* call with brightness_mutex held! */
6543static int tpacpi_brightness_get_raw(int *status)
6544{
6545 u8 lec = 0;
6546
6547 switch (brightness_mode) {
6548 case TPACPI_BRGHT_MODE_UCMS_STEP:
6549 *status = tpacpi_brightness_nvram_get();
6550 return 0;
6551 case TPACPI_BRGHT_MODE_EC:
6552 case TPACPI_BRGHT_MODE_ECNVRAM:
6553 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006554 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006555 *status = lec;
6556 return 0;
6557 default:
6558 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006559 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006560}
6561
6562/* call with brightness_mutex held! */
6563/* do NOT call with illegal backlight level value */
6564static int tpacpi_brightness_set_ec(unsigned int value)
6565{
6566 u8 lec = 0;
6567
6568 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6569 return -EIO;
6570
6571 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6572 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6573 (value & TP_EC_BACKLIGHT_LVLMSK))))
6574 return -EIO;
6575
6576 return 0;
6577}
6578
6579/* call with brightness_mutex held! */
6580static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6581{
6582 int cmos_cmd, inc;
6583 unsigned int current_value, i;
6584
6585 current_value = tpacpi_brightness_nvram_get();
6586
6587 if (value == current_value)
6588 return 0;
6589
6590 cmos_cmd = (value > current_value) ?
6591 TP_CMOS_BRIGHTNESS_UP :
6592 TP_CMOS_BRIGHTNESS_DOWN;
6593 inc = (value > current_value) ? 1 : -1;
6594
6595 for (i = current_value; i != value; i += inc)
6596 if (issue_thinkpad_cmos_command(cmos_cmd))
6597 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006598
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006599 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006600}
6601
6602/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006603static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006604{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006605 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006606
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006607 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006608 return -EINVAL;
6609
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006610 vdbg_printk(TPACPI_DBG_BRGHT,
6611 "set backlight level to %d\n", value);
6612
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006613 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006614 if (res < 0)
6615 return res;
6616
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006617 switch (brightness_mode) {
6618 case TPACPI_BRGHT_MODE_EC:
6619 case TPACPI_BRGHT_MODE_ECNVRAM:
6620 res = tpacpi_brightness_set_ec(value);
6621 break;
6622 case TPACPI_BRGHT_MODE_UCMS_STEP:
6623 res = tpacpi_brightness_set_ucmsstep(value);
6624 break;
6625 default:
6626 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006627 }
6628
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006629 mutex_unlock(&brightness_mutex);
6630 return res;
6631}
6632
6633/* sysfs backlight class ----------------------------------------------- */
6634
6635static int brightness_update_status(struct backlight_device *bd)
6636{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006637 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006638 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6639 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006640 bd->props.brightness : 0;
6641
6642 dbg_printk(TPACPI_DBG_BRGHT,
6643 "backlight: attempt to set level to %d\n",
6644 level);
6645
6646 /* it is the backlight class's job (caller) to handle
6647 * EINTR and other errors properly */
6648 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006649}
Holger Macht8acb0252006-10-20 14:30:28 -07006650
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006651static int brightness_get(struct backlight_device *bd)
6652{
6653 int status, res;
6654
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006655 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006656 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006657 return 0;
6658
6659 res = tpacpi_brightness_get_raw(&status);
6660
6661 mutex_unlock(&brightness_mutex);
6662
6663 if (res < 0)
6664 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006665
6666 return status & TP_EC_BACKLIGHT_LVLMSK;
6667}
6668
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006669static void tpacpi_brightness_notify_change(void)
6670{
6671 backlight_force_update(ibm_backlight_device,
6672 BACKLIGHT_UPDATE_HOTKEY);
6673}
6674
Lionel Debrouxacc24722010-11-16 14:14:02 +01006675static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006676 .get_brightness = brightness_get,
6677 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006678};
6679
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006680/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006681
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006682/*
6683 * Call _BCL method of video device. On some ThinkPads this will
6684 * switch the firmware to the ACPI brightness control mode.
6685 */
6686
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006687static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6688{
6689 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6690 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006691 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006692 int rc;
6693
Aaron Lu46445b62013-10-11 21:27:46 +08006694 if (acpi_bus_get_device(handle, &device))
6695 return 0;
6696
6697 rc = 0;
6698 list_for_each_entry(child, &device->children, node) {
6699 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6700 NULL, &buffer);
6701 if (ACPI_FAILURE(status))
6702 continue;
6703
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006704 obj = (union acpi_object *)buffer.pointer;
6705 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006706 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006707 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006708 rc = 0;
6709 } else {
6710 rc = obj->package.count;
6711 }
Aaron Lu46445b62013-10-11 21:27:46 +08006712 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006713 }
6714
6715 kfree(buffer.pointer);
6716 return rc;
6717}
6718
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006719
6720/*
6721 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6722 */
6723static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6724{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006725 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006726 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006727
Aaron Lu46445b62013-10-11 21:27:46 +08006728 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006729 if (video_device)
6730 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006731
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006732 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006733
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006734 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006735}
6736
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006737/*
6738 * These are only useful for models that have only one possibility
6739 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6740 * these quirks.
6741 */
6742#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6743#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6744#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6745
6746static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6747 /* Models with ATI GPUs known to require ECNVRAM mode */
6748 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6749
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006750 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006751 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006752 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006753 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006754 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6755
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006756 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006757 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006758 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6759 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006760
6761 /* Models with Intel GMA900 */
6762 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6763 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6764 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6765};
6766
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006767/*
6768 * Returns < 0 for error, otherwise sets tp_features.bright_*
6769 * and bright_maxlvl.
6770 */
6771static void __init tpacpi_detect_brightness_capabilities(void)
6772{
6773 unsigned int b;
6774
6775 vdbg_printk(TPACPI_DBG_INIT,
6776 "detecting firmware brightness interface capabilities\n");
6777
6778 /* we could run a quirks check here (same table used by
6779 * brightness_init) if needed */
6780
6781 /*
6782 * We always attempt to detect acpi support, so as to switch
6783 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6784 * going to publish a backlight interface
6785 */
6786 b = tpacpi_check_std_acpi_brightness_support();
6787 switch (b) {
6788 case 16:
6789 bright_maxlvl = 15;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006790 break;
6791 case 8:
6792 case 0:
6793 bright_maxlvl = 7;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006794 break;
6795 default:
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006796 tp_features.bright_unkfw = 1;
6797 bright_maxlvl = b - 1;
6798 }
Eric Curtin15c75622016-01-30 16:55:59 +00006799 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006800}
6801
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006802static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006803{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006804 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006805 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006806 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006807
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006808 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6809
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006810 mutex_init(&brightness_mutex);
6811
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006812 quirks = tpacpi_check_quirks(brightness_quirk_table,
6813 ARRAY_SIZE(brightness_quirk_table));
6814
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006815 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006816
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006817 /* if it is unknown, we don't handle it: it wouldn't be safe */
6818 if (tp_features.bright_unkfw)
6819 return 1;
6820
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006821 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006822 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006823 "brightness support disabled by "
6824 "module parameter\n");
6825 return 1;
6826 }
6827
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02006828 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006829 if (brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006830 pr_info("Standard ACPI backlight interface "
6831 "available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006832 return 1;
6833 } else if (brightness_enable == 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006834 pr_warn("Cannot enable backlight brightness support, "
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006835 "ACPI is already handling it. Refer to the "
Joe Perches0978e012011-04-04 10:06:25 -07006836 "acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006837 return 1;
6838 }
6839 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006840 pr_notice("Standard ACPI backlight interface not "
6841 "available, thinkpad_acpi native "
6842 "brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006843 }
6844
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006845 /*
6846 * Check for module parameter bogosity, note that we
6847 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6848 * able to detect "unspecified"
6849 */
6850 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006851 return -EINVAL;
6852
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006853 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6854 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6855 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006856 if (quirks & TPACPI_BRGHT_Q_EC)
6857 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6858 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006859 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6860
6861 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006862 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006863 brightness_mode);
6864 }
6865
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006866 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006867 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006868 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6869 brightness_mode == TPACPI_BRGHT_MODE_EC))
6870 return -EINVAL;
6871
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006872 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006873 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006874
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006875 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006876 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006877 props.max_brightness = bright_maxlvl;
6878 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006879 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6880 NULL, NULL,
6881 &ibm_backlight_data,
6882 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006883 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006884 int rc = PTR_ERR(ibm_backlight_device);
6885 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006886 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006887 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006888 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006889 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6890 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006891
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006892 if (quirks & TPACPI_BRGHT_Q_ASK) {
Joe Perches0978e012011-04-04 10:06:25 -07006893 pr_notice("brightness: will use unverified default: "
6894 "brightness_mode=%d\n", brightness_mode);
6895 pr_notice("brightness: please report to %s whether it works well "
6896 "or not on your ThinkPad\n", TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006897 }
6898
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006899 /* Added by mistake in early 2007. Probably useless, but it could
6900 * be working around some unknown firmware problem where the value
6901 * read at startup doesn't match the real hardware state... so leave
6902 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006903 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006904
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006905 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6906 "brightness: registering brightness hotkeys "
6907 "as change notification\n");
6908 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6909 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006910 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006911 return 0;
6912}
6913
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006914static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006915{
6916 tpacpi_brightness_checkpoint_nvram();
6917}
6918
6919static void brightness_shutdown(void)
6920{
6921 tpacpi_brightness_checkpoint_nvram();
6922}
6923
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006924static void brightness_exit(void)
6925{
6926 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006927 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006928 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006929 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006930 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006931
6932 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006933}
6934
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006935static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006936{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006937 int level;
6938
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006939 level = brightness_get(NULL);
6940 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006941 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006942 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006943 seq_printf(m, "level:\t\t%d\n", level);
6944 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006945 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6946 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006947 }
6948
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006949 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006950}
6951
6952static int brightness_write(char *buf)
6953{
6954 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006955 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006956 char *cmd;
6957
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006958 level = brightness_get(NULL);
6959 if (level < 0)
6960 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006961
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006962 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006963 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006964 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006965 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006966 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006967 if (level > 0)
6968 level--;
6969 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006970 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006971 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006972 } else
6973 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006974 }
6975
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006976 tpacpi_disclose_usertask("procfs brightness",
6977 "set level to %d\n", level);
6978
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006979 /*
6980 * Now we know what the final level should be, so we try to set it.
6981 * Doing it this way makes the syscall restartable in case of EINTR
6982 */
6983 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006984 if (!rc && ibm_backlight_device)
6985 backlight_force_update(ibm_backlight_device,
6986 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02006987 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006988}
6989
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006990static struct ibm_struct brightness_driver_data = {
6991 .name = "brightness",
6992 .read = brightness_read,
6993 .write = brightness_write,
6994 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006995 .suspend = brightness_suspend,
6996 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006997};
6998
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006999/*************************************************************************
7000 * Volume subdriver
7001 */
7002
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007003/*
7004 * IBM ThinkPads have a simple volume controller with MUTE gating.
7005 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7006 *
7007 * Since the *61 series (and probably also the later *60 series), Lenovo
7008 * ThinkPads only implement the MUTE gate.
7009 *
7010 * EC register 0x30
7011 * Bit 6: MUTE (1 mutes sound)
7012 * Bit 3-0: Volume
7013 * Other bits should be zero as far as we know.
7014 *
7015 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7016 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7017 * such as bit 7 which is used to detect repeated presses of MUTE,
7018 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007019 *
7020 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7021 * in response to user input. Unfortunately, this rarely works well.
7022 * The laptop changes the state of its internal MUTE gate and, on some
7023 * models, sends KEY_MUTE, causing any user code that responds to the
7024 * mute button to get confused. The hardware MUTE gate is also
7025 * unnecessary, since user code can handle the mute button without
7026 * kernel or EC help.
7027 *
7028 * To avoid confusing userspace, we simply disable all EC-based mute
7029 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007030 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007031
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007032#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7033
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007034#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7035#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7036#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7037
Takashi Iwaicab66612013-10-24 16:06:32 +02007038#if SNDRV_CARDS <= 32
7039#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7040#else
7041#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7042#endif
7043static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007044static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307045static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007046
7047struct tpacpi_alsa_data {
7048 struct snd_card *card;
7049 struct snd_ctl_elem_id *ctl_mute_id;
7050 struct snd_ctl_elem_id *ctl_vol_id;
7051};
7052
7053static struct snd_card *alsa_card;
7054
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007055enum {
7056 TP_EC_AUDIO = 0x30,
7057
7058 /* TP_EC_AUDIO bits */
7059 TP_EC_AUDIO_MUTESW = 6,
7060
7061 /* TP_EC_AUDIO bitmasks */
7062 TP_EC_AUDIO_LVL_MSK = 0x0F,
7063 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7064
7065 /* Maximum volume */
7066 TP_EC_VOLUME_MAX = 14,
7067};
7068
7069enum tpacpi_volume_access_mode {
7070 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7071 TPACPI_VOL_MODE_EC, /* Pure EC control */
7072 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7073 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7074 TPACPI_VOL_MODE_MAX
7075};
7076
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007077enum tpacpi_volume_capabilities {
7078 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7079 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7080 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7081 TPACPI_VOL_CAP_MAX
7082};
7083
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007084enum tpacpi_mute_btn_mode {
7085 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7086 /* We don't know what mode 1 is. */
7087 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7088 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7089};
7090
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007091static enum tpacpi_volume_access_mode volume_mode =
7092 TPACPI_VOL_MODE_MAX;
7093
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007094static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307095static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007096static bool software_mute_requested = true;
7097static bool software_mute_active;
7098static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007099
7100/*
7101 * Used to syncronize writers to TP_EC_AUDIO and
7102 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7103 */
7104static struct mutex volume_mutex;
7105
7106static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007107{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007108 u8 lec = 0;
7109 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007110 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007111
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007112 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7113 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007114 if (!volume_control_allowed)
7115 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007116 if (software_mute_active)
7117 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007118
7119 vdbg_printk(TPACPI_DBG_MIXER,
7120 "trying to checkpoint mixer state to NVRAM...\n");
7121
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007122 if (tp_features.mixer_no_level_control)
7123 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7124 else
7125 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7126
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007127 if (mutex_lock_killable(&volume_mutex) < 0)
7128 return;
7129
7130 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7131 goto unlock;
7132 lec &= ec_mask;
7133 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7134
7135 if (lec != (b_nvram & ec_mask)) {
7136 /* NVRAM needs update */
7137 b_nvram &= ~ec_mask;
7138 b_nvram |= lec;
7139 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7140 dbg_printk(TPACPI_DBG_MIXER,
7141 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7142 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007143 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007144 vdbg_printk(TPACPI_DBG_MIXER,
7145 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7146 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007147 }
7148
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007149unlock:
7150 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007151}
7152
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007153static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007154{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007155 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007156
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007157 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7158 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007159
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007160 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007161
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007162 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007163
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007164 return 0;
7165}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007166
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007167static int volume_get_status(u8 *status)
7168{
7169 return volume_get_status_ec(status);
7170}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007171
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007172static int volume_set_status_ec(const u8 status)
7173{
7174 if (!acpi_ec_write(TP_EC_AUDIO, status))
7175 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007176
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007177 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7178
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007179 /*
7180 * On X200s, and possibly on others, it can take a while for
7181 * reads to become correct.
7182 */
7183 msleep(1);
7184
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007185 return 0;
7186}
7187
7188static int volume_set_status(const u8 status)
7189{
7190 return volume_set_status_ec(status);
7191}
7192
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007193/* returns < 0 on error, 0 on no change, 1 on change */
7194static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007195{
7196 int rc;
7197 u8 s, n;
7198
7199 if (mutex_lock_killable(&volume_mutex) < 0)
7200 return -EINTR;
7201
7202 rc = volume_get_status_ec(&s);
7203 if (rc)
7204 goto unlock;
7205
7206 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7207 s & ~TP_EC_AUDIO_MUTESW_MSK;
7208
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007209 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007210 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007211 if (!rc)
7212 rc = 1;
7213 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007214
7215unlock:
7216 mutex_unlock(&volume_mutex);
7217 return rc;
7218}
7219
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007220static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007221{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007222 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007223 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007224 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007225}
7226
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007227static int volume_set_mute(const bool mute)
7228{
7229 int rc;
7230
7231 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7232 (mute) ? "" : "un");
7233
7234 rc = __volume_set_mute_ec(mute);
7235 return (rc < 0) ? rc : 0;
7236}
7237
7238/* returns < 0 on error, 0 on no change, 1 on change */
7239static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007240{
7241 int rc;
7242 u8 s, n;
7243
7244 if (vol > TP_EC_VOLUME_MAX)
7245 return -EINVAL;
7246
7247 if (mutex_lock_killable(&volume_mutex) < 0)
7248 return -EINTR;
7249
7250 rc = volume_get_status_ec(&s);
7251 if (rc)
7252 goto unlock;
7253
7254 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7255
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007256 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007257 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007258 if (!rc)
7259 rc = 1;
7260 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007261
7262unlock:
7263 mutex_unlock(&volume_mutex);
7264 return rc;
7265}
7266
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007267static int volume_set_software_mute(bool startup)
7268{
7269 int result;
7270
7271 if (!tpacpi_is_lenovo())
7272 return -ENODEV;
7273
7274 if (startup) {
7275 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7276 "HAUM", "qd"))
7277 return -EIO;
7278
7279 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7280 "Initial HAUM setting was %d\n",
7281 software_mute_orig_mode);
7282 }
7283
7284 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7285 (int)TP_EC_MUTE_BTN_NONE))
7286 return -EIO;
7287
7288 if (result != TP_EC_MUTE_BTN_NONE)
7289 pr_warn("Unexpected SAUM result %d\n",
7290 result);
7291
7292 /*
7293 * In software mute mode, the standard codec controls take
7294 * precendence, so we unmute the ThinkPad HW switch at
7295 * startup. Just on case there are SAUM-capable ThinkPads
7296 * with level controls, set max HW volume as well.
7297 */
7298 if (tp_features.mixer_no_level_control)
7299 result = volume_set_mute(false);
7300 else
7301 result = volume_set_status(TP_EC_VOLUME_MAX);
7302
7303 if (result != 0)
7304 pr_warn("Failed to unmute the HW mute switch\n");
7305
7306 return 0;
7307}
7308
7309static void volume_exit_software_mute(void)
7310{
7311 int r;
7312
7313 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7314 || r != software_mute_orig_mode)
7315 pr_warn("Failed to restore mute mode\n");
7316}
7317
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007318static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007319{
7320 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007321 "ALSA: trying to set volume level to %hu\n", vol);
7322 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007323}
7324
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007325static void volume_alsa_notify_change(void)
7326{
7327 struct tpacpi_alsa_data *d;
7328
7329 if (alsa_card && alsa_card->private_data) {
7330 d = alsa_card->private_data;
7331 if (d->ctl_mute_id)
7332 snd_ctl_notify(alsa_card,
7333 SNDRV_CTL_EVENT_MASK_VALUE,
7334 d->ctl_mute_id);
7335 if (d->ctl_vol_id)
7336 snd_ctl_notify(alsa_card,
7337 SNDRV_CTL_EVENT_MASK_VALUE,
7338 d->ctl_vol_id);
7339 }
7340}
7341
7342static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7343 struct snd_ctl_elem_info *uinfo)
7344{
7345 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7346 uinfo->count = 1;
7347 uinfo->value.integer.min = 0;
7348 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7349 return 0;
7350}
7351
7352static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7353 struct snd_ctl_elem_value *ucontrol)
7354{
7355 u8 s;
7356 int rc;
7357
7358 rc = volume_get_status(&s);
7359 if (rc < 0)
7360 return rc;
7361
7362 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7363 return 0;
7364}
7365
7366static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7367 struct snd_ctl_elem_value *ucontrol)
7368{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007369 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7370 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007371 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007372}
7373
7374#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7375
7376static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7377 struct snd_ctl_elem_value *ucontrol)
7378{
7379 u8 s;
7380 int rc;
7381
7382 rc = volume_get_status(&s);
7383 if (rc < 0)
7384 return rc;
7385
7386 ucontrol->value.integer.value[0] =
7387 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7388 return 0;
7389}
7390
7391static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7392 struct snd_ctl_elem_value *ucontrol)
7393{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007394 tpacpi_disclose_usertask("ALSA", "%smute\n",
7395 ucontrol->value.integer.value[0] ?
7396 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007397 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007398}
7399
Mathias Krausec3aa4722014-07-16 19:43:17 +02007400static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007401 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7402 .name = "Console Playback Volume",
7403 .index = 0,
7404 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7405 .info = volume_alsa_vol_info,
7406 .get = volume_alsa_vol_get,
7407};
7408
Mathias Krausec3aa4722014-07-16 19:43:17 +02007409static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007410 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7411 .name = "Console Playback Switch",
7412 .index = 0,
7413 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7414 .info = volume_alsa_mute_info,
7415 .get = volume_alsa_mute_get,
7416};
7417
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007418static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007419{
7420 tpacpi_volume_checkpoint_nvram();
7421}
7422
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007423static void volume_resume(void)
7424{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007425 if (software_mute_active) {
7426 if (volume_set_software_mute(false) < 0)
7427 pr_warn("Failed to restore software mute\n");
7428 } else {
7429 volume_alsa_notify_change();
7430 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007431}
7432
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007433static void volume_shutdown(void)
7434{
7435 tpacpi_volume_checkpoint_nvram();
7436}
7437
7438static void volume_exit(void)
7439{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007440 if (alsa_card) {
7441 snd_card_free(alsa_card);
7442 alsa_card = NULL;
7443 }
7444
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007445 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007446
7447 if (software_mute_active)
7448 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007449}
7450
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007451static int __init volume_create_alsa_mixer(void)
7452{
7453 struct snd_card *card;
7454 struct tpacpi_alsa_data *data;
7455 struct snd_kcontrol *ctl_vol;
7456 struct snd_kcontrol *ctl_mute;
7457 int rc;
7458
Takashi Iwai89235e52014-01-29 15:01:27 +01007459 rc = snd_card_new(&tpacpi_pdev->dev,
7460 alsa_index, alsa_id, THIS_MODULE,
7461 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007462 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007463 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007464 return 1;
7465 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007466
7467 BUG_ON(!card->private_data);
7468 data = card->private_data;
7469 data->card = card;
7470
7471 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7472 sizeof(card->driver));
7473 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7474 sizeof(card->shortname));
7475 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7476 (thinkpad_id.ec_version_str) ?
7477 thinkpad_id.ec_version_str : "(unknown)");
7478 snprintf(card->longname, sizeof(card->longname),
7479 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7480 (thinkpad_id.ec_version_str) ?
7481 thinkpad_id.ec_version_str : "unknown");
7482
7483 if (volume_control_allowed) {
7484 volume_alsa_control_vol.put = volume_alsa_vol_put;
7485 volume_alsa_control_vol.access =
7486 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7487
7488 volume_alsa_control_mute.put = volume_alsa_mute_put;
7489 volume_alsa_control_mute.access =
7490 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7491 }
7492
7493 if (!tp_features.mixer_no_level_control) {
7494 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7495 rc = snd_ctl_add(card, ctl_vol);
7496 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007497 pr_err("Failed to create ALSA volume control: %d\n",
7498 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007499 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007500 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007501 data->ctl_vol_id = &ctl_vol->id;
7502 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007503
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007504 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7505 rc = snd_ctl_add(card, ctl_mute);
7506 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007507 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007508 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007509 }
7510 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007511
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007512 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007513 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007514 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007515 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007516 }
7517
7518 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007519 return 0;
7520
7521err_exit:
7522 snd_card_free(card);
7523 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007524}
7525
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007526#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7527#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7528
7529static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7530 /* Whitelist volume level on all IBM by default */
7531 { .vendor = PCI_VENDOR_ID_IBM,
7532 .bios = TPACPI_MATCH_ANY,
7533 .ec = TPACPI_MATCH_ANY,
7534 .quirks = TPACPI_VOL_Q_LEVEL },
7535
7536 /* Lenovo models with volume control (needs confirmation) */
7537 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7538 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7539 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7540 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7541 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7542 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7543 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7544
7545 /* Whitelist mute-only on all Lenovo by default */
7546 { .vendor = PCI_VENDOR_ID_LENOVO,
7547 .bios = TPACPI_MATCH_ANY,
7548 .ec = TPACPI_MATCH_ANY,
7549 .quirks = TPACPI_VOL_Q_MUTEONLY }
7550};
7551
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007552static int __init volume_init(struct ibm_init_struct *iibm)
7553{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007554 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007555 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007556
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007557 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7558
7559 mutex_init(&volume_mutex);
7560
7561 /*
7562 * Check for module parameter bogosity, note that we
7563 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7564 * able to detect "unspecified"
7565 */
7566 if (volume_mode > TPACPI_VOL_MODE_MAX)
7567 return -EINVAL;
7568
7569 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Joe Perches0978e012011-04-04 10:06:25 -07007570 pr_err("UCMS step volume mode not implemented, "
7571 "please contact %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007572 return 1;
7573 }
7574
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007575 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7576 return -EINVAL;
7577
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007578 /*
7579 * The ALSA mixer is our primary interface.
7580 * When disabled, don't install the subdriver at all
7581 */
7582 if (!alsa_enable) {
7583 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7584 "ALSA mixer disabled by parameter, "
7585 "not loading volume subdriver...\n");
7586 return 1;
7587 }
7588
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007589 quirks = tpacpi_check_quirks(volume_quirk_table,
7590 ARRAY_SIZE(volume_quirk_table));
7591
7592 switch (volume_capabilities) {
7593 case TPACPI_VOL_CAP_AUTO:
7594 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7595 tp_features.mixer_no_level_control = 1;
7596 else if (quirks & TPACPI_VOL_Q_LEVEL)
7597 tp_features.mixer_no_level_control = 0;
7598 else
7599 return 1; /* no mixer */
7600 break;
7601 case TPACPI_VOL_CAP_VOLMUTE:
7602 tp_features.mixer_no_level_control = 0;
7603 break;
7604 case TPACPI_VOL_CAP_MUTEONLY:
7605 tp_features.mixer_no_level_control = 1;
7606 break;
7607 default:
7608 return 1;
7609 }
7610
7611 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7612 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7613 "using user-supplied volume_capabilities=%d\n",
7614 volume_capabilities);
7615
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007616 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7617 volume_mode == TPACPI_VOL_MODE_MAX) {
7618 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7619
7620 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7621 "driver auto-selected volume_mode=%d\n",
7622 volume_mode);
7623 } else {
7624 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7625 "using user-supplied volume_mode=%d\n",
7626 volume_mode);
7627 }
7628
7629 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007630 "mute is supported, volume control is %s\n",
7631 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007632
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007633 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7634 software_mute_active = true;
7635 } else {
7636 rc = volume_create_alsa_mixer();
7637 if (rc) {
7638 pr_err("Could not create the ALSA mixer interface\n");
7639 return rc;
7640 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007641
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007642 pr_info("Console audio control enabled, mode: %s\n",
7643 (volume_control_allowed) ?
7644 "override (read/write)" :
7645 "monitor (read only)");
7646 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007647
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007648 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7649 "registering volume hotkeys as change notification\n");
7650 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7651 | TP_ACPI_HKEY_VOLUP_MASK
7652 | TP_ACPI_HKEY_VOLDWN_MASK
7653 | TP_ACPI_HKEY_MUTE_MASK);
7654
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007655 return 0;
7656}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007657
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007658static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007659{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007660 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007661
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007662 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007663 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007664 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007665 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007666 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007667 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007668 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007669 status & TP_EC_AUDIO_LVL_MSK);
7670
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007671 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007672 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007673
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007674 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007675 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007676 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007677 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007678 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007679 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007680 "commands:\tlevel <level>"
7681 " (<level> is 0-%d)\n",
7682 TP_EC_VOLUME_MAX);
7683 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007684 }
7685 }
7686
7687 return 0;
7688}
7689
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007690static int volume_write(char *buf)
7691{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007692 u8 s;
7693 u8 new_level, new_mute;
7694 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007695 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007696 int rc;
7697
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007698 /*
7699 * We do allow volume control at driver startup, so that the
7700 * user can set initial state through the volume=... parameter hack.
7701 */
7702 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7703 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7704 tp_warned.volume_ctrl_forbidden = 1;
Joe Perches0978e012011-04-04 10:06:25 -07007705 pr_notice("Console audio control in monitor mode, "
7706 "changes are not allowed\n");
7707 pr_notice("Use the volume_control=1 module parameter "
7708 "to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007709 }
7710 return -EPERM;
7711 }
7712
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007713 rc = volume_get_status(&s);
7714 if (rc < 0)
7715 return rc;
7716
7717 new_level = s & TP_EC_AUDIO_LVL_MSK;
7718 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007719
7720 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007721 if (!tp_features.mixer_no_level_control) {
7722 if (strlencmp(cmd, "up") == 0) {
7723 if (new_mute)
7724 new_mute = 0;
7725 else if (new_level < TP_EC_VOLUME_MAX)
7726 new_level++;
7727 continue;
7728 } else if (strlencmp(cmd, "down") == 0) {
7729 if (new_mute)
7730 new_mute = 0;
7731 else if (new_level > 0)
7732 new_level--;
7733 continue;
7734 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7735 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7736 new_level = l;
7737 continue;
7738 }
7739 }
7740 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007741 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007742 else if (strlencmp(cmd, "unmute") == 0)
7743 new_mute = 0;
7744 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007745 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007746 }
7747
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007748 if (tp_features.mixer_no_level_control) {
7749 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7750 new_mute ? "" : "un");
7751 rc = volume_set_mute(!!new_mute);
7752 } else {
7753 tpacpi_disclose_usertask("procfs volume",
7754 "%smute and set level to %d\n",
7755 new_mute ? "" : "un", new_level);
7756 rc = volume_set_status(new_mute | new_level);
7757 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007758 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007759
7760 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007761}
7762
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007763static struct ibm_struct volume_driver_data = {
7764 .name = "volume",
7765 .read = volume_read,
7766 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007767 .exit = volume_exit,
7768 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007769 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007770 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007771};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007772
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007773#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7774
7775#define alsa_card NULL
7776
Tobias Klauserbb55a2e2016-11-25 14:18:07 +01007777static inline void volume_alsa_notify_change(void)
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007778{
7779}
7780
7781static int __init volume_init(struct ibm_init_struct *iibm)
7782{
Joe Perches0978e012011-04-04 10:06:25 -07007783 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007784
7785 return 1;
7786}
7787
7788static struct ibm_struct volume_driver_data = {
7789 .name = "volume",
7790};
7791
7792#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7793
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007794/*************************************************************************
7795 * Fan subdriver
7796 */
7797
7798/*
7799 * FAN ACCESS MODES
7800 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007801 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007802 * ACPI GFAN method: returns fan level
7803 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007804 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007805 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007806 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007807 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007808 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7809 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007810 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7811 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007812 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007813 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007814 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7815 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007816 *
7817 * Fan speed changes of any sort (including those caused by the
7818 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007819 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007820 * limited.
7821 *
7822 * Reading is not available if GFAN exists.
7823 * Writing is not available if SFAN exists.
7824 *
7825 * Bits
7826 * 7 automatic mode engaged;
7827 * (default operation mode of the ThinkPad)
7828 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007829 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007830 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007831 * the tachometer while the fan controller ramps up
7832 * the speed (which can take up to a few *minutes*).
7833 * Speeds up fan to 100% duty-cycle, which is far above
7834 * the standard RPM levels. It is not impossible that
7835 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007836 * 5-3 unused in some models. Extra bits for fan level
7837 * in others, but still useless as all values above
7838 * 7 map to the same speed as level 7 in these models.
7839 * 2-0 fan level (0..7 usually)
7840 * 0x00 = stop
7841 * 0x07 = max (set when temperatures critical)
7842 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007843 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007844 *
7845 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007846 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007847 * does so, its initial value is meaningless (0x07).
7848 *
7849 * For firmware bugs, refer to:
7850 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7851 *
7852 * ----
7853 *
7854 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7855 * Main fan tachometer reading (in RPM)
7856 *
7857 * This register is present on all ThinkPads with a new-style EC, and
7858 * it is known not to be present on the A21m/e, and T22, as there is
7859 * something else in offset 0x84 according to the ACPI DSDT. Other
7860 * ThinkPads from this same time period (and earlier) probably lack the
7861 * tachometer as well.
7862 *
Nick Andrew877d0312009-01-26 11:06:57 +01007863 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007864 * was never fixed by IBM to report the EC firmware version string
7865 * probably support the tachometer (like the early X models), so
7866 * detecting it is quite hard. We need more data to know for sure.
7867 *
7868 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7869 * might result.
7870 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007871 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7872 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007873 *
7874 * For firmware bugs, refer to:
7875 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7876 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007877 * ----
7878 *
7879 * ThinkPad EC register 0x31 bit 0 (only on select models)
7880 *
7881 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7882 * show the speed of the main fan. When bit 0 of EC register 0x31
7883 * is one, the tachometer registers show the speed of the auxiliary
7884 * fan.
7885 *
7886 * Fan control seems to affect both fans, regardless of the state
7887 * of this bit.
7888 *
7889 * So far, only the firmware for the X60/X61 non-tablet versions
7890 * seem to support this (firmware TP-7M).
7891 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007892 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007893 * ThinkPad X31, X40, X41. Not available in the X60.
7894 *
7895 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7896 * high speed. ACPI DSDT seems to map these three speeds to levels
7897 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7898 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7899 *
7900 * The speeds are stored on handles
7901 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7902 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007903 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007904 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7905 *
7906 * ACPI DSDT switches which set is in use depending on various
7907 * factors.
7908 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007909 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007910 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7911 * but the ACPI tables just mention level 7.
7912 */
7913
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007914enum { /* Fan control constants */
7915 fan_status_offset = 0x2f, /* EC register 0x2f */
7916 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7917 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007918 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7919 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007920
7921 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7922 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7923
7924 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7925};
7926
7927enum fan_status_access_mode {
7928 TPACPI_FAN_NONE = 0, /* No fan status or control */
7929 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7930 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7931};
7932
7933enum fan_control_access_mode {
7934 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7935 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7936 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7937 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7938};
7939
7940enum fan_control_commands {
7941 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7942 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7943 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7944 * and also watchdog cmd */
7945};
7946
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307947static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007948
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007949static enum fan_status_access_mode fan_status_access_mode;
7950static enum fan_control_access_mode fan_control_access_mode;
7951static enum fan_control_commands fan_control_commands;
7952
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007953static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007954static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007955static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007956static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007957
7958static struct mutex fan_mutex;
7959
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007960static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007961static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007962
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007963TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7964TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007965 "\\FSPD", /* 600e/x, 770e, 770x */
7966 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007967TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007968 "JFNS", /* 770x-JL */
7969 ); /* all others */
7970
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007971/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007972 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7973 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7974 * be in auto mode (0x80).
7975 *
7976 * This is corrected by any write to HFSP either by the driver, or
7977 * by the firmware.
7978 *
7979 * We assume 0x07 really means auto mode while this quirk is active,
7980 * as this is far more likely than the ThinkPad being in level 7,
7981 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007982 *
7983 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7984 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007985 */
7986
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007987static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007988{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007989 if (fan_control_initial_status == 0x07) {
Joe Perches0978e012011-04-04 10:06:25 -07007990 pr_notice("fan_init: initial fan status is unknown, "
7991 "assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007992 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007993 }
7994}
7995
7996static void fan_quirk1_handle(u8 *fan_status)
7997{
7998 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7999 if (*fan_status != fan_control_initial_status) {
8000 /* something changed the HFSP regisnter since
8001 * driver init time, so it is not undefined
8002 * anymore */
8003 tp_features.fan_ctrl_status_undef = 0;
8004 } else {
8005 /* Return most likely status. In fact, it
8006 * might be the only possible status */
8007 *fan_status = TP_EC_FAN_AUTO;
8008 }
8009 }
8010}
8011
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008012/* Select main fan on X60/X61, NOOP on others */
8013static bool fan_select_fan1(void)
8014{
8015 if (tp_features.second_fan) {
8016 u8 val;
8017
8018 if (ec_read(fan_select_offset, &val) < 0)
8019 return false;
8020 val &= 0xFEU;
8021 if (ec_write(fan_select_offset, val) < 0)
8022 return false;
8023 }
8024 return true;
8025}
8026
8027/* Select secondary fan on X60/X61 */
8028static bool fan_select_fan2(void)
8029{
8030 u8 val;
8031
8032 if (!tp_features.second_fan)
8033 return false;
8034
8035 if (ec_read(fan_select_offset, &val) < 0)
8036 return false;
8037 val |= 0x01U;
8038 if (ec_write(fan_select_offset, val) < 0)
8039 return false;
8040
8041 return true;
8042}
8043
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008044/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008045 * Call with fan_mutex held
8046 */
8047static void fan_update_desired_level(u8 status)
8048{
8049 if ((status &
8050 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8051 if (status > 7)
8052 fan_control_desired_level = 7;
8053 else
8054 fan_control_desired_level = status;
8055 }
8056}
8057
8058static int fan_get_status(u8 *status)
8059{
8060 u8 s;
8061
8062 /* TODO:
8063 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8064
8065 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07008066 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008067 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07008068 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008069
Dan Carpentereceeb432012-09-01 12:54:07 -07008070 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008071 return -EIO;
8072
8073 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07008074 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008075
8076 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07008077 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008078 case TPACPI_FAN_RD_TPEC:
8079 /* all except 570, 600e/x, 770e, 770x */
8080 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8081 return -EIO;
8082
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008083 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008084 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008085 fan_quirk1_handle(status);
8086 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008087
8088 break;
8089
8090 default:
8091 return -ENXIO;
8092 }
8093
8094 return 0;
8095}
8096
8097static int fan_get_status_safe(u8 *status)
8098{
8099 int rc;
8100 u8 s;
8101
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008102 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008103 return -ERESTARTSYS;
8104 rc = fan_get_status(&s);
8105 if (!rc)
8106 fan_update_desired_level(s);
8107 mutex_unlock(&fan_mutex);
8108
Dan Carpentera7718362016-04-15 17:46:34 +03008109 if (rc)
8110 return rc;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008111 if (status)
8112 *status = s;
8113
Dan Carpentera7718362016-04-15 17:46:34 +03008114 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008115}
8116
8117static int fan_get_speed(unsigned int *speed)
8118{
8119 u8 hi, lo;
8120
8121 switch (fan_status_access_mode) {
8122 case TPACPI_FAN_RD_TPEC:
8123 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008124 if (unlikely(!fan_select_fan1()))
8125 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008126 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8127 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8128 return -EIO;
8129
8130 if (likely(speed))
8131 *speed = (hi << 8) | lo;
8132
8133 break;
8134
8135 default:
8136 return -ENXIO;
8137 }
8138
8139 return 0;
8140}
8141
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008142static int fan2_get_speed(unsigned int *speed)
8143{
8144 u8 hi, lo;
8145 bool rc;
8146
8147 switch (fan_status_access_mode) {
8148 case TPACPI_FAN_RD_TPEC:
8149 /* all except 570, 600e/x, 770e, 770x */
8150 if (unlikely(!fan_select_fan2()))
8151 return -EIO;
8152 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8153 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8154 fan_select_fan1(); /* play it safe */
8155 if (rc)
8156 return -EIO;
8157
8158 if (likely(speed))
8159 *speed = (hi << 8) | lo;
8160
8161 break;
8162
8163 default:
8164 return -ENXIO;
8165 }
8166
8167 return 0;
8168}
8169
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008170static int fan_set_level(int level)
8171{
8172 if (!fan_control_allowed)
8173 return -EPERM;
8174
8175 switch (fan_control_access_mode) {
8176 case TPACPI_FAN_WR_ACPI_SFAN:
8177 if (level >= 0 && level <= 7) {
8178 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8179 return -EIO;
8180 } else
8181 return -EINVAL;
8182 break;
8183
8184 case TPACPI_FAN_WR_ACPI_FANS:
8185 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008186 if (!(level & TP_EC_FAN_AUTO) &&
8187 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008188 ((level < 0) || (level > 7)))
8189 return -EINVAL;
8190
8191 /* safety net should the EC not support AUTO
8192 * or FULLSPEED mode bits and just ignore them */
8193 if (level & TP_EC_FAN_FULLSPEED)
8194 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01008195 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008196 level |= 4; /* safety min speed 4 */
8197
8198 if (!acpi_ec_write(fan_status_offset, level))
8199 return -EIO;
8200 else
8201 tp_features.fan_ctrl_status_undef = 0;
8202 break;
8203
8204 default:
8205 return -ENXIO;
8206 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008207
8208 vdbg_printk(TPACPI_DBG_FAN,
8209 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008210 return 0;
8211}
8212
8213static int fan_set_level_safe(int level)
8214{
8215 int rc;
8216
8217 if (!fan_control_allowed)
8218 return -EPERM;
8219
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008220 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008221 return -ERESTARTSYS;
8222
8223 if (level == TPACPI_FAN_LAST_LEVEL)
8224 level = fan_control_desired_level;
8225
8226 rc = fan_set_level(level);
8227 if (!rc)
8228 fan_update_desired_level(level);
8229
8230 mutex_unlock(&fan_mutex);
8231 return rc;
8232}
8233
8234static int fan_set_enable(void)
8235{
8236 u8 s;
8237 int rc;
8238
8239 if (!fan_control_allowed)
8240 return -EPERM;
8241
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008242 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008243 return -ERESTARTSYS;
8244
8245 switch (fan_control_access_mode) {
8246 case TPACPI_FAN_WR_ACPI_FANS:
8247 case TPACPI_FAN_WR_TPEC:
8248 rc = fan_get_status(&s);
8249 if (rc < 0)
8250 break;
8251
8252 /* Don't go out of emergency fan mode */
8253 if (s != 7) {
8254 s &= 0x07;
8255 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8256 }
8257
8258 if (!acpi_ec_write(fan_status_offset, s))
8259 rc = -EIO;
8260 else {
8261 tp_features.fan_ctrl_status_undef = 0;
8262 rc = 0;
8263 }
8264 break;
8265
8266 case TPACPI_FAN_WR_ACPI_SFAN:
8267 rc = fan_get_status(&s);
8268 if (rc < 0)
8269 break;
8270
8271 s &= 0x07;
8272
8273 /* Set fan to at least level 4 */
8274 s |= 4;
8275
8276 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008277 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008278 else
8279 rc = 0;
8280 break;
8281
8282 default:
8283 rc = -ENXIO;
8284 }
8285
8286 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008287
8288 if (!rc)
8289 vdbg_printk(TPACPI_DBG_FAN,
8290 "fan control: set fan control register to 0x%02x\n",
8291 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008292 return rc;
8293}
8294
8295static int fan_set_disable(void)
8296{
8297 int rc;
8298
8299 if (!fan_control_allowed)
8300 return -EPERM;
8301
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008302 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008303 return -ERESTARTSYS;
8304
8305 rc = 0;
8306 switch (fan_control_access_mode) {
8307 case TPACPI_FAN_WR_ACPI_FANS:
8308 case TPACPI_FAN_WR_TPEC:
8309 if (!acpi_ec_write(fan_status_offset, 0x00))
8310 rc = -EIO;
8311 else {
8312 fan_control_desired_level = 0;
8313 tp_features.fan_ctrl_status_undef = 0;
8314 }
8315 break;
8316
8317 case TPACPI_FAN_WR_ACPI_SFAN:
8318 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8319 rc = -EIO;
8320 else
8321 fan_control_desired_level = 0;
8322 break;
8323
8324 default:
8325 rc = -ENXIO;
8326 }
8327
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008328 if (!rc)
8329 vdbg_printk(TPACPI_DBG_FAN,
8330 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008331
8332 mutex_unlock(&fan_mutex);
8333 return rc;
8334}
8335
8336static int fan_set_speed(int speed)
8337{
8338 int rc;
8339
8340 if (!fan_control_allowed)
8341 return -EPERM;
8342
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008343 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008344 return -ERESTARTSYS;
8345
8346 rc = 0;
8347 switch (fan_control_access_mode) {
8348 case TPACPI_FAN_WR_ACPI_FANS:
8349 if (speed >= 0 && speed <= 65535) {
8350 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8351 speed, speed, speed))
8352 rc = -EIO;
8353 } else
8354 rc = -EINVAL;
8355 break;
8356
8357 default:
8358 rc = -ENXIO;
8359 }
8360
8361 mutex_unlock(&fan_mutex);
8362 return rc;
8363}
8364
8365static void fan_watchdog_reset(void)
8366{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008367 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8368 return;
8369
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008370 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008371 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8372 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8373 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8374 else
8375 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008376}
8377
8378static void fan_watchdog_fire(struct work_struct *ignored)
8379{
8380 int rc;
8381
8382 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8383 return;
8384
Joe Perches0978e012011-04-04 10:06:25 -07008385 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008386 rc = fan_set_enable();
8387 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07008388 pr_err("fan watchdog: error %d while enabling fan, "
8389 "will try again later...\n", -rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008390 /* reschedule for later */
8391 fan_watchdog_reset();
8392 }
8393}
8394
8395/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008396 * SYSFS fan layout: hwmon compatible (device)
8397 *
8398 * pwm*_enable:
8399 * 0: "disengaged" mode
8400 * 1: manual mode
8401 * 2: native EC "auto" mode (recommended, hardware default)
8402 *
8403 * pwm*: set speed in manual mode, ignored otherwise.
8404 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8405 * interpolation.
8406 *
8407 * fan*_input: tachometer reading, RPM
8408 *
8409 *
8410 * SYSFS fan layout: extensions
8411 *
8412 * fan_watchdog (driver):
8413 * fan watchdog interval in seconds, 0 disables (default), max 120
8414 */
8415
8416/* sysfs fan pwm1_enable ----------------------------------------------- */
8417static ssize_t fan_pwm1_enable_show(struct device *dev,
8418 struct device_attribute *attr,
8419 char *buf)
8420{
8421 int res, mode;
8422 u8 status;
8423
8424 res = fan_get_status_safe(&status);
8425 if (res)
8426 return res;
8427
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008428 if (status & TP_EC_FAN_FULLSPEED) {
8429 mode = 0;
8430 } else if (status & TP_EC_FAN_AUTO) {
8431 mode = 2;
8432 } else
8433 mode = 1;
8434
8435 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8436}
8437
8438static ssize_t fan_pwm1_enable_store(struct device *dev,
8439 struct device_attribute *attr,
8440 const char *buf, size_t count)
8441{
8442 unsigned long t;
8443 int res, level;
8444
8445 if (parse_strtoul(buf, 2, &t))
8446 return -EINVAL;
8447
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008448 tpacpi_disclose_usertask("hwmon pwm1_enable",
8449 "set fan mode to %lu\n", t);
8450
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008451 switch (t) {
8452 case 0:
8453 level = TP_EC_FAN_FULLSPEED;
8454 break;
8455 case 1:
8456 level = TPACPI_FAN_LAST_LEVEL;
8457 break;
8458 case 2:
8459 level = TP_EC_FAN_AUTO;
8460 break;
8461 case 3:
8462 /* reserved for software-controlled auto mode */
8463 return -ENOSYS;
8464 default:
8465 return -EINVAL;
8466 }
8467
8468 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008469 if (res == -ENXIO)
8470 return -EINVAL;
8471 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008472 return res;
8473
8474 fan_watchdog_reset();
8475
8476 return count;
8477}
8478
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008479static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8480 fan_pwm1_enable_show, fan_pwm1_enable_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008481
8482/* sysfs fan pwm1 ------------------------------------------------------ */
8483static ssize_t fan_pwm1_show(struct device *dev,
8484 struct device_attribute *attr,
8485 char *buf)
8486{
8487 int res;
8488 u8 status;
8489
8490 res = fan_get_status_safe(&status);
8491 if (res)
8492 return res;
8493
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008494 if ((status &
8495 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8496 status = fan_control_desired_level;
8497
8498 if (status > 7)
8499 status = 7;
8500
8501 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8502}
8503
8504static ssize_t fan_pwm1_store(struct device *dev,
8505 struct device_attribute *attr,
8506 const char *buf, size_t count)
8507{
8508 unsigned long s;
8509 int rc;
8510 u8 status, newlevel;
8511
8512 if (parse_strtoul(buf, 255, &s))
8513 return -EINVAL;
8514
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008515 tpacpi_disclose_usertask("hwmon pwm1",
8516 "set fan speed to %lu\n", s);
8517
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008518 /* scale down from 0-255 to 0-7 */
8519 newlevel = (s >> 5) & 0x07;
8520
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008521 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008522 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008523
8524 rc = fan_get_status(&status);
8525 if (!rc && (status &
8526 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8527 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008528 if (rc == -ENXIO)
8529 rc = -EINVAL;
8530 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008531 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008532 fan_watchdog_reset();
8533 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008534 }
8535
8536 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008537 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008538}
8539
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008540static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008541
8542/* sysfs fan fan1_input ------------------------------------------------ */
8543static ssize_t fan_fan1_input_show(struct device *dev,
8544 struct device_attribute *attr,
8545 char *buf)
8546{
8547 int res;
8548 unsigned int speed;
8549
8550 res = fan_get_speed(&speed);
8551 if (res < 0)
8552 return res;
8553
8554 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8555}
8556
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008557static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008558
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008559/* sysfs fan fan2_input ------------------------------------------------ */
8560static ssize_t fan_fan2_input_show(struct device *dev,
8561 struct device_attribute *attr,
8562 char *buf)
8563{
8564 int res;
8565 unsigned int speed;
8566
8567 res = fan2_get_speed(&speed);
8568 if (res < 0)
8569 return res;
8570
8571 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8572}
8573
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008574static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008575
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008576/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008577static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008578{
8579 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8580}
8581
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008582static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8583 size_t count)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008584{
8585 unsigned long t;
8586
8587 if (parse_strtoul(buf, 120, &t))
8588 return -EINVAL;
8589
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008590 if (!fan_control_allowed)
8591 return -EPERM;
8592
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008593 fan_watchdog_maxinterval = t;
8594 fan_watchdog_reset();
8595
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008596 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8597
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008598 return count;
8599}
Greg Kroah-Hartmanac3054c2017-06-09 11:03:12 +02008600static DRIVER_ATTR_RW(fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008601
8602/* --------------------------------------------------------------------- */
8603static struct attribute *fan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008604 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8605 &dev_attr_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008606 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008607 NULL
8608};
8609
8610static const struct attribute_group fan_attr_group = {
8611 .attrs = fan_attributes,
8612};
8613
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008614#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8615#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008616
8617#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8618 { .vendor = PCI_VENDOR_ID_IBM, \
8619 .bios = TPACPI_MATCH_ANY, \
8620 .ec = TPID(__id1, __id2), \
8621 .quirks = __quirks }
8622
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008623#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8624 { .vendor = PCI_VENDOR_ID_LENOVO, \
8625 .bios = TPACPI_MATCH_ANY, \
8626 .ec = TPID(__id1, __id2), \
8627 .quirks = __quirks }
8628
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008629static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8630 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8631 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8632 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8633 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008634 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008635};
8636
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008637#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008638#undef TPACPI_FAN_QI
8639
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008640static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008641{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008642 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008643 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008644
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008645 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8646 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008647
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008648 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008649 fan_status_access_mode = TPACPI_FAN_NONE;
8650 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008651 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008652 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008653 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008654 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008655 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008656
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008657 if (tpacpi_is_ibm()) {
8658 TPACPI_ACPIHANDLE_INIT(fans);
8659 TPACPI_ACPIHANDLE_INIT(gfan);
8660 TPACPI_ACPIHANDLE_INIT(sfan);
8661 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008662
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008663 quirks = tpacpi_check_quirks(fan_quirk_table,
8664 ARRAY_SIZE(fan_quirk_table));
8665
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008666 if (gfan_handle) {
8667 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008668 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008669 } else {
8670 /* all other ThinkPads: note that even old-style
8671 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008672 if (likely(acpi_ec_read(fan_status_offset,
8673 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008674 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008675 if (quirks & TPACPI_FAN_Q1)
8676 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008677 if (quirks & TPACPI_FAN_2FAN) {
8678 tp_features.second_fan = 1;
8679 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8680 "secondary fan support enabled\n");
8681 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008682 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008683 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008684 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008685 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008686 }
8687 }
8688
8689 if (sfan_handle) {
8690 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008691 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008692 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008693 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008694 } else {
8695 if (!gfan_handle) {
8696 /* gfan without sfan means no fan control */
8697 /* all other models implement TP EC 0x2f control */
8698
8699 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008700 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008701 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008702 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008703 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008704 TPACPI_FAN_CMD_SPEED |
8705 TPACPI_FAN_CMD_LEVEL |
8706 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008707 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008708 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008709 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008710 TPACPI_FAN_CMD_LEVEL |
8711 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008712 }
8713 }
8714 }
8715
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008716 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8717 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008718 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8719 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8720 fan_status_access_mode, fan_control_access_mode);
8721
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008722 /* fan control master switch */
8723 if (!fan_control_allowed) {
8724 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8725 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008726 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008727 "fan control features disabled by parameter\n");
8728 }
8729
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008730 /* update fan_control_desired_level */
8731 if (fan_status_access_mode != TPACPI_FAN_NONE)
8732 fan_get_status_safe(NULL);
8733
8734 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8735 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008736 if (tp_features.second_fan) {
8737 /* attach second fan tachometer */
8738 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008739 &dev_attr_fan2_input.attr;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008740 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008741 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008742 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008743 if (rc < 0)
8744 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008745
8746 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8747 &driver_attr_fan_watchdog);
8748 if (rc < 0) {
8749 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8750 &fan_attr_group);
8751 return rc;
8752 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008753 return 0;
8754 } else
8755 return 1;
8756}
8757
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008758static void fan_exit(void)
8759{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008760 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008761 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008762
8763 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008764 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008765 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8766 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008767
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008768 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008769 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008770}
8771
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008772static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008773{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008774 int rc;
8775
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008776 if (!fan_control_allowed)
8777 return;
8778
8779 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008780 fan_control_resume_level = 0;
8781 rc = fan_get_status_safe(&fan_control_resume_level);
8782 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008783 pr_notice("failed to read fan level for later "
8784 "restore during resume: %d\n", rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008785
8786 /* if it is undefined, don't attempt to restore it.
8787 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008788 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008789 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008790}
8791
8792static void fan_resume(void)
8793{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008794 u8 current_level = 7;
8795 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008796 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008797
8798 /* DSDT *always* updates status on resume */
8799 tp_features.fan_ctrl_status_undef = 0;
8800
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008801 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008802 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008803 (fan_get_status_safe(&current_level) < 0))
8804 return;
8805
8806 switch (fan_control_access_mode) {
8807 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008808 /* never decrease fan level */
8809 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008810 break;
8811 case TPACPI_FAN_WR_ACPI_FANS:
8812 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008813 /* never decrease fan level, scale is:
8814 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8815 *
8816 * We expect the firmware to set either 7 or AUTO, but we
8817 * handle FULLSPEED out of paranoia.
8818 *
8819 * So, we can safely only restore FULLSPEED or 7, anything
8820 * else could slow the fan. Restoring AUTO is useless, at
8821 * best that's exactly what the DSDT already set (it is the
8822 * slower it uses).
8823 *
8824 * Always keep in mind that the DSDT *will* have set the
8825 * fans to what the vendor supposes is the best level. We
8826 * muck with it only to speed the fan up.
8827 */
8828 if (fan_control_resume_level != 7 &&
8829 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8830 return;
8831 else
8832 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8833 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008834 break;
8835 default:
8836 return;
8837 }
8838 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008839 pr_notice("restoring fan level to 0x%02x\n",
8840 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008841 rc = fan_set_level_safe(fan_control_resume_level);
8842 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008843 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008844 }
8845}
8846
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008847static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008848{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008849 int rc;
8850 u8 status;
8851 unsigned int speed = 0;
8852
8853 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008854 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008855 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008856 rc = fan_get_status_safe(&status);
8857 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008858 return rc;
8859
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008860 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008861 "level:\t\t%d\n",
8862 (status != 0) ? "enabled" : "disabled", status);
8863 break;
8864
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008865 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008866 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008867 rc = fan_get_status_safe(&status);
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, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008872 (status != 0) ? "enabled" : "disabled");
8873
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008874 rc = fan_get_speed(&speed);
8875 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008876 return rc;
8877
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008878 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008879
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008880 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008881 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008882 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008883 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008884 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008885 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008886 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008887 break;
8888
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008889 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008890 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008891 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008892 }
8893
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008894 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008895 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008896
8897 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008898 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008899 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008900 break;
8901
8902 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008903 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008904 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008905 break;
8906 }
8907 }
8908
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008909 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008910 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008911 "commands:\twatchdog <timeout> (<timeout> "
8912 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008913
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008914 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008915 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008916 " (<speed> is 0-65535)\n");
8917
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008918 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008919}
8920
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008921static int fan_write_cmd_level(const char *cmd, int *rc)
8922{
8923 int level;
8924
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008925 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008926 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008927 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008928 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008929 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008930 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008931 return 0;
8932
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008933 *rc = fan_set_level_safe(level);
8934 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008935 pr_err("level command accepted for unsupported access mode %d\n",
8936 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008937 else if (!*rc)
8938 tpacpi_disclose_usertask("procfs fan",
8939 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008940
8941 return 1;
8942}
8943
8944static int fan_write_cmd_enable(const char *cmd, int *rc)
8945{
8946 if (strlencmp(cmd, "enable") != 0)
8947 return 0;
8948
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008949 *rc = fan_set_enable();
8950 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008951 pr_err("enable command accepted for unsupported access mode %d\n",
8952 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008953 else if (!*rc)
8954 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008955
8956 return 1;
8957}
8958
8959static int fan_write_cmd_disable(const char *cmd, int *rc)
8960{
8961 if (strlencmp(cmd, "disable") != 0)
8962 return 0;
8963
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008964 *rc = fan_set_disable();
8965 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008966 pr_err("disable command accepted for unsupported access mode %d\n",
8967 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008968 else if (!*rc)
8969 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008970
8971 return 1;
8972}
8973
8974static int fan_write_cmd_speed(const char *cmd, int *rc)
8975{
8976 int speed;
8977
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008978 /* TODO:
8979 * Support speed <low> <medium> <high> ? */
8980
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008981 if (sscanf(cmd, "speed %d", &speed) != 1)
8982 return 0;
8983
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008984 *rc = fan_set_speed(speed);
8985 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008986 pr_err("speed command accepted for unsupported access mode %d\n",
8987 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008988 else if (!*rc)
8989 tpacpi_disclose_usertask("procfs fan",
8990 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008991
8992 return 1;
8993}
8994
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008995static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8996{
8997 int interval;
8998
8999 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9000 return 0;
9001
9002 if (interval < 0 || interval > 120)
9003 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009004 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009005 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009006 tpacpi_disclose_usertask("procfs fan",
9007 "set watchdog timer to %d\n",
9008 interval);
9009 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009010
9011 return 1;
9012}
9013
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009014static int fan_write(char *buf)
9015{
9016 char *cmd;
9017 int rc = 0;
9018
9019 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009020 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009021 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009022 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009023 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009024 fan_write_cmd_disable(cmd, &rc) ||
9025 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009026 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009027 fan_write_cmd_speed(cmd, &rc))
9028 )
9029 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009030 else if (!rc)
9031 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009032 }
9033
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009034 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009035}
9036
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009037static struct ibm_struct fan_driver_data = {
9038 .name = "fan",
9039 .read = fan_read,
9040 .write = fan_write,
9041 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03009042 .suspend = fan_suspend,
9043 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009044};
9045
David Henningsson420f9732013-10-16 23:10:31 +02009046/*************************************************************************
9047 * Mute LED subdriver
9048 */
9049
9050
9051struct tp_led_table {
9052 acpi_string name;
9053 int on_value;
9054 int off_value;
9055 int state;
9056};
9057
9058static struct tp_led_table led_tables[] = {
9059 [TPACPI_LED_MUTE] = {
9060 .name = "SSMS",
9061 .on_value = 1,
9062 .off_value = 0,
9063 },
9064 [TPACPI_LED_MICMUTE] = {
9065 .name = "MMTS",
9066 .on_value = 2,
9067 .off_value = 0,
9068 },
9069};
9070
9071static int mute_led_on_off(struct tp_led_table *t, bool state)
9072{
9073 acpi_handle temp;
9074 int output;
9075
Axel Lin3526eca2016-09-19 09:33:51 +08009076 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009077 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9078 return -EIO;
9079 }
9080
9081 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9082 state ? t->on_value : t->off_value))
9083 return -EIO;
9084
9085 t->state = state;
9086 return state;
9087}
9088
9089int tpacpi_led_set(int whichled, bool on)
9090{
9091 struct tp_led_table *t;
9092
9093 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9094 return -EINVAL;
9095
9096 t = &led_tables[whichled];
9097 if (t->state < 0 || t->state == on)
9098 return t->state;
9099 return mute_led_on_off(t, on);
9100}
9101EXPORT_SYMBOL_GPL(tpacpi_led_set);
9102
9103static int mute_led_init(struct ibm_init_struct *iibm)
9104{
9105 acpi_handle temp;
9106 int i;
9107
9108 for (i = 0; i < TPACPI_LED_MAX; i++) {
9109 struct tp_led_table *t = &led_tables[i];
9110 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9111 mute_led_on_off(t, false);
9112 else
9113 t->state = -ENODEV;
9114 }
9115 return 0;
9116}
9117
9118static void mute_led_exit(void)
9119{
9120 int i;
9121
9122 for (i = 0; i < TPACPI_LED_MAX; i++)
9123 tpacpi_led_set(i, false);
9124}
9125
Takashi Iwai119f4492014-02-12 16:32:45 +01009126static void mute_led_resume(void)
9127{
9128 int i;
9129
9130 for (i = 0; i < TPACPI_LED_MAX; i++) {
9131 struct tp_led_table *t = &led_tables[i];
9132 if (t->state >= 0)
9133 mute_led_on_off(t, t->state);
9134 }
9135}
9136
David Henningsson420f9732013-10-16 23:10:31 +02009137static struct ibm_struct mute_led_driver_data = {
9138 .name = "mute_led",
9139 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01009140 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02009141};
9142
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009143/****************************************************************************
9144 ****************************************************************************
9145 *
9146 * Infrastructure
9147 *
9148 ****************************************************************************
9149 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07009150
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009151/*
9152 * HKEY event callout for other subdrivers go here
9153 * (yes, it is ugly, but it is quick, safe, and gets the job done
9154 */
9155static void tpacpi_driver_event(const unsigned int hkey_event)
9156{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00009157 if (ibm_backlight_device) {
9158 switch (hkey_event) {
9159 case TP_HKEY_EV_BRGHT_UP:
9160 case TP_HKEY_EV_BRGHT_DOWN:
9161 tpacpi_brightness_notify_change();
9162 }
9163 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009164 if (alsa_card) {
9165 switch (hkey_event) {
9166 case TP_HKEY_EV_VOL_UP:
9167 case TP_HKEY_EV_VOL_DOWN:
9168 case TP_HKEY_EV_VOL_MUTE:
9169 volume_alsa_notify_change();
9170 }
9171 }
Hans de Goedec685e202017-02-09 16:44:13 +01009172 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9173 enum led_brightness brightness;
9174
9175 mutex_lock(&kbdlight_mutex);
9176
9177 /*
9178 * Check the brightness actually changed, setting the brightness
9179 * through kbdlight_set_level() also triggers this event.
9180 */
9181 brightness = kbdlight_sysfs_get(NULL);
9182 if (kbdlight_brightness != brightness) {
9183 kbdlight_brightness = brightness;
9184 led_classdev_notify_brightness_hw_changed(
9185 &tpacpi_led_kbdlight.led_classdev, brightness);
9186 }
9187
9188 mutex_unlock(&kbdlight_mutex);
9189 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009190}
9191
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009192static void hotkey_driver_event(const unsigned int scancode)
9193{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03009194 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009195}
9196
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009197/* sysfs name ---------------------------------------------------------- */
9198static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
9199 struct device_attribute *attr,
9200 char *buf)
9201{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009202 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009203}
9204
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009205static DEVICE_ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009206
9207/* --------------------------------------------------------------------- */
9208
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009209/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03009210static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009211
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009212/*
9213 * Module and infrastructure proble, init and exit handling
9214 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009215
Rusty Russell90ab5ee2012-01-13 09:32:20 +10309216static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009217
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009218#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009219static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009220{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009221 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009222
Jan van den Berg72a979f2014-09-17 00:01:08 +02009223 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009224}
9225#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9226
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009227static void ibm_exit(struct ibm_struct *ibm)
9228{
9229 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9230
9231 list_del_init(&ibm->all_drivers);
9232
9233 if (ibm->flags.acpi_notify_installed) {
9234 dbg_printk(TPACPI_DBG_EXIT,
9235 "%s: acpi_remove_notify_handler\n", ibm->name);
9236 BUG_ON(!ibm->acpi);
9237 acpi_remove_notify_handler(*ibm->acpi->handle,
9238 ibm->acpi->type,
9239 dispatch_acpi_notify);
9240 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009241 }
9242
9243 if (ibm->flags.proc_created) {
9244 dbg_printk(TPACPI_DBG_EXIT,
9245 "%s: remove_proc_entry\n", ibm->name);
9246 remove_proc_entry(ibm->name, proc_dir);
9247 ibm->flags.proc_created = 0;
9248 }
9249
9250 if (ibm->flags.acpi_driver_registered) {
9251 dbg_printk(TPACPI_DBG_EXIT,
9252 "%s: acpi_bus_unregister_driver\n", ibm->name);
9253 BUG_ON(!ibm->acpi);
9254 acpi_bus_unregister_driver(ibm->acpi->driver);
9255 kfree(ibm->acpi->driver);
9256 ibm->acpi->driver = NULL;
9257 ibm->flags.acpi_driver_registered = 0;
9258 }
9259
9260 if (ibm->flags.init_called && ibm->exit) {
9261 ibm->exit();
9262 ibm->flags.init_called = 0;
9263 }
9264
9265 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9266}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009267
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009268static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009269{
9270 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009271 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009272 struct proc_dir_entry *entry;
9273
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009274 BUG_ON(ibm == NULL);
9275
9276 INIT_LIST_HEAD(&ibm->all_drivers);
9277
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009278 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009279 return 0;
9280
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009281 dbg_printk(TPACPI_DBG_INIT,
9282 "probing for %s\n", ibm->name);
9283
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009284 if (iibm->init) {
9285 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009286 if (ret > 0)
9287 return 0; /* probe failed */
9288 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009289 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009290
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009291 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292 }
9293
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009294 if (ibm->acpi) {
9295 if (ibm->acpi->hid) {
9296 ret = register_tpacpi_subdriver(ibm);
9297 if (ret)
9298 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009299 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009300
9301 if (ibm->acpi->notify) {
9302 ret = setup_acpi_notify(ibm);
9303 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07009304 pr_notice("disabling subdriver %s\n",
9305 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009306 ret = 0;
9307 goto err_out;
9308 }
9309 if (ret < 0)
9310 goto err_out;
9311 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009312 }
9313
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009314 dbg_printk(TPACPI_DBG_INIT,
9315 "%s installed\n", ibm->name);
9316
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009317 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04009318 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009319
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009320 if (!mode)
9321 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009322 if (ibm->write)
9323 mode |= S_IWUSR;
9324 entry = proc_create_data(ibm->name, mode, proc_dir,
9325 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009326 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07009327 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009328 ret = -ENODEV;
9329 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009330 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009331 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009333
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009334 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9335
Linus Torvalds1da177e2005-04-16 15:20:36 -07009336 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009337
9338err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009339 dbg_printk(TPACPI_DBG_INIT,
9340 "%s: at error exit path with result %d\n",
9341 ibm->name, ret);
9342
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009343 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +02009344 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009345}
9346
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009347/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009348
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009349static bool __pure __init tpacpi_is_fw_digit(const char c)
9350{
9351 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9352}
9353
Jan van den Berg72a979f2014-09-17 00:01:08 +02009354static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009355 const char t)
9356{
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009357 /*
9358 * Most models: xxyTkkWW (#.##c)
9359 * Ancient 570/600 and -SL lacks (#.##c)
9360 */
9361 if (s && strlen(s) >= 8 &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009362 tpacpi_is_fw_digit(s[0]) &&
9363 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009364 s[2] == t &&
9365 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009366 tpacpi_is_fw_digit(s[4]) &&
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009367 tpacpi_is_fw_digit(s[5]))
9368 return true;
9369
9370 /* New models: xxxyTkkW (#.##c); T550 and some others */
9371 return s && strlen(s) >= 8 &&
9372 tpacpi_is_fw_digit(s[0]) &&
9373 tpacpi_is_fw_digit(s[1]) &&
9374 tpacpi_is_fw_digit(s[2]) &&
9375 s[3] == t &&
9376 (s[4] == 'T' || s[4] == 'N') &&
9377 tpacpi_is_fw_digit(s[5]) &&
9378 tpacpi_is_fw_digit(s[6]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009379}
9380
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009381/* returns 0 - probe ok, or < 0 - probe error.
9382 * Probe ok doesn't mean thinkpad found.
9383 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9384static int __must_check __init get_thinkpad_model_data(
9385 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009386{
Jeff Garzik18552562007-10-03 15:15:40 -04009387 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009388 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009389 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009390
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009391 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009392 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009393
9394 memset(tp, 0, sizeof(*tp));
9395
9396 if (dmi_name_in_vendors("IBM"))
9397 tp->vendor = PCI_VENDOR_ID_IBM;
9398 else if (dmi_name_in_vendors("LENOVO"))
9399 tp->vendor = PCI_VENDOR_ID_LENOVO;
9400 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009401 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009402
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009403 s = dmi_get_system_info(DMI_BIOS_VERSION);
9404 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9405 if (s && !tp->bios_version_str)
9406 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009407
9408 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009409 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9410 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009411 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009412
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009413 tp->bios_model = tp->bios_version_str[0]
9414 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009415 tp->bios_release = (tp->bios_version_str[4] << 8)
9416 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009417
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009418 /*
9419 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9420 * X32 or newer, all Z series; Some models must have an
9421 * up-to-date BIOS or they will not be detected.
9422 *
9423 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9424 */
9425 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009426 if (sscanf(dev->name,
9427 "IBM ThinkPad Embedded Controller -[%17c",
9428 ec_fw_string) == 1) {
9429 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9430 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009431
9432 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009433 if (!tp->ec_version_str)
9434 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009435
9436 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9437 tp->ec_model = ec_fw_string[0]
9438 | (ec_fw_string[1] << 8);
9439 tp->ec_release = (ec_fw_string[4] << 8)
9440 | ec_fw_string[5];
9441 } else {
Joe Perches0978e012011-04-04 10:06:25 -07009442 pr_notice("ThinkPad firmware release %s "
9443 "doesn't match the known patterns\n",
9444 ec_fw_string);
9445 pr_notice("please report this to %s\n",
9446 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009447 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009448 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009449 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009450 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009451
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009452 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009453 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009454 tp->model_str = kstrdup(s, GFP_KERNEL);
9455 if (!tp->model_str)
9456 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009457 } else {
9458 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009459 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009460 tp->model_str = kstrdup(s, GFP_KERNEL);
9461 if (!tp->model_str)
9462 return -ENOMEM;
9463 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009464 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03009465
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009466 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9467 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9468 if (s && !tp->nummodel_str)
9469 return -ENOMEM;
9470
9471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009472}
9473
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009474static int __init probe_for_thinkpad(void)
9475{
9476 int is_thinkpad;
9477
9478 if (acpi_disabled)
9479 return -ENODEV;
9480
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009481 /* It would be dangerous to run the driver in this case */
9482 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9483 return -ENODEV;
9484
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009485 /*
9486 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009487 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009488 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009489 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9490 (thinkpad_id.ec_model != 0) ||
9491 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009492
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03009493 /* The EC handler is required */
9494 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009495 if (!ec_handle) {
9496 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07009497 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009498 return -ENODEV;
9499 }
9500
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009501 if (!is_thinkpad && !force_load)
9502 return -ENODEV;
9503
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009504 return 0;
9505}
9506
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009507static void __init thinkpad_acpi_init_banner(void)
9508{
Joe Perches0978e012011-04-04 10:06:25 -07009509 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9510 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009511
Joe Perches0978e012011-04-04 10:06:25 -07009512 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009513 (thinkpad_id.bios_version_str) ?
9514 thinkpad_id.bios_version_str : "unknown",
9515 (thinkpad_id.ec_version_str) ?
9516 thinkpad_id.ec_version_str : "unknown");
9517
9518 BUG_ON(!thinkpad_id.vendor);
9519
9520 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07009521 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009522 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9523 "IBM" : ((thinkpad_id.vendor ==
9524 PCI_VENDOR_ID_LENOVO) ?
9525 "Lenovo" : "Unknown vendor"),
9526 thinkpad_id.model_str,
9527 (thinkpad_id.nummodel_str) ?
9528 thinkpad_id.nummodel_str : "unknown");
9529}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009530
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009531/* Module init, exit, parameters */
9532
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009533static struct ibm_init_struct ibms_init[] __initdata = {
9534 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009535 .data = &thinkpad_acpi_driver_data,
9536 },
9537 {
9538 .init = hotkey_init,
9539 .data = &hotkey_driver_data,
9540 },
9541 {
9542 .init = bluetooth_init,
9543 .data = &bluetooth_driver_data,
9544 },
9545 {
9546 .init = wan_init,
9547 .data = &wan_driver_data,
9548 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009549 {
9550 .init = uwb_init,
9551 .data = &uwb_driver_data,
9552 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009553#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009554 {
9555 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009556 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009557 .data = &video_driver_data,
9558 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009559#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009560 {
Pali Rohárbb28f3d52015-12-30 23:27:41 +01009561 .init = kbdlight_init,
9562 .data = &kbdlight_driver_data,
9563 },
9564 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009565 .init = light_init,
9566 .data = &light_driver_data,
9567 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009568 {
9569 .init = cmos_init,
9570 .data = &cmos_driver_data,
9571 },
9572 {
9573 .init = led_init,
9574 .data = &led_driver_data,
9575 },
9576 {
9577 .init = beep_init,
9578 .data = &beep_driver_data,
9579 },
9580 {
9581 .init = thermal_init,
9582 .data = &thermal_driver_data,
9583 },
9584 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009585 .init = brightness_init,
9586 .data = &brightness_driver_data,
9587 },
9588 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009589 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009590 .data = &volume_driver_data,
9591 },
9592 {
9593 .init = fan_init,
9594 .data = &fan_driver_data,
9595 },
David Henningsson420f9732013-10-16 23:10:31 +02009596 {
9597 .init = mute_led_init,
9598 .data = &mute_led_driver_data,
9599 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009600};
9601
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009602static int __init set_ibm_param(const char *val, struct kernel_param *kp)
9603{
9604 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009605 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009606
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009607 if (!kp || !kp->name || !val)
9608 return -EINVAL;
9609
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009610 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9611 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009612 WARN_ON(ibm == NULL);
9613
9614 if (!ibm || !ibm->name)
9615 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009616
9617 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9618 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009619 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009620 strcpy(ibms_init[i].param, val);
9621 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009622 return 0;
9623 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009624 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009625
9626 return -EINVAL;
9627}
9628
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009629module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009630MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009631 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009632
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009633module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009634MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009635
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009636module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009637MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009638 "Attempts to load the driver even on a "
9639 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009640
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009641module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009642MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009643 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03009644
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009645module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009646MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009647 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00009648 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03009649
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009650module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009651MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009652 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02009653
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009654#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009655module_param_named(volume_mode, volume_mode, uint, 0444);
9656MODULE_PARM_DESC(volume_mode,
9657 "Selects volume control strategy: "
9658 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9659
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02009660module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9661MODULE_PARM_DESC(volume_capabilities,
9662 "Selects the mixer capabilites: "
9663 "0=auto, 1=volume and mute, 2=mute only");
9664
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02009665module_param_named(volume_control, volume_control_allowed, bool, 0444);
9666MODULE_PARM_DESC(volume_control,
9667 "Enables software override for the console audio "
9668 "control when true");
9669
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07009670module_param_named(software_mute, software_mute_requested, bool, 0444);
9671MODULE_PARM_DESC(software_mute,
9672 "Request full software mute control");
9673
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009674/* ALSA module API parameters */
9675module_param_named(index, alsa_index, int, 0444);
9676MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9677module_param_named(id, alsa_id, charp, 0444);
9678MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9679module_param_named(enable, alsa_enable, bool, 0444);
9680MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009681#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009682
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009683#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009684 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02009685 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009686 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009687
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009688TPACPI_PARAM(hotkey);
9689TPACPI_PARAM(bluetooth);
9690TPACPI_PARAM(video);
9691TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009692TPACPI_PARAM(cmos);
9693TPACPI_PARAM(led);
9694TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009695TPACPI_PARAM(brightness);
9696TPACPI_PARAM(volume);
9697TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009698
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009699#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009700module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009701MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9702module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9703MODULE_PARM_DESC(wlsw_state,
9704 "Initial state of the emulated WLSW switch");
9705
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009706module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009707MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9708module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9709MODULE_PARM_DESC(bluetooth_state,
9710 "Initial state of the emulated bluetooth switch");
9711
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009712module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009713MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9714module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9715MODULE_PARM_DESC(wwan_state,
9716 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009717
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009718module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009719MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9720module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9721MODULE_PARM_DESC(uwb_state,
9722 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009723#endif
9724
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009725static void thinkpad_acpi_module_exit(void)
9726{
9727 struct ibm_struct *ibm, *itmp;
9728
9729 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9730
9731 list_for_each_entry_safe_reverse(ibm, itmp,
9732 &tpacpi_all_drivers,
9733 all_drivers) {
9734 ibm_exit(ibm);
9735 }
9736
9737 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9738
9739 if (tpacpi_inputdev) {
9740 if (tp_features.input_device_registered)
9741 input_unregister_device(tpacpi_inputdev);
9742 else
9743 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +10009744 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009745 }
9746
9747 if (tpacpi_hwmon)
9748 hwmon_device_unregister(tpacpi_hwmon);
9749
9750 if (tp_features.sensors_pdev_attrs_registered)
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009751 device_remove_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009752 if (tpacpi_sensors_pdev)
9753 platform_device_unregister(tpacpi_sensors_pdev);
9754 if (tpacpi_pdev)
9755 platform_device_unregister(tpacpi_pdev);
9756
9757 if (tp_features.sensors_pdrv_attrs_registered)
9758 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9759 if (tp_features.platform_drv_attrs_registered)
9760 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9761
9762 if (tp_features.sensors_pdrv_registered)
9763 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9764
9765 if (tp_features.platform_drv_registered)
9766 platform_driver_unregister(&tpacpi_pdriver);
9767
9768 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009769 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009770
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009771 if (tpacpi_wq)
9772 destroy_workqueue(tpacpi_wq);
9773
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009774 kfree(thinkpad_id.bios_version_str);
9775 kfree(thinkpad_id.ec_version_str);
9776 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +10009777 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009778}
9779
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009780
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009781static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009782{
9783 int ret, i;
9784
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009785 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9786
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009787 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009788
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009789 ret = get_thinkpad_model_data(&thinkpad_id);
9790 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009791 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009792 thinkpad_acpi_module_exit();
9793 return ret;
9794 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009795 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009796 if (ret) {
9797 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009798 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009801 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009802
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009803 thinkpad_acpi_init_banner();
9804 tpacpi_check_outdated_fw();
9805
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009806 TPACPI_ACPIHANDLE_INIT(ecrd);
9807 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009809 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9810 if (!tpacpi_wq) {
9811 thinkpad_acpi_module_exit();
9812 return -ENOMEM;
9813 }
9814
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009815 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009816 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07009817 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009818 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009819 return -ENODEV;
9820 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009821
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009822 ret = platform_driver_register(&tpacpi_pdriver);
9823 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009824 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009825 thinkpad_acpi_module_exit();
9826 return ret;
9827 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009828 tp_features.platform_drv_registered = 1;
9829
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009830 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9831 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009832 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009833 thinkpad_acpi_module_exit();
9834 return ret;
9835 }
9836 tp_features.sensors_pdrv_registered = 1;
9837
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009838 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009839 if (!ret) {
9840 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009841 ret = tpacpi_create_driver_attributes(
9842 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009843 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009844 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009845 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009846 thinkpad_acpi_module_exit();
9847 return ret;
9848 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009849 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009850
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009851
9852 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009853 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009854 NULL, 0);
9855 if (IS_ERR(tpacpi_pdev)) {
9856 ret = PTR_ERR(tpacpi_pdev);
9857 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009858 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009859 thinkpad_acpi_module_exit();
9860 return ret;
9861 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009862 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009863 TPACPI_HWMON_DRVR_NAME,
9864 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009865 if (IS_ERR(tpacpi_sensors_pdev)) {
9866 ret = PTR_ERR(tpacpi_sensors_pdev);
9867 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009868 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009869 thinkpad_acpi_module_exit();
9870 return ret;
9871 }
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009872 ret = device_create_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009873 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009874 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009875 thinkpad_acpi_module_exit();
9876 return ret;
9877 }
9878 tp_features.sensors_pdev_attrs_registered = 1;
9879 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009880 if (IS_ERR(tpacpi_hwmon)) {
9881 ret = PTR_ERR(tpacpi_hwmon);
9882 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009883 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009884 thinkpad_acpi_module_exit();
9885 return ret;
9886 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009887 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009888 tpacpi_inputdev = input_allocate_device();
9889 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009890 thinkpad_acpi_module_exit();
9891 return -ENOMEM;
9892 } else {
9893 /* Prepare input device, but don't register */
9894 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009895 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009896 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009897 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009898 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9899 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009900 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009901 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009902
9903 /* Init subdriver dependencies */
9904 tpacpi_detect_brightness_capabilities();
9905
9906 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009907 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9908 ret = ibm_init(&ibms_init[i]);
9909 if (ret >= 0 && *ibms_init[i].param)
9910 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009911 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009912 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009913 return ret;
9914 }
9915 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009916
9917 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9918
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009919 ret = input_register_device(tpacpi_inputdev);
9920 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009921 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009922 thinkpad_acpi_module_exit();
9923 return ret;
9924 } else {
9925 tp_features.input_device_registered = 1;
9926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009927
9928 return 0;
9929}
9930
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009931MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9932
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009933/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009934 * This will autoload the driver in almost every ThinkPad
9935 * in widespread use.
9936 *
9937 * Only _VERY_ old models, like the 240, 240x and 570 lack
9938 * the HKEY event interface.
9939 */
9940MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9941
9942/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009943 * DMI matching for module autoloading
9944 *
9945 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9946 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9947 *
9948 * Only models listed in thinkwiki will be supported, so add yours
9949 * if it is not there yet.
9950 */
9951#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009952 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009953
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009954/* Ancient thinkpad BIOSes have to be identified by
9955 * BIOS type or model number, and there are far less
9956 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009957IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009958
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009959MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9960MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009961MODULE_DESCRIPTION(TPACPI_DESC);
9962MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009963MODULE_LICENSE("GPL");
9964
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009965module_init(thinkpad_acpi_module_init);
9966module_exit(thinkpad_acpi_module_exit);