blob: 1d18b32628ecbf501aff7193fc857b0126a56f58 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Joe Perches0978e012011-04-04 10:06:25 -070024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
David Henningsson420f9732013-10-16 23:10:31 +020026#define TPACPI_VERSION "0.25"
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020027#define TPACPI_SYSFS_VERSION 0x020700
Borislav Deianov78f81cc2005-08-17 00:00:00 -040028
29/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020031 * 2007-10-20 changelog trimmed down
32 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030033 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020035 *
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030039 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040040 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020042 *
43 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040044 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
51
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020052#include <linux/kernel.h>
53#include <linux/module.h>
54#include <linux/init.h>
55#include <linux/types.h>
56#include <linux/string.h>
57#include <linux/list.h>
58#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000059#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020060#include <linux/kthread.h>
61#include <linux/freezer.h>
62#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020064#include <linux/nvram.h>
65#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020066#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020067#include <linux/sysfs.h>
68#include <linux/backlight.h>
69#include <linux/fb.h>
70#include <linux/platform_device.h>
71#include <linux/hwmon.h>
72#include <linux/hwmon-sysfs.h>
73#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030074#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030075#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020076#include <linux/dmi.h>
77#include <linux/jiffies.h>
78#include <linux/workqueue.h>
Lv Zheng8b484632013-12-03 08:49:16 +080079#include <linux/acpi.h>
80#include <linux/pci_ids.h>
81#include <linux/thinkpad_acpi.h>
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020082#include <sound/core.h>
83#include <sound/control.h>
84#include <sound/initval.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080085#include <linux/uaccess.h>
Hans de Goedeb33c6ce2015-06-16 16:28:10 +020086#include <acpi/video.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020087
88/* ThinkPad CMOS commands */
89#define TP_CMOS_VOLUME_DOWN 0
90#define TP_CMOS_VOLUME_UP 1
91#define TP_CMOS_VOLUME_MUTE 2
92#define TP_CMOS_BRIGHTNESS_UP 4
93#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030094#define TP_CMOS_THINKLIGHT_ON 12
95#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020096
97/* NVRAM Addresses */
98enum tp_nvram_addr {
99 TP_NVRAM_ADDR_HK2 = 0x57,
100 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
101 TP_NVRAM_ADDR_VIDEO = 0x59,
102 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
103 TP_NVRAM_ADDR_MIXER = 0x60,
104};
105
106/* NVRAM bit masks */
107enum {
108 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
109 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
110 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
111 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
112 TP_NVRAM_MASK_THINKLIGHT = 0x10,
113 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
114 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
115 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
116 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
117 TP_NVRAM_MASK_MUTE = 0x40,
118 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
119 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
120 TP_NVRAM_POS_LEVEL_VOLUME = 0,
121};
122
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300123/* Misc NVRAM-related */
124enum {
125 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
126};
127
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200128/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400129#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
130#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Hui Wanga3c42a42016-11-08 16:13:23 +0800131#define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300132#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200133
134/* Input IDs */
135#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
136#define TPACPI_HKEY_INPUT_VERSION 0x4101
137
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200138/* ACPI \WGSV commands */
139enum {
140 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
141 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
142 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
143 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
144};
145
146/* TP_ACPI_WGSV_GET_STATE bits */
147enum {
148 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
149 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
150 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
151 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
152 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
153 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
154 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
155 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
156 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
157 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
158};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200159
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300160/* HKEY events */
161enum tpacpi_hkey_event_t {
162 /* Hotkey-related */
163 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
164 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
165 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
Hans de Goedec685e202017-02-09 16:44:13 +0100166 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300167 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
168 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
169 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
170
171 /* Reasons for waking up from S3/S4 */
172 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
173 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
174 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
175 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
176 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
177 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
178
179 /* Auto-sleep after eject request */
180 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
181 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
182
183 /* Misc bay events */
184 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -0300185 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
186 or port replicator */
187 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
188 dock or port replicator */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300189
190 /* User-interface events */
191 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
192 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
193 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
194 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
Lyudeb03f4d42016-11-11 15:15:03 -0500195 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
196 * enter/leave tablet mode
197 */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300198 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
199 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
200 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
201
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300202 /* Key-related user-interface events */
203 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
204 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
Xavier Naveira67ab6242015-02-10 08:45:18 +0100205 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300206
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300207 /* Thermal events */
208 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
209 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
210 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
211 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
212 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
213
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100214 /* AC-related events */
215 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300216
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300217 /* Misc */
218 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
219};
220
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200221/****************************************************************************
222 * Main driver
223 */
224
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200225#define TPACPI_NAME "thinkpad"
226#define TPACPI_DESC "ThinkPad ACPI Extras"
227#define TPACPI_FILE TPACPI_NAME "_acpi"
228#define TPACPI_URL "http://ibm-acpi.sf.net/"
229#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200230
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200231#define TPACPI_PROC_DIR "ibm"
232#define TPACPI_ACPI_EVENT_PREFIX "ibm"
233#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300234#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200235#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200236
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300237#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300238#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300239
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200240#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200241
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000242/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200243#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000244#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200245#define TPACPI_DBG_INIT 0x0001
246#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000247#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000248#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000249#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000250#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200251#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200252
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200253#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
254#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
255#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200256
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200257
258/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200259 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200260 */
261
262struct ibm_struct;
263
264struct tp_acpi_drv_struct {
265 const struct acpi_device_id *hid;
266 struct acpi_driver *driver;
267
268 void (*notify) (struct ibm_struct *, u32);
269 acpi_handle *handle;
270 u32 type;
271 struct acpi_device *device;
272};
273
274struct ibm_struct {
275 char *name;
276
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200277 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200278 int (*write) (char *);
279 void (*exit) (void);
280 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200281 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200282 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200283
284 struct list_head all_drivers;
285
286 struct tp_acpi_drv_struct *acpi;
287
288 struct {
289 u8 acpi_driver_registered:1;
290 u8 acpi_notify_installed:1;
291 u8 proc_created:1;
292 u8 init_called:1;
293 u8 experimental:1;
294 } flags;
295};
296
297struct ibm_init_struct {
298 char param[32];
299
300 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400301 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200302 struct ibm_struct *data;
303};
304
305static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200306 u32 bluetooth:1;
307 u32 hotkey:1;
308 u32 hotkey_mask:1;
309 u32 hotkey_wlsw:1;
Lyudeb3180022016-11-11 15:15:02 -0500310 enum {
311 TP_HOTKEY_TABLET_NONE = 0,
312 TP_HOTKEY_TABLET_USES_MHKG,
Lyudeb03f4d42016-11-11 15:15:03 -0500313 /* X1 Yoga 2016, seen on BIOS N1FET44W */
314 TP_HOTKEY_TABLET_USES_CMMD,
Lyudeb3180022016-11-11 15:15:02 -0500315 } hotkey_tablet;
Pali Rohárbb28f3d52015-12-30 23:27:41 +0100316 u32 kbdlight:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200317 u32 light:1;
318 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300319 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300320 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200321 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200322 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200323 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300324 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300325 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200326 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200327 u32 input_device_registered:1;
328 u32 platform_drv_registered:1;
329 u32 platform_drv_attrs_registered:1;
330 u32 sensors_pdrv_registered:1;
331 u32 sensors_pdrv_attrs_registered:1;
332 u32 sensors_pdev_attrs_registered:1;
333 u32 hotkey_poll_active:1;
Bastien Noceraf23a5bc2015-03-02 14:45:16 +0100334 u32 has_adaptive_kbd:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200335} tp_features;
336
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300337static struct {
338 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200339 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300340} tp_warned;
341
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200342struct thinkpad_id_data {
343 unsigned int vendor; /* ThinkPad vendor:
344 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
345
346 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
347 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
348
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300349 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200350 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300351 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
352 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200353
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300354 char *model_str; /* ThinkPad T43 */
355 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200356};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200357static struct thinkpad_id_data thinkpad_id;
358
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300359static enum {
360 TPACPI_LIFE_INIT = 0,
361 TPACPI_LIFE_RUNNING,
362 TPACPI_LIFE_EXITING,
363} tpacpi_lifecycle;
364
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200365static int experimental;
366static u32 dbg_level;
367
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300368static struct workqueue_struct *tpacpi_wq;
369
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000370enum led_status_t {
371 TPACPI_LED_OFF = 0,
372 TPACPI_LED_ON,
373 TPACPI_LED_BLINK,
374};
375
Hans de Goede86ec0c22017-02-09 16:44:12 +0100376/* tpacpi LED class */
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300377struct tpacpi_led_classdev {
378 struct led_classdev led_classdev;
Adam Leeedf2d772013-06-08 16:51:15 +0800379 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300380};
381
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300382/* brightness level capabilities */
383static unsigned int bright_maxlvl; /* 0 = unknown */
384
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200385#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
386static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030387static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200388static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030389static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200390static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030391static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200392static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030393static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200394#endif
395
396
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000397/*************************************************************************
398 * Debugging helpers
399 */
400
Joe Perches0978e012011-04-04 10:06:25 -0700401#define dbg_printk(a_dbg_level, format, arg...) \
402do { \
403 if (dbg_level & (a_dbg_level)) \
404 printk(KERN_DEBUG pr_fmt("%s: " format), \
405 __func__, ##arg); \
406} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000407
408#ifdef CONFIG_THINKPAD_ACPI_DEBUG
409#define vdbg_printk dbg_printk
410static const char *str_supported(int is_supported);
411#else
Joe Perches0978e012011-04-04 10:06:25 -0700412static inline const char *str_supported(int is_supported) { return ""; }
413#define vdbg_printk(a_dbg_level, format, arg...) \
Joe Perchesefd85cf2015-08-26 11:13:38 -0700414 do { if (0) no_printk(format, ##arg); } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000415#endif
416
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000417static void tpacpi_log_usertask(const char * const what)
418{
Joe Perches0978e012011-04-04 10:06:25 -0700419 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
420 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000421}
422
Joe Perches0978e012011-04-04 10:06:25 -0700423#define tpacpi_disclose_usertask(what, format, arg...) \
424do { \
425 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
426 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
427 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
428 what, task_tgid_vnr(current), ## arg); \
429 } \
430} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000431
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300432/*
433 * Quirk handling helpers
434 *
435 * ThinkPad IDs and versions seen in the field so far
436 * are two-characters from the set [0-9A-Z], i.e. base 36.
437 *
438 * We use values well outside that range as specials.
439 */
440
441#define TPACPI_MATCH_ANY 0xffffU
442#define TPACPI_MATCH_UNKNOWN 0U
443
444/* TPID('1', 'Y') == 0x5931 */
445#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
446
447#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_IBM, \
449 .bios = TPID(__id1, __id2), \
450 .ec = TPACPI_MATCH_ANY, \
451 .quirks = (__quirk) }
452
453#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
454 { .vendor = PCI_VENDOR_ID_LENOVO, \
455 .bios = TPID(__id1, __id2), \
456 .ec = TPACPI_MATCH_ANY, \
457 .quirks = (__quirk) }
458
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200459#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
460 { .vendor = PCI_VENDOR_ID_LENOVO, \
461 .bios = TPACPI_MATCH_ANY, \
462 .ec = TPID(__id1, __id2), \
463 .quirks = (__quirk) }
464
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300465struct tpacpi_quirk {
466 unsigned int vendor;
467 u16 bios;
468 u16 ec;
469 unsigned long quirks;
470};
471
472/**
473 * tpacpi_check_quirks() - search BIOS/EC version on a list
474 * @qlist: array of &struct tpacpi_quirk
475 * @qlist_size: number of elements in @qlist
476 *
477 * Iterates over a quirks list until one is found that matches the
478 * ThinkPad's vendor, BIOS and EC model.
479 *
480 * Returns 0 if nothing matches, otherwise returns the quirks field of
481 * the matching &struct tpacpi_quirk entry.
482 *
483 * The match criteria is: vendor, ec and bios much match.
484 */
485static unsigned long __init tpacpi_check_quirks(
486 const struct tpacpi_quirk *qlist,
487 unsigned int qlist_size)
488{
489 while (qlist_size) {
490 if ((qlist->vendor == thinkpad_id.vendor ||
491 qlist->vendor == TPACPI_MATCH_ANY) &&
492 (qlist->bios == thinkpad_id.bios_model ||
493 qlist->bios == TPACPI_MATCH_ANY) &&
494 (qlist->ec == thinkpad_id.ec_model ||
495 qlist->ec == TPACPI_MATCH_ANY))
496 return qlist->quirks;
497
498 qlist_size--;
499 qlist++;
500 }
501 return 0;
502}
503
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300504static inline bool __pure __init tpacpi_is_lenovo(void)
505{
506 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
507}
508
509static inline bool __pure __init tpacpi_is_ibm(void)
510{
511 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
512}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300513
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300514/****************************************************************************
515 ****************************************************************************
516 *
517 * ACPI Helpers and device model
518 *
519 ****************************************************************************
520 ****************************************************************************/
521
522/*************************************************************************
523 * ACPI basic handles
524 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300526static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300527static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200529#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700531 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300532 &parent##_handle; \
533 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200535TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
536TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200538TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
539 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400540 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
541 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300542 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400543
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200544TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400545 "^HKEY", /* R30, R31 */
546 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300547 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400548
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300549/*************************************************************************
550 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200551 */
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700554 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400557 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200558 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400559 struct acpi_buffer result, *resultp;
560 union acpi_object out_obj;
561 acpi_status status;
562 va_list ap;
563 char res_type;
564 int success;
565 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700568 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return 0;
570 }
571
572 if (*fmt == 'q') {
573 quiet = 1;
574 fmt++;
575 } else
576 quiet = 0;
577
578 res_type = *(fmt++);
579
580 params.count = 0;
581 params.pointer = &in_objs[0];
582
583 va_start(ap, fmt);
584 while (*fmt) {
585 char c = *(fmt++);
586 switch (c) {
587 case 'd': /* int */
588 in_objs[params.count].integer.value = va_arg(ap, int);
589 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
590 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400591 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 default:
Joe Perches0978e012011-04-04 10:06:25 -0700593 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 "with invalid format character '%c'\n", c);
Jesper Juhl21365852010-12-24 19:56:28 +0100595 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return 0;
597 }
598 }
599 va_end(ap);
600
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400601 if (res_type != 'v') {
602 result.length = sizeof(out_obj);
603 result.pointer = &out_obj;
604 resultp = &result;
605 } else
606 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400608 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400611 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300612 success = (status == AE_OK &&
613 out_obj.type == ACPI_TYPE_INTEGER);
614 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700615 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400617 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 success = status == AE_OK;
619 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400620 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 default:
Joe Perches0978e012011-04-04 10:06:25 -0700622 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 "with invalid format character '%c'\n", res_type);
624 return 0;
625 }
626
627 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700628 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300629 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 return success;
632}
633
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200634static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300635{
636 int v;
637
638 if (ecrd_handle) {
639 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
640 return 0;
641 *p = v;
642 } else {
643 if (ec_read(i, p) < 0)
644 return 0;
645 }
646
647 return 1;
648}
649
650static int acpi_ec_write(int i, u8 v)
651{
652 if (ecwr_handle) {
653 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
654 return 0;
655 } else {
656 if (ec_write(i, v) < 0)
657 return 0;
658 }
659
660 return 1;
661}
662
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300663static int issue_thinkpad_cmos_command(int cmos_cmd)
664{
665 if (!cmos_handle)
666 return -ENXIO;
667
668 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
669 return -EIO;
670
671 return 0;
672}
673
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300674/*************************************************************************
675 * ACPI device model
676 */
677
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200678#define TPACPI_ACPIHANDLE_INIT(object) \
679 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300680 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200681
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300682static void __init drv_acpi_handle_init(const char *name,
683 acpi_handle *handle, const acpi_handle parent,
684 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300685{
686 int i;
687 acpi_status status;
688
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300689 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
690 name);
691
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300692 for (i = 0; i < num_paths; i++) {
693 status = acpi_get_handle(parent, paths[i], handle);
694 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300695 dbg_printk(TPACPI_DBG_INIT,
696 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300697 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300698 return;
699 }
700 }
701
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300702 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
703 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300704 *handle = NULL;
705}
706
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300707static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
708 u32 level, void *context, void **return_value)
709{
Aaron Lu46445b62013-10-11 21:27:46 +0800710 struct acpi_device *dev;
711 if (!strcmp(context, "video")) {
712 if (acpi_bus_get_device(handle, &dev))
713 return AE_OK;
714 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
715 return AE_OK;
716 }
717
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300718 *(acpi_handle *)return_value = handle;
719
720 return AE_CTRL_TERMINATE;
721}
722
723static void __init tpacpi_acpi_handle_locate(const char *name,
724 const char *hid,
725 acpi_handle *handle)
726{
727 acpi_status status;
728 acpi_handle device_found;
729
Aaron Lu46445b62013-10-11 21:27:46 +0800730 BUG_ON(!name || !handle);
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300731 vdbg_printk(TPACPI_DBG_INIT,
732 "trying to locate ACPI handle for %s, using HID %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +0800733 name, hid ? hid : "NULL");
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300734
735 memset(&device_found, 0, sizeof(device_found));
736 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
737 (void *)name, &device_found);
738
739 *handle = NULL;
740
741 if (ACPI_SUCCESS(status)) {
742 *handle = device_found;
743 dbg_printk(TPACPI_DBG_INIT,
744 "Found ACPI handle for %s\n", name);
745 } else {
746 vdbg_printk(TPACPI_DBG_INIT,
747 "Could not locate an ACPI handle for %s: %s\n",
748 name, acpi_format_exception(status));
749 }
750}
751
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300752static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300753{
754 struct ibm_struct *ibm = data;
755
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300756 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
757 return;
758
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300759 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300760 return;
761
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300762 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300763}
764
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300765static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300766{
767 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300768 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300769
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300770 BUG_ON(!ibm->acpi);
771
772 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300773 return 0;
774
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300775 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300776 "setting up ACPI notify for %s\n", ibm->name);
777
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300778 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
779 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700780 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300781 return -ENODEV;
782 }
783
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700784 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300785 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200786 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300787 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300788
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300789 status = acpi_install_notify_handler(*ibm->acpi->handle,
790 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300791 if (ACPI_FAILURE(status)) {
792 if (status == AE_ALREADY_EXISTS) {
Joe Perches0978e012011-04-04 10:06:25 -0700793 pr_notice("another device driver is already "
794 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300795 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700796 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300797 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300798 }
799 return -ENODEV;
800 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300801 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300802 return 0;
803}
804
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300805static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300806{
807 return 0;
808}
809
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300810static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300811{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300812 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300813
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300814 dbg_printk(TPACPI_DBG_INIT,
815 "registering %s as an ACPI driver\n", ibm->name);
816
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300817 BUG_ON(!ibm->acpi);
818
819 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
820 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700821 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300822 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300823 }
824
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200825 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300826 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200827
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300828 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300829
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300830 rc = acpi_bus_register_driver(ibm->acpi->driver);
831 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700832 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200833 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300834 kfree(ibm->acpi->driver);
835 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300836 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300837 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300838
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300839 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300840}
841
842
843/****************************************************************************
844 ****************************************************************************
845 *
846 * Procfs Helpers
847 *
848 ****************************************************************************
849 ****************************************************************************/
850
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200851static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300852{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200853 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300854
855 if (!ibm || !ibm->read)
856 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200857 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300858}
859
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200860static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300861{
Al Virod9dda782013-03-31 18:16:14 -0400862 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200863}
864
865static ssize_t dispatch_proc_write(struct file *file,
866 const char __user *userbuf,
867 size_t count, loff_t *pos)
868{
Al Virod9dda782013-03-31 18:16:14 -0400869 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300870 char *kernbuf;
871 int ret;
872
873 if (!ibm || !ibm->write)
874 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300875 if (count > PAGE_SIZE - 2)
876 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300877
878 kernbuf = kmalloc(count + 2, GFP_KERNEL);
879 if (!kernbuf)
880 return -ENOMEM;
881
882 if (copy_from_user(kernbuf, userbuf, count)) {
883 kfree(kernbuf);
884 return -EFAULT;
885 }
886
887 kernbuf[count] = 0;
888 strcat(kernbuf, ",");
889 ret = ibm->write(kernbuf);
890 if (ret == 0)
891 ret = count;
892
893 kfree(kernbuf);
894
895 return ret;
896}
897
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200898static const struct file_operations dispatch_proc_fops = {
899 .owner = THIS_MODULE,
900 .open = dispatch_proc_open,
901 .read = seq_read,
902 .llseek = seq_lseek,
903 .release = single_release,
904 .write = dispatch_proc_write,
905};
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static char *next_cmd(char **cmds)
908{
909 char *start = *cmds;
910 char *end;
911
912 while ((end = strchr(start, ',')) && end == start)
913 start = end + 1;
914
915 if (!end)
916 return NULL;
917
918 *end = 0;
919 *cmds = end + 1;
920 return start;
921}
922
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300923
924/****************************************************************************
925 ****************************************************************************
926 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300927 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300928 *
929 ****************************************************************************
930 ****************************************************************************/
931
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300932static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300933static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700934static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300935static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300936static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200937static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300938
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200939#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200940static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200941{
942 struct ibm_struct *ibm, *itmp;
943
944 list_for_each_entry_safe(ibm, itmp,
945 &tpacpi_all_drivers,
946 all_drivers) {
947 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200948 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200949 }
950
951 return 0;
952}
953
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200954static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300955{
956 struct ibm_struct *ibm, *itmp;
957
958 list_for_each_entry_safe(ibm, itmp,
959 &tpacpi_all_drivers,
960 all_drivers) {
961 if (ibm->resume)
962 (ibm->resume)();
963 }
964
965 return 0;
966}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200967#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300968
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200969static SIMPLE_DEV_PM_OPS(tpacpi_pm,
970 tpacpi_suspend_handler, tpacpi_resume_handler);
971
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200972static void tpacpi_shutdown_handler(struct platform_device *pdev)
973{
974 struct ibm_struct *ibm, *itmp;
975
976 list_for_each_entry_safe(ibm, itmp,
977 &tpacpi_all_drivers,
978 all_drivers) {
979 if (ibm->shutdown)
980 (ibm->shutdown)();
981 }
982}
983
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300984static struct platform_driver tpacpi_pdriver = {
985 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200986 .name = TPACPI_DRVR_NAME,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200987 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300988 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200989 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300990};
991
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300992static struct platform_driver tpacpi_hwmon_pdriver = {
993 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200994 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300995 },
996};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300997
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300998/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300999 * sysfs support helpers
1000 */
1001
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001002struct attribute_set {
1003 unsigned int members, max_members;
1004 struct attribute_group group;
1005};
1006
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001007struct attribute_set_obj {
1008 struct attribute_set s;
1009 struct attribute *a;
1010} __attribute__((packed));
1011
1012static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001013 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001014{
1015 struct attribute_set_obj *sobj;
1016
1017 if (max_members == 0)
1018 return NULL;
1019
1020 /* Allocates space for implicit NULL at the end too */
1021 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1022 max_members * sizeof(struct attribute *),
1023 GFP_KERNEL);
1024 if (!sobj)
1025 return NULL;
1026 sobj->s.max_members = max_members;
1027 sobj->s.group.attrs = &sobj->a;
1028 sobj->s.group.name = name;
1029
1030 return &sobj->s;
1031}
1032
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001033#define destroy_attr_set(_set) \
1034 kfree(_set);
1035
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001036/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001037static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001038{
1039 if (!s || !attr)
1040 return -EINVAL;
1041
1042 if (s->members >= s->max_members)
1043 return -ENOMEM;
1044
1045 s->group.attrs[s->members] = attr;
1046 s->members++;
1047
1048 return 0;
1049}
1050
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001051static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001052 struct attribute **attr,
1053 unsigned int count)
1054{
1055 int i, res;
1056
1057 for (i = 0; i < count; i++) {
1058 res = add_to_attr_set(s, attr[i]);
1059 if (res)
1060 return res;
1061 }
1062
1063 return 0;
1064}
1065
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001066static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001067{
1068 sysfs_remove_group(kobj, &s->group);
1069 destroy_attr_set(s);
1070}
1071
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001072#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1073 sysfs_create_group(_kobj, &_attr_set->group)
1074
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001075static int parse_strtoul(const char *buf,
1076 unsigned long max, unsigned long *value)
1077{
1078 char *endp;
1079
André Goddard Rosae7d28602009-12-14 18:01:06 -08001080 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1081 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001082 if (*endp || *value > max)
1083 return -EINVAL;
1084
1085 return 0;
1086}
1087
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001088static void tpacpi_disable_brightness_delay(void)
1089{
1090 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001091 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001092}
1093
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001094static void printk_deprecated_attribute(const char * const what,
1095 const char * const details)
1096{
1097 tpacpi_log_usertask("deprecated sysfs attribute");
Joe Perches0978e012011-04-04 10:06:25 -07001098 pr_warn("WARNING: sysfs attribute %s is deprecated and "
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001099 "will be removed. %s\n",
1100 what, details);
1101}
1102
Johannes Berg19d337d2009-06-02 13:01:37 +02001103/*************************************************************************
1104 * rfkill and radio control support helpers
1105 */
1106
1107/*
1108 * ThinkPad-ACPI firmware handling model:
1109 *
1110 * WLSW (master wireless switch) is event-driven, and is common to all
1111 * firmware-controlled radios. It cannot be controlled, just monitored,
1112 * as expected. It overrides all radio state in firmware
1113 *
1114 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1115 * (TODO: verify how WLSW interacts with the returned radio state).
1116 *
1117 * The only time there are shadow radio state changes, is when
1118 * masked-off hotkeys are used.
1119 */
1120
1121/*
1122 * Internal driver API for radio state:
1123 *
1124 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1125 * bool: true means radio blocked (off)
1126 */
1127enum tpacpi_rfkill_state {
1128 TPACPI_RFK_RADIO_OFF = 0,
1129 TPACPI_RFK_RADIO_ON
1130};
1131
1132/* rfkill switches */
1133enum tpacpi_rfk_id {
1134 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1135 TPACPI_RFK_WWAN_SW_ID,
1136 TPACPI_RFK_UWB_SW_ID,
1137 TPACPI_RFK_SW_MAX
1138};
1139
1140static const char *tpacpi_rfkill_names[] = {
1141 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1142 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1143 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1144 [TPACPI_RFK_SW_MAX] = NULL
1145};
1146
1147/* ThinkPad-ACPI rfkill subdriver */
1148struct tpacpi_rfk {
1149 struct rfkill *rfkill;
1150 enum tpacpi_rfk_id id;
1151 const struct tpacpi_rfk_ops *ops;
1152};
1153
1154struct tpacpi_rfk_ops {
1155 /* firmware interface */
1156 int (*get_status)(void);
1157 int (*set_status)(const enum tpacpi_rfkill_state);
1158};
1159
1160static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1161
1162/* Query FW and update rfkill sw state for a given rfkill switch */
1163static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1164{
1165 int status;
1166
1167 if (!tp_rfk)
1168 return -ENODEV;
1169
1170 status = (tp_rfk->ops->get_status)();
1171 if (status < 0)
1172 return status;
1173
1174 rfkill_set_sw_state(tp_rfk->rfkill,
1175 (status == TPACPI_RFK_RADIO_OFF));
1176
1177 return status;
1178}
1179
1180/* Query FW and update rfkill sw state for all rfkill switches */
1181static void tpacpi_rfk_update_swstate_all(void)
1182{
1183 unsigned int i;
1184
1185 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1186 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1187}
1188
1189/*
1190 * Sync the HW-blocking state of all rfkill switches,
1191 * do notice it causes the rfkill core to schedule uevents
1192 */
1193static void tpacpi_rfk_update_hwblock_state(bool blocked)
1194{
1195 unsigned int i;
1196 struct tpacpi_rfk *tp_rfk;
1197
1198 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1199 tp_rfk = tpacpi_rfkill_switches[i];
1200 if (tp_rfk) {
1201 if (rfkill_set_hw_state(tp_rfk->rfkill,
1202 blocked)) {
1203 /* ignore -- we track sw block */
1204 }
1205 }
1206 }
1207}
1208
1209/* Call to get the WLSW state from the firmware */
1210static int hotkey_get_wlsw(void);
1211
1212/* Call to query WLSW state and update all rfkill switches */
1213static bool tpacpi_rfk_check_hwblock_state(void)
1214{
1215 int res = hotkey_get_wlsw();
1216 int hw_blocked;
1217
1218 /* When unknown or unsupported, we have to assume it is unblocked */
1219 if (res < 0)
1220 return false;
1221
1222 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1223 tpacpi_rfk_update_hwblock_state(hw_blocked);
1224
1225 return hw_blocked;
1226}
1227
1228static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1229{
1230 struct tpacpi_rfk *tp_rfk = data;
1231 int res;
1232
1233 dbg_printk(TPACPI_DBG_RFKILL,
1234 "request to change radio state to %s\n",
1235 blocked ? "blocked" : "unblocked");
1236
1237 /* try to set radio state */
1238 res = (tp_rfk->ops->set_status)(blocked ?
1239 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1240
1241 /* and update the rfkill core with whatever the FW really did */
1242 tpacpi_rfk_update_swstate(tp_rfk);
1243
1244 return (res < 0) ? res : 0;
1245}
1246
1247static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1248 .set_block = tpacpi_rfk_hook_set_block,
1249};
1250
1251static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1252 const struct tpacpi_rfk_ops *tp_rfkops,
1253 const enum rfkill_type rfktype,
1254 const char *name,
1255 const bool set_default)
1256{
1257 struct tpacpi_rfk *atp_rfk;
1258 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001259 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001260 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001261 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001262
1263 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1264
Johannes Berg19d337d2009-06-02 13:01:37 +02001265 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1266 if (atp_rfk)
1267 atp_rfk->rfkill = rfkill_alloc(name,
1268 &tpacpi_pdev->dev,
1269 rfktype,
1270 &tpacpi_rfk_rfkill_ops,
1271 atp_rfk);
1272 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001273 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001274 kfree(atp_rfk);
1275 return -ENOMEM;
1276 }
1277
1278 atp_rfk->id = id;
1279 atp_rfk->ops = tp_rfkops;
1280
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001281 sw_status = (tp_rfkops->get_status)();
1282 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001283 pr_err("failed to read initial state for %s, error %d\n",
1284 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001285 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001286 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001287 if (set_default) {
1288 /* try to keep the initial state, since we ask the
1289 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001290 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001291 }
1292 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001293 hw_state = tpacpi_rfk_check_hwblock_state();
1294 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001295
1296 res = rfkill_register(atp_rfk->rfkill);
1297 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001298 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001299 rfkill_destroy(atp_rfk->rfkill);
1300 kfree(atp_rfk);
1301 return res;
1302 }
1303
1304 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001305
Joe Perches0978e012011-04-04 10:06:25 -07001306 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001307 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001308 return 0;
1309}
1310
1311static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1312{
1313 struct tpacpi_rfk *tp_rfk;
1314
1315 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1316
1317 tp_rfk = tpacpi_rfkill_switches[id];
1318 if (tp_rfk) {
1319 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001320 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001321 tpacpi_rfkill_switches[id] = NULL;
1322 kfree(tp_rfk);
1323 }
1324}
1325
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001326static void printk_deprecated_rfkill_attribute(const char * const what)
1327{
1328 printk_deprecated_attribute(what,
1329 "Please switch to generic rfkill before year 2010");
1330}
1331
Johannes Berg19d337d2009-06-02 13:01:37 +02001332/* sysfs <radio> enable ------------------------------------------------ */
1333static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1334 struct device_attribute *attr,
1335 char *buf)
1336{
1337 int status;
1338
1339 printk_deprecated_rfkill_attribute(attr->attr.name);
1340
1341 /* This is in the ABI... */
1342 if (tpacpi_rfk_check_hwblock_state()) {
1343 status = TPACPI_RFK_RADIO_OFF;
1344 } else {
1345 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1346 if (status < 0)
1347 return status;
1348 }
1349
1350 return snprintf(buf, PAGE_SIZE, "%d\n",
1351 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1352}
1353
1354static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1355 struct device_attribute *attr,
1356 const char *buf, size_t count)
1357{
1358 unsigned long t;
1359 int res;
1360
1361 printk_deprecated_rfkill_attribute(attr->attr.name);
1362
1363 if (parse_strtoul(buf, 1, &t))
1364 return -EINVAL;
1365
1366 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1367
1368 /* This is in the ABI... */
1369 if (tpacpi_rfk_check_hwblock_state() && !!t)
1370 return -EPERM;
1371
1372 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1373 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1374 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1375
1376 return (res < 0) ? res : count;
1377}
1378
1379/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001380static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001381{
Johannes Berg19d337d2009-06-02 13:01:37 +02001382 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001383 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001384 else {
1385 int status;
1386
1387 /* This is in the ABI... */
1388 if (tpacpi_rfk_check_hwblock_state()) {
1389 status = TPACPI_RFK_RADIO_OFF;
1390 } else {
1391 status = tpacpi_rfk_update_swstate(
1392 tpacpi_rfkill_switches[id]);
1393 if (status < 0)
1394 return status;
1395 }
1396
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001397 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001398 (status == TPACPI_RFK_RADIO_ON) ?
1399 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001400 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001401 }
1402
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001403 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001404}
1405
1406static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1407{
1408 char *cmd;
1409 int status = -1;
1410 int res = 0;
1411
1412 if (id >= TPACPI_RFK_SW_MAX)
1413 return -ENODEV;
1414
1415 while ((cmd = next_cmd(&buf))) {
1416 if (strlencmp(cmd, "enable") == 0)
1417 status = TPACPI_RFK_RADIO_ON;
1418 else if (strlencmp(cmd, "disable") == 0)
1419 status = TPACPI_RFK_RADIO_OFF;
1420 else
1421 return -EINVAL;
1422 }
1423
1424 if (status != -1) {
1425 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1426 (status == TPACPI_RFK_RADIO_ON) ?
1427 "enable" : "disable",
1428 tpacpi_rfkill_names[id]);
1429 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1430 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1431 }
1432
1433 return res;
1434}
1435
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001436/*************************************************************************
1437 * thinkpad-acpi driver attributes
1438 */
1439
1440/* interface_version --------------------------------------------------- */
1441static ssize_t tpacpi_driver_interface_version_show(
1442 struct device_driver *drv,
1443 char *buf)
1444{
1445 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1446}
1447
1448static DRIVER_ATTR(interface_version, S_IRUGO,
1449 tpacpi_driver_interface_version_show, NULL);
1450
1451/* debug_level --------------------------------------------------------- */
1452static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1453 char *buf)
1454{
1455 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1456}
1457
1458static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1459 const char *buf, size_t count)
1460{
1461 unsigned long t;
1462
1463 if (parse_strtoul(buf, 0xffff, &t))
1464 return -EINVAL;
1465
1466 dbg_level = t;
1467
1468 return count;
1469}
1470
1471static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1472 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1473
1474/* version ------------------------------------------------------------- */
1475static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1476 char *buf)
1477{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001478 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1479 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001480}
1481
1482static DRIVER_ATTR(version, S_IRUGO,
1483 tpacpi_driver_version_show, NULL);
1484
1485/* --------------------------------------------------------------------- */
1486
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001487#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1488
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001489/* wlsw_emulstate ------------------------------------------------------ */
1490static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1491 char *buf)
1492{
1493 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1494}
1495
1496static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1497 const char *buf, size_t count)
1498{
1499 unsigned long t;
1500
1501 if (parse_strtoul(buf, 1, &t))
1502 return -EINVAL;
1503
Johannes Berg19d337d2009-06-02 13:01:37 +02001504 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001505 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001506 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1507 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001508
1509 return count;
1510}
1511
1512static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1513 tpacpi_driver_wlsw_emulstate_show,
1514 tpacpi_driver_wlsw_emulstate_store);
1515
1516/* bluetooth_emulstate ------------------------------------------------- */
1517static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1518 struct device_driver *drv,
1519 char *buf)
1520{
1521 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1522}
1523
1524static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1525 struct device_driver *drv,
1526 const char *buf, size_t count)
1527{
1528 unsigned long t;
1529
1530 if (parse_strtoul(buf, 1, &t))
1531 return -EINVAL;
1532
1533 tpacpi_bluetooth_emulstate = !!t;
1534
1535 return count;
1536}
1537
1538static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1539 tpacpi_driver_bluetooth_emulstate_show,
1540 tpacpi_driver_bluetooth_emulstate_store);
1541
1542/* wwan_emulstate ------------------------------------------------- */
1543static ssize_t tpacpi_driver_wwan_emulstate_show(
1544 struct device_driver *drv,
1545 char *buf)
1546{
1547 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1548}
1549
1550static ssize_t tpacpi_driver_wwan_emulstate_store(
1551 struct device_driver *drv,
1552 const char *buf, size_t count)
1553{
1554 unsigned long t;
1555
1556 if (parse_strtoul(buf, 1, &t))
1557 return -EINVAL;
1558
1559 tpacpi_wwan_emulstate = !!t;
1560
1561 return count;
1562}
1563
1564static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1565 tpacpi_driver_wwan_emulstate_show,
1566 tpacpi_driver_wwan_emulstate_store);
1567
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001568/* uwb_emulstate ------------------------------------------------- */
1569static ssize_t tpacpi_driver_uwb_emulstate_show(
1570 struct device_driver *drv,
1571 char *buf)
1572{
1573 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1574}
1575
1576static ssize_t tpacpi_driver_uwb_emulstate_store(
1577 struct device_driver *drv,
1578 const char *buf, size_t count)
1579{
1580 unsigned long t;
1581
1582 if (parse_strtoul(buf, 1, &t))
1583 return -EINVAL;
1584
1585 tpacpi_uwb_emulstate = !!t;
1586
1587 return count;
1588}
1589
1590static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1591 tpacpi_driver_uwb_emulstate_show,
1592 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001593#endif
1594
1595/* --------------------------------------------------------------------- */
1596
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001597static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001598 &driver_attr_debug_level, &driver_attr_version,
1599 &driver_attr_interface_version,
1600};
1601
1602static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1603{
1604 int i, res;
1605
1606 i = 0;
1607 res = 0;
1608 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1609 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1610 i++;
1611 }
1612
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001613#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1614 if (!res && dbg_wlswemul)
1615 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1616 if (!res && dbg_bluetoothemul)
1617 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1618 if (!res && dbg_wwanemul)
1619 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001620 if (!res && dbg_uwbemul)
1621 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001622#endif
1623
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001624 return res;
1625}
1626
1627static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1628{
1629 int i;
1630
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001631 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001632 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001633
1634#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1635 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1636 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1637 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001638 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001639#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001640}
1641
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001642/*************************************************************************
1643 * Firmware Data
1644 */
1645
1646/*
1647 * Table of recommended minimum BIOS versions
1648 *
1649 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001650 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001651 * bugs and bad ACPI behaviour on older versions
1652 *
1653 * 2. BIOS or EC fw with known bugs that trigger on Linux
1654 *
1655 * 3. BIOS with known reduced functionality in older versions
1656 *
1657 * We recommend the latest BIOS and EC version.
1658 * We only support the latest BIOS and EC fw version as a rule.
1659 *
1660 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1661 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001662 *
1663 * WARNING: we use this table also to detect that the machine is
1664 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001665 */
1666
1667#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1668 { .vendor = (__v), \
1669 .bios = TPID(__id1, __id2), \
1670 .ec = TPACPI_MATCH_ANY, \
1671 .quirks = TPACPI_MATCH_ANY << 16 \
1672 | (__bv1) << 8 | (__bv2) }
1673
1674#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001675 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001676 { .vendor = (__v), \
1677 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001678 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001679 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1680 | (__bv1) << 8 | (__bv2) }
1681
1682#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1683 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1684
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001685/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001686#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001688 __bv1, __bv2, TPID(__id1, __id2), \
1689 __ev1, __ev2), \
1690 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1691 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1692 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001693
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001694/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001695#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1696 __eid1, __eid2, __ev1, __ev2) \
1697 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001698 __bv1, __bv2, TPID(__eid1, __eid2), \
1699 __ev1, __ev2), \
1700 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1701 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1702 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001703
1704#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1705 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1706
1707#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1708 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001709 __bv1, __bv2, TPID(__id1, __id2), \
1710 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001711
1712#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1713 __eid1, __eid2, __ev1, __ev2) \
1714 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001715 __bv1, __bv2, TPID(__eid1, __eid2), \
1716 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001717
1718static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1719 /* Numeric models ------------------ */
1720 /* FW MODEL BIOS VERS */
1721 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1722 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1723 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1724 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1725 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1726 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1727 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1728 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1729 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1730
1731 /* A-series ------------------------- */
1732 /* FW MODEL BIOS VERS EC VERS */
1733 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1734 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1735 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1736 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1737 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1738 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1739 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1740 TPV_QI0('1', '3', '2', '0'), /* A22m */
1741 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1742 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1743 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1744
1745 /* G-series ------------------------- */
1746 /* FW MODEL BIOS VERS */
1747 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1748 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1749
1750 /* R-series, T-series --------------- */
1751 /* FW MODEL BIOS VERS EC VERS */
1752 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1753 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1754 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1755 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1756 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1757 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1758 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1759 T40/p, T41/p, T42/p (1) */
1760 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1761 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1762 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1763 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1764
1765 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1766 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1767 TPV_QI0('1', '6', '3', '2'), /* T22 */
1768 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1769 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1770 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1771
1772 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1773 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001774 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001775
1776 /* BIOS FW BIOS VERS EC FW EC VERS */
1777 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1778 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1779
1780 /* X-series ------------------------- */
1781 /* FW MODEL BIOS VERS EC VERS */
1782 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1783 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1784 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1785 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1786 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1787 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1788 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1789
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001790 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1791 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001792
1793 /* (0) - older versions lack DMI EC fw string and functionality */
1794 /* (1) - older versions known to lack functionality */
1795};
1796
1797#undef TPV_QL1
1798#undef TPV_QL0
1799#undef TPV_QI2
1800#undef TPV_QI1
1801#undef TPV_QI0
1802#undef TPV_Q_X
1803#undef TPV_Q
1804
1805static void __init tpacpi_check_outdated_fw(void)
1806{
1807 unsigned long fwvers;
1808 u16 ec_version, bios_version;
1809
1810 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1811 ARRAY_SIZE(tpacpi_bios_version_qtable));
1812
1813 if (!fwvers)
1814 return;
1815
1816 bios_version = fwvers & 0xffffU;
1817 ec_version = (fwvers >> 16) & 0xffffU;
1818
1819 /* note that unknown versions are set to 0x0000 and we use that */
1820 if ((bios_version > thinkpad_id.bios_release) ||
1821 (ec_version > thinkpad_id.ec_release &&
1822 ec_version != TPACPI_MATCH_ANY)) {
1823 /*
1824 * The changelogs would let us track down the exact
1825 * reason, but it is just too much of a pain to track
1826 * it. We only list BIOSes that are either really
1827 * broken, or really stable to begin with, so it is
1828 * best if the user upgrades the firmware anyway.
1829 */
Joe Perches0978e012011-04-04 10:06:25 -07001830 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1831 pr_warn("WARNING: This firmware may be missing critical bug "
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001832 "fixes and/or important features\n");
1833 }
1834}
1835
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001836static bool __init tpacpi_is_fw_known(void)
1837{
1838 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1839 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1840}
1841
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001842/****************************************************************************
1843 ****************************************************************************
1844 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001845 * Subdrivers
1846 *
1847 ****************************************************************************
1848 ****************************************************************************/
1849
1850/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001851 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001852 */
1853
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001854static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001856 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1857 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001861static struct ibm_struct thinkpad_acpi_driver_data = {
1862 .name = "driver",
1863 .read = thinkpad_acpi_driver_read,
1864};
1865
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001866/*************************************************************************
1867 * Hotkey subdriver
1868 */
1869
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001870/*
1871 * ThinkPad firmware event model
1872 *
1873 * The ThinkPad firmware has two main event interfaces: normal ACPI
1874 * notifications (which follow the ACPI standard), and a private event
1875 * interface.
1876 *
1877 * The private event interface also issues events for the hotkeys. As
1878 * the driver gained features, the event handling code ended up being
1879 * built around the hotkey subdriver. This will need to be refactored
1880 * to a more formal event API eventually.
1881 *
1882 * Some "hotkeys" are actually supposed to be used as event reports,
1883 * such as "brightness has changed", "volume has changed", depending on
1884 * the ThinkPad model and how the firmware is operating.
1885 *
1886 * Unlike other classes, hotkey-class events have mask/unmask control on
1887 * non-ancient firmware. However, how it behaves changes a lot with the
1888 * firmware model and version.
1889 */
1890
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001891enum { /* hot key scan codes (derived from ACPI DSDT) */
1892 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1893 TP_ACPI_HOTKEYSCAN_FNF2,
1894 TP_ACPI_HOTKEYSCAN_FNF3,
1895 TP_ACPI_HOTKEYSCAN_FNF4,
1896 TP_ACPI_HOTKEYSCAN_FNF5,
1897 TP_ACPI_HOTKEYSCAN_FNF6,
1898 TP_ACPI_HOTKEYSCAN_FNF7,
1899 TP_ACPI_HOTKEYSCAN_FNF8,
1900 TP_ACPI_HOTKEYSCAN_FNF9,
1901 TP_ACPI_HOTKEYSCAN_FNF10,
1902 TP_ACPI_HOTKEYSCAN_FNF11,
1903 TP_ACPI_HOTKEYSCAN_FNF12,
1904 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1905 TP_ACPI_HOTKEYSCAN_FNINSERT,
1906 TP_ACPI_HOTKEYSCAN_FNDELETE,
1907 TP_ACPI_HOTKEYSCAN_FNHOME,
1908 TP_ACPI_HOTKEYSCAN_FNEND,
1909 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1910 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1911 TP_ACPI_HOTKEYSCAN_FNSPACE,
1912 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1913 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1914 TP_ACPI_HOTKEYSCAN_MUTE,
1915 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001916 TP_ACPI_HOTKEYSCAN_UNK1,
1917 TP_ACPI_HOTKEYSCAN_UNK2,
1918 TP_ACPI_HOTKEYSCAN_UNK3,
1919 TP_ACPI_HOTKEYSCAN_UNK4,
1920 TP_ACPI_HOTKEYSCAN_UNK5,
1921 TP_ACPI_HOTKEYSCAN_UNK6,
1922 TP_ACPI_HOTKEYSCAN_UNK7,
1923 TP_ACPI_HOTKEYSCAN_UNK8,
1924
Bastien Nocera6a68d852015-03-02 14:45:31 +01001925 TP_ACPI_HOTKEYSCAN_MUTE2,
1926 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1927 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1928 TP_ACPI_HOTKEYSCAN_CLOUD,
1929 TP_ACPI_HOTKEYSCAN_UNK9,
1930 TP_ACPI_HOTKEYSCAN_VOICE,
1931 TP_ACPI_HOTKEYSCAN_UNK10,
1932 TP_ACPI_HOTKEYSCAN_GESTURES,
1933 TP_ACPI_HOTKEYSCAN_UNK11,
1934 TP_ACPI_HOTKEYSCAN_UNK12,
1935 TP_ACPI_HOTKEYSCAN_UNK13,
1936 TP_ACPI_HOTKEYSCAN_CONFIG,
1937 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1938 TP_ACPI_HOTKEYSCAN_RELOAD,
1939 TP_ACPI_HOTKEYSCAN_BACK,
1940 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1941 TP_ACPI_HOTKEYSCAN_MIC_UP,
1942 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1943 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1944 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1945
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001946 /* Hotkey keymap size */
1947 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001948};
1949
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001950enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001951 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1952 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1953};
1954
1955enum { /* Positions of some of the keys in hotkey masks */
1956 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1957 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1958 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1959 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1960 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
Hans de Goedec685e202017-02-09 16:44:13 +01001961 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001962 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1963 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1964 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1965 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1966 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1967};
1968
1969enum { /* NVRAM to ACPI HKEY group map */
1970 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1971 TP_ACPI_HKEY_ZOOM_MASK |
1972 TP_ACPI_HKEY_DISPSWTCH_MASK |
1973 TP_ACPI_HKEY_HIBERNATE_MASK,
1974 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1975 TP_ACPI_HKEY_BRGHTDWN_MASK,
1976 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1977 TP_ACPI_HKEY_VOLDWN_MASK |
1978 TP_ACPI_HKEY_MUTE_MASK,
1979};
1980
1981#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1982struct tp_nvram_state {
1983 u16 thinkpad_toggle:1;
1984 u16 zoom_toggle:1;
1985 u16 display_toggle:1;
1986 u16 thinklight_toggle:1;
1987 u16 hibernate_toggle:1;
1988 u16 displayexp_toggle:1;
1989 u16 display_state:1;
1990 u16 brightness_toggle:1;
1991 u16 volume_toggle:1;
1992 u16 mute:1;
1993
1994 u8 brightness_level;
1995 u8 volume_level;
1996};
1997
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001998/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001999static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002000
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002001/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002002 * Acquire mutex to write poller control variables as an
2003 * atomic block.
2004 *
2005 * Increment hotkey_config_change when changing them if you
2006 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002007 *
2008 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2009 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002010static struct mutex hotkey_thread_data_mutex;
2011static unsigned int hotkey_config_change;
2012
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002013/*
2014 * hotkey poller control variables
2015 *
2016 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002017 *
2018 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2019 * should be used only when the changes need to be taken as
2020 * a block, OR when one needs to force the kthread to forget
2021 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002022 */
2023static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2024static unsigned int hotkey_poll_freq = 10; /* Hz */
2025
2026#define HOTKEY_CONFIG_CRITICAL_START \
2027 do { \
2028 mutex_lock(&hotkey_thread_data_mutex); \
2029 hotkey_config_change++; \
2030 } while (0);
2031#define HOTKEY_CONFIG_CRITICAL_END \
2032 mutex_unlock(&hotkey_thread_data_mutex);
2033
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002034#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2035
2036#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002037#define HOTKEY_CONFIG_CRITICAL_START
2038#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002039
2040#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2041
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002042static struct mutex hotkey_mutex;
2043
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002044static enum { /* Reasons for waking up */
2045 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2046 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2047 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2048} hotkey_wakeup_reason;
2049
2050static int hotkey_autosleep_ack;
2051
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002052static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2053static u32 hotkey_all_mask; /* all events supported in fw */
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002054static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002055static u32 hotkey_reserved_mask; /* events better left disabled */
2056static u32 hotkey_driver_mask; /* events needed by the driver */
2057static u32 hotkey_user_mask; /* events visible to userspace */
2058static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002059
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002060static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002061
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002062static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002063
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002064static void tpacpi_driver_event(const unsigned int hkey_event);
2065static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002066static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002067
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002068/* HKEY.MHKG() return bits */
2069#define TP_HOTKEY_TABLET_MASK (1 << 3)
Lyudeb03f4d42016-11-11 15:15:03 -05002070/* ThinkPad X1 Yoga (2016) */
2071#define TP_EC_CMMD_TABLET_MODE 0x6
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002072
Johannes Berg19d337d2009-06-02 13:01:37 +02002073static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002074{
Johannes Berg19d337d2009-06-02 13:01:37 +02002075 int status;
2076
2077 if (!tp_features.hotkey_wlsw)
2078 return -ENODEV;
2079
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002080#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002081 if (dbg_wlswemul)
2082 return (tpacpi_wlsw_emulstate) ?
2083 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002084#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002085
2086 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002087 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002088
2089 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002090}
2091
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002092static int hotkey_get_tablet_mode(int *status)
2093{
2094 int s;
2095
Lyudeb03f4d42016-11-11 15:15:03 -05002096 switch (tp_features.hotkey_tablet) {
2097 case TP_HOTKEY_TABLET_USES_MHKG:
2098 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2099 return -EIO;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002100
Lyudeb03f4d42016-11-11 15:15:03 -05002101 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2102 break;
2103 case TP_HOTKEY_TABLET_USES_CMMD:
2104 if (!acpi_evalf(ec_handle, &s, "CMMD", "d"))
2105 return -EIO;
2106
2107 *status = (s == TP_EC_CMMD_TABLET_MODE);
2108 break;
2109 default:
2110 break;
2111 }
2112
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002113 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002114}
2115
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002116/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002117 * Reads current event mask from firmware, and updates
2118 * hotkey_acpi_mask accordingly. Also resets any bits
2119 * from hotkey_user_mask that are unavailable to be
2120 * delivered (shadow requirement of the userspace ABI).
2121 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002122 * Call with hotkey_mutex held
2123 */
2124static int hotkey_mask_get(void)
2125{
2126 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002127 u32 m = 0;
2128
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002129 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002130 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002131
2132 hotkey_acpi_mask = m;
2133 } else {
2134 /* no mask support doesn't mean no event support... */
2135 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002136 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002137
2138 /* sync userspace-visible mask */
2139 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002140
2141 return 0;
2142}
2143
Jean Delvaredf6dd1b2015-04-27 09:45:06 +02002144static void hotkey_mask_warn_incomplete_mask(void)
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002145{
2146 /* log only what the user can fix... */
2147 const u32 wantedmask = hotkey_driver_mask &
2148 ~(hotkey_acpi_mask | hotkey_source_mask) &
2149 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2150
2151 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002152 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002153}
2154
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002155/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002156 * Set the firmware mask when supported
2157 *
2158 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2159 *
2160 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2161 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002162 * Call with hotkey_mutex held
2163 */
2164static int hotkey_mask_set(u32 mask)
2165{
2166 int i;
2167 int rc = 0;
2168
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002169 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002170
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002171 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002172 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002173 if (!acpi_evalf(hkey_handle,
2174 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002175 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002176 rc = -EIO;
2177 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002178 }
2179 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002180 }
2181
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002182 /*
2183 * We *must* make an inconditional call to hotkey_mask_get to
2184 * refresh hotkey_acpi_mask and update hotkey_user_mask
2185 *
2186 * Take the opportunity to also log when we cannot _enable_
2187 * a given event.
2188 */
2189 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Joe Perches0978e012011-04-04 10:06:25 -07002190 pr_notice("asked for hotkey mask 0x%08x, but "
2191 "firmware forced it to 0x%08x\n",
2192 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002193 }
2194
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002195 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2196 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002197
2198 return rc;
2199}
2200
2201/*
2202 * Sets hotkey_user_mask and tries to set the firmware mask
2203 *
2204 * Call with hotkey_mutex held
2205 */
2206static int hotkey_user_mask_set(const u32 mask)
2207{
2208 int rc;
2209
2210 /* Give people a chance to notice they are doing something that
2211 * is bound to go boom on their users sooner or later */
2212 if (!tp_warned.hotkey_mask_ff &&
2213 (mask == 0xffff || mask == 0xffffff ||
2214 mask == 0xffffffff)) {
2215 tp_warned.hotkey_mask_ff = 1;
Joe Perches0978e012011-04-04 10:06:25 -07002216 pr_notice("setting the hotkey mask to 0x%08x is likely "
2217 "not the best way to go about it\n", mask);
2218 pr_notice("please consider using the driver defaults, "
2219 "and refer to up-to-date thinkpad-acpi "
2220 "documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002221 }
2222
2223 /* Try to enable what the user asked for, plus whatever we need.
2224 * this syncs everything but won't enable bits in hotkey_user_mask */
2225 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2226
2227 /* Enable the available bits in hotkey_user_mask */
2228 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2229
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002230 return rc;
2231}
2232
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002233/*
2234 * Sets the driver hotkey mask.
2235 *
2236 * Can be called even if the hotkey subdriver is inactive
2237 */
2238static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2239{
2240 int rc;
2241
2242 /* Do the right thing if hotkey_init has not been called yet */
2243 if (!tp_features.hotkey) {
2244 hotkey_driver_mask = mask;
2245 return 0;
2246 }
2247
2248 mutex_lock(&hotkey_mutex);
2249
2250 HOTKEY_CONFIG_CRITICAL_START
2251 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002252#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002253 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002254#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002255 HOTKEY_CONFIG_CRITICAL_END
2256
2257 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2258 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002259 hotkey_poll_setup(true);
2260
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002261 mutex_unlock(&hotkey_mutex);
2262
2263 return rc;
2264}
2265
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002266static int hotkey_status_get(int *status)
2267{
2268 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2269 return -EIO;
2270
2271 return 0;
2272}
2273
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002274static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002275{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002276 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002277 return -EIO;
2278
2279 return 0;
2280}
2281
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002282static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002283{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002284 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002285
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002286 if (tp_features.hotkey_tablet &&
2287 !hotkey_get_tablet_mode(&state)) {
2288 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002289
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002290 input_report_switch(tpacpi_inputdev,
2291 SW_TABLET_MODE, !!state);
2292 input_sync(tpacpi_inputdev);
2293
2294 mutex_unlock(&tpacpi_inputdev_send_mutex);
2295 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002296}
2297
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002298/* Do NOT call without validating scancode first */
2299static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002300{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002301 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002302
2303 if (keycode != KEY_RESERVED) {
2304 mutex_lock(&tpacpi_inputdev_send_mutex);
2305
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002306 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002307 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002308 input_sync(tpacpi_inputdev);
2309
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002310 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002311 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002312 input_sync(tpacpi_inputdev);
2313
2314 mutex_unlock(&tpacpi_inputdev_send_mutex);
2315 }
2316}
2317
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002318/* Do NOT call without validating scancode first */
2319static void tpacpi_input_send_key_masked(const unsigned int scancode)
2320{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002321 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002322 if (hotkey_user_mask & (1 << scancode))
2323 tpacpi_input_send_key(scancode);
2324}
2325
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002326#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2327static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2328
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002329/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002330static void tpacpi_hotkey_send_key(unsigned int scancode)
2331{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002332 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002333}
2334
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002335static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002336{
2337 u8 d;
2338
2339 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2340 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2341 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2342 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2343 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2344 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2345 }
Hans de Goedec685e202017-02-09 16:44:13 +01002346 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002347 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2348 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2349 }
2350 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2351 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2352 n->displayexp_toggle =
2353 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2354 }
2355 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2356 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2357 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2358 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2359 n->brightness_toggle =
2360 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2361 }
2362 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2363 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2364 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2365 >> TP_NVRAM_POS_LEVEL_VOLUME;
2366 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2367 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2368 }
2369}
2370
Behan Webstera4d44ba2014-02-12 21:58:46 +01002371#define TPACPI_COMPARE_KEY(__scancode, __member) \
2372do { \
2373 if ((event_mask & (1 << __scancode)) && \
2374 oldn->__member != newn->__member) \
2375 tpacpi_hotkey_send_key(__scancode); \
2376} while (0)
2377
2378#define TPACPI_MAY_SEND_KEY(__scancode) \
2379do { \
2380 if (event_mask & (1 << __scancode)) \
2381 tpacpi_hotkey_send_key(__scancode); \
2382} while (0)
2383
2384static void issue_volchange(const unsigned int oldvol,
2385 const unsigned int newvol,
2386 const u32 event_mask)
2387{
2388 unsigned int i = oldvol;
2389
2390 while (i > newvol) {
2391 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2392 i--;
2393 }
2394 while (i < newvol) {
2395 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2396 i++;
2397 }
2398}
2399
2400static void issue_brightnesschange(const unsigned int oldbrt,
2401 const unsigned int newbrt,
2402 const u32 event_mask)
2403{
2404 unsigned int i = oldbrt;
2405
2406 while (i > newbrt) {
2407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2408 i--;
2409 }
2410 while (i < newbrt) {
2411 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2412 i++;
2413 }
2414}
2415
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002416static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2417 struct tp_nvram_state *newn,
2418 const u32 event_mask)
2419{
2420
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002421 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2422 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2423 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2424 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2425
2426 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2427
2428 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2429
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002430 /*
2431 * Handle volume
2432 *
2433 * This code is supposed to duplicate the IBM firmware behaviour:
2434 * - Pressing MUTE issues mute hotkey message, even when already mute
2435 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002436 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002437 * - The act of unmuting issues volume up/down notification,
2438 * depending which key was used to unmute
2439 *
2440 * We are constrained to what the NVRAM can tell us, which is not much
2441 * and certainly not enough if more than one volume hotkey was pressed
2442 * since the last poll cycle.
2443 *
2444 * Just to make our life interesting, some newer Lenovo ThinkPads have
2445 * bugs in the BIOS and may fail to update volume_toggle properly.
2446 */
2447 if (newn->mute) {
2448 /* muted */
2449 if (!oldn->mute ||
2450 oldn->volume_toggle != newn->volume_toggle ||
2451 oldn->volume_level != newn->volume_level) {
2452 /* recently muted, or repeated mute keypress, or
2453 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002454 issue_volchange(oldn->volume_level, newn->volume_level,
2455 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002456 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2457 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002458 } else {
2459 /* unmute */
2460 if (oldn->mute) {
2461 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002462 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002463 }
2464 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002465 issue_volchange(oldn->volume_level, newn->volume_level,
2466 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002467 } else if (oldn->volume_toggle != newn->volume_toggle) {
2468 /* repeated vol up/down keypress at end of scale ? */
2469 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002470 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002471 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2472 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002473 }
2474 }
2475
2476 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002477 if (oldn->brightness_level != newn->brightness_level) {
2478 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002479 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002480 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2481 /* repeated key presses that didn't change state */
2482 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002483 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002484 else if (newn->brightness_level >= bright_maxlvl
2485 && !tp_features.bright_unkfw)
2486 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002487 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002488
2489#undef TPACPI_COMPARE_KEY
2490#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002491}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002492
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002493/*
2494 * Polling driver
2495 *
2496 * We track all events in hotkey_source_mask all the time, since
2497 * most of them are edge-based. We only issue those requested by
2498 * hotkey_user_mask or hotkey_driver_mask, though.
2499 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002500static int hotkey_kthread(void *data)
2501{
2502 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002503 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002504 unsigned int si, so;
2505 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002506 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002507 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002508 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002509
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002510 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2511 goto exit;
2512
2513 set_freezable();
2514
2515 so = 0;
2516 si = 1;
2517 t = 0;
2518
2519 /* Initial state for compares */
2520 mutex_lock(&hotkey_thread_data_mutex);
2521 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002522 poll_mask = hotkey_source_mask;
2523 event_mask = hotkey_source_mask &
2524 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002525 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002526 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002527 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002528
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002529 while (!kthread_should_stop()) {
2530 if (t == 0) {
2531 if (likely(poll_freq))
2532 t = 1000/poll_freq;
2533 else
2534 t = 100; /* should never happen... */
2535 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002536 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002537 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002538 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002539
2540 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002541 continue;
2542
2543 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002544 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002545 /* forget old state on thaw or config change */
2546 si = so;
2547 t = 0;
2548 change_detector = hotkey_config_change;
2549 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002550 poll_mask = hotkey_source_mask;
2551 event_mask = hotkey_source_mask &
2552 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002553 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002554 mutex_unlock(&hotkey_thread_data_mutex);
2555
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002556 if (likely(poll_mask)) {
2557 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002558 if (likely(si != so)) {
2559 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002560 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002561 }
2562 }
2563
2564 so = si;
2565 si ^= 1;
2566 }
2567
2568exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002569 return 0;
2570}
2571
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002572/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002573static void hotkey_poll_stop_sync(void)
2574{
2575 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002576 kthread_stop(tpacpi_hotkey_task);
2577 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002578 }
2579}
2580
2581/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002582static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002583{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002584 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2585 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002586
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002587 if (hotkey_poll_freq > 0 &&
2588 (poll_driver_mask ||
2589 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002590 if (!tpacpi_hotkey_task) {
2591 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002592 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002593 if (IS_ERR(tpacpi_hotkey_task)) {
2594 tpacpi_hotkey_task = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07002595 pr_err("could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002596 "for hotkey polling\n");
2597 }
2598 }
2599 } else {
2600 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002601 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002602 hotkey_poll_freq == 0) {
Joe Perches0978e012011-04-04 10:06:25 -07002603 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2604 "require polling, which is currently "
2605 "disabled\n",
2606 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002607 }
2608 }
2609}
2610
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002611static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002612{
2613 mutex_lock(&hotkey_mutex);
2614 hotkey_poll_setup(may_warn);
2615 mutex_unlock(&hotkey_mutex);
2616}
2617
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002618/* call with hotkey_mutex held */
2619static void hotkey_poll_set_freq(unsigned int freq)
2620{
2621 if (!freq)
2622 hotkey_poll_stop_sync();
2623
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002624 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002625}
2626
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002627#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2628
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002629static void hotkey_poll_setup(const bool __unused)
2630{
2631}
2632
2633static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002634{
2635}
2636
2637#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2638
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002639static int hotkey_inputdev_open(struct input_dev *dev)
2640{
2641 switch (tpacpi_lifecycle) {
2642 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002643 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002644 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002645 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002646 case TPACPI_LIFE_EXITING:
2647 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002648 }
2649
2650 /* Should only happen if tpacpi_lifecycle is corrupt */
2651 BUG();
2652 return -EBUSY;
2653}
2654
2655static void hotkey_inputdev_close(struct input_dev *dev)
2656{
2657 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002658 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002659 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002660 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002661}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002662
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002663/* sysfs hotkey enable ------------------------------------------------- */
2664static ssize_t hotkey_enable_show(struct device *dev,
2665 struct device_attribute *attr,
2666 char *buf)
2667{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002668 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002669
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002670 printk_deprecated_attribute("hotkey_enable",
2671 "Hotkey reporting is always enabled");
2672
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002673 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002674 if (res)
2675 return res;
2676
2677 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2678}
2679
2680static ssize_t hotkey_enable_store(struct device *dev,
2681 struct device_attribute *attr,
2682 const char *buf, size_t count)
2683{
2684 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002685
2686 printk_deprecated_attribute("hotkey_enable",
2687 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002688
2689 if (parse_strtoul(buf, 1, &t))
2690 return -EINVAL;
2691
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002692 if (t == 0)
2693 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002694
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002695 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002696}
2697
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002698static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002699
2700/* sysfs hotkey mask --------------------------------------------------- */
2701static ssize_t hotkey_mask_show(struct device *dev,
2702 struct device_attribute *attr,
2703 char *buf)
2704{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002705 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002706}
2707
2708static ssize_t hotkey_mask_store(struct device *dev,
2709 struct device_attribute *attr,
2710 const char *buf, size_t count)
2711{
2712 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002713 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002714
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002715 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002716 return -EINVAL;
2717
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002718 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002719 return -ERESTARTSYS;
2720
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002721 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002722
2723#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002724 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002725#endif
2726
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002727 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002728
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002729 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2730
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002731 return (res) ? res : count;
2732}
2733
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002734static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002735
2736/* sysfs hotkey bios_enabled ------------------------------------------- */
2737static ssize_t hotkey_bios_enabled_show(struct device *dev,
2738 struct device_attribute *attr,
2739 char *buf)
2740{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002741 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002742}
2743
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002744static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002745
2746/* sysfs hotkey bios_mask ---------------------------------------------- */
2747static ssize_t hotkey_bios_mask_show(struct device *dev,
2748 struct device_attribute *attr,
2749 char *buf)
2750{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002751 printk_deprecated_attribute("hotkey_bios_mask",
2752 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002753 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002754}
2755
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002756static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002757
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002758/* sysfs hotkey all_mask ----------------------------------------------- */
2759static ssize_t hotkey_all_mask_show(struct device *dev,
2760 struct device_attribute *attr,
2761 char *buf)
2762{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002763 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2764 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002765}
2766
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002767static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002768
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002769/* sysfs hotkey all_mask ----------------------------------------------- */
2770static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2771 struct device_attribute *attr,
2772 char *buf)
2773{
2774 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2775 hotkey_adaptive_all_mask | hotkey_source_mask);
2776}
2777
2778static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2779
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002780/* sysfs hotkey recommended_mask --------------------------------------- */
2781static ssize_t hotkey_recommended_mask_show(struct device *dev,
2782 struct device_attribute *attr,
2783 char *buf)
2784{
2785 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002786 (hotkey_all_mask | hotkey_source_mask)
2787 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002788}
2789
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002790static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002791
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002792#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2793
2794/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2795static ssize_t hotkey_source_mask_show(struct device *dev,
2796 struct device_attribute *attr,
2797 char *buf)
2798{
2799 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2800}
2801
2802static ssize_t hotkey_source_mask_store(struct device *dev,
2803 struct device_attribute *attr,
2804 const char *buf, size_t count)
2805{
2806 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002807 u32 r_ev;
2808 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002809
2810 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2811 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2812 return -EINVAL;
2813
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002814 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002815 return -ERESTARTSYS;
2816
2817 HOTKEY_CONFIG_CRITICAL_START
2818 hotkey_source_mask = t;
2819 HOTKEY_CONFIG_CRITICAL_END
2820
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002821 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2822 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002823 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002824
2825 /* check if events needed by the driver got disabled */
2826 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2827 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002828
2829 mutex_unlock(&hotkey_mutex);
2830
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002831 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07002832 pr_err("hotkey_source_mask: "
2833 "failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002834
2835 if (r_ev)
Joe Perches0978e012011-04-04 10:06:25 -07002836 pr_notice("hotkey_source_mask: "
2837 "some important events were disabled: 0x%04x\n",
2838 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002839
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002840 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2841
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002842 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002843}
2844
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002845static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002846
2847/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2848static ssize_t hotkey_poll_freq_show(struct device *dev,
2849 struct device_attribute *attr,
2850 char *buf)
2851{
2852 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2853}
2854
2855static ssize_t hotkey_poll_freq_store(struct device *dev,
2856 struct device_attribute *attr,
2857 const char *buf, size_t count)
2858{
2859 unsigned long t;
2860
2861 if (parse_strtoul(buf, 25, &t))
2862 return -EINVAL;
2863
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002864 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002865 return -ERESTARTSYS;
2866
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002867 hotkey_poll_set_freq(t);
2868 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002869
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002870 mutex_unlock(&hotkey_mutex);
2871
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002872 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2873
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002874 return count;
2875}
2876
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002877static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002878
2879#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2880
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002881/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002882static ssize_t hotkey_radio_sw_show(struct device *dev,
2883 struct device_attribute *attr,
2884 char *buf)
2885{
Johannes Berg19d337d2009-06-02 13:01:37 +02002886 int res;
2887 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002888 if (res < 0)
2889 return res;
2890
Johannes Berg19d337d2009-06-02 13:01:37 +02002891 /* Opportunistic update */
2892 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2893
2894 return snprintf(buf, PAGE_SIZE, "%d\n",
2895 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002896}
2897
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002898static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002899
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002900static void hotkey_radio_sw_notify_change(void)
2901{
2902 if (tp_features.hotkey_wlsw)
2903 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2904 "hotkey_radio_sw");
2905}
2906
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002907/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2908static ssize_t hotkey_tablet_mode_show(struct device *dev,
2909 struct device_attribute *attr,
2910 char *buf)
2911{
2912 int res, s;
2913 res = hotkey_get_tablet_mode(&s);
2914 if (res < 0)
2915 return res;
2916
2917 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2918}
2919
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002920static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002921
2922static void hotkey_tablet_mode_notify_change(void)
2923{
2924 if (tp_features.hotkey_tablet)
2925 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2926 "hotkey_tablet_mode");
2927}
2928
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002929/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002930static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2931 struct device_attribute *attr,
2932 char *buf)
2933{
2934 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2935}
2936
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002937static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002938
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002939static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002940{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002941 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2942 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002943}
2944
2945/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002946static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2947 struct device_attribute *attr,
2948 char *buf)
2949{
2950 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2951}
2952
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002953static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2954 hotkey_wakeup_hotunplug_complete_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002955
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002956static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002957{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002958 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2959 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002960}
2961
Bastien Nocerab790cee2015-03-02 14:45:27 +01002962/* sysfs adaptive kbd mode --------------------------------------------- */
2963
2964static int adaptive_keyboard_get_mode(void);
2965static int adaptive_keyboard_set_mode(int new_mode);
2966
2967enum ADAPTIVE_KEY_MODE {
2968 HOME_MODE,
2969 WEB_BROWSER_MODE,
2970 WEB_CONFERENCE_MODE,
2971 FUNCTION_MODE,
2972 LAYFLAT_MODE
2973};
2974
2975static ssize_t adaptive_kbd_mode_show(struct device *dev,
2976 struct device_attribute *attr,
2977 char *buf)
2978{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03002979 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01002980
2981 current_mode = adaptive_keyboard_get_mode();
2982 if (current_mode < 0)
2983 return current_mode;
2984
2985 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
2986}
2987
2988static ssize_t adaptive_kbd_mode_store(struct device *dev,
2989 struct device_attribute *attr,
2990 const char *buf, size_t count)
2991{
2992 unsigned long t;
2993 int res;
2994
2995 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2996 return -EINVAL;
2997
2998 res = adaptive_keyboard_set_mode(t);
2999 return (res < 0) ? res : count;
3000}
3001
3002static DEVICE_ATTR_RW(adaptive_kbd_mode);
3003
3004static struct attribute *adaptive_kbd_attributes[] = {
3005 &dev_attr_adaptive_kbd_mode.attr,
3006 NULL
3007};
3008
3009static const struct attribute_group adaptive_kbd_attr_group = {
3010 .attrs = adaptive_kbd_attributes,
3011};
3012
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003013/* --------------------------------------------------------------------- */
3014
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003015static struct attribute *hotkey_attributes[] __initdata = {
3016 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003017 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003018 &dev_attr_hotkey_bios_mask.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003019 &dev_attr_wakeup_reason.attr,
3020 &dev_attr_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003021 &dev_attr_hotkey_mask.attr,
3022 &dev_attr_hotkey_all_mask.attr,
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003023 &dev_attr_hotkey_adaptive_all_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003024 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003025#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003026 &dev_attr_hotkey_source_mask.attr,
3027 &dev_attr_hotkey_poll_freq.attr,
3028#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003029};
3030
Johannes Berg19d337d2009-06-02 13:01:37 +02003031/*
3032 * Sync both the hw and sw blocking state of all switches
3033 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003034static void tpacpi_send_radiosw_update(void)
3035{
3036 int wlsw;
3037
Johannes Berg19d337d2009-06-02 13:01:37 +02003038 /*
3039 * We must sync all rfkill controllers *before* issuing any
3040 * rfkill input events, or we will race the rfkill core input
3041 * handler.
3042 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003043 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003044 * for the above.
3045 *
3046 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3047 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003048
Johannes Berg19d337d2009-06-02 13:01:37 +02003049 wlsw = hotkey_get_wlsw();
3050
3051 /* Sync hw blocking state first if it is hw-blocked */
3052 if (wlsw == TPACPI_RFK_RADIO_OFF)
3053 tpacpi_rfk_update_hwblock_state(true);
3054
3055 /* Sync sw blocking state */
3056 tpacpi_rfk_update_swstate_all();
3057
3058 /* Sync hw blocking state last if it is hw-unblocked */
3059 if (wlsw == TPACPI_RFK_RADIO_ON)
3060 tpacpi_rfk_update_hwblock_state(false);
3061
3062 /* Issue rfkill input event for WLSW switch */
3063 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003064 mutex_lock(&tpacpi_inputdev_send_mutex);
3065
3066 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003067 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003068 input_sync(tpacpi_inputdev);
3069
3070 mutex_unlock(&tpacpi_inputdev_send_mutex);
3071 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003072
3073 /*
3074 * this can be unconditional, as we will poll state again
3075 * if userspace uses the notify to read data
3076 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003077 hotkey_radio_sw_notify_change();
3078}
3079
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003080static void hotkey_exit(void)
3081{
3082#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003083 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003084 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003085 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003086#endif
3087
3088 if (hotkey_dev_attributes)
3089 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3090
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003091 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003092 "restoring original HKEY status and mask\n");
3093 /* yes, there is a bitwise or below, we want the
3094 * functions to be called even if one of them fail */
3095 if (((tp_features.hotkey_mask &&
3096 hotkey_mask_set(hotkey_orig_mask)) |
3097 hotkey_status_set(false)) != 0)
Joe Perches0978e012011-04-04 10:06:25 -07003098 pr_err("failed to restore hot key mask "
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003099 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003100}
3101
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003102static void __init hotkey_unmap(const unsigned int scancode)
3103{
3104 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3105 clear_bit(hotkey_keycode_map[scancode],
3106 tpacpi_inputdev->keybit);
3107 hotkey_keycode_map[scancode] = KEY_RESERVED;
3108 }
3109}
3110
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003111/*
3112 * HKEY quirks:
3113 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3114 */
3115
3116#define TPACPI_HK_Q_INIMASK 0x0001
3117
3118static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3119 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3120 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3121 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3122 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3123 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3124 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3125 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3126 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3127 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3128 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3129 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3130 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3131 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3132 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3133 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3134 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3135 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3136 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3137 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3138};
3139
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003140typedef u16 tpacpi_keymap_entry_t;
3141typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003142
Lyudeb3180022016-11-11 15:15:02 -05003143static int hotkey_init_tablet_mode(void)
3144{
Darren Hartcb2bf252016-12-14 14:45:48 -08003145 int in_tablet_mode = 0, res;
3146 char *type = NULL;
Lyudeb3180022016-11-11 15:15:02 -05003147
Lyudeb3180022016-11-11 15:15:02 -05003148 if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
Lyudeb03f4d42016-11-11 15:15:03 -05003149 /* For X41t, X60t, X61t Tablets... */
Lyudeb3180022016-11-11 15:15:02 -05003150 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3151 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3152 type = "MHKG";
Lyudeb03f4d42016-11-11 15:15:03 -05003153 } else if (acpi_evalf(ec_handle, &res, "CMMD", "qd")) {
3154 /* For X1 Yoga (2016) */
3155 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_CMMD;
3156 in_tablet_mode = res == TP_EC_CMMD_TABLET_MODE;
3157 type = "CMMD";
Lyudeb3180022016-11-11 15:15:02 -05003158 }
3159
3160 if (!tp_features.hotkey_tablet)
3161 return 0;
3162
3163 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3164 type, in_tablet_mode ? "tablet" : "laptop");
3165
3166 res = add_to_attr_set(hotkey_dev_attributes,
3167 &dev_attr_hotkey_tablet_mode.attr);
3168 if (res)
3169 return -1;
3170
3171 return in_tablet_mode;
3172}
3173
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003174static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003175{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003176 /* Requirements for changing the default keymaps:
3177 *
3178 * 1. Many of the keys are mapped to KEY_RESERVED for very
3179 * good reasons. Do not change them unless you have deep
3180 * knowledge on the IBM and Lenovo ThinkPad firmware for
3181 * the various ThinkPad models. The driver behaves
3182 * differently for KEY_RESERVED: such keys have their
3183 * hot key mask *unset* in mask_recommended, and also
3184 * in the initial hot key mask programmed into the
3185 * firmware at driver load time, which means the firm-
3186 * ware may react very differently if you change them to
3187 * something else;
3188 *
3189 * 2. You must be subscribed to the linux-thinkpad and
3190 * ibm-acpi-devel mailing lists, and you should read the
3191 * list archives since 2007 if you want to change the
3192 * keymaps. This requirement exists so that you will
3193 * know the past history of problems with the thinkpad-
3194 * acpi driver keymaps, and also that you will be
3195 * listening to any bug reports;
3196 *
3197 * 3. Do not send thinkpad-acpi specific patches directly to
3198 * for merging, *ever*. Send them to the linux-acpi
3199 * mailinglist for comments. Merging is to be done only
3200 * through acpi-test and the ACPI maintainer.
3201 *
3202 * If the above is too much to ask, don't change the keymap.
3203 * Ask the thinkpad-acpi maintainer to do it, instead.
3204 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003205
3206 enum keymap_index {
3207 TPACPI_KEYMAP_IBM_GENERIC = 0,
3208 TPACPI_KEYMAP_LENOVO_GENERIC,
3209 };
3210
3211 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3212 /* Generic keymap for IBM ThinkPads */
3213 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003214 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003215 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003216 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3217 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003218
3219 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003220 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3221 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3222 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003223
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003224 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003225 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003226 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003227
3228 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003229 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003230
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003231 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3232 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003233
3234 /* Volume: firmware always react to it and reprograms
3235 * the built-in *extra* mixer. Never map it to control
3236 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003237 KEY_RESERVED, /* 0x14: VOLUME UP */
3238 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3239 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003240
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003241 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003242
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003243 /* (assignments unknown, please report if found) */
3244 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3245 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003246
3247 /* No assignments, only used for Adaptive keyboards. */
3248 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3249 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3250 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3251 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3252 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003253 },
3254
3255 /* Generic keymap for Lenovo ThinkPads */
3256 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003257 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3258 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003259 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003260 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003261
3262 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003263 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3264 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3265 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003266
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003267 /* These should be enabled --only-- when ACPI video
3268 * is disabled (i.e. in "vendor" mode), and are handled
3269 * in a special way by the init code */
3270 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3271 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003272
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003273 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003274
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003275 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3276 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003277
3278 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3279 * react to it and reprograms the built-in *extra* mixer.
3280 * Never map it to control another mixer by default.
3281 *
3282 * T60?, T61, R60?, R61: firmware and EC tries to send
3283 * these over the regular keyboard, so these are no-ops,
3284 * but there are still weird bugs re. MUTE, so do not
3285 * change unless you get test reports from all Lenovo
3286 * models. May cause the BIOS to interfere with the
3287 * HDA mixer.
3288 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003289 KEY_RESERVED, /* 0x14: VOLUME UP */
3290 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3291 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003292
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003293 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003294
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003295 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003296 KEY_UNKNOWN, KEY_UNKNOWN,
3297
3298 /*
3299 * The mic mute button only sends 0x1a. It does not
3300 * automatically mute the mic or change the mute light.
3301 */
3302 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3303
3304 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003305 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003306
3307 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003308 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003309
3310 /*
3311 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3312 * The first item in this list is the Mute button which is
3313 * emitted with 0x103 through
3314 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3315 * symbol is held.
3316 * We'll need to offset those by 0x20.
3317 */
3318 KEY_RESERVED, /* Mute held, 0x103 */
3319 KEY_BRIGHTNESS_MIN, /* Backlight off */
3320 KEY_RESERVED, /* Clipping tool */
3321 KEY_RESERVED, /* Cloud */
3322 KEY_RESERVED,
3323 KEY_VOICECOMMAND, /* Voice */
3324 KEY_RESERVED,
3325 KEY_RESERVED, /* Gestures */
3326 KEY_RESERVED,
3327 KEY_RESERVED,
3328 KEY_RESERVED,
3329 KEY_CONFIG, /* Settings */
3330 KEY_RESERVED, /* New tab */
3331 KEY_REFRESH, /* Reload */
3332 KEY_BACK, /* Back */
3333 KEY_RESERVED, /* Microphone down */
3334 KEY_RESERVED, /* Microphone up */
3335 KEY_RESERVED, /* Microphone cancellation */
3336 KEY_RESERVED, /* Camera mode */
3337 KEY_RESERVED, /* Rotate display, 0x116 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003338 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003339 };
3340
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003341 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3342 /* Generic maps (fallback) */
3343 {
3344 .vendor = PCI_VENDOR_ID_IBM,
3345 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3346 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3347 },
3348 {
3349 .vendor = PCI_VENDOR_ID_LENOVO,
3350 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3351 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3352 },
3353 };
3354
3355#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003356#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003357
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003358 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003359 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003360 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003361 bool radiosw_state = false;
3362 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003363
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003364 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003365 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003366
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003367 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3368 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003369
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003370 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003371 BUG_ON(tpacpi_inputdev->open != NULL ||
3372 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003373
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003374 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003375 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003376
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003377#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003378 mutex_init(&hotkey_thread_data_mutex);
3379#endif
3380
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003381 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003382 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003383
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003384 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3385 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003386 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003387
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003388 if (!tp_features.hotkey)
3389 return 1;
3390
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003391 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3392 ARRAY_SIZE(tpacpi_hotkey_qtable));
3393
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003394 tpacpi_disable_brightness_delay();
3395
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003396 /* MUST have enough space for all attributes to be added to
3397 * hotkey_dev_attributes */
3398 hotkey_dev_attributes = create_attr_set(
3399 ARRAY_SIZE(hotkey_attributes) + 2,
3400 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003401 if (!hotkey_dev_attributes)
3402 return -ENOMEM;
3403 res = add_many_to_attr_set(hotkey_dev_attributes,
3404 hotkey_attributes,
3405 ARRAY_SIZE(hotkey_attributes));
3406 if (res)
3407 goto err_exit;
3408
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003409 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003410 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3411 for HKEY interface version 0x100 */
3412 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003413 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3414 "firmware HKEY interface version: 0x%x\n",
3415 hkeyv);
3416
3417 switch (hkeyv >> 8) {
3418 case 1:
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003419 /*
3420 * MHKV 0x100 in A31, R40, R40e,
3421 * T4x, X31, and later
3422 */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003423
3424 /* Paranoia check AND init hotkey_all_mask */
3425 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3426 "MHKA", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003427 pr_err("missing MHKA handler, please report this to %s\n",
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003428 TPACPI_MAIL);
3429 /* Fallback: pre-init for FN+F3,F4,F12 */
3430 hotkey_all_mask = 0x080cU;
3431 } else {
3432 tp_features.hotkey_mask = 1;
3433 }
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003434 break;
3435
3436 case 2:
3437 /*
3438 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3439 */
3440
3441 /* Paranoia check AND init hotkey_all_mask */
3442 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3443 "MHKA", "dd", 1)) {
3444 pr_err("missing MHKA handler, please report this to %s\n",
3445 TPACPI_MAIL);
3446 /* Fallback: pre-init for FN+F3,F4,F12 */
3447 hotkey_all_mask = 0x080cU;
3448 } else {
3449 tp_features.hotkey_mask = 1;
3450 }
3451
3452 /*
3453 * Check if we have an adaptive keyboard, like on the
3454 * Lenovo Carbon X1 2014 (2nd Gen).
3455 */
3456 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3457 "MHKA", "dd", 2)) {
3458 if (hotkey_adaptive_all_mask != 0) {
3459 tp_features.has_adaptive_kbd = true;
3460 res = sysfs_create_group(
3461 &tpacpi_pdev->dev.kobj,
3462 &adaptive_kbd_attr_group);
3463 if (res)
3464 goto err_exit;
3465 }
3466 } else {
3467 tp_features.has_adaptive_kbd = false;
3468 hotkey_adaptive_all_mask = 0x0U;
3469 }
3470 break;
3471
3472 default:
3473 pr_err("unknown version of the HKEY interface: 0x%x\n",
3474 hkeyv);
3475 pr_err("please report this to %s\n", TPACPI_MAIL);
3476 break;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003477 }
3478 }
3479
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003480 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3481 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003482 str_supported(tp_features.hotkey_mask));
3483
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003484 /* Init hotkey_all_mask if not initialized yet */
3485 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3486 (quirks & TPACPI_HK_Q_INIMASK))
3487 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003488
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003489 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003490 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003491 /* hotkey_source_mask *must* be zero for
3492 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003493 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003494 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003495 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003496
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003497 hotkey_orig_mask = hotkey_acpi_mask;
3498 } else {
3499 hotkey_orig_mask = hotkey_all_mask;
3500 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003501 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003502
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003503#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3504 if (dbg_wlswemul) {
3505 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003506 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003507 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003508 } else
3509#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003510 /* Not all thinkpads have a hardware radio switch */
3511 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3512 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003513 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003514 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003515 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003516 }
3517 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003518 res = add_to_attr_set(hotkey_dev_attributes,
3519 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003520
Lyudeb3180022016-11-11 15:15:02 -05003521 res = hotkey_init_tablet_mode();
3522 if (res < 0)
3523 goto err_exit;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003524
Lyudeb3180022016-11-11 15:15:02 -05003525 tabletsw_state = res;
3526
3527 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3528 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003529 if (res)
3530 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003531
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003532 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003533 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3534 GFP_KERNEL);
3535 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003536 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003537 res = -ENOMEM;
3538 goto err_exit;
3539 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003540
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003541 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3542 ARRAY_SIZE(tpacpi_keymap_qtable));
3543 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3544 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3545 "using keymap number %lu\n", keymap_id);
3546
3547 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3548 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003549
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003550 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003551 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3552 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3553 tpacpi_inputdev->keycode = hotkey_keycode_map;
3554 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3555 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003556 input_set_capability(tpacpi_inputdev, EV_KEY,
3557 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003558 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003559 if (i < sizeof(hotkey_reserved_mask)*8)
3560 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003561 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003562 }
3563
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003564 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003565 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003566 input_report_switch(tpacpi_inputdev,
3567 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003568 }
3569 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003570 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003571 input_report_switch(tpacpi_inputdev,
3572 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003573 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003574
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003575 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003576 * userspace. tpacpi_detect_brightness_capabilities() must have
3577 * been called before this point */
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02003578 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Joe Perches0978e012011-04-04 10:06:25 -07003579 pr_info("This ThinkPad has standard ACPI backlight "
3580 "brightness control, supported by the ACPI "
3581 "video driver\n");
3582 pr_notice("Disabling thinkpad-acpi brightness events "
3583 "by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003584
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003585 /* Disable brightness up/down on Lenovo thinkpads when
3586 * ACPI is handling them, otherwise it is plain impossible
3587 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003588 hotkey_reserved_mask |=
3589 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3590 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003591 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3592 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003593 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003594
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003595#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003596 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3597 & ~hotkey_all_mask
3598 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003599
3600 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3601 "hotkey source mask 0x%08x, polling freq %u\n",
3602 hotkey_source_mask, hotkey_poll_freq);
3603#endif
3604
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003605 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3606 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003607 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003608 if (res) {
3609 hotkey_exit();
3610 return res;
3611 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003612 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3613 | hotkey_driver_mask)
3614 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003615 if (res < 0 && res != -ENXIO) {
3616 hotkey_exit();
3617 return res;
3618 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003619 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3620 & ~hotkey_reserved_mask;
3621 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3622 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3623 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003624
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003625 tpacpi_inputdev->open = &hotkey_inputdev_open;
3626 tpacpi_inputdev->close = &hotkey_inputdev_close;
3627
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003628 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003629
3630 return 0;
3631
3632err_exit:
3633 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
Bastien Nocerab790cee2015-03-02 14:45:27 +01003634 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3635 &adaptive_kbd_attr_group);
3636
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003637 hotkey_dev_attributes = NULL;
3638
Jan van den Berg72a979f2014-09-17 00:01:08 +02003639 return (res < 0) ? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003640}
3641
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003642/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3643 * mode, Web conference mode, Function mode and Lay-flat mode.
3644 * We support Home mode and Function mode currently.
3645 *
3646 * Will consider support rest of modes in future.
3647 *
3648 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003649static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003650 HOME_MODE,
3651/* WEB_BROWSER_MODE = 2,
3652 WEB_CONFERENCE_MODE = 3, */
3653 FUNCTION_MODE
3654};
3655
3656#define DFR_CHANGE_ROW 0x101
3657#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003658#define FIRST_ADAPTIVE_KEY 0x103
3659#define ADAPTIVE_KEY_OFFSET 0x020
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003660
3661/* press Fn key a while second, it will switch to Function Mode. Then
3662 * release Fn key, previous mode be restored.
3663 */
3664static bool adaptive_keyboard_mode_is_saved;
3665static int adaptive_keyboard_prev_mode;
3666
Bastien Noceraf74587f2015-03-02 14:45:22 +01003667static int adaptive_keyboard_get_mode(void)
3668{
3669 int mode = 0;
3670
3671 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3672 pr_err("Cannot read adaptive keyboard mode\n");
3673 return -EIO;
3674 }
3675
3676 return mode;
3677}
3678
3679static int adaptive_keyboard_set_mode(int new_mode)
3680{
3681 if (new_mode < 0 ||
3682 new_mode > LAYFLAT_MODE)
3683 return -EINVAL;
3684
3685 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3686 pr_err("Cannot set adaptive keyboard mode\n");
3687 return -EIO;
3688 }
3689
3690 return 0;
3691}
3692
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003693static int adaptive_keyboard_get_next_mode(int mode)
3694{
3695 size_t i;
3696 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3697
3698 for (i = 0; i <= max_mode; i++) {
3699 if (adaptive_keyboard_modes[i] == mode)
3700 break;
3701 }
3702
3703 if (i >= max_mode)
3704 i = 0;
3705 else
3706 i++;
3707
3708 return adaptive_keyboard_modes[i];
3709}
3710
3711static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3712{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003713 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003714 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003715 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003716
3717 switch (scancode) {
3718 case DFR_CHANGE_ROW:
3719 if (adaptive_keyboard_mode_is_saved) {
3720 new_mode = adaptive_keyboard_prev_mode;
3721 adaptive_keyboard_mode_is_saved = false;
3722 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003723 current_mode = adaptive_keyboard_get_mode();
3724 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003725 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003726 new_mode = adaptive_keyboard_get_next_mode(
3727 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003728 }
3729
Bastien Noceraf74587f2015-03-02 14:45:22 +01003730 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003731 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003732
3733 return true;
3734
3735 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003736 current_mode = adaptive_keyboard_get_mode();
3737 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003738 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003739
Bastien Noceraf74587f2015-03-02 14:45:22 +01003740 adaptive_keyboard_prev_mode = current_mode;
3741 adaptive_keyboard_mode_is_saved = true;
3742
3743 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3744 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003745 return true;
3746
3747 default:
Dan Carpenter741d98c2015-03-11 12:36:07 +03003748 if (scancode < FIRST_ADAPTIVE_KEY ||
3749 scancode >= FIRST_ADAPTIVE_KEY + TPACPI_HOTKEY_MAP_LEN -
3750 ADAPTIVE_KEY_OFFSET) {
Bastien Nocera6a68d852015-03-02 14:45:31 +01003751 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3752 scancode);
3753 return false;
3754 }
3755 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY + ADAPTIVE_KEY_OFFSET];
3756 if (keycode != KEY_RESERVED) {
3757 mutex_lock(&tpacpi_inputdev_send_mutex);
3758
3759 input_report_key(tpacpi_inputdev, keycode, 1);
3760 input_sync(tpacpi_inputdev);
3761
3762 input_report_key(tpacpi_inputdev, keycode, 0);
3763 input_sync(tpacpi_inputdev);
3764
3765 mutex_unlock(&tpacpi_inputdev_send_mutex);
3766 }
3767 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003768 }
3769}
3770
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003771static bool hotkey_notify_hotkey(const u32 hkey,
3772 bool *send_acpi_ev,
3773 bool *ignore_acpi_ev)
3774{
3775 /* 0x1000-0x1FFF: key presses */
3776 unsigned int scancode = hkey & 0xfff;
3777 *send_acpi_ev = true;
3778 *ignore_acpi_ev = false;
3779
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03003780 /* HKEY event 0x1001 is scancode 0x00 */
3781 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003782 scancode--;
3783 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003784 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003785 *send_acpi_ev = false;
3786 } else {
3787 *ignore_acpi_ev = true;
3788 }
3789 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003790 } else {
3791 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003792 }
3793 return false;
3794}
3795
3796static bool hotkey_notify_wakeup(const u32 hkey,
3797 bool *send_acpi_ev,
3798 bool *ignore_acpi_ev)
3799{
3800 /* 0x2000-0x2FFF: Wakeup reason */
3801 *send_acpi_ev = true;
3802 *ignore_acpi_ev = false;
3803
3804 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003805 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3806 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003807 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3808 *ignore_acpi_ev = true;
3809 break;
3810
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003811 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3812 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003813 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3814 *ignore_acpi_ev = true;
3815 break;
3816
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003817 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3818 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003819 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003820 /* how to auto-heal: */
3821 /* 2313: woke up from S3, go to S4/S5 */
3822 /* 2413: woke up from S4, go to S5 */
3823 break;
3824
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003825 default:
3826 return false;
3827 }
3828
3829 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003830 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003831 hotkey_wakeup_reason_notify_change();
3832 }
3833 return true;
3834}
3835
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003836static bool hotkey_notify_dockevent(const u32 hkey,
3837 bool *send_acpi_ev,
3838 bool *ignore_acpi_ev)
3839{
3840 /* 0x4000-0x4FFF: dock-related events */
3841 *send_acpi_ev = true;
3842 *ignore_acpi_ev = false;
3843
3844 switch (hkey) {
3845 case TP_HKEY_EV_UNDOCK_ACK:
3846 /* ACPI undock operation completed after wakeup */
3847 hotkey_autosleep_ack = 1;
3848 pr_info("undocked\n");
3849 hotkey_wakeup_hotunplug_complete_notify_change();
3850 return true;
3851
3852 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3853 pr_info("docked into hotplug port replicator\n");
3854 return true;
3855 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3856 pr_info("undocked from hotplug port replicator\n");
3857 return true;
3858
3859 default:
3860 return false;
3861 }
3862}
3863
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003864static bool hotkey_notify_usrevent(const u32 hkey,
3865 bool *send_acpi_ev,
3866 bool *ignore_acpi_ev)
3867{
3868 /* 0x5000-0x5FFF: human interface helpers */
3869 *send_acpi_ev = true;
3870 *ignore_acpi_ev = false;
3871
3872 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003873 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3874 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003875 return true;
3876
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003877 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3878 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003879 tpacpi_input_send_tabletsw();
3880 hotkey_tablet_mode_notify_change();
3881 *send_acpi_ev = false;
3882 return true;
3883
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003884 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3885 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3886 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003887 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003888 *ignore_acpi_ev = true;
3889 return true;
3890
3891 default:
3892 return false;
3893 }
3894}
3895
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003896static void thermal_dump_all_sensors(void);
3897
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003898static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003899 bool *send_acpi_ev,
3900 bool *ignore_acpi_ev)
3901{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003902 bool known = true;
3903
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003904 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003905 *send_acpi_ev = true;
3906 *ignore_acpi_ev = false;
3907
3908 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003909 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003910 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003911 /* recommended action: do nothing, we don't have
3912 * Lenovo ATM information */
3913 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003914 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003915 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003916 /* recommended action: warn user through gui */
3917 break;
3918 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003919 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003920 /* recommended action: immediate sleep/hibernate */
3921 break;
3922 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003923 pr_crit("THERMAL ALARM: "
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003924 "a sensor reports something is too hot!\n");
3925 /* recommended action: warn user through gui, that */
3926 /* some internal component is too hot */
3927 break;
3928 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003929 pr_alert("THERMAL EMERGENCY: "
3930 "a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003931 /* recommended action: immediate sleep/hibernate */
3932 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01003933 case TP_HKEY_EV_AC_CHANGED:
3934 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3935 * AC status changed; can be triggered by plugging or
3936 * unplugging AC adapter, docking or undocking. */
3937
3938 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003939
3940 case TP_HKEY_EV_KEY_NUMLOCK:
3941 case TP_HKEY_EV_KEY_FN:
Xavier Naveira67ab6242015-02-10 08:45:18 +01003942 case TP_HKEY_EV_KEY_FN_ESC:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003943 /* key press events, we just ignore them as long as the EC
3944 * is still reporting them in the normal keyboard stream */
3945 *send_acpi_ev = false;
3946 *ignore_acpi_ev = true;
3947 return true;
3948
Lyudeb03f4d42016-11-11 15:15:03 -05003949 case TP_HKEY_EV_TABLET_CHANGED:
3950 tpacpi_input_send_tabletsw();
3951 hotkey_tablet_mode_notify_change();
3952 *send_acpi_ev = false;
3953 break;
3954
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003955 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003956 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003957 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003958 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003959
3960 thermal_dump_all_sensors();
3961
3962 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003963}
3964
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003965static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3966{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003967 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003968 bool send_acpi_ev;
3969 bool ignore_acpi_ev;
3970 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003971
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003972 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07003973 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003974 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003975 acpi_bus_generate_netlink_event(
3976 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003977 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003978 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003979 return;
3980 }
3981
3982 while (1) {
3983 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07003984 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003985 return;
3986 }
3987
3988 if (hkey == 0) {
3989 /* queue empty */
3990 return;
3991 }
3992
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003993 send_acpi_ev = true;
3994 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003995
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003996 switch (hkey >> 12) {
3997 case 1:
3998 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003999 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4000 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004001 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004002 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004003 /* 0x2000-0x2FFF: Wakeup reason */
4004 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4005 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004006 break;
4007 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004008 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004009 switch (hkey) {
4010 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004011 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004012 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004013 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004014 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004015 break;
4016 case TP_HKEY_EV_OPTDRV_EJ:
4017 /* FIXME: kick libata if SATA link offline */
4018 known_ev = true;
4019 break;
4020 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004021 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004022 }
4023 break;
4024 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03004025 /* 0x4000-0x4FFF: dock-related events */
4026 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4027 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004028 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004029 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02004030 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004031 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4032 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004033 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004034 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004035 /* 0x6000-0x6FFF: thermal alarms/notices and
4036 * keyboard events */
4037 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004038 &ignore_acpi_ev);
4039 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004040 case 7:
4041 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004042 if (tp_features.hotkey_wlsw &&
4043 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004044 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004045 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004046 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004047 break;
4048 }
4049 /* fallthrough to default */
4050 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004051 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004052 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004053 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07004054 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4055 pr_notice("please report the conditions when this "
4056 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004057 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004058
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004059 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03004060 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004061 acpi_bus_generate_netlink_event(
4062 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004063 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004064 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004065 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004066 }
4067}
4068
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02004069static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004070{
4071 /* Do these on suspend, we get the events on early resume! */
4072 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4073 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08004074
4075 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004076 if (tp_features.has_adaptive_kbd) {
4077 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4078 "GTRW", "dd", 0)) {
4079 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004080 }
4081 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004082}
4083
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004084static void hotkey_resume(void)
4085{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03004086 tpacpi_disable_brightness_delay();
4087
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004088 if (hotkey_status_set(true) < 0 ||
4089 hotkey_mask_set(hotkey_acpi_mask) < 0)
Joe Perches0978e012011-04-04 10:06:25 -07004090 pr_err("error while attempting to reset the event "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004091 "firmware interface\n");
4092
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004093 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02004094 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004095 hotkey_wakeup_reason_notify_change();
4096 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004097 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004098
4099 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004100 if (tp_features.has_adaptive_kbd) {
4101 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4102 adaptive_keyboard_prev_mode)) {
4103 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004104 }
4105 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004106}
4107
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004108/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004109static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004111 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004113 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004114 seq_printf(m, "status:\t\tnot supported\n");
4115 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004116 }
4117
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004118 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004119 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004120 res = hotkey_status_get(&status);
4121 if (!res)
4122 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004123 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004124 if (res)
4125 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004127 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004128 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004129 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4130 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004132 seq_printf(m, "mask:\t\tnot supported\n");
4133 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 }
4135
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137}
4138
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004139static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004140{
4141 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004142 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Joe Perches0978e012011-04-04 10:06:25 -07004143 pr_fmt("hotkey enable/disable functionality has been "
4144 "removed from the driver. "
4145 "Hotkeys are always enabled.\n")))
4146 pr_err("Please remove the hotkey=enable module "
4147 "parameter, it is deprecated. "
4148 "Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004149}
4150
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004151static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004153 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004154 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004157 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return -ENODEV;
4159
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004160 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004161 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004162
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004163 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004164
4165 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 while ((cmd = next_cmd(&buf))) {
4167 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004168 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004170 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004171 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03004173 mask = (hotkey_all_mask | hotkey_source_mask)
4174 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4176 /* mask set */
4177 } else if (sscanf(cmd, "%x", &mask) == 1) {
4178 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004179 } else {
4180 res = -EINVAL;
4181 goto errexit;
4182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004184
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004185 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004186 tpacpi_disclose_usertask("procfs hotkey",
4187 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004188 res = hotkey_user_mask_set(mask);
4189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004191errexit:
4192 mutex_unlock(&hotkey_mutex);
4193 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004194}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004196static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004197 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4198 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Hui Wanga3c42a42016-11-08 16:13:23 +08004199 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004200 {"", 0},
4201};
4202
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004203static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004204 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004205 .notify = hotkey_notify,
4206 .handle = &hkey_handle,
4207 .type = ACPI_DEVICE_NOTIFY,
4208};
4209
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004210static struct ibm_struct hotkey_driver_data = {
4211 .name = "hotkey",
4212 .read = hotkey_read,
4213 .write = hotkey_write,
4214 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004215 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004216 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004217 .acpi = &ibm_hotkey_acpidriver,
4218};
4219
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004220/*************************************************************************
4221 * Bluetooth subdriver
4222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004224enum {
4225 /* ACPI GBDC/SBDC bits */
4226 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4227 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004228 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004229 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004230};
4231
4232enum {
4233 /* ACPI \BLTH commands */
4234 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4235 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4236 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4237 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4238 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004239};
4240
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004241#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4242
Johannes Berg19d337d2009-06-02 13:01:37 +02004243static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004244{
4245 int status;
4246
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004247#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4248 if (dbg_bluetoothemul)
4249 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004250 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004251#endif
4252
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004253 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4254 return -EIO;
4255
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004256 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004257 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004258}
4259
Johannes Berg19d337d2009-06-02 13:01:37 +02004260static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004261{
4262 int status;
4263
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004264 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004265 "will attempt to %s bluetooth\n",
4266 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004267
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004268#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4269 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004270 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004271 return 0;
4272 }
4273#endif
4274
Johannes Berg19d337d2009-06-02 13:01:37 +02004275 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004276 status = TP_ACPI_BLUETOOTH_RADIOSSW
4277 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4278 else
4279 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004280
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004281 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4282 return -EIO;
4283
4284 return 0;
4285}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004286
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004287/* sysfs bluetooth enable ---------------------------------------------- */
4288static ssize_t bluetooth_enable_show(struct device *dev,
4289 struct device_attribute *attr,
4290 char *buf)
4291{
Johannes Berg19d337d2009-06-02 13:01:37 +02004292 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4293 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004294}
4295
4296static ssize_t bluetooth_enable_store(struct device *dev,
4297 struct device_attribute *attr,
4298 const char *buf, size_t count)
4299{
Johannes Berg19d337d2009-06-02 13:01:37 +02004300 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4301 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004302}
4303
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004304static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004305
4306/* --------------------------------------------------------------------- */
4307
4308static struct attribute *bluetooth_attributes[] = {
4309 &dev_attr_bluetooth_enable.attr,
4310 NULL
4311};
4312
4313static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004314 .attrs = bluetooth_attributes,
4315};
4316
Johannes Berg19d337d2009-06-02 13:01:37 +02004317static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4318 .get_status = bluetooth_get_status,
4319 .set_status = bluetooth_set_status,
4320};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004321
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004322static void bluetooth_shutdown(void)
4323{
4324 /* Order firmware to save current state to NVRAM */
4325 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4326 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004327 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004328 else
4329 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004330 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004331}
4332
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004333static void bluetooth_exit(void)
4334{
4335 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4336 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004337
4338 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4339
4340 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004341}
4342
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004343static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004345 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004346 int status = 0;
4347
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004348 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4349 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004350
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004351 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004352
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004353 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4354 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004355 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004356 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004358 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4359 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004360 str_supported(tp_features.bluetooth),
4361 status);
4362
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004363#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4364 if (dbg_bluetoothemul) {
4365 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004366 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004367 } else
4368#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004369 if (tp_features.bluetooth &&
4370 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4371 /* no bluetooth hardware present in system */
4372 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004373 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004374 "bluetooth hardware not installed\n");
4375 }
4376
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004377 if (!tp_features.bluetooth)
4378 return 1;
4379
Johannes Berg19d337d2009-06-02 13:01:37 +02004380 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4381 &bluetooth_tprfk_ops,
4382 RFKILL_TYPE_BLUETOOTH,
4383 TPACPI_RFK_BLUETOOTH_SW_NAME,
4384 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004385 if (res)
4386 return res;
4387
Johannes Berg19d337d2009-06-02 13:01:37 +02004388 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4389 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004390 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004391 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004392 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004393 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004394
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396}
4397
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004398/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004399static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004401 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402}
4403
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004404static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405{
Johannes Berg19d337d2009-06-02 13:01:37 +02004406 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407}
4408
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004409static struct ibm_struct bluetooth_driver_data = {
4410 .name = "bluetooth",
4411 .read = bluetooth_read,
4412 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004413 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004414 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004415};
4416
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004417/*************************************************************************
4418 * Wan subdriver
4419 */
4420
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004421enum {
4422 /* ACPI GWAN/SWAN bits */
4423 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4424 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004425 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004426 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004427};
4428
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004429#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4430
Johannes Berg19d337d2009-06-02 13:01:37 +02004431static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004432{
4433 int status;
4434
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004435#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4436 if (dbg_wwanemul)
4437 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004438 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004439#endif
4440
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004441 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4442 return -EIO;
4443
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004444 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004445 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004446}
4447
Johannes Berg19d337d2009-06-02 13:01:37 +02004448static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004449{
4450 int status;
4451
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004452 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004453 "will attempt to %s wwan\n",
4454 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004455
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004456#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4457 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004458 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004459 return 0;
4460 }
4461#endif
4462
Johannes Berg19d337d2009-06-02 13:01:37 +02004463 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004464 status = TP_ACPI_WANCARD_RADIOSSW
4465 | TP_ACPI_WANCARD_RESUMECTRL;
4466 else
4467 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004468
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004469 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4470 return -EIO;
4471
4472 return 0;
4473}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004474
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004475/* sysfs wan enable ---------------------------------------------------- */
4476static ssize_t wan_enable_show(struct device *dev,
4477 struct device_attribute *attr,
4478 char *buf)
4479{
Johannes Berg19d337d2009-06-02 13:01:37 +02004480 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4481 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004482}
4483
4484static ssize_t wan_enable_store(struct device *dev,
4485 struct device_attribute *attr,
4486 const char *buf, size_t count)
4487{
Johannes Berg19d337d2009-06-02 13:01:37 +02004488 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4489 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004490}
4491
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004492static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4493 wan_enable_show, wan_enable_store);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004494
4495/* --------------------------------------------------------------------- */
4496
4497static struct attribute *wan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004498 &dev_attr_wwan_enable.attr,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004499 NULL
4500};
4501
4502static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004503 .attrs = wan_attributes,
4504};
4505
Johannes Berg19d337d2009-06-02 13:01:37 +02004506static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4507 .get_status = wan_get_status,
4508 .set_status = wan_set_status,
4509};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004510
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004511static void wan_shutdown(void)
4512{
4513 /* Order firmware to save current state to NVRAM */
4514 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4515 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004516 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004517 else
4518 vdbg_printk(TPACPI_DBG_RFKILL,
4519 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004520}
4521
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004522static void wan_exit(void)
4523{
4524 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4525 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004526
4527 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4528
4529 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004530}
4531
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004532static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004533{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004534 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004535 int status = 0;
4536
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004537 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4538 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004539
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004540 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004541
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004542 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004543 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004544
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004545 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4546 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004547 str_supported(tp_features.wan),
4548 status);
4549
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004550#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4551 if (dbg_wwanemul) {
4552 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004553 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004554 } else
4555#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004556 if (tp_features.wan &&
4557 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4558 /* no wan hardware present in system */
4559 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004560 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004561 "wan hardware not installed\n");
4562 }
4563
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004564 if (!tp_features.wan)
4565 return 1;
4566
Johannes Berg19d337d2009-06-02 13:01:37 +02004567 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4568 &wan_tprfk_ops,
4569 RFKILL_TYPE_WWAN,
4570 TPACPI_RFK_WWAN_SW_NAME,
4571 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004572 if (res)
4573 return res;
4574
Johannes Berg19d337d2009-06-02 13:01:37 +02004575 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4576 &wan_attr_group);
4577
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004578 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004579 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004580 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004581 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004582
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004583 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004584}
4585
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004586/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004587static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004588{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004589 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004590}
4591
4592static int wan_write(char *buf)
4593{
Johannes Berg19d337d2009-06-02 13:01:37 +02004594 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004595}
4596
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004597static struct ibm_struct wan_driver_data = {
4598 .name = "wan",
4599 .read = wan_read,
4600 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004601 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004602 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004603};
4604
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004605/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004606 * UWB subdriver
4607 */
4608
4609enum {
4610 /* ACPI GUWB/SUWB bits */
4611 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4612 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4613};
4614
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004615#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4616
Johannes Berg19d337d2009-06-02 13:01:37 +02004617static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004618{
4619 int status;
4620
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004621#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4622 if (dbg_uwbemul)
4623 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004624 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004625#endif
4626
4627 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4628 return -EIO;
4629
4630 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004631 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004632}
4633
Johannes Berg19d337d2009-06-02 13:01:37 +02004634static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004635{
4636 int status;
4637
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004638 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004639 "will attempt to %s UWB\n",
4640 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004641
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004642#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4643 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004644 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004645 return 0;
4646 }
4647#endif
4648
Johannes Berg19d337d2009-06-02 13:01:37 +02004649 if (state == TPACPI_RFK_RADIO_ON)
4650 status = TP_ACPI_UWB_RADIOSSW;
4651 else
4652 status = 0;
4653
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004654 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4655 return -EIO;
4656
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004657 return 0;
4658}
4659
4660/* --------------------------------------------------------------------- */
4661
Johannes Berg19d337d2009-06-02 13:01:37 +02004662static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4663 .get_status = uwb_get_status,
4664 .set_status = uwb_set_status,
4665};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004666
4667static void uwb_exit(void)
4668{
Johannes Berg19d337d2009-06-02 13:01:37 +02004669 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004670}
4671
4672static int __init uwb_init(struct ibm_init_struct *iibm)
4673{
4674 int res;
4675 int status = 0;
4676
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004677 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4678 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004679
4680 TPACPI_ACPIHANDLE_INIT(hkey);
4681
4682 tp_features.uwb = hkey_handle &&
4683 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4684
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004685 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4686 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004687 str_supported(tp_features.uwb),
4688 status);
4689
4690#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4691 if (dbg_uwbemul) {
4692 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004693 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004694 } else
4695#endif
4696 if (tp_features.uwb &&
4697 !(status & TP_ACPI_UWB_HWPRESENT)) {
4698 /* no uwb hardware present in system */
4699 tp_features.uwb = 0;
4700 dbg_printk(TPACPI_DBG_INIT,
4701 "uwb hardware not installed\n");
4702 }
4703
4704 if (!tp_features.uwb)
4705 return 1;
4706
4707 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004708 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004709 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004710 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004711 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004712 return res;
4713}
4714
4715static struct ibm_struct uwb_driver_data = {
4716 .name = "uwb",
4717 .exit = uwb_exit,
4718 .flags.experimental = 1,
4719};
4720
4721/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004722 * Video subdriver
4723 */
4724
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004725#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4726
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004727enum video_access_mode {
4728 TPACPI_VIDEO_NONE = 0,
4729 TPACPI_VIDEO_570, /* 570 */
4730 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4731 TPACPI_VIDEO_NEW, /* all others */
4732};
4733
4734enum { /* video status flags, based on VIDEO_570 */
4735 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4736 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4737 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4738};
4739
4740enum { /* TPACPI_VIDEO_570 constants */
4741 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4742 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4743 * video_status_flags */
4744 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4745 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4746};
4747
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004748static enum video_access_mode video_supported;
4749static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004750
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004751static int video_autosw_get(void);
4752static int video_autosw_set(int enable);
4753
Joe Perches112a6ee2011-04-04 10:06:24 -07004754TPACPI_HANDLE(vid, root,
4755 "\\_SB.PCI.AGP.VGA", /* 570 */
4756 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4757 "\\_SB.PCI0.VID0", /* 770e */
4758 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4759 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4760 "\\_SB.PCI0.AGP.VID", /* all others */
4761 ); /* R30, R31 */
4762
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004763TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004764
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004765static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004767 int ivga;
4768
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004769 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4770
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004771 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004772 if (tpacpi_is_ibm())
4773 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004774
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004775 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4776 /* G41, assume IVGA doesn't change */
4777 vid_handle = vid2_handle;
4778
4779 if (!vid_handle)
4780 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004781 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004782 else if (tpacpi_is_ibm() &&
4783 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004784 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004785 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004786 else if (tpacpi_is_ibm() &&
4787 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004788 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004789 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004790 else
4791 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004792 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004794 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4795 str_supported(video_supported != TPACPI_VIDEO_NONE),
4796 video_supported);
4797
Jan van den Berg72a979f2014-09-17 00:01:08 +02004798 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799}
4800
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004801static void video_exit(void)
4802{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004803 dbg_printk(TPACPI_DBG_EXIT,
4804 "restoring original video autoswitch mode\n");
4805 if (video_autosw_set(video_orig_autosw))
Joe Perches0978e012011-04-04 10:06:25 -07004806 pr_err("error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004807 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004808}
4809
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004810static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811{
4812 int status = 0;
4813 int i;
4814
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004815 switch (video_supported) {
4816 case TPACPI_VIDEO_570:
4817 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4818 TP_ACPI_VIDEO_570_PHSCMD))
4819 return -EIO;
4820 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4821 break;
4822 case TPACPI_VIDEO_770:
4823 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4824 return -EIO;
4825 if (i)
4826 status |= TP_ACPI_VIDEO_S_LCD;
4827 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4828 return -EIO;
4829 if (i)
4830 status |= TP_ACPI_VIDEO_S_CRT;
4831 break;
4832 case TPACPI_VIDEO_NEW:
4833 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4834 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4835 return -EIO;
4836 if (i)
4837 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004839 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4840 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4841 return -EIO;
4842 if (i)
4843 status |= TP_ACPI_VIDEO_S_LCD;
4844 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4845 return -EIO;
4846 if (i)
4847 status |= TP_ACPI_VIDEO_S_DVI;
4848 break;
4849 default:
4850 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
4853 return status;
4854}
4855
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004856static int video_outputsw_set(int status)
4857{
4858 int autosw;
4859 int res = 0;
4860
4861 switch (video_supported) {
4862 case TPACPI_VIDEO_570:
4863 res = acpi_evalf(NULL, NULL,
4864 "\\_SB.PHS2", "vdd",
4865 TP_ACPI_VIDEO_570_PHS2CMD,
4866 status | TP_ACPI_VIDEO_570_PHS2SET);
4867 break;
4868 case TPACPI_VIDEO_770:
4869 autosw = video_autosw_get();
4870 if (autosw < 0)
4871 return autosw;
4872
4873 res = video_autosw_set(1);
4874 if (res)
4875 return res;
4876 res = acpi_evalf(vid_handle, NULL,
4877 "ASWT", "vdd", status * 0x100, 0);
4878 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004879 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004880 return -EIO;
4881 }
4882 break;
4883 case TPACPI_VIDEO_NEW:
4884 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004885 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004886 break;
4887 default:
4888 return -ENOSYS;
4889 }
4890
Jan van den Berg72a979f2014-09-17 00:01:08 +02004891 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004892}
4893
4894static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004896 int autosw = 0;
4897
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004898 switch (video_supported) {
4899 case TPACPI_VIDEO_570:
4900 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4901 return -EIO;
4902 break;
4903 case TPACPI_VIDEO_770:
4904 case TPACPI_VIDEO_NEW:
4905 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4906 return -EIO;
4907 break;
4908 default:
4909 return -ENOSYS;
4910 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004911
4912 return autosw & 1;
4913}
4914
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004915static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004916{
Jan van den Berg72a979f2014-09-17 00:01:08 +02004917 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004918 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004919 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004920}
4921
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004922static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004923{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004924 int autosw = video_autosw_get();
4925 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004926
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004927 if (autosw < 0)
4928 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004929
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004930 switch (video_supported) {
4931 case TPACPI_VIDEO_570:
4932 res = video_autosw_set(1);
4933 if (res)
4934 return res;
4935 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4936 break;
4937 case TPACPI_VIDEO_770:
4938 case TPACPI_VIDEO_NEW:
4939 res = video_autosw_set(1);
4940 if (res)
4941 return res;
4942 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4943 break;
4944 default:
4945 return -ENOSYS;
4946 }
4947 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004948 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004949 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004950 }
4951
Jan van den Berg72a979f2014-09-17 00:01:08 +02004952 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004953}
4954
4955static int video_expand_toggle(void)
4956{
4957 switch (video_supported) {
4958 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004959 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004960 0 : -EIO;
4961 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004962 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004963 0 : -EIO;
4964 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004965 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004966 0 : -EIO;
4967 default:
4968 return -ENOSYS;
4969 }
4970 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004971}
4972
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004973static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004974{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004975 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004976
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004977 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004978 seq_printf(m, "status:\t\tnot supported\n");
4979 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004980 }
4981
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004982 /* Even reads can crash X.org, so... */
4983 if (!capable(CAP_SYS_ADMIN))
4984 return -EPERM;
4985
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004986 status = video_outputsw_get();
4987 if (status < 0)
4988 return status;
4989
4990 autosw = video_autosw_get();
4991 if (autosw < 0)
4992 return autosw;
4993
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004994 seq_printf(m, "status:\t\tsupported\n");
4995 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4996 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004997 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004998 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4999 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5000 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5001 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005002 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005003 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5004 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5005 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005006
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005007 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005008}
5009
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005010static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011{
5012 char *cmd;
5013 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005014 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005016 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005017 return -ENODEV;
5018
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005019 /* Even reads can crash X.org, let alone writes... */
5020 if (!capable(CAP_SYS_ADMIN))
5021 return -EPERM;
5022
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005023 enable = 0;
5024 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
5026 while ((cmd = next_cmd(&buf))) {
5027 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005028 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005030 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005032 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005034 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005035 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005036 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005037 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005038 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005039 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005040 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005042 res = video_autosw_set(1);
5043 if (res)
5044 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005046 res = video_autosw_set(0);
5047 if (res)
5048 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005050 res = video_outputsw_cycle();
5051 if (res)
5052 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005054 res = video_expand_toggle();
5055 if (res)
5056 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 } else
5058 return -EINVAL;
5059 }
5060
5061 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005062 status = video_outputsw_get();
5063 if (status < 0)
5064 return status;
5065 res = video_outputsw_set((status & ~disable) | enable);
5066 if (res)
5067 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 }
5069
5070 return 0;
5071}
5072
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005073static struct ibm_struct video_driver_data = {
5074 .name = "video",
5075 .read = video_read,
5076 .write = video_write,
5077 .exit = video_exit,
5078};
5079
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005080#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5081
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005082/*************************************************************************
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005083 * Keyboard backlight subdriver
5084 */
5085
Hans de Goedec685e202017-02-09 16:44:13 +01005086static enum led_brightness kbdlight_brightness;
5087static DEFINE_MUTEX(kbdlight_mutex);
5088
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005089static int kbdlight_set_level(int level)
5090{
Hans de Goedec685e202017-02-09 16:44:13 +01005091 int ret = 0;
5092
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005093 if (!hkey_handle)
5094 return -ENXIO;
5095
Hans de Goedec685e202017-02-09 16:44:13 +01005096 mutex_lock(&kbdlight_mutex);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005097
Hans de Goedec685e202017-02-09 16:44:13 +01005098 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5099 ret = -EIO;
5100 else
5101 kbdlight_brightness = level;
5102
5103 mutex_unlock(&kbdlight_mutex);
5104
5105 return ret;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005106}
5107
5108static int kbdlight_get_level(void)
5109{
5110 int status = 0;
5111
5112 if (!hkey_handle)
5113 return -ENXIO;
5114
5115 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5116 return -EIO;
5117
5118 if (status < 0)
5119 return status;
5120
5121 return status & 0x3;
5122}
5123
5124static bool kbdlight_is_supported(void)
5125{
5126 int status = 0;
5127
5128 if (!hkey_handle)
5129 return false;
5130
5131 if (!acpi_has_method(hkey_handle, "MLCG")) {
5132 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5133 return false;
5134 }
5135
5136 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5137 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5138 return false;
5139 }
5140
5141 if (status < 0) {
5142 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5143 return false;
5144 }
5145
5146 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5147 /*
5148 * Guessed test for keyboard backlight:
5149 *
5150 * Machines with backlight keyboard return:
5151 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5152 * b110100010010000000XX - ThinkPad x230
5153 * b010100000010000000XX - ThinkPad x240
5154 * b010100000010000000XX - ThinkPad W541
5155 * (XX is current backlight level)
5156 *
5157 * Machines without backlight keyboard return:
5158 * b10100001000000000000 - ThinkPad x230
5159 * b10110001000000000000 - ThinkPad E430
5160 * b00000000000000000000 - ThinkPad E450
5161 *
5162 * Candidate BITs for detection test (XOR):
5163 * b01000000001000000000
5164 * ^
5165 */
5166 return status & BIT(9);
5167}
5168
Hans de Goede86ec0c22017-02-09 16:44:12 +01005169static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005170 enum led_brightness brightness)
5171{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005172 return kbdlight_set_level(brightness);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005173}
5174
5175static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5176{
5177 int level;
5178
5179 level = kbdlight_get_level();
5180 if (level < 0)
5181 return 0;
5182
5183 return level;
5184}
5185
5186static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5187 .led_classdev = {
5188 .name = "tpacpi::kbd_backlight",
5189 .max_brightness = 2,
Hans de Goedec685e202017-02-09 16:44:13 +01005190 .flags = LED_BRIGHT_HW_CHANGED,
Hans de Goede86ec0c22017-02-09 16:44:12 +01005191 .brightness_set_blocking = &kbdlight_sysfs_set,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005192 .brightness_get = &kbdlight_sysfs_get,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005193 }
5194};
5195
5196static int __init kbdlight_init(struct ibm_init_struct *iibm)
5197{
5198 int rc;
5199
5200 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5201
5202 TPACPI_ACPIHANDLE_INIT(hkey);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005203
5204 if (!kbdlight_is_supported()) {
5205 tp_features.kbdlight = 0;
5206 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5207 return 1;
5208 }
5209
Hans de Goedec685e202017-02-09 16:44:13 +01005210 kbdlight_brightness = kbdlight_sysfs_get(NULL);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005211 tp_features.kbdlight = 1;
5212
5213 rc = led_classdev_register(&tpacpi_pdev->dev,
5214 &tpacpi_led_kbdlight.led_classdev);
5215 if (rc < 0) {
5216 tp_features.kbdlight = 0;
5217 return rc;
5218 }
5219
Hans de Goedec685e202017-02-09 16:44:13 +01005220 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5221 TP_ACPI_HKEY_KBD_LIGHT_MASK);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005222 return 0;
5223}
5224
5225static void kbdlight_exit(void)
5226{
5227 if (tp_features.kbdlight)
5228 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005229}
5230
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005231static int kbdlight_set_level_and_update(int level)
5232{
5233 int ret;
5234 struct led_classdev *led_cdev;
5235
5236 ret = kbdlight_set_level(level);
5237 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5238
5239 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5240 led_cdev->brightness = level;
5241
5242 return ret;
5243}
5244
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005245static int kbdlight_read(struct seq_file *m)
5246{
5247 int level;
5248
5249 if (!tp_features.kbdlight) {
5250 seq_printf(m, "status:\t\tnot supported\n");
5251 } else {
5252 level = kbdlight_get_level();
5253 if (level < 0)
5254 seq_printf(m, "status:\t\terror %d\n", level);
5255 else
5256 seq_printf(m, "status:\t\t%d\n", level);
5257 seq_printf(m, "commands:\t0, 1, 2\n");
5258 }
5259
5260 return 0;
5261}
5262
5263static int kbdlight_write(char *buf)
5264{
5265 char *cmd;
5266 int level = -1;
5267
5268 if (!tp_features.kbdlight)
5269 return -ENODEV;
5270
5271 while ((cmd = next_cmd(&buf))) {
5272 if (strlencmp(cmd, "0") == 0)
5273 level = 0;
5274 else if (strlencmp(cmd, "1") == 0)
5275 level = 1;
5276 else if (strlencmp(cmd, "2") == 0)
5277 level = 2;
5278 else
5279 return -EINVAL;
5280 }
5281
5282 if (level == -1)
5283 return -EINVAL;
5284
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005285 return kbdlight_set_level_and_update(level);
5286}
5287
5288static void kbdlight_suspend(void)
5289{
5290 struct led_classdev *led_cdev;
5291
5292 if (!tp_features.kbdlight)
5293 return;
5294
5295 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5296 led_update_brightness(led_cdev);
5297 led_classdev_suspend(led_cdev);
5298}
5299
5300static void kbdlight_resume(void)
5301{
5302 if (!tp_features.kbdlight)
5303 return;
5304
5305 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005306}
5307
5308static struct ibm_struct kbdlight_driver_data = {
5309 .name = "kbdlight",
5310 .read = kbdlight_read,
5311 .write = kbdlight_write,
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005312 .suspend = kbdlight_suspend,
5313 .resume = kbdlight_resume,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005314 .exit = kbdlight_exit,
5315};
5316
5317/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005318 * Light (thinklight) subdriver
5319 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005321TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5322TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005323
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005324static int light_get_status(void)
5325{
5326 int status = 0;
5327
5328 if (tp_features.light_status) {
5329 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5330 return -EIO;
5331 return (!!status);
5332 }
5333
5334 return -ENXIO;
5335}
5336
5337static int light_set_status(int status)
5338{
5339 int rc;
5340
5341 if (tp_features.light) {
5342 if (cmos_handle) {
5343 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005344 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005345 TP_CMOS_THINKLIGHT_ON :
5346 TP_CMOS_THINKLIGHT_OFF);
5347 } else {
5348 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005349 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005350 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005351 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005352 }
5353
5354 return -ENXIO;
5355}
5356
Hans de Goede86ec0c22017-02-09 16:44:12 +01005357static int light_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005358 enum led_brightness brightness)
5359{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005360 return light_set_status((brightness != LED_OFF) ?
5361 TPACPI_LED_ON : TPACPI_LED_OFF);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005362}
5363
5364static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5365{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005366 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005367}
5368
5369static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5370 .led_classdev = {
5371 .name = "tpacpi::thinklight",
Hans de Goede86ec0c22017-02-09 16:44:12 +01005372 .brightness_set_blocking = &light_sysfs_set,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005373 .brightness_get = &light_sysfs_get,
5374 }
5375};
5376
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005377static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005379 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005380
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005381 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5382
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005383 if (tpacpi_is_ibm()) {
5384 TPACPI_ACPIHANDLE_INIT(ledb);
5385 TPACPI_ACPIHANDLE_INIT(lght);
5386 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005387 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005388
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005389 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005390 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005391
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005392 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005393 /* light status not supported on
5394 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005395 tp_features.light_status =
5396 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005398 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5399 str_supported(tp_features.light),
5400 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005401
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005402 if (!tp_features.light)
5403 return 1;
5404
5405 rc = led_classdev_register(&tpacpi_pdev->dev,
5406 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005407
5408 if (rc < 0) {
5409 tp_features.light = 0;
5410 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005411 } else {
5412 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005413 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005414
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005415 return rc;
5416}
5417
5418static void light_exit(void)
5419{
5420 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421}
5422
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005423static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005425 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005427 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005428 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005429 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005430 seq_printf(m, "status:\t\tunknown\n");
5431 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005432 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005433 status = light_get_status();
5434 if (status < 0)
5435 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005436 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5437 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441}
5442
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005443static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005446 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005447
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005448 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005449 return -ENODEV;
5450
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 while ((cmd = next_cmd(&buf))) {
5452 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005453 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005455 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 } else
5457 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 }
5459
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005460 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461}
5462
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005463static struct ibm_struct light_driver_data = {
5464 .name = "light",
5465 .read = light_read,
5466 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005467 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005468};
5469
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005470/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005471 * CMOS subdriver
5472 */
5473
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005474/* sysfs cmos_command -------------------------------------------------- */
5475static ssize_t cmos_command_store(struct device *dev,
5476 struct device_attribute *attr,
5477 const char *buf, size_t count)
5478{
5479 unsigned long cmos_cmd;
5480 int res;
5481
5482 if (parse_strtoul(buf, 21, &cmos_cmd))
5483 return -EINVAL;
5484
5485 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005486 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005487}
5488
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01005489static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005490
5491/* --------------------------------------------------------------------- */
5492
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005493static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005494{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005495 int res;
5496
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005497 vdbg_printk(TPACPI_DBG_INIT,
5498 "initializing cmos commands subdriver\n");
5499
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005500 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005501
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005502 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5503 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005504
5505 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5506 if (res)
5507 return res;
5508
Jan van den Berg72a979f2014-09-17 00:01:08 +02005509 return (cmos_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005510}
5511
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005512static void cmos_exit(void)
5513{
5514 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5515}
5516
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005517static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005519 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5520 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005522 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005524 seq_printf(m, "status:\t\tsupported\n");
5525 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 }
5527
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529}
5530
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005531static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532{
5533 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005534 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
5536 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005537 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5538 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 /* cmos_cmd set */
5540 } else
5541 return -EINVAL;
5542
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005543 res = issue_thinkpad_cmos_command(cmos_cmd);
5544 if (res)
5545 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 }
5547
5548 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005549}
5550
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005551static struct ibm_struct cmos_driver_data = {
5552 .name = "cmos",
5553 .read = cmos_read,
5554 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005555 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005556};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005557
5558/*************************************************************************
5559 * LED subdriver
5560 */
5561
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005562enum led_access_mode {
5563 TPACPI_LED_NONE = 0,
5564 TPACPI_LED_570, /* 570 */
5565 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5566 TPACPI_LED_NEW, /* all others */
5567};
5568
5569enum { /* For TPACPI_LED_OLD */
5570 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5571 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5572 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5573};
5574
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005575static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005576
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005577static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005578
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005579#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005580static struct tpacpi_led_classdev *tpacpi_leds;
5581static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005582static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005583 /* there's a limit of 19 chars + NULL before 2.6.26 */
5584 "tpacpi::power",
5585 "tpacpi:orange:batt",
5586 "tpacpi:green:batt",
5587 "tpacpi::dock_active",
5588 "tpacpi::bay_active",
5589 "tpacpi::dock_batt",
5590 "tpacpi::unknown_led",
5591 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005592 "tpacpi::dock_status1",
5593 "tpacpi::dock_status2",
5594 "tpacpi::unknown_led2",
5595 "tpacpi::unknown_led3",
5596 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005597};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005598#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005599
5600static inline bool tpacpi_is_led_restricted(const unsigned int led)
5601{
5602#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5603 return false;
5604#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005605 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005606#endif
5607}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005608
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005609static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005610{
5611 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005612 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005613
5614 switch (led_supported) {
5615 case TPACPI_LED_570:
5616 if (!acpi_evalf(ec_handle,
5617 &status, "GLED", "dd", 1 << led))
5618 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005619 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005620 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005621 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005622 TPACPI_LED_ON :
5623 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005624 tpacpi_led_state_cache[led] = led_s;
5625 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005626 default:
5627 return -ENXIO;
5628 }
5629
5630 /* not reached */
5631}
5632
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005633static int led_set_status(const unsigned int led,
5634 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005635{
5636 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005637 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5638 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005639
5640 int rc = 0;
5641
5642 switch (led_supported) {
5643 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005644 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005645 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005646 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005647 if (unlikely(tpacpi_is_led_restricted(led)))
5648 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005649 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5650 (1 << led), led_sled_arg1[ledstatus]))
5651 rc = -EIO;
5652 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005653 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005654 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005655 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005656 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005657 if (unlikely(tpacpi_is_led_restricted(led)))
5658 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005659 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5660 if (rc >= 0)
5661 rc = ec_write(TPACPI_LED_EC_HLBL,
5662 (ledstatus == TPACPI_LED_BLINK) << led);
5663 if (rc >= 0)
5664 rc = ec_write(TPACPI_LED_EC_HLCL,
5665 (ledstatus != TPACPI_LED_OFF) << led);
5666 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005667 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005668 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005669 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5670 return -EINVAL;
5671 if (unlikely(tpacpi_is_led_restricted(led)))
5672 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005673 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5674 led, led_led_arg1[ledstatus]))
5675 rc = -EIO;
5676 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005677 default:
5678 rc = -ENXIO;
5679 }
5680
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005681 if (!rc)
5682 tpacpi_led_state_cache[led] = ledstatus;
5683
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005684 return rc;
5685}
5686
Hans de Goede86ec0c22017-02-09 16:44:12 +01005687static int led_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005688 enum led_brightness brightness)
5689{
5690 struct tpacpi_led_classdev *data = container_of(led_cdev,
5691 struct tpacpi_led_classdev, led_classdev);
Hans de Goede86ec0c22017-02-09 16:44:12 +01005692 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005693
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005694 if (brightness == LED_OFF)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005695 new_state = TPACPI_LED_OFF;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005696 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005697 new_state = TPACPI_LED_ON;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005698 else
Hans de Goede86ec0c22017-02-09 16:44:12 +01005699 new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005700
Hans de Goede86ec0c22017-02-09 16:44:12 +01005701 return led_set_status(data->led, new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005702}
5703
5704static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5705 unsigned long *delay_on, unsigned long *delay_off)
5706{
5707 struct tpacpi_led_classdev *data = container_of(led_cdev,
5708 struct tpacpi_led_classdev, led_classdev);
5709
5710 /* Can we choose the flash rate? */
5711 if (*delay_on == 0 && *delay_off == 0) {
5712 /* yes. set them to the hardware blink rate (1 Hz) */
5713 *delay_on = 500; /* ms */
5714 *delay_off = 500; /* ms */
5715 } else if ((*delay_on != 500) || (*delay_off != 500))
5716 return -EINVAL;
5717
Hans de Goede86ec0c22017-02-09 16:44:12 +01005718 return led_set_status(data->led, TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005719}
5720
5721static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5722{
5723 int rc;
5724
5725 struct tpacpi_led_classdev *data = container_of(led_cdev,
5726 struct tpacpi_led_classdev, led_classdev);
5727
5728 rc = led_get_status(data->led);
5729
5730 if (rc == TPACPI_LED_OFF || rc < 0)
5731 rc = LED_OFF; /* no error handling in led class :( */
5732 else
5733 rc = LED_FULL;
5734
5735 return rc;
5736}
5737
5738static void led_exit(void)
5739{
5740 unsigned int i;
5741
5742 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5743 if (tpacpi_leds[i].led_classdev.name)
5744 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5745 }
5746
5747 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005748}
5749
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005750static int __init tpacpi_init_led(unsigned int led)
5751{
5752 int rc;
5753
5754 tpacpi_leds[led].led = led;
5755
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005756 /* LEDs with no name don't get registered */
5757 if (!tpacpi_led_names[led])
5758 return 0;
5759
Hans de Goede86ec0c22017-02-09 16:44:12 +01005760 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005761 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5762 if (led_supported == TPACPI_LED_570)
5763 tpacpi_leds[led].led_classdev.brightness_get =
5764 &led_sysfs_get;
5765
5766 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5767
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005768 rc = led_classdev_register(&tpacpi_pdev->dev,
5769 &tpacpi_leds[led].led_classdev);
5770 if (rc < 0)
5771 tpacpi_leds[led].led_classdev.name = NULL;
5772
5773 return rc;
5774}
5775
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005776static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5777 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5778 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5779 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5780
5781 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5782 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5783 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5784 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5785 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5786 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5787 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5788 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5789
5790 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5791 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5792 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5793 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5794 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5795
5796 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5797 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5798 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5799 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5800
5801 /* (1) - may have excess leds enabled on MSB */
5802
5803 /* Defaults (order matters, keep last, don't reorder!) */
5804 { /* Lenovo */
5805 .vendor = PCI_VENDOR_ID_LENOVO,
5806 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5807 .quirks = 0x1fffU,
5808 },
5809 { /* IBM ThinkPads with no EC version string */
5810 .vendor = PCI_VENDOR_ID_IBM,
5811 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5812 .quirks = 0x00ffU,
5813 },
5814 { /* IBM ThinkPads with EC version string */
5815 .vendor = PCI_VENDOR_ID_IBM,
5816 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5817 .quirks = 0x00bfU,
5818 },
5819};
5820
5821#undef TPACPI_LEDQ_IBM
5822#undef TPACPI_LEDQ_LNV
5823
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005824static enum led_access_mode __init led_init_detect_mode(void)
5825{
5826 acpi_status status;
5827
5828 if (tpacpi_is_ibm()) {
5829 /* 570 */
5830 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5831 if (ACPI_SUCCESS(status))
5832 return TPACPI_LED_570;
5833
5834 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5835 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5836 if (ACPI_SUCCESS(status))
5837 return TPACPI_LED_OLD;
5838 }
5839
5840 /* most others */
5841 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5842 if (ACPI_SUCCESS(status))
5843 return TPACPI_LED_NEW;
5844
5845 /* R30, R31, and unknown firmwares */
5846 led_handle = NULL;
5847 return TPACPI_LED_NONE;
5848}
5849
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005850static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005851{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005852 unsigned int i;
5853 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005854 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005855
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005856 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5857
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005858 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005859
Adam Leefcb44e12013-06-07 16:20:08 +08005860 if (led_supported != TPACPI_LED_NONE) {
5861 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5862 ARRAY_SIZE(led_useful_qtable));
5863
5864 if (!useful_leds) {
5865 led_handle = NULL;
5866 led_supported = TPACPI_LED_NONE;
5867 }
5868 }
5869
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005870 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5871 str_supported(led_supported), led_supported);
5872
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005873 if (led_supported == TPACPI_LED_NONE)
5874 return 1;
5875
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005876 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5877 GFP_KERNEL);
5878 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005879 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005880 return -ENOMEM;
5881 }
5882
5883 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08005884 tpacpi_leds[i].led = -1;
5885
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005886 if (!tpacpi_is_led_restricted(i) &&
5887 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005888 rc = tpacpi_init_led(i);
5889 if (rc < 0) {
5890 led_exit();
5891 return rc;
5892 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005893 }
5894 }
5895
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005896#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Joe Perches0978e012011-04-04 10:06:25 -07005897 pr_notice("warning: userspace override of important "
5898 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005899#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005900 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005901}
5902
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005903#define str_led_status(s) \
5904 ((s) == TPACPI_LED_OFF ? "off" : \
5905 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005906
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005907static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005909 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005910 seq_printf(m, "status:\t\tnot supported\n");
5911 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005912 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005913 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005914
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005915 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005916 /* 570 */
5917 int i, status;
5918 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005919 status = led_get_status(i);
5920 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005921 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005922 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005923 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005924 }
5925 }
5926
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005927 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005928 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005930 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931}
5932
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005933static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934{
5935 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005936 int led, rc;
5937 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005938
5939 if (!led_supported)
5940 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
5942 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08005943 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 return -EINVAL;
5945
Adam Leeedf2d772013-06-08 16:51:15 +08005946 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5947 tpacpi_leds[led].led < 0)
5948 return -ENODEV;
5949
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005950 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005951 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005953 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005954 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005955 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005956 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005957 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005958 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005959
5960 rc = led_set_status(led, s);
5961 if (rc < 0)
5962 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 }
5964
5965 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005966}
5967
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005968static struct ibm_struct led_driver_data = {
5969 .name = "led",
5970 .read = led_read,
5971 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005972 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005973};
5974
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005975/*************************************************************************
5976 * Beep subdriver
5977 */
5978
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005979TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005980
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005981#define TPACPI_BEEP_Q1 0x0001
5982
5983static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5984 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5985 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5986};
5987
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005988static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005989{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005990 unsigned long quirks;
5991
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005992 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5993
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005994 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005995
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005996 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5997 str_supported(beep_handle != NULL));
5998
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005999 quirks = tpacpi_check_quirks(beep_quirk_table,
6000 ARRAY_SIZE(beep_quirk_table));
6001
6002 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6003
Jan van den Berg72a979f2014-09-17 00:01:08 +02006004 return (beep_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006005}
6006
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006007static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006009 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006010 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006011 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006012 seq_printf(m, "status:\t\tsupported\n");
6013 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006016 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017}
6018
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006019static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020{
6021 char *cmd;
6022 int beep_cmd;
6023
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006024 if (!beep_handle)
6025 return -ENODEV;
6026
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006028 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6029 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 /* beep_cmd set */
6031 } else
6032 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006033 if (tp_features.beep_needs_two_args) {
6034 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6035 beep_cmd, 0))
6036 return -EIO;
6037 } else {
6038 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6039 beep_cmd))
6040 return -EIO;
6041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 }
6043
6044 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006045}
6046
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006047static struct ibm_struct beep_driver_data = {
6048 .name = "beep",
6049 .read = beep_read,
6050 .write = beep_write,
6051};
6052
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006053/*************************************************************************
6054 * Thermal subdriver
6055 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006056
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006057enum thermal_access_mode {
6058 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6059 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6060 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6061 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6062 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6063};
6064
6065enum { /* TPACPI_THERMAL_TPEC_* */
6066 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6067 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6068 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006069
6070 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006071};
6072
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006073
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006074#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6075struct ibm_thermal_sensors_struct {
6076 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6077};
6078
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006079static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006080
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006081/* idx is zero-based */
6082static int thermal_get_sensor(int idx, s32 *value)
6083{
6084 int t;
6085 s8 tmp;
6086 char tmpi[5];
6087
6088 t = TP_EC_THERMAL_TMP0;
6089
6090 switch (thermal_read_mode) {
6091#if TPACPI_MAX_THERMAL_SENSORS >= 16
6092 case TPACPI_THERMAL_TPEC_16:
6093 if (idx >= 8 && idx <= 15) {
6094 t = TP_EC_THERMAL_TMP8;
6095 idx -= 8;
6096 }
6097 /* fallthrough */
6098#endif
6099 case TPACPI_THERMAL_TPEC_8:
6100 if (idx <= 7) {
6101 if (!acpi_ec_read(t + idx, &tmp))
6102 return -EIO;
6103 *value = tmp * 1000;
6104 return 0;
6105 }
6106 break;
6107
6108 case TPACPI_THERMAL_ACPI_UPDT:
6109 if (idx <= 7) {
6110 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6111 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6112 return -EIO;
6113 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6114 return -EIO;
6115 *value = (t - 2732) * 100;
6116 return 0;
6117 }
6118 break;
6119
6120 case TPACPI_THERMAL_ACPI_TMP07:
6121 if (idx <= 7) {
6122 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6123 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6124 return -EIO;
6125 if (t > 127 || t < -127)
6126 t = TP_EC_THERMAL_TMP_NA;
6127 *value = t * 1000;
6128 return 0;
6129 }
6130 break;
6131
6132 case TPACPI_THERMAL_NONE:
6133 default:
6134 return -ENOSYS;
6135 }
6136
6137 return -EINVAL;
6138}
6139
6140static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6141{
6142 int res, i;
6143 int n;
6144
6145 n = 8;
6146 i = 0;
6147
6148 if (!s)
6149 return -EINVAL;
6150
6151 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6152 n = 16;
6153
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006154 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006155 res = thermal_get_sensor(i, &s->temp[i]);
6156 if (res)
6157 return res;
6158 }
6159
6160 return n;
6161}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006162
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006163static void thermal_dump_all_sensors(void)
6164{
6165 int n, i;
6166 struct ibm_thermal_sensors_struct t;
6167
6168 n = thermal_get_sensors(&t);
6169 if (n <= 0)
6170 return;
6171
Joe Perches0978e012011-04-04 10:06:25 -07006172 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006173
6174 for (i = 0; i < n; i++) {
6175 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07006176 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006177 else
Joe Perches0978e012011-04-04 10:06:25 -07006178 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006179 }
6180
Joe Perches0978e012011-04-04 10:06:25 -07006181 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006182}
6183
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006184/* sysfs temp##_input -------------------------------------------------- */
6185
6186static ssize_t thermal_temp_input_show(struct device *dev,
6187 struct device_attribute *attr,
6188 char *buf)
6189{
6190 struct sensor_device_attribute *sensor_attr =
6191 to_sensor_dev_attr(attr);
6192 int idx = sensor_attr->index;
6193 s32 value;
6194 int res;
6195
6196 res = thermal_get_sensor(idx, &value);
6197 if (res)
6198 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006199 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006200 return -ENXIO;
6201
6202 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6203}
6204
6205#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006206 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6207 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006208
6209static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6210 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6211 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6212 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6213 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6214 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6215 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6216 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6217 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6218 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6219 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6220 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6221 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6222 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6223 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6224 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6225 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6226};
6227
6228#define THERMAL_ATTRS(X) \
6229 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6230
6231static struct attribute *thermal_temp_input_attr[] = {
6232 THERMAL_ATTRS(8),
6233 THERMAL_ATTRS(9),
6234 THERMAL_ATTRS(10),
6235 THERMAL_ATTRS(11),
6236 THERMAL_ATTRS(12),
6237 THERMAL_ATTRS(13),
6238 THERMAL_ATTRS(14),
6239 THERMAL_ATTRS(15),
6240 THERMAL_ATTRS(0),
6241 THERMAL_ATTRS(1),
6242 THERMAL_ATTRS(2),
6243 THERMAL_ATTRS(3),
6244 THERMAL_ATTRS(4),
6245 THERMAL_ATTRS(5),
6246 THERMAL_ATTRS(6),
6247 THERMAL_ATTRS(7),
6248 NULL
6249};
6250
6251static const struct attribute_group thermal_temp_input16_group = {
6252 .attrs = thermal_temp_input_attr
6253};
6254
6255static const struct attribute_group thermal_temp_input8_group = {
6256 .attrs = &thermal_temp_input_attr[8]
6257};
6258
6259#undef THERMAL_SENSOR_ATTR_TEMP
6260#undef THERMAL_ATTRS
6261
6262/* --------------------------------------------------------------------- */
6263
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006264static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006265{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006266 u8 t, ta1, ta2;
6267 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006268 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006269 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006270
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006271 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6272
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006273 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006274
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03006275 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006276 /*
6277 * Direct EC access mode: sensors at registers
6278 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6279 * non-implemented, thermal sensors return 0x80 when
6280 * not available
6281 */
6282
6283 ta1 = ta2 = 0;
6284 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006285 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006286 ta1 |= t;
6287 } else {
6288 ta1 = 0;
6289 break;
6290 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006291 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006292 ta2 |= t;
6293 } else {
6294 ta1 = 0;
6295 break;
6296 }
6297 }
6298 if (ta1 == 0) {
6299 /* This is sheer paranoia, but we handle it anyway */
6300 if (acpi_tmp7) {
Joe Perches0978e012011-04-04 10:06:25 -07006301 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006302 "falling back to ACPI TMPx access "
6303 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006304 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006305 } else {
Joe Perches0978e012011-04-04 10:06:25 -07006306 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006307 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006308 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006309 }
6310 } else {
6311 thermal_read_mode =
6312 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006313 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006314 }
6315 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006316 if (tpacpi_is_ibm() &&
6317 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006318 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006319 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006320 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006321 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006322 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006323 }
6324 } else {
6325 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006326 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006327 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006328
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006329 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6330 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6331 thermal_read_mode);
6332
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006333 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006334 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006335 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006336 &thermal_temp_input16_group);
6337 if (res)
6338 return res;
6339 break;
6340 case TPACPI_THERMAL_TPEC_8:
6341 case TPACPI_THERMAL_ACPI_TMP07:
6342 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006343 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006344 &thermal_temp_input8_group);
6345 if (res)
6346 return res;
6347 break;
6348 case TPACPI_THERMAL_NONE:
6349 default:
6350 return 1;
6351 }
6352
6353 return 0;
6354}
6355
6356static void thermal_exit(void)
6357{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006358 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006359 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006360 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006361 &thermal_temp_input16_group);
6362 break;
6363 case TPACPI_THERMAL_TPEC_8:
6364 case TPACPI_THERMAL_ACPI_TMP07:
6365 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006366 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08006367 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006368 break;
6369 case TPACPI_THERMAL_NONE:
6370 default:
6371 break;
6372 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006373}
6374
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006375static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006376{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006377 int n, i;
6378 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006379
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006380 n = thermal_get_sensors(&t);
6381 if (unlikely(n < 0))
6382 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006383
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006384 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006385
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006386 if (n > 0) {
6387 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006388 seq_printf(m, "%d ", t.temp[i] / 1000);
6389 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006390 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006391 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006392
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006393 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006394}
6395
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006396static struct ibm_struct thermal_driver_data = {
6397 .name = "thermal",
6398 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006399 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006400};
6401
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006402/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006403 * Backlight/brightness subdriver
6404 */
Holger Macht8acb0252006-10-20 14:30:28 -07006405
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006406#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6407
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006408/*
6409 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6410 * CMOS NVRAM byte 0x5E, bits 0-3.
6411 *
6412 * EC HBRV (0x31) has the following layout
6413 * Bit 7: unknown function
6414 * Bit 6: unknown function
6415 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6416 * Bit 4: must be set to zero to avoid problems
6417 * Bit 3-0: backlight brightness level
6418 *
6419 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006420 *
6421 * WARNING: The X61 has been verified to use HBRV for something else, so
6422 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6423 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006424 */
6425
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006426enum {
6427 TP_EC_BACKLIGHT = 0x31,
6428
6429 /* TP_EC_BACKLIGHT bitmasks */
6430 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6431 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6432 TP_EC_BACKLIGHT_MAPSW = 0x20,
6433};
6434
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006435enum tpacpi_brightness_access_mode {
6436 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6437 TPACPI_BRGHT_MODE_EC, /* EC control */
6438 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6439 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6440 TPACPI_BRGHT_MODE_MAX
6441};
6442
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006443static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006444
6445static enum tpacpi_brightness_access_mode brightness_mode =
6446 TPACPI_BRGHT_MODE_MAX;
6447
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006448static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6449
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006450static struct mutex brightness_mutex;
6451
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006452/* NVRAM brightness access,
6453 * call with brightness_mutex held! */
6454static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006455{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006456 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006457
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006458 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6459 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6460 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006461 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006462
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006463 return lnvram;
6464}
6465
6466static void tpacpi_brightness_checkpoint_nvram(void)
6467{
6468 u8 lec = 0;
6469 u8 b_nvram;
6470
6471 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6472 return;
6473
6474 vdbg_printk(TPACPI_DBG_BRGHT,
6475 "trying to checkpoint backlight level to NVRAM...\n");
6476
6477 if (mutex_lock_killable(&brightness_mutex) < 0)
6478 return;
6479
6480 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6481 goto unlock;
6482 lec &= TP_EC_BACKLIGHT_LVLMSK;
6483 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6484
6485 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6486 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6487 /* NVRAM needs update */
6488 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6489 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6490 b_nvram |= lec;
6491 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6492 dbg_printk(TPACPI_DBG_BRGHT,
6493 "updated NVRAM backlight level to %u (0x%02x)\n",
6494 (unsigned int) lec, (unsigned int) b_nvram);
6495 } else
6496 vdbg_printk(TPACPI_DBG_BRGHT,
6497 "NVRAM backlight level already is %u (0x%02x)\n",
6498 (unsigned int) lec, (unsigned int) b_nvram);
6499
6500unlock:
6501 mutex_unlock(&brightness_mutex);
6502}
6503
6504
6505/* call with brightness_mutex held! */
6506static int tpacpi_brightness_get_raw(int *status)
6507{
6508 u8 lec = 0;
6509
6510 switch (brightness_mode) {
6511 case TPACPI_BRGHT_MODE_UCMS_STEP:
6512 *status = tpacpi_brightness_nvram_get();
6513 return 0;
6514 case TPACPI_BRGHT_MODE_EC:
6515 case TPACPI_BRGHT_MODE_ECNVRAM:
6516 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006517 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006518 *status = lec;
6519 return 0;
6520 default:
6521 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006522 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006523}
6524
6525/* call with brightness_mutex held! */
6526/* do NOT call with illegal backlight level value */
6527static int tpacpi_brightness_set_ec(unsigned int value)
6528{
6529 u8 lec = 0;
6530
6531 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6532 return -EIO;
6533
6534 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6535 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6536 (value & TP_EC_BACKLIGHT_LVLMSK))))
6537 return -EIO;
6538
6539 return 0;
6540}
6541
6542/* call with brightness_mutex held! */
6543static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6544{
6545 int cmos_cmd, inc;
6546 unsigned int current_value, i;
6547
6548 current_value = tpacpi_brightness_nvram_get();
6549
6550 if (value == current_value)
6551 return 0;
6552
6553 cmos_cmd = (value > current_value) ?
6554 TP_CMOS_BRIGHTNESS_UP :
6555 TP_CMOS_BRIGHTNESS_DOWN;
6556 inc = (value > current_value) ? 1 : -1;
6557
6558 for (i = current_value; i != value; i += inc)
6559 if (issue_thinkpad_cmos_command(cmos_cmd))
6560 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006561
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006562 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006563}
6564
6565/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006566static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006567{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006568 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006569
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006570 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006571 return -EINVAL;
6572
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006573 vdbg_printk(TPACPI_DBG_BRGHT,
6574 "set backlight level to %d\n", value);
6575
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006576 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006577 if (res < 0)
6578 return res;
6579
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006580 switch (brightness_mode) {
6581 case TPACPI_BRGHT_MODE_EC:
6582 case TPACPI_BRGHT_MODE_ECNVRAM:
6583 res = tpacpi_brightness_set_ec(value);
6584 break;
6585 case TPACPI_BRGHT_MODE_UCMS_STEP:
6586 res = tpacpi_brightness_set_ucmsstep(value);
6587 break;
6588 default:
6589 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006590 }
6591
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006592 mutex_unlock(&brightness_mutex);
6593 return res;
6594}
6595
6596/* sysfs backlight class ----------------------------------------------- */
6597
6598static int brightness_update_status(struct backlight_device *bd)
6599{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006600 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006601 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6602 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006603 bd->props.brightness : 0;
6604
6605 dbg_printk(TPACPI_DBG_BRGHT,
6606 "backlight: attempt to set level to %d\n",
6607 level);
6608
6609 /* it is the backlight class's job (caller) to handle
6610 * EINTR and other errors properly */
6611 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006612}
Holger Macht8acb0252006-10-20 14:30:28 -07006613
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006614static int brightness_get(struct backlight_device *bd)
6615{
6616 int status, res;
6617
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006618 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006619 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006620 return 0;
6621
6622 res = tpacpi_brightness_get_raw(&status);
6623
6624 mutex_unlock(&brightness_mutex);
6625
6626 if (res < 0)
6627 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006628
6629 return status & TP_EC_BACKLIGHT_LVLMSK;
6630}
6631
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006632static void tpacpi_brightness_notify_change(void)
6633{
6634 backlight_force_update(ibm_backlight_device,
6635 BACKLIGHT_UPDATE_HOTKEY);
6636}
6637
Lionel Debrouxacc24722010-11-16 14:14:02 +01006638static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006639 .get_brightness = brightness_get,
6640 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006641};
6642
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006643/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006644
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006645/*
6646 * Call _BCL method of video device. On some ThinkPads this will
6647 * switch the firmware to the ACPI brightness control mode.
6648 */
6649
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006650static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6651{
6652 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6653 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006654 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006655 int rc;
6656
Aaron Lu46445b62013-10-11 21:27:46 +08006657 if (acpi_bus_get_device(handle, &device))
6658 return 0;
6659
6660 rc = 0;
6661 list_for_each_entry(child, &device->children, node) {
6662 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6663 NULL, &buffer);
6664 if (ACPI_FAILURE(status))
6665 continue;
6666
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006667 obj = (union acpi_object *)buffer.pointer;
6668 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006669 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006670 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006671 rc = 0;
6672 } else {
6673 rc = obj->package.count;
6674 }
Aaron Lu46445b62013-10-11 21:27:46 +08006675 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006676 }
6677
6678 kfree(buffer.pointer);
6679 return rc;
6680}
6681
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006682
6683/*
6684 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6685 */
6686static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6687{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006688 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006689 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006690
Aaron Lu46445b62013-10-11 21:27:46 +08006691 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006692 if (video_device)
6693 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006694
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006695 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006696
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006697 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006698}
6699
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006700/*
6701 * These are only useful for models that have only one possibility
6702 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6703 * these quirks.
6704 */
6705#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6706#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6707#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6708
6709static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6710 /* Models with ATI GPUs known to require ECNVRAM mode */
6711 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6712
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006713 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006714 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006715 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006716 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006717 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6718
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006719 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006720 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006721 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6722 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006723
6724 /* Models with Intel GMA900 */
6725 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6726 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6727 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6728};
6729
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006730/*
6731 * Returns < 0 for error, otherwise sets tp_features.bright_*
6732 * and bright_maxlvl.
6733 */
6734static void __init tpacpi_detect_brightness_capabilities(void)
6735{
6736 unsigned int b;
6737
6738 vdbg_printk(TPACPI_DBG_INIT,
6739 "detecting firmware brightness interface capabilities\n");
6740
6741 /* we could run a quirks check here (same table used by
6742 * brightness_init) if needed */
6743
6744 /*
6745 * We always attempt to detect acpi support, so as to switch
6746 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6747 * going to publish a backlight interface
6748 */
6749 b = tpacpi_check_std_acpi_brightness_support();
6750 switch (b) {
6751 case 16:
6752 bright_maxlvl = 15;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006753 break;
6754 case 8:
6755 case 0:
6756 bright_maxlvl = 7;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006757 break;
6758 default:
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006759 tp_features.bright_unkfw = 1;
6760 bright_maxlvl = b - 1;
6761 }
Eric Curtin15c75622016-01-30 16:55:59 +00006762 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006763}
6764
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006765static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006766{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006767 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006768 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006769 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006770
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006771 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6772
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006773 mutex_init(&brightness_mutex);
6774
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006775 quirks = tpacpi_check_quirks(brightness_quirk_table,
6776 ARRAY_SIZE(brightness_quirk_table));
6777
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006778 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006779
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006780 /* if it is unknown, we don't handle it: it wouldn't be safe */
6781 if (tp_features.bright_unkfw)
6782 return 1;
6783
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006784 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006785 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006786 "brightness support disabled by "
6787 "module parameter\n");
6788 return 1;
6789 }
6790
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02006791 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006792 if (brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006793 pr_info("Standard ACPI backlight interface "
6794 "available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006795 return 1;
6796 } else if (brightness_enable == 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006797 pr_warn("Cannot enable backlight brightness support, "
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006798 "ACPI is already handling it. Refer to the "
Joe Perches0978e012011-04-04 10:06:25 -07006799 "acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006800 return 1;
6801 }
6802 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006803 pr_notice("Standard ACPI backlight interface not "
6804 "available, thinkpad_acpi native "
6805 "brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006806 }
6807
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006808 /*
6809 * Check for module parameter bogosity, note that we
6810 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6811 * able to detect "unspecified"
6812 */
6813 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006814 return -EINVAL;
6815
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006816 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6817 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6818 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006819 if (quirks & TPACPI_BRGHT_Q_EC)
6820 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6821 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006822 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6823
6824 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006825 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006826 brightness_mode);
6827 }
6828
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006829 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006830 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006831 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6832 brightness_mode == TPACPI_BRGHT_MODE_EC))
6833 return -EINVAL;
6834
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006835 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006836 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006837
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006838 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006839 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006840 props.max_brightness = bright_maxlvl;
6841 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006842 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6843 NULL, NULL,
6844 &ibm_backlight_data,
6845 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006846 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006847 int rc = PTR_ERR(ibm_backlight_device);
6848 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006849 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006850 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006851 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006852 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6853 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006854
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006855 if (quirks & TPACPI_BRGHT_Q_ASK) {
Joe Perches0978e012011-04-04 10:06:25 -07006856 pr_notice("brightness: will use unverified default: "
6857 "brightness_mode=%d\n", brightness_mode);
6858 pr_notice("brightness: please report to %s whether it works well "
6859 "or not on your ThinkPad\n", TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006860 }
6861
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006862 /* Added by mistake in early 2007. Probably useless, but it could
6863 * be working around some unknown firmware problem where the value
6864 * read at startup doesn't match the real hardware state... so leave
6865 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006866 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006867
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006868 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6869 "brightness: registering brightness hotkeys "
6870 "as change notification\n");
6871 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6872 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006873 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006874 return 0;
6875}
6876
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006877static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006878{
6879 tpacpi_brightness_checkpoint_nvram();
6880}
6881
6882static void brightness_shutdown(void)
6883{
6884 tpacpi_brightness_checkpoint_nvram();
6885}
6886
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006887static void brightness_exit(void)
6888{
6889 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006890 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006891 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006892 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006893 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006894
6895 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006896}
6897
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006898static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006899{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006900 int level;
6901
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006902 level = brightness_get(NULL);
6903 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006904 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006905 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006906 seq_printf(m, "level:\t\t%d\n", level);
6907 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006908 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6909 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006910 }
6911
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006912 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006913}
6914
6915static int brightness_write(char *buf)
6916{
6917 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006918 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006919 char *cmd;
6920
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006921 level = brightness_get(NULL);
6922 if (level < 0)
6923 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006924
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006925 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006926 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006927 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006928 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006929 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006930 if (level > 0)
6931 level--;
6932 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006933 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006934 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006935 } else
6936 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006937 }
6938
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006939 tpacpi_disclose_usertask("procfs brightness",
6940 "set level to %d\n", level);
6941
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006942 /*
6943 * Now we know what the final level should be, so we try to set it.
6944 * Doing it this way makes the syscall restartable in case of EINTR
6945 */
6946 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006947 if (!rc && ibm_backlight_device)
6948 backlight_force_update(ibm_backlight_device,
6949 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02006950 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006951}
6952
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006953static struct ibm_struct brightness_driver_data = {
6954 .name = "brightness",
6955 .read = brightness_read,
6956 .write = brightness_write,
6957 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006958 .suspend = brightness_suspend,
6959 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006960};
6961
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006962/*************************************************************************
6963 * Volume subdriver
6964 */
6965
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006966/*
6967 * IBM ThinkPads have a simple volume controller with MUTE gating.
6968 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6969 *
6970 * Since the *61 series (and probably also the later *60 series), Lenovo
6971 * ThinkPads only implement the MUTE gate.
6972 *
6973 * EC register 0x30
6974 * Bit 6: MUTE (1 mutes sound)
6975 * Bit 3-0: Volume
6976 * Other bits should be zero as far as we know.
6977 *
6978 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6979 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6980 * such as bit 7 which is used to detect repeated presses of MUTE,
6981 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07006982 *
6983 * On newer Lenovo ThinkPads, the EC can automatically change the volume
6984 * in response to user input. Unfortunately, this rarely works well.
6985 * The laptop changes the state of its internal MUTE gate and, on some
6986 * models, sends KEY_MUTE, causing any user code that responds to the
6987 * mute button to get confused. The hardware MUTE gate is also
6988 * unnecessary, since user code can handle the mute button without
6989 * kernel or EC help.
6990 *
6991 * To avoid confusing userspace, we simply disable all EC-based mute
6992 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006993 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006994
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006995#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6996
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006997#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6998#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6999#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7000
Takashi Iwaicab66612013-10-24 16:06:32 +02007001#if SNDRV_CARDS <= 32
7002#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7003#else
7004#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7005#endif
7006static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007007static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307008static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007009
7010struct tpacpi_alsa_data {
7011 struct snd_card *card;
7012 struct snd_ctl_elem_id *ctl_mute_id;
7013 struct snd_ctl_elem_id *ctl_vol_id;
7014};
7015
7016static struct snd_card *alsa_card;
7017
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007018enum {
7019 TP_EC_AUDIO = 0x30,
7020
7021 /* TP_EC_AUDIO bits */
7022 TP_EC_AUDIO_MUTESW = 6,
7023
7024 /* TP_EC_AUDIO bitmasks */
7025 TP_EC_AUDIO_LVL_MSK = 0x0F,
7026 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7027
7028 /* Maximum volume */
7029 TP_EC_VOLUME_MAX = 14,
7030};
7031
7032enum tpacpi_volume_access_mode {
7033 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7034 TPACPI_VOL_MODE_EC, /* Pure EC control */
7035 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7036 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7037 TPACPI_VOL_MODE_MAX
7038};
7039
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007040enum tpacpi_volume_capabilities {
7041 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7042 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7043 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7044 TPACPI_VOL_CAP_MAX
7045};
7046
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007047enum tpacpi_mute_btn_mode {
7048 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7049 /* We don't know what mode 1 is. */
7050 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7051 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7052};
7053
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007054static enum tpacpi_volume_access_mode volume_mode =
7055 TPACPI_VOL_MODE_MAX;
7056
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007057static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307058static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007059static bool software_mute_requested = true;
7060static bool software_mute_active;
7061static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007062
7063/*
7064 * Used to syncronize writers to TP_EC_AUDIO and
7065 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7066 */
7067static struct mutex volume_mutex;
7068
7069static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007070{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007071 u8 lec = 0;
7072 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007073 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007074
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007075 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7076 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007077 if (!volume_control_allowed)
7078 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007079 if (software_mute_active)
7080 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007081
7082 vdbg_printk(TPACPI_DBG_MIXER,
7083 "trying to checkpoint mixer state to NVRAM...\n");
7084
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007085 if (tp_features.mixer_no_level_control)
7086 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7087 else
7088 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7089
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007090 if (mutex_lock_killable(&volume_mutex) < 0)
7091 return;
7092
7093 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7094 goto unlock;
7095 lec &= ec_mask;
7096 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7097
7098 if (lec != (b_nvram & ec_mask)) {
7099 /* NVRAM needs update */
7100 b_nvram &= ~ec_mask;
7101 b_nvram |= lec;
7102 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7103 dbg_printk(TPACPI_DBG_MIXER,
7104 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7105 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007106 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007107 vdbg_printk(TPACPI_DBG_MIXER,
7108 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7109 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007110 }
7111
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007112unlock:
7113 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007114}
7115
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007116static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007117{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007118 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007119
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007120 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7121 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007122
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007123 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007124
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007125 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007126
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007127 return 0;
7128}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007129
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007130static int volume_get_status(u8 *status)
7131{
7132 return volume_get_status_ec(status);
7133}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007134
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007135static int volume_set_status_ec(const u8 status)
7136{
7137 if (!acpi_ec_write(TP_EC_AUDIO, status))
7138 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007139
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007140 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7141
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007142 /*
7143 * On X200s, and possibly on others, it can take a while for
7144 * reads to become correct.
7145 */
7146 msleep(1);
7147
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007148 return 0;
7149}
7150
7151static int volume_set_status(const u8 status)
7152{
7153 return volume_set_status_ec(status);
7154}
7155
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007156/* returns < 0 on error, 0 on no change, 1 on change */
7157static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007158{
7159 int rc;
7160 u8 s, n;
7161
7162 if (mutex_lock_killable(&volume_mutex) < 0)
7163 return -EINTR;
7164
7165 rc = volume_get_status_ec(&s);
7166 if (rc)
7167 goto unlock;
7168
7169 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7170 s & ~TP_EC_AUDIO_MUTESW_MSK;
7171
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007172 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007173 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007174 if (!rc)
7175 rc = 1;
7176 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007177
7178unlock:
7179 mutex_unlock(&volume_mutex);
7180 return rc;
7181}
7182
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007183static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007184{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007185 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007186 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007187 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007188}
7189
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007190static int volume_set_mute(const bool mute)
7191{
7192 int rc;
7193
7194 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7195 (mute) ? "" : "un");
7196
7197 rc = __volume_set_mute_ec(mute);
7198 return (rc < 0) ? rc : 0;
7199}
7200
7201/* returns < 0 on error, 0 on no change, 1 on change */
7202static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007203{
7204 int rc;
7205 u8 s, n;
7206
7207 if (vol > TP_EC_VOLUME_MAX)
7208 return -EINVAL;
7209
7210 if (mutex_lock_killable(&volume_mutex) < 0)
7211 return -EINTR;
7212
7213 rc = volume_get_status_ec(&s);
7214 if (rc)
7215 goto unlock;
7216
7217 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7218
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007219 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007220 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007221 if (!rc)
7222 rc = 1;
7223 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007224
7225unlock:
7226 mutex_unlock(&volume_mutex);
7227 return rc;
7228}
7229
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007230static int volume_set_software_mute(bool startup)
7231{
7232 int result;
7233
7234 if (!tpacpi_is_lenovo())
7235 return -ENODEV;
7236
7237 if (startup) {
7238 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7239 "HAUM", "qd"))
7240 return -EIO;
7241
7242 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7243 "Initial HAUM setting was %d\n",
7244 software_mute_orig_mode);
7245 }
7246
7247 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7248 (int)TP_EC_MUTE_BTN_NONE))
7249 return -EIO;
7250
7251 if (result != TP_EC_MUTE_BTN_NONE)
7252 pr_warn("Unexpected SAUM result %d\n",
7253 result);
7254
7255 /*
7256 * In software mute mode, the standard codec controls take
7257 * precendence, so we unmute the ThinkPad HW switch at
7258 * startup. Just on case there are SAUM-capable ThinkPads
7259 * with level controls, set max HW volume as well.
7260 */
7261 if (tp_features.mixer_no_level_control)
7262 result = volume_set_mute(false);
7263 else
7264 result = volume_set_status(TP_EC_VOLUME_MAX);
7265
7266 if (result != 0)
7267 pr_warn("Failed to unmute the HW mute switch\n");
7268
7269 return 0;
7270}
7271
7272static void volume_exit_software_mute(void)
7273{
7274 int r;
7275
7276 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7277 || r != software_mute_orig_mode)
7278 pr_warn("Failed to restore mute mode\n");
7279}
7280
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007281static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007282{
7283 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007284 "ALSA: trying to set volume level to %hu\n", vol);
7285 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007286}
7287
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007288static void volume_alsa_notify_change(void)
7289{
7290 struct tpacpi_alsa_data *d;
7291
7292 if (alsa_card && alsa_card->private_data) {
7293 d = alsa_card->private_data;
7294 if (d->ctl_mute_id)
7295 snd_ctl_notify(alsa_card,
7296 SNDRV_CTL_EVENT_MASK_VALUE,
7297 d->ctl_mute_id);
7298 if (d->ctl_vol_id)
7299 snd_ctl_notify(alsa_card,
7300 SNDRV_CTL_EVENT_MASK_VALUE,
7301 d->ctl_vol_id);
7302 }
7303}
7304
7305static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7306 struct snd_ctl_elem_info *uinfo)
7307{
7308 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7309 uinfo->count = 1;
7310 uinfo->value.integer.min = 0;
7311 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7312 return 0;
7313}
7314
7315static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7316 struct snd_ctl_elem_value *ucontrol)
7317{
7318 u8 s;
7319 int rc;
7320
7321 rc = volume_get_status(&s);
7322 if (rc < 0)
7323 return rc;
7324
7325 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7326 return 0;
7327}
7328
7329static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7330 struct snd_ctl_elem_value *ucontrol)
7331{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007332 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7333 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007334 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007335}
7336
7337#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7338
7339static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7340 struct snd_ctl_elem_value *ucontrol)
7341{
7342 u8 s;
7343 int rc;
7344
7345 rc = volume_get_status(&s);
7346 if (rc < 0)
7347 return rc;
7348
7349 ucontrol->value.integer.value[0] =
7350 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7351 return 0;
7352}
7353
7354static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7355 struct snd_ctl_elem_value *ucontrol)
7356{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007357 tpacpi_disclose_usertask("ALSA", "%smute\n",
7358 ucontrol->value.integer.value[0] ?
7359 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007360 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007361}
7362
Mathias Krausec3aa4722014-07-16 19:43:17 +02007363static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007364 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7365 .name = "Console Playback Volume",
7366 .index = 0,
7367 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7368 .info = volume_alsa_vol_info,
7369 .get = volume_alsa_vol_get,
7370};
7371
Mathias Krausec3aa4722014-07-16 19:43:17 +02007372static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007373 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7374 .name = "Console Playback Switch",
7375 .index = 0,
7376 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7377 .info = volume_alsa_mute_info,
7378 .get = volume_alsa_mute_get,
7379};
7380
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007381static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007382{
7383 tpacpi_volume_checkpoint_nvram();
7384}
7385
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007386static void volume_resume(void)
7387{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007388 if (software_mute_active) {
7389 if (volume_set_software_mute(false) < 0)
7390 pr_warn("Failed to restore software mute\n");
7391 } else {
7392 volume_alsa_notify_change();
7393 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007394}
7395
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007396static void volume_shutdown(void)
7397{
7398 tpacpi_volume_checkpoint_nvram();
7399}
7400
7401static void volume_exit(void)
7402{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007403 if (alsa_card) {
7404 snd_card_free(alsa_card);
7405 alsa_card = NULL;
7406 }
7407
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007408 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007409
7410 if (software_mute_active)
7411 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007412}
7413
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007414static int __init volume_create_alsa_mixer(void)
7415{
7416 struct snd_card *card;
7417 struct tpacpi_alsa_data *data;
7418 struct snd_kcontrol *ctl_vol;
7419 struct snd_kcontrol *ctl_mute;
7420 int rc;
7421
Takashi Iwai89235e52014-01-29 15:01:27 +01007422 rc = snd_card_new(&tpacpi_pdev->dev,
7423 alsa_index, alsa_id, THIS_MODULE,
7424 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007425 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007426 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007427 return 1;
7428 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007429
7430 BUG_ON(!card->private_data);
7431 data = card->private_data;
7432 data->card = card;
7433
7434 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7435 sizeof(card->driver));
7436 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7437 sizeof(card->shortname));
7438 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7439 (thinkpad_id.ec_version_str) ?
7440 thinkpad_id.ec_version_str : "(unknown)");
7441 snprintf(card->longname, sizeof(card->longname),
7442 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7443 (thinkpad_id.ec_version_str) ?
7444 thinkpad_id.ec_version_str : "unknown");
7445
7446 if (volume_control_allowed) {
7447 volume_alsa_control_vol.put = volume_alsa_vol_put;
7448 volume_alsa_control_vol.access =
7449 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7450
7451 volume_alsa_control_mute.put = volume_alsa_mute_put;
7452 volume_alsa_control_mute.access =
7453 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7454 }
7455
7456 if (!tp_features.mixer_no_level_control) {
7457 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7458 rc = snd_ctl_add(card, ctl_vol);
7459 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007460 pr_err("Failed to create ALSA volume control: %d\n",
7461 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007462 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007463 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007464 data->ctl_vol_id = &ctl_vol->id;
7465 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007466
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007467 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7468 rc = snd_ctl_add(card, ctl_mute);
7469 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007470 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007471 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007472 }
7473 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007474
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007475 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007476 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007477 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007478 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007479 }
7480
7481 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007482 return 0;
7483
7484err_exit:
7485 snd_card_free(card);
7486 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007487}
7488
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007489#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7490#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7491
7492static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7493 /* Whitelist volume level on all IBM by default */
7494 { .vendor = PCI_VENDOR_ID_IBM,
7495 .bios = TPACPI_MATCH_ANY,
7496 .ec = TPACPI_MATCH_ANY,
7497 .quirks = TPACPI_VOL_Q_LEVEL },
7498
7499 /* Lenovo models with volume control (needs confirmation) */
7500 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7501 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7502 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7503 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7504 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7505 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7506 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7507
7508 /* Whitelist mute-only on all Lenovo by default */
7509 { .vendor = PCI_VENDOR_ID_LENOVO,
7510 .bios = TPACPI_MATCH_ANY,
7511 .ec = TPACPI_MATCH_ANY,
7512 .quirks = TPACPI_VOL_Q_MUTEONLY }
7513};
7514
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007515static int __init volume_init(struct ibm_init_struct *iibm)
7516{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007517 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007518 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007519
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007520 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7521
7522 mutex_init(&volume_mutex);
7523
7524 /*
7525 * Check for module parameter bogosity, note that we
7526 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7527 * able to detect "unspecified"
7528 */
7529 if (volume_mode > TPACPI_VOL_MODE_MAX)
7530 return -EINVAL;
7531
7532 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Joe Perches0978e012011-04-04 10:06:25 -07007533 pr_err("UCMS step volume mode not implemented, "
7534 "please contact %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007535 return 1;
7536 }
7537
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007538 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7539 return -EINVAL;
7540
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007541 /*
7542 * The ALSA mixer is our primary interface.
7543 * When disabled, don't install the subdriver at all
7544 */
7545 if (!alsa_enable) {
7546 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7547 "ALSA mixer disabled by parameter, "
7548 "not loading volume subdriver...\n");
7549 return 1;
7550 }
7551
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007552 quirks = tpacpi_check_quirks(volume_quirk_table,
7553 ARRAY_SIZE(volume_quirk_table));
7554
7555 switch (volume_capabilities) {
7556 case TPACPI_VOL_CAP_AUTO:
7557 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7558 tp_features.mixer_no_level_control = 1;
7559 else if (quirks & TPACPI_VOL_Q_LEVEL)
7560 tp_features.mixer_no_level_control = 0;
7561 else
7562 return 1; /* no mixer */
7563 break;
7564 case TPACPI_VOL_CAP_VOLMUTE:
7565 tp_features.mixer_no_level_control = 0;
7566 break;
7567 case TPACPI_VOL_CAP_MUTEONLY:
7568 tp_features.mixer_no_level_control = 1;
7569 break;
7570 default:
7571 return 1;
7572 }
7573
7574 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7575 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7576 "using user-supplied volume_capabilities=%d\n",
7577 volume_capabilities);
7578
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007579 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7580 volume_mode == TPACPI_VOL_MODE_MAX) {
7581 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7582
7583 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7584 "driver auto-selected volume_mode=%d\n",
7585 volume_mode);
7586 } else {
7587 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7588 "using user-supplied volume_mode=%d\n",
7589 volume_mode);
7590 }
7591
7592 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007593 "mute is supported, volume control is %s\n",
7594 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007595
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007596 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7597 software_mute_active = true;
7598 } else {
7599 rc = volume_create_alsa_mixer();
7600 if (rc) {
7601 pr_err("Could not create the ALSA mixer interface\n");
7602 return rc;
7603 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007604
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007605 pr_info("Console audio control enabled, mode: %s\n",
7606 (volume_control_allowed) ?
7607 "override (read/write)" :
7608 "monitor (read only)");
7609 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007610
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007611 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7612 "registering volume hotkeys as change notification\n");
7613 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7614 | TP_ACPI_HKEY_VOLUP_MASK
7615 | TP_ACPI_HKEY_VOLDWN_MASK
7616 | TP_ACPI_HKEY_MUTE_MASK);
7617
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007618 return 0;
7619}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007620
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007621static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007622{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007623 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007624
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007625 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007626 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007627 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007628 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007629 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007630 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007631 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007632 status & TP_EC_AUDIO_LVL_MSK);
7633
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007634 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007635 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007636
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007637 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007638 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007639 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007640 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007641 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007642 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007643 "commands:\tlevel <level>"
7644 " (<level> is 0-%d)\n",
7645 TP_EC_VOLUME_MAX);
7646 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007647 }
7648 }
7649
7650 return 0;
7651}
7652
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007653static int volume_write(char *buf)
7654{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007655 u8 s;
7656 u8 new_level, new_mute;
7657 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007658 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007659 int rc;
7660
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007661 /*
7662 * We do allow volume control at driver startup, so that the
7663 * user can set initial state through the volume=... parameter hack.
7664 */
7665 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7666 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7667 tp_warned.volume_ctrl_forbidden = 1;
Joe Perches0978e012011-04-04 10:06:25 -07007668 pr_notice("Console audio control in monitor mode, "
7669 "changes are not allowed\n");
7670 pr_notice("Use the volume_control=1 module parameter "
7671 "to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007672 }
7673 return -EPERM;
7674 }
7675
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007676 rc = volume_get_status(&s);
7677 if (rc < 0)
7678 return rc;
7679
7680 new_level = s & TP_EC_AUDIO_LVL_MSK;
7681 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007682
7683 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007684 if (!tp_features.mixer_no_level_control) {
7685 if (strlencmp(cmd, "up") == 0) {
7686 if (new_mute)
7687 new_mute = 0;
7688 else if (new_level < TP_EC_VOLUME_MAX)
7689 new_level++;
7690 continue;
7691 } else if (strlencmp(cmd, "down") == 0) {
7692 if (new_mute)
7693 new_mute = 0;
7694 else if (new_level > 0)
7695 new_level--;
7696 continue;
7697 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7698 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7699 new_level = l;
7700 continue;
7701 }
7702 }
7703 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007704 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007705 else if (strlencmp(cmd, "unmute") == 0)
7706 new_mute = 0;
7707 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007708 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007709 }
7710
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007711 if (tp_features.mixer_no_level_control) {
7712 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7713 new_mute ? "" : "un");
7714 rc = volume_set_mute(!!new_mute);
7715 } else {
7716 tpacpi_disclose_usertask("procfs volume",
7717 "%smute and set level to %d\n",
7718 new_mute ? "" : "un", new_level);
7719 rc = volume_set_status(new_mute | new_level);
7720 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007721 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007722
7723 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007724}
7725
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007726static struct ibm_struct volume_driver_data = {
7727 .name = "volume",
7728 .read = volume_read,
7729 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007730 .exit = volume_exit,
7731 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007732 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007733 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007734};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007735
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007736#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7737
7738#define alsa_card NULL
7739
Tobias Klauserbb55a2e2016-11-25 14:18:07 +01007740static inline void volume_alsa_notify_change(void)
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007741{
7742}
7743
7744static int __init volume_init(struct ibm_init_struct *iibm)
7745{
Joe Perches0978e012011-04-04 10:06:25 -07007746 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007747
7748 return 1;
7749}
7750
7751static struct ibm_struct volume_driver_data = {
7752 .name = "volume",
7753};
7754
7755#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7756
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007757/*************************************************************************
7758 * Fan subdriver
7759 */
7760
7761/*
7762 * FAN ACCESS MODES
7763 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007764 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007765 * ACPI GFAN method: returns fan level
7766 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007767 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007768 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007769 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007770 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007771 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7772 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007773 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7774 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007775 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007776 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007777 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7778 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007779 *
7780 * Fan speed changes of any sort (including those caused by the
7781 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007782 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007783 * limited.
7784 *
7785 * Reading is not available if GFAN exists.
7786 * Writing is not available if SFAN exists.
7787 *
7788 * Bits
7789 * 7 automatic mode engaged;
7790 * (default operation mode of the ThinkPad)
7791 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007792 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007793 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007794 * the tachometer while the fan controller ramps up
7795 * the speed (which can take up to a few *minutes*).
7796 * Speeds up fan to 100% duty-cycle, which is far above
7797 * the standard RPM levels. It is not impossible that
7798 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007799 * 5-3 unused in some models. Extra bits for fan level
7800 * in others, but still useless as all values above
7801 * 7 map to the same speed as level 7 in these models.
7802 * 2-0 fan level (0..7 usually)
7803 * 0x00 = stop
7804 * 0x07 = max (set when temperatures critical)
7805 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007806 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007807 *
7808 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007809 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007810 * does so, its initial value is meaningless (0x07).
7811 *
7812 * For firmware bugs, refer to:
7813 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7814 *
7815 * ----
7816 *
7817 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7818 * Main fan tachometer reading (in RPM)
7819 *
7820 * This register is present on all ThinkPads with a new-style EC, and
7821 * it is known not to be present on the A21m/e, and T22, as there is
7822 * something else in offset 0x84 according to the ACPI DSDT. Other
7823 * ThinkPads from this same time period (and earlier) probably lack the
7824 * tachometer as well.
7825 *
Nick Andrew877d0312009-01-26 11:06:57 +01007826 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007827 * was never fixed by IBM to report the EC firmware version string
7828 * probably support the tachometer (like the early X models), so
7829 * detecting it is quite hard. We need more data to know for sure.
7830 *
7831 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7832 * might result.
7833 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007834 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7835 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007836 *
7837 * For firmware bugs, refer to:
7838 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7839 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007840 * ----
7841 *
7842 * ThinkPad EC register 0x31 bit 0 (only on select models)
7843 *
7844 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7845 * show the speed of the main fan. When bit 0 of EC register 0x31
7846 * is one, the tachometer registers show the speed of the auxiliary
7847 * fan.
7848 *
7849 * Fan control seems to affect both fans, regardless of the state
7850 * of this bit.
7851 *
7852 * So far, only the firmware for the X60/X61 non-tablet versions
7853 * seem to support this (firmware TP-7M).
7854 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007855 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007856 * ThinkPad X31, X40, X41. Not available in the X60.
7857 *
7858 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7859 * high speed. ACPI DSDT seems to map these three speeds to levels
7860 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7861 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7862 *
7863 * The speeds are stored on handles
7864 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7865 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007866 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007867 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7868 *
7869 * ACPI DSDT switches which set is in use depending on various
7870 * factors.
7871 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007872 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007873 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7874 * but the ACPI tables just mention level 7.
7875 */
7876
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007877enum { /* Fan control constants */
7878 fan_status_offset = 0x2f, /* EC register 0x2f */
7879 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7880 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007881 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7882 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007883
7884 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7885 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7886
7887 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7888};
7889
7890enum fan_status_access_mode {
7891 TPACPI_FAN_NONE = 0, /* No fan status or control */
7892 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7893 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7894};
7895
7896enum fan_control_access_mode {
7897 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7898 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7899 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7900 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7901};
7902
7903enum fan_control_commands {
7904 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7905 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7906 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7907 * and also watchdog cmd */
7908};
7909
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307910static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007911
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007912static enum fan_status_access_mode fan_status_access_mode;
7913static enum fan_control_access_mode fan_control_access_mode;
7914static enum fan_control_commands fan_control_commands;
7915
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007916static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007917static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007918static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007919static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007920
7921static struct mutex fan_mutex;
7922
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007923static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007924static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007925
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007926TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7927TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007928 "\\FSPD", /* 600e/x, 770e, 770x */
7929 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007930TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007931 "JFNS", /* 770x-JL */
7932 ); /* all others */
7933
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007934/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007935 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7936 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7937 * be in auto mode (0x80).
7938 *
7939 * This is corrected by any write to HFSP either by the driver, or
7940 * by the firmware.
7941 *
7942 * We assume 0x07 really means auto mode while this quirk is active,
7943 * as this is far more likely than the ThinkPad being in level 7,
7944 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007945 *
7946 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7947 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007948 */
7949
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007950static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007951{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007952 if (fan_control_initial_status == 0x07) {
Joe Perches0978e012011-04-04 10:06:25 -07007953 pr_notice("fan_init: initial fan status is unknown, "
7954 "assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007955 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007956 }
7957}
7958
7959static void fan_quirk1_handle(u8 *fan_status)
7960{
7961 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7962 if (*fan_status != fan_control_initial_status) {
7963 /* something changed the HFSP regisnter since
7964 * driver init time, so it is not undefined
7965 * anymore */
7966 tp_features.fan_ctrl_status_undef = 0;
7967 } else {
7968 /* Return most likely status. In fact, it
7969 * might be the only possible status */
7970 *fan_status = TP_EC_FAN_AUTO;
7971 }
7972 }
7973}
7974
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007975/* Select main fan on X60/X61, NOOP on others */
7976static bool fan_select_fan1(void)
7977{
7978 if (tp_features.second_fan) {
7979 u8 val;
7980
7981 if (ec_read(fan_select_offset, &val) < 0)
7982 return false;
7983 val &= 0xFEU;
7984 if (ec_write(fan_select_offset, val) < 0)
7985 return false;
7986 }
7987 return true;
7988}
7989
7990/* Select secondary fan on X60/X61 */
7991static bool fan_select_fan2(void)
7992{
7993 u8 val;
7994
7995 if (!tp_features.second_fan)
7996 return false;
7997
7998 if (ec_read(fan_select_offset, &val) < 0)
7999 return false;
8000 val |= 0x01U;
8001 if (ec_write(fan_select_offset, val) < 0)
8002 return false;
8003
8004 return true;
8005}
8006
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008007/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008008 * Call with fan_mutex held
8009 */
8010static void fan_update_desired_level(u8 status)
8011{
8012 if ((status &
8013 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8014 if (status > 7)
8015 fan_control_desired_level = 7;
8016 else
8017 fan_control_desired_level = status;
8018 }
8019}
8020
8021static int fan_get_status(u8 *status)
8022{
8023 u8 s;
8024
8025 /* TODO:
8026 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8027
8028 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07008029 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008030 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07008031 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008032
Dan Carpentereceeb432012-09-01 12:54:07 -07008033 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008034 return -EIO;
8035
8036 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07008037 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008038
8039 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07008040 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008041 case TPACPI_FAN_RD_TPEC:
8042 /* all except 570, 600e/x, 770e, 770x */
8043 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8044 return -EIO;
8045
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008046 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008047 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008048 fan_quirk1_handle(status);
8049 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008050
8051 break;
8052
8053 default:
8054 return -ENXIO;
8055 }
8056
8057 return 0;
8058}
8059
8060static int fan_get_status_safe(u8 *status)
8061{
8062 int rc;
8063 u8 s;
8064
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008065 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008066 return -ERESTARTSYS;
8067 rc = fan_get_status(&s);
8068 if (!rc)
8069 fan_update_desired_level(s);
8070 mutex_unlock(&fan_mutex);
8071
Dan Carpentera7718362016-04-15 17:46:34 +03008072 if (rc)
8073 return rc;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008074 if (status)
8075 *status = s;
8076
Dan Carpentera7718362016-04-15 17:46:34 +03008077 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008078}
8079
8080static int fan_get_speed(unsigned int *speed)
8081{
8082 u8 hi, lo;
8083
8084 switch (fan_status_access_mode) {
8085 case TPACPI_FAN_RD_TPEC:
8086 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008087 if (unlikely(!fan_select_fan1()))
8088 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008089 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8090 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8091 return -EIO;
8092
8093 if (likely(speed))
8094 *speed = (hi << 8) | lo;
8095
8096 break;
8097
8098 default:
8099 return -ENXIO;
8100 }
8101
8102 return 0;
8103}
8104
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008105static int fan2_get_speed(unsigned int *speed)
8106{
8107 u8 hi, lo;
8108 bool rc;
8109
8110 switch (fan_status_access_mode) {
8111 case TPACPI_FAN_RD_TPEC:
8112 /* all except 570, 600e/x, 770e, 770x */
8113 if (unlikely(!fan_select_fan2()))
8114 return -EIO;
8115 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8116 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8117 fan_select_fan1(); /* play it safe */
8118 if (rc)
8119 return -EIO;
8120
8121 if (likely(speed))
8122 *speed = (hi << 8) | lo;
8123
8124 break;
8125
8126 default:
8127 return -ENXIO;
8128 }
8129
8130 return 0;
8131}
8132
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008133static int fan_set_level(int level)
8134{
8135 if (!fan_control_allowed)
8136 return -EPERM;
8137
8138 switch (fan_control_access_mode) {
8139 case TPACPI_FAN_WR_ACPI_SFAN:
8140 if (level >= 0 && level <= 7) {
8141 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8142 return -EIO;
8143 } else
8144 return -EINVAL;
8145 break;
8146
8147 case TPACPI_FAN_WR_ACPI_FANS:
8148 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008149 if (!(level & TP_EC_FAN_AUTO) &&
8150 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008151 ((level < 0) || (level > 7)))
8152 return -EINVAL;
8153
8154 /* safety net should the EC not support AUTO
8155 * or FULLSPEED mode bits and just ignore them */
8156 if (level & TP_EC_FAN_FULLSPEED)
8157 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01008158 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008159 level |= 4; /* safety min speed 4 */
8160
8161 if (!acpi_ec_write(fan_status_offset, level))
8162 return -EIO;
8163 else
8164 tp_features.fan_ctrl_status_undef = 0;
8165 break;
8166
8167 default:
8168 return -ENXIO;
8169 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008170
8171 vdbg_printk(TPACPI_DBG_FAN,
8172 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008173 return 0;
8174}
8175
8176static int fan_set_level_safe(int level)
8177{
8178 int rc;
8179
8180 if (!fan_control_allowed)
8181 return -EPERM;
8182
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008183 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008184 return -ERESTARTSYS;
8185
8186 if (level == TPACPI_FAN_LAST_LEVEL)
8187 level = fan_control_desired_level;
8188
8189 rc = fan_set_level(level);
8190 if (!rc)
8191 fan_update_desired_level(level);
8192
8193 mutex_unlock(&fan_mutex);
8194 return rc;
8195}
8196
8197static int fan_set_enable(void)
8198{
8199 u8 s;
8200 int rc;
8201
8202 if (!fan_control_allowed)
8203 return -EPERM;
8204
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008205 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008206 return -ERESTARTSYS;
8207
8208 switch (fan_control_access_mode) {
8209 case TPACPI_FAN_WR_ACPI_FANS:
8210 case TPACPI_FAN_WR_TPEC:
8211 rc = fan_get_status(&s);
8212 if (rc < 0)
8213 break;
8214
8215 /* Don't go out of emergency fan mode */
8216 if (s != 7) {
8217 s &= 0x07;
8218 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8219 }
8220
8221 if (!acpi_ec_write(fan_status_offset, s))
8222 rc = -EIO;
8223 else {
8224 tp_features.fan_ctrl_status_undef = 0;
8225 rc = 0;
8226 }
8227 break;
8228
8229 case TPACPI_FAN_WR_ACPI_SFAN:
8230 rc = fan_get_status(&s);
8231 if (rc < 0)
8232 break;
8233
8234 s &= 0x07;
8235
8236 /* Set fan to at least level 4 */
8237 s |= 4;
8238
8239 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008240 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008241 else
8242 rc = 0;
8243 break;
8244
8245 default:
8246 rc = -ENXIO;
8247 }
8248
8249 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008250
8251 if (!rc)
8252 vdbg_printk(TPACPI_DBG_FAN,
8253 "fan control: set fan control register to 0x%02x\n",
8254 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008255 return rc;
8256}
8257
8258static int fan_set_disable(void)
8259{
8260 int rc;
8261
8262 if (!fan_control_allowed)
8263 return -EPERM;
8264
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008265 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008266 return -ERESTARTSYS;
8267
8268 rc = 0;
8269 switch (fan_control_access_mode) {
8270 case TPACPI_FAN_WR_ACPI_FANS:
8271 case TPACPI_FAN_WR_TPEC:
8272 if (!acpi_ec_write(fan_status_offset, 0x00))
8273 rc = -EIO;
8274 else {
8275 fan_control_desired_level = 0;
8276 tp_features.fan_ctrl_status_undef = 0;
8277 }
8278 break;
8279
8280 case TPACPI_FAN_WR_ACPI_SFAN:
8281 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8282 rc = -EIO;
8283 else
8284 fan_control_desired_level = 0;
8285 break;
8286
8287 default:
8288 rc = -ENXIO;
8289 }
8290
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008291 if (!rc)
8292 vdbg_printk(TPACPI_DBG_FAN,
8293 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008294
8295 mutex_unlock(&fan_mutex);
8296 return rc;
8297}
8298
8299static int fan_set_speed(int speed)
8300{
8301 int rc;
8302
8303 if (!fan_control_allowed)
8304 return -EPERM;
8305
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008306 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008307 return -ERESTARTSYS;
8308
8309 rc = 0;
8310 switch (fan_control_access_mode) {
8311 case TPACPI_FAN_WR_ACPI_FANS:
8312 if (speed >= 0 && speed <= 65535) {
8313 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8314 speed, speed, speed))
8315 rc = -EIO;
8316 } else
8317 rc = -EINVAL;
8318 break;
8319
8320 default:
8321 rc = -ENXIO;
8322 }
8323
8324 mutex_unlock(&fan_mutex);
8325 return rc;
8326}
8327
8328static void fan_watchdog_reset(void)
8329{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008330 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8331 return;
8332
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008333 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008334 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8335 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8336 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8337 else
8338 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008339}
8340
8341static void fan_watchdog_fire(struct work_struct *ignored)
8342{
8343 int rc;
8344
8345 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8346 return;
8347
Joe Perches0978e012011-04-04 10:06:25 -07008348 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008349 rc = fan_set_enable();
8350 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07008351 pr_err("fan watchdog: error %d while enabling fan, "
8352 "will try again later...\n", -rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008353 /* reschedule for later */
8354 fan_watchdog_reset();
8355 }
8356}
8357
8358/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008359 * SYSFS fan layout: hwmon compatible (device)
8360 *
8361 * pwm*_enable:
8362 * 0: "disengaged" mode
8363 * 1: manual mode
8364 * 2: native EC "auto" mode (recommended, hardware default)
8365 *
8366 * pwm*: set speed in manual mode, ignored otherwise.
8367 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8368 * interpolation.
8369 *
8370 * fan*_input: tachometer reading, RPM
8371 *
8372 *
8373 * SYSFS fan layout: extensions
8374 *
8375 * fan_watchdog (driver):
8376 * fan watchdog interval in seconds, 0 disables (default), max 120
8377 */
8378
8379/* sysfs fan pwm1_enable ----------------------------------------------- */
8380static ssize_t fan_pwm1_enable_show(struct device *dev,
8381 struct device_attribute *attr,
8382 char *buf)
8383{
8384 int res, mode;
8385 u8 status;
8386
8387 res = fan_get_status_safe(&status);
8388 if (res)
8389 return res;
8390
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008391 if (status & TP_EC_FAN_FULLSPEED) {
8392 mode = 0;
8393 } else if (status & TP_EC_FAN_AUTO) {
8394 mode = 2;
8395 } else
8396 mode = 1;
8397
8398 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8399}
8400
8401static ssize_t fan_pwm1_enable_store(struct device *dev,
8402 struct device_attribute *attr,
8403 const char *buf, size_t count)
8404{
8405 unsigned long t;
8406 int res, level;
8407
8408 if (parse_strtoul(buf, 2, &t))
8409 return -EINVAL;
8410
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008411 tpacpi_disclose_usertask("hwmon pwm1_enable",
8412 "set fan mode to %lu\n", t);
8413
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008414 switch (t) {
8415 case 0:
8416 level = TP_EC_FAN_FULLSPEED;
8417 break;
8418 case 1:
8419 level = TPACPI_FAN_LAST_LEVEL;
8420 break;
8421 case 2:
8422 level = TP_EC_FAN_AUTO;
8423 break;
8424 case 3:
8425 /* reserved for software-controlled auto mode */
8426 return -ENOSYS;
8427 default:
8428 return -EINVAL;
8429 }
8430
8431 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008432 if (res == -ENXIO)
8433 return -EINVAL;
8434 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008435 return res;
8436
8437 fan_watchdog_reset();
8438
8439 return count;
8440}
8441
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008442static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8443 fan_pwm1_enable_show, fan_pwm1_enable_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008444
8445/* sysfs fan pwm1 ------------------------------------------------------ */
8446static ssize_t fan_pwm1_show(struct device *dev,
8447 struct device_attribute *attr,
8448 char *buf)
8449{
8450 int res;
8451 u8 status;
8452
8453 res = fan_get_status_safe(&status);
8454 if (res)
8455 return res;
8456
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008457 if ((status &
8458 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8459 status = fan_control_desired_level;
8460
8461 if (status > 7)
8462 status = 7;
8463
8464 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8465}
8466
8467static ssize_t fan_pwm1_store(struct device *dev,
8468 struct device_attribute *attr,
8469 const char *buf, size_t count)
8470{
8471 unsigned long s;
8472 int rc;
8473 u8 status, newlevel;
8474
8475 if (parse_strtoul(buf, 255, &s))
8476 return -EINVAL;
8477
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008478 tpacpi_disclose_usertask("hwmon pwm1",
8479 "set fan speed to %lu\n", s);
8480
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008481 /* scale down from 0-255 to 0-7 */
8482 newlevel = (s >> 5) & 0x07;
8483
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008484 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008485 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008486
8487 rc = fan_get_status(&status);
8488 if (!rc && (status &
8489 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8490 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008491 if (rc == -ENXIO)
8492 rc = -EINVAL;
8493 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008494 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008495 fan_watchdog_reset();
8496 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008497 }
8498
8499 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008500 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008501}
8502
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008503static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008504
8505/* sysfs fan fan1_input ------------------------------------------------ */
8506static ssize_t fan_fan1_input_show(struct device *dev,
8507 struct device_attribute *attr,
8508 char *buf)
8509{
8510 int res;
8511 unsigned int speed;
8512
8513 res = fan_get_speed(&speed);
8514 if (res < 0)
8515 return res;
8516
8517 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8518}
8519
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008520static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008521
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008522/* sysfs fan fan2_input ------------------------------------------------ */
8523static ssize_t fan_fan2_input_show(struct device *dev,
8524 struct device_attribute *attr,
8525 char *buf)
8526{
8527 int res;
8528 unsigned int speed;
8529
8530 res = fan2_get_speed(&speed);
8531 if (res < 0)
8532 return res;
8533
8534 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8535}
8536
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008537static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008538
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008539/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008540static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
8541 char *buf)
8542{
8543 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8544}
8545
8546static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8547 const char *buf, size_t count)
8548{
8549 unsigned long t;
8550
8551 if (parse_strtoul(buf, 120, &t))
8552 return -EINVAL;
8553
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008554 if (!fan_control_allowed)
8555 return -EPERM;
8556
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008557 fan_watchdog_maxinterval = t;
8558 fan_watchdog_reset();
8559
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008560 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8561
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008562 return count;
8563}
8564
8565static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8566 fan_fan_watchdog_show, fan_fan_watchdog_store);
8567
8568/* --------------------------------------------------------------------- */
8569static struct attribute *fan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008570 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8571 &dev_attr_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008572 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008573 NULL
8574};
8575
8576static const struct attribute_group fan_attr_group = {
8577 .attrs = fan_attributes,
8578};
8579
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008580#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8581#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008582
8583#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8584 { .vendor = PCI_VENDOR_ID_IBM, \
8585 .bios = TPACPI_MATCH_ANY, \
8586 .ec = TPID(__id1, __id2), \
8587 .quirks = __quirks }
8588
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008589#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8590 { .vendor = PCI_VENDOR_ID_LENOVO, \
8591 .bios = TPACPI_MATCH_ANY, \
8592 .ec = TPID(__id1, __id2), \
8593 .quirks = __quirks }
8594
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008595static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8596 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8597 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8598 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8599 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008600 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008601};
8602
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008603#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008604#undef TPACPI_FAN_QI
8605
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008606static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008607{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008608 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008609 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008610
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008611 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8612 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008613
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008614 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008615 fan_status_access_mode = TPACPI_FAN_NONE;
8616 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008617 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008618 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008619 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008620 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008621 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008622
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008623 if (tpacpi_is_ibm()) {
8624 TPACPI_ACPIHANDLE_INIT(fans);
8625 TPACPI_ACPIHANDLE_INIT(gfan);
8626 TPACPI_ACPIHANDLE_INIT(sfan);
8627 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008628
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008629 quirks = tpacpi_check_quirks(fan_quirk_table,
8630 ARRAY_SIZE(fan_quirk_table));
8631
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008632 if (gfan_handle) {
8633 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008634 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008635 } else {
8636 /* all other ThinkPads: note that even old-style
8637 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008638 if (likely(acpi_ec_read(fan_status_offset,
8639 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008640 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008641 if (quirks & TPACPI_FAN_Q1)
8642 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008643 if (quirks & TPACPI_FAN_2FAN) {
8644 tp_features.second_fan = 1;
8645 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8646 "secondary fan support enabled\n");
8647 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008648 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008649 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008650 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008651 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008652 }
8653 }
8654
8655 if (sfan_handle) {
8656 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008657 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008658 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008659 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008660 } else {
8661 if (!gfan_handle) {
8662 /* gfan without sfan means no fan control */
8663 /* all other models implement TP EC 0x2f control */
8664
8665 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008666 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008667 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008668 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008669 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008670 TPACPI_FAN_CMD_SPEED |
8671 TPACPI_FAN_CMD_LEVEL |
8672 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008673 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008674 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008675 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008676 TPACPI_FAN_CMD_LEVEL |
8677 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008678 }
8679 }
8680 }
8681
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008682 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8683 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008684 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8685 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8686 fan_status_access_mode, fan_control_access_mode);
8687
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008688 /* fan control master switch */
8689 if (!fan_control_allowed) {
8690 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8691 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008692 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008693 "fan control features disabled by parameter\n");
8694 }
8695
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008696 /* update fan_control_desired_level */
8697 if (fan_status_access_mode != TPACPI_FAN_NONE)
8698 fan_get_status_safe(NULL);
8699
8700 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8701 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008702 if (tp_features.second_fan) {
8703 /* attach second fan tachometer */
8704 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008705 &dev_attr_fan2_input.attr;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008706 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008707 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008708 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008709 if (rc < 0)
8710 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008711
8712 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8713 &driver_attr_fan_watchdog);
8714 if (rc < 0) {
8715 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8716 &fan_attr_group);
8717 return rc;
8718 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008719 return 0;
8720 } else
8721 return 1;
8722}
8723
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008724static void fan_exit(void)
8725{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008726 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008727 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008728
8729 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008730 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008731 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8732 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008733
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008734 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008735 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008736}
8737
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008738static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008739{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008740 int rc;
8741
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008742 if (!fan_control_allowed)
8743 return;
8744
8745 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008746 fan_control_resume_level = 0;
8747 rc = fan_get_status_safe(&fan_control_resume_level);
8748 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008749 pr_notice("failed to read fan level for later "
8750 "restore during resume: %d\n", rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008751
8752 /* if it is undefined, don't attempt to restore it.
8753 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008754 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008755 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008756}
8757
8758static void fan_resume(void)
8759{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008760 u8 current_level = 7;
8761 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008762 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008763
8764 /* DSDT *always* updates status on resume */
8765 tp_features.fan_ctrl_status_undef = 0;
8766
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008767 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008768 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008769 (fan_get_status_safe(&current_level) < 0))
8770 return;
8771
8772 switch (fan_control_access_mode) {
8773 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008774 /* never decrease fan level */
8775 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008776 break;
8777 case TPACPI_FAN_WR_ACPI_FANS:
8778 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008779 /* never decrease fan level, scale is:
8780 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8781 *
8782 * We expect the firmware to set either 7 or AUTO, but we
8783 * handle FULLSPEED out of paranoia.
8784 *
8785 * So, we can safely only restore FULLSPEED or 7, anything
8786 * else could slow the fan. Restoring AUTO is useless, at
8787 * best that's exactly what the DSDT already set (it is the
8788 * slower it uses).
8789 *
8790 * Always keep in mind that the DSDT *will* have set the
8791 * fans to what the vendor supposes is the best level. We
8792 * muck with it only to speed the fan up.
8793 */
8794 if (fan_control_resume_level != 7 &&
8795 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8796 return;
8797 else
8798 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8799 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008800 break;
8801 default:
8802 return;
8803 }
8804 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008805 pr_notice("restoring fan level to 0x%02x\n",
8806 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008807 rc = fan_set_level_safe(fan_control_resume_level);
8808 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008809 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008810 }
8811}
8812
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008813static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008814{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008815 int rc;
8816 u8 status;
8817 unsigned int speed = 0;
8818
8819 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008820 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008821 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008822 rc = fan_get_status_safe(&status);
8823 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008824 return rc;
8825
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008826 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008827 "level:\t\t%d\n",
8828 (status != 0) ? "enabled" : "disabled", status);
8829 break;
8830
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008831 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008832 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008833 rc = fan_get_status_safe(&status);
8834 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008835 return rc;
8836
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008837 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008838 (status != 0) ? "enabled" : "disabled");
8839
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008840 rc = fan_get_speed(&speed);
8841 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008842 return rc;
8843
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008844 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008845
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008846 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008847 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008848 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008849 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008850 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008851 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008852 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008853 break;
8854
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008855 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008856 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008857 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008858 }
8859
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008860 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008861 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008862
8863 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008864 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008865 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008866 break;
8867
8868 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008869 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008870 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008871 break;
8872 }
8873 }
8874
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008875 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008876 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008877 "commands:\twatchdog <timeout> (<timeout> "
8878 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008879
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008880 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008881 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008882 " (<speed> is 0-65535)\n");
8883
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008884 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008885}
8886
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008887static int fan_write_cmd_level(const char *cmd, int *rc)
8888{
8889 int level;
8890
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008891 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008892 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008893 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008894 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008895 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008896 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008897 return 0;
8898
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008899 *rc = fan_set_level_safe(level);
8900 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008901 pr_err("level command accepted for unsupported access mode %d\n",
8902 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008903 else if (!*rc)
8904 tpacpi_disclose_usertask("procfs fan",
8905 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008906
8907 return 1;
8908}
8909
8910static int fan_write_cmd_enable(const char *cmd, int *rc)
8911{
8912 if (strlencmp(cmd, "enable") != 0)
8913 return 0;
8914
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008915 *rc = fan_set_enable();
8916 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008917 pr_err("enable command accepted for unsupported access mode %d\n",
8918 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008919 else if (!*rc)
8920 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008921
8922 return 1;
8923}
8924
8925static int fan_write_cmd_disable(const char *cmd, int *rc)
8926{
8927 if (strlencmp(cmd, "disable") != 0)
8928 return 0;
8929
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008930 *rc = fan_set_disable();
8931 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008932 pr_err("disable command accepted for unsupported access mode %d\n",
8933 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008934 else if (!*rc)
8935 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008936
8937 return 1;
8938}
8939
8940static int fan_write_cmd_speed(const char *cmd, int *rc)
8941{
8942 int speed;
8943
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008944 /* TODO:
8945 * Support speed <low> <medium> <high> ? */
8946
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008947 if (sscanf(cmd, "speed %d", &speed) != 1)
8948 return 0;
8949
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008950 *rc = fan_set_speed(speed);
8951 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008952 pr_err("speed command accepted for unsupported access mode %d\n",
8953 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008954 else if (!*rc)
8955 tpacpi_disclose_usertask("procfs fan",
8956 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008957
8958 return 1;
8959}
8960
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008961static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8962{
8963 int interval;
8964
8965 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8966 return 0;
8967
8968 if (interval < 0 || interval > 120)
8969 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008970 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008971 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008972 tpacpi_disclose_usertask("procfs fan",
8973 "set watchdog timer to %d\n",
8974 interval);
8975 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008976
8977 return 1;
8978}
8979
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008980static int fan_write(char *buf)
8981{
8982 char *cmd;
8983 int rc = 0;
8984
8985 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008986 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008987 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008988 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008989 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008990 fan_write_cmd_disable(cmd, &rc) ||
8991 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008992 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008993 fan_write_cmd_speed(cmd, &rc))
8994 )
8995 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008996 else if (!rc)
8997 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008998 }
8999
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009000 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009001}
9002
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009003static struct ibm_struct fan_driver_data = {
9004 .name = "fan",
9005 .read = fan_read,
9006 .write = fan_write,
9007 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03009008 .suspend = fan_suspend,
9009 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009010};
9011
David Henningsson420f9732013-10-16 23:10:31 +02009012/*************************************************************************
9013 * Mute LED subdriver
9014 */
9015
9016
9017struct tp_led_table {
9018 acpi_string name;
9019 int on_value;
9020 int off_value;
9021 int state;
9022};
9023
9024static struct tp_led_table led_tables[] = {
9025 [TPACPI_LED_MUTE] = {
9026 .name = "SSMS",
9027 .on_value = 1,
9028 .off_value = 0,
9029 },
9030 [TPACPI_LED_MICMUTE] = {
9031 .name = "MMTS",
9032 .on_value = 2,
9033 .off_value = 0,
9034 },
9035};
9036
9037static int mute_led_on_off(struct tp_led_table *t, bool state)
9038{
9039 acpi_handle temp;
9040 int output;
9041
Axel Lin3526eca2016-09-19 09:33:51 +08009042 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009043 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9044 return -EIO;
9045 }
9046
9047 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9048 state ? t->on_value : t->off_value))
9049 return -EIO;
9050
9051 t->state = state;
9052 return state;
9053}
9054
9055int tpacpi_led_set(int whichled, bool on)
9056{
9057 struct tp_led_table *t;
9058
9059 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9060 return -EINVAL;
9061
9062 t = &led_tables[whichled];
9063 if (t->state < 0 || t->state == on)
9064 return t->state;
9065 return mute_led_on_off(t, on);
9066}
9067EXPORT_SYMBOL_GPL(tpacpi_led_set);
9068
9069static int mute_led_init(struct ibm_init_struct *iibm)
9070{
9071 acpi_handle temp;
9072 int i;
9073
9074 for (i = 0; i < TPACPI_LED_MAX; i++) {
9075 struct tp_led_table *t = &led_tables[i];
9076 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9077 mute_led_on_off(t, false);
9078 else
9079 t->state = -ENODEV;
9080 }
9081 return 0;
9082}
9083
9084static void mute_led_exit(void)
9085{
9086 int i;
9087
9088 for (i = 0; i < TPACPI_LED_MAX; i++)
9089 tpacpi_led_set(i, false);
9090}
9091
Takashi Iwai119f4492014-02-12 16:32:45 +01009092static void mute_led_resume(void)
9093{
9094 int i;
9095
9096 for (i = 0; i < TPACPI_LED_MAX; i++) {
9097 struct tp_led_table *t = &led_tables[i];
9098 if (t->state >= 0)
9099 mute_led_on_off(t, t->state);
9100 }
9101}
9102
David Henningsson420f9732013-10-16 23:10:31 +02009103static struct ibm_struct mute_led_driver_data = {
9104 .name = "mute_led",
9105 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01009106 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02009107};
9108
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009109/****************************************************************************
9110 ****************************************************************************
9111 *
9112 * Infrastructure
9113 *
9114 ****************************************************************************
9115 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07009116
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009117/*
9118 * HKEY event callout for other subdrivers go here
9119 * (yes, it is ugly, but it is quick, safe, and gets the job done
9120 */
9121static void tpacpi_driver_event(const unsigned int hkey_event)
9122{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00009123 if (ibm_backlight_device) {
9124 switch (hkey_event) {
9125 case TP_HKEY_EV_BRGHT_UP:
9126 case TP_HKEY_EV_BRGHT_DOWN:
9127 tpacpi_brightness_notify_change();
9128 }
9129 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009130 if (alsa_card) {
9131 switch (hkey_event) {
9132 case TP_HKEY_EV_VOL_UP:
9133 case TP_HKEY_EV_VOL_DOWN:
9134 case TP_HKEY_EV_VOL_MUTE:
9135 volume_alsa_notify_change();
9136 }
9137 }
Hans de Goedec685e202017-02-09 16:44:13 +01009138 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9139 enum led_brightness brightness;
9140
9141 mutex_lock(&kbdlight_mutex);
9142
9143 /*
9144 * Check the brightness actually changed, setting the brightness
9145 * through kbdlight_set_level() also triggers this event.
9146 */
9147 brightness = kbdlight_sysfs_get(NULL);
9148 if (kbdlight_brightness != brightness) {
9149 kbdlight_brightness = brightness;
9150 led_classdev_notify_brightness_hw_changed(
9151 &tpacpi_led_kbdlight.led_classdev, brightness);
9152 }
9153
9154 mutex_unlock(&kbdlight_mutex);
9155 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009156}
9157
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009158static void hotkey_driver_event(const unsigned int scancode)
9159{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03009160 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009161}
9162
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009163/* sysfs name ---------------------------------------------------------- */
9164static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
9165 struct device_attribute *attr,
9166 char *buf)
9167{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009168 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009169}
9170
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009171static DEVICE_ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009172
9173/* --------------------------------------------------------------------- */
9174
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009175/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03009176static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009177
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009178/*
9179 * Module and infrastructure proble, init and exit handling
9180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009181
Rusty Russell90ab5ee2012-01-13 09:32:20 +10309182static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009183
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009184#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009185static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009186{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009187 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009188
Jan van den Berg72a979f2014-09-17 00:01:08 +02009189 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009190}
9191#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9192
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009193static void ibm_exit(struct ibm_struct *ibm)
9194{
9195 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9196
9197 list_del_init(&ibm->all_drivers);
9198
9199 if (ibm->flags.acpi_notify_installed) {
9200 dbg_printk(TPACPI_DBG_EXIT,
9201 "%s: acpi_remove_notify_handler\n", ibm->name);
9202 BUG_ON(!ibm->acpi);
9203 acpi_remove_notify_handler(*ibm->acpi->handle,
9204 ibm->acpi->type,
9205 dispatch_acpi_notify);
9206 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009207 }
9208
9209 if (ibm->flags.proc_created) {
9210 dbg_printk(TPACPI_DBG_EXIT,
9211 "%s: remove_proc_entry\n", ibm->name);
9212 remove_proc_entry(ibm->name, proc_dir);
9213 ibm->flags.proc_created = 0;
9214 }
9215
9216 if (ibm->flags.acpi_driver_registered) {
9217 dbg_printk(TPACPI_DBG_EXIT,
9218 "%s: acpi_bus_unregister_driver\n", ibm->name);
9219 BUG_ON(!ibm->acpi);
9220 acpi_bus_unregister_driver(ibm->acpi->driver);
9221 kfree(ibm->acpi->driver);
9222 ibm->acpi->driver = NULL;
9223 ibm->flags.acpi_driver_registered = 0;
9224 }
9225
9226 if (ibm->flags.init_called && ibm->exit) {
9227 ibm->exit();
9228 ibm->flags.init_called = 0;
9229 }
9230
9231 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9232}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009233
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009234static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009235{
9236 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009237 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009238 struct proc_dir_entry *entry;
9239
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009240 BUG_ON(ibm == NULL);
9241
9242 INIT_LIST_HEAD(&ibm->all_drivers);
9243
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009244 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009245 return 0;
9246
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009247 dbg_printk(TPACPI_DBG_INIT,
9248 "probing for %s\n", ibm->name);
9249
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009250 if (iibm->init) {
9251 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009252 if (ret > 0)
9253 return 0; /* probe failed */
9254 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009255 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009256
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009257 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009258 }
9259
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009260 if (ibm->acpi) {
9261 if (ibm->acpi->hid) {
9262 ret = register_tpacpi_subdriver(ibm);
9263 if (ret)
9264 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009265 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009266
9267 if (ibm->acpi->notify) {
9268 ret = setup_acpi_notify(ibm);
9269 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07009270 pr_notice("disabling subdriver %s\n",
9271 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009272 ret = 0;
9273 goto err_out;
9274 }
9275 if (ret < 0)
9276 goto err_out;
9277 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009278 }
9279
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009280 dbg_printk(TPACPI_DBG_INIT,
9281 "%s installed\n", ibm->name);
9282
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009283 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04009284 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009285
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009286 if (!mode)
9287 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009288 if (ibm->write)
9289 mode |= S_IWUSR;
9290 entry = proc_create_data(ibm->name, mode, proc_dir,
9291 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009292 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07009293 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009294 ret = -ENODEV;
9295 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009296 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009297 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009299
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009300 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9301
Linus Torvalds1da177e2005-04-16 15:20:36 -07009302 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009303
9304err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009305 dbg_printk(TPACPI_DBG_INIT,
9306 "%s: at error exit path with result %d\n",
9307 ibm->name, ret);
9308
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009309 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +02009310 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009311}
9312
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009313/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009314
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009315static bool __pure __init tpacpi_is_fw_digit(const char c)
9316{
9317 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9318}
9319
Jan van den Berg72a979f2014-09-17 00:01:08 +02009320static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009321 const char t)
9322{
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009323 /*
9324 * Most models: xxyTkkWW (#.##c)
9325 * Ancient 570/600 and -SL lacks (#.##c)
9326 */
9327 if (s && strlen(s) >= 8 &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009328 tpacpi_is_fw_digit(s[0]) &&
9329 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009330 s[2] == t &&
9331 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009332 tpacpi_is_fw_digit(s[4]) &&
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009333 tpacpi_is_fw_digit(s[5]))
9334 return true;
9335
9336 /* New models: xxxyTkkW (#.##c); T550 and some others */
9337 return s && strlen(s) >= 8 &&
9338 tpacpi_is_fw_digit(s[0]) &&
9339 tpacpi_is_fw_digit(s[1]) &&
9340 tpacpi_is_fw_digit(s[2]) &&
9341 s[3] == t &&
9342 (s[4] == 'T' || s[4] == 'N') &&
9343 tpacpi_is_fw_digit(s[5]) &&
9344 tpacpi_is_fw_digit(s[6]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009345}
9346
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009347/* returns 0 - probe ok, or < 0 - probe error.
9348 * Probe ok doesn't mean thinkpad found.
9349 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9350static int __must_check __init get_thinkpad_model_data(
9351 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009352{
Jeff Garzik18552562007-10-03 15:15:40 -04009353 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009354 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009355 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009356
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009357 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009358 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009359
9360 memset(tp, 0, sizeof(*tp));
9361
9362 if (dmi_name_in_vendors("IBM"))
9363 tp->vendor = PCI_VENDOR_ID_IBM;
9364 else if (dmi_name_in_vendors("LENOVO"))
9365 tp->vendor = PCI_VENDOR_ID_LENOVO;
9366 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009367 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009368
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009369 s = dmi_get_system_info(DMI_BIOS_VERSION);
9370 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9371 if (s && !tp->bios_version_str)
9372 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009373
9374 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009375 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9376 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009377 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009378
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009379 tp->bios_model = tp->bios_version_str[0]
9380 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009381 tp->bios_release = (tp->bios_version_str[4] << 8)
9382 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009383
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009384 /*
9385 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9386 * X32 or newer, all Z series; Some models must have an
9387 * up-to-date BIOS or they will not be detected.
9388 *
9389 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9390 */
9391 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009392 if (sscanf(dev->name,
9393 "IBM ThinkPad Embedded Controller -[%17c",
9394 ec_fw_string) == 1) {
9395 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9396 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009397
9398 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009399 if (!tp->ec_version_str)
9400 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009401
9402 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9403 tp->ec_model = ec_fw_string[0]
9404 | (ec_fw_string[1] << 8);
9405 tp->ec_release = (ec_fw_string[4] << 8)
9406 | ec_fw_string[5];
9407 } else {
Joe Perches0978e012011-04-04 10:06:25 -07009408 pr_notice("ThinkPad firmware release %s "
9409 "doesn't match the known patterns\n",
9410 ec_fw_string);
9411 pr_notice("please report this to %s\n",
9412 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009413 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009414 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009415 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009416 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009417
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009418 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009419 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009420 tp->model_str = kstrdup(s, GFP_KERNEL);
9421 if (!tp->model_str)
9422 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009423 } else {
9424 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009425 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009426 tp->model_str = kstrdup(s, GFP_KERNEL);
9427 if (!tp->model_str)
9428 return -ENOMEM;
9429 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009430 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03009431
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009432 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9433 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9434 if (s && !tp->nummodel_str)
9435 return -ENOMEM;
9436
9437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009438}
9439
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009440static int __init probe_for_thinkpad(void)
9441{
9442 int is_thinkpad;
9443
9444 if (acpi_disabled)
9445 return -ENODEV;
9446
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009447 /* It would be dangerous to run the driver in this case */
9448 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9449 return -ENODEV;
9450
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009451 /*
9452 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009453 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009454 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009455 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9456 (thinkpad_id.ec_model != 0) ||
9457 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009458
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03009459 /* The EC handler is required */
9460 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009461 if (!ec_handle) {
9462 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07009463 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009464 return -ENODEV;
9465 }
9466
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009467 if (!is_thinkpad && !force_load)
9468 return -ENODEV;
9469
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009470 return 0;
9471}
9472
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009473static void __init thinkpad_acpi_init_banner(void)
9474{
Joe Perches0978e012011-04-04 10:06:25 -07009475 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9476 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009477
Joe Perches0978e012011-04-04 10:06:25 -07009478 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009479 (thinkpad_id.bios_version_str) ?
9480 thinkpad_id.bios_version_str : "unknown",
9481 (thinkpad_id.ec_version_str) ?
9482 thinkpad_id.ec_version_str : "unknown");
9483
9484 BUG_ON(!thinkpad_id.vendor);
9485
9486 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07009487 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009488 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9489 "IBM" : ((thinkpad_id.vendor ==
9490 PCI_VENDOR_ID_LENOVO) ?
9491 "Lenovo" : "Unknown vendor"),
9492 thinkpad_id.model_str,
9493 (thinkpad_id.nummodel_str) ?
9494 thinkpad_id.nummodel_str : "unknown");
9495}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009496
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009497/* Module init, exit, parameters */
9498
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009499static struct ibm_init_struct ibms_init[] __initdata = {
9500 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009501 .data = &thinkpad_acpi_driver_data,
9502 },
9503 {
9504 .init = hotkey_init,
9505 .data = &hotkey_driver_data,
9506 },
9507 {
9508 .init = bluetooth_init,
9509 .data = &bluetooth_driver_data,
9510 },
9511 {
9512 .init = wan_init,
9513 .data = &wan_driver_data,
9514 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009515 {
9516 .init = uwb_init,
9517 .data = &uwb_driver_data,
9518 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009519#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009520 {
9521 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009522 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009523 .data = &video_driver_data,
9524 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009525#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009526 {
Pali Rohárbb28f3d52015-12-30 23:27:41 +01009527 .init = kbdlight_init,
9528 .data = &kbdlight_driver_data,
9529 },
9530 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009531 .init = light_init,
9532 .data = &light_driver_data,
9533 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009534 {
9535 .init = cmos_init,
9536 .data = &cmos_driver_data,
9537 },
9538 {
9539 .init = led_init,
9540 .data = &led_driver_data,
9541 },
9542 {
9543 .init = beep_init,
9544 .data = &beep_driver_data,
9545 },
9546 {
9547 .init = thermal_init,
9548 .data = &thermal_driver_data,
9549 },
9550 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009551 .init = brightness_init,
9552 .data = &brightness_driver_data,
9553 },
9554 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009555 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009556 .data = &volume_driver_data,
9557 },
9558 {
9559 .init = fan_init,
9560 .data = &fan_driver_data,
9561 },
David Henningsson420f9732013-10-16 23:10:31 +02009562 {
9563 .init = mute_led_init,
9564 .data = &mute_led_driver_data,
9565 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009566};
9567
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009568static int __init set_ibm_param(const char *val, struct kernel_param *kp)
9569{
9570 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009571 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009572
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009573 if (!kp || !kp->name || !val)
9574 return -EINVAL;
9575
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009576 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9577 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009578 WARN_ON(ibm == NULL);
9579
9580 if (!ibm || !ibm->name)
9581 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009582
9583 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9584 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009585 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009586 strcpy(ibms_init[i].param, val);
9587 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009588 return 0;
9589 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009590 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009591
9592 return -EINVAL;
9593}
9594
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009595module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009596MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009597 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009598
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009599module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009600MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009601
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009602module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009603MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009604 "Attempts to load the driver even on a "
9605 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009606
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009607module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009608MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009609 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03009610
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009611module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009612MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009613 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00009614 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03009615
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009616module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009617MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009618 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02009619
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009620#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009621module_param_named(volume_mode, volume_mode, uint, 0444);
9622MODULE_PARM_DESC(volume_mode,
9623 "Selects volume control strategy: "
9624 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9625
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02009626module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9627MODULE_PARM_DESC(volume_capabilities,
9628 "Selects the mixer capabilites: "
9629 "0=auto, 1=volume and mute, 2=mute only");
9630
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02009631module_param_named(volume_control, volume_control_allowed, bool, 0444);
9632MODULE_PARM_DESC(volume_control,
9633 "Enables software override for the console audio "
9634 "control when true");
9635
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07009636module_param_named(software_mute, software_mute_requested, bool, 0444);
9637MODULE_PARM_DESC(software_mute,
9638 "Request full software mute control");
9639
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009640/* ALSA module API parameters */
9641module_param_named(index, alsa_index, int, 0444);
9642MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9643module_param_named(id, alsa_id, charp, 0444);
9644MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9645module_param_named(enable, alsa_enable, bool, 0444);
9646MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009647#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009648
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009649#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009650 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02009651 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009652 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009653
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009654TPACPI_PARAM(hotkey);
9655TPACPI_PARAM(bluetooth);
9656TPACPI_PARAM(video);
9657TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009658TPACPI_PARAM(cmos);
9659TPACPI_PARAM(led);
9660TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009661TPACPI_PARAM(brightness);
9662TPACPI_PARAM(volume);
9663TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009664
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009665#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009666module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009667MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9668module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9669MODULE_PARM_DESC(wlsw_state,
9670 "Initial state of the emulated WLSW switch");
9671
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009672module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009673MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9674module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9675MODULE_PARM_DESC(bluetooth_state,
9676 "Initial state of the emulated bluetooth switch");
9677
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009678module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009679MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9680module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9681MODULE_PARM_DESC(wwan_state,
9682 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009683
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009684module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009685MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9686module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9687MODULE_PARM_DESC(uwb_state,
9688 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009689#endif
9690
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009691static void thinkpad_acpi_module_exit(void)
9692{
9693 struct ibm_struct *ibm, *itmp;
9694
9695 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9696
9697 list_for_each_entry_safe_reverse(ibm, itmp,
9698 &tpacpi_all_drivers,
9699 all_drivers) {
9700 ibm_exit(ibm);
9701 }
9702
9703 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9704
9705 if (tpacpi_inputdev) {
9706 if (tp_features.input_device_registered)
9707 input_unregister_device(tpacpi_inputdev);
9708 else
9709 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +10009710 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009711 }
9712
9713 if (tpacpi_hwmon)
9714 hwmon_device_unregister(tpacpi_hwmon);
9715
9716 if (tp_features.sensors_pdev_attrs_registered)
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009717 device_remove_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009718 if (tpacpi_sensors_pdev)
9719 platform_device_unregister(tpacpi_sensors_pdev);
9720 if (tpacpi_pdev)
9721 platform_device_unregister(tpacpi_pdev);
9722
9723 if (tp_features.sensors_pdrv_attrs_registered)
9724 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9725 if (tp_features.platform_drv_attrs_registered)
9726 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9727
9728 if (tp_features.sensors_pdrv_registered)
9729 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9730
9731 if (tp_features.platform_drv_registered)
9732 platform_driver_unregister(&tpacpi_pdriver);
9733
9734 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009735 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009736
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009737 if (tpacpi_wq)
9738 destroy_workqueue(tpacpi_wq);
9739
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009740 kfree(thinkpad_id.bios_version_str);
9741 kfree(thinkpad_id.ec_version_str);
9742 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +10009743 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009744}
9745
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009746
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009747static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009748{
9749 int ret, i;
9750
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009751 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9752
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009753 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009754
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009755 ret = get_thinkpad_model_data(&thinkpad_id);
9756 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009757 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009758 thinkpad_acpi_module_exit();
9759 return ret;
9760 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009761 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009762 if (ret) {
9763 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009764 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009766
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009767 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009768
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009769 thinkpad_acpi_init_banner();
9770 tpacpi_check_outdated_fw();
9771
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009772 TPACPI_ACPIHANDLE_INIT(ecrd);
9773 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009774
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009775 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9776 if (!tpacpi_wq) {
9777 thinkpad_acpi_module_exit();
9778 return -ENOMEM;
9779 }
9780
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009781 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009782 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07009783 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009784 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785 return -ENODEV;
9786 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009787
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009788 ret = platform_driver_register(&tpacpi_pdriver);
9789 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009790 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009791 thinkpad_acpi_module_exit();
9792 return ret;
9793 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009794 tp_features.platform_drv_registered = 1;
9795
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009796 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9797 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009798 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009799 thinkpad_acpi_module_exit();
9800 return ret;
9801 }
9802 tp_features.sensors_pdrv_registered = 1;
9803
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009804 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009805 if (!ret) {
9806 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009807 ret = tpacpi_create_driver_attributes(
9808 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009809 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009810 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009811 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009812 thinkpad_acpi_module_exit();
9813 return ret;
9814 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009815 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009816
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009817
9818 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009819 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009820 NULL, 0);
9821 if (IS_ERR(tpacpi_pdev)) {
9822 ret = PTR_ERR(tpacpi_pdev);
9823 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009824 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009825 thinkpad_acpi_module_exit();
9826 return ret;
9827 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009828 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009829 TPACPI_HWMON_DRVR_NAME,
9830 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009831 if (IS_ERR(tpacpi_sensors_pdev)) {
9832 ret = PTR_ERR(tpacpi_sensors_pdev);
9833 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009834 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009835 thinkpad_acpi_module_exit();
9836 return ret;
9837 }
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009838 ret = device_create_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009839 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009840 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009841 thinkpad_acpi_module_exit();
9842 return ret;
9843 }
9844 tp_features.sensors_pdev_attrs_registered = 1;
9845 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009846 if (IS_ERR(tpacpi_hwmon)) {
9847 ret = PTR_ERR(tpacpi_hwmon);
9848 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009849 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009850 thinkpad_acpi_module_exit();
9851 return ret;
9852 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009853 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009854 tpacpi_inputdev = input_allocate_device();
9855 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009856 thinkpad_acpi_module_exit();
9857 return -ENOMEM;
9858 } else {
9859 /* Prepare input device, but don't register */
9860 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009861 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009862 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009863 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009864 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9865 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009866 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009867 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009868
9869 /* Init subdriver dependencies */
9870 tpacpi_detect_brightness_capabilities();
9871
9872 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009873 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9874 ret = ibm_init(&ibms_init[i]);
9875 if (ret >= 0 && *ibms_init[i].param)
9876 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009877 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009878 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009879 return ret;
9880 }
9881 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009882
9883 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9884
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009885 ret = input_register_device(tpacpi_inputdev);
9886 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009887 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009888 thinkpad_acpi_module_exit();
9889 return ret;
9890 } else {
9891 tp_features.input_device_registered = 1;
9892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009893
9894 return 0;
9895}
9896
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009897MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9898
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009899/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009900 * This will autoload the driver in almost every ThinkPad
9901 * in widespread use.
9902 *
9903 * Only _VERY_ old models, like the 240, 240x and 570 lack
9904 * the HKEY event interface.
9905 */
9906MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9907
9908/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009909 * DMI matching for module autoloading
9910 *
9911 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9912 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9913 *
9914 * Only models listed in thinkwiki will be supported, so add yours
9915 * if it is not there yet.
9916 */
9917#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009918 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009919
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009920/* Ancient thinkpad BIOSes have to be identified by
9921 * BIOS type or model number, and there are far less
9922 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009923IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009924
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009925MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9926MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009927MODULE_DESCRIPTION(TPACPI_DESC);
9928MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009929MODULE_LICENSE("GPL");
9930
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009931module_init(thinkpad_acpi_module_init);
9932module_exit(thinkpad_acpi_module_exit);