blob: 69cb0da95be91bfb01ece405ff615fa7aa5bb83f [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>
Lv Zheng8b484632013-12-03 08:49:16 +080085#include <asm/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 */
166 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
167 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
168 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
169
170 /* Reasons for waking up from S3/S4 */
171 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
172 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
173 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
174 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
175 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
176 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
177
178 /* Auto-sleep after eject request */
179 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
180 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
181
182 /* Misc bay events */
183 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -0300184 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
185 or port replicator */
186 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
187 dock or port replicator */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300188
189 /* User-interface events */
190 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
191 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
192 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
193 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
Lyudeb03f4d42016-11-11 15:15:03 -0500194 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
195 * enter/leave tablet mode
196 */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300197 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
198 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
199 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
200
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300201 /* Key-related user-interface events */
202 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
203 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
Xavier Naveira67ab6242015-02-10 08:45:18 +0100204 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300205
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300206 /* Thermal events */
207 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
208 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
209 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
210 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
211 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
212
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100213 /* AC-related events */
214 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300215
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300216 /* Misc */
217 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
218};
219
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200220/****************************************************************************
221 * Main driver
222 */
223
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200224#define TPACPI_NAME "thinkpad"
225#define TPACPI_DESC "ThinkPad ACPI Extras"
226#define TPACPI_FILE TPACPI_NAME "_acpi"
227#define TPACPI_URL "http://ibm-acpi.sf.net/"
228#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200229
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200230#define TPACPI_PROC_DIR "ibm"
231#define TPACPI_ACPI_EVENT_PREFIX "ibm"
232#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300233#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200234#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200235
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300236#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300237#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300238
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200239#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200240
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000241/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200242#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000243#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200244#define TPACPI_DBG_INIT 0x0001
245#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +0000246#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000247#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000248#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000249#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200250#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200251
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200252#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
253#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
254#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200255
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200256
257/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200258 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200259 */
260
261struct ibm_struct;
262
263struct tp_acpi_drv_struct {
264 const struct acpi_device_id *hid;
265 struct acpi_driver *driver;
266
267 void (*notify) (struct ibm_struct *, u32);
268 acpi_handle *handle;
269 u32 type;
270 struct acpi_device *device;
271};
272
273struct ibm_struct {
274 char *name;
275
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200276 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200277 int (*write) (char *);
278 void (*exit) (void);
279 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200280 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200281 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200282
283 struct list_head all_drivers;
284
285 struct tp_acpi_drv_struct *acpi;
286
287 struct {
288 u8 acpi_driver_registered:1;
289 u8 acpi_notify_installed:1;
290 u8 proc_created:1;
291 u8 init_called:1;
292 u8 experimental:1;
293 } flags;
294};
295
296struct ibm_init_struct {
297 char param[32];
298
299 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400300 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200301 struct ibm_struct *data;
302};
303
304static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200305 u32 bluetooth:1;
306 u32 hotkey:1;
307 u32 hotkey_mask:1;
308 u32 hotkey_wlsw:1;
Lyudeb3180022016-11-11 15:15:02 -0500309 enum {
310 TP_HOTKEY_TABLET_NONE = 0,
311 TP_HOTKEY_TABLET_USES_MHKG,
Lyudeb03f4d42016-11-11 15:15:03 -0500312 /* X1 Yoga 2016, seen on BIOS N1FET44W */
313 TP_HOTKEY_TABLET_USES_CMMD,
Lyudeb3180022016-11-11 15:15:02 -0500314 } hotkey_tablet;
Pali Rohárbb28f3d52015-12-30 23:27:41 +0100315 u32 kbdlight:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200316 u32 light:1;
317 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300318 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300319 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200320 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200321 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200322 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300323 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300324 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200325 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200326 u32 input_device_registered:1;
327 u32 platform_drv_registered:1;
328 u32 platform_drv_attrs_registered:1;
329 u32 sensors_pdrv_registered:1;
330 u32 sensors_pdrv_attrs_registered:1;
331 u32 sensors_pdev_attrs_registered:1;
332 u32 hotkey_poll_active:1;
Bastien Noceraf23a5bc2015-03-02 14:45:16 +0100333 u32 has_adaptive_kbd:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200334} tp_features;
335
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300336static struct {
337 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200338 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300339} tp_warned;
340
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200341struct thinkpad_id_data {
342 unsigned int vendor; /* ThinkPad vendor:
343 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
344
345 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
346 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
347
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300348 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200349 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300350 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
351 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200352
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300353 char *model_str; /* ThinkPad T43 */
354 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200355};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200356static struct thinkpad_id_data thinkpad_id;
357
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300358static enum {
359 TPACPI_LIFE_INIT = 0,
360 TPACPI_LIFE_RUNNING,
361 TPACPI_LIFE_EXITING,
362} tpacpi_lifecycle;
363
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200364static int experimental;
365static u32 dbg_level;
366
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300367static struct workqueue_struct *tpacpi_wq;
368
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000369enum led_status_t {
370 TPACPI_LED_OFF = 0,
371 TPACPI_LED_ON,
372 TPACPI_LED_BLINK,
373};
374
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300375/* Special LED class that can defer work */
376struct tpacpi_led_classdev {
377 struct led_classdev led_classdev;
378 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000379 enum led_status_t new_state;
Adam Leeedf2d772013-06-08 16:51:15 +0800380 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300381};
382
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300383/* brightness level capabilities */
384static unsigned int bright_maxlvl; /* 0 = unknown */
385
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200386#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
387static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030388static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200389static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030390static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200391static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030392static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200393static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030394static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200395#endif
396
397
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000398/*************************************************************************
399 * Debugging helpers
400 */
401
Joe Perches0978e012011-04-04 10:06:25 -0700402#define dbg_printk(a_dbg_level, format, arg...) \
403do { \
404 if (dbg_level & (a_dbg_level)) \
405 printk(KERN_DEBUG pr_fmt("%s: " format), \
406 __func__, ##arg); \
407} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000408
409#ifdef CONFIG_THINKPAD_ACPI_DEBUG
410#define vdbg_printk dbg_printk
411static const char *str_supported(int is_supported);
412#else
Joe Perches0978e012011-04-04 10:06:25 -0700413static inline const char *str_supported(int is_supported) { return ""; }
414#define vdbg_printk(a_dbg_level, format, arg...) \
Joe Perchesefd85cf2015-08-26 11:13:38 -0700415 do { if (0) no_printk(format, ##arg); } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000416#endif
417
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000418static void tpacpi_log_usertask(const char * const what)
419{
Joe Perches0978e012011-04-04 10:06:25 -0700420 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
421 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000422}
423
Joe Perches0978e012011-04-04 10:06:25 -0700424#define tpacpi_disclose_usertask(what, format, arg...) \
425do { \
426 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
427 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
428 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
429 what, task_tgid_vnr(current), ## arg); \
430 } \
431} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000432
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300433/*
434 * Quirk handling helpers
435 *
436 * ThinkPad IDs and versions seen in the field so far
437 * are two-characters from the set [0-9A-Z], i.e. base 36.
438 *
439 * We use values well outside that range as specials.
440 */
441
442#define TPACPI_MATCH_ANY 0xffffU
443#define TPACPI_MATCH_UNKNOWN 0U
444
445/* TPID('1', 'Y') == 0x5931 */
446#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
447
448#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
449 { .vendor = PCI_VENDOR_ID_IBM, \
450 .bios = TPID(__id1, __id2), \
451 .ec = TPACPI_MATCH_ANY, \
452 .quirks = (__quirk) }
453
454#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
455 { .vendor = PCI_VENDOR_ID_LENOVO, \
456 .bios = TPID(__id1, __id2), \
457 .ec = TPACPI_MATCH_ANY, \
458 .quirks = (__quirk) }
459
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200460#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
461 { .vendor = PCI_VENDOR_ID_LENOVO, \
462 .bios = TPACPI_MATCH_ANY, \
463 .ec = TPID(__id1, __id2), \
464 .quirks = (__quirk) }
465
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300466struct tpacpi_quirk {
467 unsigned int vendor;
468 u16 bios;
469 u16 ec;
470 unsigned long quirks;
471};
472
473/**
474 * tpacpi_check_quirks() - search BIOS/EC version on a list
475 * @qlist: array of &struct tpacpi_quirk
476 * @qlist_size: number of elements in @qlist
477 *
478 * Iterates over a quirks list until one is found that matches the
479 * ThinkPad's vendor, BIOS and EC model.
480 *
481 * Returns 0 if nothing matches, otherwise returns the quirks field of
482 * the matching &struct tpacpi_quirk entry.
483 *
484 * The match criteria is: vendor, ec and bios much match.
485 */
486static unsigned long __init tpacpi_check_quirks(
487 const struct tpacpi_quirk *qlist,
488 unsigned int qlist_size)
489{
490 while (qlist_size) {
491 if ((qlist->vendor == thinkpad_id.vendor ||
492 qlist->vendor == TPACPI_MATCH_ANY) &&
493 (qlist->bios == thinkpad_id.bios_model ||
494 qlist->bios == TPACPI_MATCH_ANY) &&
495 (qlist->ec == thinkpad_id.ec_model ||
496 qlist->ec == TPACPI_MATCH_ANY))
497 return qlist->quirks;
498
499 qlist_size--;
500 qlist++;
501 }
502 return 0;
503}
504
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300505static inline bool __pure __init tpacpi_is_lenovo(void)
506{
507 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
508}
509
510static inline bool __pure __init tpacpi_is_ibm(void)
511{
512 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
513}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300514
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300515/****************************************************************************
516 ****************************************************************************
517 *
518 * ACPI Helpers and device model
519 *
520 ****************************************************************************
521 ****************************************************************************/
522
523/*************************************************************************
524 * ACPI basic handles
525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300527static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300528static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200530#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700532 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300533 &parent##_handle; \
534 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200536TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
537TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200539TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
540 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400541 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
542 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300543 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400544
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200545TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400546 "^HKEY", /* R30, R31 */
547 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300548 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400549
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300550/*************************************************************************
551 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200552 */
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700555 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400558 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200559 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400560 struct acpi_buffer result, *resultp;
561 union acpi_object out_obj;
562 acpi_status status;
563 va_list ap;
564 char res_type;
565 int success;
566 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700569 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return 0;
571 }
572
573 if (*fmt == 'q') {
574 quiet = 1;
575 fmt++;
576 } else
577 quiet = 0;
578
579 res_type = *(fmt++);
580
581 params.count = 0;
582 params.pointer = &in_objs[0];
583
584 va_start(ap, fmt);
585 while (*fmt) {
586 char c = *(fmt++);
587 switch (c) {
588 case 'd': /* int */
589 in_objs[params.count].integer.value = va_arg(ap, int);
590 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
591 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400592 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 default:
Joe Perches0978e012011-04-04 10:06:25 -0700594 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 "with invalid format character '%c'\n", c);
Jesper Juhl21365852010-12-24 19:56:28 +0100596 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return 0;
598 }
599 }
600 va_end(ap);
601
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400602 if (res_type != 'v') {
603 result.length = sizeof(out_obj);
604 result.pointer = &out_obj;
605 resultp = &result;
606 } else
607 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400609 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400612 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300613 success = (status == AE_OK &&
614 out_obj.type == ACPI_TYPE_INTEGER);
615 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700616 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400618 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 success = status == AE_OK;
620 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400621 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 default:
Joe Perches0978e012011-04-04 10:06:25 -0700623 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 "with invalid format character '%c'\n", res_type);
625 return 0;
626 }
627
628 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700629 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300630 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 return success;
633}
634
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200635static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300636{
637 int v;
638
639 if (ecrd_handle) {
640 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
641 return 0;
642 *p = v;
643 } else {
644 if (ec_read(i, p) < 0)
645 return 0;
646 }
647
648 return 1;
649}
650
651static int acpi_ec_write(int i, u8 v)
652{
653 if (ecwr_handle) {
654 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
655 return 0;
656 } else {
657 if (ec_write(i, v) < 0)
658 return 0;
659 }
660
661 return 1;
662}
663
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300664static int issue_thinkpad_cmos_command(int cmos_cmd)
665{
666 if (!cmos_handle)
667 return -ENXIO;
668
669 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
670 return -EIO;
671
672 return 0;
673}
674
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300675/*************************************************************************
676 * ACPI device model
677 */
678
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200679#define TPACPI_ACPIHANDLE_INIT(object) \
680 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300681 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200682
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300683static void __init drv_acpi_handle_init(const char *name,
684 acpi_handle *handle, const acpi_handle parent,
685 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300686{
687 int i;
688 acpi_status status;
689
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300690 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
691 name);
692
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300693 for (i = 0; i < num_paths; i++) {
694 status = acpi_get_handle(parent, paths[i], handle);
695 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300696 dbg_printk(TPACPI_DBG_INIT,
697 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300698 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300699 return;
700 }
701 }
702
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300703 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
704 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300705 *handle = NULL;
706}
707
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300708static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
709 u32 level, void *context, void **return_value)
710{
Aaron Lu46445b62013-10-11 21:27:46 +0800711 struct acpi_device *dev;
712 if (!strcmp(context, "video")) {
713 if (acpi_bus_get_device(handle, &dev))
714 return AE_OK;
715 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
716 return AE_OK;
717 }
718
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300719 *(acpi_handle *)return_value = handle;
720
721 return AE_CTRL_TERMINATE;
722}
723
724static void __init tpacpi_acpi_handle_locate(const char *name,
725 const char *hid,
726 acpi_handle *handle)
727{
728 acpi_status status;
729 acpi_handle device_found;
730
Aaron Lu46445b62013-10-11 21:27:46 +0800731 BUG_ON(!name || !handle);
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300732 vdbg_printk(TPACPI_DBG_INIT,
733 "trying to locate ACPI handle for %s, using HID %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +0800734 name, hid ? hid : "NULL");
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300735
736 memset(&device_found, 0, sizeof(device_found));
737 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
738 (void *)name, &device_found);
739
740 *handle = NULL;
741
742 if (ACPI_SUCCESS(status)) {
743 *handle = device_found;
744 dbg_printk(TPACPI_DBG_INIT,
745 "Found ACPI handle for %s\n", name);
746 } else {
747 vdbg_printk(TPACPI_DBG_INIT,
748 "Could not locate an ACPI handle for %s: %s\n",
749 name, acpi_format_exception(status));
750 }
751}
752
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300753static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300754{
755 struct ibm_struct *ibm = data;
756
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300757 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
758 return;
759
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300760 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300761 return;
762
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300763 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300764}
765
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300766static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300767{
768 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300769 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300770
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300771 BUG_ON(!ibm->acpi);
772
773 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300774 return 0;
775
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300776 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300777 "setting up ACPI notify for %s\n", ibm->name);
778
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300779 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
780 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700781 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300782 return -ENODEV;
783 }
784
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700785 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300786 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200787 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300788 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300789
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300790 status = acpi_install_notify_handler(*ibm->acpi->handle,
791 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300792 if (ACPI_FAILURE(status)) {
793 if (status == AE_ALREADY_EXISTS) {
Joe Perches0978e012011-04-04 10:06:25 -0700794 pr_notice("another device driver is already "
795 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300796 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700797 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300798 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300799 }
800 return -ENODEV;
801 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300802 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300803 return 0;
804}
805
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300806static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300807{
808 return 0;
809}
810
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300811static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300812{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300813 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300814
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300815 dbg_printk(TPACPI_DBG_INIT,
816 "registering %s as an ACPI driver\n", ibm->name);
817
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300818 BUG_ON(!ibm->acpi);
819
820 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
821 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700822 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300823 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300824 }
825
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200826 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300827 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200828
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300829 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300830
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300831 rc = acpi_bus_register_driver(ibm->acpi->driver);
832 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700833 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200834 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300835 kfree(ibm->acpi->driver);
836 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300837 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300838 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300839
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300840 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300841}
842
843
844/****************************************************************************
845 ****************************************************************************
846 *
847 * Procfs Helpers
848 *
849 ****************************************************************************
850 ****************************************************************************/
851
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200852static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300853{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200854 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300855
856 if (!ibm || !ibm->read)
857 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200858 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300859}
860
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200861static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300862{
Al Virod9dda782013-03-31 18:16:14 -0400863 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200864}
865
866static ssize_t dispatch_proc_write(struct file *file,
867 const char __user *userbuf,
868 size_t count, loff_t *pos)
869{
Al Virod9dda782013-03-31 18:16:14 -0400870 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300871 char *kernbuf;
872 int ret;
873
874 if (!ibm || !ibm->write)
875 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300876 if (count > PAGE_SIZE - 2)
877 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300878
879 kernbuf = kmalloc(count + 2, GFP_KERNEL);
880 if (!kernbuf)
881 return -ENOMEM;
882
883 if (copy_from_user(kernbuf, userbuf, count)) {
884 kfree(kernbuf);
885 return -EFAULT;
886 }
887
888 kernbuf[count] = 0;
889 strcat(kernbuf, ",");
890 ret = ibm->write(kernbuf);
891 if (ret == 0)
892 ret = count;
893
894 kfree(kernbuf);
895
896 return ret;
897}
898
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200899static const struct file_operations dispatch_proc_fops = {
900 .owner = THIS_MODULE,
901 .open = dispatch_proc_open,
902 .read = seq_read,
903 .llseek = seq_lseek,
904 .release = single_release,
905 .write = dispatch_proc_write,
906};
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908static char *next_cmd(char **cmds)
909{
910 char *start = *cmds;
911 char *end;
912
913 while ((end = strchr(start, ',')) && end == start)
914 start = end + 1;
915
916 if (!end)
917 return NULL;
918
919 *end = 0;
920 *cmds = end + 1;
921 return start;
922}
923
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300924
925/****************************************************************************
926 ****************************************************************************
927 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300928 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300929 *
930 ****************************************************************************
931 ****************************************************************************/
932
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300933static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300934static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700935static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300936static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300937static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200938static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300939
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200940#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200941static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200942{
943 struct ibm_struct *ibm, *itmp;
944
945 list_for_each_entry_safe(ibm, itmp,
946 &tpacpi_all_drivers,
947 all_drivers) {
948 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200949 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200950 }
951
952 return 0;
953}
954
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200955static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300956{
957 struct ibm_struct *ibm, *itmp;
958
959 list_for_each_entry_safe(ibm, itmp,
960 &tpacpi_all_drivers,
961 all_drivers) {
962 if (ibm->resume)
963 (ibm->resume)();
964 }
965
966 return 0;
967}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200968#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300969
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200970static SIMPLE_DEV_PM_OPS(tpacpi_pm,
971 tpacpi_suspend_handler, tpacpi_resume_handler);
972
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200973static void tpacpi_shutdown_handler(struct platform_device *pdev)
974{
975 struct ibm_struct *ibm, *itmp;
976
977 list_for_each_entry_safe(ibm, itmp,
978 &tpacpi_all_drivers,
979 all_drivers) {
980 if (ibm->shutdown)
981 (ibm->shutdown)();
982 }
983}
984
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300985static struct platform_driver tpacpi_pdriver = {
986 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200987 .name = TPACPI_DRVR_NAME,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200988 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300989 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200990 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300991};
992
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300993static struct platform_driver tpacpi_hwmon_pdriver = {
994 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200995 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300996 },
997};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300998
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300999/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001000 * sysfs support helpers
1001 */
1002
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001003struct attribute_set {
1004 unsigned int members, max_members;
1005 struct attribute_group group;
1006};
1007
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001008struct attribute_set_obj {
1009 struct attribute_set s;
1010 struct attribute *a;
1011} __attribute__((packed));
1012
1013static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001014 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001015{
1016 struct attribute_set_obj *sobj;
1017
1018 if (max_members == 0)
1019 return NULL;
1020
1021 /* Allocates space for implicit NULL at the end too */
1022 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1023 max_members * sizeof(struct attribute *),
1024 GFP_KERNEL);
1025 if (!sobj)
1026 return NULL;
1027 sobj->s.max_members = max_members;
1028 sobj->s.group.attrs = &sobj->a;
1029 sobj->s.group.name = name;
1030
1031 return &sobj->s;
1032}
1033
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001034#define destroy_attr_set(_set) \
1035 kfree(_set);
1036
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001037/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001038static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001039{
1040 if (!s || !attr)
1041 return -EINVAL;
1042
1043 if (s->members >= s->max_members)
1044 return -ENOMEM;
1045
1046 s->group.attrs[s->members] = attr;
1047 s->members++;
1048
1049 return 0;
1050}
1051
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001052static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001053 struct attribute **attr,
1054 unsigned int count)
1055{
1056 int i, res;
1057
1058 for (i = 0; i < count; i++) {
1059 res = add_to_attr_set(s, attr[i]);
1060 if (res)
1061 return res;
1062 }
1063
1064 return 0;
1065}
1066
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001067static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001068{
1069 sysfs_remove_group(kobj, &s->group);
1070 destroy_attr_set(s);
1071}
1072
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001073#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1074 sysfs_create_group(_kobj, &_attr_set->group)
1075
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001076static int parse_strtoul(const char *buf,
1077 unsigned long max, unsigned long *value)
1078{
1079 char *endp;
1080
André Goddard Rosae7d28602009-12-14 18:01:06 -08001081 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1082 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001083 if (*endp || *value > max)
1084 return -EINVAL;
1085
1086 return 0;
1087}
1088
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001089static void tpacpi_disable_brightness_delay(void)
1090{
1091 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001092 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001093}
1094
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001095static void printk_deprecated_attribute(const char * const what,
1096 const char * const details)
1097{
1098 tpacpi_log_usertask("deprecated sysfs attribute");
Joe Perches0978e012011-04-04 10:06:25 -07001099 pr_warn("WARNING: sysfs attribute %s is deprecated and "
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001100 "will be removed. %s\n",
1101 what, details);
1102}
1103
Johannes Berg19d337d2009-06-02 13:01:37 +02001104/*************************************************************************
1105 * rfkill and radio control support helpers
1106 */
1107
1108/*
1109 * ThinkPad-ACPI firmware handling model:
1110 *
1111 * WLSW (master wireless switch) is event-driven, and is common to all
1112 * firmware-controlled radios. It cannot be controlled, just monitored,
1113 * as expected. It overrides all radio state in firmware
1114 *
1115 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1116 * (TODO: verify how WLSW interacts with the returned radio state).
1117 *
1118 * The only time there are shadow radio state changes, is when
1119 * masked-off hotkeys are used.
1120 */
1121
1122/*
1123 * Internal driver API for radio state:
1124 *
1125 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1126 * bool: true means radio blocked (off)
1127 */
1128enum tpacpi_rfkill_state {
1129 TPACPI_RFK_RADIO_OFF = 0,
1130 TPACPI_RFK_RADIO_ON
1131};
1132
1133/* rfkill switches */
1134enum tpacpi_rfk_id {
1135 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1136 TPACPI_RFK_WWAN_SW_ID,
1137 TPACPI_RFK_UWB_SW_ID,
1138 TPACPI_RFK_SW_MAX
1139};
1140
1141static const char *tpacpi_rfkill_names[] = {
1142 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1143 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1144 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1145 [TPACPI_RFK_SW_MAX] = NULL
1146};
1147
1148/* ThinkPad-ACPI rfkill subdriver */
1149struct tpacpi_rfk {
1150 struct rfkill *rfkill;
1151 enum tpacpi_rfk_id id;
1152 const struct tpacpi_rfk_ops *ops;
1153};
1154
1155struct tpacpi_rfk_ops {
1156 /* firmware interface */
1157 int (*get_status)(void);
1158 int (*set_status)(const enum tpacpi_rfkill_state);
1159};
1160
1161static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1162
1163/* Query FW and update rfkill sw state for a given rfkill switch */
1164static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1165{
1166 int status;
1167
1168 if (!tp_rfk)
1169 return -ENODEV;
1170
1171 status = (tp_rfk->ops->get_status)();
1172 if (status < 0)
1173 return status;
1174
1175 rfkill_set_sw_state(tp_rfk->rfkill,
1176 (status == TPACPI_RFK_RADIO_OFF));
1177
1178 return status;
1179}
1180
1181/* Query FW and update rfkill sw state for all rfkill switches */
1182static void tpacpi_rfk_update_swstate_all(void)
1183{
1184 unsigned int i;
1185
1186 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1187 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1188}
1189
1190/*
1191 * Sync the HW-blocking state of all rfkill switches,
1192 * do notice it causes the rfkill core to schedule uevents
1193 */
1194static void tpacpi_rfk_update_hwblock_state(bool blocked)
1195{
1196 unsigned int i;
1197 struct tpacpi_rfk *tp_rfk;
1198
1199 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1200 tp_rfk = tpacpi_rfkill_switches[i];
1201 if (tp_rfk) {
1202 if (rfkill_set_hw_state(tp_rfk->rfkill,
1203 blocked)) {
1204 /* ignore -- we track sw block */
1205 }
1206 }
1207 }
1208}
1209
1210/* Call to get the WLSW state from the firmware */
1211static int hotkey_get_wlsw(void);
1212
1213/* Call to query WLSW state and update all rfkill switches */
1214static bool tpacpi_rfk_check_hwblock_state(void)
1215{
1216 int res = hotkey_get_wlsw();
1217 int hw_blocked;
1218
1219 /* When unknown or unsupported, we have to assume it is unblocked */
1220 if (res < 0)
1221 return false;
1222
1223 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1224 tpacpi_rfk_update_hwblock_state(hw_blocked);
1225
1226 return hw_blocked;
1227}
1228
1229static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1230{
1231 struct tpacpi_rfk *tp_rfk = data;
1232 int res;
1233
1234 dbg_printk(TPACPI_DBG_RFKILL,
1235 "request to change radio state to %s\n",
1236 blocked ? "blocked" : "unblocked");
1237
1238 /* try to set radio state */
1239 res = (tp_rfk->ops->set_status)(blocked ?
1240 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1241
1242 /* and update the rfkill core with whatever the FW really did */
1243 tpacpi_rfk_update_swstate(tp_rfk);
1244
1245 return (res < 0) ? res : 0;
1246}
1247
1248static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1249 .set_block = tpacpi_rfk_hook_set_block,
1250};
1251
1252static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1253 const struct tpacpi_rfk_ops *tp_rfkops,
1254 const enum rfkill_type rfktype,
1255 const char *name,
1256 const bool set_default)
1257{
1258 struct tpacpi_rfk *atp_rfk;
1259 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001260 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001261 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001262 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001263
1264 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1265
Johannes Berg19d337d2009-06-02 13:01:37 +02001266 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1267 if (atp_rfk)
1268 atp_rfk->rfkill = rfkill_alloc(name,
1269 &tpacpi_pdev->dev,
1270 rfktype,
1271 &tpacpi_rfk_rfkill_ops,
1272 atp_rfk);
1273 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001274 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001275 kfree(atp_rfk);
1276 return -ENOMEM;
1277 }
1278
1279 atp_rfk->id = id;
1280 atp_rfk->ops = tp_rfkops;
1281
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001282 sw_status = (tp_rfkops->get_status)();
1283 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001284 pr_err("failed to read initial state for %s, error %d\n",
1285 name, sw_status);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001286 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001287 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001288 if (set_default) {
1289 /* try to keep the initial state, since we ask the
1290 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001291 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001292 }
1293 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001294 hw_state = tpacpi_rfk_check_hwblock_state();
1295 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001296
1297 res = rfkill_register(atp_rfk->rfkill);
1298 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001299 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001300 rfkill_destroy(atp_rfk->rfkill);
1301 kfree(atp_rfk);
1302 return res;
1303 }
1304
1305 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001306
Joe Perches0978e012011-04-04 10:06:25 -07001307 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001308 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001309 return 0;
1310}
1311
1312static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1313{
1314 struct tpacpi_rfk *tp_rfk;
1315
1316 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1317
1318 tp_rfk = tpacpi_rfkill_switches[id];
1319 if (tp_rfk) {
1320 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001321 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001322 tpacpi_rfkill_switches[id] = NULL;
1323 kfree(tp_rfk);
1324 }
1325}
1326
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001327static void printk_deprecated_rfkill_attribute(const char * const what)
1328{
1329 printk_deprecated_attribute(what,
1330 "Please switch to generic rfkill before year 2010");
1331}
1332
Johannes Berg19d337d2009-06-02 13:01:37 +02001333/* sysfs <radio> enable ------------------------------------------------ */
1334static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1335 struct device_attribute *attr,
1336 char *buf)
1337{
1338 int status;
1339
1340 printk_deprecated_rfkill_attribute(attr->attr.name);
1341
1342 /* This is in the ABI... */
1343 if (tpacpi_rfk_check_hwblock_state()) {
1344 status = TPACPI_RFK_RADIO_OFF;
1345 } else {
1346 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1347 if (status < 0)
1348 return status;
1349 }
1350
1351 return snprintf(buf, PAGE_SIZE, "%d\n",
1352 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1353}
1354
1355static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1356 struct device_attribute *attr,
1357 const char *buf, size_t count)
1358{
1359 unsigned long t;
1360 int res;
1361
1362 printk_deprecated_rfkill_attribute(attr->attr.name);
1363
1364 if (parse_strtoul(buf, 1, &t))
1365 return -EINVAL;
1366
1367 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1368
1369 /* This is in the ABI... */
1370 if (tpacpi_rfk_check_hwblock_state() && !!t)
1371 return -EPERM;
1372
1373 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1374 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1375 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1376
1377 return (res < 0) ? res : count;
1378}
1379
1380/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001381static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001382{
Johannes Berg19d337d2009-06-02 13:01:37 +02001383 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001384 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001385 else {
1386 int status;
1387
1388 /* This is in the ABI... */
1389 if (tpacpi_rfk_check_hwblock_state()) {
1390 status = TPACPI_RFK_RADIO_OFF;
1391 } else {
1392 status = tpacpi_rfk_update_swstate(
1393 tpacpi_rfkill_switches[id]);
1394 if (status < 0)
1395 return status;
1396 }
1397
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001398 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001399 (status == TPACPI_RFK_RADIO_ON) ?
1400 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001401 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001402 }
1403
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001404 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001405}
1406
1407static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1408{
1409 char *cmd;
1410 int status = -1;
1411 int res = 0;
1412
1413 if (id >= TPACPI_RFK_SW_MAX)
1414 return -ENODEV;
1415
1416 while ((cmd = next_cmd(&buf))) {
1417 if (strlencmp(cmd, "enable") == 0)
1418 status = TPACPI_RFK_RADIO_ON;
1419 else if (strlencmp(cmd, "disable") == 0)
1420 status = TPACPI_RFK_RADIO_OFF;
1421 else
1422 return -EINVAL;
1423 }
1424
1425 if (status != -1) {
1426 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1427 (status == TPACPI_RFK_RADIO_ON) ?
1428 "enable" : "disable",
1429 tpacpi_rfkill_names[id]);
1430 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1431 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1432 }
1433
1434 return res;
1435}
1436
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001437/*************************************************************************
1438 * thinkpad-acpi driver attributes
1439 */
1440
1441/* interface_version --------------------------------------------------- */
1442static ssize_t tpacpi_driver_interface_version_show(
1443 struct device_driver *drv,
1444 char *buf)
1445{
1446 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1447}
1448
1449static DRIVER_ATTR(interface_version, S_IRUGO,
1450 tpacpi_driver_interface_version_show, NULL);
1451
1452/* debug_level --------------------------------------------------------- */
1453static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1454 char *buf)
1455{
1456 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1457}
1458
1459static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1460 const char *buf, size_t count)
1461{
1462 unsigned long t;
1463
1464 if (parse_strtoul(buf, 0xffff, &t))
1465 return -EINVAL;
1466
1467 dbg_level = t;
1468
1469 return count;
1470}
1471
1472static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1473 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1474
1475/* version ------------------------------------------------------------- */
1476static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1477 char *buf)
1478{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001479 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1480 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001481}
1482
1483static DRIVER_ATTR(version, S_IRUGO,
1484 tpacpi_driver_version_show, NULL);
1485
1486/* --------------------------------------------------------------------- */
1487
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001488#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1489
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001490/* wlsw_emulstate ------------------------------------------------------ */
1491static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1492 char *buf)
1493{
1494 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1495}
1496
1497static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1498 const char *buf, size_t count)
1499{
1500 unsigned long t;
1501
1502 if (parse_strtoul(buf, 1, &t))
1503 return -EINVAL;
1504
Johannes Berg19d337d2009-06-02 13:01:37 +02001505 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001506 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001507 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1508 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001509
1510 return count;
1511}
1512
1513static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1514 tpacpi_driver_wlsw_emulstate_show,
1515 tpacpi_driver_wlsw_emulstate_store);
1516
1517/* bluetooth_emulstate ------------------------------------------------- */
1518static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1519 struct device_driver *drv,
1520 char *buf)
1521{
1522 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1523}
1524
1525static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1526 struct device_driver *drv,
1527 const char *buf, size_t count)
1528{
1529 unsigned long t;
1530
1531 if (parse_strtoul(buf, 1, &t))
1532 return -EINVAL;
1533
1534 tpacpi_bluetooth_emulstate = !!t;
1535
1536 return count;
1537}
1538
1539static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1540 tpacpi_driver_bluetooth_emulstate_show,
1541 tpacpi_driver_bluetooth_emulstate_store);
1542
1543/* wwan_emulstate ------------------------------------------------- */
1544static ssize_t tpacpi_driver_wwan_emulstate_show(
1545 struct device_driver *drv,
1546 char *buf)
1547{
1548 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1549}
1550
1551static ssize_t tpacpi_driver_wwan_emulstate_store(
1552 struct device_driver *drv,
1553 const char *buf, size_t count)
1554{
1555 unsigned long t;
1556
1557 if (parse_strtoul(buf, 1, &t))
1558 return -EINVAL;
1559
1560 tpacpi_wwan_emulstate = !!t;
1561
1562 return count;
1563}
1564
1565static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1566 tpacpi_driver_wwan_emulstate_show,
1567 tpacpi_driver_wwan_emulstate_store);
1568
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001569/* uwb_emulstate ------------------------------------------------- */
1570static ssize_t tpacpi_driver_uwb_emulstate_show(
1571 struct device_driver *drv,
1572 char *buf)
1573{
1574 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1575}
1576
1577static ssize_t tpacpi_driver_uwb_emulstate_store(
1578 struct device_driver *drv,
1579 const char *buf, size_t count)
1580{
1581 unsigned long t;
1582
1583 if (parse_strtoul(buf, 1, &t))
1584 return -EINVAL;
1585
1586 tpacpi_uwb_emulstate = !!t;
1587
1588 return count;
1589}
1590
1591static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1592 tpacpi_driver_uwb_emulstate_show,
1593 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001594#endif
1595
1596/* --------------------------------------------------------------------- */
1597
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001598static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001599 &driver_attr_debug_level, &driver_attr_version,
1600 &driver_attr_interface_version,
1601};
1602
1603static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1604{
1605 int i, res;
1606
1607 i = 0;
1608 res = 0;
1609 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1610 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1611 i++;
1612 }
1613
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001614#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1615 if (!res && dbg_wlswemul)
1616 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1617 if (!res && dbg_bluetoothemul)
1618 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1619 if (!res && dbg_wwanemul)
1620 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001621 if (!res && dbg_uwbemul)
1622 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001623#endif
1624
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001625 return res;
1626}
1627
1628static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1629{
1630 int i;
1631
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001632 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001633 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001634
1635#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1636 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1637 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1638 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001639 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001640#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001641}
1642
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001643/*************************************************************************
1644 * Firmware Data
1645 */
1646
1647/*
1648 * Table of recommended minimum BIOS versions
1649 *
1650 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001651 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001652 * bugs and bad ACPI behaviour on older versions
1653 *
1654 * 2. BIOS or EC fw with known bugs that trigger on Linux
1655 *
1656 * 3. BIOS with known reduced functionality in older versions
1657 *
1658 * We recommend the latest BIOS and EC version.
1659 * We only support the latest BIOS and EC fw version as a rule.
1660 *
1661 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1662 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001663 *
1664 * WARNING: we use this table also to detect that the machine is
1665 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001666 */
1667
1668#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1669 { .vendor = (__v), \
1670 .bios = TPID(__id1, __id2), \
1671 .ec = TPACPI_MATCH_ANY, \
1672 .quirks = TPACPI_MATCH_ANY << 16 \
1673 | (__bv1) << 8 | (__bv2) }
1674
1675#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001676 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001677 { .vendor = (__v), \
1678 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001679 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001680 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1681 | (__bv1) << 8 | (__bv2) }
1682
1683#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1684 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1685
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001686/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001687#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1688 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001689 __bv1, __bv2, TPID(__id1, __id2), \
1690 __ev1, __ev2), \
1691 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1692 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1693 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001694
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001695/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001696#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1697 __eid1, __eid2, __ev1, __ev2) \
1698 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001699 __bv1, __bv2, TPID(__eid1, __eid2), \
1700 __ev1, __ev2), \
1701 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1702 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1703 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001704
1705#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1706 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1707
1708#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1709 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001710 __bv1, __bv2, TPID(__id1, __id2), \
1711 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001712
1713#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1714 __eid1, __eid2, __ev1, __ev2) \
1715 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001716 __bv1, __bv2, TPID(__eid1, __eid2), \
1717 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001718
1719static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1720 /* Numeric models ------------------ */
1721 /* FW MODEL BIOS VERS */
1722 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1723 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1724 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1725 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1726 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1727 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1728 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1729 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1730 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1731
1732 /* A-series ------------------------- */
1733 /* FW MODEL BIOS VERS EC VERS */
1734 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1735 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1736 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1737 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1738 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1739 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1740 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1741 TPV_QI0('1', '3', '2', '0'), /* A22m */
1742 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1743 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1744 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1745
1746 /* G-series ------------------------- */
1747 /* FW MODEL BIOS VERS */
1748 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1749 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1750
1751 /* R-series, T-series --------------- */
1752 /* FW MODEL BIOS VERS EC VERS */
1753 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1754 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1755 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1756 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1757 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1758 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1759 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1760 T40/p, T41/p, T42/p (1) */
1761 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1762 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1763 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1764 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1765
1766 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1767 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1768 TPV_QI0('1', '6', '3', '2'), /* T22 */
1769 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1770 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1771 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1772
1773 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1774 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001775 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001776
1777 /* BIOS FW BIOS VERS EC FW EC VERS */
1778 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1779 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1780
1781 /* X-series ------------------------- */
1782 /* FW MODEL BIOS VERS EC VERS */
1783 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1784 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1785 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1786 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1787 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1788 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1789 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1790
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001791 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1792 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001793
1794 /* (0) - older versions lack DMI EC fw string and functionality */
1795 /* (1) - older versions known to lack functionality */
1796};
1797
1798#undef TPV_QL1
1799#undef TPV_QL0
1800#undef TPV_QI2
1801#undef TPV_QI1
1802#undef TPV_QI0
1803#undef TPV_Q_X
1804#undef TPV_Q
1805
1806static void __init tpacpi_check_outdated_fw(void)
1807{
1808 unsigned long fwvers;
1809 u16 ec_version, bios_version;
1810
1811 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1812 ARRAY_SIZE(tpacpi_bios_version_qtable));
1813
1814 if (!fwvers)
1815 return;
1816
1817 bios_version = fwvers & 0xffffU;
1818 ec_version = (fwvers >> 16) & 0xffffU;
1819
1820 /* note that unknown versions are set to 0x0000 and we use that */
1821 if ((bios_version > thinkpad_id.bios_release) ||
1822 (ec_version > thinkpad_id.ec_release &&
1823 ec_version != TPACPI_MATCH_ANY)) {
1824 /*
1825 * The changelogs would let us track down the exact
1826 * reason, but it is just too much of a pain to track
1827 * it. We only list BIOSes that are either really
1828 * broken, or really stable to begin with, so it is
1829 * best if the user upgrades the firmware anyway.
1830 */
Joe Perches0978e012011-04-04 10:06:25 -07001831 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1832 pr_warn("WARNING: This firmware may be missing critical bug "
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001833 "fixes and/or important features\n");
1834 }
1835}
1836
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001837static bool __init tpacpi_is_fw_known(void)
1838{
1839 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1840 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1841}
1842
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001843/****************************************************************************
1844 ****************************************************************************
1845 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001846 * Subdrivers
1847 *
1848 ****************************************************************************
1849 ****************************************************************************/
1850
1851/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001852 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001853 */
1854
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001855static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001857 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1858 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001862static struct ibm_struct thinkpad_acpi_driver_data = {
1863 .name = "driver",
1864 .read = thinkpad_acpi_driver_read,
1865};
1866
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001867/*************************************************************************
1868 * Hotkey subdriver
1869 */
1870
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001871/*
1872 * ThinkPad firmware event model
1873 *
1874 * The ThinkPad firmware has two main event interfaces: normal ACPI
1875 * notifications (which follow the ACPI standard), and a private event
1876 * interface.
1877 *
1878 * The private event interface also issues events for the hotkeys. As
1879 * the driver gained features, the event handling code ended up being
1880 * built around the hotkey subdriver. This will need to be refactored
1881 * to a more formal event API eventually.
1882 *
1883 * Some "hotkeys" are actually supposed to be used as event reports,
1884 * such as "brightness has changed", "volume has changed", depending on
1885 * the ThinkPad model and how the firmware is operating.
1886 *
1887 * Unlike other classes, hotkey-class events have mask/unmask control on
1888 * non-ancient firmware. However, how it behaves changes a lot with the
1889 * firmware model and version.
1890 */
1891
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001892enum { /* hot key scan codes (derived from ACPI DSDT) */
1893 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1894 TP_ACPI_HOTKEYSCAN_FNF2,
1895 TP_ACPI_HOTKEYSCAN_FNF3,
1896 TP_ACPI_HOTKEYSCAN_FNF4,
1897 TP_ACPI_HOTKEYSCAN_FNF5,
1898 TP_ACPI_HOTKEYSCAN_FNF6,
1899 TP_ACPI_HOTKEYSCAN_FNF7,
1900 TP_ACPI_HOTKEYSCAN_FNF8,
1901 TP_ACPI_HOTKEYSCAN_FNF9,
1902 TP_ACPI_HOTKEYSCAN_FNF10,
1903 TP_ACPI_HOTKEYSCAN_FNF11,
1904 TP_ACPI_HOTKEYSCAN_FNF12,
1905 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1906 TP_ACPI_HOTKEYSCAN_FNINSERT,
1907 TP_ACPI_HOTKEYSCAN_FNDELETE,
1908 TP_ACPI_HOTKEYSCAN_FNHOME,
1909 TP_ACPI_HOTKEYSCAN_FNEND,
1910 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1911 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1912 TP_ACPI_HOTKEYSCAN_FNSPACE,
1913 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1914 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1915 TP_ACPI_HOTKEYSCAN_MUTE,
1916 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001917 TP_ACPI_HOTKEYSCAN_UNK1,
1918 TP_ACPI_HOTKEYSCAN_UNK2,
1919 TP_ACPI_HOTKEYSCAN_UNK3,
1920 TP_ACPI_HOTKEYSCAN_UNK4,
1921 TP_ACPI_HOTKEYSCAN_UNK5,
1922 TP_ACPI_HOTKEYSCAN_UNK6,
1923 TP_ACPI_HOTKEYSCAN_UNK7,
1924 TP_ACPI_HOTKEYSCAN_UNK8,
1925
Bastien Nocera6a68d852015-03-02 14:45:31 +01001926 TP_ACPI_HOTKEYSCAN_MUTE2,
1927 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1928 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1929 TP_ACPI_HOTKEYSCAN_CLOUD,
1930 TP_ACPI_HOTKEYSCAN_UNK9,
1931 TP_ACPI_HOTKEYSCAN_VOICE,
1932 TP_ACPI_HOTKEYSCAN_UNK10,
1933 TP_ACPI_HOTKEYSCAN_GESTURES,
1934 TP_ACPI_HOTKEYSCAN_UNK11,
1935 TP_ACPI_HOTKEYSCAN_UNK12,
1936 TP_ACPI_HOTKEYSCAN_UNK13,
1937 TP_ACPI_HOTKEYSCAN_CONFIG,
1938 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1939 TP_ACPI_HOTKEYSCAN_RELOAD,
1940 TP_ACPI_HOTKEYSCAN_BACK,
1941 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1942 TP_ACPI_HOTKEYSCAN_MIC_UP,
1943 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1944 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1945 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1946
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001947 /* Hotkey keymap size */
1948 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001949};
1950
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001951enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001952 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1953 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1954};
1955
1956enum { /* Positions of some of the keys in hotkey masks */
1957 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1958 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1959 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1960 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1961 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1962 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1963 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1964 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1965 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1966 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1967 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1968};
1969
1970enum { /* NVRAM to ACPI HKEY group map */
1971 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1972 TP_ACPI_HKEY_ZOOM_MASK |
1973 TP_ACPI_HKEY_DISPSWTCH_MASK |
1974 TP_ACPI_HKEY_HIBERNATE_MASK,
1975 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1976 TP_ACPI_HKEY_BRGHTDWN_MASK,
1977 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1978 TP_ACPI_HKEY_VOLDWN_MASK |
1979 TP_ACPI_HKEY_MUTE_MASK,
1980};
1981
1982#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1983struct tp_nvram_state {
1984 u16 thinkpad_toggle:1;
1985 u16 zoom_toggle:1;
1986 u16 display_toggle:1;
1987 u16 thinklight_toggle:1;
1988 u16 hibernate_toggle:1;
1989 u16 displayexp_toggle:1;
1990 u16 display_state:1;
1991 u16 brightness_toggle:1;
1992 u16 volume_toggle:1;
1993 u16 mute:1;
1994
1995 u8 brightness_level;
1996 u8 volume_level;
1997};
1998
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001999/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002000static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002001
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002002/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002003 * Acquire mutex to write poller control variables as an
2004 * atomic block.
2005 *
2006 * Increment hotkey_config_change when changing them if you
2007 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002008 *
2009 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2010 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002011static struct mutex hotkey_thread_data_mutex;
2012static unsigned int hotkey_config_change;
2013
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002014/*
2015 * hotkey poller control variables
2016 *
2017 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002018 *
2019 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2020 * should be used only when the changes need to be taken as
2021 * a block, OR when one needs to force the kthread to forget
2022 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002023 */
2024static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2025static unsigned int hotkey_poll_freq = 10; /* Hz */
2026
2027#define HOTKEY_CONFIG_CRITICAL_START \
2028 do { \
2029 mutex_lock(&hotkey_thread_data_mutex); \
2030 hotkey_config_change++; \
2031 } while (0);
2032#define HOTKEY_CONFIG_CRITICAL_END \
2033 mutex_unlock(&hotkey_thread_data_mutex);
2034
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002035#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2036
2037#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002038#define HOTKEY_CONFIG_CRITICAL_START
2039#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002040
2041#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2042
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002043static struct mutex hotkey_mutex;
2044
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002045static enum { /* Reasons for waking up */
2046 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2047 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2048 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2049} hotkey_wakeup_reason;
2050
2051static int hotkey_autosleep_ack;
2052
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002053static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2054static u32 hotkey_all_mask; /* all events supported in fw */
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002055static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002056static u32 hotkey_reserved_mask; /* events better left disabled */
2057static u32 hotkey_driver_mask; /* events needed by the driver */
2058static u32 hotkey_user_mask; /* events visible to userspace */
2059static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002060
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002061static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002062
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002063static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002064
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002065static void tpacpi_driver_event(const unsigned int hkey_event);
2066static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002067static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002068
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002069/* HKEY.MHKG() return bits */
2070#define TP_HOTKEY_TABLET_MASK (1 << 3)
Lyudeb03f4d42016-11-11 15:15:03 -05002071/* ThinkPad X1 Yoga (2016) */
2072#define TP_EC_CMMD_TABLET_MODE 0x6
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002073
Johannes Berg19d337d2009-06-02 13:01:37 +02002074static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002075{
Johannes Berg19d337d2009-06-02 13:01:37 +02002076 int status;
2077
2078 if (!tp_features.hotkey_wlsw)
2079 return -ENODEV;
2080
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002081#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002082 if (dbg_wlswemul)
2083 return (tpacpi_wlsw_emulstate) ?
2084 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002085#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002086
2087 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002088 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002089
2090 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002091}
2092
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002093static int hotkey_get_tablet_mode(int *status)
2094{
2095 int s;
2096
Lyudeb03f4d42016-11-11 15:15:03 -05002097 switch (tp_features.hotkey_tablet) {
2098 case TP_HOTKEY_TABLET_USES_MHKG:
2099 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2100 return -EIO;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002101
Lyudeb03f4d42016-11-11 15:15:03 -05002102 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2103 break;
2104 case TP_HOTKEY_TABLET_USES_CMMD:
2105 if (!acpi_evalf(ec_handle, &s, "CMMD", "d"))
2106 return -EIO;
2107
2108 *status = (s == TP_EC_CMMD_TABLET_MODE);
2109 break;
2110 default:
2111 break;
2112 }
2113
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002114 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002115}
2116
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002117/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002118 * Reads current event mask from firmware, and updates
2119 * hotkey_acpi_mask accordingly. Also resets any bits
2120 * from hotkey_user_mask that are unavailable to be
2121 * delivered (shadow requirement of the userspace ABI).
2122 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002123 * Call with hotkey_mutex held
2124 */
2125static int hotkey_mask_get(void)
2126{
2127 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002128 u32 m = 0;
2129
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002130 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002131 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002132
2133 hotkey_acpi_mask = m;
2134 } else {
2135 /* no mask support doesn't mean no event support... */
2136 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002137 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002138
2139 /* sync userspace-visible mask */
2140 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002141
2142 return 0;
2143}
2144
Jean Delvaredf6dd1b2015-04-27 09:45:06 +02002145static void hotkey_mask_warn_incomplete_mask(void)
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002146{
2147 /* log only what the user can fix... */
2148 const u32 wantedmask = hotkey_driver_mask &
2149 ~(hotkey_acpi_mask | hotkey_source_mask) &
2150 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2151
2152 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002153 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002154}
2155
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002156/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002157 * Set the firmware mask when supported
2158 *
2159 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2160 *
2161 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2162 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002163 * Call with hotkey_mutex held
2164 */
2165static int hotkey_mask_set(u32 mask)
2166{
2167 int i;
2168 int rc = 0;
2169
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002170 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002171
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002172 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002173 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002174 if (!acpi_evalf(hkey_handle,
2175 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002176 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002177 rc = -EIO;
2178 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002179 }
2180 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002181 }
2182
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002183 /*
2184 * We *must* make an inconditional call to hotkey_mask_get to
2185 * refresh hotkey_acpi_mask and update hotkey_user_mask
2186 *
2187 * Take the opportunity to also log when we cannot _enable_
2188 * a given event.
2189 */
2190 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Joe Perches0978e012011-04-04 10:06:25 -07002191 pr_notice("asked for hotkey mask 0x%08x, but "
2192 "firmware forced it to 0x%08x\n",
2193 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002194 }
2195
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002196 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2197 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002198
2199 return rc;
2200}
2201
2202/*
2203 * Sets hotkey_user_mask and tries to set the firmware mask
2204 *
2205 * Call with hotkey_mutex held
2206 */
2207static int hotkey_user_mask_set(const u32 mask)
2208{
2209 int rc;
2210
2211 /* Give people a chance to notice they are doing something that
2212 * is bound to go boom on their users sooner or later */
2213 if (!tp_warned.hotkey_mask_ff &&
2214 (mask == 0xffff || mask == 0xffffff ||
2215 mask == 0xffffffff)) {
2216 tp_warned.hotkey_mask_ff = 1;
Joe Perches0978e012011-04-04 10:06:25 -07002217 pr_notice("setting the hotkey mask to 0x%08x is likely "
2218 "not the best way to go about it\n", mask);
2219 pr_notice("please consider using the driver defaults, "
2220 "and refer to up-to-date thinkpad-acpi "
2221 "documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002222 }
2223
2224 /* Try to enable what the user asked for, plus whatever we need.
2225 * this syncs everything but won't enable bits in hotkey_user_mask */
2226 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2227
2228 /* Enable the available bits in hotkey_user_mask */
2229 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2230
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002231 return rc;
2232}
2233
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002234/*
2235 * Sets the driver hotkey mask.
2236 *
2237 * Can be called even if the hotkey subdriver is inactive
2238 */
2239static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2240{
2241 int rc;
2242
2243 /* Do the right thing if hotkey_init has not been called yet */
2244 if (!tp_features.hotkey) {
2245 hotkey_driver_mask = mask;
2246 return 0;
2247 }
2248
2249 mutex_lock(&hotkey_mutex);
2250
2251 HOTKEY_CONFIG_CRITICAL_START
2252 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002253#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002254 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002255#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002256 HOTKEY_CONFIG_CRITICAL_END
2257
2258 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2259 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002260 hotkey_poll_setup(true);
2261
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002262 mutex_unlock(&hotkey_mutex);
2263
2264 return rc;
2265}
2266
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002267static int hotkey_status_get(int *status)
2268{
2269 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2270 return -EIO;
2271
2272 return 0;
2273}
2274
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002275static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002276{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002277 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002278 return -EIO;
2279
2280 return 0;
2281}
2282
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002283static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002284{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002285 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002286
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002287 if (tp_features.hotkey_tablet &&
2288 !hotkey_get_tablet_mode(&state)) {
2289 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002290
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002291 input_report_switch(tpacpi_inputdev,
2292 SW_TABLET_MODE, !!state);
2293 input_sync(tpacpi_inputdev);
2294
2295 mutex_unlock(&tpacpi_inputdev_send_mutex);
2296 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002297}
2298
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002299/* Do NOT call without validating scancode first */
2300static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002301{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002302 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002303
2304 if (keycode != KEY_RESERVED) {
2305 mutex_lock(&tpacpi_inputdev_send_mutex);
2306
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002307 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002308 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002309 input_sync(tpacpi_inputdev);
2310
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002311 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002312 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002313 input_sync(tpacpi_inputdev);
2314
2315 mutex_unlock(&tpacpi_inputdev_send_mutex);
2316 }
2317}
2318
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002319/* Do NOT call without validating scancode first */
2320static void tpacpi_input_send_key_masked(const unsigned int scancode)
2321{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002322 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002323 if (hotkey_user_mask & (1 << scancode))
2324 tpacpi_input_send_key(scancode);
2325}
2326
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002327#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2328static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2329
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002330/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002331static void tpacpi_hotkey_send_key(unsigned int scancode)
2332{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002333 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002334}
2335
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002336static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002337{
2338 u8 d;
2339
2340 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2341 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2342 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2343 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2344 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2345 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2346 }
2347 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2348 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2349 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2350 }
2351 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2352 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2353 n->displayexp_toggle =
2354 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2355 }
2356 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2357 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2358 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2359 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2360 n->brightness_toggle =
2361 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2362 }
2363 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2364 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2365 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2366 >> TP_NVRAM_POS_LEVEL_VOLUME;
2367 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2368 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2369 }
2370}
2371
Behan Webstera4d44ba2014-02-12 21:58:46 +01002372#define TPACPI_COMPARE_KEY(__scancode, __member) \
2373do { \
2374 if ((event_mask & (1 << __scancode)) && \
2375 oldn->__member != newn->__member) \
2376 tpacpi_hotkey_send_key(__scancode); \
2377} while (0)
2378
2379#define TPACPI_MAY_SEND_KEY(__scancode) \
2380do { \
2381 if (event_mask & (1 << __scancode)) \
2382 tpacpi_hotkey_send_key(__scancode); \
2383} while (0)
2384
2385static void issue_volchange(const unsigned int oldvol,
2386 const unsigned int newvol,
2387 const u32 event_mask)
2388{
2389 unsigned int i = oldvol;
2390
2391 while (i > newvol) {
2392 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2393 i--;
2394 }
2395 while (i < newvol) {
2396 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2397 i++;
2398 }
2399}
2400
2401static void issue_brightnesschange(const unsigned int oldbrt,
2402 const unsigned int newbrt,
2403 const u32 event_mask)
2404{
2405 unsigned int i = oldbrt;
2406
2407 while (i > newbrt) {
2408 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2409 i--;
2410 }
2411 while (i < newbrt) {
2412 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2413 i++;
2414 }
2415}
2416
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002417static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2418 struct tp_nvram_state *newn,
2419 const u32 event_mask)
2420{
2421
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002422 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2423 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2424 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2425 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2426
2427 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2428
2429 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2430
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002431 /*
2432 * Handle volume
2433 *
2434 * This code is supposed to duplicate the IBM firmware behaviour:
2435 * - Pressing MUTE issues mute hotkey message, even when already mute
2436 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002437 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002438 * - The act of unmuting issues volume up/down notification,
2439 * depending which key was used to unmute
2440 *
2441 * We are constrained to what the NVRAM can tell us, which is not much
2442 * and certainly not enough if more than one volume hotkey was pressed
2443 * since the last poll cycle.
2444 *
2445 * Just to make our life interesting, some newer Lenovo ThinkPads have
2446 * bugs in the BIOS and may fail to update volume_toggle properly.
2447 */
2448 if (newn->mute) {
2449 /* muted */
2450 if (!oldn->mute ||
2451 oldn->volume_toggle != newn->volume_toggle ||
2452 oldn->volume_level != newn->volume_level) {
2453 /* recently muted, or repeated mute keypress, or
2454 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002455 issue_volchange(oldn->volume_level, newn->volume_level,
2456 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002457 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2458 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002459 } else {
2460 /* unmute */
2461 if (oldn->mute) {
2462 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002463 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002464 }
2465 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002466 issue_volchange(oldn->volume_level, newn->volume_level,
2467 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002468 } else if (oldn->volume_toggle != newn->volume_toggle) {
2469 /* repeated vol up/down keypress at end of scale ? */
2470 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002471 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002472 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2473 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002474 }
2475 }
2476
2477 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002478 if (oldn->brightness_level != newn->brightness_level) {
2479 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002480 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002481 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2482 /* repeated key presses that didn't change state */
2483 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002484 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002485 else if (newn->brightness_level >= bright_maxlvl
2486 && !tp_features.bright_unkfw)
2487 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002488 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002489
2490#undef TPACPI_COMPARE_KEY
2491#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002492}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002493
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002494/*
2495 * Polling driver
2496 *
2497 * We track all events in hotkey_source_mask all the time, since
2498 * most of them are edge-based. We only issue those requested by
2499 * hotkey_user_mask or hotkey_driver_mask, though.
2500 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002501static int hotkey_kthread(void *data)
2502{
2503 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002504 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002505 unsigned int si, so;
2506 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002507 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002508 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002509 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002510
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002511 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2512 goto exit;
2513
2514 set_freezable();
2515
2516 so = 0;
2517 si = 1;
2518 t = 0;
2519
2520 /* Initial state for compares */
2521 mutex_lock(&hotkey_thread_data_mutex);
2522 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002523 poll_mask = hotkey_source_mask;
2524 event_mask = hotkey_source_mask &
2525 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002526 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002527 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002528 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002529
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002530 while (!kthread_should_stop()) {
2531 if (t == 0) {
2532 if (likely(poll_freq))
2533 t = 1000/poll_freq;
2534 else
2535 t = 100; /* should never happen... */
2536 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002537 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002538 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002539 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002540
2541 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002542 continue;
2543
2544 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002545 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002546 /* forget old state on thaw or config change */
2547 si = so;
2548 t = 0;
2549 change_detector = hotkey_config_change;
2550 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002551 poll_mask = hotkey_source_mask;
2552 event_mask = hotkey_source_mask &
2553 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002554 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002555 mutex_unlock(&hotkey_thread_data_mutex);
2556
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002557 if (likely(poll_mask)) {
2558 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002559 if (likely(si != so)) {
2560 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002561 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002562 }
2563 }
2564
2565 so = si;
2566 si ^= 1;
2567 }
2568
2569exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002570 return 0;
2571}
2572
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002573/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002574static void hotkey_poll_stop_sync(void)
2575{
2576 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002577 kthread_stop(tpacpi_hotkey_task);
2578 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002579 }
2580}
2581
2582/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002583static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002584{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002585 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2586 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002587
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002588 if (hotkey_poll_freq > 0 &&
2589 (poll_driver_mask ||
2590 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002591 if (!tpacpi_hotkey_task) {
2592 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002593 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002594 if (IS_ERR(tpacpi_hotkey_task)) {
2595 tpacpi_hotkey_task = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07002596 pr_err("could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002597 "for hotkey polling\n");
2598 }
2599 }
2600 } else {
2601 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002602 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002603 hotkey_poll_freq == 0) {
Joe Perches0978e012011-04-04 10:06:25 -07002604 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2605 "require polling, which is currently "
2606 "disabled\n",
2607 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002608 }
2609 }
2610}
2611
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002612static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002613{
2614 mutex_lock(&hotkey_mutex);
2615 hotkey_poll_setup(may_warn);
2616 mutex_unlock(&hotkey_mutex);
2617}
2618
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002619/* call with hotkey_mutex held */
2620static void hotkey_poll_set_freq(unsigned int freq)
2621{
2622 if (!freq)
2623 hotkey_poll_stop_sync();
2624
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002625 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002626}
2627
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002628#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2629
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002630static void hotkey_poll_setup(const bool __unused)
2631{
2632}
2633
2634static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002635{
2636}
2637
2638#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2639
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002640static int hotkey_inputdev_open(struct input_dev *dev)
2641{
2642 switch (tpacpi_lifecycle) {
2643 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002644 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002645 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002646 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002647 case TPACPI_LIFE_EXITING:
2648 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002649 }
2650
2651 /* Should only happen if tpacpi_lifecycle is corrupt */
2652 BUG();
2653 return -EBUSY;
2654}
2655
2656static void hotkey_inputdev_close(struct input_dev *dev)
2657{
2658 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002659 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002660 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002661 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002662}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002663
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002664/* sysfs hotkey enable ------------------------------------------------- */
2665static ssize_t hotkey_enable_show(struct device *dev,
2666 struct device_attribute *attr,
2667 char *buf)
2668{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002669 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002670
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002671 printk_deprecated_attribute("hotkey_enable",
2672 "Hotkey reporting is always enabled");
2673
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002674 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002675 if (res)
2676 return res;
2677
2678 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2679}
2680
2681static ssize_t hotkey_enable_store(struct device *dev,
2682 struct device_attribute *attr,
2683 const char *buf, size_t count)
2684{
2685 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002686
2687 printk_deprecated_attribute("hotkey_enable",
2688 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002689
2690 if (parse_strtoul(buf, 1, &t))
2691 return -EINVAL;
2692
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002693 if (t == 0)
2694 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002695
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002696 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002697}
2698
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002699static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002700
2701/* sysfs hotkey mask --------------------------------------------------- */
2702static ssize_t hotkey_mask_show(struct device *dev,
2703 struct device_attribute *attr,
2704 char *buf)
2705{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002706 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002707}
2708
2709static ssize_t hotkey_mask_store(struct device *dev,
2710 struct device_attribute *attr,
2711 const char *buf, size_t count)
2712{
2713 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002714 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002715
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002716 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002717 return -EINVAL;
2718
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002719 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002720 return -ERESTARTSYS;
2721
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002722 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002723
2724#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002725 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002726#endif
2727
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002728 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002729
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002730 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2731
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002732 return (res) ? res : count;
2733}
2734
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002735static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002736
2737/* sysfs hotkey bios_enabled ------------------------------------------- */
2738static ssize_t hotkey_bios_enabled_show(struct device *dev,
2739 struct device_attribute *attr,
2740 char *buf)
2741{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002742 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002743}
2744
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002745static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002746
2747/* sysfs hotkey bios_mask ---------------------------------------------- */
2748static ssize_t hotkey_bios_mask_show(struct device *dev,
2749 struct device_attribute *attr,
2750 char *buf)
2751{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002752 printk_deprecated_attribute("hotkey_bios_mask",
2753 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002754 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002755}
2756
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002757static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002758
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002759/* sysfs hotkey all_mask ----------------------------------------------- */
2760static ssize_t hotkey_all_mask_show(struct device *dev,
2761 struct device_attribute *attr,
2762 char *buf)
2763{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002764 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2765 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002766}
2767
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002768static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002769
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002770/* sysfs hotkey all_mask ----------------------------------------------- */
2771static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2772 struct device_attribute *attr,
2773 char *buf)
2774{
2775 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2776 hotkey_adaptive_all_mask | hotkey_source_mask);
2777}
2778
2779static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2780
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002781/* sysfs hotkey recommended_mask --------------------------------------- */
2782static ssize_t hotkey_recommended_mask_show(struct device *dev,
2783 struct device_attribute *attr,
2784 char *buf)
2785{
2786 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002787 (hotkey_all_mask | hotkey_source_mask)
2788 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002789}
2790
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002791static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002792
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002793#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2794
2795/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2796static ssize_t hotkey_source_mask_show(struct device *dev,
2797 struct device_attribute *attr,
2798 char *buf)
2799{
2800 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2801}
2802
2803static ssize_t hotkey_source_mask_store(struct device *dev,
2804 struct device_attribute *attr,
2805 const char *buf, size_t count)
2806{
2807 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002808 u32 r_ev;
2809 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002810
2811 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2812 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2813 return -EINVAL;
2814
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002815 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002816 return -ERESTARTSYS;
2817
2818 HOTKEY_CONFIG_CRITICAL_START
2819 hotkey_source_mask = t;
2820 HOTKEY_CONFIG_CRITICAL_END
2821
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002822 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2823 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002824 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002825
2826 /* check if events needed by the driver got disabled */
2827 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2828 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002829
2830 mutex_unlock(&hotkey_mutex);
2831
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002832 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07002833 pr_err("hotkey_source_mask: "
2834 "failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002835
2836 if (r_ev)
Joe Perches0978e012011-04-04 10:06:25 -07002837 pr_notice("hotkey_source_mask: "
2838 "some important events were disabled: 0x%04x\n",
2839 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002840
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002841 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2842
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002843 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002844}
2845
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002846static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002847
2848/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2849static ssize_t hotkey_poll_freq_show(struct device *dev,
2850 struct device_attribute *attr,
2851 char *buf)
2852{
2853 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2854}
2855
2856static ssize_t hotkey_poll_freq_store(struct device *dev,
2857 struct device_attribute *attr,
2858 const char *buf, size_t count)
2859{
2860 unsigned long t;
2861
2862 if (parse_strtoul(buf, 25, &t))
2863 return -EINVAL;
2864
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002865 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002866 return -ERESTARTSYS;
2867
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002868 hotkey_poll_set_freq(t);
2869 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002870
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002871 mutex_unlock(&hotkey_mutex);
2872
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002873 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2874
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002875 return count;
2876}
2877
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002878static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002879
2880#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2881
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002882/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002883static ssize_t hotkey_radio_sw_show(struct device *dev,
2884 struct device_attribute *attr,
2885 char *buf)
2886{
Johannes Berg19d337d2009-06-02 13:01:37 +02002887 int res;
2888 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002889 if (res < 0)
2890 return res;
2891
Johannes Berg19d337d2009-06-02 13:01:37 +02002892 /* Opportunistic update */
2893 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2894
2895 return snprintf(buf, PAGE_SIZE, "%d\n",
2896 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002897}
2898
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002899static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002900
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002901static void hotkey_radio_sw_notify_change(void)
2902{
2903 if (tp_features.hotkey_wlsw)
2904 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2905 "hotkey_radio_sw");
2906}
2907
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002908/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2909static ssize_t hotkey_tablet_mode_show(struct device *dev,
2910 struct device_attribute *attr,
2911 char *buf)
2912{
2913 int res, s;
2914 res = hotkey_get_tablet_mode(&s);
2915 if (res < 0)
2916 return res;
2917
2918 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2919}
2920
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002921static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002922
2923static void hotkey_tablet_mode_notify_change(void)
2924{
2925 if (tp_features.hotkey_tablet)
2926 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2927 "hotkey_tablet_mode");
2928}
2929
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002930/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002931static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2932 struct device_attribute *attr,
2933 char *buf)
2934{
2935 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2936}
2937
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002938static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002939
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002940static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002941{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002942 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2943 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002944}
2945
2946/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002947static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2948 struct device_attribute *attr,
2949 char *buf)
2950{
2951 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2952}
2953
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002954static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2955 hotkey_wakeup_hotunplug_complete_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002956
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002957static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002958{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002959 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2960 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002961}
2962
Bastien Nocerab790cee2015-03-02 14:45:27 +01002963/* sysfs adaptive kbd mode --------------------------------------------- */
2964
2965static int adaptive_keyboard_get_mode(void);
2966static int adaptive_keyboard_set_mode(int new_mode);
2967
2968enum ADAPTIVE_KEY_MODE {
2969 HOME_MODE,
2970 WEB_BROWSER_MODE,
2971 WEB_CONFERENCE_MODE,
2972 FUNCTION_MODE,
2973 LAYFLAT_MODE
2974};
2975
2976static ssize_t adaptive_kbd_mode_show(struct device *dev,
2977 struct device_attribute *attr,
2978 char *buf)
2979{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03002980 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01002981
2982 current_mode = adaptive_keyboard_get_mode();
2983 if (current_mode < 0)
2984 return current_mode;
2985
2986 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
2987}
2988
2989static ssize_t adaptive_kbd_mode_store(struct device *dev,
2990 struct device_attribute *attr,
2991 const char *buf, size_t count)
2992{
2993 unsigned long t;
2994 int res;
2995
2996 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2997 return -EINVAL;
2998
2999 res = adaptive_keyboard_set_mode(t);
3000 return (res < 0) ? res : count;
3001}
3002
3003static DEVICE_ATTR_RW(adaptive_kbd_mode);
3004
3005static struct attribute *adaptive_kbd_attributes[] = {
3006 &dev_attr_adaptive_kbd_mode.attr,
3007 NULL
3008};
3009
3010static const struct attribute_group adaptive_kbd_attr_group = {
3011 .attrs = adaptive_kbd_attributes,
3012};
3013
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003014/* --------------------------------------------------------------------- */
3015
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003016static struct attribute *hotkey_attributes[] __initdata = {
3017 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003018 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003019 &dev_attr_hotkey_bios_mask.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003020 &dev_attr_wakeup_reason.attr,
3021 &dev_attr_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003022 &dev_attr_hotkey_mask.attr,
3023 &dev_attr_hotkey_all_mask.attr,
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003024 &dev_attr_hotkey_adaptive_all_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003025 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003026#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003027 &dev_attr_hotkey_source_mask.attr,
3028 &dev_attr_hotkey_poll_freq.attr,
3029#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003030};
3031
Johannes Berg19d337d2009-06-02 13:01:37 +02003032/*
3033 * Sync both the hw and sw blocking state of all switches
3034 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003035static void tpacpi_send_radiosw_update(void)
3036{
3037 int wlsw;
3038
Johannes Berg19d337d2009-06-02 13:01:37 +02003039 /*
3040 * We must sync all rfkill controllers *before* issuing any
3041 * rfkill input events, or we will race the rfkill core input
3042 * handler.
3043 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003044 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003045 * for the above.
3046 *
3047 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3048 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003049
Johannes Berg19d337d2009-06-02 13:01:37 +02003050 wlsw = hotkey_get_wlsw();
3051
3052 /* Sync hw blocking state first if it is hw-blocked */
3053 if (wlsw == TPACPI_RFK_RADIO_OFF)
3054 tpacpi_rfk_update_hwblock_state(true);
3055
3056 /* Sync sw blocking state */
3057 tpacpi_rfk_update_swstate_all();
3058
3059 /* Sync hw blocking state last if it is hw-unblocked */
3060 if (wlsw == TPACPI_RFK_RADIO_ON)
3061 tpacpi_rfk_update_hwblock_state(false);
3062
3063 /* Issue rfkill input event for WLSW switch */
3064 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003065 mutex_lock(&tpacpi_inputdev_send_mutex);
3066
3067 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003068 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003069 input_sync(tpacpi_inputdev);
3070
3071 mutex_unlock(&tpacpi_inputdev_send_mutex);
3072 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003073
3074 /*
3075 * this can be unconditional, as we will poll state again
3076 * if userspace uses the notify to read data
3077 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003078 hotkey_radio_sw_notify_change();
3079}
3080
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003081static void hotkey_exit(void)
3082{
3083#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003084 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003085 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003086 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003087#endif
3088
3089 if (hotkey_dev_attributes)
3090 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3091
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003092 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003093 "restoring original HKEY status and mask\n");
3094 /* yes, there is a bitwise or below, we want the
3095 * functions to be called even if one of them fail */
3096 if (((tp_features.hotkey_mask &&
3097 hotkey_mask_set(hotkey_orig_mask)) |
3098 hotkey_status_set(false)) != 0)
Joe Perches0978e012011-04-04 10:06:25 -07003099 pr_err("failed to restore hot key mask "
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003100 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003101}
3102
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003103static void __init hotkey_unmap(const unsigned int scancode)
3104{
3105 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3106 clear_bit(hotkey_keycode_map[scancode],
3107 tpacpi_inputdev->keybit);
3108 hotkey_keycode_map[scancode] = KEY_RESERVED;
3109 }
3110}
3111
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003112/*
3113 * HKEY quirks:
3114 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3115 */
3116
3117#define TPACPI_HK_Q_INIMASK 0x0001
3118
3119static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3120 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3121 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3122 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3123 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3124 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3125 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3126 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3127 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3128 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3129 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3130 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3131 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3132 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3133 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3134 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3135 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3136 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3137 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3138 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3139};
3140
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003141typedef u16 tpacpi_keymap_entry_t;
3142typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003143
Lyudeb3180022016-11-11 15:15:02 -05003144static int hotkey_init_tablet_mode(void)
3145{
3146 int in_tablet_mode, res;
3147 char *type;
3148
Lyudeb3180022016-11-11 15:15:02 -05003149 if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
Lyudeb03f4d42016-11-11 15:15:03 -05003150 /* For X41t, X60t, X61t Tablets... */
Lyudeb3180022016-11-11 15:15:02 -05003151 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3152 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3153 type = "MHKG";
Lyudeb03f4d42016-11-11 15:15:03 -05003154 } else if (acpi_evalf(ec_handle, &res, "CMMD", "qd")) {
3155 /* For X1 Yoga (2016) */
3156 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_CMMD;
3157 in_tablet_mode = res == TP_EC_CMMD_TABLET_MODE;
3158 type = "CMMD";
Lyudeb3180022016-11-11 15:15:02 -05003159 }
3160
3161 if (!tp_features.hotkey_tablet)
3162 return 0;
3163
3164 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3165 type, in_tablet_mode ? "tablet" : "laptop");
3166
3167 res = add_to_attr_set(hotkey_dev_attributes,
3168 &dev_attr_hotkey_tablet_mode.attr);
3169 if (res)
3170 return -1;
3171
3172 return in_tablet_mode;
3173}
3174
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003175static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003176{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003177 /* Requirements for changing the default keymaps:
3178 *
3179 * 1. Many of the keys are mapped to KEY_RESERVED for very
3180 * good reasons. Do not change them unless you have deep
3181 * knowledge on the IBM and Lenovo ThinkPad firmware for
3182 * the various ThinkPad models. The driver behaves
3183 * differently for KEY_RESERVED: such keys have their
3184 * hot key mask *unset* in mask_recommended, and also
3185 * in the initial hot key mask programmed into the
3186 * firmware at driver load time, which means the firm-
3187 * ware may react very differently if you change them to
3188 * something else;
3189 *
3190 * 2. You must be subscribed to the linux-thinkpad and
3191 * ibm-acpi-devel mailing lists, and you should read the
3192 * list archives since 2007 if you want to change the
3193 * keymaps. This requirement exists so that you will
3194 * know the past history of problems with the thinkpad-
3195 * acpi driver keymaps, and also that you will be
3196 * listening to any bug reports;
3197 *
3198 * 3. Do not send thinkpad-acpi specific patches directly to
3199 * for merging, *ever*. Send them to the linux-acpi
3200 * mailinglist for comments. Merging is to be done only
3201 * through acpi-test and the ACPI maintainer.
3202 *
3203 * If the above is too much to ask, don't change the keymap.
3204 * Ask the thinkpad-acpi maintainer to do it, instead.
3205 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003206
3207 enum keymap_index {
3208 TPACPI_KEYMAP_IBM_GENERIC = 0,
3209 TPACPI_KEYMAP_LENOVO_GENERIC,
3210 };
3211
3212 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3213 /* Generic keymap for IBM ThinkPads */
3214 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003215 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003216 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003217 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3218 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003219
3220 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003221 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3222 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3223 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003224
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003225 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003226 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003227 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003228
3229 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003230 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003231
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003232 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3233 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003234
3235 /* Volume: firmware always react to it and reprograms
3236 * the built-in *extra* mixer. Never map it to control
3237 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003238 KEY_RESERVED, /* 0x14: VOLUME UP */
3239 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3240 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003241
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003242 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003243
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003244 /* (assignments unknown, please report if found) */
3245 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3246 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003247
3248 /* No assignments, only used for Adaptive keyboards. */
3249 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3250 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3251 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3252 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3253 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003254 },
3255
3256 /* Generic keymap for Lenovo ThinkPads */
3257 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003258 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3259 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003260 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003261 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003262
3263 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003264 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3265 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3266 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003267
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003268 /* These should be enabled --only-- when ACPI video
3269 * is disabled (i.e. in "vendor" mode), and are handled
3270 * in a special way by the init code */
3271 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3272 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003273
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003274 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003275
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003276 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3277 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003278
3279 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3280 * react to it and reprograms the built-in *extra* mixer.
3281 * Never map it to control another mixer by default.
3282 *
3283 * T60?, T61, R60?, R61: firmware and EC tries to send
3284 * these over the regular keyboard, so these are no-ops,
3285 * but there are still weird bugs re. MUTE, so do not
3286 * change unless you get test reports from all Lenovo
3287 * models. May cause the BIOS to interfere with the
3288 * HDA mixer.
3289 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003290 KEY_RESERVED, /* 0x14: VOLUME UP */
3291 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3292 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003293
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003294 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003295
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003296 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003297 KEY_UNKNOWN, KEY_UNKNOWN,
3298
3299 /*
3300 * The mic mute button only sends 0x1a. It does not
3301 * automatically mute the mic or change the mute light.
3302 */
3303 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3304
3305 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003306 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003307
3308 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003309 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003310
3311 /*
3312 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3313 * The first item in this list is the Mute button which is
3314 * emitted with 0x103 through
3315 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3316 * symbol is held.
3317 * We'll need to offset those by 0x20.
3318 */
3319 KEY_RESERVED, /* Mute held, 0x103 */
3320 KEY_BRIGHTNESS_MIN, /* Backlight off */
3321 KEY_RESERVED, /* Clipping tool */
3322 KEY_RESERVED, /* Cloud */
3323 KEY_RESERVED,
3324 KEY_VOICECOMMAND, /* Voice */
3325 KEY_RESERVED,
3326 KEY_RESERVED, /* Gestures */
3327 KEY_RESERVED,
3328 KEY_RESERVED,
3329 KEY_RESERVED,
3330 KEY_CONFIG, /* Settings */
3331 KEY_RESERVED, /* New tab */
3332 KEY_REFRESH, /* Reload */
3333 KEY_BACK, /* Back */
3334 KEY_RESERVED, /* Microphone down */
3335 KEY_RESERVED, /* Microphone up */
3336 KEY_RESERVED, /* Microphone cancellation */
3337 KEY_RESERVED, /* Camera mode */
3338 KEY_RESERVED, /* Rotate display, 0x116 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003339 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003340 };
3341
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003342 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3343 /* Generic maps (fallback) */
3344 {
3345 .vendor = PCI_VENDOR_ID_IBM,
3346 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3347 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3348 },
3349 {
3350 .vendor = PCI_VENDOR_ID_LENOVO,
3351 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3352 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3353 },
3354 };
3355
3356#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003357#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003358
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003359 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003360 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003361 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003362 bool radiosw_state = false;
3363 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003364
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003365 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003366 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003367
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003368 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3369 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003370
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003371 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003372 BUG_ON(tpacpi_inputdev->open != NULL ||
3373 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003374
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003375 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003376 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003377
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003378#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003379 mutex_init(&hotkey_thread_data_mutex);
3380#endif
3381
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003382 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003383 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003384
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003385 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3386 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003387 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003388
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003389 if (!tp_features.hotkey)
3390 return 1;
3391
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003392 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3393 ARRAY_SIZE(tpacpi_hotkey_qtable));
3394
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003395 tpacpi_disable_brightness_delay();
3396
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003397 /* MUST have enough space for all attributes to be added to
3398 * hotkey_dev_attributes */
3399 hotkey_dev_attributes = create_attr_set(
3400 ARRAY_SIZE(hotkey_attributes) + 2,
3401 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003402 if (!hotkey_dev_attributes)
3403 return -ENOMEM;
3404 res = add_many_to_attr_set(hotkey_dev_attributes,
3405 hotkey_attributes,
3406 ARRAY_SIZE(hotkey_attributes));
3407 if (res)
3408 goto err_exit;
3409
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003410 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003411 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3412 for HKEY interface version 0x100 */
3413 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003414 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3415 "firmware HKEY interface version: 0x%x\n",
3416 hkeyv);
3417
3418 switch (hkeyv >> 8) {
3419 case 1:
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003420 /*
3421 * MHKV 0x100 in A31, R40, R40e,
3422 * T4x, X31, and later
3423 */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003424
3425 /* Paranoia check AND init hotkey_all_mask */
3426 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3427 "MHKA", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003428 pr_err("missing MHKA handler, please report this to %s\n",
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003429 TPACPI_MAIL);
3430 /* Fallback: pre-init for FN+F3,F4,F12 */
3431 hotkey_all_mask = 0x080cU;
3432 } else {
3433 tp_features.hotkey_mask = 1;
3434 }
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003435 break;
3436
3437 case 2:
3438 /*
3439 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3440 */
3441
3442 /* Paranoia check AND init hotkey_all_mask */
3443 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3444 "MHKA", "dd", 1)) {
3445 pr_err("missing MHKA handler, please report this to %s\n",
3446 TPACPI_MAIL);
3447 /* Fallback: pre-init for FN+F3,F4,F12 */
3448 hotkey_all_mask = 0x080cU;
3449 } else {
3450 tp_features.hotkey_mask = 1;
3451 }
3452
3453 /*
3454 * Check if we have an adaptive keyboard, like on the
3455 * Lenovo Carbon X1 2014 (2nd Gen).
3456 */
3457 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3458 "MHKA", "dd", 2)) {
3459 if (hotkey_adaptive_all_mask != 0) {
3460 tp_features.has_adaptive_kbd = true;
3461 res = sysfs_create_group(
3462 &tpacpi_pdev->dev.kobj,
3463 &adaptive_kbd_attr_group);
3464 if (res)
3465 goto err_exit;
3466 }
3467 } else {
3468 tp_features.has_adaptive_kbd = false;
3469 hotkey_adaptive_all_mask = 0x0U;
3470 }
3471 break;
3472
3473 default:
3474 pr_err("unknown version of the HKEY interface: 0x%x\n",
3475 hkeyv);
3476 pr_err("please report this to %s\n", TPACPI_MAIL);
3477 break;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003478 }
3479 }
3480
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003481 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3482 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003483 str_supported(tp_features.hotkey_mask));
3484
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003485 /* Init hotkey_all_mask if not initialized yet */
3486 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3487 (quirks & TPACPI_HK_Q_INIMASK))
3488 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003489
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003490 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003491 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003492 /* hotkey_source_mask *must* be zero for
3493 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003494 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003495 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003496 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003497
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003498 hotkey_orig_mask = hotkey_acpi_mask;
3499 } else {
3500 hotkey_orig_mask = hotkey_all_mask;
3501 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003502 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003503
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003504#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3505 if (dbg_wlswemul) {
3506 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003507 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003508 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003509 } else
3510#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003511 /* Not all thinkpads have a hardware radio switch */
3512 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3513 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003514 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003515 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003516 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003517 }
3518 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003519 res = add_to_attr_set(hotkey_dev_attributes,
3520 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003521
Lyudeb3180022016-11-11 15:15:02 -05003522 res = hotkey_init_tablet_mode();
3523 if (res < 0)
3524 goto err_exit;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003525
Lyudeb3180022016-11-11 15:15:02 -05003526 tabletsw_state = res;
3527
3528 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3529 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003530 if (res)
3531 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003532
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003533 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003534 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3535 GFP_KERNEL);
3536 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003537 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003538 res = -ENOMEM;
3539 goto err_exit;
3540 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003541
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003542 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3543 ARRAY_SIZE(tpacpi_keymap_qtable));
3544 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3545 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3546 "using keymap number %lu\n", keymap_id);
3547
3548 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3549 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003550
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003551 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003552 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3553 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3554 tpacpi_inputdev->keycode = hotkey_keycode_map;
3555 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3556 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003557 input_set_capability(tpacpi_inputdev, EV_KEY,
3558 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003559 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003560 if (i < sizeof(hotkey_reserved_mask)*8)
3561 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003562 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003563 }
3564
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003565 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003566 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003567 input_report_switch(tpacpi_inputdev,
3568 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003569 }
3570 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003571 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003572 input_report_switch(tpacpi_inputdev,
3573 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003574 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003575
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003576 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003577 * userspace. tpacpi_detect_brightness_capabilities() must have
3578 * been called before this point */
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02003579 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Joe Perches0978e012011-04-04 10:06:25 -07003580 pr_info("This ThinkPad has standard ACPI backlight "
3581 "brightness control, supported by the ACPI "
3582 "video driver\n");
3583 pr_notice("Disabling thinkpad-acpi brightness events "
3584 "by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003585
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003586 /* Disable brightness up/down on Lenovo thinkpads when
3587 * ACPI is handling them, otherwise it is plain impossible
3588 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003589 hotkey_reserved_mask |=
3590 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3591 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003592 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3593 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003594 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003595
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003596#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003597 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3598 & ~hotkey_all_mask
3599 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003600
3601 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3602 "hotkey source mask 0x%08x, polling freq %u\n",
3603 hotkey_source_mask, hotkey_poll_freq);
3604#endif
3605
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003606 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3607 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003608 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003609 if (res) {
3610 hotkey_exit();
3611 return res;
3612 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003613 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3614 | hotkey_driver_mask)
3615 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003616 if (res < 0 && res != -ENXIO) {
3617 hotkey_exit();
3618 return res;
3619 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003620 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3621 & ~hotkey_reserved_mask;
3622 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3623 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3624 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003625
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003626 tpacpi_inputdev->open = &hotkey_inputdev_open;
3627 tpacpi_inputdev->close = &hotkey_inputdev_close;
3628
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003629 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003630
3631 return 0;
3632
3633err_exit:
3634 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
Bastien Nocerab790cee2015-03-02 14:45:27 +01003635 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3636 &adaptive_kbd_attr_group);
3637
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003638 hotkey_dev_attributes = NULL;
3639
Jan van den Berg72a979f2014-09-17 00:01:08 +02003640 return (res < 0) ? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003641}
3642
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003643/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3644 * mode, Web conference mode, Function mode and Lay-flat mode.
3645 * We support Home mode and Function mode currently.
3646 *
3647 * Will consider support rest of modes in future.
3648 *
3649 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003650static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003651 HOME_MODE,
3652/* WEB_BROWSER_MODE = 2,
3653 WEB_CONFERENCE_MODE = 3, */
3654 FUNCTION_MODE
3655};
3656
3657#define DFR_CHANGE_ROW 0x101
3658#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003659#define FIRST_ADAPTIVE_KEY 0x103
3660#define ADAPTIVE_KEY_OFFSET 0x020
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003661
3662/* press Fn key a while second, it will switch to Function Mode. Then
3663 * release Fn key, previous mode be restored.
3664 */
3665static bool adaptive_keyboard_mode_is_saved;
3666static int adaptive_keyboard_prev_mode;
3667
Bastien Noceraf74587f2015-03-02 14:45:22 +01003668static int adaptive_keyboard_get_mode(void)
3669{
3670 int mode = 0;
3671
3672 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3673 pr_err("Cannot read adaptive keyboard mode\n");
3674 return -EIO;
3675 }
3676
3677 return mode;
3678}
3679
3680static int adaptive_keyboard_set_mode(int new_mode)
3681{
3682 if (new_mode < 0 ||
3683 new_mode > LAYFLAT_MODE)
3684 return -EINVAL;
3685
3686 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3687 pr_err("Cannot set adaptive keyboard mode\n");
3688 return -EIO;
3689 }
3690
3691 return 0;
3692}
3693
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003694static int adaptive_keyboard_get_next_mode(int mode)
3695{
3696 size_t i;
3697 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3698
3699 for (i = 0; i <= max_mode; i++) {
3700 if (adaptive_keyboard_modes[i] == mode)
3701 break;
3702 }
3703
3704 if (i >= max_mode)
3705 i = 0;
3706 else
3707 i++;
3708
3709 return adaptive_keyboard_modes[i];
3710}
3711
3712static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3713{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003714 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003715 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003716 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003717
3718 switch (scancode) {
3719 case DFR_CHANGE_ROW:
3720 if (adaptive_keyboard_mode_is_saved) {
3721 new_mode = adaptive_keyboard_prev_mode;
3722 adaptive_keyboard_mode_is_saved = false;
3723 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003724 current_mode = adaptive_keyboard_get_mode();
3725 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003726 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003727 new_mode = adaptive_keyboard_get_next_mode(
3728 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003729 }
3730
Bastien Noceraf74587f2015-03-02 14:45:22 +01003731 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003732 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003733
3734 return true;
3735
3736 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003737 current_mode = adaptive_keyboard_get_mode();
3738 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003739 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003740
Bastien Noceraf74587f2015-03-02 14:45:22 +01003741 adaptive_keyboard_prev_mode = current_mode;
3742 adaptive_keyboard_mode_is_saved = true;
3743
3744 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3745 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003746 return true;
3747
3748 default:
Dan Carpenter741d98c2015-03-11 12:36:07 +03003749 if (scancode < FIRST_ADAPTIVE_KEY ||
3750 scancode >= FIRST_ADAPTIVE_KEY + TPACPI_HOTKEY_MAP_LEN -
3751 ADAPTIVE_KEY_OFFSET) {
Bastien Nocera6a68d852015-03-02 14:45:31 +01003752 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3753 scancode);
3754 return false;
3755 }
3756 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY + ADAPTIVE_KEY_OFFSET];
3757 if (keycode != KEY_RESERVED) {
3758 mutex_lock(&tpacpi_inputdev_send_mutex);
3759
3760 input_report_key(tpacpi_inputdev, keycode, 1);
3761 input_sync(tpacpi_inputdev);
3762
3763 input_report_key(tpacpi_inputdev, keycode, 0);
3764 input_sync(tpacpi_inputdev);
3765
3766 mutex_unlock(&tpacpi_inputdev_send_mutex);
3767 }
3768 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003769 }
3770}
3771
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003772static bool hotkey_notify_hotkey(const u32 hkey,
3773 bool *send_acpi_ev,
3774 bool *ignore_acpi_ev)
3775{
3776 /* 0x1000-0x1FFF: key presses */
3777 unsigned int scancode = hkey & 0xfff;
3778 *send_acpi_ev = true;
3779 *ignore_acpi_ev = false;
3780
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03003781 /* HKEY event 0x1001 is scancode 0x00 */
3782 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003783 scancode--;
3784 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003785 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003786 *send_acpi_ev = false;
3787 } else {
3788 *ignore_acpi_ev = true;
3789 }
3790 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003791 } else {
3792 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003793 }
3794 return false;
3795}
3796
3797static bool hotkey_notify_wakeup(const u32 hkey,
3798 bool *send_acpi_ev,
3799 bool *ignore_acpi_ev)
3800{
3801 /* 0x2000-0x2FFF: Wakeup reason */
3802 *send_acpi_ev = true;
3803 *ignore_acpi_ev = false;
3804
3805 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003806 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3807 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003808 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3809 *ignore_acpi_ev = true;
3810 break;
3811
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003812 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3813 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003814 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3815 *ignore_acpi_ev = true;
3816 break;
3817
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003818 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3819 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003820 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003821 /* how to auto-heal: */
3822 /* 2313: woke up from S3, go to S4/S5 */
3823 /* 2413: woke up from S4, go to S5 */
3824 break;
3825
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003826 default:
3827 return false;
3828 }
3829
3830 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003831 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003832 hotkey_wakeup_reason_notify_change();
3833 }
3834 return true;
3835}
3836
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003837static bool hotkey_notify_dockevent(const u32 hkey,
3838 bool *send_acpi_ev,
3839 bool *ignore_acpi_ev)
3840{
3841 /* 0x4000-0x4FFF: dock-related events */
3842 *send_acpi_ev = true;
3843 *ignore_acpi_ev = false;
3844
3845 switch (hkey) {
3846 case TP_HKEY_EV_UNDOCK_ACK:
3847 /* ACPI undock operation completed after wakeup */
3848 hotkey_autosleep_ack = 1;
3849 pr_info("undocked\n");
3850 hotkey_wakeup_hotunplug_complete_notify_change();
3851 return true;
3852
3853 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3854 pr_info("docked into hotplug port replicator\n");
3855 return true;
3856 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3857 pr_info("undocked from hotplug port replicator\n");
3858 return true;
3859
3860 default:
3861 return false;
3862 }
3863}
3864
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003865static bool hotkey_notify_usrevent(const u32 hkey,
3866 bool *send_acpi_ev,
3867 bool *ignore_acpi_ev)
3868{
3869 /* 0x5000-0x5FFF: human interface helpers */
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_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3875 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003876 return true;
3877
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003878 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3879 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003880 tpacpi_input_send_tabletsw();
3881 hotkey_tablet_mode_notify_change();
3882 *send_acpi_ev = false;
3883 return true;
3884
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003885 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3886 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3887 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003888 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003889 *ignore_acpi_ev = true;
3890 return true;
3891
3892 default:
3893 return false;
3894 }
3895}
3896
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003897static void thermal_dump_all_sensors(void);
3898
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003899static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003900 bool *send_acpi_ev,
3901 bool *ignore_acpi_ev)
3902{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003903 bool known = true;
3904
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003905 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003906 *send_acpi_ev = true;
3907 *ignore_acpi_ev = false;
3908
3909 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003910 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003911 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003912 /* recommended action: do nothing, we don't have
3913 * Lenovo ATM information */
3914 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003915 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003916 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003917 /* recommended action: warn user through gui */
3918 break;
3919 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003920 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003921 /* recommended action: immediate sleep/hibernate */
3922 break;
3923 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003924 pr_crit("THERMAL ALARM: "
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003925 "a sensor reports something is too hot!\n");
3926 /* recommended action: warn user through gui, that */
3927 /* some internal component is too hot */
3928 break;
3929 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003930 pr_alert("THERMAL EMERGENCY: "
3931 "a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003932 /* recommended action: immediate sleep/hibernate */
3933 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01003934 case TP_HKEY_EV_AC_CHANGED:
3935 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3936 * AC status changed; can be triggered by plugging or
3937 * unplugging AC adapter, docking or undocking. */
3938
3939 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003940
3941 case TP_HKEY_EV_KEY_NUMLOCK:
3942 case TP_HKEY_EV_KEY_FN:
Xavier Naveira67ab6242015-02-10 08:45:18 +01003943 case TP_HKEY_EV_KEY_FN_ESC:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003944 /* key press events, we just ignore them as long as the EC
3945 * is still reporting them in the normal keyboard stream */
3946 *send_acpi_ev = false;
3947 *ignore_acpi_ev = true;
3948 return true;
3949
Lyudeb03f4d42016-11-11 15:15:03 -05003950 case TP_HKEY_EV_TABLET_CHANGED:
3951 tpacpi_input_send_tabletsw();
3952 hotkey_tablet_mode_notify_change();
3953 *send_acpi_ev = false;
3954 break;
3955
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003956 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003957 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003958 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003959 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003960
3961 thermal_dump_all_sensors();
3962
3963 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003964}
3965
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003966static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3967{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003968 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003969 bool send_acpi_ev;
3970 bool ignore_acpi_ev;
3971 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003972
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003973 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07003974 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003975 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003976 acpi_bus_generate_netlink_event(
3977 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003978 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003979 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003980 return;
3981 }
3982
3983 while (1) {
3984 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07003985 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003986 return;
3987 }
3988
3989 if (hkey == 0) {
3990 /* queue empty */
3991 return;
3992 }
3993
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003994 send_acpi_ev = true;
3995 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003996
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003997 switch (hkey >> 12) {
3998 case 1:
3999 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004000 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4001 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004002 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004003 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004004 /* 0x2000-0x2FFF: Wakeup reason */
4005 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4006 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004007 break;
4008 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004009 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004010 switch (hkey) {
4011 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004012 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004013 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004014 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004015 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004016 break;
4017 case TP_HKEY_EV_OPTDRV_EJ:
4018 /* FIXME: kick libata if SATA link offline */
4019 known_ev = true;
4020 break;
4021 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004022 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004023 }
4024 break;
4025 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03004026 /* 0x4000-0x4FFF: dock-related events */
4027 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4028 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004029 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004030 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02004031 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004032 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4033 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004034 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004035 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004036 /* 0x6000-0x6FFF: thermal alarms/notices and
4037 * keyboard events */
4038 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004039 &ignore_acpi_ev);
4040 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004041 case 7:
4042 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004043 if (tp_features.hotkey_wlsw &&
4044 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004045 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004046 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004047 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004048 break;
4049 }
4050 /* fallthrough to default */
4051 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004052 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004053 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004054 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07004055 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4056 pr_notice("please report the conditions when this "
4057 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004058 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004059
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004060 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03004061 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004062 acpi_bus_generate_netlink_event(
4063 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004064 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004065 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004066 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004067 }
4068}
4069
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02004070static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004071{
4072 /* Do these on suspend, we get the events on early resume! */
4073 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4074 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08004075
4076 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004077 if (tp_features.has_adaptive_kbd) {
4078 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4079 "GTRW", "dd", 0)) {
4080 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004081 }
4082 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004083}
4084
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004085static void hotkey_resume(void)
4086{
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03004087 tpacpi_disable_brightness_delay();
4088
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004089 if (hotkey_status_set(true) < 0 ||
4090 hotkey_mask_set(hotkey_acpi_mask) < 0)
Joe Perches0978e012011-04-04 10:06:25 -07004091 pr_err("error while attempting to reset the event "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004092 "firmware interface\n");
4093
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004094 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02004095 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004096 hotkey_wakeup_reason_notify_change();
4097 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004098 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004099
4100 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004101 if (tp_features.has_adaptive_kbd) {
4102 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4103 adaptive_keyboard_prev_mode)) {
4104 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004105 }
4106 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004107}
4108
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004109/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004110static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004112 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004114 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004115 seq_printf(m, "status:\t\tnot supported\n");
4116 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004117 }
4118
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004119 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004120 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004121 res = hotkey_status_get(&status);
4122 if (!res)
4123 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004124 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004125 if (res)
4126 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004128 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004129 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004130 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4131 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004133 seq_printf(m, "mask:\t\tnot supported\n");
4134 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 }
4136
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004137 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138}
4139
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004140static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004141{
4142 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004143 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Joe Perches0978e012011-04-04 10:06:25 -07004144 pr_fmt("hotkey enable/disable functionality has been "
4145 "removed from the driver. "
4146 "Hotkeys are always enabled.\n")))
4147 pr_err("Please remove the hotkey=enable module "
4148 "parameter, it is deprecated. "
4149 "Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004150}
4151
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004152static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004154 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004155 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004158 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 return -ENODEV;
4160
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004161 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004162 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004163
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004164 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004165
4166 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 while ((cmd = next_cmd(&buf))) {
4168 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004169 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004171 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004172 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa462009-09-12 15:22:16 -03004174 mask = (hotkey_all_mask | hotkey_source_mask)
4175 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4177 /* mask set */
4178 } else if (sscanf(cmd, "%x", &mask) == 1) {
4179 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004180 } else {
4181 res = -EINVAL;
4182 goto errexit;
4183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004185
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004186 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004187 tpacpi_disclose_usertask("procfs hotkey",
4188 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004189 res = hotkey_user_mask_set(mask);
4190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004192errexit:
4193 mutex_unlock(&hotkey_mutex);
4194 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004195}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004197static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004198 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4199 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Hui Wanga3c42a42016-11-08 16:13:23 +08004200 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004201 {"", 0},
4202};
4203
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004204static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004205 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004206 .notify = hotkey_notify,
4207 .handle = &hkey_handle,
4208 .type = ACPI_DEVICE_NOTIFY,
4209};
4210
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004211static struct ibm_struct hotkey_driver_data = {
4212 .name = "hotkey",
4213 .read = hotkey_read,
4214 .write = hotkey_write,
4215 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004216 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004217 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004218 .acpi = &ibm_hotkey_acpidriver,
4219};
4220
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004221/*************************************************************************
4222 * Bluetooth subdriver
4223 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004225enum {
4226 /* ACPI GBDC/SBDC bits */
4227 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4228 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004229 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004230 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004231};
4232
4233enum {
4234 /* ACPI \BLTH commands */
4235 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4236 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4237 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4238 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4239 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004240};
4241
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004242#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4243
Johannes Berg19d337d2009-06-02 13:01:37 +02004244static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004245{
4246 int status;
4247
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004248#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4249 if (dbg_bluetoothemul)
4250 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004251 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004252#endif
4253
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004254 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4255 return -EIO;
4256
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004257 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004258 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004259}
4260
Johannes Berg19d337d2009-06-02 13:01:37 +02004261static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004262{
4263 int status;
4264
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004265 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004266 "will attempt to %s bluetooth\n",
4267 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004268
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004269#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4270 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004271 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004272 return 0;
4273 }
4274#endif
4275
Johannes Berg19d337d2009-06-02 13:01:37 +02004276 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004277 status = TP_ACPI_BLUETOOTH_RADIOSSW
4278 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4279 else
4280 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004281
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004282 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4283 return -EIO;
4284
4285 return 0;
4286}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004287
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004288/* sysfs bluetooth enable ---------------------------------------------- */
4289static ssize_t bluetooth_enable_show(struct device *dev,
4290 struct device_attribute *attr,
4291 char *buf)
4292{
Johannes Berg19d337d2009-06-02 13:01:37 +02004293 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4294 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004295}
4296
4297static ssize_t bluetooth_enable_store(struct device *dev,
4298 struct device_attribute *attr,
4299 const char *buf, size_t count)
4300{
Johannes Berg19d337d2009-06-02 13:01:37 +02004301 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4302 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004303}
4304
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004305static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004306
4307/* --------------------------------------------------------------------- */
4308
4309static struct attribute *bluetooth_attributes[] = {
4310 &dev_attr_bluetooth_enable.attr,
4311 NULL
4312};
4313
4314static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004315 .attrs = bluetooth_attributes,
4316};
4317
Johannes Berg19d337d2009-06-02 13:01:37 +02004318static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4319 .get_status = bluetooth_get_status,
4320 .set_status = bluetooth_set_status,
4321};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004322
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004323static void bluetooth_shutdown(void)
4324{
4325 /* Order firmware to save current state to NVRAM */
4326 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4327 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004328 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004329 else
4330 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004331 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004332}
4333
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004334static void bluetooth_exit(void)
4335{
4336 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4337 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004338
4339 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4340
4341 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004342}
4343
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004344static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004346 int res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004347 int status = 0;
4348
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004349 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4350 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004351
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004352 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004353
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004354 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4355 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004356 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004357 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004359 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4360 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004361 str_supported(tp_features.bluetooth),
4362 status);
4363
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004364#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4365 if (dbg_bluetoothemul) {
4366 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004367 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004368 } else
4369#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004370 if (tp_features.bluetooth &&
4371 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4372 /* no bluetooth hardware present in system */
4373 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004374 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004375 "bluetooth hardware not installed\n");
4376 }
4377
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004378 if (!tp_features.bluetooth)
4379 return 1;
4380
Johannes Berg19d337d2009-06-02 13:01:37 +02004381 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4382 &bluetooth_tprfk_ops,
4383 RFKILL_TYPE_BLUETOOTH,
4384 TPACPI_RFK_BLUETOOTH_SW_NAME,
4385 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004386 if (res)
4387 return res;
4388
Johannes Berg19d337d2009-06-02 13:01:37 +02004389 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4390 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004391 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004392 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004393 return res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004394 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004395
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397}
4398
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004399/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004400static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004402 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403}
4404
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004405static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406{
Johannes Berg19d337d2009-06-02 13:01:37 +02004407 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408}
4409
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004410static struct ibm_struct bluetooth_driver_data = {
4411 .name = "bluetooth",
4412 .read = bluetooth_read,
4413 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004414 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004415 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004416};
4417
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004418/*************************************************************************
4419 * Wan subdriver
4420 */
4421
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004422enum {
4423 /* ACPI GWAN/SWAN bits */
4424 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4425 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004426 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004427 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004428};
4429
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004430#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4431
Johannes Berg19d337d2009-06-02 13:01:37 +02004432static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004433{
4434 int status;
4435
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004436#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4437 if (dbg_wwanemul)
4438 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004439 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004440#endif
4441
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004442 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4443 return -EIO;
4444
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004445 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004446 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004447}
4448
Johannes Berg19d337d2009-06-02 13:01:37 +02004449static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004450{
4451 int status;
4452
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004453 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004454 "will attempt to %s wwan\n",
4455 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004456
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004457#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4458 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004459 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004460 return 0;
4461 }
4462#endif
4463
Johannes Berg19d337d2009-06-02 13:01:37 +02004464 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004465 status = TP_ACPI_WANCARD_RADIOSSW
4466 | TP_ACPI_WANCARD_RESUMECTRL;
4467 else
4468 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004469
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004470 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4471 return -EIO;
4472
4473 return 0;
4474}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004475
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004476/* sysfs wan enable ---------------------------------------------------- */
4477static ssize_t wan_enable_show(struct device *dev,
4478 struct device_attribute *attr,
4479 char *buf)
4480{
Johannes Berg19d337d2009-06-02 13:01:37 +02004481 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4482 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004483}
4484
4485static ssize_t wan_enable_store(struct device *dev,
4486 struct device_attribute *attr,
4487 const char *buf, size_t count)
4488{
Johannes Berg19d337d2009-06-02 13:01:37 +02004489 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4490 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004491}
4492
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004493static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4494 wan_enable_show, wan_enable_store);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004495
4496/* --------------------------------------------------------------------- */
4497
4498static struct attribute *wan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004499 &dev_attr_wwan_enable.attr,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004500 NULL
4501};
4502
4503static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004504 .attrs = wan_attributes,
4505};
4506
Johannes Berg19d337d2009-06-02 13:01:37 +02004507static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4508 .get_status = wan_get_status,
4509 .set_status = wan_set_status,
4510};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004511
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004512static void wan_shutdown(void)
4513{
4514 /* Order firmware to save current state to NVRAM */
4515 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4516 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004517 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004518 else
4519 vdbg_printk(TPACPI_DBG_RFKILL,
4520 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004521}
4522
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004523static void wan_exit(void)
4524{
4525 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4526 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004527
4528 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4529
4530 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004531}
4532
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004533static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004534{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004535 int res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004536 int status = 0;
4537
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004538 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4539 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004540
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004541 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004542
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004543 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004544 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004545
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004546 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4547 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004548 str_supported(tp_features.wan),
4549 status);
4550
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004551#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4552 if (dbg_wwanemul) {
4553 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004554 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004555 } else
4556#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004557 if (tp_features.wan &&
4558 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4559 /* no wan hardware present in system */
4560 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004561 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004562 "wan hardware not installed\n");
4563 }
4564
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004565 if (!tp_features.wan)
4566 return 1;
4567
Johannes Berg19d337d2009-06-02 13:01:37 +02004568 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4569 &wan_tprfk_ops,
4570 RFKILL_TYPE_WWAN,
4571 TPACPI_RFK_WWAN_SW_NAME,
4572 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004573 if (res)
4574 return res;
4575
Johannes Berg19d337d2009-06-02 13:01:37 +02004576 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4577 &wan_attr_group);
4578
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004579 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004580 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004581 return res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004582 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004583
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004584 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004585}
4586
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004587/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004588static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004589{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004590 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004591}
4592
4593static int wan_write(char *buf)
4594{
Johannes Berg19d337d2009-06-02 13:01:37 +02004595 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004596}
4597
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004598static struct ibm_struct wan_driver_data = {
4599 .name = "wan",
4600 .read = wan_read,
4601 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004602 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004603 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004604};
4605
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004606/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004607 * UWB subdriver
4608 */
4609
4610enum {
4611 /* ACPI GUWB/SUWB bits */
4612 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4613 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4614};
4615
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004616#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4617
Johannes Berg19d337d2009-06-02 13:01:37 +02004618static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004619{
4620 int status;
4621
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004622#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4623 if (dbg_uwbemul)
4624 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004625 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004626#endif
4627
4628 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4629 return -EIO;
4630
4631 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004632 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004633}
4634
Johannes Berg19d337d2009-06-02 13:01:37 +02004635static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004636{
4637 int status;
4638
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004639 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004640 "will attempt to %s UWB\n",
4641 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004642
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004643#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4644 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004645 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004646 return 0;
4647 }
4648#endif
4649
Johannes Berg19d337d2009-06-02 13:01:37 +02004650 if (state == TPACPI_RFK_RADIO_ON)
4651 status = TP_ACPI_UWB_RADIOSSW;
4652 else
4653 status = 0;
4654
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004655 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4656 return -EIO;
4657
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004658 return 0;
4659}
4660
4661/* --------------------------------------------------------------------- */
4662
Johannes Berg19d337d2009-06-02 13:01:37 +02004663static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4664 .get_status = uwb_get_status,
4665 .set_status = uwb_set_status,
4666};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004667
4668static void uwb_exit(void)
4669{
Johannes Berg19d337d2009-06-02 13:01:37 +02004670 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004671}
4672
4673static int __init uwb_init(struct ibm_init_struct *iibm)
4674{
4675 int res;
4676 int status = 0;
4677
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004678 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4679 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004680
4681 TPACPI_ACPIHANDLE_INIT(hkey);
4682
4683 tp_features.uwb = hkey_handle &&
4684 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4685
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004686 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4687 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004688 str_supported(tp_features.uwb),
4689 status);
4690
4691#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4692 if (dbg_uwbemul) {
4693 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004694 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004695 } else
4696#endif
4697 if (tp_features.uwb &&
4698 !(status & TP_ACPI_UWB_HWPRESENT)) {
4699 /* no uwb hardware present in system */
4700 tp_features.uwb = 0;
4701 dbg_printk(TPACPI_DBG_INIT,
4702 "uwb hardware not installed\n");
4703 }
4704
4705 if (!tp_features.uwb)
4706 return 1;
4707
4708 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004709 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004710 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004711 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004712 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004713 return res;
4714}
4715
4716static struct ibm_struct uwb_driver_data = {
4717 .name = "uwb",
4718 .exit = uwb_exit,
4719 .flags.experimental = 1,
4720};
4721
4722/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004723 * Video subdriver
4724 */
4725
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004726#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4727
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004728enum video_access_mode {
4729 TPACPI_VIDEO_NONE = 0,
4730 TPACPI_VIDEO_570, /* 570 */
4731 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4732 TPACPI_VIDEO_NEW, /* all others */
4733};
4734
4735enum { /* video status flags, based on VIDEO_570 */
4736 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4737 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4738 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4739};
4740
4741enum { /* TPACPI_VIDEO_570 constants */
4742 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4743 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4744 * video_status_flags */
4745 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4746 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4747};
4748
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004749static enum video_access_mode video_supported;
4750static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004751
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004752static int video_autosw_get(void);
4753static int video_autosw_set(int enable);
4754
Joe Perches112a6ee2011-04-04 10:06:24 -07004755TPACPI_HANDLE(vid, root,
4756 "\\_SB.PCI.AGP.VGA", /* 570 */
4757 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4758 "\\_SB.PCI0.VID0", /* 770e */
4759 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4760 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4761 "\\_SB.PCI0.AGP.VID", /* all others */
4762 ); /* R30, R31 */
4763
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004764TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004765
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004766static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004768 int ivga;
4769
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004770 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4771
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004772 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004773 if (tpacpi_is_ibm())
4774 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004775
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004776 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4777 /* G41, assume IVGA doesn't change */
4778 vid_handle = vid2_handle;
4779
4780 if (!vid_handle)
4781 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004782 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004783 else if (tpacpi_is_ibm() &&
4784 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004785 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004786 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004787 else if (tpacpi_is_ibm() &&
4788 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004789 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004790 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004791 else
4792 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004793 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004795 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4796 str_supported(video_supported != TPACPI_VIDEO_NONE),
4797 video_supported);
4798
Jan van den Berg72a979f2014-09-17 00:01:08 +02004799 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800}
4801
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004802static void video_exit(void)
4803{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004804 dbg_printk(TPACPI_DBG_EXIT,
4805 "restoring original video autoswitch mode\n");
4806 if (video_autosw_set(video_orig_autosw))
Joe Perches0978e012011-04-04 10:06:25 -07004807 pr_err("error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004808 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004809}
4810
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004811static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812{
4813 int status = 0;
4814 int i;
4815
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004816 switch (video_supported) {
4817 case TPACPI_VIDEO_570:
4818 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4819 TP_ACPI_VIDEO_570_PHSCMD))
4820 return -EIO;
4821 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4822 break;
4823 case TPACPI_VIDEO_770:
4824 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4825 return -EIO;
4826 if (i)
4827 status |= TP_ACPI_VIDEO_S_LCD;
4828 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4829 return -EIO;
4830 if (i)
4831 status |= TP_ACPI_VIDEO_S_CRT;
4832 break;
4833 case TPACPI_VIDEO_NEW:
4834 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4835 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4836 return -EIO;
4837 if (i)
4838 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004840 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4841 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4842 return -EIO;
4843 if (i)
4844 status |= TP_ACPI_VIDEO_S_LCD;
4845 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4846 return -EIO;
4847 if (i)
4848 status |= TP_ACPI_VIDEO_S_DVI;
4849 break;
4850 default:
4851 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
4854 return status;
4855}
4856
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004857static int video_outputsw_set(int status)
4858{
4859 int autosw;
4860 int res = 0;
4861
4862 switch (video_supported) {
4863 case TPACPI_VIDEO_570:
4864 res = acpi_evalf(NULL, NULL,
4865 "\\_SB.PHS2", "vdd",
4866 TP_ACPI_VIDEO_570_PHS2CMD,
4867 status | TP_ACPI_VIDEO_570_PHS2SET);
4868 break;
4869 case TPACPI_VIDEO_770:
4870 autosw = video_autosw_get();
4871 if (autosw < 0)
4872 return autosw;
4873
4874 res = video_autosw_set(1);
4875 if (res)
4876 return res;
4877 res = acpi_evalf(vid_handle, NULL,
4878 "ASWT", "vdd", status * 0x100, 0);
4879 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004880 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004881 return -EIO;
4882 }
4883 break;
4884 case TPACPI_VIDEO_NEW:
4885 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004886 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004887 break;
4888 default:
4889 return -ENOSYS;
4890 }
4891
Jan van den Berg72a979f2014-09-17 00:01:08 +02004892 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004893}
4894
4895static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004897 int autosw = 0;
4898
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004899 switch (video_supported) {
4900 case TPACPI_VIDEO_570:
4901 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4902 return -EIO;
4903 break;
4904 case TPACPI_VIDEO_770:
4905 case TPACPI_VIDEO_NEW:
4906 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4907 return -EIO;
4908 break;
4909 default:
4910 return -ENOSYS;
4911 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004912
4913 return autosw & 1;
4914}
4915
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004916static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004917{
Jan van den Berg72a979f2014-09-17 00:01:08 +02004918 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004919 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004920 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004921}
4922
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004923static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004924{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004925 int autosw = video_autosw_get();
4926 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004927
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004928 if (autosw < 0)
4929 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004930
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004931 switch (video_supported) {
4932 case TPACPI_VIDEO_570:
4933 res = video_autosw_set(1);
4934 if (res)
4935 return res;
4936 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4937 break;
4938 case TPACPI_VIDEO_770:
4939 case TPACPI_VIDEO_NEW:
4940 res = video_autosw_set(1);
4941 if (res)
4942 return res;
4943 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4944 break;
4945 default:
4946 return -ENOSYS;
4947 }
4948 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004949 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004950 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004951 }
4952
Jan van den Berg72a979f2014-09-17 00:01:08 +02004953 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004954}
4955
4956static int video_expand_toggle(void)
4957{
4958 switch (video_supported) {
4959 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004960 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004961 0 : -EIO;
4962 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004963 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004964 0 : -EIO;
4965 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004966 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004967 0 : -EIO;
4968 default:
4969 return -ENOSYS;
4970 }
4971 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004972}
4973
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004974static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004975{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004976 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004977
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004978 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004979 seq_printf(m, "status:\t\tnot supported\n");
4980 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004981 }
4982
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004983 /* Even reads can crash X.org, so... */
4984 if (!capable(CAP_SYS_ADMIN))
4985 return -EPERM;
4986
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004987 status = video_outputsw_get();
4988 if (status < 0)
4989 return status;
4990
4991 autosw = video_autosw_get();
4992 if (autosw < 0)
4993 return autosw;
4994
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004995 seq_printf(m, "status:\t\tsupported\n");
4996 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4997 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004998 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004999 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5000 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5001 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5002 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005003 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005004 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5005 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5006 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005007
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005008 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005009}
5010
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005011static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012{
5013 char *cmd;
5014 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005015 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005017 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005018 return -ENODEV;
5019
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005020 /* Even reads can crash X.org, let alone writes... */
5021 if (!capable(CAP_SYS_ADMIN))
5022 return -EPERM;
5023
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005024 enable = 0;
5025 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
5027 while ((cmd = next_cmd(&buf))) {
5028 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005029 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005031 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005033 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005035 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005036 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005037 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005038 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005039 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005040 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005041 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005043 res = video_autosw_set(1);
5044 if (res)
5045 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005047 res = video_autosw_set(0);
5048 if (res)
5049 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005051 res = video_outputsw_cycle();
5052 if (res)
5053 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005055 res = video_expand_toggle();
5056 if (res)
5057 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 } else
5059 return -EINVAL;
5060 }
5061
5062 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005063 status = video_outputsw_get();
5064 if (status < 0)
5065 return status;
5066 res = video_outputsw_set((status & ~disable) | enable);
5067 if (res)
5068 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 }
5070
5071 return 0;
5072}
5073
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005074static struct ibm_struct video_driver_data = {
5075 .name = "video",
5076 .read = video_read,
5077 .write = video_write,
5078 .exit = video_exit,
5079};
5080
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005081#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5082
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005083/*************************************************************************
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005084 * Keyboard backlight subdriver
5085 */
5086
5087static int kbdlight_set_level(int level)
5088{
5089 if (!hkey_handle)
5090 return -ENXIO;
5091
5092 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5093 return -EIO;
5094
5095 return 0;
5096}
5097
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005098static int kbdlight_set_level_and_update(int level);
5099
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005100static int kbdlight_get_level(void)
5101{
5102 int status = 0;
5103
5104 if (!hkey_handle)
5105 return -ENXIO;
5106
5107 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5108 return -EIO;
5109
5110 if (status < 0)
5111 return status;
5112
5113 return status & 0x3;
5114}
5115
5116static bool kbdlight_is_supported(void)
5117{
5118 int status = 0;
5119
5120 if (!hkey_handle)
5121 return false;
5122
5123 if (!acpi_has_method(hkey_handle, "MLCG")) {
5124 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5125 return false;
5126 }
5127
5128 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5129 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5130 return false;
5131 }
5132
5133 if (status < 0) {
5134 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5135 return false;
5136 }
5137
5138 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5139 /*
5140 * Guessed test for keyboard backlight:
5141 *
5142 * Machines with backlight keyboard return:
5143 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5144 * b110100010010000000XX - ThinkPad x230
5145 * b010100000010000000XX - ThinkPad x240
5146 * b010100000010000000XX - ThinkPad W541
5147 * (XX is current backlight level)
5148 *
5149 * Machines without backlight keyboard return:
5150 * b10100001000000000000 - ThinkPad x230
5151 * b10110001000000000000 - ThinkPad E430
5152 * b00000000000000000000 - ThinkPad E450
5153 *
5154 * Candidate BITs for detection test (XOR):
5155 * b01000000001000000000
5156 * ^
5157 */
5158 return status & BIT(9);
5159}
5160
5161static void kbdlight_set_worker(struct work_struct *work)
5162{
5163 struct tpacpi_led_classdev *data =
5164 container_of(work, struct tpacpi_led_classdev, work);
5165
5166 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005167 kbdlight_set_level_and_update(data->new_state);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005168}
5169
5170static void kbdlight_sysfs_set(struct led_classdev *led_cdev,
5171 enum led_brightness brightness)
5172{
5173 struct tpacpi_led_classdev *data =
5174 container_of(led_cdev,
5175 struct tpacpi_led_classdev,
5176 led_classdev);
5177 data->new_state = brightness;
5178 queue_work(tpacpi_wq, &data->work);
5179}
5180
5181static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5182{
5183 int level;
5184
5185 level = kbdlight_get_level();
5186 if (level < 0)
5187 return 0;
5188
5189 return level;
5190}
5191
5192static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5193 .led_classdev = {
5194 .name = "tpacpi::kbd_backlight",
5195 .max_brightness = 2,
5196 .brightness_set = &kbdlight_sysfs_set,
5197 .brightness_get = &kbdlight_sysfs_get,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005198 }
5199};
5200
5201static int __init kbdlight_init(struct ibm_init_struct *iibm)
5202{
5203 int rc;
5204
5205 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5206
5207 TPACPI_ACPIHANDLE_INIT(hkey);
5208 INIT_WORK(&tpacpi_led_kbdlight.work, kbdlight_set_worker);
5209
5210 if (!kbdlight_is_supported()) {
5211 tp_features.kbdlight = 0;
5212 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5213 return 1;
5214 }
5215
5216 tp_features.kbdlight = 1;
5217
5218 rc = led_classdev_register(&tpacpi_pdev->dev,
5219 &tpacpi_led_kbdlight.led_classdev);
5220 if (rc < 0) {
5221 tp_features.kbdlight = 0;
5222 return rc;
5223 }
5224
5225 return 0;
5226}
5227
5228static void kbdlight_exit(void)
5229{
5230 if (tp_features.kbdlight)
5231 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5232 flush_workqueue(tpacpi_wq);
5233}
5234
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005235static int kbdlight_set_level_and_update(int level)
5236{
5237 int ret;
5238 struct led_classdev *led_cdev;
5239
5240 ret = kbdlight_set_level(level);
5241 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5242
5243 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5244 led_cdev->brightness = level;
5245
5246 return ret;
5247}
5248
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005249static int kbdlight_read(struct seq_file *m)
5250{
5251 int level;
5252
5253 if (!tp_features.kbdlight) {
5254 seq_printf(m, "status:\t\tnot supported\n");
5255 } else {
5256 level = kbdlight_get_level();
5257 if (level < 0)
5258 seq_printf(m, "status:\t\terror %d\n", level);
5259 else
5260 seq_printf(m, "status:\t\t%d\n", level);
5261 seq_printf(m, "commands:\t0, 1, 2\n");
5262 }
5263
5264 return 0;
5265}
5266
5267static int kbdlight_write(char *buf)
5268{
5269 char *cmd;
5270 int level = -1;
5271
5272 if (!tp_features.kbdlight)
5273 return -ENODEV;
5274
5275 while ((cmd = next_cmd(&buf))) {
5276 if (strlencmp(cmd, "0") == 0)
5277 level = 0;
5278 else if (strlencmp(cmd, "1") == 0)
5279 level = 1;
5280 else if (strlencmp(cmd, "2") == 0)
5281 level = 2;
5282 else
5283 return -EINVAL;
5284 }
5285
5286 if (level == -1)
5287 return -EINVAL;
5288
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005289 return kbdlight_set_level_and_update(level);
5290}
5291
5292static void kbdlight_suspend(void)
5293{
5294 struct led_classdev *led_cdev;
5295
5296 if (!tp_features.kbdlight)
5297 return;
5298
5299 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5300 led_update_brightness(led_cdev);
5301 led_classdev_suspend(led_cdev);
5302}
5303
5304static void kbdlight_resume(void)
5305{
5306 if (!tp_features.kbdlight)
5307 return;
5308
5309 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005310}
5311
5312static struct ibm_struct kbdlight_driver_data = {
5313 .name = "kbdlight",
5314 .read = kbdlight_read,
5315 .write = kbdlight_write,
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005316 .suspend = kbdlight_suspend,
5317 .resume = kbdlight_resume,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005318 .exit = kbdlight_exit,
5319};
5320
5321/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005322 * Light (thinklight) subdriver
5323 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005325TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5326TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005327
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005328static int light_get_status(void)
5329{
5330 int status = 0;
5331
5332 if (tp_features.light_status) {
5333 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5334 return -EIO;
5335 return (!!status);
5336 }
5337
5338 return -ENXIO;
5339}
5340
5341static int light_set_status(int status)
5342{
5343 int rc;
5344
5345 if (tp_features.light) {
5346 if (cmos_handle) {
5347 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005348 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005349 TP_CMOS_THINKLIGHT_ON :
5350 TP_CMOS_THINKLIGHT_OFF);
5351 } else {
5352 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005353 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005354 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005355 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005356 }
5357
5358 return -ENXIO;
5359}
5360
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005361static void light_set_status_worker(struct work_struct *work)
5362{
5363 struct tpacpi_led_classdev *data =
5364 container_of(work, struct tpacpi_led_classdev, work);
5365
5366 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005367 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005368}
5369
5370static void light_sysfs_set(struct led_classdev *led_cdev,
5371 enum led_brightness brightness)
5372{
5373 struct tpacpi_led_classdev *data =
5374 container_of(led_cdev,
5375 struct tpacpi_led_classdev,
5376 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005377 data->new_state = (brightness != LED_OFF) ?
5378 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005379 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005380}
5381
5382static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5383{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005384 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005385}
5386
5387static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5388 .led_classdev = {
5389 .name = "tpacpi::thinklight",
5390 .brightness_set = &light_sysfs_set,
5391 .brightness_get = &light_sysfs_get,
5392 }
5393};
5394
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005395static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005397 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005398
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005399 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5400
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005401 if (tpacpi_is_ibm()) {
5402 TPACPI_ACPIHANDLE_INIT(ledb);
5403 TPACPI_ACPIHANDLE_INIT(lght);
5404 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005405 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005406 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005407
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005408 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005409 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005410
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005411 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005412 /* light status not supported on
5413 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005414 tp_features.light_status =
5415 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005417 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5418 str_supported(tp_features.light),
5419 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005420
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005421 if (!tp_features.light)
5422 return 1;
5423
5424 rc = led_classdev_register(&tpacpi_pdev->dev,
5425 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005426
5427 if (rc < 0) {
5428 tp_features.light = 0;
5429 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005430 } else {
5431 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005432 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005433
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005434 return rc;
5435}
5436
5437static void light_exit(void)
5438{
5439 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Tejun Heo6d394e12012-12-21 17:56:58 -08005440 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441}
5442
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005443static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005445 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005447 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005448 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005449 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005450 seq_printf(m, "status:\t\tunknown\n");
5451 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005452 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005453 status = light_get_status();
5454 if (status < 0)
5455 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005456 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5457 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005460 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461}
5462
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005463static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005466 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005467
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005468 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005469 return -ENODEV;
5470
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 while ((cmd = next_cmd(&buf))) {
5472 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005473 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005475 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 } else
5477 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 }
5479
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005480 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481}
5482
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005483static struct ibm_struct light_driver_data = {
5484 .name = "light",
5485 .read = light_read,
5486 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005487 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005488};
5489
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005490/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005491 * CMOS subdriver
5492 */
5493
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005494/* sysfs cmos_command -------------------------------------------------- */
5495static ssize_t cmos_command_store(struct device *dev,
5496 struct device_attribute *attr,
5497 const char *buf, size_t count)
5498{
5499 unsigned long cmos_cmd;
5500 int res;
5501
5502 if (parse_strtoul(buf, 21, &cmos_cmd))
5503 return -EINVAL;
5504
5505 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005506 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005507}
5508
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01005509static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005510
5511/* --------------------------------------------------------------------- */
5512
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005513static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005514{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005515 int res;
5516
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005517 vdbg_printk(TPACPI_DBG_INIT,
5518 "initializing cmos commands subdriver\n");
5519
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005520 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005521
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005522 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5523 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005524
5525 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5526 if (res)
5527 return res;
5528
Jan van den Berg72a979f2014-09-17 00:01:08 +02005529 return (cmos_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005530}
5531
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005532static void cmos_exit(void)
5533{
5534 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5535}
5536
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005537static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005539 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5540 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005542 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005544 seq_printf(m, "status:\t\tsupported\n");
5545 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 }
5547
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549}
5550
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005551static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552{
5553 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005554 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
5556 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005557 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5558 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 /* cmos_cmd set */
5560 } else
5561 return -EINVAL;
5562
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005563 res = issue_thinkpad_cmos_command(cmos_cmd);
5564 if (res)
5565 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 }
5567
5568 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005569}
5570
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005571static struct ibm_struct cmos_driver_data = {
5572 .name = "cmos",
5573 .read = cmos_read,
5574 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005575 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005576};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005577
5578/*************************************************************************
5579 * LED subdriver
5580 */
5581
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005582enum led_access_mode {
5583 TPACPI_LED_NONE = 0,
5584 TPACPI_LED_570, /* 570 */
5585 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5586 TPACPI_LED_NEW, /* all others */
5587};
5588
5589enum { /* For TPACPI_LED_OLD */
5590 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5591 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5592 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5593};
5594
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005595static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005596
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005597static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005598
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005599#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005600static struct tpacpi_led_classdev *tpacpi_leds;
5601static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005602static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005603 /* there's a limit of 19 chars + NULL before 2.6.26 */
5604 "tpacpi::power",
5605 "tpacpi:orange:batt",
5606 "tpacpi:green:batt",
5607 "tpacpi::dock_active",
5608 "tpacpi::bay_active",
5609 "tpacpi::dock_batt",
5610 "tpacpi::unknown_led",
5611 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005612 "tpacpi::dock_status1",
5613 "tpacpi::dock_status2",
5614 "tpacpi::unknown_led2",
5615 "tpacpi::unknown_led3",
5616 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005617};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005618#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005619
5620static inline bool tpacpi_is_led_restricted(const unsigned int led)
5621{
5622#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5623 return false;
5624#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005625 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005626#endif
5627}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005628
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005629static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005630{
5631 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005632 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005633
5634 switch (led_supported) {
5635 case TPACPI_LED_570:
5636 if (!acpi_evalf(ec_handle,
5637 &status, "GLED", "dd", 1 << led))
5638 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005639 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005640 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005641 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005642 TPACPI_LED_ON :
5643 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005644 tpacpi_led_state_cache[led] = led_s;
5645 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005646 default:
5647 return -ENXIO;
5648 }
5649
5650 /* not reached */
5651}
5652
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005653static int led_set_status(const unsigned int led,
5654 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005655{
5656 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005657 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5658 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005659
5660 int rc = 0;
5661
5662 switch (led_supported) {
5663 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005664 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005665 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005666 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005667 if (unlikely(tpacpi_is_led_restricted(led)))
5668 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005669 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5670 (1 << led), led_sled_arg1[ledstatus]))
5671 rc = -EIO;
5672 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005673 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005674 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005675 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005676 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005677 if (unlikely(tpacpi_is_led_restricted(led)))
5678 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005679 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5680 if (rc >= 0)
5681 rc = ec_write(TPACPI_LED_EC_HLBL,
5682 (ledstatus == TPACPI_LED_BLINK) << led);
5683 if (rc >= 0)
5684 rc = ec_write(TPACPI_LED_EC_HLCL,
5685 (ledstatus != TPACPI_LED_OFF) << led);
5686 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005687 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005688 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005689 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5690 return -EINVAL;
5691 if (unlikely(tpacpi_is_led_restricted(led)))
5692 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005693 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5694 led, led_led_arg1[ledstatus]))
5695 rc = -EIO;
5696 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005697 default:
5698 rc = -ENXIO;
5699 }
5700
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005701 if (!rc)
5702 tpacpi_led_state_cache[led] = ledstatus;
5703
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005704 return rc;
5705}
5706
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005707static void led_set_status_worker(struct work_struct *work)
5708{
5709 struct tpacpi_led_classdev *data =
5710 container_of(work, struct tpacpi_led_classdev, work);
5711
5712 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005713 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005714}
5715
5716static void led_sysfs_set(struct led_classdev *led_cdev,
5717 enum led_brightness brightness)
5718{
5719 struct tpacpi_led_classdev *data = container_of(led_cdev,
5720 struct tpacpi_led_classdev, led_classdev);
5721
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005722 if (brightness == LED_OFF)
5723 data->new_state = TPACPI_LED_OFF;
5724 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5725 data->new_state = TPACPI_LED_ON;
5726 else
5727 data->new_state = TPACPI_LED_BLINK;
5728
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005729 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005730}
5731
5732static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5733 unsigned long *delay_on, unsigned long *delay_off)
5734{
5735 struct tpacpi_led_classdev *data = container_of(led_cdev,
5736 struct tpacpi_led_classdev, led_classdev);
5737
5738 /* Can we choose the flash rate? */
5739 if (*delay_on == 0 && *delay_off == 0) {
5740 /* yes. set them to the hardware blink rate (1 Hz) */
5741 *delay_on = 500; /* ms */
5742 *delay_off = 500; /* ms */
5743 } else if ((*delay_on != 500) || (*delay_off != 500))
5744 return -EINVAL;
5745
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005746 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005747 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005748
5749 return 0;
5750}
5751
5752static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5753{
5754 int rc;
5755
5756 struct tpacpi_led_classdev *data = container_of(led_cdev,
5757 struct tpacpi_led_classdev, led_classdev);
5758
5759 rc = led_get_status(data->led);
5760
5761 if (rc == TPACPI_LED_OFF || rc < 0)
5762 rc = LED_OFF; /* no error handling in led class :( */
5763 else
5764 rc = LED_FULL;
5765
5766 return rc;
5767}
5768
5769static void led_exit(void)
5770{
5771 unsigned int i;
5772
5773 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5774 if (tpacpi_leds[i].led_classdev.name)
5775 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5776 }
5777
Li Dongyange03e3892012-07-25 10:45:07 +10005778 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005779 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005780}
5781
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005782static int __init tpacpi_init_led(unsigned int led)
5783{
5784 int rc;
5785
5786 tpacpi_leds[led].led = led;
5787
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005788 /* LEDs with no name don't get registered */
5789 if (!tpacpi_led_names[led])
5790 return 0;
5791
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005792 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5793 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5794 if (led_supported == TPACPI_LED_570)
5795 tpacpi_leds[led].led_classdev.brightness_get =
5796 &led_sysfs_get;
5797
5798 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5799
5800 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5801
5802 rc = led_classdev_register(&tpacpi_pdev->dev,
5803 &tpacpi_leds[led].led_classdev);
5804 if (rc < 0)
5805 tpacpi_leds[led].led_classdev.name = NULL;
5806
5807 return rc;
5808}
5809
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005810static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5811 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5812 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5813 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5814
5815 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5816 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5817 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5818 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5819 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5820 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5821 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5822 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5823
5824 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5825 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5826 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5827 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5828 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5829
5830 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5831 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5832 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5833 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5834
5835 /* (1) - may have excess leds enabled on MSB */
5836
5837 /* Defaults (order matters, keep last, don't reorder!) */
5838 { /* Lenovo */
5839 .vendor = PCI_VENDOR_ID_LENOVO,
5840 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5841 .quirks = 0x1fffU,
5842 },
5843 { /* IBM ThinkPads with no EC version string */
5844 .vendor = PCI_VENDOR_ID_IBM,
5845 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5846 .quirks = 0x00ffU,
5847 },
5848 { /* IBM ThinkPads with EC version string */
5849 .vendor = PCI_VENDOR_ID_IBM,
5850 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5851 .quirks = 0x00bfU,
5852 },
5853};
5854
5855#undef TPACPI_LEDQ_IBM
5856#undef TPACPI_LEDQ_LNV
5857
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005858static enum led_access_mode __init led_init_detect_mode(void)
5859{
5860 acpi_status status;
5861
5862 if (tpacpi_is_ibm()) {
5863 /* 570 */
5864 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5865 if (ACPI_SUCCESS(status))
5866 return TPACPI_LED_570;
5867
5868 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5869 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5870 if (ACPI_SUCCESS(status))
5871 return TPACPI_LED_OLD;
5872 }
5873
5874 /* most others */
5875 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5876 if (ACPI_SUCCESS(status))
5877 return TPACPI_LED_NEW;
5878
5879 /* R30, R31, and unknown firmwares */
5880 led_handle = NULL;
5881 return TPACPI_LED_NONE;
5882}
5883
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005884static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005885{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005886 unsigned int i;
5887 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005888 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005889
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005890 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5891
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005892 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005893
Adam Leefcb44e12013-06-07 16:20:08 +08005894 if (led_supported != TPACPI_LED_NONE) {
5895 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5896 ARRAY_SIZE(led_useful_qtable));
5897
5898 if (!useful_leds) {
5899 led_handle = NULL;
5900 led_supported = TPACPI_LED_NONE;
5901 }
5902 }
5903
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005904 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5905 str_supported(led_supported), led_supported);
5906
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005907 if (led_supported == TPACPI_LED_NONE)
5908 return 1;
5909
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005910 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5911 GFP_KERNEL);
5912 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005913 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005914 return -ENOMEM;
5915 }
5916
5917 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08005918 tpacpi_leds[i].led = -1;
5919
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005920 if (!tpacpi_is_led_restricted(i) &&
5921 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005922 rc = tpacpi_init_led(i);
5923 if (rc < 0) {
5924 led_exit();
5925 return rc;
5926 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005927 }
5928 }
5929
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005930#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Joe Perches0978e012011-04-04 10:06:25 -07005931 pr_notice("warning: userspace override of important "
5932 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005933#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005934 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005935}
5936
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005937#define str_led_status(s) \
5938 ((s) == TPACPI_LED_OFF ? "off" : \
5939 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005940
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005941static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005943 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005944 seq_printf(m, "status:\t\tnot supported\n");
5945 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005946 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005947 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005948
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005949 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005950 /* 570 */
5951 int i, status;
5952 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005953 status = led_get_status(i);
5954 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005955 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005956 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005957 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005958 }
5959 }
5960
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005961 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005962 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005964 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965}
5966
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005967static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968{
5969 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005970 int led, rc;
5971 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005972
5973 if (!led_supported)
5974 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975
5976 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08005977 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 return -EINVAL;
5979
Adam Leeedf2d772013-06-08 16:51:15 +08005980 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5981 tpacpi_leds[led].led < 0)
5982 return -ENODEV;
5983
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005984 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005985 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005987 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005988 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005989 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005990 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005991 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005992 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005993
5994 rc = led_set_status(led, s);
5995 if (rc < 0)
5996 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 }
5998
5999 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006000}
6001
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006002static struct ibm_struct led_driver_data = {
6003 .name = "led",
6004 .read = led_read,
6005 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03006006 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006007};
6008
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006009/*************************************************************************
6010 * Beep subdriver
6011 */
6012
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006013TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006014
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006015#define TPACPI_BEEP_Q1 0x0001
6016
6017static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6018 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6019 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6020};
6021
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006022static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006023{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006024 unsigned long quirks;
6025
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006026 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6027
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006028 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006029
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006030 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6031 str_supported(beep_handle != NULL));
6032
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006033 quirks = tpacpi_check_quirks(beep_quirk_table,
6034 ARRAY_SIZE(beep_quirk_table));
6035
6036 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6037
Jan van den Berg72a979f2014-09-17 00:01:08 +02006038 return (beep_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006039}
6040
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006041static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006043 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006044 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006045 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006046 seq_printf(m, "status:\t\tsupported\n");
6047 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006050 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051}
6052
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006053static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054{
6055 char *cmd;
6056 int beep_cmd;
6057
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006058 if (!beep_handle)
6059 return -ENODEV;
6060
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006062 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6063 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 /* beep_cmd set */
6065 } else
6066 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006067 if (tp_features.beep_needs_two_args) {
6068 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6069 beep_cmd, 0))
6070 return -EIO;
6071 } else {
6072 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6073 beep_cmd))
6074 return -EIO;
6075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 }
6077
6078 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006079}
6080
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006081static struct ibm_struct beep_driver_data = {
6082 .name = "beep",
6083 .read = beep_read,
6084 .write = beep_write,
6085};
6086
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006087/*************************************************************************
6088 * Thermal subdriver
6089 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006090
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006091enum thermal_access_mode {
6092 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6093 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6094 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6095 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6096 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6097};
6098
6099enum { /* TPACPI_THERMAL_TPEC_* */
6100 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6101 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6102 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006103
6104 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006105};
6106
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006107
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006108#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6109struct ibm_thermal_sensors_struct {
6110 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6111};
6112
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006113static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006114
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006115/* idx is zero-based */
6116static int thermal_get_sensor(int idx, s32 *value)
6117{
6118 int t;
6119 s8 tmp;
6120 char tmpi[5];
6121
6122 t = TP_EC_THERMAL_TMP0;
6123
6124 switch (thermal_read_mode) {
6125#if TPACPI_MAX_THERMAL_SENSORS >= 16
6126 case TPACPI_THERMAL_TPEC_16:
6127 if (idx >= 8 && idx <= 15) {
6128 t = TP_EC_THERMAL_TMP8;
6129 idx -= 8;
6130 }
6131 /* fallthrough */
6132#endif
6133 case TPACPI_THERMAL_TPEC_8:
6134 if (idx <= 7) {
6135 if (!acpi_ec_read(t + idx, &tmp))
6136 return -EIO;
6137 *value = tmp * 1000;
6138 return 0;
6139 }
6140 break;
6141
6142 case TPACPI_THERMAL_ACPI_UPDT:
6143 if (idx <= 7) {
6144 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6145 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6146 return -EIO;
6147 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6148 return -EIO;
6149 *value = (t - 2732) * 100;
6150 return 0;
6151 }
6152 break;
6153
6154 case TPACPI_THERMAL_ACPI_TMP07:
6155 if (idx <= 7) {
6156 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6157 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6158 return -EIO;
6159 if (t > 127 || t < -127)
6160 t = TP_EC_THERMAL_TMP_NA;
6161 *value = t * 1000;
6162 return 0;
6163 }
6164 break;
6165
6166 case TPACPI_THERMAL_NONE:
6167 default:
6168 return -ENOSYS;
6169 }
6170
6171 return -EINVAL;
6172}
6173
6174static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6175{
6176 int res, i;
6177 int n;
6178
6179 n = 8;
6180 i = 0;
6181
6182 if (!s)
6183 return -EINVAL;
6184
6185 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6186 n = 16;
6187
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006188 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006189 res = thermal_get_sensor(i, &s->temp[i]);
6190 if (res)
6191 return res;
6192 }
6193
6194 return n;
6195}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006196
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006197static void thermal_dump_all_sensors(void)
6198{
6199 int n, i;
6200 struct ibm_thermal_sensors_struct t;
6201
6202 n = thermal_get_sensors(&t);
6203 if (n <= 0)
6204 return;
6205
Joe Perches0978e012011-04-04 10:06:25 -07006206 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006207
6208 for (i = 0; i < n; i++) {
6209 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07006210 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006211 else
Joe Perches0978e012011-04-04 10:06:25 -07006212 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006213 }
6214
Joe Perches0978e012011-04-04 10:06:25 -07006215 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006216}
6217
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006218/* sysfs temp##_input -------------------------------------------------- */
6219
6220static ssize_t thermal_temp_input_show(struct device *dev,
6221 struct device_attribute *attr,
6222 char *buf)
6223{
6224 struct sensor_device_attribute *sensor_attr =
6225 to_sensor_dev_attr(attr);
6226 int idx = sensor_attr->index;
6227 s32 value;
6228 int res;
6229
6230 res = thermal_get_sensor(idx, &value);
6231 if (res)
6232 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006233 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006234 return -ENXIO;
6235
6236 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6237}
6238
6239#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006240 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6241 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006242
6243static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6244 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6245 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6246 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6247 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6248 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6249 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6250 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6251 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6252 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6253 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6254 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6255 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6256 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6257 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6258 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6259 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6260};
6261
6262#define THERMAL_ATTRS(X) \
6263 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6264
6265static struct attribute *thermal_temp_input_attr[] = {
6266 THERMAL_ATTRS(8),
6267 THERMAL_ATTRS(9),
6268 THERMAL_ATTRS(10),
6269 THERMAL_ATTRS(11),
6270 THERMAL_ATTRS(12),
6271 THERMAL_ATTRS(13),
6272 THERMAL_ATTRS(14),
6273 THERMAL_ATTRS(15),
6274 THERMAL_ATTRS(0),
6275 THERMAL_ATTRS(1),
6276 THERMAL_ATTRS(2),
6277 THERMAL_ATTRS(3),
6278 THERMAL_ATTRS(4),
6279 THERMAL_ATTRS(5),
6280 THERMAL_ATTRS(6),
6281 THERMAL_ATTRS(7),
6282 NULL
6283};
6284
6285static const struct attribute_group thermal_temp_input16_group = {
6286 .attrs = thermal_temp_input_attr
6287};
6288
6289static const struct attribute_group thermal_temp_input8_group = {
6290 .attrs = &thermal_temp_input_attr[8]
6291};
6292
6293#undef THERMAL_SENSOR_ATTR_TEMP
6294#undef THERMAL_ATTRS
6295
6296/* --------------------------------------------------------------------- */
6297
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006298static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006299{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006300 u8 t, ta1, ta2;
6301 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006302 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006303 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006304
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006305 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6306
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006307 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006308
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03006309 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006310 /*
6311 * Direct EC access mode: sensors at registers
6312 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6313 * non-implemented, thermal sensors return 0x80 when
6314 * not available
6315 */
6316
6317 ta1 = ta2 = 0;
6318 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006319 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006320 ta1 |= t;
6321 } else {
6322 ta1 = 0;
6323 break;
6324 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006325 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006326 ta2 |= t;
6327 } else {
6328 ta1 = 0;
6329 break;
6330 }
6331 }
6332 if (ta1 == 0) {
6333 /* This is sheer paranoia, but we handle it anyway */
6334 if (acpi_tmp7) {
Joe Perches0978e012011-04-04 10:06:25 -07006335 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006336 "falling back to ACPI TMPx access "
6337 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006338 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006339 } else {
Joe Perches0978e012011-04-04 10:06:25 -07006340 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006341 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006342 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006343 }
6344 } else {
6345 thermal_read_mode =
6346 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006347 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006348 }
6349 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006350 if (tpacpi_is_ibm() &&
6351 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006352 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006353 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006354 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006355 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006356 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006357 }
6358 } else {
6359 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006360 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006361 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006362
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006363 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6364 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6365 thermal_read_mode);
6366
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006367 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006368 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006369 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006370 &thermal_temp_input16_group);
6371 if (res)
6372 return res;
6373 break;
6374 case TPACPI_THERMAL_TPEC_8:
6375 case TPACPI_THERMAL_ACPI_TMP07:
6376 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006377 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006378 &thermal_temp_input8_group);
6379 if (res)
6380 return res;
6381 break;
6382 case TPACPI_THERMAL_NONE:
6383 default:
6384 return 1;
6385 }
6386
6387 return 0;
6388}
6389
6390static void thermal_exit(void)
6391{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006392 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006393 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006394 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006395 &thermal_temp_input16_group);
6396 break;
6397 case TPACPI_THERMAL_TPEC_8:
6398 case TPACPI_THERMAL_ACPI_TMP07:
6399 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006400 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08006401 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006402 break;
6403 case TPACPI_THERMAL_NONE:
6404 default:
6405 break;
6406 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006407}
6408
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006409static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006410{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006411 int n, i;
6412 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006413
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006414 n = thermal_get_sensors(&t);
6415 if (unlikely(n < 0))
6416 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006417
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006418 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006419
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006420 if (n > 0) {
6421 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006422 seq_printf(m, "%d ", t.temp[i] / 1000);
6423 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006424 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006425 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006426
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006427 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006428}
6429
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006430static struct ibm_struct thermal_driver_data = {
6431 .name = "thermal",
6432 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006433 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006434};
6435
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006436/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006437 * Backlight/brightness subdriver
6438 */
Holger Macht8acb0252006-10-20 14:30:28 -07006439
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006440#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6441
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006442/*
6443 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6444 * CMOS NVRAM byte 0x5E, bits 0-3.
6445 *
6446 * EC HBRV (0x31) has the following layout
6447 * Bit 7: unknown function
6448 * Bit 6: unknown function
6449 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6450 * Bit 4: must be set to zero to avoid problems
6451 * Bit 3-0: backlight brightness level
6452 *
6453 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006454 *
6455 * WARNING: The X61 has been verified to use HBRV for something else, so
6456 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6457 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006458 */
6459
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006460enum {
6461 TP_EC_BACKLIGHT = 0x31,
6462
6463 /* TP_EC_BACKLIGHT bitmasks */
6464 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6465 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6466 TP_EC_BACKLIGHT_MAPSW = 0x20,
6467};
6468
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006469enum tpacpi_brightness_access_mode {
6470 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6471 TPACPI_BRGHT_MODE_EC, /* EC control */
6472 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6473 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6474 TPACPI_BRGHT_MODE_MAX
6475};
6476
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006477static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006478
6479static enum tpacpi_brightness_access_mode brightness_mode =
6480 TPACPI_BRGHT_MODE_MAX;
6481
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006482static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6483
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006484static struct mutex brightness_mutex;
6485
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006486/* NVRAM brightness access,
6487 * call with brightness_mutex held! */
6488static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006489{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006490 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006491
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006492 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6493 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6494 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006495 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006496
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006497 return lnvram;
6498}
6499
6500static void tpacpi_brightness_checkpoint_nvram(void)
6501{
6502 u8 lec = 0;
6503 u8 b_nvram;
6504
6505 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6506 return;
6507
6508 vdbg_printk(TPACPI_DBG_BRGHT,
6509 "trying to checkpoint backlight level to NVRAM...\n");
6510
6511 if (mutex_lock_killable(&brightness_mutex) < 0)
6512 return;
6513
6514 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6515 goto unlock;
6516 lec &= TP_EC_BACKLIGHT_LVLMSK;
6517 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6518
6519 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6520 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6521 /* NVRAM needs update */
6522 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6523 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6524 b_nvram |= lec;
6525 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6526 dbg_printk(TPACPI_DBG_BRGHT,
6527 "updated NVRAM backlight level to %u (0x%02x)\n",
6528 (unsigned int) lec, (unsigned int) b_nvram);
6529 } else
6530 vdbg_printk(TPACPI_DBG_BRGHT,
6531 "NVRAM backlight level already is %u (0x%02x)\n",
6532 (unsigned int) lec, (unsigned int) b_nvram);
6533
6534unlock:
6535 mutex_unlock(&brightness_mutex);
6536}
6537
6538
6539/* call with brightness_mutex held! */
6540static int tpacpi_brightness_get_raw(int *status)
6541{
6542 u8 lec = 0;
6543
6544 switch (brightness_mode) {
6545 case TPACPI_BRGHT_MODE_UCMS_STEP:
6546 *status = tpacpi_brightness_nvram_get();
6547 return 0;
6548 case TPACPI_BRGHT_MODE_EC:
6549 case TPACPI_BRGHT_MODE_ECNVRAM:
6550 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006551 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006552 *status = lec;
6553 return 0;
6554 default:
6555 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006556 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006557}
6558
6559/* call with brightness_mutex held! */
6560/* do NOT call with illegal backlight level value */
6561static int tpacpi_brightness_set_ec(unsigned int value)
6562{
6563 u8 lec = 0;
6564
6565 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6566 return -EIO;
6567
6568 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6569 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6570 (value & TP_EC_BACKLIGHT_LVLMSK))))
6571 return -EIO;
6572
6573 return 0;
6574}
6575
6576/* call with brightness_mutex held! */
6577static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6578{
6579 int cmos_cmd, inc;
6580 unsigned int current_value, i;
6581
6582 current_value = tpacpi_brightness_nvram_get();
6583
6584 if (value == current_value)
6585 return 0;
6586
6587 cmos_cmd = (value > current_value) ?
6588 TP_CMOS_BRIGHTNESS_UP :
6589 TP_CMOS_BRIGHTNESS_DOWN;
6590 inc = (value > current_value) ? 1 : -1;
6591
6592 for (i = current_value; i != value; i += inc)
6593 if (issue_thinkpad_cmos_command(cmos_cmd))
6594 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006595
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006596 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006597}
6598
6599/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006600static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006601{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006602 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006603
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006604 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006605 return -EINVAL;
6606
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006607 vdbg_printk(TPACPI_DBG_BRGHT,
6608 "set backlight level to %d\n", value);
6609
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006610 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006611 if (res < 0)
6612 return res;
6613
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006614 switch (brightness_mode) {
6615 case TPACPI_BRGHT_MODE_EC:
6616 case TPACPI_BRGHT_MODE_ECNVRAM:
6617 res = tpacpi_brightness_set_ec(value);
6618 break;
6619 case TPACPI_BRGHT_MODE_UCMS_STEP:
6620 res = tpacpi_brightness_set_ucmsstep(value);
6621 break;
6622 default:
6623 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006624 }
6625
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006626 mutex_unlock(&brightness_mutex);
6627 return res;
6628}
6629
6630/* sysfs backlight class ----------------------------------------------- */
6631
6632static int brightness_update_status(struct backlight_device *bd)
6633{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006634 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006635 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6636 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006637 bd->props.brightness : 0;
6638
6639 dbg_printk(TPACPI_DBG_BRGHT,
6640 "backlight: attempt to set level to %d\n",
6641 level);
6642
6643 /* it is the backlight class's job (caller) to handle
6644 * EINTR and other errors properly */
6645 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006646}
Holger Macht8acb0252006-10-20 14:30:28 -07006647
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006648static int brightness_get(struct backlight_device *bd)
6649{
6650 int status, res;
6651
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006652 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006653 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006654 return 0;
6655
6656 res = tpacpi_brightness_get_raw(&status);
6657
6658 mutex_unlock(&brightness_mutex);
6659
6660 if (res < 0)
6661 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006662
6663 return status & TP_EC_BACKLIGHT_LVLMSK;
6664}
6665
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006666static void tpacpi_brightness_notify_change(void)
6667{
6668 backlight_force_update(ibm_backlight_device,
6669 BACKLIGHT_UPDATE_HOTKEY);
6670}
6671
Lionel Debrouxacc24722010-11-16 14:14:02 +01006672static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006673 .get_brightness = brightness_get,
6674 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006675};
6676
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006677/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006678
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006679/*
6680 * Call _BCL method of video device. On some ThinkPads this will
6681 * switch the firmware to the ACPI brightness control mode.
6682 */
6683
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006684static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6685{
6686 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6687 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006688 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006689 int rc;
6690
Aaron Lu46445b62013-10-11 21:27:46 +08006691 if (acpi_bus_get_device(handle, &device))
6692 return 0;
6693
6694 rc = 0;
6695 list_for_each_entry(child, &device->children, node) {
6696 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6697 NULL, &buffer);
6698 if (ACPI_FAILURE(status))
6699 continue;
6700
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006701 obj = (union acpi_object *)buffer.pointer;
6702 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006703 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006704 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006705 rc = 0;
6706 } else {
6707 rc = obj->package.count;
6708 }
Aaron Lu46445b62013-10-11 21:27:46 +08006709 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006710 }
6711
6712 kfree(buffer.pointer);
6713 return rc;
6714}
6715
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006716
6717/*
6718 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6719 */
6720static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6721{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006722 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006723 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006724
Aaron Lu46445b62013-10-11 21:27:46 +08006725 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006726 if (video_device)
6727 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006728
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006729 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006730
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006731 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006732}
6733
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006734/*
6735 * These are only useful for models that have only one possibility
6736 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6737 * these quirks.
6738 */
6739#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6740#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6741#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6742
6743static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6744 /* Models with ATI GPUs known to require ECNVRAM mode */
6745 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6746
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006747 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006748 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006749 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006750 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006751 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6752
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006753 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006754 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006755 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6756 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006757
6758 /* Models with Intel GMA900 */
6759 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6760 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6761 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6762};
6763
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006764/*
6765 * Returns < 0 for error, otherwise sets tp_features.bright_*
6766 * and bright_maxlvl.
6767 */
6768static void __init tpacpi_detect_brightness_capabilities(void)
6769{
6770 unsigned int b;
6771
6772 vdbg_printk(TPACPI_DBG_INIT,
6773 "detecting firmware brightness interface capabilities\n");
6774
6775 /* we could run a quirks check here (same table used by
6776 * brightness_init) if needed */
6777
6778 /*
6779 * We always attempt to detect acpi support, so as to switch
6780 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6781 * going to publish a backlight interface
6782 */
6783 b = tpacpi_check_std_acpi_brightness_support();
6784 switch (b) {
6785 case 16:
6786 bright_maxlvl = 15;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006787 break;
6788 case 8:
6789 case 0:
6790 bright_maxlvl = 7;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006791 break;
6792 default:
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006793 tp_features.bright_unkfw = 1;
6794 bright_maxlvl = b - 1;
6795 }
Eric Curtin15c75622016-01-30 16:55:59 +00006796 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006797}
6798
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006799static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006800{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006801 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006802 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006803 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006804
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006805 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6806
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006807 mutex_init(&brightness_mutex);
6808
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006809 quirks = tpacpi_check_quirks(brightness_quirk_table,
6810 ARRAY_SIZE(brightness_quirk_table));
6811
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006812 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006813
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006814 /* if it is unknown, we don't handle it: it wouldn't be safe */
6815 if (tp_features.bright_unkfw)
6816 return 1;
6817
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006818 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006819 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006820 "brightness support disabled by "
6821 "module parameter\n");
6822 return 1;
6823 }
6824
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02006825 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006826 if (brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006827 pr_info("Standard ACPI backlight interface "
6828 "available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006829 return 1;
6830 } else if (brightness_enable == 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006831 pr_warn("Cannot enable backlight brightness support, "
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006832 "ACPI is already handling it. Refer to the "
Joe Perches0978e012011-04-04 10:06:25 -07006833 "acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006834 return 1;
6835 }
6836 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006837 pr_notice("Standard ACPI backlight interface not "
6838 "available, thinkpad_acpi native "
6839 "brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006840 }
6841
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006842 /*
6843 * Check for module parameter bogosity, note that we
6844 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6845 * able to detect "unspecified"
6846 */
6847 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006848 return -EINVAL;
6849
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006850 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6851 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6852 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006853 if (quirks & TPACPI_BRGHT_Q_EC)
6854 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6855 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006856 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6857
6858 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006859 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006860 brightness_mode);
6861 }
6862
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006863 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006864 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006865 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6866 brightness_mode == TPACPI_BRGHT_MODE_EC))
6867 return -EINVAL;
6868
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006869 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006870 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006871
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006872 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006873 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006874 props.max_brightness = bright_maxlvl;
6875 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006876 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6877 NULL, NULL,
6878 &ibm_backlight_data,
6879 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006880 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006881 int rc = PTR_ERR(ibm_backlight_device);
6882 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006883 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006884 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006885 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006886 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6887 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006888
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006889 if (quirks & TPACPI_BRGHT_Q_ASK) {
Joe Perches0978e012011-04-04 10:06:25 -07006890 pr_notice("brightness: will use unverified default: "
6891 "brightness_mode=%d\n", brightness_mode);
6892 pr_notice("brightness: please report to %s whether it works well "
6893 "or not on your ThinkPad\n", TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006894 }
6895
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006896 /* Added by mistake in early 2007. Probably useless, but it could
6897 * be working around some unknown firmware problem where the value
6898 * read at startup doesn't match the real hardware state... so leave
6899 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006900 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006901
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006902 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6903 "brightness: registering brightness hotkeys "
6904 "as change notification\n");
6905 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6906 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006907 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006908 return 0;
6909}
6910
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006911static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006912{
6913 tpacpi_brightness_checkpoint_nvram();
6914}
6915
6916static void brightness_shutdown(void)
6917{
6918 tpacpi_brightness_checkpoint_nvram();
6919}
6920
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006921static void brightness_exit(void)
6922{
6923 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006924 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006925 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006926 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006927 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006928
6929 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006930}
6931
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006932static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006933{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006934 int level;
6935
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006936 level = brightness_get(NULL);
6937 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006938 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006939 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006940 seq_printf(m, "level:\t\t%d\n", level);
6941 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006942 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6943 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006944 }
6945
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006946 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006947}
6948
6949static int brightness_write(char *buf)
6950{
6951 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006952 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006953 char *cmd;
6954
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006955 level = brightness_get(NULL);
6956 if (level < 0)
6957 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006958
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006959 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006960 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006961 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006962 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006963 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006964 if (level > 0)
6965 level--;
6966 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006967 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006968 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006969 } else
6970 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006971 }
6972
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006973 tpacpi_disclose_usertask("procfs brightness",
6974 "set level to %d\n", level);
6975
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006976 /*
6977 * Now we know what the final level should be, so we try to set it.
6978 * Doing it this way makes the syscall restartable in case of EINTR
6979 */
6980 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006981 if (!rc && ibm_backlight_device)
6982 backlight_force_update(ibm_backlight_device,
6983 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02006984 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006985}
6986
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006987static struct ibm_struct brightness_driver_data = {
6988 .name = "brightness",
6989 .read = brightness_read,
6990 .write = brightness_write,
6991 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006992 .suspend = brightness_suspend,
6993 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006994};
6995
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006996/*************************************************************************
6997 * Volume subdriver
6998 */
6999
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007000/*
7001 * IBM ThinkPads have a simple volume controller with MUTE gating.
7002 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7003 *
7004 * Since the *61 series (and probably also the later *60 series), Lenovo
7005 * ThinkPads only implement the MUTE gate.
7006 *
7007 * EC register 0x30
7008 * Bit 6: MUTE (1 mutes sound)
7009 * Bit 3-0: Volume
7010 * Other bits should be zero as far as we know.
7011 *
7012 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7013 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7014 * such as bit 7 which is used to detect repeated presses of MUTE,
7015 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007016 *
7017 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7018 * in response to user input. Unfortunately, this rarely works well.
7019 * The laptop changes the state of its internal MUTE gate and, on some
7020 * models, sends KEY_MUTE, causing any user code that responds to the
7021 * mute button to get confused. The hardware MUTE gate is also
7022 * unnecessary, since user code can handle the mute button without
7023 * kernel or EC help.
7024 *
7025 * To avoid confusing userspace, we simply disable all EC-based mute
7026 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007027 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007028
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007029#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7030
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007031#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7032#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7033#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7034
Takashi Iwaicab66612013-10-24 16:06:32 +02007035#if SNDRV_CARDS <= 32
7036#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7037#else
7038#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7039#endif
7040static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007041static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307042static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007043
7044struct tpacpi_alsa_data {
7045 struct snd_card *card;
7046 struct snd_ctl_elem_id *ctl_mute_id;
7047 struct snd_ctl_elem_id *ctl_vol_id;
7048};
7049
7050static struct snd_card *alsa_card;
7051
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007052enum {
7053 TP_EC_AUDIO = 0x30,
7054
7055 /* TP_EC_AUDIO bits */
7056 TP_EC_AUDIO_MUTESW = 6,
7057
7058 /* TP_EC_AUDIO bitmasks */
7059 TP_EC_AUDIO_LVL_MSK = 0x0F,
7060 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7061
7062 /* Maximum volume */
7063 TP_EC_VOLUME_MAX = 14,
7064};
7065
7066enum tpacpi_volume_access_mode {
7067 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7068 TPACPI_VOL_MODE_EC, /* Pure EC control */
7069 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7070 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7071 TPACPI_VOL_MODE_MAX
7072};
7073
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007074enum tpacpi_volume_capabilities {
7075 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7076 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7077 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7078 TPACPI_VOL_CAP_MAX
7079};
7080
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007081enum tpacpi_mute_btn_mode {
7082 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7083 /* We don't know what mode 1 is. */
7084 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7085 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7086};
7087
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007088static enum tpacpi_volume_access_mode volume_mode =
7089 TPACPI_VOL_MODE_MAX;
7090
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007091static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307092static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007093static bool software_mute_requested = true;
7094static bool software_mute_active;
7095static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007096
7097/*
7098 * Used to syncronize writers to TP_EC_AUDIO and
7099 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7100 */
7101static struct mutex volume_mutex;
7102
7103static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007104{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007105 u8 lec = 0;
7106 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007107 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007108
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007109 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7110 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007111 if (!volume_control_allowed)
7112 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007113 if (software_mute_active)
7114 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007115
7116 vdbg_printk(TPACPI_DBG_MIXER,
7117 "trying to checkpoint mixer state to NVRAM...\n");
7118
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007119 if (tp_features.mixer_no_level_control)
7120 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7121 else
7122 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7123
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007124 if (mutex_lock_killable(&volume_mutex) < 0)
7125 return;
7126
7127 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7128 goto unlock;
7129 lec &= ec_mask;
7130 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7131
7132 if (lec != (b_nvram & ec_mask)) {
7133 /* NVRAM needs update */
7134 b_nvram &= ~ec_mask;
7135 b_nvram |= lec;
7136 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7137 dbg_printk(TPACPI_DBG_MIXER,
7138 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7139 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007140 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007141 vdbg_printk(TPACPI_DBG_MIXER,
7142 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7143 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007144 }
7145
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007146unlock:
7147 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007148}
7149
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007150static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007151{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007152 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007153
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007154 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7155 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007156
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007157 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007158
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007159 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007160
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007161 return 0;
7162}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007163
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007164static int volume_get_status(u8 *status)
7165{
7166 return volume_get_status_ec(status);
7167}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007168
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007169static int volume_set_status_ec(const u8 status)
7170{
7171 if (!acpi_ec_write(TP_EC_AUDIO, status))
7172 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007173
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007174 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7175
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007176 /*
7177 * On X200s, and possibly on others, it can take a while for
7178 * reads to become correct.
7179 */
7180 msleep(1);
7181
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007182 return 0;
7183}
7184
7185static int volume_set_status(const u8 status)
7186{
7187 return volume_set_status_ec(status);
7188}
7189
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007190/* returns < 0 on error, 0 on no change, 1 on change */
7191static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007192{
7193 int rc;
7194 u8 s, n;
7195
7196 if (mutex_lock_killable(&volume_mutex) < 0)
7197 return -EINTR;
7198
7199 rc = volume_get_status_ec(&s);
7200 if (rc)
7201 goto unlock;
7202
7203 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7204 s & ~TP_EC_AUDIO_MUTESW_MSK;
7205
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007206 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007207 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007208 if (!rc)
7209 rc = 1;
7210 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007211
7212unlock:
7213 mutex_unlock(&volume_mutex);
7214 return rc;
7215}
7216
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007217static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007218{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007219 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007220 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007221 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007222}
7223
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007224static int volume_set_mute(const bool mute)
7225{
7226 int rc;
7227
7228 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7229 (mute) ? "" : "un");
7230
7231 rc = __volume_set_mute_ec(mute);
7232 return (rc < 0) ? rc : 0;
7233}
7234
7235/* returns < 0 on error, 0 on no change, 1 on change */
7236static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007237{
7238 int rc;
7239 u8 s, n;
7240
7241 if (vol > TP_EC_VOLUME_MAX)
7242 return -EINVAL;
7243
7244 if (mutex_lock_killable(&volume_mutex) < 0)
7245 return -EINTR;
7246
7247 rc = volume_get_status_ec(&s);
7248 if (rc)
7249 goto unlock;
7250
7251 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7252
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007253 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007254 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007255 if (!rc)
7256 rc = 1;
7257 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007258
7259unlock:
7260 mutex_unlock(&volume_mutex);
7261 return rc;
7262}
7263
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007264static int volume_set_software_mute(bool startup)
7265{
7266 int result;
7267
7268 if (!tpacpi_is_lenovo())
7269 return -ENODEV;
7270
7271 if (startup) {
7272 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7273 "HAUM", "qd"))
7274 return -EIO;
7275
7276 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7277 "Initial HAUM setting was %d\n",
7278 software_mute_orig_mode);
7279 }
7280
7281 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7282 (int)TP_EC_MUTE_BTN_NONE))
7283 return -EIO;
7284
7285 if (result != TP_EC_MUTE_BTN_NONE)
7286 pr_warn("Unexpected SAUM result %d\n",
7287 result);
7288
7289 /*
7290 * In software mute mode, the standard codec controls take
7291 * precendence, so we unmute the ThinkPad HW switch at
7292 * startup. Just on case there are SAUM-capable ThinkPads
7293 * with level controls, set max HW volume as well.
7294 */
7295 if (tp_features.mixer_no_level_control)
7296 result = volume_set_mute(false);
7297 else
7298 result = volume_set_status(TP_EC_VOLUME_MAX);
7299
7300 if (result != 0)
7301 pr_warn("Failed to unmute the HW mute switch\n");
7302
7303 return 0;
7304}
7305
7306static void volume_exit_software_mute(void)
7307{
7308 int r;
7309
7310 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7311 || r != software_mute_orig_mode)
7312 pr_warn("Failed to restore mute mode\n");
7313}
7314
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007315static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007316{
7317 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007318 "ALSA: trying to set volume level to %hu\n", vol);
7319 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007320}
7321
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007322static void volume_alsa_notify_change(void)
7323{
7324 struct tpacpi_alsa_data *d;
7325
7326 if (alsa_card && alsa_card->private_data) {
7327 d = alsa_card->private_data;
7328 if (d->ctl_mute_id)
7329 snd_ctl_notify(alsa_card,
7330 SNDRV_CTL_EVENT_MASK_VALUE,
7331 d->ctl_mute_id);
7332 if (d->ctl_vol_id)
7333 snd_ctl_notify(alsa_card,
7334 SNDRV_CTL_EVENT_MASK_VALUE,
7335 d->ctl_vol_id);
7336 }
7337}
7338
7339static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7340 struct snd_ctl_elem_info *uinfo)
7341{
7342 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7343 uinfo->count = 1;
7344 uinfo->value.integer.min = 0;
7345 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7346 return 0;
7347}
7348
7349static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7350 struct snd_ctl_elem_value *ucontrol)
7351{
7352 u8 s;
7353 int rc;
7354
7355 rc = volume_get_status(&s);
7356 if (rc < 0)
7357 return rc;
7358
7359 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7360 return 0;
7361}
7362
7363static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7364 struct snd_ctl_elem_value *ucontrol)
7365{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007366 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7367 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007368 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007369}
7370
7371#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7372
7373static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7374 struct snd_ctl_elem_value *ucontrol)
7375{
7376 u8 s;
7377 int rc;
7378
7379 rc = volume_get_status(&s);
7380 if (rc < 0)
7381 return rc;
7382
7383 ucontrol->value.integer.value[0] =
7384 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7385 return 0;
7386}
7387
7388static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7389 struct snd_ctl_elem_value *ucontrol)
7390{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007391 tpacpi_disclose_usertask("ALSA", "%smute\n",
7392 ucontrol->value.integer.value[0] ?
7393 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007394 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007395}
7396
Mathias Krausec3aa4722014-07-16 19:43:17 +02007397static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007398 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7399 .name = "Console Playback Volume",
7400 .index = 0,
7401 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7402 .info = volume_alsa_vol_info,
7403 .get = volume_alsa_vol_get,
7404};
7405
Mathias Krausec3aa4722014-07-16 19:43:17 +02007406static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007407 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7408 .name = "Console Playback Switch",
7409 .index = 0,
7410 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7411 .info = volume_alsa_mute_info,
7412 .get = volume_alsa_mute_get,
7413};
7414
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007415static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007416{
7417 tpacpi_volume_checkpoint_nvram();
7418}
7419
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007420static void volume_resume(void)
7421{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007422 if (software_mute_active) {
7423 if (volume_set_software_mute(false) < 0)
7424 pr_warn("Failed to restore software mute\n");
7425 } else {
7426 volume_alsa_notify_change();
7427 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007428}
7429
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007430static void volume_shutdown(void)
7431{
7432 tpacpi_volume_checkpoint_nvram();
7433}
7434
7435static void volume_exit(void)
7436{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007437 if (alsa_card) {
7438 snd_card_free(alsa_card);
7439 alsa_card = NULL;
7440 }
7441
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007442 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007443
7444 if (software_mute_active)
7445 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007446}
7447
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007448static int __init volume_create_alsa_mixer(void)
7449{
7450 struct snd_card *card;
7451 struct tpacpi_alsa_data *data;
7452 struct snd_kcontrol *ctl_vol;
7453 struct snd_kcontrol *ctl_mute;
7454 int rc;
7455
Takashi Iwai89235e52014-01-29 15:01:27 +01007456 rc = snd_card_new(&tpacpi_pdev->dev,
7457 alsa_index, alsa_id, THIS_MODULE,
7458 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007459 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007460 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007461 return 1;
7462 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007463
7464 BUG_ON(!card->private_data);
7465 data = card->private_data;
7466 data->card = card;
7467
7468 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7469 sizeof(card->driver));
7470 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7471 sizeof(card->shortname));
7472 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7473 (thinkpad_id.ec_version_str) ?
7474 thinkpad_id.ec_version_str : "(unknown)");
7475 snprintf(card->longname, sizeof(card->longname),
7476 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7477 (thinkpad_id.ec_version_str) ?
7478 thinkpad_id.ec_version_str : "unknown");
7479
7480 if (volume_control_allowed) {
7481 volume_alsa_control_vol.put = volume_alsa_vol_put;
7482 volume_alsa_control_vol.access =
7483 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7484
7485 volume_alsa_control_mute.put = volume_alsa_mute_put;
7486 volume_alsa_control_mute.access =
7487 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7488 }
7489
7490 if (!tp_features.mixer_no_level_control) {
7491 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7492 rc = snd_ctl_add(card, ctl_vol);
7493 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007494 pr_err("Failed to create ALSA volume control: %d\n",
7495 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007496 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007497 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007498 data->ctl_vol_id = &ctl_vol->id;
7499 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007500
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007501 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7502 rc = snd_ctl_add(card, ctl_mute);
7503 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007504 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007505 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007506 }
7507 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007508
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007509 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007510 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007511 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007512 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007513 }
7514
7515 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007516 return 0;
7517
7518err_exit:
7519 snd_card_free(card);
7520 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007521}
7522
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007523#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7524#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7525
7526static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7527 /* Whitelist volume level on all IBM by default */
7528 { .vendor = PCI_VENDOR_ID_IBM,
7529 .bios = TPACPI_MATCH_ANY,
7530 .ec = TPACPI_MATCH_ANY,
7531 .quirks = TPACPI_VOL_Q_LEVEL },
7532
7533 /* Lenovo models with volume control (needs confirmation) */
7534 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7535 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7536 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7537 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7538 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7539 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7540 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7541
7542 /* Whitelist mute-only on all Lenovo by default */
7543 { .vendor = PCI_VENDOR_ID_LENOVO,
7544 .bios = TPACPI_MATCH_ANY,
7545 .ec = TPACPI_MATCH_ANY,
7546 .quirks = TPACPI_VOL_Q_MUTEONLY }
7547};
7548
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007549static int __init volume_init(struct ibm_init_struct *iibm)
7550{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007551 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007552 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007553
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007554 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7555
7556 mutex_init(&volume_mutex);
7557
7558 /*
7559 * Check for module parameter bogosity, note that we
7560 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7561 * able to detect "unspecified"
7562 */
7563 if (volume_mode > TPACPI_VOL_MODE_MAX)
7564 return -EINVAL;
7565
7566 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Joe Perches0978e012011-04-04 10:06:25 -07007567 pr_err("UCMS step volume mode not implemented, "
7568 "please contact %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007569 return 1;
7570 }
7571
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007572 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7573 return -EINVAL;
7574
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007575 /*
7576 * The ALSA mixer is our primary interface.
7577 * When disabled, don't install the subdriver at all
7578 */
7579 if (!alsa_enable) {
7580 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7581 "ALSA mixer disabled by parameter, "
7582 "not loading volume subdriver...\n");
7583 return 1;
7584 }
7585
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007586 quirks = tpacpi_check_quirks(volume_quirk_table,
7587 ARRAY_SIZE(volume_quirk_table));
7588
7589 switch (volume_capabilities) {
7590 case TPACPI_VOL_CAP_AUTO:
7591 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7592 tp_features.mixer_no_level_control = 1;
7593 else if (quirks & TPACPI_VOL_Q_LEVEL)
7594 tp_features.mixer_no_level_control = 0;
7595 else
7596 return 1; /* no mixer */
7597 break;
7598 case TPACPI_VOL_CAP_VOLMUTE:
7599 tp_features.mixer_no_level_control = 0;
7600 break;
7601 case TPACPI_VOL_CAP_MUTEONLY:
7602 tp_features.mixer_no_level_control = 1;
7603 break;
7604 default:
7605 return 1;
7606 }
7607
7608 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7609 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7610 "using user-supplied volume_capabilities=%d\n",
7611 volume_capabilities);
7612
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007613 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7614 volume_mode == TPACPI_VOL_MODE_MAX) {
7615 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7616
7617 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7618 "driver auto-selected volume_mode=%d\n",
7619 volume_mode);
7620 } else {
7621 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7622 "using user-supplied volume_mode=%d\n",
7623 volume_mode);
7624 }
7625
7626 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007627 "mute is supported, volume control is %s\n",
7628 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007629
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007630 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7631 software_mute_active = true;
7632 } else {
7633 rc = volume_create_alsa_mixer();
7634 if (rc) {
7635 pr_err("Could not create the ALSA mixer interface\n");
7636 return rc;
7637 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007638
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007639 pr_info("Console audio control enabled, mode: %s\n",
7640 (volume_control_allowed) ?
7641 "override (read/write)" :
7642 "monitor (read only)");
7643 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007644
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007645 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7646 "registering volume hotkeys as change notification\n");
7647 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7648 | TP_ACPI_HKEY_VOLUP_MASK
7649 | TP_ACPI_HKEY_VOLDWN_MASK
7650 | TP_ACPI_HKEY_MUTE_MASK);
7651
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007652 return 0;
7653}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007654
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007655static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007656{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007657 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007658
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007659 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007660 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007661 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007662 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007663 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007664 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007665 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007666 status & TP_EC_AUDIO_LVL_MSK);
7667
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007668 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007669 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007670
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007671 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007672 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007673 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007674 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007675 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007676 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007677 "commands:\tlevel <level>"
7678 " (<level> is 0-%d)\n",
7679 TP_EC_VOLUME_MAX);
7680 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007681 }
7682 }
7683
7684 return 0;
7685}
7686
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007687static int volume_write(char *buf)
7688{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007689 u8 s;
7690 u8 new_level, new_mute;
7691 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007692 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007693 int rc;
7694
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007695 /*
7696 * We do allow volume control at driver startup, so that the
7697 * user can set initial state through the volume=... parameter hack.
7698 */
7699 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7700 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7701 tp_warned.volume_ctrl_forbidden = 1;
Joe Perches0978e012011-04-04 10:06:25 -07007702 pr_notice("Console audio control in monitor mode, "
7703 "changes are not allowed\n");
7704 pr_notice("Use the volume_control=1 module parameter "
7705 "to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007706 }
7707 return -EPERM;
7708 }
7709
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007710 rc = volume_get_status(&s);
7711 if (rc < 0)
7712 return rc;
7713
7714 new_level = s & TP_EC_AUDIO_LVL_MSK;
7715 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007716
7717 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007718 if (!tp_features.mixer_no_level_control) {
7719 if (strlencmp(cmd, "up") == 0) {
7720 if (new_mute)
7721 new_mute = 0;
7722 else if (new_level < TP_EC_VOLUME_MAX)
7723 new_level++;
7724 continue;
7725 } else if (strlencmp(cmd, "down") == 0) {
7726 if (new_mute)
7727 new_mute = 0;
7728 else if (new_level > 0)
7729 new_level--;
7730 continue;
7731 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7732 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7733 new_level = l;
7734 continue;
7735 }
7736 }
7737 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007738 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007739 else if (strlencmp(cmd, "unmute") == 0)
7740 new_mute = 0;
7741 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007742 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007743 }
7744
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007745 if (tp_features.mixer_no_level_control) {
7746 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7747 new_mute ? "" : "un");
7748 rc = volume_set_mute(!!new_mute);
7749 } else {
7750 tpacpi_disclose_usertask("procfs volume",
7751 "%smute and set level to %d\n",
7752 new_mute ? "" : "un", new_level);
7753 rc = volume_set_status(new_mute | new_level);
7754 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007755 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007756
7757 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007758}
7759
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007760static struct ibm_struct volume_driver_data = {
7761 .name = "volume",
7762 .read = volume_read,
7763 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007764 .exit = volume_exit,
7765 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007766 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007767 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007768};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007769
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007770#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7771
7772#define alsa_card NULL
7773
Tobias Klauserbb55a2e2016-11-25 14:18:07 +01007774static inline void volume_alsa_notify_change(void)
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007775{
7776}
7777
7778static int __init volume_init(struct ibm_init_struct *iibm)
7779{
Joe Perches0978e012011-04-04 10:06:25 -07007780 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007781
7782 return 1;
7783}
7784
7785static struct ibm_struct volume_driver_data = {
7786 .name = "volume",
7787};
7788
7789#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7790
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007791/*************************************************************************
7792 * Fan subdriver
7793 */
7794
7795/*
7796 * FAN ACCESS MODES
7797 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007798 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007799 * ACPI GFAN method: returns fan level
7800 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007801 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007802 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007803 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007804 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007805 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7806 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007807 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7808 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007809 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007810 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007811 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7812 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007813 *
7814 * Fan speed changes of any sort (including those caused by the
7815 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007816 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007817 * limited.
7818 *
7819 * Reading is not available if GFAN exists.
7820 * Writing is not available if SFAN exists.
7821 *
7822 * Bits
7823 * 7 automatic mode engaged;
7824 * (default operation mode of the ThinkPad)
7825 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007826 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007827 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007828 * the tachometer while the fan controller ramps up
7829 * the speed (which can take up to a few *minutes*).
7830 * Speeds up fan to 100% duty-cycle, which is far above
7831 * the standard RPM levels. It is not impossible that
7832 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007833 * 5-3 unused in some models. Extra bits for fan level
7834 * in others, but still useless as all values above
7835 * 7 map to the same speed as level 7 in these models.
7836 * 2-0 fan level (0..7 usually)
7837 * 0x00 = stop
7838 * 0x07 = max (set when temperatures critical)
7839 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007840 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007841 *
7842 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007843 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007844 * does so, its initial value is meaningless (0x07).
7845 *
7846 * For firmware bugs, refer to:
7847 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7848 *
7849 * ----
7850 *
7851 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7852 * Main fan tachometer reading (in RPM)
7853 *
7854 * This register is present on all ThinkPads with a new-style EC, and
7855 * it is known not to be present on the A21m/e, and T22, as there is
7856 * something else in offset 0x84 according to the ACPI DSDT. Other
7857 * ThinkPads from this same time period (and earlier) probably lack the
7858 * tachometer as well.
7859 *
Nick Andrew877d0312009-01-26 11:06:57 +01007860 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007861 * was never fixed by IBM to report the EC firmware version string
7862 * probably support the tachometer (like the early X models), so
7863 * detecting it is quite hard. We need more data to know for sure.
7864 *
7865 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7866 * might result.
7867 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007868 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7869 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007870 *
7871 * For firmware bugs, refer to:
7872 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7873 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007874 * ----
7875 *
7876 * ThinkPad EC register 0x31 bit 0 (only on select models)
7877 *
7878 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7879 * show the speed of the main fan. When bit 0 of EC register 0x31
7880 * is one, the tachometer registers show the speed of the auxiliary
7881 * fan.
7882 *
7883 * Fan control seems to affect both fans, regardless of the state
7884 * of this bit.
7885 *
7886 * So far, only the firmware for the X60/X61 non-tablet versions
7887 * seem to support this (firmware TP-7M).
7888 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007889 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007890 * ThinkPad X31, X40, X41. Not available in the X60.
7891 *
7892 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7893 * high speed. ACPI DSDT seems to map these three speeds to levels
7894 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7895 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7896 *
7897 * The speeds are stored on handles
7898 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7899 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007900 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007901 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7902 *
7903 * ACPI DSDT switches which set is in use depending on various
7904 * factors.
7905 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007906 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007907 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7908 * but the ACPI tables just mention level 7.
7909 */
7910
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007911enum { /* Fan control constants */
7912 fan_status_offset = 0x2f, /* EC register 0x2f */
7913 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7914 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007915 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7916 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007917
7918 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7919 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7920
7921 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7922};
7923
7924enum fan_status_access_mode {
7925 TPACPI_FAN_NONE = 0, /* No fan status or control */
7926 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7927 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7928};
7929
7930enum fan_control_access_mode {
7931 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7932 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7933 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7934 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7935};
7936
7937enum fan_control_commands {
7938 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7939 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7940 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7941 * and also watchdog cmd */
7942};
7943
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307944static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007945
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007946static enum fan_status_access_mode fan_status_access_mode;
7947static enum fan_control_access_mode fan_control_access_mode;
7948static enum fan_control_commands fan_control_commands;
7949
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007950static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007951static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007952static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007953static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007954
7955static struct mutex fan_mutex;
7956
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007957static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007958static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007959
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007960TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7961TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007962 "\\FSPD", /* 600e/x, 770e, 770x */
7963 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007964TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007965 "JFNS", /* 770x-JL */
7966 ); /* all others */
7967
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007968/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007969 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7970 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7971 * be in auto mode (0x80).
7972 *
7973 * This is corrected by any write to HFSP either by the driver, or
7974 * by the firmware.
7975 *
7976 * We assume 0x07 really means auto mode while this quirk is active,
7977 * as this is far more likely than the ThinkPad being in level 7,
7978 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007979 *
7980 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7981 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007982 */
7983
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007984static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007985{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007986 if (fan_control_initial_status == 0x07) {
Joe Perches0978e012011-04-04 10:06:25 -07007987 pr_notice("fan_init: initial fan status is unknown, "
7988 "assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007989 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007990 }
7991}
7992
7993static void fan_quirk1_handle(u8 *fan_status)
7994{
7995 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7996 if (*fan_status != fan_control_initial_status) {
7997 /* something changed the HFSP regisnter since
7998 * driver init time, so it is not undefined
7999 * anymore */
8000 tp_features.fan_ctrl_status_undef = 0;
8001 } else {
8002 /* Return most likely status. In fact, it
8003 * might be the only possible status */
8004 *fan_status = TP_EC_FAN_AUTO;
8005 }
8006 }
8007}
8008
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008009/* Select main fan on X60/X61, NOOP on others */
8010static bool fan_select_fan1(void)
8011{
8012 if (tp_features.second_fan) {
8013 u8 val;
8014
8015 if (ec_read(fan_select_offset, &val) < 0)
8016 return false;
8017 val &= 0xFEU;
8018 if (ec_write(fan_select_offset, val) < 0)
8019 return false;
8020 }
8021 return true;
8022}
8023
8024/* Select secondary fan on X60/X61 */
8025static bool fan_select_fan2(void)
8026{
8027 u8 val;
8028
8029 if (!tp_features.second_fan)
8030 return false;
8031
8032 if (ec_read(fan_select_offset, &val) < 0)
8033 return false;
8034 val |= 0x01U;
8035 if (ec_write(fan_select_offset, val) < 0)
8036 return false;
8037
8038 return true;
8039}
8040
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008041/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008042 * Call with fan_mutex held
8043 */
8044static void fan_update_desired_level(u8 status)
8045{
8046 if ((status &
8047 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8048 if (status > 7)
8049 fan_control_desired_level = 7;
8050 else
8051 fan_control_desired_level = status;
8052 }
8053}
8054
8055static int fan_get_status(u8 *status)
8056{
8057 u8 s;
8058
8059 /* TODO:
8060 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8061
8062 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07008063 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008064 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07008065 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008066
Dan Carpentereceeb432012-09-01 12:54:07 -07008067 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008068 return -EIO;
8069
8070 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07008071 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008072
8073 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07008074 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008075 case TPACPI_FAN_RD_TPEC:
8076 /* all except 570, 600e/x, 770e, 770x */
8077 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8078 return -EIO;
8079
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008080 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008081 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008082 fan_quirk1_handle(status);
8083 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008084
8085 break;
8086
8087 default:
8088 return -ENXIO;
8089 }
8090
8091 return 0;
8092}
8093
8094static int fan_get_status_safe(u8 *status)
8095{
8096 int rc;
8097 u8 s;
8098
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008099 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008100 return -ERESTARTSYS;
8101 rc = fan_get_status(&s);
8102 if (!rc)
8103 fan_update_desired_level(s);
8104 mutex_unlock(&fan_mutex);
8105
Dan Carpentera7718362016-04-15 17:46:34 +03008106 if (rc)
8107 return rc;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008108 if (status)
8109 *status = s;
8110
Dan Carpentera7718362016-04-15 17:46:34 +03008111 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008112}
8113
8114static int fan_get_speed(unsigned int *speed)
8115{
8116 u8 hi, lo;
8117
8118 switch (fan_status_access_mode) {
8119 case TPACPI_FAN_RD_TPEC:
8120 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008121 if (unlikely(!fan_select_fan1()))
8122 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008123 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8124 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8125 return -EIO;
8126
8127 if (likely(speed))
8128 *speed = (hi << 8) | lo;
8129
8130 break;
8131
8132 default:
8133 return -ENXIO;
8134 }
8135
8136 return 0;
8137}
8138
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008139static int fan2_get_speed(unsigned int *speed)
8140{
8141 u8 hi, lo;
8142 bool rc;
8143
8144 switch (fan_status_access_mode) {
8145 case TPACPI_FAN_RD_TPEC:
8146 /* all except 570, 600e/x, 770e, 770x */
8147 if (unlikely(!fan_select_fan2()))
8148 return -EIO;
8149 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8150 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8151 fan_select_fan1(); /* play it safe */
8152 if (rc)
8153 return -EIO;
8154
8155 if (likely(speed))
8156 *speed = (hi << 8) | lo;
8157
8158 break;
8159
8160 default:
8161 return -ENXIO;
8162 }
8163
8164 return 0;
8165}
8166
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008167static int fan_set_level(int level)
8168{
8169 if (!fan_control_allowed)
8170 return -EPERM;
8171
8172 switch (fan_control_access_mode) {
8173 case TPACPI_FAN_WR_ACPI_SFAN:
8174 if (level >= 0 && level <= 7) {
8175 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8176 return -EIO;
8177 } else
8178 return -EINVAL;
8179 break;
8180
8181 case TPACPI_FAN_WR_ACPI_FANS:
8182 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008183 if (!(level & TP_EC_FAN_AUTO) &&
8184 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008185 ((level < 0) || (level > 7)))
8186 return -EINVAL;
8187
8188 /* safety net should the EC not support AUTO
8189 * or FULLSPEED mode bits and just ignore them */
8190 if (level & TP_EC_FAN_FULLSPEED)
8191 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01008192 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008193 level |= 4; /* safety min speed 4 */
8194
8195 if (!acpi_ec_write(fan_status_offset, level))
8196 return -EIO;
8197 else
8198 tp_features.fan_ctrl_status_undef = 0;
8199 break;
8200
8201 default:
8202 return -ENXIO;
8203 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008204
8205 vdbg_printk(TPACPI_DBG_FAN,
8206 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008207 return 0;
8208}
8209
8210static int fan_set_level_safe(int level)
8211{
8212 int rc;
8213
8214 if (!fan_control_allowed)
8215 return -EPERM;
8216
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008217 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008218 return -ERESTARTSYS;
8219
8220 if (level == TPACPI_FAN_LAST_LEVEL)
8221 level = fan_control_desired_level;
8222
8223 rc = fan_set_level(level);
8224 if (!rc)
8225 fan_update_desired_level(level);
8226
8227 mutex_unlock(&fan_mutex);
8228 return rc;
8229}
8230
8231static int fan_set_enable(void)
8232{
8233 u8 s;
8234 int rc;
8235
8236 if (!fan_control_allowed)
8237 return -EPERM;
8238
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008239 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008240 return -ERESTARTSYS;
8241
8242 switch (fan_control_access_mode) {
8243 case TPACPI_FAN_WR_ACPI_FANS:
8244 case TPACPI_FAN_WR_TPEC:
8245 rc = fan_get_status(&s);
8246 if (rc < 0)
8247 break;
8248
8249 /* Don't go out of emergency fan mode */
8250 if (s != 7) {
8251 s &= 0x07;
8252 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8253 }
8254
8255 if (!acpi_ec_write(fan_status_offset, s))
8256 rc = -EIO;
8257 else {
8258 tp_features.fan_ctrl_status_undef = 0;
8259 rc = 0;
8260 }
8261 break;
8262
8263 case TPACPI_FAN_WR_ACPI_SFAN:
8264 rc = fan_get_status(&s);
8265 if (rc < 0)
8266 break;
8267
8268 s &= 0x07;
8269
8270 /* Set fan to at least level 4 */
8271 s |= 4;
8272
8273 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008274 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008275 else
8276 rc = 0;
8277 break;
8278
8279 default:
8280 rc = -ENXIO;
8281 }
8282
8283 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008284
8285 if (!rc)
8286 vdbg_printk(TPACPI_DBG_FAN,
8287 "fan control: set fan control register to 0x%02x\n",
8288 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008289 return rc;
8290}
8291
8292static int fan_set_disable(void)
8293{
8294 int rc;
8295
8296 if (!fan_control_allowed)
8297 return -EPERM;
8298
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008299 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008300 return -ERESTARTSYS;
8301
8302 rc = 0;
8303 switch (fan_control_access_mode) {
8304 case TPACPI_FAN_WR_ACPI_FANS:
8305 case TPACPI_FAN_WR_TPEC:
8306 if (!acpi_ec_write(fan_status_offset, 0x00))
8307 rc = -EIO;
8308 else {
8309 fan_control_desired_level = 0;
8310 tp_features.fan_ctrl_status_undef = 0;
8311 }
8312 break;
8313
8314 case TPACPI_FAN_WR_ACPI_SFAN:
8315 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8316 rc = -EIO;
8317 else
8318 fan_control_desired_level = 0;
8319 break;
8320
8321 default:
8322 rc = -ENXIO;
8323 }
8324
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008325 if (!rc)
8326 vdbg_printk(TPACPI_DBG_FAN,
8327 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008328
8329 mutex_unlock(&fan_mutex);
8330 return rc;
8331}
8332
8333static int fan_set_speed(int speed)
8334{
8335 int rc;
8336
8337 if (!fan_control_allowed)
8338 return -EPERM;
8339
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008340 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008341 return -ERESTARTSYS;
8342
8343 rc = 0;
8344 switch (fan_control_access_mode) {
8345 case TPACPI_FAN_WR_ACPI_FANS:
8346 if (speed >= 0 && speed <= 65535) {
8347 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8348 speed, speed, speed))
8349 rc = -EIO;
8350 } else
8351 rc = -EINVAL;
8352 break;
8353
8354 default:
8355 rc = -ENXIO;
8356 }
8357
8358 mutex_unlock(&fan_mutex);
8359 return rc;
8360}
8361
8362static void fan_watchdog_reset(void)
8363{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008364 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8365 return;
8366
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008367 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008368 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8369 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8370 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8371 else
8372 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008373}
8374
8375static void fan_watchdog_fire(struct work_struct *ignored)
8376{
8377 int rc;
8378
8379 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8380 return;
8381
Joe Perches0978e012011-04-04 10:06:25 -07008382 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008383 rc = fan_set_enable();
8384 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07008385 pr_err("fan watchdog: error %d while enabling fan, "
8386 "will try again later...\n", -rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008387 /* reschedule for later */
8388 fan_watchdog_reset();
8389 }
8390}
8391
8392/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008393 * SYSFS fan layout: hwmon compatible (device)
8394 *
8395 * pwm*_enable:
8396 * 0: "disengaged" mode
8397 * 1: manual mode
8398 * 2: native EC "auto" mode (recommended, hardware default)
8399 *
8400 * pwm*: set speed in manual mode, ignored otherwise.
8401 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8402 * interpolation.
8403 *
8404 * fan*_input: tachometer reading, RPM
8405 *
8406 *
8407 * SYSFS fan layout: extensions
8408 *
8409 * fan_watchdog (driver):
8410 * fan watchdog interval in seconds, 0 disables (default), max 120
8411 */
8412
8413/* sysfs fan pwm1_enable ----------------------------------------------- */
8414static ssize_t fan_pwm1_enable_show(struct device *dev,
8415 struct device_attribute *attr,
8416 char *buf)
8417{
8418 int res, mode;
8419 u8 status;
8420
8421 res = fan_get_status_safe(&status);
8422 if (res)
8423 return res;
8424
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008425 if (status & TP_EC_FAN_FULLSPEED) {
8426 mode = 0;
8427 } else if (status & TP_EC_FAN_AUTO) {
8428 mode = 2;
8429 } else
8430 mode = 1;
8431
8432 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8433}
8434
8435static ssize_t fan_pwm1_enable_store(struct device *dev,
8436 struct device_attribute *attr,
8437 const char *buf, size_t count)
8438{
8439 unsigned long t;
8440 int res, level;
8441
8442 if (parse_strtoul(buf, 2, &t))
8443 return -EINVAL;
8444
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008445 tpacpi_disclose_usertask("hwmon pwm1_enable",
8446 "set fan mode to %lu\n", t);
8447
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008448 switch (t) {
8449 case 0:
8450 level = TP_EC_FAN_FULLSPEED;
8451 break;
8452 case 1:
8453 level = TPACPI_FAN_LAST_LEVEL;
8454 break;
8455 case 2:
8456 level = TP_EC_FAN_AUTO;
8457 break;
8458 case 3:
8459 /* reserved for software-controlled auto mode */
8460 return -ENOSYS;
8461 default:
8462 return -EINVAL;
8463 }
8464
8465 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008466 if (res == -ENXIO)
8467 return -EINVAL;
8468 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008469 return res;
8470
8471 fan_watchdog_reset();
8472
8473 return count;
8474}
8475
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008476static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8477 fan_pwm1_enable_show, fan_pwm1_enable_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008478
8479/* sysfs fan pwm1 ------------------------------------------------------ */
8480static ssize_t fan_pwm1_show(struct device *dev,
8481 struct device_attribute *attr,
8482 char *buf)
8483{
8484 int res;
8485 u8 status;
8486
8487 res = fan_get_status_safe(&status);
8488 if (res)
8489 return res;
8490
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008491 if ((status &
8492 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8493 status = fan_control_desired_level;
8494
8495 if (status > 7)
8496 status = 7;
8497
8498 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8499}
8500
8501static ssize_t fan_pwm1_store(struct device *dev,
8502 struct device_attribute *attr,
8503 const char *buf, size_t count)
8504{
8505 unsigned long s;
8506 int rc;
8507 u8 status, newlevel;
8508
8509 if (parse_strtoul(buf, 255, &s))
8510 return -EINVAL;
8511
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008512 tpacpi_disclose_usertask("hwmon pwm1",
8513 "set fan speed to %lu\n", s);
8514
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008515 /* scale down from 0-255 to 0-7 */
8516 newlevel = (s >> 5) & 0x07;
8517
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008518 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008519 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008520
8521 rc = fan_get_status(&status);
8522 if (!rc && (status &
8523 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8524 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008525 if (rc == -ENXIO)
8526 rc = -EINVAL;
8527 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008528 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008529 fan_watchdog_reset();
8530 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008531 }
8532
8533 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008534 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008535}
8536
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008537static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008538
8539/* sysfs fan fan1_input ------------------------------------------------ */
8540static ssize_t fan_fan1_input_show(struct device *dev,
8541 struct device_attribute *attr,
8542 char *buf)
8543{
8544 int res;
8545 unsigned int speed;
8546
8547 res = fan_get_speed(&speed);
8548 if (res < 0)
8549 return res;
8550
8551 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8552}
8553
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008554static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008555
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008556/* sysfs fan fan2_input ------------------------------------------------ */
8557static ssize_t fan_fan2_input_show(struct device *dev,
8558 struct device_attribute *attr,
8559 char *buf)
8560{
8561 int res;
8562 unsigned int speed;
8563
8564 res = fan2_get_speed(&speed);
8565 if (res < 0)
8566 return res;
8567
8568 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8569}
8570
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008571static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008572
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008573/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008574static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
8575 char *buf)
8576{
8577 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8578}
8579
8580static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8581 const char *buf, size_t count)
8582{
8583 unsigned long t;
8584
8585 if (parse_strtoul(buf, 120, &t))
8586 return -EINVAL;
8587
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008588 if (!fan_control_allowed)
8589 return -EPERM;
8590
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008591 fan_watchdog_maxinterval = t;
8592 fan_watchdog_reset();
8593
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008594 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8595
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008596 return count;
8597}
8598
8599static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8600 fan_fan_watchdog_show, fan_fan_watchdog_store);
8601
8602/* --------------------------------------------------------------------- */
8603static struct attribute *fan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008604 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8605 &dev_attr_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008606 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008607 NULL
8608};
8609
8610static const struct attribute_group fan_attr_group = {
8611 .attrs = fan_attributes,
8612};
8613
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008614#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8615#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008616
8617#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8618 { .vendor = PCI_VENDOR_ID_IBM, \
8619 .bios = TPACPI_MATCH_ANY, \
8620 .ec = TPID(__id1, __id2), \
8621 .quirks = __quirks }
8622
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008623#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8624 { .vendor = PCI_VENDOR_ID_LENOVO, \
8625 .bios = TPACPI_MATCH_ANY, \
8626 .ec = TPID(__id1, __id2), \
8627 .quirks = __quirks }
8628
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008629static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8630 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8631 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8632 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8633 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008634 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008635};
8636
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008637#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008638#undef TPACPI_FAN_QI
8639
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008640static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008641{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008642 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008643 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008644
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008645 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8646 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008647
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008648 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008649 fan_status_access_mode = TPACPI_FAN_NONE;
8650 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008651 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008652 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008653 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008654 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008655 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008656
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008657 if (tpacpi_is_ibm()) {
8658 TPACPI_ACPIHANDLE_INIT(fans);
8659 TPACPI_ACPIHANDLE_INIT(gfan);
8660 TPACPI_ACPIHANDLE_INIT(sfan);
8661 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008662
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008663 quirks = tpacpi_check_quirks(fan_quirk_table,
8664 ARRAY_SIZE(fan_quirk_table));
8665
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008666 if (gfan_handle) {
8667 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008668 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008669 } else {
8670 /* all other ThinkPads: note that even old-style
8671 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008672 if (likely(acpi_ec_read(fan_status_offset,
8673 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008674 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008675 if (quirks & TPACPI_FAN_Q1)
8676 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008677 if (quirks & TPACPI_FAN_2FAN) {
8678 tp_features.second_fan = 1;
8679 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8680 "secondary fan support enabled\n");
8681 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008682 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008683 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008684 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008685 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008686 }
8687 }
8688
8689 if (sfan_handle) {
8690 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008691 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008692 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008693 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008694 } else {
8695 if (!gfan_handle) {
8696 /* gfan without sfan means no fan control */
8697 /* all other models implement TP EC 0x2f control */
8698
8699 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008700 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008701 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008702 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008703 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008704 TPACPI_FAN_CMD_SPEED |
8705 TPACPI_FAN_CMD_LEVEL |
8706 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008707 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008708 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008709 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008710 TPACPI_FAN_CMD_LEVEL |
8711 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008712 }
8713 }
8714 }
8715
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008716 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8717 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008718 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8719 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8720 fan_status_access_mode, fan_control_access_mode);
8721
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008722 /* fan control master switch */
8723 if (!fan_control_allowed) {
8724 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8725 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008726 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008727 "fan control features disabled by parameter\n");
8728 }
8729
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008730 /* update fan_control_desired_level */
8731 if (fan_status_access_mode != TPACPI_FAN_NONE)
8732 fan_get_status_safe(NULL);
8733
8734 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8735 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008736 if (tp_features.second_fan) {
8737 /* attach second fan tachometer */
8738 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008739 &dev_attr_fan2_input.attr;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008740 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008741 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008742 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008743 if (rc < 0)
8744 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008745
8746 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8747 &driver_attr_fan_watchdog);
8748 if (rc < 0) {
8749 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8750 &fan_attr_group);
8751 return rc;
8752 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008753 return 0;
8754 } else
8755 return 1;
8756}
8757
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008758static void fan_exit(void)
8759{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008760 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008761 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008762
8763 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008764 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008765 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8766 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008767
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008768 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008769 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008770}
8771
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008772static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008773{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008774 int rc;
8775
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008776 if (!fan_control_allowed)
8777 return;
8778
8779 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008780 fan_control_resume_level = 0;
8781 rc = fan_get_status_safe(&fan_control_resume_level);
8782 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008783 pr_notice("failed to read fan level for later "
8784 "restore during resume: %d\n", rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008785
8786 /* if it is undefined, don't attempt to restore it.
8787 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008788 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008789 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008790}
8791
8792static void fan_resume(void)
8793{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008794 u8 current_level = 7;
8795 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008796 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008797
8798 /* DSDT *always* updates status on resume */
8799 tp_features.fan_ctrl_status_undef = 0;
8800
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008801 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008802 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008803 (fan_get_status_safe(&current_level) < 0))
8804 return;
8805
8806 switch (fan_control_access_mode) {
8807 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008808 /* never decrease fan level */
8809 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008810 break;
8811 case TPACPI_FAN_WR_ACPI_FANS:
8812 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008813 /* never decrease fan level, scale is:
8814 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8815 *
8816 * We expect the firmware to set either 7 or AUTO, but we
8817 * handle FULLSPEED out of paranoia.
8818 *
8819 * So, we can safely only restore FULLSPEED or 7, anything
8820 * else could slow the fan. Restoring AUTO is useless, at
8821 * best that's exactly what the DSDT already set (it is the
8822 * slower it uses).
8823 *
8824 * Always keep in mind that the DSDT *will* have set the
8825 * fans to what the vendor supposes is the best level. We
8826 * muck with it only to speed the fan up.
8827 */
8828 if (fan_control_resume_level != 7 &&
8829 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8830 return;
8831 else
8832 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8833 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008834 break;
8835 default:
8836 return;
8837 }
8838 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008839 pr_notice("restoring fan level to 0x%02x\n",
8840 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008841 rc = fan_set_level_safe(fan_control_resume_level);
8842 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008843 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008844 }
8845}
8846
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008847static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008848{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008849 int rc;
8850 u8 status;
8851 unsigned int speed = 0;
8852
8853 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008854 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008855 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008856 rc = fan_get_status_safe(&status);
8857 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008858 return rc;
8859
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008860 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008861 "level:\t\t%d\n",
8862 (status != 0) ? "enabled" : "disabled", status);
8863 break;
8864
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008865 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008866 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008867 rc = fan_get_status_safe(&status);
8868 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008869 return rc;
8870
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008871 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008872 (status != 0) ? "enabled" : "disabled");
8873
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008874 rc = fan_get_speed(&speed);
8875 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008876 return rc;
8877
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008878 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008879
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008880 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008881 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008882 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008883 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008884 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008885 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008886 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008887 break;
8888
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008889 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008890 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008891 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008892 }
8893
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008894 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008895 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008896
8897 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008898 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008899 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008900 break;
8901
8902 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008903 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008904 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008905 break;
8906 }
8907 }
8908
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008909 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008910 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008911 "commands:\twatchdog <timeout> (<timeout> "
8912 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008913
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008914 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008915 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008916 " (<speed> is 0-65535)\n");
8917
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008918 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008919}
8920
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008921static int fan_write_cmd_level(const char *cmd, int *rc)
8922{
8923 int level;
8924
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008925 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008926 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008927 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008928 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008929 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008930 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008931 return 0;
8932
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008933 *rc = fan_set_level_safe(level);
8934 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008935 pr_err("level command accepted for unsupported access mode %d\n",
8936 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008937 else if (!*rc)
8938 tpacpi_disclose_usertask("procfs fan",
8939 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008940
8941 return 1;
8942}
8943
8944static int fan_write_cmd_enable(const char *cmd, int *rc)
8945{
8946 if (strlencmp(cmd, "enable") != 0)
8947 return 0;
8948
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008949 *rc = fan_set_enable();
8950 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008951 pr_err("enable command accepted for unsupported access mode %d\n",
8952 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008953 else if (!*rc)
8954 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008955
8956 return 1;
8957}
8958
8959static int fan_write_cmd_disable(const char *cmd, int *rc)
8960{
8961 if (strlencmp(cmd, "disable") != 0)
8962 return 0;
8963
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008964 *rc = fan_set_disable();
8965 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008966 pr_err("disable command accepted for unsupported access mode %d\n",
8967 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008968 else if (!*rc)
8969 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008970
8971 return 1;
8972}
8973
8974static int fan_write_cmd_speed(const char *cmd, int *rc)
8975{
8976 int speed;
8977
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008978 /* TODO:
8979 * Support speed <low> <medium> <high> ? */
8980
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008981 if (sscanf(cmd, "speed %d", &speed) != 1)
8982 return 0;
8983
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008984 *rc = fan_set_speed(speed);
8985 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008986 pr_err("speed command accepted for unsupported access mode %d\n",
8987 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008988 else if (!*rc)
8989 tpacpi_disclose_usertask("procfs fan",
8990 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008991
8992 return 1;
8993}
8994
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008995static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8996{
8997 int interval;
8998
8999 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9000 return 0;
9001
9002 if (interval < 0 || interval > 120)
9003 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009004 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009005 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00009006 tpacpi_disclose_usertask("procfs fan",
9007 "set watchdog timer to %d\n",
9008 interval);
9009 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009010
9011 return 1;
9012}
9013
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009014static int fan_write(char *buf)
9015{
9016 char *cmd;
9017 int rc = 0;
9018
9019 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009020 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009021 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009022 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009023 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009024 fan_write_cmd_disable(cmd, &rc) ||
9025 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03009026 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009027 fan_write_cmd_speed(cmd, &rc))
9028 )
9029 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02009030 else if (!rc)
9031 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009032 }
9033
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009034 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009035}
9036
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009037static struct ibm_struct fan_driver_data = {
9038 .name = "fan",
9039 .read = fan_read,
9040 .write = fan_write,
9041 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03009042 .suspend = fan_suspend,
9043 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009044};
9045
David Henningsson420f9732013-10-16 23:10:31 +02009046/*************************************************************************
9047 * Mute LED subdriver
9048 */
9049
9050
9051struct tp_led_table {
9052 acpi_string name;
9053 int on_value;
9054 int off_value;
9055 int state;
9056};
9057
9058static struct tp_led_table led_tables[] = {
9059 [TPACPI_LED_MUTE] = {
9060 .name = "SSMS",
9061 .on_value = 1,
9062 .off_value = 0,
9063 },
9064 [TPACPI_LED_MICMUTE] = {
9065 .name = "MMTS",
9066 .on_value = 2,
9067 .off_value = 0,
9068 },
9069};
9070
9071static int mute_led_on_off(struct tp_led_table *t, bool state)
9072{
9073 acpi_handle temp;
9074 int output;
9075
Axel Lin3526eca2016-09-19 09:33:51 +08009076 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009077 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9078 return -EIO;
9079 }
9080
9081 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9082 state ? t->on_value : t->off_value))
9083 return -EIO;
9084
9085 t->state = state;
9086 return state;
9087}
9088
9089int tpacpi_led_set(int whichled, bool on)
9090{
9091 struct tp_led_table *t;
9092
9093 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9094 return -EINVAL;
9095
9096 t = &led_tables[whichled];
9097 if (t->state < 0 || t->state == on)
9098 return t->state;
9099 return mute_led_on_off(t, on);
9100}
9101EXPORT_SYMBOL_GPL(tpacpi_led_set);
9102
9103static int mute_led_init(struct ibm_init_struct *iibm)
9104{
9105 acpi_handle temp;
9106 int i;
9107
9108 for (i = 0; i < TPACPI_LED_MAX; i++) {
9109 struct tp_led_table *t = &led_tables[i];
9110 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9111 mute_led_on_off(t, false);
9112 else
9113 t->state = -ENODEV;
9114 }
9115 return 0;
9116}
9117
9118static void mute_led_exit(void)
9119{
9120 int i;
9121
9122 for (i = 0; i < TPACPI_LED_MAX; i++)
9123 tpacpi_led_set(i, false);
9124}
9125
Takashi Iwai119f4492014-02-12 16:32:45 +01009126static void mute_led_resume(void)
9127{
9128 int i;
9129
9130 for (i = 0; i < TPACPI_LED_MAX; i++) {
9131 struct tp_led_table *t = &led_tables[i];
9132 if (t->state >= 0)
9133 mute_led_on_off(t, t->state);
9134 }
9135}
9136
David Henningsson420f9732013-10-16 23:10:31 +02009137static struct ibm_struct mute_led_driver_data = {
9138 .name = "mute_led",
9139 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01009140 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02009141};
9142
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009143/****************************************************************************
9144 ****************************************************************************
9145 *
9146 * Infrastructure
9147 *
9148 ****************************************************************************
9149 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07009150
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009151/*
9152 * HKEY event callout for other subdrivers go here
9153 * (yes, it is ugly, but it is quick, safe, and gets the job done
9154 */
9155static void tpacpi_driver_event(const unsigned int hkey_event)
9156{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00009157 if (ibm_backlight_device) {
9158 switch (hkey_event) {
9159 case TP_HKEY_EV_BRGHT_UP:
9160 case TP_HKEY_EV_BRGHT_DOWN:
9161 tpacpi_brightness_notify_change();
9162 }
9163 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009164 if (alsa_card) {
9165 switch (hkey_event) {
9166 case TP_HKEY_EV_VOL_UP:
9167 case TP_HKEY_EV_VOL_DOWN:
9168 case TP_HKEY_EV_VOL_MUTE:
9169 volume_alsa_notify_change();
9170 }
9171 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009172}
9173
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009174static void hotkey_driver_event(const unsigned int scancode)
9175{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03009176 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009177}
9178
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009179/* sysfs name ---------------------------------------------------------- */
9180static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
9181 struct device_attribute *attr,
9182 char *buf)
9183{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009184 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009185}
9186
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009187static DEVICE_ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009188
9189/* --------------------------------------------------------------------- */
9190
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009191/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03009192static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009193
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009194/*
9195 * Module and infrastructure proble, init and exit handling
9196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009197
Rusty Russell90ab5ee2012-01-13 09:32:20 +10309198static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009199
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009200#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009201static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009202{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009203 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009204
Jan van den Berg72a979f2014-09-17 00:01:08 +02009205 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009206}
9207#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9208
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009209static void ibm_exit(struct ibm_struct *ibm)
9210{
9211 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9212
9213 list_del_init(&ibm->all_drivers);
9214
9215 if (ibm->flags.acpi_notify_installed) {
9216 dbg_printk(TPACPI_DBG_EXIT,
9217 "%s: acpi_remove_notify_handler\n", ibm->name);
9218 BUG_ON(!ibm->acpi);
9219 acpi_remove_notify_handler(*ibm->acpi->handle,
9220 ibm->acpi->type,
9221 dispatch_acpi_notify);
9222 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009223 }
9224
9225 if (ibm->flags.proc_created) {
9226 dbg_printk(TPACPI_DBG_EXIT,
9227 "%s: remove_proc_entry\n", ibm->name);
9228 remove_proc_entry(ibm->name, proc_dir);
9229 ibm->flags.proc_created = 0;
9230 }
9231
9232 if (ibm->flags.acpi_driver_registered) {
9233 dbg_printk(TPACPI_DBG_EXIT,
9234 "%s: acpi_bus_unregister_driver\n", ibm->name);
9235 BUG_ON(!ibm->acpi);
9236 acpi_bus_unregister_driver(ibm->acpi->driver);
9237 kfree(ibm->acpi->driver);
9238 ibm->acpi->driver = NULL;
9239 ibm->flags.acpi_driver_registered = 0;
9240 }
9241
9242 if (ibm->flags.init_called && ibm->exit) {
9243 ibm->exit();
9244 ibm->flags.init_called = 0;
9245 }
9246
9247 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9248}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009249
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009250static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009251{
9252 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009253 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009254 struct proc_dir_entry *entry;
9255
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009256 BUG_ON(ibm == NULL);
9257
9258 INIT_LIST_HEAD(&ibm->all_drivers);
9259
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009260 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261 return 0;
9262
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009263 dbg_printk(TPACPI_DBG_INIT,
9264 "probing for %s\n", ibm->name);
9265
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009266 if (iibm->init) {
9267 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009268 if (ret > 0)
9269 return 0; /* probe failed */
9270 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009271 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009272
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009273 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009274 }
9275
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009276 if (ibm->acpi) {
9277 if (ibm->acpi->hid) {
9278 ret = register_tpacpi_subdriver(ibm);
9279 if (ret)
9280 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009281 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009282
9283 if (ibm->acpi->notify) {
9284 ret = setup_acpi_notify(ibm);
9285 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07009286 pr_notice("disabling subdriver %s\n",
9287 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009288 ret = 0;
9289 goto err_out;
9290 }
9291 if (ret < 0)
9292 goto err_out;
9293 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009294 }
9295
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009296 dbg_printk(TPACPI_DBG_INIT,
9297 "%s installed\n", ibm->name);
9298
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009299 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04009300 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009301
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009302 if (!mode)
9303 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009304 if (ibm->write)
9305 mode |= S_IWUSR;
9306 entry = proc_create_data(ibm->name, mode, proc_dir,
9307 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009308 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07009309 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009310 ret = -ENODEV;
9311 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009312 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009313 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009316 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9317
Linus Torvalds1da177e2005-04-16 15:20:36 -07009318 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009319
9320err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009321 dbg_printk(TPACPI_DBG_INIT,
9322 "%s: at error exit path with result %d\n",
9323 ibm->name, ret);
9324
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009325 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +02009326 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009327}
9328
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009329/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009331static bool __pure __init tpacpi_is_fw_digit(const char c)
9332{
9333 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9334}
9335
Jan van den Berg72a979f2014-09-17 00:01:08 +02009336static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009337 const char t)
9338{
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009339 /*
9340 * Most models: xxyTkkWW (#.##c)
9341 * Ancient 570/600 and -SL lacks (#.##c)
9342 */
9343 if (s && strlen(s) >= 8 &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009344 tpacpi_is_fw_digit(s[0]) &&
9345 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009346 s[2] == t &&
9347 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009348 tpacpi_is_fw_digit(s[4]) &&
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009349 tpacpi_is_fw_digit(s[5]))
9350 return true;
9351
9352 /* New models: xxxyTkkW (#.##c); T550 and some others */
9353 return s && strlen(s) >= 8 &&
9354 tpacpi_is_fw_digit(s[0]) &&
9355 tpacpi_is_fw_digit(s[1]) &&
9356 tpacpi_is_fw_digit(s[2]) &&
9357 s[3] == t &&
9358 (s[4] == 'T' || s[4] == 'N') &&
9359 tpacpi_is_fw_digit(s[5]) &&
9360 tpacpi_is_fw_digit(s[6]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009361}
9362
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009363/* returns 0 - probe ok, or < 0 - probe error.
9364 * Probe ok doesn't mean thinkpad found.
9365 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9366static int __must_check __init get_thinkpad_model_data(
9367 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009368{
Jeff Garzik18552562007-10-03 15:15:40 -04009369 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009370 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009371 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009372
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009373 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009374 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009375
9376 memset(tp, 0, sizeof(*tp));
9377
9378 if (dmi_name_in_vendors("IBM"))
9379 tp->vendor = PCI_VENDOR_ID_IBM;
9380 else if (dmi_name_in_vendors("LENOVO"))
9381 tp->vendor = PCI_VENDOR_ID_LENOVO;
9382 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009383 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009384
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009385 s = dmi_get_system_info(DMI_BIOS_VERSION);
9386 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9387 if (s && !tp->bios_version_str)
9388 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009389
9390 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009391 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9392 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009393 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009394
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009395 tp->bios_model = tp->bios_version_str[0]
9396 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009397 tp->bios_release = (tp->bios_version_str[4] << 8)
9398 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009399
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009400 /*
9401 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9402 * X32 or newer, all Z series; Some models must have an
9403 * up-to-date BIOS or they will not be detected.
9404 *
9405 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9406 */
9407 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009408 if (sscanf(dev->name,
9409 "IBM ThinkPad Embedded Controller -[%17c",
9410 ec_fw_string) == 1) {
9411 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9412 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009413
9414 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009415 if (!tp->ec_version_str)
9416 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009417
9418 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9419 tp->ec_model = ec_fw_string[0]
9420 | (ec_fw_string[1] << 8);
9421 tp->ec_release = (ec_fw_string[4] << 8)
9422 | ec_fw_string[5];
9423 } else {
Joe Perches0978e012011-04-04 10:06:25 -07009424 pr_notice("ThinkPad firmware release %s "
9425 "doesn't match the known patterns\n",
9426 ec_fw_string);
9427 pr_notice("please report this to %s\n",
9428 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009429 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009430 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009431 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009432 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009433
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009434 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009435 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009436 tp->model_str = kstrdup(s, GFP_KERNEL);
9437 if (!tp->model_str)
9438 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009439 } else {
9440 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009441 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009442 tp->model_str = kstrdup(s, GFP_KERNEL);
9443 if (!tp->model_str)
9444 return -ENOMEM;
9445 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009446 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03009447
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009448 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9449 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9450 if (s && !tp->nummodel_str)
9451 return -ENOMEM;
9452
9453 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009454}
9455
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009456static int __init probe_for_thinkpad(void)
9457{
9458 int is_thinkpad;
9459
9460 if (acpi_disabled)
9461 return -ENODEV;
9462
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009463 /* It would be dangerous to run the driver in this case */
9464 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9465 return -ENODEV;
9466
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009467 /*
9468 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009469 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009470 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009471 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9472 (thinkpad_id.ec_model != 0) ||
9473 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009474
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03009475 /* The EC handler is required */
9476 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009477 if (!ec_handle) {
9478 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07009479 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009480 return -ENODEV;
9481 }
9482
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009483 if (!is_thinkpad && !force_load)
9484 return -ENODEV;
9485
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009486 return 0;
9487}
9488
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009489static void __init thinkpad_acpi_init_banner(void)
9490{
Joe Perches0978e012011-04-04 10:06:25 -07009491 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9492 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009493
Joe Perches0978e012011-04-04 10:06:25 -07009494 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009495 (thinkpad_id.bios_version_str) ?
9496 thinkpad_id.bios_version_str : "unknown",
9497 (thinkpad_id.ec_version_str) ?
9498 thinkpad_id.ec_version_str : "unknown");
9499
9500 BUG_ON(!thinkpad_id.vendor);
9501
9502 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07009503 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009504 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9505 "IBM" : ((thinkpad_id.vendor ==
9506 PCI_VENDOR_ID_LENOVO) ?
9507 "Lenovo" : "Unknown vendor"),
9508 thinkpad_id.model_str,
9509 (thinkpad_id.nummodel_str) ?
9510 thinkpad_id.nummodel_str : "unknown");
9511}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009512
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009513/* Module init, exit, parameters */
9514
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009515static struct ibm_init_struct ibms_init[] __initdata = {
9516 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009517 .data = &thinkpad_acpi_driver_data,
9518 },
9519 {
9520 .init = hotkey_init,
9521 .data = &hotkey_driver_data,
9522 },
9523 {
9524 .init = bluetooth_init,
9525 .data = &bluetooth_driver_data,
9526 },
9527 {
9528 .init = wan_init,
9529 .data = &wan_driver_data,
9530 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009531 {
9532 .init = uwb_init,
9533 .data = &uwb_driver_data,
9534 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009535#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009536 {
9537 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009538 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009539 .data = &video_driver_data,
9540 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009541#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009542 {
Pali Rohárbb28f3d52015-12-30 23:27:41 +01009543 .init = kbdlight_init,
9544 .data = &kbdlight_driver_data,
9545 },
9546 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009547 .init = light_init,
9548 .data = &light_driver_data,
9549 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009550 {
9551 .init = cmos_init,
9552 .data = &cmos_driver_data,
9553 },
9554 {
9555 .init = led_init,
9556 .data = &led_driver_data,
9557 },
9558 {
9559 .init = beep_init,
9560 .data = &beep_driver_data,
9561 },
9562 {
9563 .init = thermal_init,
9564 .data = &thermal_driver_data,
9565 },
9566 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009567 .init = brightness_init,
9568 .data = &brightness_driver_data,
9569 },
9570 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009571 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009572 .data = &volume_driver_data,
9573 },
9574 {
9575 .init = fan_init,
9576 .data = &fan_driver_data,
9577 },
David Henningsson420f9732013-10-16 23:10:31 +02009578 {
9579 .init = mute_led_init,
9580 .data = &mute_led_driver_data,
9581 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009582};
9583
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009584static int __init set_ibm_param(const char *val, struct kernel_param *kp)
9585{
9586 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009587 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009588
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009589 if (!kp || !kp->name || !val)
9590 return -EINVAL;
9591
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009592 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9593 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009594 WARN_ON(ibm == NULL);
9595
9596 if (!ibm || !ibm->name)
9597 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009598
9599 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9600 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009601 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009602 strcpy(ibms_init[i].param, val);
9603 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009604 return 0;
9605 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009606 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009607
9608 return -EINVAL;
9609}
9610
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009611module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009612MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009613 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009614
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009615module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009616MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009617
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009618module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009619MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009620 "Attempts to load the driver even on a "
9621 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009622
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009623module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009624MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009625 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03009626
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009627module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009628MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009629 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00009630 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03009631
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009632module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009633MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009634 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02009635
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009636#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009637module_param_named(volume_mode, volume_mode, uint, 0444);
9638MODULE_PARM_DESC(volume_mode,
9639 "Selects volume control strategy: "
9640 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9641
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02009642module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9643MODULE_PARM_DESC(volume_capabilities,
9644 "Selects the mixer capabilites: "
9645 "0=auto, 1=volume and mute, 2=mute only");
9646
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02009647module_param_named(volume_control, volume_control_allowed, bool, 0444);
9648MODULE_PARM_DESC(volume_control,
9649 "Enables software override for the console audio "
9650 "control when true");
9651
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07009652module_param_named(software_mute, software_mute_requested, bool, 0444);
9653MODULE_PARM_DESC(software_mute,
9654 "Request full software mute control");
9655
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009656/* ALSA module API parameters */
9657module_param_named(index, alsa_index, int, 0444);
9658MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9659module_param_named(id, alsa_id, charp, 0444);
9660MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9661module_param_named(enable, alsa_enable, bool, 0444);
9662MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009663#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009664
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009665#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009666 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02009667 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009668 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009669
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009670TPACPI_PARAM(hotkey);
9671TPACPI_PARAM(bluetooth);
9672TPACPI_PARAM(video);
9673TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009674TPACPI_PARAM(cmos);
9675TPACPI_PARAM(led);
9676TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009677TPACPI_PARAM(brightness);
9678TPACPI_PARAM(volume);
9679TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009680
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009681#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009682module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009683MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9684module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9685MODULE_PARM_DESC(wlsw_state,
9686 "Initial state of the emulated WLSW switch");
9687
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009688module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009689MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9690module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9691MODULE_PARM_DESC(bluetooth_state,
9692 "Initial state of the emulated bluetooth switch");
9693
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009694module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009695MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9696module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9697MODULE_PARM_DESC(wwan_state,
9698 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009699
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009700module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009701MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9702module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9703MODULE_PARM_DESC(uwb_state,
9704 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009705#endif
9706
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009707static void thinkpad_acpi_module_exit(void)
9708{
9709 struct ibm_struct *ibm, *itmp;
9710
9711 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9712
9713 list_for_each_entry_safe_reverse(ibm, itmp,
9714 &tpacpi_all_drivers,
9715 all_drivers) {
9716 ibm_exit(ibm);
9717 }
9718
9719 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9720
9721 if (tpacpi_inputdev) {
9722 if (tp_features.input_device_registered)
9723 input_unregister_device(tpacpi_inputdev);
9724 else
9725 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +10009726 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009727 }
9728
9729 if (tpacpi_hwmon)
9730 hwmon_device_unregister(tpacpi_hwmon);
9731
9732 if (tp_features.sensors_pdev_attrs_registered)
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009733 device_remove_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009734 if (tpacpi_sensors_pdev)
9735 platform_device_unregister(tpacpi_sensors_pdev);
9736 if (tpacpi_pdev)
9737 platform_device_unregister(tpacpi_pdev);
9738
9739 if (tp_features.sensors_pdrv_attrs_registered)
9740 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9741 if (tp_features.platform_drv_attrs_registered)
9742 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9743
9744 if (tp_features.sensors_pdrv_registered)
9745 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9746
9747 if (tp_features.platform_drv_registered)
9748 platform_driver_unregister(&tpacpi_pdriver);
9749
9750 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009751 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009752
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009753 if (tpacpi_wq)
9754 destroy_workqueue(tpacpi_wq);
9755
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009756 kfree(thinkpad_id.bios_version_str);
9757 kfree(thinkpad_id.ec_version_str);
9758 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +10009759 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009760}
9761
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009762
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009763static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764{
9765 int ret, i;
9766
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009767 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9768
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009769 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009770
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009771 ret = get_thinkpad_model_data(&thinkpad_id);
9772 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009773 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009774 thinkpad_acpi_module_exit();
9775 return ret;
9776 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009777 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009778 if (ret) {
9779 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009780 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009782
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009783 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009784
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009785 thinkpad_acpi_init_banner();
9786 tpacpi_check_outdated_fw();
9787
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009788 TPACPI_ACPIHANDLE_INIT(ecrd);
9789 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009791 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9792 if (!tpacpi_wq) {
9793 thinkpad_acpi_module_exit();
9794 return -ENOMEM;
9795 }
9796
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009797 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009798 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07009799 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009800 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009801 return -ENODEV;
9802 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009803
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009804 ret = platform_driver_register(&tpacpi_pdriver);
9805 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009806 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009807 thinkpad_acpi_module_exit();
9808 return ret;
9809 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009810 tp_features.platform_drv_registered = 1;
9811
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009812 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9813 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009814 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009815 thinkpad_acpi_module_exit();
9816 return ret;
9817 }
9818 tp_features.sensors_pdrv_registered = 1;
9819
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009820 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009821 if (!ret) {
9822 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009823 ret = tpacpi_create_driver_attributes(
9824 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009825 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009826 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009827 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009828 thinkpad_acpi_module_exit();
9829 return ret;
9830 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009831 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009832
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009833
9834 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009835 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009836 NULL, 0);
9837 if (IS_ERR(tpacpi_pdev)) {
9838 ret = PTR_ERR(tpacpi_pdev);
9839 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009840 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009841 thinkpad_acpi_module_exit();
9842 return ret;
9843 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009844 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009845 TPACPI_HWMON_DRVR_NAME,
9846 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009847 if (IS_ERR(tpacpi_sensors_pdev)) {
9848 ret = PTR_ERR(tpacpi_sensors_pdev);
9849 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009850 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009851 thinkpad_acpi_module_exit();
9852 return ret;
9853 }
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009854 ret = device_create_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009855 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009856 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009857 thinkpad_acpi_module_exit();
9858 return ret;
9859 }
9860 tp_features.sensors_pdev_attrs_registered = 1;
9861 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009862 if (IS_ERR(tpacpi_hwmon)) {
9863 ret = PTR_ERR(tpacpi_hwmon);
9864 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009865 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009866 thinkpad_acpi_module_exit();
9867 return ret;
9868 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009869 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009870 tpacpi_inputdev = input_allocate_device();
9871 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009872 thinkpad_acpi_module_exit();
9873 return -ENOMEM;
9874 } else {
9875 /* Prepare input device, but don't register */
9876 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009877 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009878 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009879 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009880 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9881 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009882 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009883 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009884
9885 /* Init subdriver dependencies */
9886 tpacpi_detect_brightness_capabilities();
9887
9888 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009889 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9890 ret = ibm_init(&ibms_init[i]);
9891 if (ret >= 0 && *ibms_init[i].param)
9892 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009893 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009894 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009895 return ret;
9896 }
9897 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009898
9899 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9900
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009901 ret = input_register_device(tpacpi_inputdev);
9902 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009903 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009904 thinkpad_acpi_module_exit();
9905 return ret;
9906 } else {
9907 tp_features.input_device_registered = 1;
9908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009909
9910 return 0;
9911}
9912
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009913MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9914
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009915/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009916 * This will autoload the driver in almost every ThinkPad
9917 * in widespread use.
9918 *
9919 * Only _VERY_ old models, like the 240, 240x and 570 lack
9920 * the HKEY event interface.
9921 */
9922MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9923
9924/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009925 * DMI matching for module autoloading
9926 *
9927 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9928 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9929 *
9930 * Only models listed in thinkwiki will be supported, so add yours
9931 * if it is not there yet.
9932 */
9933#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009934 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009935
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009936/* Ancient thinkpad BIOSes have to be identified by
9937 * BIOS type or model number, and there are far less
9938 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009939IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009940
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009941MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9942MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009943MODULE_DESCRIPTION(TPACPI_DESC);
9944MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009945MODULE_LICENSE("GPL");
9946
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009947module_init(thinkpad_acpi_module_init);
9948module_exit(thinkpad_acpi_module_exit);