blob: 7b6cb0c69b02f49faeaf27a0bf48186dcf4c0611 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Joe Perches0978e012011-04-04 10:06:25 -070024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
David Henningsson420f9732013-10-16 23:10:31 +020026#define TPACPI_VERSION "0.25"
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020027#define TPACPI_SYSFS_VERSION 0x020700
Borislav Deianov78f81cc2005-08-17 00:00:00 -040028
29/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020031 * 2007-10-20 changelog trimmed down
32 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030033 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020035 *
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030039 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040040 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020042 *
43 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040044 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
51
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020052#include <linux/kernel.h>
53#include <linux/module.h>
54#include <linux/init.h>
55#include <linux/types.h>
56#include <linux/string.h>
57#include <linux/list.h>
58#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000059#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020060#include <linux/kthread.h>
61#include <linux/freezer.h>
62#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020064#include <linux/nvram.h>
65#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020066#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020067#include <linux/sysfs.h>
68#include <linux/backlight.h>
69#include <linux/fb.h>
70#include <linux/platform_device.h>
71#include <linux/hwmon.h>
72#include <linux/hwmon-sysfs.h>
73#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030074#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030075#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020076#include <linux/dmi.h>
77#include <linux/jiffies.h>
78#include <linux/workqueue.h>
Lv Zheng8b484632013-12-03 08:49:16 +080079#include <linux/acpi.h>
80#include <linux/pci_ids.h>
81#include <linux/thinkpad_acpi.h>
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020082#include <sound/core.h>
83#include <sound/control.h>
84#include <sound/initval.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080085#include <linux/uaccess.h>
Hans de Goedeb33c6ce2015-06-16 16:28:10 +020086#include <acpi/video.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020087
88/* ThinkPad CMOS commands */
89#define TP_CMOS_VOLUME_DOWN 0
90#define TP_CMOS_VOLUME_UP 1
91#define TP_CMOS_VOLUME_MUTE 2
92#define TP_CMOS_BRIGHTNESS_UP 4
93#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030094#define TP_CMOS_THINKLIGHT_ON 12
95#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020096
97/* NVRAM Addresses */
98enum tp_nvram_addr {
99 TP_NVRAM_ADDR_HK2 = 0x57,
100 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
101 TP_NVRAM_ADDR_VIDEO = 0x59,
102 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
103 TP_NVRAM_ADDR_MIXER = 0x60,
104};
105
106/* NVRAM bit masks */
107enum {
108 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
109 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
110 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
111 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
112 TP_NVRAM_MASK_THINKLIGHT = 0x10,
113 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
114 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
115 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
116 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
117 TP_NVRAM_MASK_MUTE = 0x40,
118 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
119 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
120 TP_NVRAM_POS_LEVEL_VOLUME = 0,
121};
122
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300123/* Misc NVRAM-related */
124enum {
125 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
126};
127
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200128/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400129#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
130#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Hui Wanga3c42a42016-11-08 16:13:23 +0800131#define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300132#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200133
134/* Input IDs */
135#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
136#define TPACPI_HKEY_INPUT_VERSION 0x4101
137
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200138/* ACPI \WGSV commands */
139enum {
140 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
141 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
142 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
143 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
144};
145
146/* TP_ACPI_WGSV_GET_STATE bits */
147enum {
148 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
149 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
150 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
151 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
152 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
153 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
154 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
155 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
156 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
157 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
158};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200159
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300160/* HKEY events */
161enum tpacpi_hkey_event_t {
162 /* Hotkey-related */
163 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
164 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
165 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
Hans de Goedec685e202017-02-09 16:44:13 +0100166 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300167 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
168 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
169 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
170
171 /* Reasons for waking up from S3/S4 */
172 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
173 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
174 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
175 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
176 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
177 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
178
179 /* Auto-sleep after eject request */
180 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
181 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
182
183 /* Misc bay events */
184 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -0300185 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
186 or port replicator */
187 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
188 dock or port replicator */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300189
190 /* User-interface events */
191 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
192 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
193 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
194 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
Lyudeb03f4d42016-11-11 15:15:03 -0500195 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
196 * enter/leave tablet mode
197 */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300198 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
199 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
200 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
201
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300202 /* Key-related user-interface events */
203 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
204 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
Xavier Naveira67ab6242015-02-10 08:45:18 +0100205 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300206
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300207 /* Thermal events */
208 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
209 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
210 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
211 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
212 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
213
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100214 /* AC-related events */
215 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300216
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300217 /* Misc */
218 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
219};
220
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200221/****************************************************************************
222 * Main driver
223 */
224
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200225#define TPACPI_NAME "thinkpad"
226#define TPACPI_DESC "ThinkPad ACPI Extras"
227#define TPACPI_FILE TPACPI_NAME "_acpi"
228#define TPACPI_URL "http://ibm-acpi.sf.net/"
229#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200230
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200231#define TPACPI_PROC_DIR "ibm"
232#define TPACPI_ACPI_EVENT_PREFIX "ibm"
233#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300234#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200235#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200236
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300237#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300238#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300239
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200240#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200241
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000242/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200243#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000244#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200245#define TPACPI_DBG_INIT 0x0001
246#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000247#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000248#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000249#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000250#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200251#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200252
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200253#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
254#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
255#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200256
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200257
258/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200259 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200260 */
261
262struct ibm_struct;
263
264struct tp_acpi_drv_struct {
265 const struct acpi_device_id *hid;
266 struct acpi_driver *driver;
267
268 void (*notify) (struct ibm_struct *, u32);
269 acpi_handle *handle;
270 u32 type;
271 struct acpi_device *device;
272};
273
274struct ibm_struct {
275 char *name;
276
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200277 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200278 int (*write) (char *);
279 void (*exit) (void);
280 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200281 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200282 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200283
284 struct list_head all_drivers;
285
286 struct tp_acpi_drv_struct *acpi;
287
288 struct {
289 u8 acpi_driver_registered:1;
290 u8 acpi_notify_installed:1;
291 u8 proc_created:1;
292 u8 init_called:1;
293 u8 experimental:1;
294 } flags;
295};
296
297struct ibm_init_struct {
298 char param[32];
299
300 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400301 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200302 struct ibm_struct *data;
303};
304
305static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200306 u32 bluetooth:1;
307 u32 hotkey:1;
308 u32 hotkey_mask:1;
309 u32 hotkey_wlsw:1;
Lyudeb3180022016-11-11 15:15:02 -0500310 enum {
311 TP_HOTKEY_TABLET_NONE = 0,
312 TP_HOTKEY_TABLET_USES_MHKG,
Lyudeb03f4d42016-11-11 15:15:03 -0500313 /* X1 Yoga 2016, seen on BIOS N1FET44W */
314 TP_HOTKEY_TABLET_USES_CMMD,
Lyudeb3180022016-11-11 15:15:02 -0500315 } hotkey_tablet;
Pali Rohárbb28f3d52015-12-30 23:27:41 +0100316 u32 kbdlight:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200317 u32 light:1;
318 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300319 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300320 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200321 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200322 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200323 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300324 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300325 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200326 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200327 u32 input_device_registered:1;
328 u32 platform_drv_registered:1;
329 u32 platform_drv_attrs_registered:1;
330 u32 sensors_pdrv_registered:1;
331 u32 sensors_pdrv_attrs_registered:1;
332 u32 sensors_pdev_attrs_registered:1;
333 u32 hotkey_poll_active:1;
Bastien Noceraf23a5bc2015-03-02 14:45:16 +0100334 u32 has_adaptive_kbd:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200335} tp_features;
336
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300337static struct {
338 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200339 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300340} tp_warned;
341
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200342struct thinkpad_id_data {
343 unsigned int vendor; /* ThinkPad vendor:
344 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
345
346 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
347 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
348
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300349 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200350 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300351 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
352 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200353
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300354 char *model_str; /* ThinkPad T43 */
355 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200356};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200357static struct thinkpad_id_data thinkpad_id;
358
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300359static enum {
360 TPACPI_LIFE_INIT = 0,
361 TPACPI_LIFE_RUNNING,
362 TPACPI_LIFE_EXITING,
363} tpacpi_lifecycle;
364
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200365static int experimental;
366static u32 dbg_level;
367
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300368static struct workqueue_struct *tpacpi_wq;
369
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000370enum led_status_t {
371 TPACPI_LED_OFF = 0,
372 TPACPI_LED_ON,
373 TPACPI_LED_BLINK,
374};
375
Hans de Goede86ec0c22017-02-09 16:44:12 +0100376/* tpacpi LED class */
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300377struct tpacpi_led_classdev {
378 struct led_classdev led_classdev;
Adam Leeedf2d772013-06-08 16:51:15 +0800379 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300380};
381
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300382/* brightness level capabilities */
383static unsigned int bright_maxlvl; /* 0 = unknown */
384
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200385#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
386static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030387static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200388static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030389static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200390static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030391static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200392static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030393static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200394#endif
395
396
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000397/*************************************************************************
398 * Debugging helpers
399 */
400
Joe Perches0978e012011-04-04 10:06:25 -0700401#define dbg_printk(a_dbg_level, format, arg...) \
402do { \
403 if (dbg_level & (a_dbg_level)) \
404 printk(KERN_DEBUG pr_fmt("%s: " format), \
405 __func__, ##arg); \
406} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000407
408#ifdef CONFIG_THINKPAD_ACPI_DEBUG
409#define vdbg_printk dbg_printk
410static const char *str_supported(int is_supported);
411#else
Joe Perches0978e012011-04-04 10:06:25 -0700412static inline const char *str_supported(int is_supported) { return ""; }
413#define vdbg_printk(a_dbg_level, format, arg...) \
Joe Perchesefd85cf2015-08-26 11:13:38 -0700414 do { if (0) no_printk(format, ##arg); } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000415#endif
416
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000417static void tpacpi_log_usertask(const char * const what)
418{
Joe Perches0978e012011-04-04 10:06:25 -0700419 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
420 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000421}
422
Joe Perches0978e012011-04-04 10:06:25 -0700423#define tpacpi_disclose_usertask(what, format, arg...) \
424do { \
425 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
426 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
427 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
428 what, task_tgid_vnr(current), ## arg); \
429 } \
430} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000431
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300432/*
433 * Quirk handling helpers
434 *
435 * ThinkPad IDs and versions seen in the field so far
436 * are two-characters from the set [0-9A-Z], i.e. base 36.
437 *
438 * We use values well outside that range as specials.
439 */
440
441#define TPACPI_MATCH_ANY 0xffffU
442#define TPACPI_MATCH_UNKNOWN 0U
443
444/* TPID('1', 'Y') == 0x5931 */
445#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
446
447#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_IBM, \
449 .bios = TPID(__id1, __id2), \
450 .ec = TPACPI_MATCH_ANY, \
451 .quirks = (__quirk) }
452
453#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
454 { .vendor = PCI_VENDOR_ID_LENOVO, \
455 .bios = TPID(__id1, __id2), \
456 .ec = TPACPI_MATCH_ANY, \
457 .quirks = (__quirk) }
458
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200459#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
460 { .vendor = PCI_VENDOR_ID_LENOVO, \
461 .bios = TPACPI_MATCH_ANY, \
462 .ec = TPID(__id1, __id2), \
463 .quirks = (__quirk) }
464
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300465struct tpacpi_quirk {
466 unsigned int vendor;
467 u16 bios;
468 u16 ec;
469 unsigned long quirks;
470};
471
472/**
473 * tpacpi_check_quirks() - search BIOS/EC version on a list
474 * @qlist: array of &struct tpacpi_quirk
475 * @qlist_size: number of elements in @qlist
476 *
477 * Iterates over a quirks list until one is found that matches the
478 * ThinkPad's vendor, BIOS and EC model.
479 *
480 * Returns 0 if nothing matches, otherwise returns the quirks field of
481 * the matching &struct tpacpi_quirk entry.
482 *
483 * The match criteria is: vendor, ec and bios much match.
484 */
485static unsigned long __init tpacpi_check_quirks(
486 const struct tpacpi_quirk *qlist,
487 unsigned int qlist_size)
488{
489 while (qlist_size) {
490 if ((qlist->vendor == thinkpad_id.vendor ||
491 qlist->vendor == TPACPI_MATCH_ANY) &&
492 (qlist->bios == thinkpad_id.bios_model ||
493 qlist->bios == TPACPI_MATCH_ANY) &&
494 (qlist->ec == thinkpad_id.ec_model ||
495 qlist->ec == TPACPI_MATCH_ANY))
496 return qlist->quirks;
497
498 qlist_size--;
499 qlist++;
500 }
501 return 0;
502}
503
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300504static inline bool __pure __init tpacpi_is_lenovo(void)
505{
506 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
507}
508
509static inline bool __pure __init tpacpi_is_ibm(void)
510{
511 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
512}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300513
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300514/****************************************************************************
515 ****************************************************************************
516 *
517 * ACPI Helpers and device model
518 *
519 ****************************************************************************
520 ****************************************************************************/
521
522/*************************************************************************
523 * ACPI basic handles
524 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300526static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300527static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200529#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700531 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300532 &parent##_handle; \
533 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200535TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
536TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200538TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
539 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400540 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
541 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300542 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400543
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200544TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400545 "^HKEY", /* R30, R31 */
546 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300547 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400548
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300549/*************************************************************************
550 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200551 */
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700554 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400557 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200558 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400559 struct acpi_buffer result, *resultp;
560 union acpi_object out_obj;
561 acpi_status status;
562 va_list ap;
563 char res_type;
564 int success;
565 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700568 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return 0;
570 }
571
572 if (*fmt == 'q') {
573 quiet = 1;
574 fmt++;
575 } else
576 quiet = 0;
577
578 res_type = *(fmt++);
579
580 params.count = 0;
581 params.pointer = &in_objs[0];
582
583 va_start(ap, fmt);
584 while (*fmt) {
585 char c = *(fmt++);
586 switch (c) {
587 case 'd': /* int */
588 in_objs[params.count].integer.value = va_arg(ap, int);
589 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
590 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400591 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 default:
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 Holschuhd64c81c2008-10-18 14:23:55 -03001088static void tpacpi_disable_brightness_delay(void)
1089{
1090 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001091 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001092}
1093
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001094static void printk_deprecated_attribute(const char * const what,
1095 const char * const details)
1096{
1097 tpacpi_log_usertask("deprecated sysfs attribute");
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 Jenkinsb3fa1322009-06-08 13:27:27 +01001285 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001286 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-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 Jenkinsb3fa1322009-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 --------------------------------------------------- */
1441static ssize_t tpacpi_driver_interface_version_show(
1442 struct device_driver *drv,
1443 char *buf)
1444{
1445 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1446}
1447
1448static DRIVER_ATTR(interface_version, S_IRUGO,
1449 tpacpi_driver_interface_version_show, NULL);
1450
1451/* debug_level --------------------------------------------------------- */
1452static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1453 char *buf)
1454{
1455 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1456}
1457
1458static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1459 const char *buf, size_t count)
1460{
1461 unsigned long t;
1462
1463 if (parse_strtoul(buf, 0xffff, &t))
1464 return -EINVAL;
1465
1466 dbg_level = t;
1467
1468 return count;
1469}
1470
1471static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1472 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1473
1474/* version ------------------------------------------------------------- */
1475static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1476 char *buf)
1477{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001478 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1479 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001480}
1481
1482static DRIVER_ATTR(version, S_IRUGO,
1483 tpacpi_driver_version_show, NULL);
1484
1485/* --------------------------------------------------------------------- */
1486
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001487#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1488
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001489/* wlsw_emulstate ------------------------------------------------------ */
1490static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1491 char *buf)
1492{
1493 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1494}
1495
1496static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1497 const char *buf, size_t count)
1498{
1499 unsigned long t;
1500
1501 if (parse_strtoul(buf, 1, &t))
1502 return -EINVAL;
1503
Johannes Berg19d337d2009-06-02 13:01:37 +02001504 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001505 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001506 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1507 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001508
1509 return count;
1510}
1511
1512static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1513 tpacpi_driver_wlsw_emulstate_show,
1514 tpacpi_driver_wlsw_emulstate_store);
1515
1516/* bluetooth_emulstate ------------------------------------------------- */
1517static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1518 struct device_driver *drv,
1519 char *buf)
1520{
1521 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1522}
1523
1524static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1525 struct device_driver *drv,
1526 const char *buf, size_t count)
1527{
1528 unsigned long t;
1529
1530 if (parse_strtoul(buf, 1, &t))
1531 return -EINVAL;
1532
1533 tpacpi_bluetooth_emulstate = !!t;
1534
1535 return count;
1536}
1537
1538static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1539 tpacpi_driver_bluetooth_emulstate_show,
1540 tpacpi_driver_bluetooth_emulstate_store);
1541
1542/* wwan_emulstate ------------------------------------------------- */
1543static ssize_t tpacpi_driver_wwan_emulstate_show(
1544 struct device_driver *drv,
1545 char *buf)
1546{
1547 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1548}
1549
1550static ssize_t tpacpi_driver_wwan_emulstate_store(
1551 struct device_driver *drv,
1552 const char *buf, size_t count)
1553{
1554 unsigned long t;
1555
1556 if (parse_strtoul(buf, 1, &t))
1557 return -EINVAL;
1558
1559 tpacpi_wwan_emulstate = !!t;
1560
1561 return count;
1562}
1563
1564static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1565 tpacpi_driver_wwan_emulstate_show,
1566 tpacpi_driver_wwan_emulstate_store);
1567
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001568/* uwb_emulstate ------------------------------------------------- */
1569static ssize_t tpacpi_driver_uwb_emulstate_show(
1570 struct device_driver *drv,
1571 char *buf)
1572{
1573 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1574}
1575
1576static ssize_t tpacpi_driver_uwb_emulstate_store(
1577 struct device_driver *drv,
1578 const char *buf, size_t count)
1579{
1580 unsigned long t;
1581
1582 if (parse_strtoul(buf, 1, &t))
1583 return -EINVAL;
1584
1585 tpacpi_uwb_emulstate = !!t;
1586
1587 return count;
1588}
1589
1590static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1591 tpacpi_driver_uwb_emulstate_show,
1592 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001593#endif
1594
1595/* --------------------------------------------------------------------- */
1596
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001597static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001598 &driver_attr_debug_level, &driver_attr_version,
1599 &driver_attr_interface_version,
1600};
1601
1602static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1603{
1604 int i, res;
1605
1606 i = 0;
1607 res = 0;
1608 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1609 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1610 i++;
1611 }
1612
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001613#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1614 if (!res && dbg_wlswemul)
1615 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1616 if (!res && dbg_bluetoothemul)
1617 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1618 if (!res && dbg_wwanemul)
1619 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001620 if (!res && dbg_uwbemul)
1621 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001622#endif
1623
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001624 return res;
1625}
1626
1627static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1628{
1629 int i;
1630
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001631 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001632 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001633
1634#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1635 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1636 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1637 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001638 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001639#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001640}
1641
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001642/*************************************************************************
1643 * Firmware Data
1644 */
1645
1646/*
1647 * Table of recommended minimum BIOS versions
1648 *
1649 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001650 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001651 * bugs and bad ACPI behaviour on older versions
1652 *
1653 * 2. BIOS or EC fw with known bugs that trigger on Linux
1654 *
1655 * 3. BIOS with known reduced functionality in older versions
1656 *
1657 * We recommend the latest BIOS and EC version.
1658 * We only support the latest BIOS and EC fw version as a rule.
1659 *
1660 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1661 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001662 *
1663 * WARNING: we use this table also to detect that the machine is
1664 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001665 */
1666
1667#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1668 { .vendor = (__v), \
1669 .bios = TPID(__id1, __id2), \
1670 .ec = TPACPI_MATCH_ANY, \
1671 .quirks = TPACPI_MATCH_ANY << 16 \
1672 | (__bv1) << 8 | (__bv2) }
1673
1674#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001675 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001676 { .vendor = (__v), \
1677 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001678 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001679 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1680 | (__bv1) << 8 | (__bv2) }
1681
1682#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1683 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1684
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001685/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001686#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001688 __bv1, __bv2, TPID(__id1, __id2), \
1689 __ev1, __ev2), \
1690 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1691 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1692 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001693
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001694/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001695#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1696 __eid1, __eid2, __ev1, __ev2) \
1697 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001698 __bv1, __bv2, TPID(__eid1, __eid2), \
1699 __ev1, __ev2), \
1700 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1701 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1702 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001703
1704#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1705 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1706
1707#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1708 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001709 __bv1, __bv2, TPID(__id1, __id2), \
1710 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001711
1712#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1713 __eid1, __eid2, __ev1, __ev2) \
1714 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001715 __bv1, __bv2, TPID(__eid1, __eid2), \
1716 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001717
1718static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1719 /* Numeric models ------------------ */
1720 /* FW MODEL BIOS VERS */
1721 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1722 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1723 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1724 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1725 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1726 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1727 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1728 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1729 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1730
1731 /* A-series ------------------------- */
1732 /* FW MODEL BIOS VERS EC VERS */
1733 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1734 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1735 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1736 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1737 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1738 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1739 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1740 TPV_QI0('1', '3', '2', '0'), /* A22m */
1741 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1742 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1743 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1744
1745 /* G-series ------------------------- */
1746 /* FW MODEL BIOS VERS */
1747 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1748 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1749
1750 /* R-series, T-series --------------- */
1751 /* FW MODEL BIOS VERS EC VERS */
1752 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1753 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1754 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1755 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1756 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1757 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1758 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1759 T40/p, T41/p, T42/p (1) */
1760 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1761 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1762 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1763 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1764
1765 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1766 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1767 TPV_QI0('1', '6', '3', '2'), /* T22 */
1768 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1769 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1770 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1771
1772 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1773 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001774 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001775
1776 /* BIOS FW BIOS VERS EC FW EC VERS */
1777 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1778 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1779
1780 /* X-series ------------------------- */
1781 /* FW MODEL BIOS VERS EC VERS */
1782 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1783 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1784 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1785 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1786 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1787 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1788 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1789
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001790 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1791 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001792
1793 /* (0) - older versions lack DMI EC fw string and functionality */
1794 /* (1) - older versions known to lack functionality */
1795};
1796
1797#undef TPV_QL1
1798#undef TPV_QL0
1799#undef TPV_QI2
1800#undef TPV_QI1
1801#undef TPV_QI0
1802#undef TPV_Q_X
1803#undef TPV_Q
1804
1805static void __init tpacpi_check_outdated_fw(void)
1806{
1807 unsigned long fwvers;
1808 u16 ec_version, bios_version;
1809
1810 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1811 ARRAY_SIZE(tpacpi_bios_version_qtable));
1812
1813 if (!fwvers)
1814 return;
1815
1816 bios_version = fwvers & 0xffffU;
1817 ec_version = (fwvers >> 16) & 0xffffU;
1818
1819 /* note that unknown versions are set to 0x0000 and we use that */
1820 if ((bios_version > thinkpad_id.bios_release) ||
1821 (ec_version > thinkpad_id.ec_release &&
1822 ec_version != TPACPI_MATCH_ANY)) {
1823 /*
1824 * The changelogs would let us track down the exact
1825 * reason, but it is just too much of a pain to track
1826 * it. We only list BIOSes that are either really
1827 * broken, or really stable to begin with, so it is
1828 * best if the user upgrades the firmware anyway.
1829 */
Joe Perches0978e012011-04-04 10:06:25 -07001830 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1831 pr_warn("WARNING: This firmware may be missing critical bug "
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001832 "fixes and/or important features\n");
1833 }
1834}
1835
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001836static bool __init tpacpi_is_fw_known(void)
1837{
1838 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1839 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1840}
1841
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001842/****************************************************************************
1843 ****************************************************************************
1844 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001845 * Subdrivers
1846 *
1847 ****************************************************************************
1848 ****************************************************************************/
1849
1850/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001851 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001852 */
1853
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001854static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001856 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1857 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001861static struct ibm_struct thinkpad_acpi_driver_data = {
1862 .name = "driver",
1863 .read = thinkpad_acpi_driver_read,
1864};
1865
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001866/*************************************************************************
1867 * Hotkey subdriver
1868 */
1869
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001870/*
1871 * ThinkPad firmware event model
1872 *
1873 * The ThinkPad firmware has two main event interfaces: normal ACPI
1874 * notifications (which follow the ACPI standard), and a private event
1875 * interface.
1876 *
1877 * The private event interface also issues events for the hotkeys. As
1878 * the driver gained features, the event handling code ended up being
1879 * built around the hotkey subdriver. This will need to be refactored
1880 * to a more formal event API eventually.
1881 *
1882 * Some "hotkeys" are actually supposed to be used as event reports,
1883 * such as "brightness has changed", "volume has changed", depending on
1884 * the ThinkPad model and how the firmware is operating.
1885 *
1886 * Unlike other classes, hotkey-class events have mask/unmask control on
1887 * non-ancient firmware. However, how it behaves changes a lot with the
1888 * firmware model and version.
1889 */
1890
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001891enum { /* hot key scan codes (derived from ACPI DSDT) */
1892 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1893 TP_ACPI_HOTKEYSCAN_FNF2,
1894 TP_ACPI_HOTKEYSCAN_FNF3,
1895 TP_ACPI_HOTKEYSCAN_FNF4,
1896 TP_ACPI_HOTKEYSCAN_FNF5,
1897 TP_ACPI_HOTKEYSCAN_FNF6,
1898 TP_ACPI_HOTKEYSCAN_FNF7,
1899 TP_ACPI_HOTKEYSCAN_FNF8,
1900 TP_ACPI_HOTKEYSCAN_FNF9,
1901 TP_ACPI_HOTKEYSCAN_FNF10,
1902 TP_ACPI_HOTKEYSCAN_FNF11,
1903 TP_ACPI_HOTKEYSCAN_FNF12,
1904 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1905 TP_ACPI_HOTKEYSCAN_FNINSERT,
1906 TP_ACPI_HOTKEYSCAN_FNDELETE,
1907 TP_ACPI_HOTKEYSCAN_FNHOME,
1908 TP_ACPI_HOTKEYSCAN_FNEND,
1909 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1910 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1911 TP_ACPI_HOTKEYSCAN_FNSPACE,
1912 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1913 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1914 TP_ACPI_HOTKEYSCAN_MUTE,
1915 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001916 TP_ACPI_HOTKEYSCAN_UNK1,
1917 TP_ACPI_HOTKEYSCAN_UNK2,
1918 TP_ACPI_HOTKEYSCAN_UNK3,
1919 TP_ACPI_HOTKEYSCAN_UNK4,
1920 TP_ACPI_HOTKEYSCAN_UNK5,
1921 TP_ACPI_HOTKEYSCAN_UNK6,
1922 TP_ACPI_HOTKEYSCAN_UNK7,
1923 TP_ACPI_HOTKEYSCAN_UNK8,
1924
Christian Kellner149c8c72017-02-28 17:10:56 +01001925 /* Adaptive keyboard keycodes */
1926 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1927 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
Bastien Nocera6a68d852015-03-02 14:45:31 +01001928 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1929 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1930 TP_ACPI_HOTKEYSCAN_CLOUD,
1931 TP_ACPI_HOTKEYSCAN_UNK9,
1932 TP_ACPI_HOTKEYSCAN_VOICE,
1933 TP_ACPI_HOTKEYSCAN_UNK10,
1934 TP_ACPI_HOTKEYSCAN_GESTURES,
1935 TP_ACPI_HOTKEYSCAN_UNK11,
1936 TP_ACPI_HOTKEYSCAN_UNK12,
1937 TP_ACPI_HOTKEYSCAN_UNK13,
1938 TP_ACPI_HOTKEYSCAN_CONFIG,
1939 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1940 TP_ACPI_HOTKEYSCAN_RELOAD,
1941 TP_ACPI_HOTKEYSCAN_BACK,
1942 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1943 TP_ACPI_HOTKEYSCAN_MIC_UP,
1944 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1945 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1946 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1947
Christian Kellner696c65232017-02-28 17:10:57 +01001948 /* Lenovo extended keymap, starting at 0x1300 */
1949 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1950 /* first new observed key (star, favorites) is 0x1311 */
1951 TP_ACPI_HOTKEYSCAN_STAR = 69,
1952 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1953 TP_ACPI_HOTKEYSCAN_UNK25,
1954 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1955 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1956
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001957 /* Hotkey keymap size */
1958 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001959};
1960
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001961enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001962 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1963 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1964};
1965
1966enum { /* Positions of some of the keys in hotkey masks */
1967 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1968 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1969 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1970 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1971 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
Hans de Goedec685e202017-02-09 16:44:13 +01001972 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001973 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1974 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1975 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1976 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1977 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1978};
1979
1980enum { /* NVRAM to ACPI HKEY group map */
1981 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1982 TP_ACPI_HKEY_ZOOM_MASK |
1983 TP_ACPI_HKEY_DISPSWTCH_MASK |
1984 TP_ACPI_HKEY_HIBERNATE_MASK,
1985 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1986 TP_ACPI_HKEY_BRGHTDWN_MASK,
1987 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1988 TP_ACPI_HKEY_VOLDWN_MASK |
1989 TP_ACPI_HKEY_MUTE_MASK,
1990};
1991
1992#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1993struct tp_nvram_state {
1994 u16 thinkpad_toggle:1;
1995 u16 zoom_toggle:1;
1996 u16 display_toggle:1;
1997 u16 thinklight_toggle:1;
1998 u16 hibernate_toggle:1;
1999 u16 displayexp_toggle:1;
2000 u16 display_state:1;
2001 u16 brightness_toggle:1;
2002 u16 volume_toggle:1;
2003 u16 mute:1;
2004
2005 u8 brightness_level;
2006 u8 volume_level;
2007};
2008
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002009/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002010static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002011
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002012/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002013 * Acquire mutex to write poller control variables as an
2014 * atomic block.
2015 *
2016 * Increment hotkey_config_change when changing them if you
2017 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002018 *
2019 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2020 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002021static struct mutex hotkey_thread_data_mutex;
2022static unsigned int hotkey_config_change;
2023
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002024/*
2025 * hotkey poller control variables
2026 *
2027 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002028 *
2029 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2030 * should be used only when the changes need to be taken as
2031 * a block, OR when one needs to force the kthread to forget
2032 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002033 */
2034static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2035static unsigned int hotkey_poll_freq = 10; /* Hz */
2036
2037#define HOTKEY_CONFIG_CRITICAL_START \
2038 do { \
2039 mutex_lock(&hotkey_thread_data_mutex); \
2040 hotkey_config_change++; \
2041 } while (0);
2042#define HOTKEY_CONFIG_CRITICAL_END \
2043 mutex_unlock(&hotkey_thread_data_mutex);
2044
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002045#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2046
2047#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002048#define HOTKEY_CONFIG_CRITICAL_START
2049#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002050
2051#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2052
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002053static struct mutex hotkey_mutex;
2054
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002055static enum { /* Reasons for waking up */
2056 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2057 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2058 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2059} hotkey_wakeup_reason;
2060
2061static int hotkey_autosleep_ack;
2062
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002063static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2064static u32 hotkey_all_mask; /* all events supported in fw */
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002065static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002066static u32 hotkey_reserved_mask; /* events better left disabled */
2067static u32 hotkey_driver_mask; /* events needed by the driver */
2068static u32 hotkey_user_mask; /* events visible to userspace */
2069static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002070
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002071static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002072
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002073static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002074
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002075static void tpacpi_driver_event(const unsigned int hkey_event);
2076static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002077static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002078
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002079/* HKEY.MHKG() return bits */
2080#define TP_HOTKEY_TABLET_MASK (1 << 3)
Lyudeb03f4d42016-11-11 15:15:03 -05002081/* ThinkPad X1 Yoga (2016) */
2082#define TP_EC_CMMD_TABLET_MODE 0x6
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002083
Johannes Berg19d337d2009-06-02 13:01:37 +02002084static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002085{
Johannes Berg19d337d2009-06-02 13:01:37 +02002086 int status;
2087
2088 if (!tp_features.hotkey_wlsw)
2089 return -ENODEV;
2090
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002091#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002092 if (dbg_wlswemul)
2093 return (tpacpi_wlsw_emulstate) ?
2094 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002095#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002096
2097 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002098 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002099
2100 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002101}
2102
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002103static int hotkey_get_tablet_mode(int *status)
2104{
2105 int s;
2106
Lyudeb03f4d42016-11-11 15:15:03 -05002107 switch (tp_features.hotkey_tablet) {
2108 case TP_HOTKEY_TABLET_USES_MHKG:
2109 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2110 return -EIO;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002111
Lyudeb03f4d42016-11-11 15:15:03 -05002112 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2113 break;
2114 case TP_HOTKEY_TABLET_USES_CMMD:
2115 if (!acpi_evalf(ec_handle, &s, "CMMD", "d"))
2116 return -EIO;
2117
2118 *status = (s == TP_EC_CMMD_TABLET_MODE);
2119 break;
2120 default:
2121 break;
2122 }
2123
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002124 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002125}
2126
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002127/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002128 * Reads current event mask from firmware, and updates
2129 * hotkey_acpi_mask accordingly. Also resets any bits
2130 * from hotkey_user_mask that are unavailable to be
2131 * delivered (shadow requirement of the userspace ABI).
2132 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002133 * Call with hotkey_mutex held
2134 */
2135static int hotkey_mask_get(void)
2136{
2137 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002138 u32 m = 0;
2139
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002140 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002141 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002142
2143 hotkey_acpi_mask = m;
2144 } else {
2145 /* no mask support doesn't mean no event support... */
2146 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002147 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002148
2149 /* sync userspace-visible mask */
2150 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002151
2152 return 0;
2153}
2154
Jean Delvaredf6dd1b2015-04-27 09:45:06 +02002155static void hotkey_mask_warn_incomplete_mask(void)
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002156{
2157 /* log only what the user can fix... */
2158 const u32 wantedmask = hotkey_driver_mask &
2159 ~(hotkey_acpi_mask | hotkey_source_mask) &
2160 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2161
2162 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002163 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002164}
2165
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002166/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002167 * Set the firmware mask when supported
2168 *
2169 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2170 *
2171 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2172 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002173 * Call with hotkey_mutex held
2174 */
2175static int hotkey_mask_set(u32 mask)
2176{
2177 int i;
2178 int rc = 0;
2179
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002180 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002181
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002182 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002183 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002184 if (!acpi_evalf(hkey_handle,
2185 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002186 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002187 rc = -EIO;
2188 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002189 }
2190 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002191 }
2192
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002193 /*
2194 * We *must* make an inconditional call to hotkey_mask_get to
2195 * refresh hotkey_acpi_mask and update hotkey_user_mask
2196 *
2197 * Take the opportunity to also log when we cannot _enable_
2198 * a given event.
2199 */
2200 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Joe Perches0978e012011-04-04 10:06:25 -07002201 pr_notice("asked for hotkey mask 0x%08x, but "
2202 "firmware forced it to 0x%08x\n",
2203 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002204 }
2205
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002206 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2207 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002208
2209 return rc;
2210}
2211
2212/*
2213 * Sets hotkey_user_mask and tries to set the firmware mask
2214 *
2215 * Call with hotkey_mutex held
2216 */
2217static int hotkey_user_mask_set(const u32 mask)
2218{
2219 int rc;
2220
2221 /* Give people a chance to notice they are doing something that
2222 * is bound to go boom on their users sooner or later */
2223 if (!tp_warned.hotkey_mask_ff &&
2224 (mask == 0xffff || mask == 0xffffff ||
2225 mask == 0xffffffff)) {
2226 tp_warned.hotkey_mask_ff = 1;
Joe Perches0978e012011-04-04 10:06:25 -07002227 pr_notice("setting the hotkey mask to 0x%08x is likely "
2228 "not the best way to go about it\n", mask);
2229 pr_notice("please consider using the driver defaults, "
2230 "and refer to up-to-date thinkpad-acpi "
2231 "documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002232 }
2233
2234 /* Try to enable what the user asked for, plus whatever we need.
2235 * this syncs everything but won't enable bits in hotkey_user_mask */
2236 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2237
2238 /* Enable the available bits in hotkey_user_mask */
2239 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2240
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002241 return rc;
2242}
2243
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002244/*
2245 * Sets the driver hotkey mask.
2246 *
2247 * Can be called even if the hotkey subdriver is inactive
2248 */
2249static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2250{
2251 int rc;
2252
2253 /* Do the right thing if hotkey_init has not been called yet */
2254 if (!tp_features.hotkey) {
2255 hotkey_driver_mask = mask;
2256 return 0;
2257 }
2258
2259 mutex_lock(&hotkey_mutex);
2260
2261 HOTKEY_CONFIG_CRITICAL_START
2262 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002263#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002264 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002265#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002266 HOTKEY_CONFIG_CRITICAL_END
2267
2268 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2269 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002270 hotkey_poll_setup(true);
2271
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002272 mutex_unlock(&hotkey_mutex);
2273
2274 return rc;
2275}
2276
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002277static int hotkey_status_get(int *status)
2278{
2279 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2280 return -EIO;
2281
2282 return 0;
2283}
2284
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002285static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002286{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002287 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002288 return -EIO;
2289
2290 return 0;
2291}
2292
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002293static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002294{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002295 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002296
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002297 if (tp_features.hotkey_tablet &&
2298 !hotkey_get_tablet_mode(&state)) {
2299 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002300
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002301 input_report_switch(tpacpi_inputdev,
2302 SW_TABLET_MODE, !!state);
2303 input_sync(tpacpi_inputdev);
2304
2305 mutex_unlock(&tpacpi_inputdev_send_mutex);
2306 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002307}
2308
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002309/* Do NOT call without validating scancode first */
2310static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002311{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002312 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002313
2314 if (keycode != KEY_RESERVED) {
2315 mutex_lock(&tpacpi_inputdev_send_mutex);
2316
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002317 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002318 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002319 input_sync(tpacpi_inputdev);
2320
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002321 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002322 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002323 input_sync(tpacpi_inputdev);
2324
2325 mutex_unlock(&tpacpi_inputdev_send_mutex);
2326 }
2327}
2328
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002329/* Do NOT call without validating scancode first */
2330static void tpacpi_input_send_key_masked(const unsigned int scancode)
2331{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002332 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002333 if (hotkey_user_mask & (1 << scancode))
2334 tpacpi_input_send_key(scancode);
2335}
2336
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002337#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2338static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2339
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002340/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002341static void tpacpi_hotkey_send_key(unsigned int scancode)
2342{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002343 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002344}
2345
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002346static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002347{
2348 u8 d;
2349
2350 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2351 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2352 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2353 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2354 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2355 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2356 }
Hans de Goedec685e202017-02-09 16:44:13 +01002357 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002358 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2359 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2360 }
2361 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2362 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2363 n->displayexp_toggle =
2364 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2365 }
2366 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2367 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2368 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2369 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2370 n->brightness_toggle =
2371 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2372 }
2373 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2374 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2375 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2376 >> TP_NVRAM_POS_LEVEL_VOLUME;
2377 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2378 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2379 }
2380}
2381
Behan Webstera4d44ba2014-02-12 21:58:46 +01002382#define TPACPI_COMPARE_KEY(__scancode, __member) \
2383do { \
2384 if ((event_mask & (1 << __scancode)) && \
2385 oldn->__member != newn->__member) \
2386 tpacpi_hotkey_send_key(__scancode); \
2387} while (0)
2388
2389#define TPACPI_MAY_SEND_KEY(__scancode) \
2390do { \
2391 if (event_mask & (1 << __scancode)) \
2392 tpacpi_hotkey_send_key(__scancode); \
2393} while (0)
2394
2395static void issue_volchange(const unsigned int oldvol,
2396 const unsigned int newvol,
2397 const u32 event_mask)
2398{
2399 unsigned int i = oldvol;
2400
2401 while (i > newvol) {
2402 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2403 i--;
2404 }
2405 while (i < newvol) {
2406 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2407 i++;
2408 }
2409}
2410
2411static void issue_brightnesschange(const unsigned int oldbrt,
2412 const unsigned int newbrt,
2413 const u32 event_mask)
2414{
2415 unsigned int i = oldbrt;
2416
2417 while (i > newbrt) {
2418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2419 i--;
2420 }
2421 while (i < newbrt) {
2422 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2423 i++;
2424 }
2425}
2426
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002427static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2428 struct tp_nvram_state *newn,
2429 const u32 event_mask)
2430{
2431
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002432 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2433 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2434 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2435 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2436
2437 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2438
2439 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2440
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002441 /*
2442 * Handle volume
2443 *
2444 * This code is supposed to duplicate the IBM firmware behaviour:
2445 * - Pressing MUTE issues mute hotkey message, even when already mute
2446 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002447 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002448 * - The act of unmuting issues volume up/down notification,
2449 * depending which key was used to unmute
2450 *
2451 * We are constrained to what the NVRAM can tell us, which is not much
2452 * and certainly not enough if more than one volume hotkey was pressed
2453 * since the last poll cycle.
2454 *
2455 * Just to make our life interesting, some newer Lenovo ThinkPads have
2456 * bugs in the BIOS and may fail to update volume_toggle properly.
2457 */
2458 if (newn->mute) {
2459 /* muted */
2460 if (!oldn->mute ||
2461 oldn->volume_toggle != newn->volume_toggle ||
2462 oldn->volume_level != newn->volume_level) {
2463 /* recently muted, or repeated mute keypress, or
2464 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002465 issue_volchange(oldn->volume_level, newn->volume_level,
2466 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002467 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2468 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002469 } else {
2470 /* unmute */
2471 if (oldn->mute) {
2472 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002473 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002474 }
2475 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002476 issue_volchange(oldn->volume_level, newn->volume_level,
2477 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002478 } else if (oldn->volume_toggle != newn->volume_toggle) {
2479 /* repeated vol up/down keypress at end of scale ? */
2480 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002481 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002482 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2483 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002484 }
2485 }
2486
2487 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002488 if (oldn->brightness_level != newn->brightness_level) {
2489 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002490 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002491 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2492 /* repeated key presses that didn't change state */
2493 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002494 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002495 else if (newn->brightness_level >= bright_maxlvl
2496 && !tp_features.bright_unkfw)
2497 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002498 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002499
2500#undef TPACPI_COMPARE_KEY
2501#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002502}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002503
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002504/*
2505 * Polling driver
2506 *
2507 * We track all events in hotkey_source_mask all the time, since
2508 * most of them are edge-based. We only issue those requested by
2509 * hotkey_user_mask or hotkey_driver_mask, though.
2510 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002511static int hotkey_kthread(void *data)
2512{
2513 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002514 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002515 unsigned int si, so;
2516 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002517 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002518 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002519 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002520
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002521 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2522 goto exit;
2523
2524 set_freezable();
2525
2526 so = 0;
2527 si = 1;
2528 t = 0;
2529
2530 /* Initial state for compares */
2531 mutex_lock(&hotkey_thread_data_mutex);
2532 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002533 poll_mask = hotkey_source_mask;
2534 event_mask = hotkey_source_mask &
2535 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002536 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002537 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002538 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002539
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002540 while (!kthread_should_stop()) {
2541 if (t == 0) {
2542 if (likely(poll_freq))
2543 t = 1000/poll_freq;
2544 else
2545 t = 100; /* should never happen... */
2546 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002547 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002548 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002549 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002550
2551 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002552 continue;
2553
2554 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002555 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002556 /* forget old state on thaw or config change */
2557 si = so;
2558 t = 0;
2559 change_detector = hotkey_config_change;
2560 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002561 poll_mask = hotkey_source_mask;
2562 event_mask = hotkey_source_mask &
2563 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002564 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002565 mutex_unlock(&hotkey_thread_data_mutex);
2566
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002567 if (likely(poll_mask)) {
2568 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002569 if (likely(si != so)) {
2570 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002571 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002572 }
2573 }
2574
2575 so = si;
2576 si ^= 1;
2577 }
2578
2579exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002580 return 0;
2581}
2582
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002583/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002584static void hotkey_poll_stop_sync(void)
2585{
2586 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002587 kthread_stop(tpacpi_hotkey_task);
2588 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002589 }
2590}
2591
2592/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002593static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002594{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002595 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2596 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002597
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002598 if (hotkey_poll_freq > 0 &&
2599 (poll_driver_mask ||
2600 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002601 if (!tpacpi_hotkey_task) {
2602 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002603 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002604 if (IS_ERR(tpacpi_hotkey_task)) {
2605 tpacpi_hotkey_task = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07002606 pr_err("could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002607 "for hotkey polling\n");
2608 }
2609 }
2610 } else {
2611 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002612 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002613 hotkey_poll_freq == 0) {
Joe Perches0978e012011-04-04 10:06:25 -07002614 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2615 "require polling, which is currently "
2616 "disabled\n",
2617 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002618 }
2619 }
2620}
2621
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002622static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002623{
2624 mutex_lock(&hotkey_mutex);
2625 hotkey_poll_setup(may_warn);
2626 mutex_unlock(&hotkey_mutex);
2627}
2628
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002629/* call with hotkey_mutex held */
2630static void hotkey_poll_set_freq(unsigned int freq)
2631{
2632 if (!freq)
2633 hotkey_poll_stop_sync();
2634
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002635 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002636}
2637
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002638#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2639
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002640static void hotkey_poll_setup(const bool __unused)
2641{
2642}
2643
2644static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002645{
2646}
2647
2648#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2649
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002650static int hotkey_inputdev_open(struct input_dev *dev)
2651{
2652 switch (tpacpi_lifecycle) {
2653 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002654 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002655 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002656 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002657 case TPACPI_LIFE_EXITING:
2658 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002659 }
2660
2661 /* Should only happen if tpacpi_lifecycle is corrupt */
2662 BUG();
2663 return -EBUSY;
2664}
2665
2666static void hotkey_inputdev_close(struct input_dev *dev)
2667{
2668 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002669 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002670 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002671 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002672}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002673
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002674/* sysfs hotkey enable ------------------------------------------------- */
2675static ssize_t hotkey_enable_show(struct device *dev,
2676 struct device_attribute *attr,
2677 char *buf)
2678{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002679 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002680
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002681 printk_deprecated_attribute("hotkey_enable",
2682 "Hotkey reporting is always enabled");
2683
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002684 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002685 if (res)
2686 return res;
2687
2688 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2689}
2690
2691static ssize_t hotkey_enable_store(struct device *dev,
2692 struct device_attribute *attr,
2693 const char *buf, size_t count)
2694{
2695 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002696
2697 printk_deprecated_attribute("hotkey_enable",
2698 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002699
2700 if (parse_strtoul(buf, 1, &t))
2701 return -EINVAL;
2702
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002703 if (t == 0)
2704 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002705
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002706 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002707}
2708
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002709static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002710
2711/* sysfs hotkey mask --------------------------------------------------- */
2712static ssize_t hotkey_mask_show(struct device *dev,
2713 struct device_attribute *attr,
2714 char *buf)
2715{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002716 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002717}
2718
2719static ssize_t hotkey_mask_store(struct device *dev,
2720 struct device_attribute *attr,
2721 const char *buf, size_t count)
2722{
2723 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002724 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002725
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002726 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002727 return -EINVAL;
2728
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002729 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002730 return -ERESTARTSYS;
2731
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002732 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002733
2734#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002735 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002736#endif
2737
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002738 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002739
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002740 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2741
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002742 return (res) ? res : count;
2743}
2744
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002745static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002746
2747/* sysfs hotkey bios_enabled ------------------------------------------- */
2748static ssize_t hotkey_bios_enabled_show(struct device *dev,
2749 struct device_attribute *attr,
2750 char *buf)
2751{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002752 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002753}
2754
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002755static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002756
2757/* sysfs hotkey bios_mask ---------------------------------------------- */
2758static ssize_t hotkey_bios_mask_show(struct device *dev,
2759 struct device_attribute *attr,
2760 char *buf)
2761{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002762 printk_deprecated_attribute("hotkey_bios_mask",
2763 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002764 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002765}
2766
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002767static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002768
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002769/* sysfs hotkey all_mask ----------------------------------------------- */
2770static ssize_t hotkey_all_mask_show(struct device *dev,
2771 struct device_attribute *attr,
2772 char *buf)
2773{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002774 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2775 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002776}
2777
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002778static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002779
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002780/* sysfs hotkey all_mask ----------------------------------------------- */
2781static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2782 struct device_attribute *attr,
2783 char *buf)
2784{
2785 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2786 hotkey_adaptive_all_mask | hotkey_source_mask);
2787}
2788
2789static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2790
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002791/* sysfs hotkey recommended_mask --------------------------------------- */
2792static ssize_t hotkey_recommended_mask_show(struct device *dev,
2793 struct device_attribute *attr,
2794 char *buf)
2795{
2796 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002797 (hotkey_all_mask | hotkey_source_mask)
2798 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002799}
2800
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002801static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002802
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002803#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2804
2805/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2806static ssize_t hotkey_source_mask_show(struct device *dev,
2807 struct device_attribute *attr,
2808 char *buf)
2809{
2810 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2811}
2812
2813static ssize_t hotkey_source_mask_store(struct device *dev,
2814 struct device_attribute *attr,
2815 const char *buf, size_t count)
2816{
2817 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002818 u32 r_ev;
2819 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002820
2821 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2822 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2823 return -EINVAL;
2824
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002825 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002826 return -ERESTARTSYS;
2827
2828 HOTKEY_CONFIG_CRITICAL_START
2829 hotkey_source_mask = t;
2830 HOTKEY_CONFIG_CRITICAL_END
2831
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002832 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2833 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002834 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002835
2836 /* check if events needed by the driver got disabled */
2837 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2838 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002839
2840 mutex_unlock(&hotkey_mutex);
2841
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002842 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07002843 pr_err("hotkey_source_mask: "
2844 "failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002845
2846 if (r_ev)
Joe Perches0978e012011-04-04 10:06:25 -07002847 pr_notice("hotkey_source_mask: "
2848 "some important events were disabled: 0x%04x\n",
2849 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002850
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002851 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2852
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002853 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002854}
2855
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002856static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002857
2858/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2859static ssize_t hotkey_poll_freq_show(struct device *dev,
2860 struct device_attribute *attr,
2861 char *buf)
2862{
2863 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2864}
2865
2866static ssize_t hotkey_poll_freq_store(struct device *dev,
2867 struct device_attribute *attr,
2868 const char *buf, size_t count)
2869{
2870 unsigned long t;
2871
2872 if (parse_strtoul(buf, 25, &t))
2873 return -EINVAL;
2874
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002875 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002876 return -ERESTARTSYS;
2877
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002878 hotkey_poll_set_freq(t);
2879 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002880
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002881 mutex_unlock(&hotkey_mutex);
2882
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002883 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2884
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002885 return count;
2886}
2887
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002888static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002889
2890#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2891
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002892/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002893static ssize_t hotkey_radio_sw_show(struct device *dev,
2894 struct device_attribute *attr,
2895 char *buf)
2896{
Johannes Berg19d337d2009-06-02 13:01:37 +02002897 int res;
2898 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002899 if (res < 0)
2900 return res;
2901
Johannes Berg19d337d2009-06-02 13:01:37 +02002902 /* Opportunistic update */
2903 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2904
2905 return snprintf(buf, PAGE_SIZE, "%d\n",
2906 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002907}
2908
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002909static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002910
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002911static void hotkey_radio_sw_notify_change(void)
2912{
2913 if (tp_features.hotkey_wlsw)
2914 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2915 "hotkey_radio_sw");
2916}
2917
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002918/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2919static ssize_t hotkey_tablet_mode_show(struct device *dev,
2920 struct device_attribute *attr,
2921 char *buf)
2922{
2923 int res, s;
2924 res = hotkey_get_tablet_mode(&s);
2925 if (res < 0)
2926 return res;
2927
2928 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2929}
2930
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002931static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002932
2933static void hotkey_tablet_mode_notify_change(void)
2934{
2935 if (tp_features.hotkey_tablet)
2936 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2937 "hotkey_tablet_mode");
2938}
2939
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002940/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002941static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2942 struct device_attribute *attr,
2943 char *buf)
2944{
2945 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2946}
2947
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002948static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002949
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002950static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002951{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002952 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2953 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002954}
2955
2956/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002957static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2958 struct device_attribute *attr,
2959 char *buf)
2960{
2961 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2962}
2963
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002964static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2965 hotkey_wakeup_hotunplug_complete_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002966
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002967static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002968{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002969 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2970 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002971}
2972
Bastien Nocerab790cee2015-03-02 14:45:27 +01002973/* sysfs adaptive kbd mode --------------------------------------------- */
2974
2975static int adaptive_keyboard_get_mode(void);
2976static int adaptive_keyboard_set_mode(int new_mode);
2977
2978enum ADAPTIVE_KEY_MODE {
2979 HOME_MODE,
2980 WEB_BROWSER_MODE,
2981 WEB_CONFERENCE_MODE,
2982 FUNCTION_MODE,
2983 LAYFLAT_MODE
2984};
2985
2986static ssize_t adaptive_kbd_mode_show(struct device *dev,
2987 struct device_attribute *attr,
2988 char *buf)
2989{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03002990 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01002991
2992 current_mode = adaptive_keyboard_get_mode();
2993 if (current_mode < 0)
2994 return current_mode;
2995
2996 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
2997}
2998
2999static ssize_t adaptive_kbd_mode_store(struct device *dev,
3000 struct device_attribute *attr,
3001 const char *buf, size_t count)
3002{
3003 unsigned long t;
3004 int res;
3005
3006 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
3007 return -EINVAL;
3008
3009 res = adaptive_keyboard_set_mode(t);
3010 return (res < 0) ? res : count;
3011}
3012
3013static DEVICE_ATTR_RW(adaptive_kbd_mode);
3014
3015static struct attribute *adaptive_kbd_attributes[] = {
3016 &dev_attr_adaptive_kbd_mode.attr,
3017 NULL
3018};
3019
3020static const struct attribute_group adaptive_kbd_attr_group = {
3021 .attrs = adaptive_kbd_attributes,
3022};
3023
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003024/* --------------------------------------------------------------------- */
3025
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003026static struct attribute *hotkey_attributes[] __initdata = {
3027 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003028 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003029 &dev_attr_hotkey_bios_mask.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003030 &dev_attr_wakeup_reason.attr,
3031 &dev_attr_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003032 &dev_attr_hotkey_mask.attr,
3033 &dev_attr_hotkey_all_mask.attr,
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003034 &dev_attr_hotkey_adaptive_all_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003035 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003036#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003037 &dev_attr_hotkey_source_mask.attr,
3038 &dev_attr_hotkey_poll_freq.attr,
3039#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003040};
3041
Johannes Berg19d337d2009-06-02 13:01:37 +02003042/*
3043 * Sync both the hw and sw blocking state of all switches
3044 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003045static void tpacpi_send_radiosw_update(void)
3046{
3047 int wlsw;
3048
Johannes Berg19d337d2009-06-02 13:01:37 +02003049 /*
3050 * We must sync all rfkill controllers *before* issuing any
3051 * rfkill input events, or we will race the rfkill core input
3052 * handler.
3053 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003054 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003055 * for the above.
3056 *
3057 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3058 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003059
Johannes Berg19d337d2009-06-02 13:01:37 +02003060 wlsw = hotkey_get_wlsw();
3061
3062 /* Sync hw blocking state first if it is hw-blocked */
3063 if (wlsw == TPACPI_RFK_RADIO_OFF)
3064 tpacpi_rfk_update_hwblock_state(true);
3065
3066 /* Sync sw blocking state */
3067 tpacpi_rfk_update_swstate_all();
3068
3069 /* Sync hw blocking state last if it is hw-unblocked */
3070 if (wlsw == TPACPI_RFK_RADIO_ON)
3071 tpacpi_rfk_update_hwblock_state(false);
3072
3073 /* Issue rfkill input event for WLSW switch */
3074 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003075 mutex_lock(&tpacpi_inputdev_send_mutex);
3076
3077 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003078 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003079 input_sync(tpacpi_inputdev);
3080
3081 mutex_unlock(&tpacpi_inputdev_send_mutex);
3082 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003083
3084 /*
3085 * this can be unconditional, as we will poll state again
3086 * if userspace uses the notify to read data
3087 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003088 hotkey_radio_sw_notify_change();
3089}
3090
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003091static void hotkey_exit(void)
3092{
3093#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003094 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003095 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003096 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003097#endif
3098
3099 if (hotkey_dev_attributes)
3100 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3101
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003102 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003103 "restoring original HKEY status and mask\n");
3104 /* yes, there is a bitwise or below, we want the
3105 * functions to be called even if one of them fail */
3106 if (((tp_features.hotkey_mask &&
3107 hotkey_mask_set(hotkey_orig_mask)) |
3108 hotkey_status_set(false)) != 0)
Joe Perches0978e012011-04-04 10:06:25 -07003109 pr_err("failed to restore hot key mask "
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003110 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003111}
3112
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003113static void __init hotkey_unmap(const unsigned int scancode)
3114{
3115 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3116 clear_bit(hotkey_keycode_map[scancode],
3117 tpacpi_inputdev->keybit);
3118 hotkey_keycode_map[scancode] = KEY_RESERVED;
3119 }
3120}
3121
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003122/*
3123 * HKEY quirks:
3124 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3125 */
3126
3127#define TPACPI_HK_Q_INIMASK 0x0001
3128
3129static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3130 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3131 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3132 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3133 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3134 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3135 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3136 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3137 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3138 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3139 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3140 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3141 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3142 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3143 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3144 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3145 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3146 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3147 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3148 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3149};
3150
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003151typedef u16 tpacpi_keymap_entry_t;
3152typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003153
Lyudeb3180022016-11-11 15:15:02 -05003154static int hotkey_init_tablet_mode(void)
3155{
Darren Hartcb2bf252016-12-14 14:45:48 -08003156 int in_tablet_mode = 0, res;
3157 char *type = NULL;
Lyudeb3180022016-11-11 15:15:02 -05003158
Lyudeb3180022016-11-11 15:15:02 -05003159 if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
Lyudeb03f4d42016-11-11 15:15:03 -05003160 /* For X41t, X60t, X61t Tablets... */
Lyudeb3180022016-11-11 15:15:02 -05003161 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3162 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3163 type = "MHKG";
Lyudeb03f4d42016-11-11 15:15:03 -05003164 } else if (acpi_evalf(ec_handle, &res, "CMMD", "qd")) {
3165 /* For X1 Yoga (2016) */
3166 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_CMMD;
3167 in_tablet_mode = res == TP_EC_CMMD_TABLET_MODE;
3168 type = "CMMD";
Lyudeb3180022016-11-11 15:15:02 -05003169 }
3170
3171 if (!tp_features.hotkey_tablet)
3172 return 0;
3173
3174 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3175 type, in_tablet_mode ? "tablet" : "laptop");
3176
3177 res = add_to_attr_set(hotkey_dev_attributes,
3178 &dev_attr_hotkey_tablet_mode.attr);
3179 if (res)
3180 return -1;
3181
3182 return in_tablet_mode;
3183}
3184
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003185static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003186{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003187 /* Requirements for changing the default keymaps:
3188 *
3189 * 1. Many of the keys are mapped to KEY_RESERVED for very
3190 * good reasons. Do not change them unless you have deep
3191 * knowledge on the IBM and Lenovo ThinkPad firmware for
3192 * the various ThinkPad models. The driver behaves
3193 * differently for KEY_RESERVED: such keys have their
3194 * hot key mask *unset* in mask_recommended, and also
3195 * in the initial hot key mask programmed into the
3196 * firmware at driver load time, which means the firm-
3197 * ware may react very differently if you change them to
3198 * something else;
3199 *
3200 * 2. You must be subscribed to the linux-thinkpad and
3201 * ibm-acpi-devel mailing lists, and you should read the
3202 * list archives since 2007 if you want to change the
3203 * keymaps. This requirement exists so that you will
3204 * know the past history of problems with the thinkpad-
3205 * acpi driver keymaps, and also that you will be
3206 * listening to any bug reports;
3207 *
3208 * 3. Do not send thinkpad-acpi specific patches directly to
3209 * for merging, *ever*. Send them to the linux-acpi
3210 * mailinglist for comments. Merging is to be done only
3211 * through acpi-test and the ACPI maintainer.
3212 *
3213 * If the above is too much to ask, don't change the keymap.
3214 * Ask the thinkpad-acpi maintainer to do it, instead.
3215 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003216
3217 enum keymap_index {
3218 TPACPI_KEYMAP_IBM_GENERIC = 0,
3219 TPACPI_KEYMAP_LENOVO_GENERIC,
3220 };
3221
3222 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3223 /* Generic keymap for IBM ThinkPads */
3224 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003225 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003226 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003227 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3228 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003229
3230 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003231 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3232 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3233 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003234
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003235 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003236 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003237 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003238
3239 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003240 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003241
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003242 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3243 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003244
3245 /* Volume: firmware always react to it and reprograms
3246 * the built-in *extra* mixer. Never map it to control
3247 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003248 KEY_RESERVED, /* 0x14: VOLUME UP */
3249 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3250 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003251
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003252 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003253
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003254 /* (assignments unknown, please report if found) */
3255 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3256 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003257
3258 /* No assignments, only used for Adaptive keyboards. */
3259 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3260 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3261 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3262 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3263 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Christian Kellner696c65232017-02-28 17:10:57 +01003264
3265 /* No assignment, used for newer Lenovo models */
3266 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3267 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3268 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3269 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3270 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3271 KEY_UNKNOWN, KEY_UNKNOWN
3272
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003273 },
3274
3275 /* Generic keymap for Lenovo ThinkPads */
3276 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003277 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3278 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003279 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003280 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003281
3282 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003283 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3284 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3285 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003286
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003287 /* These should be enabled --only-- when ACPI video
3288 * is disabled (i.e. in "vendor" mode), and are handled
3289 * in a special way by the init code */
3290 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3291 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003292
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003293 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003294
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003295 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3296 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003297
3298 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3299 * react to it and reprograms the built-in *extra* mixer.
3300 * Never map it to control another mixer by default.
3301 *
3302 * T60?, T61, R60?, R61: firmware and EC tries to send
3303 * these over the regular keyboard, so these are no-ops,
3304 * but there are still weird bugs re. MUTE, so do not
3305 * change unless you get test reports from all Lenovo
3306 * models. May cause the BIOS to interfere with the
3307 * HDA mixer.
3308 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003309 KEY_RESERVED, /* 0x14: VOLUME UP */
3310 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3311 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003312
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003313 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003314
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003315 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003316 KEY_UNKNOWN, KEY_UNKNOWN,
3317
3318 /*
3319 * The mic mute button only sends 0x1a. It does not
3320 * automatically mute the mic or change the mute light.
3321 */
3322 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3323
3324 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003325 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003326
3327 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003328 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003329
3330 /*
3331 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3332 * The first item in this list is the Mute button which is
3333 * emitted with 0x103 through
3334 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3335 * symbol is held.
3336 * We'll need to offset those by 0x20.
3337 */
3338 KEY_RESERVED, /* Mute held, 0x103 */
3339 KEY_BRIGHTNESS_MIN, /* Backlight off */
3340 KEY_RESERVED, /* Clipping tool */
3341 KEY_RESERVED, /* Cloud */
3342 KEY_RESERVED,
3343 KEY_VOICECOMMAND, /* Voice */
3344 KEY_RESERVED,
3345 KEY_RESERVED, /* Gestures */
3346 KEY_RESERVED,
3347 KEY_RESERVED,
3348 KEY_RESERVED,
3349 KEY_CONFIG, /* Settings */
3350 KEY_RESERVED, /* New tab */
3351 KEY_REFRESH, /* Reload */
3352 KEY_BACK, /* Back */
3353 KEY_RESERVED, /* Microphone down */
3354 KEY_RESERVED, /* Microphone up */
3355 KEY_RESERVED, /* Microphone cancellation */
3356 KEY_RESERVED, /* Camera mode */
3357 KEY_RESERVED, /* Rotate display, 0x116 */
Christian Kellner696c65232017-02-28 17:10:57 +01003358
3359 /*
3360 * These are found in 2017 models (e.g. T470s, X270).
3361 * The lowest known value is 0x311, which according to
3362 * the manual should launch a user defined favorite
3363 * application.
3364 *
3365 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3366 * corresponding to 0x34.
3367 */
3368
3369 /* (assignments unknown, please report if found) */
3370 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3371 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3372 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3373 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3374 KEY_UNKNOWN,
3375
3376 KEY_FAVORITES, /* Favorite app, 0x311 */
3377 KEY_RESERVED, /* Clipping tool */
3378 KEY_RESERVED,
3379 KEY_BLUETOOTH, /* Bluetooth */
3380 KEY_KEYBOARD /* Keyboard, 0x315 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003381 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003382 };
3383
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003384 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3385 /* Generic maps (fallback) */
3386 {
3387 .vendor = PCI_VENDOR_ID_IBM,
3388 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3389 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3390 },
3391 {
3392 .vendor = PCI_VENDOR_ID_LENOVO,
3393 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3394 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3395 },
3396 };
3397
3398#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003399#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003400
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003401 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003402 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003403 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003404 bool radiosw_state = false;
3405 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003406
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003407 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003408 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003409
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003410 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3411 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003412
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003413 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003414 BUG_ON(tpacpi_inputdev->open != NULL ||
3415 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003416
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003417 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003418 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003419
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003420#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003421 mutex_init(&hotkey_thread_data_mutex);
3422#endif
3423
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003424 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003425 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003426
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003427 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3428 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003429 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003430
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003431 if (!tp_features.hotkey)
3432 return 1;
3433
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003434 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3435 ARRAY_SIZE(tpacpi_hotkey_qtable));
3436
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003437 tpacpi_disable_brightness_delay();
3438
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003439 /* MUST have enough space for all attributes to be added to
3440 * hotkey_dev_attributes */
3441 hotkey_dev_attributes = create_attr_set(
3442 ARRAY_SIZE(hotkey_attributes) + 2,
3443 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003444 if (!hotkey_dev_attributes)
3445 return -ENOMEM;
3446 res = add_many_to_attr_set(hotkey_dev_attributes,
3447 hotkey_attributes,
3448 ARRAY_SIZE(hotkey_attributes));
3449 if (res)
3450 goto err_exit;
3451
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003452 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003453 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3454 for HKEY interface version 0x100 */
3455 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003456 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3457 "firmware HKEY interface version: 0x%x\n",
3458 hkeyv);
3459
3460 switch (hkeyv >> 8) {
3461 case 1:
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003462 /*
3463 * MHKV 0x100 in A31, R40, R40e,
3464 * T4x, X31, and later
3465 */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003466
3467 /* Paranoia check AND init hotkey_all_mask */
3468 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3469 "MHKA", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003470 pr_err("missing MHKA handler, please report this to %s\n",
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003471 TPACPI_MAIL);
3472 /* Fallback: pre-init for FN+F3,F4,F12 */
3473 hotkey_all_mask = 0x080cU;
3474 } else {
3475 tp_features.hotkey_mask = 1;
3476 }
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003477 break;
3478
3479 case 2:
3480 /*
3481 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3482 */
3483
3484 /* Paranoia check AND init hotkey_all_mask */
3485 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3486 "MHKA", "dd", 1)) {
3487 pr_err("missing MHKA handler, please report this to %s\n",
3488 TPACPI_MAIL);
3489 /* Fallback: pre-init for FN+F3,F4,F12 */
3490 hotkey_all_mask = 0x080cU;
3491 } else {
3492 tp_features.hotkey_mask = 1;
3493 }
3494
3495 /*
3496 * Check if we have an adaptive keyboard, like on the
3497 * Lenovo Carbon X1 2014 (2nd Gen).
3498 */
3499 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3500 "MHKA", "dd", 2)) {
3501 if (hotkey_adaptive_all_mask != 0) {
3502 tp_features.has_adaptive_kbd = true;
3503 res = sysfs_create_group(
3504 &tpacpi_pdev->dev.kobj,
3505 &adaptive_kbd_attr_group);
3506 if (res)
3507 goto err_exit;
3508 }
3509 } else {
3510 tp_features.has_adaptive_kbd = false;
3511 hotkey_adaptive_all_mask = 0x0U;
3512 }
3513 break;
3514
3515 default:
3516 pr_err("unknown version of the HKEY interface: 0x%x\n",
3517 hkeyv);
3518 pr_err("please report this to %s\n", TPACPI_MAIL);
3519 break;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003520 }
3521 }
3522
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003523 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3524 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003525 str_supported(tp_features.hotkey_mask));
3526
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003527 /* Init hotkey_all_mask if not initialized yet */
3528 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3529 (quirks & TPACPI_HK_Q_INIMASK))
3530 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003531
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003532 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003533 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003534 /* hotkey_source_mask *must* be zero for
3535 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003536 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003537 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003538 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003539
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003540 hotkey_orig_mask = hotkey_acpi_mask;
3541 } else {
3542 hotkey_orig_mask = hotkey_all_mask;
3543 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003544 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003545
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003546#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3547 if (dbg_wlswemul) {
3548 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003549 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003550 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003551 } else
3552#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003553 /* Not all thinkpads have a hardware radio switch */
3554 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3555 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003556 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003557 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003558 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003559 }
3560 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003561 res = add_to_attr_set(hotkey_dev_attributes,
3562 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003563
Lyudeb3180022016-11-11 15:15:02 -05003564 res = hotkey_init_tablet_mode();
3565 if (res < 0)
3566 goto err_exit;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003567
Lyudeb3180022016-11-11 15:15:02 -05003568 tabletsw_state = res;
3569
3570 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3571 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003572 if (res)
3573 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003574
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003575 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003576 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3577 GFP_KERNEL);
3578 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003579 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003580 res = -ENOMEM;
3581 goto err_exit;
3582 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003583
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003584 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3585 ARRAY_SIZE(tpacpi_keymap_qtable));
3586 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3587 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3588 "using keymap number %lu\n", keymap_id);
3589
3590 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3591 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003592
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003593 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003594 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3595 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3596 tpacpi_inputdev->keycode = hotkey_keycode_map;
3597 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3598 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003599 input_set_capability(tpacpi_inputdev, EV_KEY,
3600 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003601 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003602 if (i < sizeof(hotkey_reserved_mask)*8)
3603 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003604 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003605 }
3606
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003607 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003608 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003609 input_report_switch(tpacpi_inputdev,
3610 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003611 }
3612 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003613 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003614 input_report_switch(tpacpi_inputdev,
3615 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003616 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003617
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003618 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003619 * userspace. tpacpi_detect_brightness_capabilities() must have
3620 * been called before this point */
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02003621 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Joe Perches0978e012011-04-04 10:06:25 -07003622 pr_info("This ThinkPad has standard ACPI backlight "
3623 "brightness control, supported by the ACPI "
3624 "video driver\n");
3625 pr_notice("Disabling thinkpad-acpi brightness events "
3626 "by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003627
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003628 /* Disable brightness up/down on Lenovo thinkpads when
3629 * ACPI is handling them, otherwise it is plain impossible
3630 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003631 hotkey_reserved_mask |=
3632 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3633 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003634 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3635 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003636 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003637
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003638#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003639 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3640 & ~hotkey_all_mask
3641 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003642
3643 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3644 "hotkey source mask 0x%08x, polling freq %u\n",
3645 hotkey_source_mask, hotkey_poll_freq);
3646#endif
3647
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003648 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3649 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003650 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003651 if (res) {
3652 hotkey_exit();
3653 return res;
3654 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003655 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3656 | hotkey_driver_mask)
3657 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003658 if (res < 0 && res != -ENXIO) {
3659 hotkey_exit();
3660 return res;
3661 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003662 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3663 & ~hotkey_reserved_mask;
3664 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3665 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3666 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003667
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003668 tpacpi_inputdev->open = &hotkey_inputdev_open;
3669 tpacpi_inputdev->close = &hotkey_inputdev_close;
3670
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003671 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003672
3673 return 0;
3674
3675err_exit:
3676 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
Bastien Nocerab790cee2015-03-02 14:45:27 +01003677 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3678 &adaptive_kbd_attr_group);
3679
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003680 hotkey_dev_attributes = NULL;
3681
Jan van den Berg72a979f2014-09-17 00:01:08 +02003682 return (res < 0) ? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003683}
3684
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003685/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3686 * mode, Web conference mode, Function mode and Lay-flat mode.
3687 * We support Home mode and Function mode currently.
3688 *
3689 * Will consider support rest of modes in future.
3690 *
3691 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003692static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003693 HOME_MODE,
3694/* WEB_BROWSER_MODE = 2,
3695 WEB_CONFERENCE_MODE = 3, */
3696 FUNCTION_MODE
3697};
3698
3699#define DFR_CHANGE_ROW 0x101
3700#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003701#define FIRST_ADAPTIVE_KEY 0x103
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003702
3703/* press Fn key a while second, it will switch to Function Mode. Then
3704 * release Fn key, previous mode be restored.
3705 */
3706static bool adaptive_keyboard_mode_is_saved;
3707static int adaptive_keyboard_prev_mode;
3708
Bastien Noceraf74587f2015-03-02 14:45:22 +01003709static int adaptive_keyboard_get_mode(void)
3710{
3711 int mode = 0;
3712
3713 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3714 pr_err("Cannot read adaptive keyboard mode\n");
3715 return -EIO;
3716 }
3717
3718 return mode;
3719}
3720
3721static int adaptive_keyboard_set_mode(int new_mode)
3722{
3723 if (new_mode < 0 ||
3724 new_mode > LAYFLAT_MODE)
3725 return -EINVAL;
3726
3727 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3728 pr_err("Cannot set adaptive keyboard mode\n");
3729 return -EIO;
3730 }
3731
3732 return 0;
3733}
3734
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003735static int adaptive_keyboard_get_next_mode(int mode)
3736{
3737 size_t i;
3738 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3739
3740 for (i = 0; i <= max_mode; i++) {
3741 if (adaptive_keyboard_modes[i] == mode)
3742 break;
3743 }
3744
3745 if (i >= max_mode)
3746 i = 0;
3747 else
3748 i++;
3749
3750 return adaptive_keyboard_modes[i];
3751}
3752
3753static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3754{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003755 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003756 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003757 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003758
3759 switch (scancode) {
3760 case DFR_CHANGE_ROW:
3761 if (adaptive_keyboard_mode_is_saved) {
3762 new_mode = adaptive_keyboard_prev_mode;
3763 adaptive_keyboard_mode_is_saved = false;
3764 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003765 current_mode = adaptive_keyboard_get_mode();
3766 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003767 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003768 new_mode = adaptive_keyboard_get_next_mode(
3769 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003770 }
3771
Bastien Noceraf74587f2015-03-02 14:45:22 +01003772 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003773 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003774
3775 return true;
3776
3777 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003778 current_mode = adaptive_keyboard_get_mode();
3779 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003780 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003781
Bastien Noceraf74587f2015-03-02 14:45:22 +01003782 adaptive_keyboard_prev_mode = current_mode;
3783 adaptive_keyboard_mode_is_saved = true;
3784
3785 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3786 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003787 return true;
3788
3789 default:
Dan Carpenter741d98c2015-03-11 12:36:07 +03003790 if (scancode < FIRST_ADAPTIVE_KEY ||
Christian Kellner696c65232017-02-28 17:10:57 +01003791 scancode >= FIRST_ADAPTIVE_KEY +
3792 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3793 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
Bastien Nocera6a68d852015-03-02 14:45:31 +01003794 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3795 scancode);
3796 return false;
3797 }
Christian Kellner149c8c72017-02-28 17:10:56 +01003798 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3799 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
Bastien Nocera6a68d852015-03-02 14:45:31 +01003800 if (keycode != KEY_RESERVED) {
3801 mutex_lock(&tpacpi_inputdev_send_mutex);
3802
3803 input_report_key(tpacpi_inputdev, keycode, 1);
3804 input_sync(tpacpi_inputdev);
3805
3806 input_report_key(tpacpi_inputdev, keycode, 0);
3807 input_sync(tpacpi_inputdev);
3808
3809 mutex_unlock(&tpacpi_inputdev_send_mutex);
3810 }
3811 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003812 }
3813}
3814
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003815static bool hotkey_notify_hotkey(const u32 hkey,
3816 bool *send_acpi_ev,
3817 bool *ignore_acpi_ev)
3818{
3819 /* 0x1000-0x1FFF: key presses */
3820 unsigned int scancode = hkey & 0xfff;
3821 *send_acpi_ev = true;
3822 *ignore_acpi_ev = false;
3823
Christian Kellner696c65232017-02-28 17:10:57 +01003824 /*
3825 * Original events are in the 0x10XX range, the adaptive keyboard
3826 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3827 * models, additional keys are emitted through 0x13XX.
3828 */
3829 switch ((hkey >> 8) & 0xf) {
3830 case 0:
3831 if (scancode > 0 &&
3832 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3833 /* HKEY event 0x1001 is scancode 0x00 */
3834 scancode--;
3835 if (!(hotkey_source_mask & (1 << scancode))) {
3836 tpacpi_input_send_key_masked(scancode);
3837 *send_acpi_ev = false;
3838 } else {
3839 *ignore_acpi_ev = true;
3840 }
3841 return true;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003842 }
Christian Kellner696c65232017-02-28 17:10:57 +01003843 break;
3844
3845 case 1:
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003846 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Christian Kellner696c65232017-02-28 17:10:57 +01003847
3848 case 3:
3849 /* Extended keycodes start at 0x300 and our offset into the map
3850 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3851 * will be positive, but might not be in the correct range.
3852 */
3853 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3854 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3855 scancode < TPACPI_HOTKEY_MAP_LEN) {
3856 tpacpi_input_send_key(scancode);
3857 return true;
3858 }
3859 break;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003860 }
Christian Kellner696c65232017-02-28 17:10:57 +01003861
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003862 return false;
3863}
3864
3865static bool hotkey_notify_wakeup(const u32 hkey,
3866 bool *send_acpi_ev,
3867 bool *ignore_acpi_ev)
3868{
3869 /* 0x2000-0x2FFF: Wakeup reason */
3870 *send_acpi_ev = true;
3871 *ignore_acpi_ev = false;
3872
3873 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003874 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3875 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003876 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3877 *ignore_acpi_ev = true;
3878 break;
3879
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003880 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3881 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003882 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3883 *ignore_acpi_ev = true;
3884 break;
3885
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003886 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3887 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003888 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003889 /* how to auto-heal: */
3890 /* 2313: woke up from S3, go to S4/S5 */
3891 /* 2413: woke up from S4, go to S5 */
3892 break;
3893
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003894 default:
3895 return false;
3896 }
3897
3898 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003899 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003900 hotkey_wakeup_reason_notify_change();
3901 }
3902 return true;
3903}
3904
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003905static bool hotkey_notify_dockevent(const u32 hkey,
3906 bool *send_acpi_ev,
3907 bool *ignore_acpi_ev)
3908{
3909 /* 0x4000-0x4FFF: dock-related events */
3910 *send_acpi_ev = true;
3911 *ignore_acpi_ev = false;
3912
3913 switch (hkey) {
3914 case TP_HKEY_EV_UNDOCK_ACK:
3915 /* ACPI undock operation completed after wakeup */
3916 hotkey_autosleep_ack = 1;
3917 pr_info("undocked\n");
3918 hotkey_wakeup_hotunplug_complete_notify_change();
3919 return true;
3920
3921 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3922 pr_info("docked into hotplug port replicator\n");
3923 return true;
3924 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3925 pr_info("undocked from hotplug port replicator\n");
3926 return true;
3927
3928 default:
3929 return false;
3930 }
3931}
3932
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003933static bool hotkey_notify_usrevent(const u32 hkey,
3934 bool *send_acpi_ev,
3935 bool *ignore_acpi_ev)
3936{
3937 /* 0x5000-0x5FFF: human interface helpers */
3938 *send_acpi_ev = true;
3939 *ignore_acpi_ev = false;
3940
3941 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003942 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3943 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003944 return true;
3945
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003946 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3947 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003948 tpacpi_input_send_tabletsw();
3949 hotkey_tablet_mode_notify_change();
3950 *send_acpi_ev = false;
3951 return true;
3952
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003953 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3954 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3955 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003956 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003957 *ignore_acpi_ev = true;
3958 return true;
3959
3960 default:
3961 return false;
3962 }
3963}
3964
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003965static void thermal_dump_all_sensors(void);
3966
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003967static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003968 bool *send_acpi_ev,
3969 bool *ignore_acpi_ev)
3970{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003971 bool known = true;
3972
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003973 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003974 *send_acpi_ev = true;
3975 *ignore_acpi_ev = false;
3976
3977 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003978 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003979 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003980 /* recommended action: do nothing, we don't have
3981 * Lenovo ATM information */
3982 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003983 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003984 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003985 /* recommended action: warn user through gui */
3986 break;
3987 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003988 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003989 /* recommended action: immediate sleep/hibernate */
3990 break;
3991 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003992 pr_crit("THERMAL ALARM: "
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003993 "a sensor reports something is too hot!\n");
3994 /* recommended action: warn user through gui, that */
3995 /* some internal component is too hot */
3996 break;
3997 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003998 pr_alert("THERMAL EMERGENCY: "
3999 "a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004000 /* recommended action: immediate sleep/hibernate */
4001 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01004002 case TP_HKEY_EV_AC_CHANGED:
4003 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4004 * AC status changed; can be triggered by plugging or
4005 * unplugging AC adapter, docking or undocking. */
4006
4007 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004008
4009 case TP_HKEY_EV_KEY_NUMLOCK:
4010 case TP_HKEY_EV_KEY_FN:
Xavier Naveira67ab6242015-02-10 08:45:18 +01004011 case TP_HKEY_EV_KEY_FN_ESC:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004012 /* key press events, we just ignore them as long as the EC
4013 * is still reporting them in the normal keyboard stream */
4014 *send_acpi_ev = false;
4015 *ignore_acpi_ev = true;
4016 return true;
4017
Lyudeb03f4d42016-11-11 15:15:03 -05004018 case TP_HKEY_EV_TABLET_CHANGED:
4019 tpacpi_input_send_tabletsw();
4020 hotkey_tablet_mode_notify_change();
4021 *send_acpi_ev = false;
4022 break;
4023
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004024 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004025 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004026 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004027 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00004028
4029 thermal_dump_all_sensors();
4030
4031 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004032}
4033
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004034static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4035{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004036 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004037 bool send_acpi_ev;
4038 bool ignore_acpi_ev;
4039 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004040
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004041 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07004042 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004043 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004044 acpi_bus_generate_netlink_event(
4045 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004046 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004047 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004048 return;
4049 }
4050
4051 while (1) {
4052 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07004053 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004054 return;
4055 }
4056
4057 if (hkey == 0) {
4058 /* queue empty */
4059 return;
4060 }
4061
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004062 send_acpi_ev = true;
4063 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004064
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004065 switch (hkey >> 12) {
4066 case 1:
4067 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004068 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4069 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004070 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004071 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004072 /* 0x2000-0x2FFF: Wakeup reason */
4073 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4074 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004075 break;
4076 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004077 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004078 switch (hkey) {
4079 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004080 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004081 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004082 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004083 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004084 break;
4085 case TP_HKEY_EV_OPTDRV_EJ:
4086 /* FIXME: kick libata if SATA link offline */
4087 known_ev = true;
4088 break;
4089 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004090 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004091 }
4092 break;
4093 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03004094 /* 0x4000-0x4FFF: dock-related events */
4095 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4096 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004097 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004098 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02004099 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004100 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4101 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004102 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004103 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004104 /* 0x6000-0x6FFF: thermal alarms/notices and
4105 * keyboard events */
4106 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004107 &ignore_acpi_ev);
4108 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004109 case 7:
4110 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004111 if (tp_features.hotkey_wlsw &&
4112 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004113 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004114 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004115 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004116 break;
4117 }
4118 /* fallthrough to default */
4119 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004120 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004121 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004122 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07004123 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4124 pr_notice("please report the conditions when this "
4125 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004126 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004127
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004128 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03004129 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004130 acpi_bus_generate_netlink_event(
4131 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004132 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004133 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004134 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004135 }
4136}
4137
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02004138static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004139{
4140 /* Do these on suspend, we get the events on early resume! */
4141 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4142 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08004143
4144 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004145 if (tp_features.has_adaptive_kbd) {
4146 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4147 "GTRW", "dd", 0)) {
4148 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004149 }
4150 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004151}
4152
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004153static void hotkey_resume(void)
4154{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03004155 tpacpi_disable_brightness_delay();
4156
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004157 if (hotkey_status_set(true) < 0 ||
4158 hotkey_mask_set(hotkey_acpi_mask) < 0)
Joe Perches0978e012011-04-04 10:06:25 -07004159 pr_err("error while attempting to reset the event "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004160 "firmware interface\n");
4161
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004162 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02004163 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004164 hotkey_wakeup_reason_notify_change();
4165 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004166 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004167
4168 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004169 if (tp_features.has_adaptive_kbd) {
4170 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4171 adaptive_keyboard_prev_mode)) {
4172 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004173 }
4174 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004175}
4176
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004177/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004178static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004180 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004182 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004183 seq_printf(m, "status:\t\tnot supported\n");
4184 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004185 }
4186
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004187 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004188 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004189 res = hotkey_status_get(&status);
4190 if (!res)
4191 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004192 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004193 if (res)
4194 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004196 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004197 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004198 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4199 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004201 seq_printf(m, "mask:\t\tnot supported\n");
4202 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 }
4204
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206}
4207
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004208static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004209{
4210 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004211 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Joe Perches0978e012011-04-04 10:06:25 -07004212 pr_fmt("hotkey enable/disable functionality has been "
4213 "removed from the driver. "
4214 "Hotkeys are always enabled.\n")))
4215 pr_err("Please remove the hotkey=enable module "
4216 "parameter, it is deprecated. "
4217 "Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004218}
4219
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004220static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004222 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004223 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004226 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 return -ENODEV;
4228
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004229 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004230 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004231
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004232 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004233
4234 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 while ((cmd = next_cmd(&buf))) {
4236 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004237 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004239 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004240 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03004242 mask = (hotkey_all_mask | hotkey_source_mask)
4243 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4245 /* mask set */
4246 } else if (sscanf(cmd, "%x", &mask) == 1) {
4247 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004248 } else {
4249 res = -EINVAL;
4250 goto errexit;
4251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004253
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004254 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004255 tpacpi_disclose_usertask("procfs hotkey",
4256 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004257 res = hotkey_user_mask_set(mask);
4258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004260errexit:
4261 mutex_unlock(&hotkey_mutex);
4262 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004263}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004265static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004266 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4267 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Hui Wanga3c42a42016-11-08 16:13:23 +08004268 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004269 {"", 0},
4270};
4271
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004272static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004273 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004274 .notify = hotkey_notify,
4275 .handle = &hkey_handle,
4276 .type = ACPI_DEVICE_NOTIFY,
4277};
4278
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004279static struct ibm_struct hotkey_driver_data = {
4280 .name = "hotkey",
4281 .read = hotkey_read,
4282 .write = hotkey_write,
4283 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004284 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004285 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004286 .acpi = &ibm_hotkey_acpidriver,
4287};
4288
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004289/*************************************************************************
4290 * Bluetooth subdriver
4291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004293enum {
4294 /* ACPI GBDC/SBDC bits */
4295 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4296 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004297 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004298 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004299};
4300
4301enum {
4302 /* ACPI \BLTH commands */
4303 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4304 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4305 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4306 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4307 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004308};
4309
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004310#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4311
Johannes Berg19d337d2009-06-02 13:01:37 +02004312static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004313{
4314 int status;
4315
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004316#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4317 if (dbg_bluetoothemul)
4318 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004319 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004320#endif
4321
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004322 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4323 return -EIO;
4324
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004325 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004326 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004327}
4328
Johannes Berg19d337d2009-06-02 13:01:37 +02004329static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004330{
4331 int status;
4332
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004333 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004334 "will attempt to %s bluetooth\n",
4335 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004336
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004337#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4338 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004339 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004340 return 0;
4341 }
4342#endif
4343
Johannes Berg19d337d2009-06-02 13:01:37 +02004344 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004345 status = TP_ACPI_BLUETOOTH_RADIOSSW
4346 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4347 else
4348 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004349
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004350 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4351 return -EIO;
4352
4353 return 0;
4354}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004355
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004356/* sysfs bluetooth enable ---------------------------------------------- */
4357static ssize_t bluetooth_enable_show(struct device *dev,
4358 struct device_attribute *attr,
4359 char *buf)
4360{
Johannes Berg19d337d2009-06-02 13:01:37 +02004361 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4362 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004363}
4364
4365static ssize_t bluetooth_enable_store(struct device *dev,
4366 struct device_attribute *attr,
4367 const char *buf, size_t count)
4368{
Johannes Berg19d337d2009-06-02 13:01:37 +02004369 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4370 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004371}
4372
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004373static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004374
4375/* --------------------------------------------------------------------- */
4376
4377static struct attribute *bluetooth_attributes[] = {
4378 &dev_attr_bluetooth_enable.attr,
4379 NULL
4380};
4381
4382static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004383 .attrs = bluetooth_attributes,
4384};
4385
Johannes Berg19d337d2009-06-02 13:01:37 +02004386static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4387 .get_status = bluetooth_get_status,
4388 .set_status = bluetooth_set_status,
4389};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004390
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004391static void bluetooth_shutdown(void)
4392{
4393 /* Order firmware to save current state to NVRAM */
4394 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4395 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004396 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004397 else
4398 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004399 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004400}
4401
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004402static void bluetooth_exit(void)
4403{
4404 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4405 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004406
4407 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4408
4409 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004410}
4411
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004412static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004414 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004415 int status = 0;
4416
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004417 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4418 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004419
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004420 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004421
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004422 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4423 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004424 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004425 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004427 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4428 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004429 str_supported(tp_features.bluetooth),
4430 status);
4431
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004432#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4433 if (dbg_bluetoothemul) {
4434 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004435 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004436 } else
4437#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004438 if (tp_features.bluetooth &&
4439 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4440 /* no bluetooth hardware present in system */
4441 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004442 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004443 "bluetooth hardware not installed\n");
4444 }
4445
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004446 if (!tp_features.bluetooth)
4447 return 1;
4448
Johannes Berg19d337d2009-06-02 13:01:37 +02004449 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4450 &bluetooth_tprfk_ops,
4451 RFKILL_TYPE_BLUETOOTH,
4452 TPACPI_RFK_BLUETOOTH_SW_NAME,
4453 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004454 if (res)
4455 return res;
4456
Johannes Berg19d337d2009-06-02 13:01:37 +02004457 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4458 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004459 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004460 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004461 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004462 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004463
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465}
4466
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004467/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004468static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004470 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471}
4472
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004473static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474{
Johannes Berg19d337d2009-06-02 13:01:37 +02004475 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476}
4477
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004478static struct ibm_struct bluetooth_driver_data = {
4479 .name = "bluetooth",
4480 .read = bluetooth_read,
4481 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004482 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004483 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004484};
4485
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004486/*************************************************************************
4487 * Wan subdriver
4488 */
4489
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004490enum {
4491 /* ACPI GWAN/SWAN bits */
4492 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4493 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004494 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004495 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004496};
4497
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004498#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4499
Johannes Berg19d337d2009-06-02 13:01:37 +02004500static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004501{
4502 int status;
4503
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004504#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4505 if (dbg_wwanemul)
4506 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004507 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004508#endif
4509
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004510 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4511 return -EIO;
4512
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004513 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004514 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004515}
4516
Johannes Berg19d337d2009-06-02 13:01:37 +02004517static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004518{
4519 int status;
4520
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004521 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004522 "will attempt to %s wwan\n",
4523 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004524
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004525#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4526 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004527 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004528 return 0;
4529 }
4530#endif
4531
Johannes Berg19d337d2009-06-02 13:01:37 +02004532 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004533 status = TP_ACPI_WANCARD_RADIOSSW
4534 | TP_ACPI_WANCARD_RESUMECTRL;
4535 else
4536 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004537
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004538 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4539 return -EIO;
4540
4541 return 0;
4542}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004543
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004544/* sysfs wan enable ---------------------------------------------------- */
4545static ssize_t wan_enable_show(struct device *dev,
4546 struct device_attribute *attr,
4547 char *buf)
4548{
Johannes Berg19d337d2009-06-02 13:01:37 +02004549 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4550 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004551}
4552
4553static ssize_t wan_enable_store(struct device *dev,
4554 struct device_attribute *attr,
4555 const char *buf, size_t count)
4556{
Johannes Berg19d337d2009-06-02 13:01:37 +02004557 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4558 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004559}
4560
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004561static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4562 wan_enable_show, wan_enable_store);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004563
4564/* --------------------------------------------------------------------- */
4565
4566static struct attribute *wan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004567 &dev_attr_wwan_enable.attr,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004568 NULL
4569};
4570
4571static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004572 .attrs = wan_attributes,
4573};
4574
Johannes Berg19d337d2009-06-02 13:01:37 +02004575static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4576 .get_status = wan_get_status,
4577 .set_status = wan_set_status,
4578};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004579
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004580static void wan_shutdown(void)
4581{
4582 /* Order firmware to save current state to NVRAM */
4583 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4584 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004585 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004586 else
4587 vdbg_printk(TPACPI_DBG_RFKILL,
4588 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004589}
4590
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004591static void wan_exit(void)
4592{
4593 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4594 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004595
4596 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4597
4598 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004599}
4600
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004601static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004602{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004603 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004604 int status = 0;
4605
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004606 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4607 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004608
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004609 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004610
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004611 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004612 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004613
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004614 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4615 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004616 str_supported(tp_features.wan),
4617 status);
4618
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004619#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4620 if (dbg_wwanemul) {
4621 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004622 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004623 } else
4624#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004625 if (tp_features.wan &&
4626 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4627 /* no wan hardware present in system */
4628 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004629 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004630 "wan hardware not installed\n");
4631 }
4632
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004633 if (!tp_features.wan)
4634 return 1;
4635
Johannes Berg19d337d2009-06-02 13:01:37 +02004636 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4637 &wan_tprfk_ops,
4638 RFKILL_TYPE_WWAN,
4639 TPACPI_RFK_WWAN_SW_NAME,
4640 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004641 if (res)
4642 return res;
4643
Johannes Berg19d337d2009-06-02 13:01:37 +02004644 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4645 &wan_attr_group);
4646
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004647 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004648 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004649 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004650 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004651
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004652 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004653}
4654
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004655/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004656static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004657{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004658 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004659}
4660
4661static int wan_write(char *buf)
4662{
Johannes Berg19d337d2009-06-02 13:01:37 +02004663 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004664}
4665
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004666static struct ibm_struct wan_driver_data = {
4667 .name = "wan",
4668 .read = wan_read,
4669 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004670 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004671 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004672};
4673
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004674/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004675 * UWB subdriver
4676 */
4677
4678enum {
4679 /* ACPI GUWB/SUWB bits */
4680 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4681 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4682};
4683
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004684#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4685
Johannes Berg19d337d2009-06-02 13:01:37 +02004686static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004687{
4688 int status;
4689
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004690#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4691 if (dbg_uwbemul)
4692 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004693 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004694#endif
4695
4696 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4697 return -EIO;
4698
4699 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004700 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004701}
4702
Johannes Berg19d337d2009-06-02 13:01:37 +02004703static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004704{
4705 int status;
4706
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004707 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004708 "will attempt to %s UWB\n",
4709 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004710
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004711#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4712 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004713 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004714 return 0;
4715 }
4716#endif
4717
Johannes Berg19d337d2009-06-02 13:01:37 +02004718 if (state == TPACPI_RFK_RADIO_ON)
4719 status = TP_ACPI_UWB_RADIOSSW;
4720 else
4721 status = 0;
4722
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004723 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4724 return -EIO;
4725
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004726 return 0;
4727}
4728
4729/* --------------------------------------------------------------------- */
4730
Johannes Berg19d337d2009-06-02 13:01:37 +02004731static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4732 .get_status = uwb_get_status,
4733 .set_status = uwb_set_status,
4734};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004735
4736static void uwb_exit(void)
4737{
Johannes Berg19d337d2009-06-02 13:01:37 +02004738 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004739}
4740
4741static int __init uwb_init(struct ibm_init_struct *iibm)
4742{
4743 int res;
4744 int status = 0;
4745
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004746 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4747 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004748
4749 TPACPI_ACPIHANDLE_INIT(hkey);
4750
4751 tp_features.uwb = hkey_handle &&
4752 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4753
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004754 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4755 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004756 str_supported(tp_features.uwb),
4757 status);
4758
4759#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4760 if (dbg_uwbemul) {
4761 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004762 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004763 } else
4764#endif
4765 if (tp_features.uwb &&
4766 !(status & TP_ACPI_UWB_HWPRESENT)) {
4767 /* no uwb hardware present in system */
4768 tp_features.uwb = 0;
4769 dbg_printk(TPACPI_DBG_INIT,
4770 "uwb hardware not installed\n");
4771 }
4772
4773 if (!tp_features.uwb)
4774 return 1;
4775
4776 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004777 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004778 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004779 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004780 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004781 return res;
4782}
4783
4784static struct ibm_struct uwb_driver_data = {
4785 .name = "uwb",
4786 .exit = uwb_exit,
4787 .flags.experimental = 1,
4788};
4789
4790/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004791 * Video subdriver
4792 */
4793
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004794#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4795
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004796enum video_access_mode {
4797 TPACPI_VIDEO_NONE = 0,
4798 TPACPI_VIDEO_570, /* 570 */
4799 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4800 TPACPI_VIDEO_NEW, /* all others */
4801};
4802
4803enum { /* video status flags, based on VIDEO_570 */
4804 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4805 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4806 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4807};
4808
4809enum { /* TPACPI_VIDEO_570 constants */
4810 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4811 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4812 * video_status_flags */
4813 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4814 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4815};
4816
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004817static enum video_access_mode video_supported;
4818static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004819
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004820static int video_autosw_get(void);
4821static int video_autosw_set(int enable);
4822
Joe Perches112a6ee2011-04-04 10:06:24 -07004823TPACPI_HANDLE(vid, root,
4824 "\\_SB.PCI.AGP.VGA", /* 570 */
4825 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4826 "\\_SB.PCI0.VID0", /* 770e */
4827 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4828 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4829 "\\_SB.PCI0.AGP.VID", /* all others */
4830 ); /* R30, R31 */
4831
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004832TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004833
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004834static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004836 int ivga;
4837
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004838 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4839
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004840 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004841 if (tpacpi_is_ibm())
4842 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004843
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004844 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4845 /* G41, assume IVGA doesn't change */
4846 vid_handle = vid2_handle;
4847
4848 if (!vid_handle)
4849 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004850 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004851 else if (tpacpi_is_ibm() &&
4852 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004853 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004854 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004855 else if (tpacpi_is_ibm() &&
4856 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004857 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004858 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004859 else
4860 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004861 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004863 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4864 str_supported(video_supported != TPACPI_VIDEO_NONE),
4865 video_supported);
4866
Jan van den Berg72a979f2014-09-17 00:01:08 +02004867 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868}
4869
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004870static void video_exit(void)
4871{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004872 dbg_printk(TPACPI_DBG_EXIT,
4873 "restoring original video autoswitch mode\n");
4874 if (video_autosw_set(video_orig_autosw))
Joe Perches0978e012011-04-04 10:06:25 -07004875 pr_err("error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004876 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004877}
4878
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004879static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880{
4881 int status = 0;
4882 int i;
4883
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004884 switch (video_supported) {
4885 case TPACPI_VIDEO_570:
4886 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4887 TP_ACPI_VIDEO_570_PHSCMD))
4888 return -EIO;
4889 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4890 break;
4891 case TPACPI_VIDEO_770:
4892 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4893 return -EIO;
4894 if (i)
4895 status |= TP_ACPI_VIDEO_S_LCD;
4896 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4897 return -EIO;
4898 if (i)
4899 status |= TP_ACPI_VIDEO_S_CRT;
4900 break;
4901 case TPACPI_VIDEO_NEW:
4902 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4903 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4904 return -EIO;
4905 if (i)
4906 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004908 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4909 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4910 return -EIO;
4911 if (i)
4912 status |= TP_ACPI_VIDEO_S_LCD;
4913 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4914 return -EIO;
4915 if (i)
4916 status |= TP_ACPI_VIDEO_S_DVI;
4917 break;
4918 default:
4919 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
4922 return status;
4923}
4924
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004925static int video_outputsw_set(int status)
4926{
4927 int autosw;
4928 int res = 0;
4929
4930 switch (video_supported) {
4931 case TPACPI_VIDEO_570:
4932 res = acpi_evalf(NULL, NULL,
4933 "\\_SB.PHS2", "vdd",
4934 TP_ACPI_VIDEO_570_PHS2CMD,
4935 status | TP_ACPI_VIDEO_570_PHS2SET);
4936 break;
4937 case TPACPI_VIDEO_770:
4938 autosw = video_autosw_get();
4939 if (autosw < 0)
4940 return autosw;
4941
4942 res = video_autosw_set(1);
4943 if (res)
4944 return res;
4945 res = acpi_evalf(vid_handle, NULL,
4946 "ASWT", "vdd", status * 0x100, 0);
4947 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004948 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004949 return -EIO;
4950 }
4951 break;
4952 case TPACPI_VIDEO_NEW:
4953 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004954 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004955 break;
4956 default:
4957 return -ENOSYS;
4958 }
4959
Jan van den Berg72a979f2014-09-17 00:01:08 +02004960 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004961}
4962
4963static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004965 int autosw = 0;
4966
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004967 switch (video_supported) {
4968 case TPACPI_VIDEO_570:
4969 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4970 return -EIO;
4971 break;
4972 case TPACPI_VIDEO_770:
4973 case TPACPI_VIDEO_NEW:
4974 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4975 return -EIO;
4976 break;
4977 default:
4978 return -ENOSYS;
4979 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004980
4981 return autosw & 1;
4982}
4983
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004984static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004985{
Jan van den Berg72a979f2014-09-17 00:01:08 +02004986 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004987 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004988 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004989}
4990
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004991static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004992{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004993 int autosw = video_autosw_get();
4994 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004995
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004996 if (autosw < 0)
4997 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004998
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004999 switch (video_supported) {
5000 case TPACPI_VIDEO_570:
5001 res = video_autosw_set(1);
5002 if (res)
5003 return res;
5004 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5005 break;
5006 case TPACPI_VIDEO_770:
5007 case TPACPI_VIDEO_NEW:
5008 res = video_autosw_set(1);
5009 if (res)
5010 return res;
5011 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5012 break;
5013 default:
5014 return -ENOSYS;
5015 }
5016 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07005017 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005018 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005019 }
5020
Jan van den Berg72a979f2014-09-17 00:01:08 +02005021 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005022}
5023
5024static int video_expand_toggle(void)
5025{
5026 switch (video_supported) {
5027 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005028 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005029 0 : -EIO;
5030 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005031 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005032 0 : -EIO;
5033 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02005034 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005035 0 : -EIO;
5036 default:
5037 return -ENOSYS;
5038 }
5039 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005040}
5041
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005042static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005043{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005044 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005045
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005046 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005047 seq_printf(m, "status:\t\tnot supported\n");
5048 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005049 }
5050
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005051 /* Even reads can crash X.org, so... */
5052 if (!capable(CAP_SYS_ADMIN))
5053 return -EPERM;
5054
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005055 status = video_outputsw_get();
5056 if (status < 0)
5057 return status;
5058
5059 autosw = video_autosw_get();
5060 if (autosw < 0)
5061 return autosw;
5062
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005063 seq_printf(m, "status:\t\tsupported\n");
5064 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5065 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005066 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005067 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5068 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5069 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5070 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005071 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005072 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5073 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5074 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005075
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005076 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005077}
5078
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005079static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080{
5081 char *cmd;
5082 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005083 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005085 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005086 return -ENODEV;
5087
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005088 /* Even reads can crash X.org, let alone writes... */
5089 if (!capable(CAP_SYS_ADMIN))
5090 return -EPERM;
5091
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005092 enable = 0;
5093 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094
5095 while ((cmd = next_cmd(&buf))) {
5096 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005097 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005099 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005101 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005103 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005104 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005105 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005106 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005107 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005108 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005109 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005111 res = video_autosw_set(1);
5112 if (res)
5113 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005115 res = video_autosw_set(0);
5116 if (res)
5117 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005119 res = video_outputsw_cycle();
5120 if (res)
5121 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005123 res = video_expand_toggle();
5124 if (res)
5125 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 } else
5127 return -EINVAL;
5128 }
5129
5130 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005131 status = video_outputsw_get();
5132 if (status < 0)
5133 return status;
5134 res = video_outputsw_set((status & ~disable) | enable);
5135 if (res)
5136 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 }
5138
5139 return 0;
5140}
5141
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005142static struct ibm_struct video_driver_data = {
5143 .name = "video",
5144 .read = video_read,
5145 .write = video_write,
5146 .exit = video_exit,
5147};
5148
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005149#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5150
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005151/*************************************************************************
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005152 * Keyboard backlight subdriver
5153 */
5154
Hans de Goedec685e202017-02-09 16:44:13 +01005155static enum led_brightness kbdlight_brightness;
5156static DEFINE_MUTEX(kbdlight_mutex);
5157
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005158static int kbdlight_set_level(int level)
5159{
Hans de Goedec685e202017-02-09 16:44:13 +01005160 int ret = 0;
5161
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005162 if (!hkey_handle)
5163 return -ENXIO;
5164
Hans de Goedec685e202017-02-09 16:44:13 +01005165 mutex_lock(&kbdlight_mutex);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005166
Hans de Goedec685e202017-02-09 16:44:13 +01005167 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5168 ret = -EIO;
5169 else
5170 kbdlight_brightness = level;
5171
5172 mutex_unlock(&kbdlight_mutex);
5173
5174 return ret;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005175}
5176
5177static int kbdlight_get_level(void)
5178{
5179 int status = 0;
5180
5181 if (!hkey_handle)
5182 return -ENXIO;
5183
5184 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5185 return -EIO;
5186
5187 if (status < 0)
5188 return status;
5189
5190 return status & 0x3;
5191}
5192
5193static bool kbdlight_is_supported(void)
5194{
5195 int status = 0;
5196
5197 if (!hkey_handle)
5198 return false;
5199
5200 if (!acpi_has_method(hkey_handle, "MLCG")) {
5201 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5202 return false;
5203 }
5204
5205 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5206 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5207 return false;
5208 }
5209
5210 if (status < 0) {
5211 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5212 return false;
5213 }
5214
5215 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5216 /*
5217 * Guessed test for keyboard backlight:
5218 *
5219 * Machines with backlight keyboard return:
5220 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5221 * b110100010010000000XX - ThinkPad x230
5222 * b010100000010000000XX - ThinkPad x240
5223 * b010100000010000000XX - ThinkPad W541
5224 * (XX is current backlight level)
5225 *
5226 * Machines without backlight keyboard return:
5227 * b10100001000000000000 - ThinkPad x230
5228 * b10110001000000000000 - ThinkPad E430
5229 * b00000000000000000000 - ThinkPad E450
5230 *
5231 * Candidate BITs for detection test (XOR):
5232 * b01000000001000000000
5233 * ^
5234 */
5235 return status & BIT(9);
5236}
5237
Hans de Goede86ec0c22017-02-09 16:44:12 +01005238static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005239 enum led_brightness brightness)
5240{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005241 return kbdlight_set_level(brightness);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005242}
5243
5244static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5245{
5246 int level;
5247
5248 level = kbdlight_get_level();
5249 if (level < 0)
5250 return 0;
5251
5252 return level;
5253}
5254
5255static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5256 .led_classdev = {
5257 .name = "tpacpi::kbd_backlight",
5258 .max_brightness = 2,
Hans de Goedec685e202017-02-09 16:44:13 +01005259 .flags = LED_BRIGHT_HW_CHANGED,
Hans de Goede86ec0c22017-02-09 16:44:12 +01005260 .brightness_set_blocking = &kbdlight_sysfs_set,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005261 .brightness_get = &kbdlight_sysfs_get,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005262 }
5263};
5264
5265static int __init kbdlight_init(struct ibm_init_struct *iibm)
5266{
5267 int rc;
5268
5269 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5270
5271 TPACPI_ACPIHANDLE_INIT(hkey);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005272
5273 if (!kbdlight_is_supported()) {
5274 tp_features.kbdlight = 0;
5275 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5276 return 1;
5277 }
5278
Hans de Goedec685e202017-02-09 16:44:13 +01005279 kbdlight_brightness = kbdlight_sysfs_get(NULL);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005280 tp_features.kbdlight = 1;
5281
5282 rc = led_classdev_register(&tpacpi_pdev->dev,
5283 &tpacpi_led_kbdlight.led_classdev);
5284 if (rc < 0) {
5285 tp_features.kbdlight = 0;
5286 return rc;
5287 }
5288
Hans de Goedec685e202017-02-09 16:44:13 +01005289 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5290 TP_ACPI_HKEY_KBD_LIGHT_MASK);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005291 return 0;
5292}
5293
5294static void kbdlight_exit(void)
5295{
5296 if (tp_features.kbdlight)
5297 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005298}
5299
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005300static int kbdlight_set_level_and_update(int level)
5301{
5302 int ret;
5303 struct led_classdev *led_cdev;
5304
5305 ret = kbdlight_set_level(level);
5306 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5307
5308 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5309 led_cdev->brightness = level;
5310
5311 return ret;
5312}
5313
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005314static int kbdlight_read(struct seq_file *m)
5315{
5316 int level;
5317
5318 if (!tp_features.kbdlight) {
5319 seq_printf(m, "status:\t\tnot supported\n");
5320 } else {
5321 level = kbdlight_get_level();
5322 if (level < 0)
5323 seq_printf(m, "status:\t\terror %d\n", level);
5324 else
5325 seq_printf(m, "status:\t\t%d\n", level);
5326 seq_printf(m, "commands:\t0, 1, 2\n");
5327 }
5328
5329 return 0;
5330}
5331
5332static int kbdlight_write(char *buf)
5333{
5334 char *cmd;
5335 int level = -1;
5336
5337 if (!tp_features.kbdlight)
5338 return -ENODEV;
5339
5340 while ((cmd = next_cmd(&buf))) {
5341 if (strlencmp(cmd, "0") == 0)
5342 level = 0;
5343 else if (strlencmp(cmd, "1") == 0)
5344 level = 1;
5345 else if (strlencmp(cmd, "2") == 0)
5346 level = 2;
5347 else
5348 return -EINVAL;
5349 }
5350
5351 if (level == -1)
5352 return -EINVAL;
5353
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005354 return kbdlight_set_level_and_update(level);
5355}
5356
5357static void kbdlight_suspend(void)
5358{
5359 struct led_classdev *led_cdev;
5360
5361 if (!tp_features.kbdlight)
5362 return;
5363
5364 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5365 led_update_brightness(led_cdev);
5366 led_classdev_suspend(led_cdev);
5367}
5368
5369static void kbdlight_resume(void)
5370{
5371 if (!tp_features.kbdlight)
5372 return;
5373
5374 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005375}
5376
5377static struct ibm_struct kbdlight_driver_data = {
5378 .name = "kbdlight",
5379 .read = kbdlight_read,
5380 .write = kbdlight_write,
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005381 .suspend = kbdlight_suspend,
5382 .resume = kbdlight_resume,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005383 .exit = kbdlight_exit,
5384};
5385
5386/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005387 * Light (thinklight) subdriver
5388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005390TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5391TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005392
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005393static int light_get_status(void)
5394{
5395 int status = 0;
5396
5397 if (tp_features.light_status) {
5398 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5399 return -EIO;
5400 return (!!status);
5401 }
5402
5403 return -ENXIO;
5404}
5405
5406static int light_set_status(int status)
5407{
5408 int rc;
5409
5410 if (tp_features.light) {
5411 if (cmos_handle) {
5412 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005413 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005414 TP_CMOS_THINKLIGHT_ON :
5415 TP_CMOS_THINKLIGHT_OFF);
5416 } else {
5417 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005418 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005419 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005420 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005421 }
5422
5423 return -ENXIO;
5424}
5425
Hans de Goede86ec0c22017-02-09 16:44:12 +01005426static int light_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005427 enum led_brightness brightness)
5428{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005429 return light_set_status((brightness != LED_OFF) ?
5430 TPACPI_LED_ON : TPACPI_LED_OFF);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005431}
5432
5433static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5434{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005435 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005436}
5437
5438static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5439 .led_classdev = {
5440 .name = "tpacpi::thinklight",
Hans de Goede86ec0c22017-02-09 16:44:12 +01005441 .brightness_set_blocking = &light_sysfs_set,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005442 .brightness_get = &light_sysfs_get,
5443 }
5444};
5445
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005446static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005448 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005449
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005450 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5451
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005452 if (tpacpi_is_ibm()) {
5453 TPACPI_ACPIHANDLE_INIT(ledb);
5454 TPACPI_ACPIHANDLE_INIT(lght);
5455 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005456 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005457
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005458 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005459 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005460
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005461 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005462 /* light status not supported on
5463 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005464 tp_features.light_status =
5465 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005467 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5468 str_supported(tp_features.light),
5469 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005470
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005471 if (!tp_features.light)
5472 return 1;
5473
5474 rc = led_classdev_register(&tpacpi_pdev->dev,
5475 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005476
5477 if (rc < 0) {
5478 tp_features.light = 0;
5479 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005480 } else {
5481 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005482 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005483
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005484 return rc;
5485}
5486
5487static void light_exit(void)
5488{
5489 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490}
5491
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005492static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005494 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005496 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005497 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005498 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005499 seq_printf(m, "status:\t\tunknown\n");
5500 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005501 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005502 status = light_get_status();
5503 if (status < 0)
5504 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005505 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5506 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510}
5511
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005512static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005515 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005516
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005517 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005518 return -ENODEV;
5519
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 while ((cmd = next_cmd(&buf))) {
5521 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005522 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005524 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 } else
5526 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 }
5528
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005529 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530}
5531
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005532static struct ibm_struct light_driver_data = {
5533 .name = "light",
5534 .read = light_read,
5535 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005536 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005537};
5538
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005539/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005540 * CMOS subdriver
5541 */
5542
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005543/* sysfs cmos_command -------------------------------------------------- */
5544static ssize_t cmos_command_store(struct device *dev,
5545 struct device_attribute *attr,
5546 const char *buf, size_t count)
5547{
5548 unsigned long cmos_cmd;
5549 int res;
5550
5551 if (parse_strtoul(buf, 21, &cmos_cmd))
5552 return -EINVAL;
5553
5554 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005555 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005556}
5557
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01005558static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005559
5560/* --------------------------------------------------------------------- */
5561
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005562static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005563{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005564 int res;
5565
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005566 vdbg_printk(TPACPI_DBG_INIT,
5567 "initializing cmos commands subdriver\n");
5568
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005569 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005570
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005571 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5572 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005573
5574 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5575 if (res)
5576 return res;
5577
Jan van den Berg72a979f2014-09-17 00:01:08 +02005578 return (cmos_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005579}
5580
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005581static void cmos_exit(void)
5582{
5583 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5584}
5585
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005586static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005588 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5589 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005591 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005593 seq_printf(m, "status:\t\tsupported\n");
5594 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 }
5596
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598}
5599
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005600static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601{
5602 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005603 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
5605 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005606 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5607 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 /* cmos_cmd set */
5609 } else
5610 return -EINVAL;
5611
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005612 res = issue_thinkpad_cmos_command(cmos_cmd);
5613 if (res)
5614 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 }
5616
5617 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005618}
5619
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005620static struct ibm_struct cmos_driver_data = {
5621 .name = "cmos",
5622 .read = cmos_read,
5623 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005624 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005625};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005626
5627/*************************************************************************
5628 * LED subdriver
5629 */
5630
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005631enum led_access_mode {
5632 TPACPI_LED_NONE = 0,
5633 TPACPI_LED_570, /* 570 */
5634 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5635 TPACPI_LED_NEW, /* all others */
5636};
5637
5638enum { /* For TPACPI_LED_OLD */
5639 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5640 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5641 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5642};
5643
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005644static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005645
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005646static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005647
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005648#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005649static struct tpacpi_led_classdev *tpacpi_leds;
5650static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005651static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005652 /* there's a limit of 19 chars + NULL before 2.6.26 */
5653 "tpacpi::power",
5654 "tpacpi:orange:batt",
5655 "tpacpi:green:batt",
5656 "tpacpi::dock_active",
5657 "tpacpi::bay_active",
5658 "tpacpi::dock_batt",
5659 "tpacpi::unknown_led",
5660 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005661 "tpacpi::dock_status1",
5662 "tpacpi::dock_status2",
5663 "tpacpi::unknown_led2",
5664 "tpacpi::unknown_led3",
5665 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005666};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005667#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005668
5669static inline bool tpacpi_is_led_restricted(const unsigned int led)
5670{
5671#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5672 return false;
5673#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005674 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005675#endif
5676}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005677
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005678static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005679{
5680 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005681 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005682
5683 switch (led_supported) {
5684 case TPACPI_LED_570:
5685 if (!acpi_evalf(ec_handle,
5686 &status, "GLED", "dd", 1 << led))
5687 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005688 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005689 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005690 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005691 TPACPI_LED_ON :
5692 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005693 tpacpi_led_state_cache[led] = led_s;
5694 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005695 default:
5696 return -ENXIO;
5697 }
5698
5699 /* not reached */
5700}
5701
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005702static int led_set_status(const unsigned int led,
5703 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005704{
5705 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005706 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5707 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005708
5709 int rc = 0;
5710
5711 switch (led_supported) {
5712 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005713 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005714 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005715 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005716 if (unlikely(tpacpi_is_led_restricted(led)))
5717 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005718 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5719 (1 << led), led_sled_arg1[ledstatus]))
5720 rc = -EIO;
5721 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005722 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005723 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005724 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005725 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005726 if (unlikely(tpacpi_is_led_restricted(led)))
5727 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005728 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5729 if (rc >= 0)
5730 rc = ec_write(TPACPI_LED_EC_HLBL,
5731 (ledstatus == TPACPI_LED_BLINK) << led);
5732 if (rc >= 0)
5733 rc = ec_write(TPACPI_LED_EC_HLCL,
5734 (ledstatus != TPACPI_LED_OFF) << led);
5735 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005736 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005737 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005738 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5739 return -EINVAL;
5740 if (unlikely(tpacpi_is_led_restricted(led)))
5741 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005742 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5743 led, led_led_arg1[ledstatus]))
5744 rc = -EIO;
5745 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005746 default:
5747 rc = -ENXIO;
5748 }
5749
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005750 if (!rc)
5751 tpacpi_led_state_cache[led] = ledstatus;
5752
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005753 return rc;
5754}
5755
Hans de Goede86ec0c22017-02-09 16:44:12 +01005756static int led_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005757 enum led_brightness brightness)
5758{
5759 struct tpacpi_led_classdev *data = container_of(led_cdev,
5760 struct tpacpi_led_classdev, led_classdev);
Hans de Goede86ec0c22017-02-09 16:44:12 +01005761 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005762
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005763 if (brightness == LED_OFF)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005764 new_state = TPACPI_LED_OFF;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005765 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005766 new_state = TPACPI_LED_ON;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005767 else
Hans de Goede86ec0c22017-02-09 16:44:12 +01005768 new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005769
Hans de Goede86ec0c22017-02-09 16:44:12 +01005770 return led_set_status(data->led, new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005771}
5772
5773static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5774 unsigned long *delay_on, unsigned long *delay_off)
5775{
5776 struct tpacpi_led_classdev *data = container_of(led_cdev,
5777 struct tpacpi_led_classdev, led_classdev);
5778
5779 /* Can we choose the flash rate? */
5780 if (*delay_on == 0 && *delay_off == 0) {
5781 /* yes. set them to the hardware blink rate (1 Hz) */
5782 *delay_on = 500; /* ms */
5783 *delay_off = 500; /* ms */
5784 } else if ((*delay_on != 500) || (*delay_off != 500))
5785 return -EINVAL;
5786
Hans de Goede86ec0c22017-02-09 16:44:12 +01005787 return led_set_status(data->led, TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005788}
5789
5790static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5791{
5792 int rc;
5793
5794 struct tpacpi_led_classdev *data = container_of(led_cdev,
5795 struct tpacpi_led_classdev, led_classdev);
5796
5797 rc = led_get_status(data->led);
5798
5799 if (rc == TPACPI_LED_OFF || rc < 0)
5800 rc = LED_OFF; /* no error handling in led class :( */
5801 else
5802 rc = LED_FULL;
5803
5804 return rc;
5805}
5806
5807static void led_exit(void)
5808{
5809 unsigned int i;
5810
5811 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5812 if (tpacpi_leds[i].led_classdev.name)
5813 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5814 }
5815
5816 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005817}
5818
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005819static int __init tpacpi_init_led(unsigned int led)
5820{
5821 int rc;
5822
5823 tpacpi_leds[led].led = led;
5824
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005825 /* LEDs with no name don't get registered */
5826 if (!tpacpi_led_names[led])
5827 return 0;
5828
Hans de Goede86ec0c22017-02-09 16:44:12 +01005829 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005830 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5831 if (led_supported == TPACPI_LED_570)
5832 tpacpi_leds[led].led_classdev.brightness_get =
5833 &led_sysfs_get;
5834
5835 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5836
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005837 rc = led_classdev_register(&tpacpi_pdev->dev,
5838 &tpacpi_leds[led].led_classdev);
5839 if (rc < 0)
5840 tpacpi_leds[led].led_classdev.name = NULL;
5841
5842 return rc;
5843}
5844
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005845static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5846 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5847 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5848 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5849
5850 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5851 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5852 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5853 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5854 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5855 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5856 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5857 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5858
5859 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5860 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5861 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5862 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5863 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5864
5865 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5866 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5867 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5868 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5869
5870 /* (1) - may have excess leds enabled on MSB */
5871
5872 /* Defaults (order matters, keep last, don't reorder!) */
5873 { /* Lenovo */
5874 .vendor = PCI_VENDOR_ID_LENOVO,
5875 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5876 .quirks = 0x1fffU,
5877 },
5878 { /* IBM ThinkPads with no EC version string */
5879 .vendor = PCI_VENDOR_ID_IBM,
5880 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5881 .quirks = 0x00ffU,
5882 },
5883 { /* IBM ThinkPads with EC version string */
5884 .vendor = PCI_VENDOR_ID_IBM,
5885 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5886 .quirks = 0x00bfU,
5887 },
5888};
5889
5890#undef TPACPI_LEDQ_IBM
5891#undef TPACPI_LEDQ_LNV
5892
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005893static enum led_access_mode __init led_init_detect_mode(void)
5894{
5895 acpi_status status;
5896
5897 if (tpacpi_is_ibm()) {
5898 /* 570 */
5899 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5900 if (ACPI_SUCCESS(status))
5901 return TPACPI_LED_570;
5902
5903 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5904 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5905 if (ACPI_SUCCESS(status))
5906 return TPACPI_LED_OLD;
5907 }
5908
5909 /* most others */
5910 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5911 if (ACPI_SUCCESS(status))
5912 return TPACPI_LED_NEW;
5913
5914 /* R30, R31, and unknown firmwares */
5915 led_handle = NULL;
5916 return TPACPI_LED_NONE;
5917}
5918
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005919static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005920{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005921 unsigned int i;
5922 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005923 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005924
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005925 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5926
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005927 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005928
Adam Leefcb44e12013-06-07 16:20:08 +08005929 if (led_supported != TPACPI_LED_NONE) {
5930 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5931 ARRAY_SIZE(led_useful_qtable));
5932
5933 if (!useful_leds) {
5934 led_handle = NULL;
5935 led_supported = TPACPI_LED_NONE;
5936 }
5937 }
5938
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005939 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5940 str_supported(led_supported), led_supported);
5941
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005942 if (led_supported == TPACPI_LED_NONE)
5943 return 1;
5944
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005945 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5946 GFP_KERNEL);
5947 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005948 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005949 return -ENOMEM;
5950 }
5951
5952 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08005953 tpacpi_leds[i].led = -1;
5954
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005955 if (!tpacpi_is_led_restricted(i) &&
5956 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005957 rc = tpacpi_init_led(i);
5958 if (rc < 0) {
5959 led_exit();
5960 return rc;
5961 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005962 }
5963 }
5964
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005965#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Joe Perches0978e012011-04-04 10:06:25 -07005966 pr_notice("warning: userspace override of important "
5967 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005968#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005969 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005970}
5971
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005972#define str_led_status(s) \
5973 ((s) == TPACPI_LED_OFF ? "off" : \
5974 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005975
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005976static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005978 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005979 seq_printf(m, "status:\t\tnot supported\n");
5980 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005981 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005982 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005983
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005984 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005985 /* 570 */
5986 int i, status;
5987 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005988 status = led_get_status(i);
5989 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005990 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005991 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005992 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005993 }
5994 }
5995
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005996 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005997 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005999 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000}
6001
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006002static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003{
6004 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006005 int led, rc;
6006 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006007
6008 if (!led_supported)
6009 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
6011 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08006012 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 return -EINVAL;
6014
Adam Leeedf2d772013-06-08 16:51:15 +08006015 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
6016 tpacpi_leds[led].led < 0)
6017 return -ENODEV;
6018
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006019 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006020 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006022 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006023 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006024 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006025 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006026 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006027 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03006028
6029 rc = led_set_status(led, s);
6030 if (rc < 0)
6031 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 }
6033
6034 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006035}
6036
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006037static struct ibm_struct led_driver_data = {
6038 .name = "led",
6039 .read = led_read,
6040 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006041 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006042};
6043
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006044/*************************************************************************
6045 * Beep subdriver
6046 */
6047
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006048TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006049
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006050#define TPACPI_BEEP_Q1 0x0001
6051
6052static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6053 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6054 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6055};
6056
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006057static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006058{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006059 unsigned long quirks;
6060
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006061 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6062
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006063 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006064
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006065 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6066 str_supported(beep_handle != NULL));
6067
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006068 quirks = tpacpi_check_quirks(beep_quirk_table,
6069 ARRAY_SIZE(beep_quirk_table));
6070
6071 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6072
Jan van den Berg72a979f2014-09-17 00:01:08 +02006073 return (beep_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006074}
6075
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006076static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006078 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006079 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006080 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006081 seq_printf(m, "status:\t\tsupported\n");
6082 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086}
6087
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006088static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089{
6090 char *cmd;
6091 int beep_cmd;
6092
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006093 if (!beep_handle)
6094 return -ENODEV;
6095
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006097 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6098 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 /* beep_cmd set */
6100 } else
6101 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006102 if (tp_features.beep_needs_two_args) {
6103 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6104 beep_cmd, 0))
6105 return -EIO;
6106 } else {
6107 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6108 beep_cmd))
6109 return -EIO;
6110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 }
6112
6113 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006114}
6115
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006116static struct ibm_struct beep_driver_data = {
6117 .name = "beep",
6118 .read = beep_read,
6119 .write = beep_write,
6120};
6121
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006122/*************************************************************************
6123 * Thermal subdriver
6124 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006125
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006126enum thermal_access_mode {
6127 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6128 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6129 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6130 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6131 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6132};
6133
6134enum { /* TPACPI_THERMAL_TPEC_* */
6135 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6136 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6137 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006138
6139 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006140};
6141
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006142
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006143#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6144struct ibm_thermal_sensors_struct {
6145 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6146};
6147
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006148static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006149
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006150/* idx is zero-based */
6151static int thermal_get_sensor(int idx, s32 *value)
6152{
6153 int t;
6154 s8 tmp;
6155 char tmpi[5];
6156
6157 t = TP_EC_THERMAL_TMP0;
6158
6159 switch (thermal_read_mode) {
6160#if TPACPI_MAX_THERMAL_SENSORS >= 16
6161 case TPACPI_THERMAL_TPEC_16:
6162 if (idx >= 8 && idx <= 15) {
6163 t = TP_EC_THERMAL_TMP8;
6164 idx -= 8;
6165 }
6166 /* fallthrough */
6167#endif
6168 case TPACPI_THERMAL_TPEC_8:
6169 if (idx <= 7) {
6170 if (!acpi_ec_read(t + idx, &tmp))
6171 return -EIO;
6172 *value = tmp * 1000;
6173 return 0;
6174 }
6175 break;
6176
6177 case TPACPI_THERMAL_ACPI_UPDT:
6178 if (idx <= 7) {
6179 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6180 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6181 return -EIO;
6182 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6183 return -EIO;
6184 *value = (t - 2732) * 100;
6185 return 0;
6186 }
6187 break;
6188
6189 case TPACPI_THERMAL_ACPI_TMP07:
6190 if (idx <= 7) {
6191 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6192 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6193 return -EIO;
6194 if (t > 127 || t < -127)
6195 t = TP_EC_THERMAL_TMP_NA;
6196 *value = t * 1000;
6197 return 0;
6198 }
6199 break;
6200
6201 case TPACPI_THERMAL_NONE:
6202 default:
6203 return -ENOSYS;
6204 }
6205
6206 return -EINVAL;
6207}
6208
6209static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6210{
6211 int res, i;
6212 int n;
6213
6214 n = 8;
6215 i = 0;
6216
6217 if (!s)
6218 return -EINVAL;
6219
6220 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6221 n = 16;
6222
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006223 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006224 res = thermal_get_sensor(i, &s->temp[i]);
6225 if (res)
6226 return res;
6227 }
6228
6229 return n;
6230}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006231
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006232static void thermal_dump_all_sensors(void)
6233{
6234 int n, i;
6235 struct ibm_thermal_sensors_struct t;
6236
6237 n = thermal_get_sensors(&t);
6238 if (n <= 0)
6239 return;
6240
Joe Perches0978e012011-04-04 10:06:25 -07006241 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006242
6243 for (i = 0; i < n; i++) {
6244 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07006245 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006246 else
Joe Perches0978e012011-04-04 10:06:25 -07006247 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006248 }
6249
Joe Perches0978e012011-04-04 10:06:25 -07006250 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006251}
6252
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006253/* sysfs temp##_input -------------------------------------------------- */
6254
6255static ssize_t thermal_temp_input_show(struct device *dev,
6256 struct device_attribute *attr,
6257 char *buf)
6258{
6259 struct sensor_device_attribute *sensor_attr =
6260 to_sensor_dev_attr(attr);
6261 int idx = sensor_attr->index;
6262 s32 value;
6263 int res;
6264
6265 res = thermal_get_sensor(idx, &value);
6266 if (res)
6267 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006268 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006269 return -ENXIO;
6270
6271 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6272}
6273
6274#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006275 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6276 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006277
6278static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6279 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6280 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6281 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6282 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6283 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6284 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6285 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6286 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6287 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6288 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6289 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6290 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6291 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6292 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6293 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6294 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6295};
6296
6297#define THERMAL_ATTRS(X) \
6298 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6299
6300static struct attribute *thermal_temp_input_attr[] = {
6301 THERMAL_ATTRS(8),
6302 THERMAL_ATTRS(9),
6303 THERMAL_ATTRS(10),
6304 THERMAL_ATTRS(11),
6305 THERMAL_ATTRS(12),
6306 THERMAL_ATTRS(13),
6307 THERMAL_ATTRS(14),
6308 THERMAL_ATTRS(15),
6309 THERMAL_ATTRS(0),
6310 THERMAL_ATTRS(1),
6311 THERMAL_ATTRS(2),
6312 THERMAL_ATTRS(3),
6313 THERMAL_ATTRS(4),
6314 THERMAL_ATTRS(5),
6315 THERMAL_ATTRS(6),
6316 THERMAL_ATTRS(7),
6317 NULL
6318};
6319
6320static const struct attribute_group thermal_temp_input16_group = {
6321 .attrs = thermal_temp_input_attr
6322};
6323
6324static const struct attribute_group thermal_temp_input8_group = {
6325 .attrs = &thermal_temp_input_attr[8]
6326};
6327
6328#undef THERMAL_SENSOR_ATTR_TEMP
6329#undef THERMAL_ATTRS
6330
6331/* --------------------------------------------------------------------- */
6332
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006333static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006334{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006335 u8 t, ta1, ta2;
6336 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006337 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006338 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006339
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006340 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6341
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006342 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006343
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03006344 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006345 /*
6346 * Direct EC access mode: sensors at registers
6347 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6348 * non-implemented, thermal sensors return 0x80 when
6349 * not available
6350 */
6351
6352 ta1 = ta2 = 0;
6353 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006354 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006355 ta1 |= t;
6356 } else {
6357 ta1 = 0;
6358 break;
6359 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006360 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006361 ta2 |= t;
6362 } else {
6363 ta1 = 0;
6364 break;
6365 }
6366 }
6367 if (ta1 == 0) {
6368 /* This is sheer paranoia, but we handle it anyway */
6369 if (acpi_tmp7) {
Joe Perches0978e012011-04-04 10:06:25 -07006370 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006371 "falling back to ACPI TMPx access "
6372 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006373 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006374 } else {
Joe Perches0978e012011-04-04 10:06:25 -07006375 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006376 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006377 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006378 }
6379 } else {
6380 thermal_read_mode =
6381 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006382 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006383 }
6384 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006385 if (tpacpi_is_ibm() &&
6386 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006387 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006388 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006389 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006390 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006391 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006392 }
6393 } else {
6394 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006395 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006396 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006397
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006398 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6399 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6400 thermal_read_mode);
6401
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006402 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006403 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006404 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006405 &thermal_temp_input16_group);
6406 if (res)
6407 return res;
6408 break;
6409 case TPACPI_THERMAL_TPEC_8:
6410 case TPACPI_THERMAL_ACPI_TMP07:
6411 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006412 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006413 &thermal_temp_input8_group);
6414 if (res)
6415 return res;
6416 break;
6417 case TPACPI_THERMAL_NONE:
6418 default:
6419 return 1;
6420 }
6421
6422 return 0;
6423}
6424
6425static void thermal_exit(void)
6426{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006427 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006428 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006429 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006430 &thermal_temp_input16_group);
6431 break;
6432 case TPACPI_THERMAL_TPEC_8:
6433 case TPACPI_THERMAL_ACPI_TMP07:
6434 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006435 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08006436 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006437 break;
6438 case TPACPI_THERMAL_NONE:
6439 default:
6440 break;
6441 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006442}
6443
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006444static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006445{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006446 int n, i;
6447 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006448
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006449 n = thermal_get_sensors(&t);
6450 if (unlikely(n < 0))
6451 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006452
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006453 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006454
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006455 if (n > 0) {
6456 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006457 seq_printf(m, "%d ", t.temp[i] / 1000);
6458 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006459 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006460 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006461
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006462 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006463}
6464
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006465static struct ibm_struct thermal_driver_data = {
6466 .name = "thermal",
6467 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006468 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006469};
6470
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006471/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006472 * Backlight/brightness subdriver
6473 */
Holger Macht8acb0252006-10-20 14:30:28 -07006474
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006475#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6476
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006477/*
6478 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6479 * CMOS NVRAM byte 0x5E, bits 0-3.
6480 *
6481 * EC HBRV (0x31) has the following layout
6482 * Bit 7: unknown function
6483 * Bit 6: unknown function
6484 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6485 * Bit 4: must be set to zero to avoid problems
6486 * Bit 3-0: backlight brightness level
6487 *
6488 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006489 *
6490 * WARNING: The X61 has been verified to use HBRV for something else, so
6491 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6492 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006493 */
6494
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006495enum {
6496 TP_EC_BACKLIGHT = 0x31,
6497
6498 /* TP_EC_BACKLIGHT bitmasks */
6499 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6500 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6501 TP_EC_BACKLIGHT_MAPSW = 0x20,
6502};
6503
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006504enum tpacpi_brightness_access_mode {
6505 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6506 TPACPI_BRGHT_MODE_EC, /* EC control */
6507 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6508 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6509 TPACPI_BRGHT_MODE_MAX
6510};
6511
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006512static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006513
6514static enum tpacpi_brightness_access_mode brightness_mode =
6515 TPACPI_BRGHT_MODE_MAX;
6516
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006517static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6518
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006519static struct mutex brightness_mutex;
6520
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006521/* NVRAM brightness access,
6522 * call with brightness_mutex held! */
6523static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006524{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006525 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006526
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006527 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6528 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6529 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006530 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006531
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006532 return lnvram;
6533}
6534
6535static void tpacpi_brightness_checkpoint_nvram(void)
6536{
6537 u8 lec = 0;
6538 u8 b_nvram;
6539
6540 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6541 return;
6542
6543 vdbg_printk(TPACPI_DBG_BRGHT,
6544 "trying to checkpoint backlight level to NVRAM...\n");
6545
6546 if (mutex_lock_killable(&brightness_mutex) < 0)
6547 return;
6548
6549 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6550 goto unlock;
6551 lec &= TP_EC_BACKLIGHT_LVLMSK;
6552 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6553
6554 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6555 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6556 /* NVRAM needs update */
6557 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6558 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6559 b_nvram |= lec;
6560 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6561 dbg_printk(TPACPI_DBG_BRGHT,
6562 "updated NVRAM backlight level to %u (0x%02x)\n",
6563 (unsigned int) lec, (unsigned int) b_nvram);
6564 } else
6565 vdbg_printk(TPACPI_DBG_BRGHT,
6566 "NVRAM backlight level already is %u (0x%02x)\n",
6567 (unsigned int) lec, (unsigned int) b_nvram);
6568
6569unlock:
6570 mutex_unlock(&brightness_mutex);
6571}
6572
6573
6574/* call with brightness_mutex held! */
6575static int tpacpi_brightness_get_raw(int *status)
6576{
6577 u8 lec = 0;
6578
6579 switch (brightness_mode) {
6580 case TPACPI_BRGHT_MODE_UCMS_STEP:
6581 *status = tpacpi_brightness_nvram_get();
6582 return 0;
6583 case TPACPI_BRGHT_MODE_EC:
6584 case TPACPI_BRGHT_MODE_ECNVRAM:
6585 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006586 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006587 *status = lec;
6588 return 0;
6589 default:
6590 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006591 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006592}
6593
6594/* call with brightness_mutex held! */
6595/* do NOT call with illegal backlight level value */
6596static int tpacpi_brightness_set_ec(unsigned int value)
6597{
6598 u8 lec = 0;
6599
6600 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6601 return -EIO;
6602
6603 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6604 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6605 (value & TP_EC_BACKLIGHT_LVLMSK))))
6606 return -EIO;
6607
6608 return 0;
6609}
6610
6611/* call with brightness_mutex held! */
6612static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6613{
6614 int cmos_cmd, inc;
6615 unsigned int current_value, i;
6616
6617 current_value = tpacpi_brightness_nvram_get();
6618
6619 if (value == current_value)
6620 return 0;
6621
6622 cmos_cmd = (value > current_value) ?
6623 TP_CMOS_BRIGHTNESS_UP :
6624 TP_CMOS_BRIGHTNESS_DOWN;
6625 inc = (value > current_value) ? 1 : -1;
6626
6627 for (i = current_value; i != value; i += inc)
6628 if (issue_thinkpad_cmos_command(cmos_cmd))
6629 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006630
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006631 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006632}
6633
6634/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006635static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006636{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006637 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006638
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006639 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006640 return -EINVAL;
6641
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006642 vdbg_printk(TPACPI_DBG_BRGHT,
6643 "set backlight level to %d\n", value);
6644
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006645 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006646 if (res < 0)
6647 return res;
6648
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006649 switch (brightness_mode) {
6650 case TPACPI_BRGHT_MODE_EC:
6651 case TPACPI_BRGHT_MODE_ECNVRAM:
6652 res = tpacpi_brightness_set_ec(value);
6653 break;
6654 case TPACPI_BRGHT_MODE_UCMS_STEP:
6655 res = tpacpi_brightness_set_ucmsstep(value);
6656 break;
6657 default:
6658 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006659 }
6660
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006661 mutex_unlock(&brightness_mutex);
6662 return res;
6663}
6664
6665/* sysfs backlight class ----------------------------------------------- */
6666
6667static int brightness_update_status(struct backlight_device *bd)
6668{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006669 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006670 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6671 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006672 bd->props.brightness : 0;
6673
6674 dbg_printk(TPACPI_DBG_BRGHT,
6675 "backlight: attempt to set level to %d\n",
6676 level);
6677
6678 /* it is the backlight class's job (caller) to handle
6679 * EINTR and other errors properly */
6680 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006681}
Holger Macht8acb0252006-10-20 14:30:28 -07006682
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006683static int brightness_get(struct backlight_device *bd)
6684{
6685 int status, res;
6686
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006687 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006688 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006689 return 0;
6690
6691 res = tpacpi_brightness_get_raw(&status);
6692
6693 mutex_unlock(&brightness_mutex);
6694
6695 if (res < 0)
6696 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006697
6698 return status & TP_EC_BACKLIGHT_LVLMSK;
6699}
6700
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006701static void tpacpi_brightness_notify_change(void)
6702{
6703 backlight_force_update(ibm_backlight_device,
6704 BACKLIGHT_UPDATE_HOTKEY);
6705}
6706
Lionel Debrouxacc24722010-11-16 14:14:02 +01006707static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006708 .get_brightness = brightness_get,
6709 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006710};
6711
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006712/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006713
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006714/*
6715 * Call _BCL method of video device. On some ThinkPads this will
6716 * switch the firmware to the ACPI brightness control mode.
6717 */
6718
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006719static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6720{
6721 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6722 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006723 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006724 int rc;
6725
Aaron Lu46445b62013-10-11 21:27:46 +08006726 if (acpi_bus_get_device(handle, &device))
6727 return 0;
6728
6729 rc = 0;
6730 list_for_each_entry(child, &device->children, node) {
6731 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6732 NULL, &buffer);
6733 if (ACPI_FAILURE(status))
6734 continue;
6735
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006736 obj = (union acpi_object *)buffer.pointer;
6737 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006738 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006739 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006740 rc = 0;
6741 } else {
6742 rc = obj->package.count;
6743 }
Aaron Lu46445b62013-10-11 21:27:46 +08006744 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006745 }
6746
6747 kfree(buffer.pointer);
6748 return rc;
6749}
6750
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006751
6752/*
6753 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6754 */
6755static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6756{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006757 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006758 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006759
Aaron Lu46445b62013-10-11 21:27:46 +08006760 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006761 if (video_device)
6762 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006763
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006764 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006765
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006766 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006767}
6768
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006769/*
6770 * These are only useful for models that have only one possibility
6771 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6772 * these quirks.
6773 */
6774#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6775#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6776#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6777
6778static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6779 /* Models with ATI GPUs known to require ECNVRAM mode */
6780 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6781
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006782 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006783 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006784 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006785 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006786 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6787
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006788 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006789 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006790 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6791 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006792
6793 /* Models with Intel GMA900 */
6794 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6795 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6796 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6797};
6798
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006799/*
6800 * Returns < 0 for error, otherwise sets tp_features.bright_*
6801 * and bright_maxlvl.
6802 */
6803static void __init tpacpi_detect_brightness_capabilities(void)
6804{
6805 unsigned int b;
6806
6807 vdbg_printk(TPACPI_DBG_INIT,
6808 "detecting firmware brightness interface capabilities\n");
6809
6810 /* we could run a quirks check here (same table used by
6811 * brightness_init) if needed */
6812
6813 /*
6814 * We always attempt to detect acpi support, so as to switch
6815 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6816 * going to publish a backlight interface
6817 */
6818 b = tpacpi_check_std_acpi_brightness_support();
6819 switch (b) {
6820 case 16:
6821 bright_maxlvl = 15;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006822 break;
6823 case 8:
6824 case 0:
6825 bright_maxlvl = 7;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006826 break;
6827 default:
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006828 tp_features.bright_unkfw = 1;
6829 bright_maxlvl = b - 1;
6830 }
Eric Curtin15c75622016-01-30 16:55:59 +00006831 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006832}
6833
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006834static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006835{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006836 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006837 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006838 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006839
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006840 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6841
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006842 mutex_init(&brightness_mutex);
6843
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006844 quirks = tpacpi_check_quirks(brightness_quirk_table,
6845 ARRAY_SIZE(brightness_quirk_table));
6846
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006847 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006848
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006849 /* if it is unknown, we don't handle it: it wouldn't be safe */
6850 if (tp_features.bright_unkfw)
6851 return 1;
6852
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006853 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006854 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006855 "brightness support disabled by "
6856 "module parameter\n");
6857 return 1;
6858 }
6859
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02006860 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006861 if (brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006862 pr_info("Standard ACPI backlight interface "
6863 "available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006864 return 1;
6865 } else if (brightness_enable == 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006866 pr_warn("Cannot enable backlight brightness support, "
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006867 "ACPI is already handling it. Refer to the "
Joe Perches0978e012011-04-04 10:06:25 -07006868 "acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006869 return 1;
6870 }
6871 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006872 pr_notice("Standard ACPI backlight interface not "
6873 "available, thinkpad_acpi native "
6874 "brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006875 }
6876
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006877 /*
6878 * Check for module parameter bogosity, note that we
6879 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6880 * able to detect "unspecified"
6881 */
6882 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006883 return -EINVAL;
6884
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006885 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6886 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6887 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006888 if (quirks & TPACPI_BRGHT_Q_EC)
6889 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6890 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006891 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6892
6893 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006894 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006895 brightness_mode);
6896 }
6897
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006898 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006899 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006900 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6901 brightness_mode == TPACPI_BRGHT_MODE_EC))
6902 return -EINVAL;
6903
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006904 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006905 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006906
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006907 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006908 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006909 props.max_brightness = bright_maxlvl;
6910 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006911 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6912 NULL, NULL,
6913 &ibm_backlight_data,
6914 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006915 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006916 int rc = PTR_ERR(ibm_backlight_device);
6917 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006918 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006919 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006920 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006921 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6922 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006923
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006924 if (quirks & TPACPI_BRGHT_Q_ASK) {
Joe Perches0978e012011-04-04 10:06:25 -07006925 pr_notice("brightness: will use unverified default: "
6926 "brightness_mode=%d\n", brightness_mode);
6927 pr_notice("brightness: please report to %s whether it works well "
6928 "or not on your ThinkPad\n", TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006929 }
6930
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006931 /* Added by mistake in early 2007. Probably useless, but it could
6932 * be working around some unknown firmware problem where the value
6933 * read at startup doesn't match the real hardware state... so leave
6934 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006935 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006936
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006937 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6938 "brightness: registering brightness hotkeys "
6939 "as change notification\n");
6940 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6941 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006942 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006943 return 0;
6944}
6945
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006946static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006947{
6948 tpacpi_brightness_checkpoint_nvram();
6949}
6950
6951static void brightness_shutdown(void)
6952{
6953 tpacpi_brightness_checkpoint_nvram();
6954}
6955
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006956static void brightness_exit(void)
6957{
6958 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006959 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006960 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006961 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006962 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006963
6964 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006965}
6966
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006967static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006968{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006969 int level;
6970
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006971 level = brightness_get(NULL);
6972 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006973 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006974 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006975 seq_printf(m, "level:\t\t%d\n", level);
6976 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006977 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6978 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006979 }
6980
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006981 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006982}
6983
6984static int brightness_write(char *buf)
6985{
6986 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006987 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006988 char *cmd;
6989
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006990 level = brightness_get(NULL);
6991 if (level < 0)
6992 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006993
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006994 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006995 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006996 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006997 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006998 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006999 if (level > 0)
7000 level--;
7001 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03007002 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007003 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007004 } else
7005 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007006 }
7007
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007008 tpacpi_disclose_usertask("procfs brightness",
7009 "set level to %d\n", level);
7010
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02007011 /*
7012 * Now we know what the final level should be, so we try to set it.
7013 * Doing it this way makes the syscall restartable in case of EINTR
7014 */
7015 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007016 if (!rc && ibm_backlight_device)
7017 backlight_force_update(ibm_backlight_device,
7018 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02007019 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007020}
7021
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007022static struct ibm_struct brightness_driver_data = {
7023 .name = "brightness",
7024 .read = brightness_read,
7025 .write = brightness_write,
7026 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007027 .suspend = brightness_suspend,
7028 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007029};
7030
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007031/*************************************************************************
7032 * Volume subdriver
7033 */
7034
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007035/*
7036 * IBM ThinkPads have a simple volume controller with MUTE gating.
7037 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7038 *
7039 * Since the *61 series (and probably also the later *60 series), Lenovo
7040 * ThinkPads only implement the MUTE gate.
7041 *
7042 * EC register 0x30
7043 * Bit 6: MUTE (1 mutes sound)
7044 * Bit 3-0: Volume
7045 * Other bits should be zero as far as we know.
7046 *
7047 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7048 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7049 * such as bit 7 which is used to detect repeated presses of MUTE,
7050 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007051 *
7052 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7053 * in response to user input. Unfortunately, this rarely works well.
7054 * The laptop changes the state of its internal MUTE gate and, on some
7055 * models, sends KEY_MUTE, causing any user code that responds to the
7056 * mute button to get confused. The hardware MUTE gate is also
7057 * unnecessary, since user code can handle the mute button without
7058 * kernel or EC help.
7059 *
7060 * To avoid confusing userspace, we simply disable all EC-based mute
7061 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007062 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007063
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007064#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7065
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007066#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7067#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7068#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7069
Takashi Iwaicab66612013-10-24 16:06:32 +02007070#if SNDRV_CARDS <= 32
7071#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7072#else
7073#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7074#endif
7075static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007076static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307077static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007078
7079struct tpacpi_alsa_data {
7080 struct snd_card *card;
7081 struct snd_ctl_elem_id *ctl_mute_id;
7082 struct snd_ctl_elem_id *ctl_vol_id;
7083};
7084
7085static struct snd_card *alsa_card;
7086
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007087enum {
7088 TP_EC_AUDIO = 0x30,
7089
7090 /* TP_EC_AUDIO bits */
7091 TP_EC_AUDIO_MUTESW = 6,
7092
7093 /* TP_EC_AUDIO bitmasks */
7094 TP_EC_AUDIO_LVL_MSK = 0x0F,
7095 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7096
7097 /* Maximum volume */
7098 TP_EC_VOLUME_MAX = 14,
7099};
7100
7101enum tpacpi_volume_access_mode {
7102 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7103 TPACPI_VOL_MODE_EC, /* Pure EC control */
7104 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7105 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7106 TPACPI_VOL_MODE_MAX
7107};
7108
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007109enum tpacpi_volume_capabilities {
7110 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7111 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7112 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7113 TPACPI_VOL_CAP_MAX
7114};
7115
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007116enum tpacpi_mute_btn_mode {
7117 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7118 /* We don't know what mode 1 is. */
7119 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7120 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7121};
7122
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007123static enum tpacpi_volume_access_mode volume_mode =
7124 TPACPI_VOL_MODE_MAX;
7125
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007126static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307127static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007128static bool software_mute_requested = true;
7129static bool software_mute_active;
7130static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007131
7132/*
7133 * Used to syncronize writers to TP_EC_AUDIO and
7134 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7135 */
7136static struct mutex volume_mutex;
7137
7138static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007139{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007140 u8 lec = 0;
7141 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007142 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007143
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007144 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7145 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007146 if (!volume_control_allowed)
7147 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007148 if (software_mute_active)
7149 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007150
7151 vdbg_printk(TPACPI_DBG_MIXER,
7152 "trying to checkpoint mixer state to NVRAM...\n");
7153
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007154 if (tp_features.mixer_no_level_control)
7155 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7156 else
7157 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7158
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007159 if (mutex_lock_killable(&volume_mutex) < 0)
7160 return;
7161
7162 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7163 goto unlock;
7164 lec &= ec_mask;
7165 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7166
7167 if (lec != (b_nvram & ec_mask)) {
7168 /* NVRAM needs update */
7169 b_nvram &= ~ec_mask;
7170 b_nvram |= lec;
7171 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7172 dbg_printk(TPACPI_DBG_MIXER,
7173 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7174 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007175 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007176 vdbg_printk(TPACPI_DBG_MIXER,
7177 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7178 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007179 }
7180
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007181unlock:
7182 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007183}
7184
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007185static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007186{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007187 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007188
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007189 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7190 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007191
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007192 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007193
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007194 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007195
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007196 return 0;
7197}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007198
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007199static int volume_get_status(u8 *status)
7200{
7201 return volume_get_status_ec(status);
7202}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007203
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007204static int volume_set_status_ec(const u8 status)
7205{
7206 if (!acpi_ec_write(TP_EC_AUDIO, status))
7207 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007208
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007209 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7210
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007211 /*
7212 * On X200s, and possibly on others, it can take a while for
7213 * reads to become correct.
7214 */
7215 msleep(1);
7216
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007217 return 0;
7218}
7219
7220static int volume_set_status(const u8 status)
7221{
7222 return volume_set_status_ec(status);
7223}
7224
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007225/* returns < 0 on error, 0 on no change, 1 on change */
7226static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007227{
7228 int rc;
7229 u8 s, n;
7230
7231 if (mutex_lock_killable(&volume_mutex) < 0)
7232 return -EINTR;
7233
7234 rc = volume_get_status_ec(&s);
7235 if (rc)
7236 goto unlock;
7237
7238 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7239 s & ~TP_EC_AUDIO_MUTESW_MSK;
7240
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007241 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007242 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007243 if (!rc)
7244 rc = 1;
7245 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007246
7247unlock:
7248 mutex_unlock(&volume_mutex);
7249 return rc;
7250}
7251
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007252static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007253{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007254 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007255 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007256 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007257}
7258
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007259static int volume_set_mute(const bool mute)
7260{
7261 int rc;
7262
7263 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7264 (mute) ? "" : "un");
7265
7266 rc = __volume_set_mute_ec(mute);
7267 return (rc < 0) ? rc : 0;
7268}
7269
7270/* returns < 0 on error, 0 on no change, 1 on change */
7271static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007272{
7273 int rc;
7274 u8 s, n;
7275
7276 if (vol > TP_EC_VOLUME_MAX)
7277 return -EINVAL;
7278
7279 if (mutex_lock_killable(&volume_mutex) < 0)
7280 return -EINTR;
7281
7282 rc = volume_get_status_ec(&s);
7283 if (rc)
7284 goto unlock;
7285
7286 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7287
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007288 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007289 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007290 if (!rc)
7291 rc = 1;
7292 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007293
7294unlock:
7295 mutex_unlock(&volume_mutex);
7296 return rc;
7297}
7298
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007299static int volume_set_software_mute(bool startup)
7300{
7301 int result;
7302
7303 if (!tpacpi_is_lenovo())
7304 return -ENODEV;
7305
7306 if (startup) {
7307 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7308 "HAUM", "qd"))
7309 return -EIO;
7310
7311 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7312 "Initial HAUM setting was %d\n",
7313 software_mute_orig_mode);
7314 }
7315
7316 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7317 (int)TP_EC_MUTE_BTN_NONE))
7318 return -EIO;
7319
7320 if (result != TP_EC_MUTE_BTN_NONE)
7321 pr_warn("Unexpected SAUM result %d\n",
7322 result);
7323
7324 /*
7325 * In software mute mode, the standard codec controls take
7326 * precendence, so we unmute the ThinkPad HW switch at
7327 * startup. Just on case there are SAUM-capable ThinkPads
7328 * with level controls, set max HW volume as well.
7329 */
7330 if (tp_features.mixer_no_level_control)
7331 result = volume_set_mute(false);
7332 else
7333 result = volume_set_status(TP_EC_VOLUME_MAX);
7334
7335 if (result != 0)
7336 pr_warn("Failed to unmute the HW mute switch\n");
7337
7338 return 0;
7339}
7340
7341static void volume_exit_software_mute(void)
7342{
7343 int r;
7344
7345 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7346 || r != software_mute_orig_mode)
7347 pr_warn("Failed to restore mute mode\n");
7348}
7349
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007350static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007351{
7352 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007353 "ALSA: trying to set volume level to %hu\n", vol);
7354 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007355}
7356
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007357static void volume_alsa_notify_change(void)
7358{
7359 struct tpacpi_alsa_data *d;
7360
7361 if (alsa_card && alsa_card->private_data) {
7362 d = alsa_card->private_data;
7363 if (d->ctl_mute_id)
7364 snd_ctl_notify(alsa_card,
7365 SNDRV_CTL_EVENT_MASK_VALUE,
7366 d->ctl_mute_id);
7367 if (d->ctl_vol_id)
7368 snd_ctl_notify(alsa_card,
7369 SNDRV_CTL_EVENT_MASK_VALUE,
7370 d->ctl_vol_id);
7371 }
7372}
7373
7374static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7375 struct snd_ctl_elem_info *uinfo)
7376{
7377 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7378 uinfo->count = 1;
7379 uinfo->value.integer.min = 0;
7380 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7381 return 0;
7382}
7383
7384static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7385 struct snd_ctl_elem_value *ucontrol)
7386{
7387 u8 s;
7388 int rc;
7389
7390 rc = volume_get_status(&s);
7391 if (rc < 0)
7392 return rc;
7393
7394 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7395 return 0;
7396}
7397
7398static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7399 struct snd_ctl_elem_value *ucontrol)
7400{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007401 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7402 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007403 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007404}
7405
7406#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7407
7408static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7409 struct snd_ctl_elem_value *ucontrol)
7410{
7411 u8 s;
7412 int rc;
7413
7414 rc = volume_get_status(&s);
7415 if (rc < 0)
7416 return rc;
7417
7418 ucontrol->value.integer.value[0] =
7419 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7420 return 0;
7421}
7422
7423static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7424 struct snd_ctl_elem_value *ucontrol)
7425{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007426 tpacpi_disclose_usertask("ALSA", "%smute\n",
7427 ucontrol->value.integer.value[0] ?
7428 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007429 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007430}
7431
Mathias Krausec3aa4722014-07-16 19:43:17 +02007432static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007433 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7434 .name = "Console Playback Volume",
7435 .index = 0,
7436 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7437 .info = volume_alsa_vol_info,
7438 .get = volume_alsa_vol_get,
7439};
7440
Mathias Krausec3aa4722014-07-16 19:43:17 +02007441static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007442 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7443 .name = "Console Playback Switch",
7444 .index = 0,
7445 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7446 .info = volume_alsa_mute_info,
7447 .get = volume_alsa_mute_get,
7448};
7449
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007450static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007451{
7452 tpacpi_volume_checkpoint_nvram();
7453}
7454
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007455static void volume_resume(void)
7456{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007457 if (software_mute_active) {
7458 if (volume_set_software_mute(false) < 0)
7459 pr_warn("Failed to restore software mute\n");
7460 } else {
7461 volume_alsa_notify_change();
7462 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007463}
7464
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007465static void volume_shutdown(void)
7466{
7467 tpacpi_volume_checkpoint_nvram();
7468}
7469
7470static void volume_exit(void)
7471{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007472 if (alsa_card) {
7473 snd_card_free(alsa_card);
7474 alsa_card = NULL;
7475 }
7476
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007477 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007478
7479 if (software_mute_active)
7480 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007481}
7482
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007483static int __init volume_create_alsa_mixer(void)
7484{
7485 struct snd_card *card;
7486 struct tpacpi_alsa_data *data;
7487 struct snd_kcontrol *ctl_vol;
7488 struct snd_kcontrol *ctl_mute;
7489 int rc;
7490
Takashi Iwai89235e52014-01-29 15:01:27 +01007491 rc = snd_card_new(&tpacpi_pdev->dev,
7492 alsa_index, alsa_id, THIS_MODULE,
7493 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007494 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007495 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007496 return 1;
7497 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007498
7499 BUG_ON(!card->private_data);
7500 data = card->private_data;
7501 data->card = card;
7502
7503 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7504 sizeof(card->driver));
7505 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7506 sizeof(card->shortname));
7507 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7508 (thinkpad_id.ec_version_str) ?
7509 thinkpad_id.ec_version_str : "(unknown)");
7510 snprintf(card->longname, sizeof(card->longname),
7511 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7512 (thinkpad_id.ec_version_str) ?
7513 thinkpad_id.ec_version_str : "unknown");
7514
7515 if (volume_control_allowed) {
7516 volume_alsa_control_vol.put = volume_alsa_vol_put;
7517 volume_alsa_control_vol.access =
7518 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7519
7520 volume_alsa_control_mute.put = volume_alsa_mute_put;
7521 volume_alsa_control_mute.access =
7522 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7523 }
7524
7525 if (!tp_features.mixer_no_level_control) {
7526 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7527 rc = snd_ctl_add(card, ctl_vol);
7528 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007529 pr_err("Failed to create ALSA volume control: %d\n",
7530 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007531 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007532 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007533 data->ctl_vol_id = &ctl_vol->id;
7534 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007535
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007536 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7537 rc = snd_ctl_add(card, ctl_mute);
7538 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007539 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007540 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007541 }
7542 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007543
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007544 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007545 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007546 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007547 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007548 }
7549
7550 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007551 return 0;
7552
7553err_exit:
7554 snd_card_free(card);
7555 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007556}
7557
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007558#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7559#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7560
7561static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7562 /* Whitelist volume level on all IBM by default */
7563 { .vendor = PCI_VENDOR_ID_IBM,
7564 .bios = TPACPI_MATCH_ANY,
7565 .ec = TPACPI_MATCH_ANY,
7566 .quirks = TPACPI_VOL_Q_LEVEL },
7567
7568 /* Lenovo models with volume control (needs confirmation) */
7569 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7570 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7571 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7572 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7573 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7574 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7575 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7576
7577 /* Whitelist mute-only on all Lenovo by default */
7578 { .vendor = PCI_VENDOR_ID_LENOVO,
7579 .bios = TPACPI_MATCH_ANY,
7580 .ec = TPACPI_MATCH_ANY,
7581 .quirks = TPACPI_VOL_Q_MUTEONLY }
7582};
7583
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007584static int __init volume_init(struct ibm_init_struct *iibm)
7585{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007586 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007587 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007588
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007589 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7590
7591 mutex_init(&volume_mutex);
7592
7593 /*
7594 * Check for module parameter bogosity, note that we
7595 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7596 * able to detect "unspecified"
7597 */
7598 if (volume_mode > TPACPI_VOL_MODE_MAX)
7599 return -EINVAL;
7600
7601 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Joe Perches0978e012011-04-04 10:06:25 -07007602 pr_err("UCMS step volume mode not implemented, "
7603 "please contact %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007604 return 1;
7605 }
7606
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007607 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7608 return -EINVAL;
7609
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007610 /*
7611 * The ALSA mixer is our primary interface.
7612 * When disabled, don't install the subdriver at all
7613 */
7614 if (!alsa_enable) {
7615 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7616 "ALSA mixer disabled by parameter, "
7617 "not loading volume subdriver...\n");
7618 return 1;
7619 }
7620
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007621 quirks = tpacpi_check_quirks(volume_quirk_table,
7622 ARRAY_SIZE(volume_quirk_table));
7623
7624 switch (volume_capabilities) {
7625 case TPACPI_VOL_CAP_AUTO:
7626 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7627 tp_features.mixer_no_level_control = 1;
7628 else if (quirks & TPACPI_VOL_Q_LEVEL)
7629 tp_features.mixer_no_level_control = 0;
7630 else
7631 return 1; /* no mixer */
7632 break;
7633 case TPACPI_VOL_CAP_VOLMUTE:
7634 tp_features.mixer_no_level_control = 0;
7635 break;
7636 case TPACPI_VOL_CAP_MUTEONLY:
7637 tp_features.mixer_no_level_control = 1;
7638 break;
7639 default:
7640 return 1;
7641 }
7642
7643 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7644 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7645 "using user-supplied volume_capabilities=%d\n",
7646 volume_capabilities);
7647
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007648 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7649 volume_mode == TPACPI_VOL_MODE_MAX) {
7650 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7651
7652 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7653 "driver auto-selected volume_mode=%d\n",
7654 volume_mode);
7655 } else {
7656 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7657 "using user-supplied volume_mode=%d\n",
7658 volume_mode);
7659 }
7660
7661 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007662 "mute is supported, volume control is %s\n",
7663 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007664
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007665 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7666 software_mute_active = true;
7667 } else {
7668 rc = volume_create_alsa_mixer();
7669 if (rc) {
7670 pr_err("Could not create the ALSA mixer interface\n");
7671 return rc;
7672 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007673
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007674 pr_info("Console audio control enabled, mode: %s\n",
7675 (volume_control_allowed) ?
7676 "override (read/write)" :
7677 "monitor (read only)");
7678 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007679
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007680 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7681 "registering volume hotkeys as change notification\n");
7682 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7683 | TP_ACPI_HKEY_VOLUP_MASK
7684 | TP_ACPI_HKEY_VOLDWN_MASK
7685 | TP_ACPI_HKEY_MUTE_MASK);
7686
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007687 return 0;
7688}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007689
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007690static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007691{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007692 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007693
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007694 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007695 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007696 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007697 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007698 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007699 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007700 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007701 status & TP_EC_AUDIO_LVL_MSK);
7702
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007703 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007704 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007705
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007706 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007707 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007708 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007709 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007710 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007711 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007712 "commands:\tlevel <level>"
7713 " (<level> is 0-%d)\n",
7714 TP_EC_VOLUME_MAX);
7715 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007716 }
7717 }
7718
7719 return 0;
7720}
7721
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007722static int volume_write(char *buf)
7723{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007724 u8 s;
7725 u8 new_level, new_mute;
7726 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007727 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007728 int rc;
7729
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007730 /*
7731 * We do allow volume control at driver startup, so that the
7732 * user can set initial state through the volume=... parameter hack.
7733 */
7734 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7735 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7736 tp_warned.volume_ctrl_forbidden = 1;
Joe Perches0978e012011-04-04 10:06:25 -07007737 pr_notice("Console audio control in monitor mode, "
7738 "changes are not allowed\n");
7739 pr_notice("Use the volume_control=1 module parameter "
7740 "to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007741 }
7742 return -EPERM;
7743 }
7744
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007745 rc = volume_get_status(&s);
7746 if (rc < 0)
7747 return rc;
7748
7749 new_level = s & TP_EC_AUDIO_LVL_MSK;
7750 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007751
7752 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007753 if (!tp_features.mixer_no_level_control) {
7754 if (strlencmp(cmd, "up") == 0) {
7755 if (new_mute)
7756 new_mute = 0;
7757 else if (new_level < TP_EC_VOLUME_MAX)
7758 new_level++;
7759 continue;
7760 } else if (strlencmp(cmd, "down") == 0) {
7761 if (new_mute)
7762 new_mute = 0;
7763 else if (new_level > 0)
7764 new_level--;
7765 continue;
7766 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7767 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7768 new_level = l;
7769 continue;
7770 }
7771 }
7772 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007773 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007774 else if (strlencmp(cmd, "unmute") == 0)
7775 new_mute = 0;
7776 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007777 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007778 }
7779
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007780 if (tp_features.mixer_no_level_control) {
7781 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7782 new_mute ? "" : "un");
7783 rc = volume_set_mute(!!new_mute);
7784 } else {
7785 tpacpi_disclose_usertask("procfs volume",
7786 "%smute and set level to %d\n",
7787 new_mute ? "" : "un", new_level);
7788 rc = volume_set_status(new_mute | new_level);
7789 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007790 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007791
7792 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007793}
7794
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007795static struct ibm_struct volume_driver_data = {
7796 .name = "volume",
7797 .read = volume_read,
7798 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007799 .exit = volume_exit,
7800 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007801 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007802 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007803};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007804
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007805#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7806
7807#define alsa_card NULL
7808
Tobias Klauserbb55a2e2016-11-25 14:18:07 +01007809static inline void volume_alsa_notify_change(void)
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007810{
7811}
7812
7813static int __init volume_init(struct ibm_init_struct *iibm)
7814{
Joe Perches0978e012011-04-04 10:06:25 -07007815 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007816
7817 return 1;
7818}
7819
7820static struct ibm_struct volume_driver_data = {
7821 .name = "volume",
7822};
7823
7824#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7825
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007826/*************************************************************************
7827 * Fan subdriver
7828 */
7829
7830/*
7831 * FAN ACCESS MODES
7832 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007833 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007834 * ACPI GFAN method: returns fan level
7835 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007836 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007837 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007838 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007839 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007840 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7841 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007842 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7843 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007844 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007845 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007846 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7847 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007848 *
7849 * Fan speed changes of any sort (including those caused by the
7850 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007851 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007852 * limited.
7853 *
7854 * Reading is not available if GFAN exists.
7855 * Writing is not available if SFAN exists.
7856 *
7857 * Bits
7858 * 7 automatic mode engaged;
7859 * (default operation mode of the ThinkPad)
7860 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007861 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007862 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007863 * the tachometer while the fan controller ramps up
7864 * the speed (which can take up to a few *minutes*).
7865 * Speeds up fan to 100% duty-cycle, which is far above
7866 * the standard RPM levels. It is not impossible that
7867 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007868 * 5-3 unused in some models. Extra bits for fan level
7869 * in others, but still useless as all values above
7870 * 7 map to the same speed as level 7 in these models.
7871 * 2-0 fan level (0..7 usually)
7872 * 0x00 = stop
7873 * 0x07 = max (set when temperatures critical)
7874 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007875 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007876 *
7877 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007878 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007879 * does so, its initial value is meaningless (0x07).
7880 *
7881 * For firmware bugs, refer to:
7882 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7883 *
7884 * ----
7885 *
7886 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7887 * Main fan tachometer reading (in RPM)
7888 *
7889 * This register is present on all ThinkPads with a new-style EC, and
7890 * it is known not to be present on the A21m/e, and T22, as there is
7891 * something else in offset 0x84 according to the ACPI DSDT. Other
7892 * ThinkPads from this same time period (and earlier) probably lack the
7893 * tachometer as well.
7894 *
Nick Andrew877d0312009-01-26 11:06:57 +01007895 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007896 * was never fixed by IBM to report the EC firmware version string
7897 * probably support the tachometer (like the early X models), so
7898 * detecting it is quite hard. We need more data to know for sure.
7899 *
7900 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7901 * might result.
7902 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007903 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7904 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007905 *
7906 * For firmware bugs, refer to:
7907 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7908 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007909 * ----
7910 *
7911 * ThinkPad EC register 0x31 bit 0 (only on select models)
7912 *
7913 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7914 * show the speed of the main fan. When bit 0 of EC register 0x31
7915 * is one, the tachometer registers show the speed of the auxiliary
7916 * fan.
7917 *
7918 * Fan control seems to affect both fans, regardless of the state
7919 * of this bit.
7920 *
7921 * So far, only the firmware for the X60/X61 non-tablet versions
7922 * seem to support this (firmware TP-7M).
7923 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007924 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007925 * ThinkPad X31, X40, X41. Not available in the X60.
7926 *
7927 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7928 * high speed. ACPI DSDT seems to map these three speeds to levels
7929 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7930 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7931 *
7932 * The speeds are stored on handles
7933 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7934 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007935 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007936 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7937 *
7938 * ACPI DSDT switches which set is in use depending on various
7939 * factors.
7940 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007941 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007942 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7943 * but the ACPI tables just mention level 7.
7944 */
7945
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007946enum { /* Fan control constants */
7947 fan_status_offset = 0x2f, /* EC register 0x2f */
7948 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7949 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007950 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7951 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007952
7953 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7954 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7955
7956 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7957};
7958
7959enum fan_status_access_mode {
7960 TPACPI_FAN_NONE = 0, /* No fan status or control */
7961 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7962 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7963};
7964
7965enum fan_control_access_mode {
7966 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7967 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7968 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7969 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7970};
7971
7972enum fan_control_commands {
7973 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7974 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7975 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7976 * and also watchdog cmd */
7977};
7978
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307979static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007980
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007981static enum fan_status_access_mode fan_status_access_mode;
7982static enum fan_control_access_mode fan_control_access_mode;
7983static enum fan_control_commands fan_control_commands;
7984
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007985static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007986static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007987static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007988static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007989
7990static struct mutex fan_mutex;
7991
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007992static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007993static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007994
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007995TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7996TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007997 "\\FSPD", /* 600e/x, 770e, 770x */
7998 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007999TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008000 "JFNS", /* 770x-JL */
8001 ); /* all others */
8002
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008003/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008004 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8005 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8006 * be in auto mode (0x80).
8007 *
8008 * This is corrected by any write to HFSP either by the driver, or
8009 * by the firmware.
8010 *
8011 * We assume 0x07 really means auto mode while this quirk is active,
8012 * as this is far more likely than the ThinkPad being in level 7,
8013 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008014 *
8015 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8016 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008017 */
8018
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008019static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008020{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008021 if (fan_control_initial_status == 0x07) {
Joe Perches0978e012011-04-04 10:06:25 -07008022 pr_notice("fan_init: initial fan status is unknown, "
8023 "assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008024 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008025 }
8026}
8027
8028static void fan_quirk1_handle(u8 *fan_status)
8029{
8030 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8031 if (*fan_status != fan_control_initial_status) {
8032 /* something changed the HFSP regisnter since
8033 * driver init time, so it is not undefined
8034 * anymore */
8035 tp_features.fan_ctrl_status_undef = 0;
8036 } else {
8037 /* Return most likely status. In fact, it
8038 * might be the only possible status */
8039 *fan_status = TP_EC_FAN_AUTO;
8040 }
8041 }
8042}
8043
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008044/* Select main fan on X60/X61, NOOP on others */
8045static bool fan_select_fan1(void)
8046{
8047 if (tp_features.second_fan) {
8048 u8 val;
8049
8050 if (ec_read(fan_select_offset, &val) < 0)
8051 return false;
8052 val &= 0xFEU;
8053 if (ec_write(fan_select_offset, val) < 0)
8054 return false;
8055 }
8056 return true;
8057}
8058
8059/* Select secondary fan on X60/X61 */
8060static bool fan_select_fan2(void)
8061{
8062 u8 val;
8063
8064 if (!tp_features.second_fan)
8065 return false;
8066
8067 if (ec_read(fan_select_offset, &val) < 0)
8068 return false;
8069 val |= 0x01U;
8070 if (ec_write(fan_select_offset, val) < 0)
8071 return false;
8072
8073 return true;
8074}
8075
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008076/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008077 * Call with fan_mutex held
8078 */
8079static void fan_update_desired_level(u8 status)
8080{
8081 if ((status &
8082 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8083 if (status > 7)
8084 fan_control_desired_level = 7;
8085 else
8086 fan_control_desired_level = status;
8087 }
8088}
8089
8090static int fan_get_status(u8 *status)
8091{
8092 u8 s;
8093
8094 /* TODO:
8095 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8096
8097 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07008098 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008099 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07008100 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008101
Dan Carpentereceeb432012-09-01 12:54:07 -07008102 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008103 return -EIO;
8104
8105 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07008106 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008107
8108 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07008109 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008110 case TPACPI_FAN_RD_TPEC:
8111 /* all except 570, 600e/x, 770e, 770x */
8112 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8113 return -EIO;
8114
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008115 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008116 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008117 fan_quirk1_handle(status);
8118 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008119
8120 break;
8121
8122 default:
8123 return -ENXIO;
8124 }
8125
8126 return 0;
8127}
8128
8129static int fan_get_status_safe(u8 *status)
8130{
8131 int rc;
8132 u8 s;
8133
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008134 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008135 return -ERESTARTSYS;
8136 rc = fan_get_status(&s);
8137 if (!rc)
8138 fan_update_desired_level(s);
8139 mutex_unlock(&fan_mutex);
8140
Dan Carpentera7718362016-04-15 17:46:34 +03008141 if (rc)
8142 return rc;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008143 if (status)
8144 *status = s;
8145
Dan Carpentera7718362016-04-15 17:46:34 +03008146 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008147}
8148
8149static int fan_get_speed(unsigned int *speed)
8150{
8151 u8 hi, lo;
8152
8153 switch (fan_status_access_mode) {
8154 case TPACPI_FAN_RD_TPEC:
8155 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008156 if (unlikely(!fan_select_fan1()))
8157 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008158 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8159 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8160 return -EIO;
8161
8162 if (likely(speed))
8163 *speed = (hi << 8) | lo;
8164
8165 break;
8166
8167 default:
8168 return -ENXIO;
8169 }
8170
8171 return 0;
8172}
8173
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008174static int fan2_get_speed(unsigned int *speed)
8175{
8176 u8 hi, lo;
8177 bool rc;
8178
8179 switch (fan_status_access_mode) {
8180 case TPACPI_FAN_RD_TPEC:
8181 /* all except 570, 600e/x, 770e, 770x */
8182 if (unlikely(!fan_select_fan2()))
8183 return -EIO;
8184 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8185 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8186 fan_select_fan1(); /* play it safe */
8187 if (rc)
8188 return -EIO;
8189
8190 if (likely(speed))
8191 *speed = (hi << 8) | lo;
8192
8193 break;
8194
8195 default:
8196 return -ENXIO;
8197 }
8198
8199 return 0;
8200}
8201
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008202static int fan_set_level(int level)
8203{
8204 if (!fan_control_allowed)
8205 return -EPERM;
8206
8207 switch (fan_control_access_mode) {
8208 case TPACPI_FAN_WR_ACPI_SFAN:
8209 if (level >= 0 && level <= 7) {
8210 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8211 return -EIO;
8212 } else
8213 return -EINVAL;
8214 break;
8215
8216 case TPACPI_FAN_WR_ACPI_FANS:
8217 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008218 if (!(level & TP_EC_FAN_AUTO) &&
8219 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008220 ((level < 0) || (level > 7)))
8221 return -EINVAL;
8222
8223 /* safety net should the EC not support AUTO
8224 * or FULLSPEED mode bits and just ignore them */
8225 if (level & TP_EC_FAN_FULLSPEED)
8226 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01008227 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008228 level |= 4; /* safety min speed 4 */
8229
8230 if (!acpi_ec_write(fan_status_offset, level))
8231 return -EIO;
8232 else
8233 tp_features.fan_ctrl_status_undef = 0;
8234 break;
8235
8236 default:
8237 return -ENXIO;
8238 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008239
8240 vdbg_printk(TPACPI_DBG_FAN,
8241 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008242 return 0;
8243}
8244
8245static int fan_set_level_safe(int level)
8246{
8247 int rc;
8248
8249 if (!fan_control_allowed)
8250 return -EPERM;
8251
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008252 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008253 return -ERESTARTSYS;
8254
8255 if (level == TPACPI_FAN_LAST_LEVEL)
8256 level = fan_control_desired_level;
8257
8258 rc = fan_set_level(level);
8259 if (!rc)
8260 fan_update_desired_level(level);
8261
8262 mutex_unlock(&fan_mutex);
8263 return rc;
8264}
8265
8266static int fan_set_enable(void)
8267{
8268 u8 s;
8269 int rc;
8270
8271 if (!fan_control_allowed)
8272 return -EPERM;
8273
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008274 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008275 return -ERESTARTSYS;
8276
8277 switch (fan_control_access_mode) {
8278 case TPACPI_FAN_WR_ACPI_FANS:
8279 case TPACPI_FAN_WR_TPEC:
8280 rc = fan_get_status(&s);
8281 if (rc < 0)
8282 break;
8283
8284 /* Don't go out of emergency fan mode */
8285 if (s != 7) {
8286 s &= 0x07;
8287 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8288 }
8289
8290 if (!acpi_ec_write(fan_status_offset, s))
8291 rc = -EIO;
8292 else {
8293 tp_features.fan_ctrl_status_undef = 0;
8294 rc = 0;
8295 }
8296 break;
8297
8298 case TPACPI_FAN_WR_ACPI_SFAN:
8299 rc = fan_get_status(&s);
8300 if (rc < 0)
8301 break;
8302
8303 s &= 0x07;
8304
8305 /* Set fan to at least level 4 */
8306 s |= 4;
8307
8308 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008309 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008310 else
8311 rc = 0;
8312 break;
8313
8314 default:
8315 rc = -ENXIO;
8316 }
8317
8318 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008319
8320 if (!rc)
8321 vdbg_printk(TPACPI_DBG_FAN,
8322 "fan control: set fan control register to 0x%02x\n",
8323 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008324 return rc;
8325}
8326
8327static int fan_set_disable(void)
8328{
8329 int rc;
8330
8331 if (!fan_control_allowed)
8332 return -EPERM;
8333
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008334 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008335 return -ERESTARTSYS;
8336
8337 rc = 0;
8338 switch (fan_control_access_mode) {
8339 case TPACPI_FAN_WR_ACPI_FANS:
8340 case TPACPI_FAN_WR_TPEC:
8341 if (!acpi_ec_write(fan_status_offset, 0x00))
8342 rc = -EIO;
8343 else {
8344 fan_control_desired_level = 0;
8345 tp_features.fan_ctrl_status_undef = 0;
8346 }
8347 break;
8348
8349 case TPACPI_FAN_WR_ACPI_SFAN:
8350 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8351 rc = -EIO;
8352 else
8353 fan_control_desired_level = 0;
8354 break;
8355
8356 default:
8357 rc = -ENXIO;
8358 }
8359
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008360 if (!rc)
8361 vdbg_printk(TPACPI_DBG_FAN,
8362 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008363
8364 mutex_unlock(&fan_mutex);
8365 return rc;
8366}
8367
8368static int fan_set_speed(int speed)
8369{
8370 int rc;
8371
8372 if (!fan_control_allowed)
8373 return -EPERM;
8374
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008375 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008376 return -ERESTARTSYS;
8377
8378 rc = 0;
8379 switch (fan_control_access_mode) {
8380 case TPACPI_FAN_WR_ACPI_FANS:
8381 if (speed >= 0 && speed <= 65535) {
8382 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8383 speed, speed, speed))
8384 rc = -EIO;
8385 } else
8386 rc = -EINVAL;
8387 break;
8388
8389 default:
8390 rc = -ENXIO;
8391 }
8392
8393 mutex_unlock(&fan_mutex);
8394 return rc;
8395}
8396
8397static void fan_watchdog_reset(void)
8398{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008399 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8400 return;
8401
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008402 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008403 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8404 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8405 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8406 else
8407 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008408}
8409
8410static void fan_watchdog_fire(struct work_struct *ignored)
8411{
8412 int rc;
8413
8414 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8415 return;
8416
Joe Perches0978e012011-04-04 10:06:25 -07008417 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008418 rc = fan_set_enable();
8419 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07008420 pr_err("fan watchdog: error %d while enabling fan, "
8421 "will try again later...\n", -rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008422 /* reschedule for later */
8423 fan_watchdog_reset();
8424 }
8425}
8426
8427/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008428 * SYSFS fan layout: hwmon compatible (device)
8429 *
8430 * pwm*_enable:
8431 * 0: "disengaged" mode
8432 * 1: manual mode
8433 * 2: native EC "auto" mode (recommended, hardware default)
8434 *
8435 * pwm*: set speed in manual mode, ignored otherwise.
8436 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8437 * interpolation.
8438 *
8439 * fan*_input: tachometer reading, RPM
8440 *
8441 *
8442 * SYSFS fan layout: extensions
8443 *
8444 * fan_watchdog (driver):
8445 * fan watchdog interval in seconds, 0 disables (default), max 120
8446 */
8447
8448/* sysfs fan pwm1_enable ----------------------------------------------- */
8449static ssize_t fan_pwm1_enable_show(struct device *dev,
8450 struct device_attribute *attr,
8451 char *buf)
8452{
8453 int res, mode;
8454 u8 status;
8455
8456 res = fan_get_status_safe(&status);
8457 if (res)
8458 return res;
8459
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008460 if (status & TP_EC_FAN_FULLSPEED) {
8461 mode = 0;
8462 } else if (status & TP_EC_FAN_AUTO) {
8463 mode = 2;
8464 } else
8465 mode = 1;
8466
8467 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8468}
8469
8470static ssize_t fan_pwm1_enable_store(struct device *dev,
8471 struct device_attribute *attr,
8472 const char *buf, size_t count)
8473{
8474 unsigned long t;
8475 int res, level;
8476
8477 if (parse_strtoul(buf, 2, &t))
8478 return -EINVAL;
8479
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008480 tpacpi_disclose_usertask("hwmon pwm1_enable",
8481 "set fan mode to %lu\n", t);
8482
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008483 switch (t) {
8484 case 0:
8485 level = TP_EC_FAN_FULLSPEED;
8486 break;
8487 case 1:
8488 level = TPACPI_FAN_LAST_LEVEL;
8489 break;
8490 case 2:
8491 level = TP_EC_FAN_AUTO;
8492 break;
8493 case 3:
8494 /* reserved for software-controlled auto mode */
8495 return -ENOSYS;
8496 default:
8497 return -EINVAL;
8498 }
8499
8500 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008501 if (res == -ENXIO)
8502 return -EINVAL;
8503 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008504 return res;
8505
8506 fan_watchdog_reset();
8507
8508 return count;
8509}
8510
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008511static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8512 fan_pwm1_enable_show, fan_pwm1_enable_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008513
8514/* sysfs fan pwm1 ------------------------------------------------------ */
8515static ssize_t fan_pwm1_show(struct device *dev,
8516 struct device_attribute *attr,
8517 char *buf)
8518{
8519 int res;
8520 u8 status;
8521
8522 res = fan_get_status_safe(&status);
8523 if (res)
8524 return res;
8525
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008526 if ((status &
8527 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8528 status = fan_control_desired_level;
8529
8530 if (status > 7)
8531 status = 7;
8532
8533 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8534}
8535
8536static ssize_t fan_pwm1_store(struct device *dev,
8537 struct device_attribute *attr,
8538 const char *buf, size_t count)
8539{
8540 unsigned long s;
8541 int rc;
8542 u8 status, newlevel;
8543
8544 if (parse_strtoul(buf, 255, &s))
8545 return -EINVAL;
8546
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008547 tpacpi_disclose_usertask("hwmon pwm1",
8548 "set fan speed to %lu\n", s);
8549
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008550 /* scale down from 0-255 to 0-7 */
8551 newlevel = (s >> 5) & 0x07;
8552
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008553 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008554 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008555
8556 rc = fan_get_status(&status);
8557 if (!rc && (status &
8558 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8559 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008560 if (rc == -ENXIO)
8561 rc = -EINVAL;
8562 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008563 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008564 fan_watchdog_reset();
8565 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008566 }
8567
8568 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008569 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008570}
8571
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008572static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008573
8574/* sysfs fan fan1_input ------------------------------------------------ */
8575static ssize_t fan_fan1_input_show(struct device *dev,
8576 struct device_attribute *attr,
8577 char *buf)
8578{
8579 int res;
8580 unsigned int speed;
8581
8582 res = fan_get_speed(&speed);
8583 if (res < 0)
8584 return res;
8585
8586 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8587}
8588
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008589static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008590
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008591/* sysfs fan fan2_input ------------------------------------------------ */
8592static ssize_t fan_fan2_input_show(struct device *dev,
8593 struct device_attribute *attr,
8594 char *buf)
8595{
8596 int res;
8597 unsigned int speed;
8598
8599 res = fan2_get_speed(&speed);
8600 if (res < 0)
8601 return res;
8602
8603 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8604}
8605
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008606static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008607
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008608/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008609static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
8610 char *buf)
8611{
8612 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8613}
8614
8615static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8616 const char *buf, size_t count)
8617{
8618 unsigned long t;
8619
8620 if (parse_strtoul(buf, 120, &t))
8621 return -EINVAL;
8622
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008623 if (!fan_control_allowed)
8624 return -EPERM;
8625
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008626 fan_watchdog_maxinterval = t;
8627 fan_watchdog_reset();
8628
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008629 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8630
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008631 return count;
8632}
8633
8634static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8635 fan_fan_watchdog_show, fan_fan_watchdog_store);
8636
8637/* --------------------------------------------------------------------- */
8638static struct attribute *fan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008639 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8640 &dev_attr_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008641 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008642 NULL
8643};
8644
8645static const struct attribute_group fan_attr_group = {
8646 .attrs = fan_attributes,
8647};
8648
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008649#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8650#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008651
8652#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8653 { .vendor = PCI_VENDOR_ID_IBM, \
8654 .bios = TPACPI_MATCH_ANY, \
8655 .ec = TPID(__id1, __id2), \
8656 .quirks = __quirks }
8657
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008658#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8659 { .vendor = PCI_VENDOR_ID_LENOVO, \
8660 .bios = TPACPI_MATCH_ANY, \
8661 .ec = TPID(__id1, __id2), \
8662 .quirks = __quirks }
8663
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008664static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8665 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8666 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8667 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8668 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008669 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008670};
8671
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008672#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008673#undef TPACPI_FAN_QI
8674
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008675static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008676{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008677 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008678 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008679
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008680 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8681 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008682
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008683 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008684 fan_status_access_mode = TPACPI_FAN_NONE;
8685 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008686 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008687 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008688 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008689 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008690 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008691
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008692 if (tpacpi_is_ibm()) {
8693 TPACPI_ACPIHANDLE_INIT(fans);
8694 TPACPI_ACPIHANDLE_INIT(gfan);
8695 TPACPI_ACPIHANDLE_INIT(sfan);
8696 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008697
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008698 quirks = tpacpi_check_quirks(fan_quirk_table,
8699 ARRAY_SIZE(fan_quirk_table));
8700
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008701 if (gfan_handle) {
8702 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008703 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008704 } else {
8705 /* all other ThinkPads: note that even old-style
8706 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008707 if (likely(acpi_ec_read(fan_status_offset,
8708 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008709 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008710 if (quirks & TPACPI_FAN_Q1)
8711 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008712 if (quirks & TPACPI_FAN_2FAN) {
8713 tp_features.second_fan = 1;
8714 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8715 "secondary fan support enabled\n");
8716 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008717 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008718 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008719 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008720 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008721 }
8722 }
8723
8724 if (sfan_handle) {
8725 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008726 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008727 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008728 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008729 } else {
8730 if (!gfan_handle) {
8731 /* gfan without sfan means no fan control */
8732 /* all other models implement TP EC 0x2f control */
8733
8734 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008735 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008736 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008737 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008738 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008739 TPACPI_FAN_CMD_SPEED |
8740 TPACPI_FAN_CMD_LEVEL |
8741 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008742 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008743 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008744 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008745 TPACPI_FAN_CMD_LEVEL |
8746 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008747 }
8748 }
8749 }
8750
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008751 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8752 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008753 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8754 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8755 fan_status_access_mode, fan_control_access_mode);
8756
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008757 /* fan control master switch */
8758 if (!fan_control_allowed) {
8759 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8760 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008761 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008762 "fan control features disabled by parameter\n");
8763 }
8764
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008765 /* update fan_control_desired_level */
8766 if (fan_status_access_mode != TPACPI_FAN_NONE)
8767 fan_get_status_safe(NULL);
8768
8769 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8770 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008771 if (tp_features.second_fan) {
8772 /* attach second fan tachometer */
8773 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008774 &dev_attr_fan2_input.attr;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008775 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008776 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008777 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008778 if (rc < 0)
8779 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008780
8781 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8782 &driver_attr_fan_watchdog);
8783 if (rc < 0) {
8784 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8785 &fan_attr_group);
8786 return rc;
8787 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008788 return 0;
8789 } else
8790 return 1;
8791}
8792
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008793static void fan_exit(void)
8794{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008795 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008796 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008797
8798 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008799 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008800 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8801 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008802
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008803 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008804 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008805}
8806
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008807static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008808{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008809 int rc;
8810
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008811 if (!fan_control_allowed)
8812 return;
8813
8814 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008815 fan_control_resume_level = 0;
8816 rc = fan_get_status_safe(&fan_control_resume_level);
8817 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008818 pr_notice("failed to read fan level for later "
8819 "restore during resume: %d\n", rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008820
8821 /* if it is undefined, don't attempt to restore it.
8822 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008823 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008824 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008825}
8826
8827static void fan_resume(void)
8828{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008829 u8 current_level = 7;
8830 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008831 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008832
8833 /* DSDT *always* updates status on resume */
8834 tp_features.fan_ctrl_status_undef = 0;
8835
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008836 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008837 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008838 (fan_get_status_safe(&current_level) < 0))
8839 return;
8840
8841 switch (fan_control_access_mode) {
8842 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008843 /* never decrease fan level */
8844 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008845 break;
8846 case TPACPI_FAN_WR_ACPI_FANS:
8847 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008848 /* never decrease fan level, scale is:
8849 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8850 *
8851 * We expect the firmware to set either 7 or AUTO, but we
8852 * handle FULLSPEED out of paranoia.
8853 *
8854 * So, we can safely only restore FULLSPEED or 7, anything
8855 * else could slow the fan. Restoring AUTO is useless, at
8856 * best that's exactly what the DSDT already set (it is the
8857 * slower it uses).
8858 *
8859 * Always keep in mind that the DSDT *will* have set the
8860 * fans to what the vendor supposes is the best level. We
8861 * muck with it only to speed the fan up.
8862 */
8863 if (fan_control_resume_level != 7 &&
8864 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8865 return;
8866 else
8867 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8868 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008869 break;
8870 default:
8871 return;
8872 }
8873 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008874 pr_notice("restoring fan level to 0x%02x\n",
8875 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008876 rc = fan_set_level_safe(fan_control_resume_level);
8877 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008878 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008879 }
8880}
8881
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008882static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008883{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008884 int rc;
8885 u8 status;
8886 unsigned int speed = 0;
8887
8888 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008889 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008890 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008891 rc = fan_get_status_safe(&status);
8892 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008893 return rc;
8894
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008895 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008896 "level:\t\t%d\n",
8897 (status != 0) ? "enabled" : "disabled", status);
8898 break;
8899
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008900 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008901 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008902 rc = fan_get_status_safe(&status);
8903 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008904 return rc;
8905
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008906 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008907 (status != 0) ? "enabled" : "disabled");
8908
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008909 rc = fan_get_speed(&speed);
8910 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008911 return rc;
8912
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008913 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008914
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008915 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008916 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008917 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008918 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008919 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008920 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008921 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008922 break;
8923
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008924 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008925 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008926 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008927 }
8928
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008929 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008930 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008931
8932 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008933 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008934 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008935 break;
8936
8937 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008938 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008939 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008940 break;
8941 }
8942 }
8943
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008944 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008945 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008946 "commands:\twatchdog <timeout> (<timeout> "
8947 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008948
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008949 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008950 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008951 " (<speed> is 0-65535)\n");
8952
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008953 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008954}
8955
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008956static int fan_write_cmd_level(const char *cmd, int *rc)
8957{
8958 int level;
8959
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008960 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008961 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008962 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008963 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008964 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008965 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008966 return 0;
8967
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008968 *rc = fan_set_level_safe(level);
8969 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008970 pr_err("level command accepted for unsupported access mode %d\n",
8971 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008972 else if (!*rc)
8973 tpacpi_disclose_usertask("procfs fan",
8974 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008975
8976 return 1;
8977}
8978
8979static int fan_write_cmd_enable(const char *cmd, int *rc)
8980{
8981 if (strlencmp(cmd, "enable") != 0)
8982 return 0;
8983
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008984 *rc = fan_set_enable();
8985 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008986 pr_err("enable 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", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008990
8991 return 1;
8992}
8993
8994static int fan_write_cmd_disable(const char *cmd, int *rc)
8995{
8996 if (strlencmp(cmd, "disable") != 0)
8997 return 0;
8998
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008999 *rc = fan_set_disable();
9000 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009001 pr_err("disable command accepted for unsupported access mode %d\n",
9002 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009003 else if (!*rc)
9004 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009005
9006 return 1;
9007}
9008
9009static int fan_write_cmd_speed(const char *cmd, int *rc)
9010{
9011 int speed;
9012
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02009013 /* TODO:
9014 * Support speed <low> <medium> <high> ? */
9015
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009016 if (sscanf(cmd, "speed %d", &speed) != 1)
9017 return 0;
9018
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009019 *rc = fan_set_speed(speed);
9020 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07009021 pr_err("speed command accepted for unsupported access mode %d\n",
9022 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009023 else if (!*rc)
9024 tpacpi_disclose_usertask("procfs fan",
9025 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009026
9027 return 1;
9028}
9029
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009030static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9031{
9032 int interval;
9033
9034 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9035 return 0;
9036
9037 if (interval < 0 || interval > 120)
9038 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009039 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009040 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009041 tpacpi_disclose_usertask("procfs fan",
9042 "set watchdog timer to %d\n",
9043 interval);
9044 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009045
9046 return 1;
9047}
9048
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009049static int fan_write(char *buf)
9050{
9051 char *cmd;
9052 int rc = 0;
9053
9054 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009055 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009056 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009057 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009058 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009059 fan_write_cmd_disable(cmd, &rc) ||
9060 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009061 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009062 fan_write_cmd_speed(cmd, &rc))
9063 )
9064 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009065 else if (!rc)
9066 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009067 }
9068
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009069 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009070}
9071
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009072static struct ibm_struct fan_driver_data = {
9073 .name = "fan",
9074 .read = fan_read,
9075 .write = fan_write,
9076 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03009077 .suspend = fan_suspend,
9078 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009079};
9080
David Henningsson420f9732013-10-16 23:10:31 +02009081/*************************************************************************
9082 * Mute LED subdriver
9083 */
9084
9085
9086struct tp_led_table {
9087 acpi_string name;
9088 int on_value;
9089 int off_value;
9090 int state;
9091};
9092
9093static struct tp_led_table led_tables[] = {
9094 [TPACPI_LED_MUTE] = {
9095 .name = "SSMS",
9096 .on_value = 1,
9097 .off_value = 0,
9098 },
9099 [TPACPI_LED_MICMUTE] = {
9100 .name = "MMTS",
9101 .on_value = 2,
9102 .off_value = 0,
9103 },
9104};
9105
9106static int mute_led_on_off(struct tp_led_table *t, bool state)
9107{
9108 acpi_handle temp;
9109 int output;
9110
Axel Lin3526eca2016-09-19 09:33:51 +08009111 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009112 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9113 return -EIO;
9114 }
9115
9116 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9117 state ? t->on_value : t->off_value))
9118 return -EIO;
9119
9120 t->state = state;
9121 return state;
9122}
9123
9124int tpacpi_led_set(int whichled, bool on)
9125{
9126 struct tp_led_table *t;
9127
9128 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9129 return -EINVAL;
9130
9131 t = &led_tables[whichled];
9132 if (t->state < 0 || t->state == on)
9133 return t->state;
9134 return mute_led_on_off(t, on);
9135}
9136EXPORT_SYMBOL_GPL(tpacpi_led_set);
9137
9138static int mute_led_init(struct ibm_init_struct *iibm)
9139{
9140 acpi_handle temp;
9141 int i;
9142
9143 for (i = 0; i < TPACPI_LED_MAX; i++) {
9144 struct tp_led_table *t = &led_tables[i];
9145 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9146 mute_led_on_off(t, false);
9147 else
9148 t->state = -ENODEV;
9149 }
9150 return 0;
9151}
9152
9153static void mute_led_exit(void)
9154{
9155 int i;
9156
9157 for (i = 0; i < TPACPI_LED_MAX; i++)
9158 tpacpi_led_set(i, false);
9159}
9160
Takashi Iwai119f4492014-02-12 16:32:45 +01009161static void mute_led_resume(void)
9162{
9163 int i;
9164
9165 for (i = 0; i < TPACPI_LED_MAX; i++) {
9166 struct tp_led_table *t = &led_tables[i];
9167 if (t->state >= 0)
9168 mute_led_on_off(t, t->state);
9169 }
9170}
9171
David Henningsson420f9732013-10-16 23:10:31 +02009172static struct ibm_struct mute_led_driver_data = {
9173 .name = "mute_led",
9174 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01009175 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02009176};
9177
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009178/****************************************************************************
9179 ****************************************************************************
9180 *
9181 * Infrastructure
9182 *
9183 ****************************************************************************
9184 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07009185
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009186/*
9187 * HKEY event callout for other subdrivers go here
9188 * (yes, it is ugly, but it is quick, safe, and gets the job done
9189 */
9190static void tpacpi_driver_event(const unsigned int hkey_event)
9191{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00009192 if (ibm_backlight_device) {
9193 switch (hkey_event) {
9194 case TP_HKEY_EV_BRGHT_UP:
9195 case TP_HKEY_EV_BRGHT_DOWN:
9196 tpacpi_brightness_notify_change();
9197 }
9198 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009199 if (alsa_card) {
9200 switch (hkey_event) {
9201 case TP_HKEY_EV_VOL_UP:
9202 case TP_HKEY_EV_VOL_DOWN:
9203 case TP_HKEY_EV_VOL_MUTE:
9204 volume_alsa_notify_change();
9205 }
9206 }
Hans de Goedec685e202017-02-09 16:44:13 +01009207 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9208 enum led_brightness brightness;
9209
9210 mutex_lock(&kbdlight_mutex);
9211
9212 /*
9213 * Check the brightness actually changed, setting the brightness
9214 * through kbdlight_set_level() also triggers this event.
9215 */
9216 brightness = kbdlight_sysfs_get(NULL);
9217 if (kbdlight_brightness != brightness) {
9218 kbdlight_brightness = brightness;
9219 led_classdev_notify_brightness_hw_changed(
9220 &tpacpi_led_kbdlight.led_classdev, brightness);
9221 }
9222
9223 mutex_unlock(&kbdlight_mutex);
9224 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009225}
9226
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009227static void hotkey_driver_event(const unsigned int scancode)
9228{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03009229 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009230}
9231
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009232/* sysfs name ---------------------------------------------------------- */
9233static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
9234 struct device_attribute *attr,
9235 char *buf)
9236{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009237 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009238}
9239
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009240static DEVICE_ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009241
9242/* --------------------------------------------------------------------- */
9243
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009244/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03009245static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009246
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009247/*
9248 * Module and infrastructure proble, init and exit handling
9249 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009250
Rusty Russell90ab5ee2012-01-13 09:32:20 +10309251static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009252
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009253#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009254static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009255{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009256 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009257
Jan van den Berg72a979f2014-09-17 00:01:08 +02009258 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009259}
9260#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9261
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009262static void ibm_exit(struct ibm_struct *ibm)
9263{
9264 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9265
9266 list_del_init(&ibm->all_drivers);
9267
9268 if (ibm->flags.acpi_notify_installed) {
9269 dbg_printk(TPACPI_DBG_EXIT,
9270 "%s: acpi_remove_notify_handler\n", ibm->name);
9271 BUG_ON(!ibm->acpi);
9272 acpi_remove_notify_handler(*ibm->acpi->handle,
9273 ibm->acpi->type,
9274 dispatch_acpi_notify);
9275 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009276 }
9277
9278 if (ibm->flags.proc_created) {
9279 dbg_printk(TPACPI_DBG_EXIT,
9280 "%s: remove_proc_entry\n", ibm->name);
9281 remove_proc_entry(ibm->name, proc_dir);
9282 ibm->flags.proc_created = 0;
9283 }
9284
9285 if (ibm->flags.acpi_driver_registered) {
9286 dbg_printk(TPACPI_DBG_EXIT,
9287 "%s: acpi_bus_unregister_driver\n", ibm->name);
9288 BUG_ON(!ibm->acpi);
9289 acpi_bus_unregister_driver(ibm->acpi->driver);
9290 kfree(ibm->acpi->driver);
9291 ibm->acpi->driver = NULL;
9292 ibm->flags.acpi_driver_registered = 0;
9293 }
9294
9295 if (ibm->flags.init_called && ibm->exit) {
9296 ibm->exit();
9297 ibm->flags.init_called = 0;
9298 }
9299
9300 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9301}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009302
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009303static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009304{
9305 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009306 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009307 struct proc_dir_entry *entry;
9308
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009309 BUG_ON(ibm == NULL);
9310
9311 INIT_LIST_HEAD(&ibm->all_drivers);
9312
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009313 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009314 return 0;
9315
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009316 dbg_printk(TPACPI_DBG_INIT,
9317 "probing for %s\n", ibm->name);
9318
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009319 if (iibm->init) {
9320 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009321 if (ret > 0)
9322 return 0; /* probe failed */
9323 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009325
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009326 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009327 }
9328
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009329 if (ibm->acpi) {
9330 if (ibm->acpi->hid) {
9331 ret = register_tpacpi_subdriver(ibm);
9332 if (ret)
9333 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009334 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009335
9336 if (ibm->acpi->notify) {
9337 ret = setup_acpi_notify(ibm);
9338 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07009339 pr_notice("disabling subdriver %s\n",
9340 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009341 ret = 0;
9342 goto err_out;
9343 }
9344 if (ret < 0)
9345 goto err_out;
9346 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009347 }
9348
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009349 dbg_printk(TPACPI_DBG_INIT,
9350 "%s installed\n", ibm->name);
9351
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009352 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04009353 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009354
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009355 if (!mode)
9356 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009357 if (ibm->write)
9358 mode |= S_IWUSR;
9359 entry = proc_create_data(ibm->name, mode, proc_dir,
9360 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009361 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07009362 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009363 ret = -ENODEV;
9364 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009365 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009366 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009368
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009369 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9370
Linus Torvalds1da177e2005-04-16 15:20:36 -07009371 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009372
9373err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009374 dbg_printk(TPACPI_DBG_INIT,
9375 "%s: at error exit path with result %d\n",
9376 ibm->name, ret);
9377
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009378 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +02009379 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009380}
9381
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009382/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009383
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009384static bool __pure __init tpacpi_is_fw_digit(const char c)
9385{
9386 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9387}
9388
Jan van den Berg72a979f2014-09-17 00:01:08 +02009389static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009390 const char t)
9391{
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009392 /*
9393 * Most models: xxyTkkWW (#.##c)
9394 * Ancient 570/600 and -SL lacks (#.##c)
9395 */
9396 if (s && strlen(s) >= 8 &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009397 tpacpi_is_fw_digit(s[0]) &&
9398 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009399 s[2] == t &&
9400 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009401 tpacpi_is_fw_digit(s[4]) &&
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009402 tpacpi_is_fw_digit(s[5]))
9403 return true;
9404
9405 /* New models: xxxyTkkW (#.##c); T550 and some others */
9406 return s && strlen(s) >= 8 &&
9407 tpacpi_is_fw_digit(s[0]) &&
9408 tpacpi_is_fw_digit(s[1]) &&
9409 tpacpi_is_fw_digit(s[2]) &&
9410 s[3] == t &&
9411 (s[4] == 'T' || s[4] == 'N') &&
9412 tpacpi_is_fw_digit(s[5]) &&
9413 tpacpi_is_fw_digit(s[6]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009414}
9415
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009416/* returns 0 - probe ok, or < 0 - probe error.
9417 * Probe ok doesn't mean thinkpad found.
9418 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9419static int __must_check __init get_thinkpad_model_data(
9420 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009421{
Jeff Garzik18552562007-10-03 15:15:40 -04009422 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009423 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009424 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009425
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009426 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009427 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009428
9429 memset(tp, 0, sizeof(*tp));
9430
9431 if (dmi_name_in_vendors("IBM"))
9432 tp->vendor = PCI_VENDOR_ID_IBM;
9433 else if (dmi_name_in_vendors("LENOVO"))
9434 tp->vendor = PCI_VENDOR_ID_LENOVO;
9435 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009436 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009437
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009438 s = dmi_get_system_info(DMI_BIOS_VERSION);
9439 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9440 if (s && !tp->bios_version_str)
9441 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009442
9443 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009444 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9445 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009446 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009447
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009448 tp->bios_model = tp->bios_version_str[0]
9449 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009450 tp->bios_release = (tp->bios_version_str[4] << 8)
9451 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009452
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009453 /*
9454 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9455 * X32 or newer, all Z series; Some models must have an
9456 * up-to-date BIOS or they will not be detected.
9457 *
9458 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9459 */
9460 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009461 if (sscanf(dev->name,
9462 "IBM ThinkPad Embedded Controller -[%17c",
9463 ec_fw_string) == 1) {
9464 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9465 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009466
9467 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009468 if (!tp->ec_version_str)
9469 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009470
9471 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9472 tp->ec_model = ec_fw_string[0]
9473 | (ec_fw_string[1] << 8);
9474 tp->ec_release = (ec_fw_string[4] << 8)
9475 | ec_fw_string[5];
9476 } else {
Joe Perches0978e012011-04-04 10:06:25 -07009477 pr_notice("ThinkPad firmware release %s "
9478 "doesn't match the known patterns\n",
9479 ec_fw_string);
9480 pr_notice("please report this to %s\n",
9481 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009482 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009483 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009484 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009485 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009486
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009487 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009488 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009489 tp->model_str = kstrdup(s, GFP_KERNEL);
9490 if (!tp->model_str)
9491 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009492 } else {
9493 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009494 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009495 tp->model_str = kstrdup(s, GFP_KERNEL);
9496 if (!tp->model_str)
9497 return -ENOMEM;
9498 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009499 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03009500
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009501 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9502 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9503 if (s && !tp->nummodel_str)
9504 return -ENOMEM;
9505
9506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009507}
9508
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009509static int __init probe_for_thinkpad(void)
9510{
9511 int is_thinkpad;
9512
9513 if (acpi_disabled)
9514 return -ENODEV;
9515
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009516 /* It would be dangerous to run the driver in this case */
9517 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9518 return -ENODEV;
9519
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009520 /*
9521 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009522 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009523 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009524 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9525 (thinkpad_id.ec_model != 0) ||
9526 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009527
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03009528 /* The EC handler is required */
9529 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009530 if (!ec_handle) {
9531 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07009532 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009533 return -ENODEV;
9534 }
9535
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009536 if (!is_thinkpad && !force_load)
9537 return -ENODEV;
9538
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009539 return 0;
9540}
9541
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009542static void __init thinkpad_acpi_init_banner(void)
9543{
Joe Perches0978e012011-04-04 10:06:25 -07009544 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9545 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009546
Joe Perches0978e012011-04-04 10:06:25 -07009547 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009548 (thinkpad_id.bios_version_str) ?
9549 thinkpad_id.bios_version_str : "unknown",
9550 (thinkpad_id.ec_version_str) ?
9551 thinkpad_id.ec_version_str : "unknown");
9552
9553 BUG_ON(!thinkpad_id.vendor);
9554
9555 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07009556 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009557 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9558 "IBM" : ((thinkpad_id.vendor ==
9559 PCI_VENDOR_ID_LENOVO) ?
9560 "Lenovo" : "Unknown vendor"),
9561 thinkpad_id.model_str,
9562 (thinkpad_id.nummodel_str) ?
9563 thinkpad_id.nummodel_str : "unknown");
9564}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009565
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009566/* Module init, exit, parameters */
9567
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009568static struct ibm_init_struct ibms_init[] __initdata = {
9569 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009570 .data = &thinkpad_acpi_driver_data,
9571 },
9572 {
9573 .init = hotkey_init,
9574 .data = &hotkey_driver_data,
9575 },
9576 {
9577 .init = bluetooth_init,
9578 .data = &bluetooth_driver_data,
9579 },
9580 {
9581 .init = wan_init,
9582 .data = &wan_driver_data,
9583 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009584 {
9585 .init = uwb_init,
9586 .data = &uwb_driver_data,
9587 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009588#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009589 {
9590 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009591 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009592 .data = &video_driver_data,
9593 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009594#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009595 {
Pali Rohárbb28f3d52015-12-30 23:27:41 +01009596 .init = kbdlight_init,
9597 .data = &kbdlight_driver_data,
9598 },
9599 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009600 .init = light_init,
9601 .data = &light_driver_data,
9602 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009603 {
9604 .init = cmos_init,
9605 .data = &cmos_driver_data,
9606 },
9607 {
9608 .init = led_init,
9609 .data = &led_driver_data,
9610 },
9611 {
9612 .init = beep_init,
9613 .data = &beep_driver_data,
9614 },
9615 {
9616 .init = thermal_init,
9617 .data = &thermal_driver_data,
9618 },
9619 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009620 .init = brightness_init,
9621 .data = &brightness_driver_data,
9622 },
9623 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009624 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009625 .data = &volume_driver_data,
9626 },
9627 {
9628 .init = fan_init,
9629 .data = &fan_driver_data,
9630 },
David Henningsson420f9732013-10-16 23:10:31 +02009631 {
9632 .init = mute_led_init,
9633 .data = &mute_led_driver_data,
9634 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009635};
9636
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009637static int __init set_ibm_param(const char *val, struct kernel_param *kp)
9638{
9639 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009640 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009641
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009642 if (!kp || !kp->name || !val)
9643 return -EINVAL;
9644
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009645 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9646 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009647 WARN_ON(ibm == NULL);
9648
9649 if (!ibm || !ibm->name)
9650 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009651
9652 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9653 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009654 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009655 strcpy(ibms_init[i].param, val);
9656 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009657 return 0;
9658 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009659 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009660
9661 return -EINVAL;
9662}
9663
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009664module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009665MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009666 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009667
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009668module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009669MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009670
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009671module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009672MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009673 "Attempts to load the driver even on a "
9674 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009675
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009676module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009677MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009678 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03009679
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009680module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009681MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009682 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00009683 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03009684
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009685module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009686MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009687 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02009688
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009689#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009690module_param_named(volume_mode, volume_mode, uint, 0444);
9691MODULE_PARM_DESC(volume_mode,
9692 "Selects volume control strategy: "
9693 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9694
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02009695module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9696MODULE_PARM_DESC(volume_capabilities,
9697 "Selects the mixer capabilites: "
9698 "0=auto, 1=volume and mute, 2=mute only");
9699
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02009700module_param_named(volume_control, volume_control_allowed, bool, 0444);
9701MODULE_PARM_DESC(volume_control,
9702 "Enables software override for the console audio "
9703 "control when true");
9704
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07009705module_param_named(software_mute, software_mute_requested, bool, 0444);
9706MODULE_PARM_DESC(software_mute,
9707 "Request full software mute control");
9708
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009709/* ALSA module API parameters */
9710module_param_named(index, alsa_index, int, 0444);
9711MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9712module_param_named(id, alsa_id, charp, 0444);
9713MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9714module_param_named(enable, alsa_enable, bool, 0444);
9715MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009716#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009717
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009718#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009719 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02009720 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009721 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009722
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009723TPACPI_PARAM(hotkey);
9724TPACPI_PARAM(bluetooth);
9725TPACPI_PARAM(video);
9726TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009727TPACPI_PARAM(cmos);
9728TPACPI_PARAM(led);
9729TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009730TPACPI_PARAM(brightness);
9731TPACPI_PARAM(volume);
9732TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009733
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009734#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009735module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009736MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9737module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9738MODULE_PARM_DESC(wlsw_state,
9739 "Initial state of the emulated WLSW switch");
9740
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009741module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009742MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9743module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9744MODULE_PARM_DESC(bluetooth_state,
9745 "Initial state of the emulated bluetooth switch");
9746
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009747module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009748MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9749module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9750MODULE_PARM_DESC(wwan_state,
9751 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009752
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009753module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009754MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9755module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9756MODULE_PARM_DESC(uwb_state,
9757 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009758#endif
9759
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009760static void thinkpad_acpi_module_exit(void)
9761{
9762 struct ibm_struct *ibm, *itmp;
9763
9764 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9765
9766 list_for_each_entry_safe_reverse(ibm, itmp,
9767 &tpacpi_all_drivers,
9768 all_drivers) {
9769 ibm_exit(ibm);
9770 }
9771
9772 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9773
9774 if (tpacpi_inputdev) {
9775 if (tp_features.input_device_registered)
9776 input_unregister_device(tpacpi_inputdev);
9777 else
9778 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +10009779 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009780 }
9781
9782 if (tpacpi_hwmon)
9783 hwmon_device_unregister(tpacpi_hwmon);
9784
9785 if (tp_features.sensors_pdev_attrs_registered)
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009786 device_remove_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009787 if (tpacpi_sensors_pdev)
9788 platform_device_unregister(tpacpi_sensors_pdev);
9789 if (tpacpi_pdev)
9790 platform_device_unregister(tpacpi_pdev);
9791
9792 if (tp_features.sensors_pdrv_attrs_registered)
9793 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9794 if (tp_features.platform_drv_attrs_registered)
9795 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9796
9797 if (tp_features.sensors_pdrv_registered)
9798 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9799
9800 if (tp_features.platform_drv_registered)
9801 platform_driver_unregister(&tpacpi_pdriver);
9802
9803 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009804 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009805
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009806 if (tpacpi_wq)
9807 destroy_workqueue(tpacpi_wq);
9808
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009809 kfree(thinkpad_id.bios_version_str);
9810 kfree(thinkpad_id.ec_version_str);
9811 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +10009812 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009813}
9814
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009815
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009816static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009817{
9818 int ret, i;
9819
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009820 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9821
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009822 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009823
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009824 ret = get_thinkpad_model_data(&thinkpad_id);
9825 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009826 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009827 thinkpad_acpi_module_exit();
9828 return ret;
9829 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009830 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009831 if (ret) {
9832 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009833 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009835
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009836 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009837
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009838 thinkpad_acpi_init_banner();
9839 tpacpi_check_outdated_fw();
9840
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009841 TPACPI_ACPIHANDLE_INIT(ecrd);
9842 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009844 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9845 if (!tpacpi_wq) {
9846 thinkpad_acpi_module_exit();
9847 return -ENOMEM;
9848 }
9849
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009850 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009851 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07009852 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009853 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009854 return -ENODEV;
9855 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009856
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009857 ret = platform_driver_register(&tpacpi_pdriver);
9858 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009859 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009860 thinkpad_acpi_module_exit();
9861 return ret;
9862 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009863 tp_features.platform_drv_registered = 1;
9864
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009865 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9866 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009867 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009868 thinkpad_acpi_module_exit();
9869 return ret;
9870 }
9871 tp_features.sensors_pdrv_registered = 1;
9872
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009873 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009874 if (!ret) {
9875 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009876 ret = tpacpi_create_driver_attributes(
9877 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009878 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009879 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009880 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009881 thinkpad_acpi_module_exit();
9882 return ret;
9883 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009884 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009885
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009886
9887 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009888 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009889 NULL, 0);
9890 if (IS_ERR(tpacpi_pdev)) {
9891 ret = PTR_ERR(tpacpi_pdev);
9892 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009893 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009894 thinkpad_acpi_module_exit();
9895 return ret;
9896 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009897 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009898 TPACPI_HWMON_DRVR_NAME,
9899 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009900 if (IS_ERR(tpacpi_sensors_pdev)) {
9901 ret = PTR_ERR(tpacpi_sensors_pdev);
9902 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009903 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009904 thinkpad_acpi_module_exit();
9905 return ret;
9906 }
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009907 ret = device_create_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009908 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009909 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009910 thinkpad_acpi_module_exit();
9911 return ret;
9912 }
9913 tp_features.sensors_pdev_attrs_registered = 1;
9914 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009915 if (IS_ERR(tpacpi_hwmon)) {
9916 ret = PTR_ERR(tpacpi_hwmon);
9917 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009918 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009919 thinkpad_acpi_module_exit();
9920 return ret;
9921 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009922 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009923 tpacpi_inputdev = input_allocate_device();
9924 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009925 thinkpad_acpi_module_exit();
9926 return -ENOMEM;
9927 } else {
9928 /* Prepare input device, but don't register */
9929 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009930 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009931 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009932 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009933 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9934 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009935 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009936 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009937
9938 /* Init subdriver dependencies */
9939 tpacpi_detect_brightness_capabilities();
9940
9941 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009942 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9943 ret = ibm_init(&ibms_init[i]);
9944 if (ret >= 0 && *ibms_init[i].param)
9945 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009946 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009947 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009948 return ret;
9949 }
9950 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009951
9952 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9953
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009954 ret = input_register_device(tpacpi_inputdev);
9955 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009956 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009957 thinkpad_acpi_module_exit();
9958 return ret;
9959 } else {
9960 tp_features.input_device_registered = 1;
9961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009962
9963 return 0;
9964}
9965
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009966MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9967
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009968/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009969 * This will autoload the driver in almost every ThinkPad
9970 * in widespread use.
9971 *
9972 * Only _VERY_ old models, like the 240, 240x and 570 lack
9973 * the HKEY event interface.
9974 */
9975MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9976
9977/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009978 * DMI matching for module autoloading
9979 *
9980 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9981 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9982 *
9983 * Only models listed in thinkwiki will be supported, so add yours
9984 * if it is not there yet.
9985 */
9986#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009987 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009988
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009989/* Ancient thinkpad BIOSes have to be identified by
9990 * BIOS type or model number, and there are far less
9991 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009992IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009993
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009994MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9995MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009996MODULE_DESCRIPTION(TPACPI_DESC);
9997MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009998MODULE_LICENSE("GPL");
9999
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -030010000module_init(thinkpad_acpi_module_init);
10001module_exit(thinkpad_acpi_module_exit);