blob: e2b962f0fdc481c8a676bde17a127cf76ce718e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Joe Perches0978e012011-04-04 10:06:25 -070024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
David Henningsson420f9732013-10-16 23:10:31 +020026#define TPACPI_VERSION "0.25"
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020027#define TPACPI_SYSFS_VERSION 0x020700
Borislav Deianov78f81cc2005-08-17 00:00:00 -040028
29/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020031 * 2007-10-20 changelog trimmed down
32 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030033 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020035 *
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030039 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040040 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020042 *
43 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040044 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
51
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020052#include <linux/kernel.h>
53#include <linux/module.h>
54#include <linux/init.h>
55#include <linux/types.h>
56#include <linux/string.h>
57#include <linux/list.h>
58#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000059#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020060#include <linux/kthread.h>
61#include <linux/freezer.h>
62#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020064#include <linux/nvram.h>
65#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020066#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020067#include <linux/sysfs.h>
68#include <linux/backlight.h>
69#include <linux/fb.h>
70#include <linux/platform_device.h>
71#include <linux/hwmon.h>
72#include <linux/hwmon-sysfs.h>
73#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030074#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030075#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020076#include <linux/dmi.h>
77#include <linux/jiffies.h>
78#include <linux/workqueue.h>
Lv Zheng8b484632013-12-03 08:49:16 +080079#include <linux/acpi.h>
80#include <linux/pci_ids.h>
81#include <linux/thinkpad_acpi.h>
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020082#include <sound/core.h>
83#include <sound/control.h>
84#include <sound/initval.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080085#include <linux/uaccess.h>
Hans de Goedeb33c6ce2015-06-16 16:28:10 +020086#include <acpi/video.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020087
88/* ThinkPad CMOS commands */
89#define TP_CMOS_VOLUME_DOWN 0
90#define TP_CMOS_VOLUME_UP 1
91#define TP_CMOS_VOLUME_MUTE 2
92#define TP_CMOS_BRIGHTNESS_UP 4
93#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030094#define TP_CMOS_THINKLIGHT_ON 12
95#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020096
97/* NVRAM Addresses */
98enum tp_nvram_addr {
99 TP_NVRAM_ADDR_HK2 = 0x57,
100 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
101 TP_NVRAM_ADDR_VIDEO = 0x59,
102 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
103 TP_NVRAM_ADDR_MIXER = 0x60,
104};
105
106/* NVRAM bit masks */
107enum {
108 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
109 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
110 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
111 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
112 TP_NVRAM_MASK_THINKLIGHT = 0x10,
113 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
114 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
115 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
116 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
117 TP_NVRAM_MASK_MUTE = 0x40,
118 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
119 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
120 TP_NVRAM_POS_LEVEL_VOLUME = 0,
121};
122
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300123/* Misc NVRAM-related */
124enum {
125 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
126};
127
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200128/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400129#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
130#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Hui Wanga3c42a42016-11-08 16:13:23 +0800131#define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300132#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200133
134/* Input IDs */
135#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
136#define TPACPI_HKEY_INPUT_VERSION 0x4101
137
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200138/* ACPI \WGSV commands */
139enum {
140 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
141 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
142 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
143 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
144};
145
146/* TP_ACPI_WGSV_GET_STATE bits */
147enum {
148 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
149 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
150 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
151 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
152 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
153 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
154 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
155 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
156 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
157 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
158};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200159
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300160/* HKEY events */
161enum tpacpi_hkey_event_t {
162 /* Hotkey-related */
163 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
164 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
165 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
Hans de Goedec685e202017-02-09 16:44:13 +0100166 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300167 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
168 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
169 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
170
171 /* Reasons for waking up from S3/S4 */
172 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
173 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
174 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
175 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
176 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
177 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
178
179 /* Auto-sleep after eject request */
180 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
181 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
182
183 /* Misc bay events */
184 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -0300185 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
186 or port replicator */
187 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
188 dock or port replicator */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300189
190 /* User-interface events */
191 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
192 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
193 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
194 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
Lyudeb03f4d42016-11-11 15:15:03 -0500195 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
196 * enter/leave tablet mode
197 */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300198 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
199 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
200 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
201
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300202 /* Key-related user-interface events */
203 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
204 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
Xavier Naveira67ab6242015-02-10 08:45:18 +0100205 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300206
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300207 /* Thermal events */
208 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
209 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
210 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
211 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
212 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
213
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100214 /* AC-related events */
215 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300216
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300217 /* Misc */
218 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
219};
220
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200221/****************************************************************************
222 * Main driver
223 */
224
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200225#define TPACPI_NAME "thinkpad"
226#define TPACPI_DESC "ThinkPad ACPI Extras"
227#define TPACPI_FILE TPACPI_NAME "_acpi"
228#define TPACPI_URL "http://ibm-acpi.sf.net/"
229#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200230
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200231#define TPACPI_PROC_DIR "ibm"
232#define TPACPI_ACPI_EVENT_PREFIX "ibm"
233#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300234#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200235#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200236
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300237#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300238#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300239
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200240#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200241
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000242/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200243#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000244#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200245#define TPACPI_DBG_INIT 0x0001
246#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +0000247#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000248#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000249#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000250#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200251#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200252
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200253#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
254#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
255#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200256
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200257
258/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200259 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200260 */
261
262struct ibm_struct;
263
264struct tp_acpi_drv_struct {
265 const struct acpi_device_id *hid;
266 struct acpi_driver *driver;
267
268 void (*notify) (struct ibm_struct *, u32);
269 acpi_handle *handle;
270 u32 type;
271 struct acpi_device *device;
272};
273
274struct ibm_struct {
275 char *name;
276
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200277 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200278 int (*write) (char *);
279 void (*exit) (void);
280 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200281 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200282 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200283
284 struct list_head all_drivers;
285
286 struct tp_acpi_drv_struct *acpi;
287
288 struct {
289 u8 acpi_driver_registered:1;
290 u8 acpi_notify_installed:1;
291 u8 proc_created:1;
292 u8 init_called:1;
293 u8 experimental:1;
294 } flags;
295};
296
297struct ibm_init_struct {
298 char param[32];
299
300 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400301 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200302 struct ibm_struct *data;
303};
304
305static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200306 u32 bluetooth:1;
307 u32 hotkey:1;
308 u32 hotkey_mask:1;
309 u32 hotkey_wlsw:1;
Lyudeb3180022016-11-11 15:15:02 -0500310 enum {
311 TP_HOTKEY_TABLET_NONE = 0,
312 TP_HOTKEY_TABLET_USES_MHKG,
Lyudeb03f4d42016-11-11 15:15:03 -0500313 /* X1 Yoga 2016, seen on BIOS N1FET44W */
314 TP_HOTKEY_TABLET_USES_CMMD,
Lyudeb3180022016-11-11 15:15:02 -0500315 } hotkey_tablet;
Pali Rohárbb28f3d52015-12-30 23:27:41 +0100316 u32 kbdlight:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200317 u32 light:1;
318 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300319 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300320 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200321 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200322 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200323 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300324 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300325 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200326 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200327 u32 input_device_registered:1;
328 u32 platform_drv_registered:1;
329 u32 platform_drv_attrs_registered:1;
330 u32 sensors_pdrv_registered:1;
331 u32 sensors_pdrv_attrs_registered:1;
332 u32 sensors_pdev_attrs_registered:1;
333 u32 hotkey_poll_active:1;
Bastien Noceraf23a5bc2015-03-02 14:45:16 +0100334 u32 has_adaptive_kbd:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200335} tp_features;
336
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300337static struct {
338 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200339 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300340} tp_warned;
341
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200342struct thinkpad_id_data {
343 unsigned int vendor; /* ThinkPad vendor:
344 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
345
346 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
347 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
348
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300349 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200350 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300351 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
352 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200353
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300354 char *model_str; /* ThinkPad T43 */
355 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200356};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200357static struct thinkpad_id_data thinkpad_id;
358
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300359static enum {
360 TPACPI_LIFE_INIT = 0,
361 TPACPI_LIFE_RUNNING,
362 TPACPI_LIFE_EXITING,
363} tpacpi_lifecycle;
364
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200365static int experimental;
366static u32 dbg_level;
367
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300368static struct workqueue_struct *tpacpi_wq;
369
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000370enum led_status_t {
371 TPACPI_LED_OFF = 0,
372 TPACPI_LED_ON,
373 TPACPI_LED_BLINK,
374};
375
Hans de Goede86ec0c22017-02-09 16:44:12 +0100376/* tpacpi LED class */
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300377struct tpacpi_led_classdev {
378 struct led_classdev led_classdev;
Adam Leeedf2d772013-06-08 16:51:15 +0800379 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300380};
381
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300382/* brightness level capabilities */
383static unsigned int bright_maxlvl; /* 0 = unknown */
384
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200385#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
386static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030387static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200388static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030389static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200390static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030391static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200392static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030393static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200394#endif
395
396
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000397/*************************************************************************
398 * Debugging helpers
399 */
400
Joe Perches0978e012011-04-04 10:06:25 -0700401#define dbg_printk(a_dbg_level, format, arg...) \
402do { \
403 if (dbg_level & (a_dbg_level)) \
404 printk(KERN_DEBUG pr_fmt("%s: " format), \
405 __func__, ##arg); \
406} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000407
408#ifdef CONFIG_THINKPAD_ACPI_DEBUG
409#define vdbg_printk dbg_printk
410static const char *str_supported(int is_supported);
411#else
Joe Perches0978e012011-04-04 10:06:25 -0700412static inline const char *str_supported(int is_supported) { return ""; }
413#define vdbg_printk(a_dbg_level, format, arg...) \
Joe Perchesefd85cf2015-08-26 11:13:38 -0700414 do { if (0) no_printk(format, ##arg); } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000415#endif
416
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000417static void tpacpi_log_usertask(const char * const what)
418{
Joe Perches0978e012011-04-04 10:06:25 -0700419 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
420 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000421}
422
Joe Perches0978e012011-04-04 10:06:25 -0700423#define tpacpi_disclose_usertask(what, format, arg...) \
424do { \
425 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
426 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
427 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
428 what, task_tgid_vnr(current), ## arg); \
429 } \
430} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000431
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300432/*
433 * Quirk handling helpers
434 *
435 * ThinkPad IDs and versions seen in the field so far
436 * are two-characters from the set [0-9A-Z], i.e. base 36.
437 *
438 * We use values well outside that range as specials.
439 */
440
441#define TPACPI_MATCH_ANY 0xffffU
442#define TPACPI_MATCH_UNKNOWN 0U
443
444/* TPID('1', 'Y') == 0x5931 */
445#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
446
447#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_IBM, \
449 .bios = TPID(__id1, __id2), \
450 .ec = TPACPI_MATCH_ANY, \
451 .quirks = (__quirk) }
452
453#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
454 { .vendor = PCI_VENDOR_ID_LENOVO, \
455 .bios = TPID(__id1, __id2), \
456 .ec = TPACPI_MATCH_ANY, \
457 .quirks = (__quirk) }
458
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200459#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
460 { .vendor = PCI_VENDOR_ID_LENOVO, \
461 .bios = TPACPI_MATCH_ANY, \
462 .ec = TPID(__id1, __id2), \
463 .quirks = (__quirk) }
464
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300465struct tpacpi_quirk {
466 unsigned int vendor;
467 u16 bios;
468 u16 ec;
469 unsigned long quirks;
470};
471
472/**
473 * tpacpi_check_quirks() - search BIOS/EC version on a list
474 * @qlist: array of &struct tpacpi_quirk
475 * @qlist_size: number of elements in @qlist
476 *
477 * Iterates over a quirks list until one is found that matches the
478 * ThinkPad's vendor, BIOS and EC model.
479 *
480 * Returns 0 if nothing matches, otherwise returns the quirks field of
481 * the matching &struct tpacpi_quirk entry.
482 *
483 * The match criteria is: vendor, ec and bios much match.
484 */
485static unsigned long __init tpacpi_check_quirks(
486 const struct tpacpi_quirk *qlist,
487 unsigned int qlist_size)
488{
489 while (qlist_size) {
490 if ((qlist->vendor == thinkpad_id.vendor ||
491 qlist->vendor == TPACPI_MATCH_ANY) &&
492 (qlist->bios == thinkpad_id.bios_model ||
493 qlist->bios == TPACPI_MATCH_ANY) &&
494 (qlist->ec == thinkpad_id.ec_model ||
495 qlist->ec == TPACPI_MATCH_ANY))
496 return qlist->quirks;
497
498 qlist_size--;
499 qlist++;
500 }
501 return 0;
502}
503
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300504static inline bool __pure __init tpacpi_is_lenovo(void)
505{
506 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
507}
508
509static inline bool __pure __init tpacpi_is_ibm(void)
510{
511 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
512}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300513
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300514/****************************************************************************
515 ****************************************************************************
516 *
517 * ACPI Helpers and device model
518 *
519 ****************************************************************************
520 ****************************************************************************/
521
522/*************************************************************************
523 * ACPI basic handles
524 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300526static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300527static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200529#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700531 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300532 &parent##_handle; \
533 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200535TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
536TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200538TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
539 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400540 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
541 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300542 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400543
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200544TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400545 "^HKEY", /* R30, R31 */
546 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300547 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400548
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300549/*************************************************************************
550 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200551 */
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700554 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400557 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200558 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400559 struct acpi_buffer result, *resultp;
560 union acpi_object out_obj;
561 acpi_status status;
562 va_list ap;
563 char res_type;
564 int success;
565 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700568 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return 0;
570 }
571
572 if (*fmt == 'q') {
573 quiet = 1;
574 fmt++;
575 } else
576 quiet = 0;
577
578 res_type = *(fmt++);
579
580 params.count = 0;
581 params.pointer = &in_objs[0];
582
583 va_start(ap, fmt);
584 while (*fmt) {
585 char c = *(fmt++);
586 switch (c) {
587 case 'd': /* int */
588 in_objs[params.count].integer.value = va_arg(ap, int);
589 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
590 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400591 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 default:
Joe Perches0978e012011-04-04 10:06:25 -0700593 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 "with invalid format character '%c'\n", c);
Jesper Juhl21365852010-12-24 19:56:28 +0100595 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return 0;
597 }
598 }
599 va_end(ap);
600
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400601 if (res_type != 'v') {
602 result.length = sizeof(out_obj);
603 result.pointer = &out_obj;
604 resultp = &result;
605 } else
606 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400608 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400611 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300612 success = (status == AE_OK &&
613 out_obj.type == ACPI_TYPE_INTEGER);
614 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700615 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400617 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 success = status == AE_OK;
619 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400620 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 default:
Joe Perches0978e012011-04-04 10:06:25 -0700622 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 "with invalid format character '%c'\n", res_type);
624 return 0;
625 }
626
627 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700628 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300629 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 return success;
632}
633
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200634static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300635{
636 int v;
637
638 if (ecrd_handle) {
639 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
640 return 0;
641 *p = v;
642 } else {
643 if (ec_read(i, p) < 0)
644 return 0;
645 }
646
647 return 1;
648}
649
650static int acpi_ec_write(int i, u8 v)
651{
652 if (ecwr_handle) {
653 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
654 return 0;
655 } else {
656 if (ec_write(i, v) < 0)
657 return 0;
658 }
659
660 return 1;
661}
662
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300663static int issue_thinkpad_cmos_command(int cmos_cmd)
664{
665 if (!cmos_handle)
666 return -ENXIO;
667
668 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
669 return -EIO;
670
671 return 0;
672}
673
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300674/*************************************************************************
675 * ACPI device model
676 */
677
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200678#define TPACPI_ACPIHANDLE_INIT(object) \
679 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300680 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200681
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300682static void __init drv_acpi_handle_init(const char *name,
683 acpi_handle *handle, const acpi_handle parent,
684 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300685{
686 int i;
687 acpi_status status;
688
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300689 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
690 name);
691
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300692 for (i = 0; i < num_paths; i++) {
693 status = acpi_get_handle(parent, paths[i], handle);
694 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300695 dbg_printk(TPACPI_DBG_INIT,
696 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300697 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300698 return;
699 }
700 }
701
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300702 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
703 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300704 *handle = NULL;
705}
706
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300707static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
708 u32 level, void *context, void **return_value)
709{
Aaron Lu46445b62013-10-11 21:27:46 +0800710 struct acpi_device *dev;
711 if (!strcmp(context, "video")) {
712 if (acpi_bus_get_device(handle, &dev))
713 return AE_OK;
714 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
715 return AE_OK;
716 }
717
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300718 *(acpi_handle *)return_value = handle;
719
720 return AE_CTRL_TERMINATE;
721}
722
723static void __init tpacpi_acpi_handle_locate(const char *name,
724 const char *hid,
725 acpi_handle *handle)
726{
727 acpi_status status;
728 acpi_handle device_found;
729
Aaron Lu46445b62013-10-11 21:27:46 +0800730 BUG_ON(!name || !handle);
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300731 vdbg_printk(TPACPI_DBG_INIT,
732 "trying to locate ACPI handle for %s, using HID %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +0800733 name, hid ? hid : "NULL");
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300734
735 memset(&device_found, 0, sizeof(device_found));
736 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
737 (void *)name, &device_found);
738
739 *handle = NULL;
740
741 if (ACPI_SUCCESS(status)) {
742 *handle = device_found;
743 dbg_printk(TPACPI_DBG_INIT,
744 "Found ACPI handle for %s\n", name);
745 } else {
746 vdbg_printk(TPACPI_DBG_INIT,
747 "Could not locate an ACPI handle for %s: %s\n",
748 name, acpi_format_exception(status));
749 }
750}
751
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300752static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300753{
754 struct ibm_struct *ibm = data;
755
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300756 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
757 return;
758
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300759 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300760 return;
761
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300762 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300763}
764
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300765static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300766{
767 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300768 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300769
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300770 BUG_ON(!ibm->acpi);
771
772 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300773 return 0;
774
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300775 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300776 "setting up ACPI notify for %s\n", ibm->name);
777
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300778 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
779 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700780 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300781 return -ENODEV;
782 }
783
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700784 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300785 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200786 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300787 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300788
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300789 status = acpi_install_notify_handler(*ibm->acpi->handle,
790 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300791 if (ACPI_FAILURE(status)) {
792 if (status == AE_ALREADY_EXISTS) {
Joe Perches0978e012011-04-04 10:06:25 -0700793 pr_notice("another device driver is already "
794 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300795 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700796 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300797 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300798 }
799 return -ENODEV;
800 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300801 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300802 return 0;
803}
804
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300805static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300806{
807 return 0;
808}
809
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300810static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300811{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300812 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300813
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300814 dbg_printk(TPACPI_DBG_INIT,
815 "registering %s as an ACPI driver\n", ibm->name);
816
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300817 BUG_ON(!ibm->acpi);
818
819 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
820 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700821 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300822 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300823 }
824
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200825 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300826 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200827
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300828 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300829
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300830 rc = acpi_bus_register_driver(ibm->acpi->driver);
831 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700832 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200833 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300834 kfree(ibm->acpi->driver);
835 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300836 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300837 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300838
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300839 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300840}
841
842
843/****************************************************************************
844 ****************************************************************************
845 *
846 * Procfs Helpers
847 *
848 ****************************************************************************
849 ****************************************************************************/
850
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200851static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300852{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200853 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300854
855 if (!ibm || !ibm->read)
856 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200857 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300858}
859
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200860static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300861{
Al Virod9dda782013-03-31 18:16:14 -0400862 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200863}
864
865static ssize_t dispatch_proc_write(struct file *file,
866 const char __user *userbuf,
867 size_t count, loff_t *pos)
868{
Al Virod9dda782013-03-31 18:16:14 -0400869 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300870 char *kernbuf;
871 int ret;
872
873 if (!ibm || !ibm->write)
874 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300875 if (count > PAGE_SIZE - 2)
876 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300877
878 kernbuf = kmalloc(count + 2, GFP_KERNEL);
879 if (!kernbuf)
880 return -ENOMEM;
881
882 if (copy_from_user(kernbuf, userbuf, count)) {
883 kfree(kernbuf);
884 return -EFAULT;
885 }
886
887 kernbuf[count] = 0;
888 strcat(kernbuf, ",");
889 ret = ibm->write(kernbuf);
890 if (ret == 0)
891 ret = count;
892
893 kfree(kernbuf);
894
895 return ret;
896}
897
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200898static const struct file_operations dispatch_proc_fops = {
899 .owner = THIS_MODULE,
900 .open = dispatch_proc_open,
901 .read = seq_read,
902 .llseek = seq_lseek,
903 .release = single_release,
904 .write = dispatch_proc_write,
905};
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static char *next_cmd(char **cmds)
908{
909 char *start = *cmds;
910 char *end;
911
912 while ((end = strchr(start, ',')) && end == start)
913 start = end + 1;
914
915 if (!end)
916 return NULL;
917
918 *end = 0;
919 *cmds = end + 1;
920 return start;
921}
922
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300923
924/****************************************************************************
925 ****************************************************************************
926 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300927 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300928 *
929 ****************************************************************************
930 ****************************************************************************/
931
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300932static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300933static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700934static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300935static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300936static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200937static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300938
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200939#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200940static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200941{
942 struct ibm_struct *ibm, *itmp;
943
944 list_for_each_entry_safe(ibm, itmp,
945 &tpacpi_all_drivers,
946 all_drivers) {
947 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200948 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200949 }
950
951 return 0;
952}
953
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200954static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300955{
956 struct ibm_struct *ibm, *itmp;
957
958 list_for_each_entry_safe(ibm, itmp,
959 &tpacpi_all_drivers,
960 all_drivers) {
961 if (ibm->resume)
962 (ibm->resume)();
963 }
964
965 return 0;
966}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200967#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300968
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200969static SIMPLE_DEV_PM_OPS(tpacpi_pm,
970 tpacpi_suspend_handler, tpacpi_resume_handler);
971
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200972static void tpacpi_shutdown_handler(struct platform_device *pdev)
973{
974 struct ibm_struct *ibm, *itmp;
975
976 list_for_each_entry_safe(ibm, itmp,
977 &tpacpi_all_drivers,
978 all_drivers) {
979 if (ibm->shutdown)
980 (ibm->shutdown)();
981 }
982}
983
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300984static struct platform_driver tpacpi_pdriver = {
985 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200986 .name = TPACPI_DRVR_NAME,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200987 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300988 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200989 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300990};
991
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300992static struct platform_driver tpacpi_hwmon_pdriver = {
993 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200994 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300995 },
996};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300997
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300998/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300999 * sysfs support helpers
1000 */
1001
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001002struct attribute_set {
1003 unsigned int members, max_members;
1004 struct attribute_group group;
1005};
1006
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001007struct attribute_set_obj {
1008 struct attribute_set s;
1009 struct attribute *a;
1010} __attribute__((packed));
1011
1012static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001013 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001014{
1015 struct attribute_set_obj *sobj;
1016
1017 if (max_members == 0)
1018 return NULL;
1019
1020 /* Allocates space for implicit NULL at the end too */
1021 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1022 max_members * sizeof(struct attribute *),
1023 GFP_KERNEL);
1024 if (!sobj)
1025 return NULL;
1026 sobj->s.max_members = max_members;
1027 sobj->s.group.attrs = &sobj->a;
1028 sobj->s.group.name = name;
1029
1030 return &sobj->s;
1031}
1032
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001033#define destroy_attr_set(_set) \
1034 kfree(_set);
1035
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001036/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001037static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001038{
1039 if (!s || !attr)
1040 return -EINVAL;
1041
1042 if (s->members >= s->max_members)
1043 return -ENOMEM;
1044
1045 s->group.attrs[s->members] = attr;
1046 s->members++;
1047
1048 return 0;
1049}
1050
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001051static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001052 struct attribute **attr,
1053 unsigned int count)
1054{
1055 int i, res;
1056
1057 for (i = 0; i < count; i++) {
1058 res = add_to_attr_set(s, attr[i]);
1059 if (res)
1060 return res;
1061 }
1062
1063 return 0;
1064}
1065
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001066static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001067{
1068 sysfs_remove_group(kobj, &s->group);
1069 destroy_attr_set(s);
1070}
1071
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001072#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1073 sysfs_create_group(_kobj, &_attr_set->group)
1074
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001075static int parse_strtoul(const char *buf,
1076 unsigned long max, unsigned long *value)
1077{
1078 char *endp;
1079
André Goddard Rosae7d28602009-12-14 18:01:06 -08001080 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1081 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001082 if (*endp || *value > max)
1083 return -EINVAL;
1084
1085 return 0;
1086}
1087
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001088static void tpacpi_disable_brightness_delay(void)
1089{
1090 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001091 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001092}
1093
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001094static void printk_deprecated_attribute(const char * const what,
1095 const char * const details)
1096{
1097 tpacpi_log_usertask("deprecated sysfs attribute");
Joe Perches0978e012011-04-04 10:06:25 -07001098 pr_warn("WARNING: sysfs attribute %s is deprecated and "
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001099 "will be removed. %s\n",
1100 what, details);
1101}
1102
Johannes Berg19d337d2009-06-02 13:01:37 +02001103/*************************************************************************
1104 * rfkill and radio control support helpers
1105 */
1106
1107/*
1108 * ThinkPad-ACPI firmware handling model:
1109 *
1110 * WLSW (master wireless switch) is event-driven, and is common to all
1111 * firmware-controlled radios. It cannot be controlled, just monitored,
1112 * as expected. It overrides all radio state in firmware
1113 *
1114 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1115 * (TODO: verify how WLSW interacts with the returned radio state).
1116 *
1117 * The only time there are shadow radio state changes, is when
1118 * masked-off hotkeys are used.
1119 */
1120
1121/*
1122 * Internal driver API for radio state:
1123 *
1124 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1125 * bool: true means radio blocked (off)
1126 */
1127enum tpacpi_rfkill_state {
1128 TPACPI_RFK_RADIO_OFF = 0,
1129 TPACPI_RFK_RADIO_ON
1130};
1131
1132/* rfkill switches */
1133enum tpacpi_rfk_id {
1134 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1135 TPACPI_RFK_WWAN_SW_ID,
1136 TPACPI_RFK_UWB_SW_ID,
1137 TPACPI_RFK_SW_MAX
1138};
1139
1140static const char *tpacpi_rfkill_names[] = {
1141 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1142 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1143 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1144 [TPACPI_RFK_SW_MAX] = NULL
1145};
1146
1147/* ThinkPad-ACPI rfkill subdriver */
1148struct tpacpi_rfk {
1149 struct rfkill *rfkill;
1150 enum tpacpi_rfk_id id;
1151 const struct tpacpi_rfk_ops *ops;
1152};
1153
1154struct tpacpi_rfk_ops {
1155 /* firmware interface */
1156 int (*get_status)(void);
1157 int (*set_status)(const enum tpacpi_rfkill_state);
1158};
1159
1160static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1161
1162/* Query FW and update rfkill sw state for a given rfkill switch */
1163static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1164{
1165 int status;
1166
1167 if (!tp_rfk)
1168 return -ENODEV;
1169
1170 status = (tp_rfk->ops->get_status)();
1171 if (status < 0)
1172 return status;
1173
1174 rfkill_set_sw_state(tp_rfk->rfkill,
1175 (status == TPACPI_RFK_RADIO_OFF));
1176
1177 return status;
1178}
1179
1180/* Query FW and update rfkill sw state for all rfkill switches */
1181static void tpacpi_rfk_update_swstate_all(void)
1182{
1183 unsigned int i;
1184
1185 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1186 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1187}
1188
1189/*
1190 * Sync the HW-blocking state of all rfkill switches,
1191 * do notice it causes the rfkill core to schedule uevents
1192 */
1193static void tpacpi_rfk_update_hwblock_state(bool blocked)
1194{
1195 unsigned int i;
1196 struct tpacpi_rfk *tp_rfk;
1197
1198 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1199 tp_rfk = tpacpi_rfkill_switches[i];
1200 if (tp_rfk) {
1201 if (rfkill_set_hw_state(tp_rfk->rfkill,
1202 blocked)) {
1203 /* ignore -- we track sw block */
1204 }
1205 }
1206 }
1207}
1208
1209/* Call to get the WLSW state from the firmware */
1210static int hotkey_get_wlsw(void);
1211
1212/* Call to query WLSW state and update all rfkill switches */
1213static bool tpacpi_rfk_check_hwblock_state(void)
1214{
1215 int res = hotkey_get_wlsw();
1216 int hw_blocked;
1217
1218 /* When unknown or unsupported, we have to assume it is unblocked */
1219 if (res < 0)
1220 return false;
1221
1222 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1223 tpacpi_rfk_update_hwblock_state(hw_blocked);
1224
1225 return hw_blocked;
1226}
1227
1228static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1229{
1230 struct tpacpi_rfk *tp_rfk = data;
1231 int res;
1232
1233 dbg_printk(TPACPI_DBG_RFKILL,
1234 "request to change radio state to %s\n",
1235 blocked ? "blocked" : "unblocked");
1236
1237 /* try to set radio state */
1238 res = (tp_rfk->ops->set_status)(blocked ?
1239 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1240
1241 /* and update the rfkill core with whatever the FW really did */
1242 tpacpi_rfk_update_swstate(tp_rfk);
1243
1244 return (res < 0) ? res : 0;
1245}
1246
1247static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1248 .set_block = tpacpi_rfk_hook_set_block,
1249};
1250
1251static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1252 const struct tpacpi_rfk_ops *tp_rfkops,
1253 const enum rfkill_type rfktype,
1254 const char *name,
1255 const bool set_default)
1256{
1257 struct tpacpi_rfk *atp_rfk;
1258 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001259 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001260 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001261 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001262
1263 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1264
Johannes Berg19d337d2009-06-02 13:01:37 +02001265 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1266 if (atp_rfk)
1267 atp_rfk->rfkill = rfkill_alloc(name,
1268 &tpacpi_pdev->dev,
1269 rfktype,
1270 &tpacpi_rfk_rfkill_ops,
1271 atp_rfk);
1272 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001273 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001274 kfree(atp_rfk);
1275 return -ENOMEM;
1276 }
1277
1278 atp_rfk->id = id;
1279 atp_rfk->ops = tp_rfkops;
1280
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001281 sw_status = (tp_rfkops->get_status)();
1282 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001283 pr_err("failed to read initial state for %s, error %d\n",
1284 name, sw_status);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001285 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001286 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001287 if (set_default) {
1288 /* try to keep the initial state, since we ask the
1289 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001290 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001291 }
1292 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001293 hw_state = tpacpi_rfk_check_hwblock_state();
1294 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001295
1296 res = rfkill_register(atp_rfk->rfkill);
1297 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001298 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001299 rfkill_destroy(atp_rfk->rfkill);
1300 kfree(atp_rfk);
1301 return res;
1302 }
1303
1304 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001305
Joe Perches0978e012011-04-04 10:06:25 -07001306 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001307 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001308 return 0;
1309}
1310
1311static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1312{
1313 struct tpacpi_rfk *tp_rfk;
1314
1315 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1316
1317 tp_rfk = tpacpi_rfkill_switches[id];
1318 if (tp_rfk) {
1319 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001320 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001321 tpacpi_rfkill_switches[id] = NULL;
1322 kfree(tp_rfk);
1323 }
1324}
1325
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001326static void printk_deprecated_rfkill_attribute(const char * const what)
1327{
1328 printk_deprecated_attribute(what,
1329 "Please switch to generic rfkill before year 2010");
1330}
1331
Johannes Berg19d337d2009-06-02 13:01:37 +02001332/* sysfs <radio> enable ------------------------------------------------ */
1333static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1334 struct device_attribute *attr,
1335 char *buf)
1336{
1337 int status;
1338
1339 printk_deprecated_rfkill_attribute(attr->attr.name);
1340
1341 /* This is in the ABI... */
1342 if (tpacpi_rfk_check_hwblock_state()) {
1343 status = TPACPI_RFK_RADIO_OFF;
1344 } else {
1345 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1346 if (status < 0)
1347 return status;
1348 }
1349
1350 return snprintf(buf, PAGE_SIZE, "%d\n",
1351 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1352}
1353
1354static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1355 struct device_attribute *attr,
1356 const char *buf, size_t count)
1357{
1358 unsigned long t;
1359 int res;
1360
1361 printk_deprecated_rfkill_attribute(attr->attr.name);
1362
1363 if (parse_strtoul(buf, 1, &t))
1364 return -EINVAL;
1365
1366 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1367
1368 /* This is in the ABI... */
1369 if (tpacpi_rfk_check_hwblock_state() && !!t)
1370 return -EPERM;
1371
1372 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1373 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1374 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1375
1376 return (res < 0) ? res : count;
1377}
1378
1379/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001380static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001381{
Johannes Berg19d337d2009-06-02 13:01:37 +02001382 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001383 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001384 else {
1385 int status;
1386
1387 /* This is in the ABI... */
1388 if (tpacpi_rfk_check_hwblock_state()) {
1389 status = TPACPI_RFK_RADIO_OFF;
1390 } else {
1391 status = tpacpi_rfk_update_swstate(
1392 tpacpi_rfkill_switches[id]);
1393 if (status < 0)
1394 return status;
1395 }
1396
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001397 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001398 (status == TPACPI_RFK_RADIO_ON) ?
1399 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001400 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001401 }
1402
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001403 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001404}
1405
1406static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1407{
1408 char *cmd;
1409 int status = -1;
1410 int res = 0;
1411
1412 if (id >= TPACPI_RFK_SW_MAX)
1413 return -ENODEV;
1414
1415 while ((cmd = next_cmd(&buf))) {
1416 if (strlencmp(cmd, "enable") == 0)
1417 status = TPACPI_RFK_RADIO_ON;
1418 else if (strlencmp(cmd, "disable") == 0)
1419 status = TPACPI_RFK_RADIO_OFF;
1420 else
1421 return -EINVAL;
1422 }
1423
1424 if (status != -1) {
1425 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1426 (status == TPACPI_RFK_RADIO_ON) ?
1427 "enable" : "disable",
1428 tpacpi_rfkill_names[id]);
1429 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1430 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1431 }
1432
1433 return res;
1434}
1435
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001436/*************************************************************************
1437 * thinkpad-acpi driver attributes
1438 */
1439
1440/* interface_version --------------------------------------------------- */
1441static ssize_t tpacpi_driver_interface_version_show(
1442 struct device_driver *drv,
1443 char *buf)
1444{
1445 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1446}
1447
1448static DRIVER_ATTR(interface_version, S_IRUGO,
1449 tpacpi_driver_interface_version_show, NULL);
1450
1451/* debug_level --------------------------------------------------------- */
1452static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1453 char *buf)
1454{
1455 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1456}
1457
1458static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1459 const char *buf, size_t count)
1460{
1461 unsigned long t;
1462
1463 if (parse_strtoul(buf, 0xffff, &t))
1464 return -EINVAL;
1465
1466 dbg_level = t;
1467
1468 return count;
1469}
1470
1471static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1472 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1473
1474/* version ------------------------------------------------------------- */
1475static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1476 char *buf)
1477{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001478 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1479 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001480}
1481
1482static DRIVER_ATTR(version, S_IRUGO,
1483 tpacpi_driver_version_show, NULL);
1484
1485/* --------------------------------------------------------------------- */
1486
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001487#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1488
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001489/* wlsw_emulstate ------------------------------------------------------ */
1490static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1491 char *buf)
1492{
1493 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1494}
1495
1496static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1497 const char *buf, size_t count)
1498{
1499 unsigned long t;
1500
1501 if (parse_strtoul(buf, 1, &t))
1502 return -EINVAL;
1503
Johannes Berg19d337d2009-06-02 13:01:37 +02001504 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001505 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001506 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1507 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001508
1509 return count;
1510}
1511
1512static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1513 tpacpi_driver_wlsw_emulstate_show,
1514 tpacpi_driver_wlsw_emulstate_store);
1515
1516/* bluetooth_emulstate ------------------------------------------------- */
1517static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1518 struct device_driver *drv,
1519 char *buf)
1520{
1521 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1522}
1523
1524static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1525 struct device_driver *drv,
1526 const char *buf, size_t count)
1527{
1528 unsigned long t;
1529
1530 if (parse_strtoul(buf, 1, &t))
1531 return -EINVAL;
1532
1533 tpacpi_bluetooth_emulstate = !!t;
1534
1535 return count;
1536}
1537
1538static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1539 tpacpi_driver_bluetooth_emulstate_show,
1540 tpacpi_driver_bluetooth_emulstate_store);
1541
1542/* wwan_emulstate ------------------------------------------------- */
1543static ssize_t tpacpi_driver_wwan_emulstate_show(
1544 struct device_driver *drv,
1545 char *buf)
1546{
1547 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1548}
1549
1550static ssize_t tpacpi_driver_wwan_emulstate_store(
1551 struct device_driver *drv,
1552 const char *buf, size_t count)
1553{
1554 unsigned long t;
1555
1556 if (parse_strtoul(buf, 1, &t))
1557 return -EINVAL;
1558
1559 tpacpi_wwan_emulstate = !!t;
1560
1561 return count;
1562}
1563
1564static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1565 tpacpi_driver_wwan_emulstate_show,
1566 tpacpi_driver_wwan_emulstate_store);
1567
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001568/* uwb_emulstate ------------------------------------------------- */
1569static ssize_t tpacpi_driver_uwb_emulstate_show(
1570 struct device_driver *drv,
1571 char *buf)
1572{
1573 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1574}
1575
1576static ssize_t tpacpi_driver_uwb_emulstate_store(
1577 struct device_driver *drv,
1578 const char *buf, size_t count)
1579{
1580 unsigned long t;
1581
1582 if (parse_strtoul(buf, 1, &t))
1583 return -EINVAL;
1584
1585 tpacpi_uwb_emulstate = !!t;
1586
1587 return count;
1588}
1589
1590static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1591 tpacpi_driver_uwb_emulstate_show,
1592 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001593#endif
1594
1595/* --------------------------------------------------------------------- */
1596
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001597static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001598 &driver_attr_debug_level, &driver_attr_version,
1599 &driver_attr_interface_version,
1600};
1601
1602static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1603{
1604 int i, res;
1605
1606 i = 0;
1607 res = 0;
1608 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1609 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1610 i++;
1611 }
1612
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001613#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1614 if (!res && dbg_wlswemul)
1615 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1616 if (!res && dbg_bluetoothemul)
1617 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1618 if (!res && dbg_wwanemul)
1619 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001620 if (!res && dbg_uwbemul)
1621 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001622#endif
1623
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001624 return res;
1625}
1626
1627static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1628{
1629 int i;
1630
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001631 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001632 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001633
1634#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1635 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1636 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1637 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001638 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001639#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001640}
1641
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001642/*************************************************************************
1643 * Firmware Data
1644 */
1645
1646/*
1647 * Table of recommended minimum BIOS versions
1648 *
1649 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001650 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001651 * bugs and bad ACPI behaviour on older versions
1652 *
1653 * 2. BIOS or EC fw with known bugs that trigger on Linux
1654 *
1655 * 3. BIOS with known reduced functionality in older versions
1656 *
1657 * We recommend the latest BIOS and EC version.
1658 * We only support the latest BIOS and EC fw version as a rule.
1659 *
1660 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1661 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001662 *
1663 * WARNING: we use this table also to detect that the machine is
1664 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001665 */
1666
1667#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1668 { .vendor = (__v), \
1669 .bios = TPID(__id1, __id2), \
1670 .ec = TPACPI_MATCH_ANY, \
1671 .quirks = TPACPI_MATCH_ANY << 16 \
1672 | (__bv1) << 8 | (__bv2) }
1673
1674#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001675 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001676 { .vendor = (__v), \
1677 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001678 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001679 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1680 | (__bv1) << 8 | (__bv2) }
1681
1682#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1683 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1684
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001685/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001686#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001688 __bv1, __bv2, TPID(__id1, __id2), \
1689 __ev1, __ev2), \
1690 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1691 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1692 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001693
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001694/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001695#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1696 __eid1, __eid2, __ev1, __ev2) \
1697 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001698 __bv1, __bv2, TPID(__eid1, __eid2), \
1699 __ev1, __ev2), \
1700 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1701 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1702 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001703
1704#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1705 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1706
1707#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1708 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001709 __bv1, __bv2, TPID(__id1, __id2), \
1710 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001711
1712#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1713 __eid1, __eid2, __ev1, __ev2) \
1714 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001715 __bv1, __bv2, TPID(__eid1, __eid2), \
1716 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001717
1718static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1719 /* Numeric models ------------------ */
1720 /* FW MODEL BIOS VERS */
1721 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1722 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1723 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1724 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1725 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1726 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1727 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1728 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1729 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1730
1731 /* A-series ------------------------- */
1732 /* FW MODEL BIOS VERS EC VERS */
1733 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1734 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1735 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1736 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1737 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1738 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1739 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1740 TPV_QI0('1', '3', '2', '0'), /* A22m */
1741 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1742 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1743 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1744
1745 /* G-series ------------------------- */
1746 /* FW MODEL BIOS VERS */
1747 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1748 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1749
1750 /* R-series, T-series --------------- */
1751 /* FW MODEL BIOS VERS EC VERS */
1752 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1753 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1754 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1755 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1756 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1757 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1758 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1759 T40/p, T41/p, T42/p (1) */
1760 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1761 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1762 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1763 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1764
1765 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1766 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1767 TPV_QI0('1', '6', '3', '2'), /* T22 */
1768 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1769 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1770 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1771
1772 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1773 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001774 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001775
1776 /* BIOS FW BIOS VERS EC FW EC VERS */
1777 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1778 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1779
1780 /* X-series ------------------------- */
1781 /* FW MODEL BIOS VERS EC VERS */
1782 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1783 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1784 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1785 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1786 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1787 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1788 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1789
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001790 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1791 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001792
1793 /* (0) - older versions lack DMI EC fw string and functionality */
1794 /* (1) - older versions known to lack functionality */
1795};
1796
1797#undef TPV_QL1
1798#undef TPV_QL0
1799#undef TPV_QI2
1800#undef TPV_QI1
1801#undef TPV_QI0
1802#undef TPV_Q_X
1803#undef TPV_Q
1804
1805static void __init tpacpi_check_outdated_fw(void)
1806{
1807 unsigned long fwvers;
1808 u16 ec_version, bios_version;
1809
1810 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1811 ARRAY_SIZE(tpacpi_bios_version_qtable));
1812
1813 if (!fwvers)
1814 return;
1815
1816 bios_version = fwvers & 0xffffU;
1817 ec_version = (fwvers >> 16) & 0xffffU;
1818
1819 /* note that unknown versions are set to 0x0000 and we use that */
1820 if ((bios_version > thinkpad_id.bios_release) ||
1821 (ec_version > thinkpad_id.ec_release &&
1822 ec_version != TPACPI_MATCH_ANY)) {
1823 /*
1824 * The changelogs would let us track down the exact
1825 * reason, but it is just too much of a pain to track
1826 * it. We only list BIOSes that are either really
1827 * broken, or really stable to begin with, so it is
1828 * best if the user upgrades the firmware anyway.
1829 */
Joe Perches0978e012011-04-04 10:06:25 -07001830 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1831 pr_warn("WARNING: This firmware may be missing critical bug "
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001832 "fixes and/or important features\n");
1833 }
1834}
1835
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001836static bool __init tpacpi_is_fw_known(void)
1837{
1838 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1839 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1840}
1841
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001842/****************************************************************************
1843 ****************************************************************************
1844 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001845 * Subdrivers
1846 *
1847 ****************************************************************************
1848 ****************************************************************************/
1849
1850/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001851 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001852 */
1853
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001854static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001856 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1857 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001861static struct ibm_struct thinkpad_acpi_driver_data = {
1862 .name = "driver",
1863 .read = thinkpad_acpi_driver_read,
1864};
1865
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001866/*************************************************************************
1867 * Hotkey subdriver
1868 */
1869
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001870/*
1871 * ThinkPad firmware event model
1872 *
1873 * The ThinkPad firmware has two main event interfaces: normal ACPI
1874 * notifications (which follow the ACPI standard), and a private event
1875 * interface.
1876 *
1877 * The private event interface also issues events for the hotkeys. As
1878 * the driver gained features, the event handling code ended up being
1879 * built around the hotkey subdriver. This will need to be refactored
1880 * to a more formal event API eventually.
1881 *
1882 * Some "hotkeys" are actually supposed to be used as event reports,
1883 * such as "brightness has changed", "volume has changed", depending on
1884 * the ThinkPad model and how the firmware is operating.
1885 *
1886 * Unlike other classes, hotkey-class events have mask/unmask control on
1887 * non-ancient firmware. However, how it behaves changes a lot with the
1888 * firmware model and version.
1889 */
1890
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001891enum { /* hot key scan codes (derived from ACPI DSDT) */
1892 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1893 TP_ACPI_HOTKEYSCAN_FNF2,
1894 TP_ACPI_HOTKEYSCAN_FNF3,
1895 TP_ACPI_HOTKEYSCAN_FNF4,
1896 TP_ACPI_HOTKEYSCAN_FNF5,
1897 TP_ACPI_HOTKEYSCAN_FNF6,
1898 TP_ACPI_HOTKEYSCAN_FNF7,
1899 TP_ACPI_HOTKEYSCAN_FNF8,
1900 TP_ACPI_HOTKEYSCAN_FNF9,
1901 TP_ACPI_HOTKEYSCAN_FNF10,
1902 TP_ACPI_HOTKEYSCAN_FNF11,
1903 TP_ACPI_HOTKEYSCAN_FNF12,
1904 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1905 TP_ACPI_HOTKEYSCAN_FNINSERT,
1906 TP_ACPI_HOTKEYSCAN_FNDELETE,
1907 TP_ACPI_HOTKEYSCAN_FNHOME,
1908 TP_ACPI_HOTKEYSCAN_FNEND,
1909 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1910 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1911 TP_ACPI_HOTKEYSCAN_FNSPACE,
1912 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1913 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1914 TP_ACPI_HOTKEYSCAN_MUTE,
1915 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001916 TP_ACPI_HOTKEYSCAN_UNK1,
1917 TP_ACPI_HOTKEYSCAN_UNK2,
1918 TP_ACPI_HOTKEYSCAN_UNK3,
1919 TP_ACPI_HOTKEYSCAN_UNK4,
1920 TP_ACPI_HOTKEYSCAN_UNK5,
1921 TP_ACPI_HOTKEYSCAN_UNK6,
1922 TP_ACPI_HOTKEYSCAN_UNK7,
1923 TP_ACPI_HOTKEYSCAN_UNK8,
1924
Christian Kellner149c8c72017-02-28 17:10:56 +01001925 /* Adaptive keyboard keycodes */
1926 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1927 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
Bastien Nocera6a68d852015-03-02 14:45:31 +01001928 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1929 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1930 TP_ACPI_HOTKEYSCAN_CLOUD,
1931 TP_ACPI_HOTKEYSCAN_UNK9,
1932 TP_ACPI_HOTKEYSCAN_VOICE,
1933 TP_ACPI_HOTKEYSCAN_UNK10,
1934 TP_ACPI_HOTKEYSCAN_GESTURES,
1935 TP_ACPI_HOTKEYSCAN_UNK11,
1936 TP_ACPI_HOTKEYSCAN_UNK12,
1937 TP_ACPI_HOTKEYSCAN_UNK13,
1938 TP_ACPI_HOTKEYSCAN_CONFIG,
1939 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1940 TP_ACPI_HOTKEYSCAN_RELOAD,
1941 TP_ACPI_HOTKEYSCAN_BACK,
1942 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1943 TP_ACPI_HOTKEYSCAN_MIC_UP,
1944 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1945 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1946 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1947
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001948 /* Hotkey keymap size */
1949 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001950};
1951
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001952enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001953 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1954 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1955};
1956
1957enum { /* Positions of some of the keys in hotkey masks */
1958 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1959 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1960 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1961 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1962 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
Hans de Goedec685e202017-02-09 16:44:13 +01001963 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001964 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1965 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1966 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1967 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1968 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1969};
1970
1971enum { /* NVRAM to ACPI HKEY group map */
1972 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1973 TP_ACPI_HKEY_ZOOM_MASK |
1974 TP_ACPI_HKEY_DISPSWTCH_MASK |
1975 TP_ACPI_HKEY_HIBERNATE_MASK,
1976 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1977 TP_ACPI_HKEY_BRGHTDWN_MASK,
1978 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1979 TP_ACPI_HKEY_VOLDWN_MASK |
1980 TP_ACPI_HKEY_MUTE_MASK,
1981};
1982
1983#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1984struct tp_nvram_state {
1985 u16 thinkpad_toggle:1;
1986 u16 zoom_toggle:1;
1987 u16 display_toggle:1;
1988 u16 thinklight_toggle:1;
1989 u16 hibernate_toggle:1;
1990 u16 displayexp_toggle:1;
1991 u16 display_state:1;
1992 u16 brightness_toggle:1;
1993 u16 volume_toggle:1;
1994 u16 mute:1;
1995
1996 u8 brightness_level;
1997 u8 volume_level;
1998};
1999
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002000/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002001static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002002
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002003/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002004 * Acquire mutex to write poller control variables as an
2005 * atomic block.
2006 *
2007 * Increment hotkey_config_change when changing them if you
2008 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002009 *
2010 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2011 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002012static struct mutex hotkey_thread_data_mutex;
2013static unsigned int hotkey_config_change;
2014
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002015/*
2016 * hotkey poller control variables
2017 *
2018 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002019 *
2020 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2021 * should be used only when the changes need to be taken as
2022 * a block, OR when one needs to force the kthread to forget
2023 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002024 */
2025static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2026static unsigned int hotkey_poll_freq = 10; /* Hz */
2027
2028#define HOTKEY_CONFIG_CRITICAL_START \
2029 do { \
2030 mutex_lock(&hotkey_thread_data_mutex); \
2031 hotkey_config_change++; \
2032 } while (0);
2033#define HOTKEY_CONFIG_CRITICAL_END \
2034 mutex_unlock(&hotkey_thread_data_mutex);
2035
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002036#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2037
2038#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002039#define HOTKEY_CONFIG_CRITICAL_START
2040#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002041
2042#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2043
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002044static struct mutex hotkey_mutex;
2045
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002046static enum { /* Reasons for waking up */
2047 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2048 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2049 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2050} hotkey_wakeup_reason;
2051
2052static int hotkey_autosleep_ack;
2053
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002054static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2055static u32 hotkey_all_mask; /* all events supported in fw */
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002056static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002057static u32 hotkey_reserved_mask; /* events better left disabled */
2058static u32 hotkey_driver_mask; /* events needed by the driver */
2059static u32 hotkey_user_mask; /* events visible to userspace */
2060static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002061
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002062static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002063
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002064static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002065
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002066static void tpacpi_driver_event(const unsigned int hkey_event);
2067static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002068static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002069
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002070/* HKEY.MHKG() return bits */
2071#define TP_HOTKEY_TABLET_MASK (1 << 3)
Lyudeb03f4d42016-11-11 15:15:03 -05002072/* ThinkPad X1 Yoga (2016) */
2073#define TP_EC_CMMD_TABLET_MODE 0x6
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002074
Johannes Berg19d337d2009-06-02 13:01:37 +02002075static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002076{
Johannes Berg19d337d2009-06-02 13:01:37 +02002077 int status;
2078
2079 if (!tp_features.hotkey_wlsw)
2080 return -ENODEV;
2081
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002082#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002083 if (dbg_wlswemul)
2084 return (tpacpi_wlsw_emulstate) ?
2085 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002086#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002087
2088 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002089 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002090
2091 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002092}
2093
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002094static int hotkey_get_tablet_mode(int *status)
2095{
2096 int s;
2097
Lyudeb03f4d42016-11-11 15:15:03 -05002098 switch (tp_features.hotkey_tablet) {
2099 case TP_HOTKEY_TABLET_USES_MHKG:
2100 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2101 return -EIO;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002102
Lyudeb03f4d42016-11-11 15:15:03 -05002103 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2104 break;
2105 case TP_HOTKEY_TABLET_USES_CMMD:
2106 if (!acpi_evalf(ec_handle, &s, "CMMD", "d"))
2107 return -EIO;
2108
2109 *status = (s == TP_EC_CMMD_TABLET_MODE);
2110 break;
2111 default:
2112 break;
2113 }
2114
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002115 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002116}
2117
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002118/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002119 * Reads current event mask from firmware, and updates
2120 * hotkey_acpi_mask accordingly. Also resets any bits
2121 * from hotkey_user_mask that are unavailable to be
2122 * delivered (shadow requirement of the userspace ABI).
2123 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002124 * Call with hotkey_mutex held
2125 */
2126static int hotkey_mask_get(void)
2127{
2128 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002129 u32 m = 0;
2130
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002131 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002132 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002133
2134 hotkey_acpi_mask = m;
2135 } else {
2136 /* no mask support doesn't mean no event support... */
2137 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002138 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002139
2140 /* sync userspace-visible mask */
2141 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002142
2143 return 0;
2144}
2145
Jean Delvaredf6dd1b2015-04-27 09:45:06 +02002146static void hotkey_mask_warn_incomplete_mask(void)
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002147{
2148 /* log only what the user can fix... */
2149 const u32 wantedmask = hotkey_driver_mask &
2150 ~(hotkey_acpi_mask | hotkey_source_mask) &
2151 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2152
2153 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002154 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002155}
2156
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002157/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002158 * Set the firmware mask when supported
2159 *
2160 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2161 *
2162 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2163 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002164 * Call with hotkey_mutex held
2165 */
2166static int hotkey_mask_set(u32 mask)
2167{
2168 int i;
2169 int rc = 0;
2170
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002171 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002172
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002173 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002174 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002175 if (!acpi_evalf(hkey_handle,
2176 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002177 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002178 rc = -EIO;
2179 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002180 }
2181 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002182 }
2183
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002184 /*
2185 * We *must* make an inconditional call to hotkey_mask_get to
2186 * refresh hotkey_acpi_mask and update hotkey_user_mask
2187 *
2188 * Take the opportunity to also log when we cannot _enable_
2189 * a given event.
2190 */
2191 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Joe Perches0978e012011-04-04 10:06:25 -07002192 pr_notice("asked for hotkey mask 0x%08x, but "
2193 "firmware forced it to 0x%08x\n",
2194 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002195 }
2196
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002197 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2198 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002199
2200 return rc;
2201}
2202
2203/*
2204 * Sets hotkey_user_mask and tries to set the firmware mask
2205 *
2206 * Call with hotkey_mutex held
2207 */
2208static int hotkey_user_mask_set(const u32 mask)
2209{
2210 int rc;
2211
2212 /* Give people a chance to notice they are doing something that
2213 * is bound to go boom on their users sooner or later */
2214 if (!tp_warned.hotkey_mask_ff &&
2215 (mask == 0xffff || mask == 0xffffff ||
2216 mask == 0xffffffff)) {
2217 tp_warned.hotkey_mask_ff = 1;
Joe Perches0978e012011-04-04 10:06:25 -07002218 pr_notice("setting the hotkey mask to 0x%08x is likely "
2219 "not the best way to go about it\n", mask);
2220 pr_notice("please consider using the driver defaults, "
2221 "and refer to up-to-date thinkpad-acpi "
2222 "documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002223 }
2224
2225 /* Try to enable what the user asked for, plus whatever we need.
2226 * this syncs everything but won't enable bits in hotkey_user_mask */
2227 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2228
2229 /* Enable the available bits in hotkey_user_mask */
2230 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2231
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002232 return rc;
2233}
2234
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002235/*
2236 * Sets the driver hotkey mask.
2237 *
2238 * Can be called even if the hotkey subdriver is inactive
2239 */
2240static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2241{
2242 int rc;
2243
2244 /* Do the right thing if hotkey_init has not been called yet */
2245 if (!tp_features.hotkey) {
2246 hotkey_driver_mask = mask;
2247 return 0;
2248 }
2249
2250 mutex_lock(&hotkey_mutex);
2251
2252 HOTKEY_CONFIG_CRITICAL_START
2253 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002254#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002255 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002256#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002257 HOTKEY_CONFIG_CRITICAL_END
2258
2259 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2260 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002261 hotkey_poll_setup(true);
2262
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002263 mutex_unlock(&hotkey_mutex);
2264
2265 return rc;
2266}
2267
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002268static int hotkey_status_get(int *status)
2269{
2270 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2271 return -EIO;
2272
2273 return 0;
2274}
2275
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002276static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002277{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002278 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002279 return -EIO;
2280
2281 return 0;
2282}
2283
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002284static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002285{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002286 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002287
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002288 if (tp_features.hotkey_tablet &&
2289 !hotkey_get_tablet_mode(&state)) {
2290 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002291
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002292 input_report_switch(tpacpi_inputdev,
2293 SW_TABLET_MODE, !!state);
2294 input_sync(tpacpi_inputdev);
2295
2296 mutex_unlock(&tpacpi_inputdev_send_mutex);
2297 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002298}
2299
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002300/* Do NOT call without validating scancode first */
2301static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002302{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002303 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002304
2305 if (keycode != KEY_RESERVED) {
2306 mutex_lock(&tpacpi_inputdev_send_mutex);
2307
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002308 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002309 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002310 input_sync(tpacpi_inputdev);
2311
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002312 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002313 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002314 input_sync(tpacpi_inputdev);
2315
2316 mutex_unlock(&tpacpi_inputdev_send_mutex);
2317 }
2318}
2319
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002320/* Do NOT call without validating scancode first */
2321static void tpacpi_input_send_key_masked(const unsigned int scancode)
2322{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002323 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002324 if (hotkey_user_mask & (1 << scancode))
2325 tpacpi_input_send_key(scancode);
2326}
2327
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002328#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2329static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2330
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002331/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002332static void tpacpi_hotkey_send_key(unsigned int scancode)
2333{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002334 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002335}
2336
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002337static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002338{
2339 u8 d;
2340
2341 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2342 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2343 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2344 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2345 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2346 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2347 }
Hans de Goedec685e202017-02-09 16:44:13 +01002348 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002349 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2350 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2351 }
2352 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2353 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2354 n->displayexp_toggle =
2355 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2356 }
2357 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2358 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2359 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2360 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2361 n->brightness_toggle =
2362 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2363 }
2364 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2365 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2366 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2367 >> TP_NVRAM_POS_LEVEL_VOLUME;
2368 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2369 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2370 }
2371}
2372
Behan Webstera4d44ba2014-02-12 21:58:46 +01002373#define TPACPI_COMPARE_KEY(__scancode, __member) \
2374do { \
2375 if ((event_mask & (1 << __scancode)) && \
2376 oldn->__member != newn->__member) \
2377 tpacpi_hotkey_send_key(__scancode); \
2378} while (0)
2379
2380#define TPACPI_MAY_SEND_KEY(__scancode) \
2381do { \
2382 if (event_mask & (1 << __scancode)) \
2383 tpacpi_hotkey_send_key(__scancode); \
2384} while (0)
2385
2386static void issue_volchange(const unsigned int oldvol,
2387 const unsigned int newvol,
2388 const u32 event_mask)
2389{
2390 unsigned int i = oldvol;
2391
2392 while (i > newvol) {
2393 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2394 i--;
2395 }
2396 while (i < newvol) {
2397 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2398 i++;
2399 }
2400}
2401
2402static void issue_brightnesschange(const unsigned int oldbrt,
2403 const unsigned int newbrt,
2404 const u32 event_mask)
2405{
2406 unsigned int i = oldbrt;
2407
2408 while (i > newbrt) {
2409 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2410 i--;
2411 }
2412 while (i < newbrt) {
2413 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2414 i++;
2415 }
2416}
2417
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002418static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2419 struct tp_nvram_state *newn,
2420 const u32 event_mask)
2421{
2422
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002423 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2424 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2425 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2426 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2427
2428 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2429
2430 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2431
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002432 /*
2433 * Handle volume
2434 *
2435 * This code is supposed to duplicate the IBM firmware behaviour:
2436 * - Pressing MUTE issues mute hotkey message, even when already mute
2437 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002438 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002439 * - The act of unmuting issues volume up/down notification,
2440 * depending which key was used to unmute
2441 *
2442 * We are constrained to what the NVRAM can tell us, which is not much
2443 * and certainly not enough if more than one volume hotkey was pressed
2444 * since the last poll cycle.
2445 *
2446 * Just to make our life interesting, some newer Lenovo ThinkPads have
2447 * bugs in the BIOS and may fail to update volume_toggle properly.
2448 */
2449 if (newn->mute) {
2450 /* muted */
2451 if (!oldn->mute ||
2452 oldn->volume_toggle != newn->volume_toggle ||
2453 oldn->volume_level != newn->volume_level) {
2454 /* recently muted, or repeated mute keypress, or
2455 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002456 issue_volchange(oldn->volume_level, newn->volume_level,
2457 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002458 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2459 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002460 } else {
2461 /* unmute */
2462 if (oldn->mute) {
2463 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002464 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002465 }
2466 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002467 issue_volchange(oldn->volume_level, newn->volume_level,
2468 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002469 } else if (oldn->volume_toggle != newn->volume_toggle) {
2470 /* repeated vol up/down keypress at end of scale ? */
2471 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002472 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002473 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2474 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002475 }
2476 }
2477
2478 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002479 if (oldn->brightness_level != newn->brightness_level) {
2480 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002481 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002482 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2483 /* repeated key presses that didn't change state */
2484 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002485 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002486 else if (newn->brightness_level >= bright_maxlvl
2487 && !tp_features.bright_unkfw)
2488 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002489 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002490
2491#undef TPACPI_COMPARE_KEY
2492#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002493}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002494
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002495/*
2496 * Polling driver
2497 *
2498 * We track all events in hotkey_source_mask all the time, since
2499 * most of them are edge-based. We only issue those requested by
2500 * hotkey_user_mask or hotkey_driver_mask, though.
2501 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002502static int hotkey_kthread(void *data)
2503{
2504 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002505 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002506 unsigned int si, so;
2507 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002508 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002509 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002510 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002511
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002512 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2513 goto exit;
2514
2515 set_freezable();
2516
2517 so = 0;
2518 si = 1;
2519 t = 0;
2520
2521 /* Initial state for compares */
2522 mutex_lock(&hotkey_thread_data_mutex);
2523 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002524 poll_mask = hotkey_source_mask;
2525 event_mask = hotkey_source_mask &
2526 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002527 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002528 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002529 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002530
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002531 while (!kthread_should_stop()) {
2532 if (t == 0) {
2533 if (likely(poll_freq))
2534 t = 1000/poll_freq;
2535 else
2536 t = 100; /* should never happen... */
2537 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002538 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002539 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002540 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002541
2542 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002543 continue;
2544
2545 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002546 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002547 /* forget old state on thaw or config change */
2548 si = so;
2549 t = 0;
2550 change_detector = hotkey_config_change;
2551 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002552 poll_mask = hotkey_source_mask;
2553 event_mask = hotkey_source_mask &
2554 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002555 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002556 mutex_unlock(&hotkey_thread_data_mutex);
2557
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002558 if (likely(poll_mask)) {
2559 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002560 if (likely(si != so)) {
2561 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002562 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002563 }
2564 }
2565
2566 so = si;
2567 si ^= 1;
2568 }
2569
2570exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002571 return 0;
2572}
2573
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002574/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002575static void hotkey_poll_stop_sync(void)
2576{
2577 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002578 kthread_stop(tpacpi_hotkey_task);
2579 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002580 }
2581}
2582
2583/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002584static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002585{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002586 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2587 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002588
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002589 if (hotkey_poll_freq > 0 &&
2590 (poll_driver_mask ||
2591 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002592 if (!tpacpi_hotkey_task) {
2593 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002594 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002595 if (IS_ERR(tpacpi_hotkey_task)) {
2596 tpacpi_hotkey_task = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07002597 pr_err("could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002598 "for hotkey polling\n");
2599 }
2600 }
2601 } else {
2602 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002603 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002604 hotkey_poll_freq == 0) {
Joe Perches0978e012011-04-04 10:06:25 -07002605 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2606 "require polling, which is currently "
2607 "disabled\n",
2608 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002609 }
2610 }
2611}
2612
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002613static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002614{
2615 mutex_lock(&hotkey_mutex);
2616 hotkey_poll_setup(may_warn);
2617 mutex_unlock(&hotkey_mutex);
2618}
2619
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002620/* call with hotkey_mutex held */
2621static void hotkey_poll_set_freq(unsigned int freq)
2622{
2623 if (!freq)
2624 hotkey_poll_stop_sync();
2625
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002626 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002627}
2628
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002629#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2630
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002631static void hotkey_poll_setup(const bool __unused)
2632{
2633}
2634
2635static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002636{
2637}
2638
2639#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2640
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002641static int hotkey_inputdev_open(struct input_dev *dev)
2642{
2643 switch (tpacpi_lifecycle) {
2644 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002645 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002646 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002647 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002648 case TPACPI_LIFE_EXITING:
2649 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002650 }
2651
2652 /* Should only happen if tpacpi_lifecycle is corrupt */
2653 BUG();
2654 return -EBUSY;
2655}
2656
2657static void hotkey_inputdev_close(struct input_dev *dev)
2658{
2659 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002660 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002661 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002662 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002663}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002664
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002665/* sysfs hotkey enable ------------------------------------------------- */
2666static ssize_t hotkey_enable_show(struct device *dev,
2667 struct device_attribute *attr,
2668 char *buf)
2669{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002670 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002671
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002672 printk_deprecated_attribute("hotkey_enable",
2673 "Hotkey reporting is always enabled");
2674
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002675 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002676 if (res)
2677 return res;
2678
2679 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2680}
2681
2682static ssize_t hotkey_enable_store(struct device *dev,
2683 struct device_attribute *attr,
2684 const char *buf, size_t count)
2685{
2686 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002687
2688 printk_deprecated_attribute("hotkey_enable",
2689 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002690
2691 if (parse_strtoul(buf, 1, &t))
2692 return -EINVAL;
2693
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002694 if (t == 0)
2695 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002696
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002697 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002698}
2699
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002700static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002701
2702/* sysfs hotkey mask --------------------------------------------------- */
2703static ssize_t hotkey_mask_show(struct device *dev,
2704 struct device_attribute *attr,
2705 char *buf)
2706{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002707 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002708}
2709
2710static ssize_t hotkey_mask_store(struct device *dev,
2711 struct device_attribute *attr,
2712 const char *buf, size_t count)
2713{
2714 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002715 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002716
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002717 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002718 return -EINVAL;
2719
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002720 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002721 return -ERESTARTSYS;
2722
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002723 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002724
2725#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002726 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002727#endif
2728
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002729 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002730
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002731 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2732
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002733 return (res) ? res : count;
2734}
2735
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002736static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002737
2738/* sysfs hotkey bios_enabled ------------------------------------------- */
2739static ssize_t hotkey_bios_enabled_show(struct device *dev,
2740 struct device_attribute *attr,
2741 char *buf)
2742{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002743 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002744}
2745
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002746static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002747
2748/* sysfs hotkey bios_mask ---------------------------------------------- */
2749static ssize_t hotkey_bios_mask_show(struct device *dev,
2750 struct device_attribute *attr,
2751 char *buf)
2752{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002753 printk_deprecated_attribute("hotkey_bios_mask",
2754 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002755 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002756}
2757
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002758static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002759
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002760/* sysfs hotkey all_mask ----------------------------------------------- */
2761static ssize_t hotkey_all_mask_show(struct device *dev,
2762 struct device_attribute *attr,
2763 char *buf)
2764{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002765 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2766 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002767}
2768
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002769static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002770
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002771/* sysfs hotkey all_mask ----------------------------------------------- */
2772static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2773 struct device_attribute *attr,
2774 char *buf)
2775{
2776 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2777 hotkey_adaptive_all_mask | hotkey_source_mask);
2778}
2779
2780static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2781
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002782/* sysfs hotkey recommended_mask --------------------------------------- */
2783static ssize_t hotkey_recommended_mask_show(struct device *dev,
2784 struct device_attribute *attr,
2785 char *buf)
2786{
2787 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002788 (hotkey_all_mask | hotkey_source_mask)
2789 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002790}
2791
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002792static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002793
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002794#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2795
2796/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2797static ssize_t hotkey_source_mask_show(struct device *dev,
2798 struct device_attribute *attr,
2799 char *buf)
2800{
2801 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2802}
2803
2804static ssize_t hotkey_source_mask_store(struct device *dev,
2805 struct device_attribute *attr,
2806 const char *buf, size_t count)
2807{
2808 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002809 u32 r_ev;
2810 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002811
2812 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2813 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2814 return -EINVAL;
2815
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002816 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002817 return -ERESTARTSYS;
2818
2819 HOTKEY_CONFIG_CRITICAL_START
2820 hotkey_source_mask = t;
2821 HOTKEY_CONFIG_CRITICAL_END
2822
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002823 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2824 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002825 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002826
2827 /* check if events needed by the driver got disabled */
2828 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2829 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002830
2831 mutex_unlock(&hotkey_mutex);
2832
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002833 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07002834 pr_err("hotkey_source_mask: "
2835 "failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002836
2837 if (r_ev)
Joe Perches0978e012011-04-04 10:06:25 -07002838 pr_notice("hotkey_source_mask: "
2839 "some important events were disabled: 0x%04x\n",
2840 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002841
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002842 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2843
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002844 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002845}
2846
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002847static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002848
2849/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2850static ssize_t hotkey_poll_freq_show(struct device *dev,
2851 struct device_attribute *attr,
2852 char *buf)
2853{
2854 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2855}
2856
2857static ssize_t hotkey_poll_freq_store(struct device *dev,
2858 struct device_attribute *attr,
2859 const char *buf, size_t count)
2860{
2861 unsigned long t;
2862
2863 if (parse_strtoul(buf, 25, &t))
2864 return -EINVAL;
2865
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002866 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002867 return -ERESTARTSYS;
2868
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002869 hotkey_poll_set_freq(t);
2870 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002871
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002872 mutex_unlock(&hotkey_mutex);
2873
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002874 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2875
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002876 return count;
2877}
2878
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002879static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002880
2881#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2882
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002883/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002884static ssize_t hotkey_radio_sw_show(struct device *dev,
2885 struct device_attribute *attr,
2886 char *buf)
2887{
Johannes Berg19d337d2009-06-02 13:01:37 +02002888 int res;
2889 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002890 if (res < 0)
2891 return res;
2892
Johannes Berg19d337d2009-06-02 13:01:37 +02002893 /* Opportunistic update */
2894 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2895
2896 return snprintf(buf, PAGE_SIZE, "%d\n",
2897 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002898}
2899
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002900static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002901
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002902static void hotkey_radio_sw_notify_change(void)
2903{
2904 if (tp_features.hotkey_wlsw)
2905 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2906 "hotkey_radio_sw");
2907}
2908
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002909/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2910static ssize_t hotkey_tablet_mode_show(struct device *dev,
2911 struct device_attribute *attr,
2912 char *buf)
2913{
2914 int res, s;
2915 res = hotkey_get_tablet_mode(&s);
2916 if (res < 0)
2917 return res;
2918
2919 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2920}
2921
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002922static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002923
2924static void hotkey_tablet_mode_notify_change(void)
2925{
2926 if (tp_features.hotkey_tablet)
2927 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2928 "hotkey_tablet_mode");
2929}
2930
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002931/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002932static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2933 struct device_attribute *attr,
2934 char *buf)
2935{
2936 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2937}
2938
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002939static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002940
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002941static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002942{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002943 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2944 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002945}
2946
2947/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002948static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2949 struct device_attribute *attr,
2950 char *buf)
2951{
2952 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2953}
2954
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002955static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2956 hotkey_wakeup_hotunplug_complete_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002957
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002958static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002959{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002960 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2961 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002962}
2963
Bastien Nocerab790cee2015-03-02 14:45:27 +01002964/* sysfs adaptive kbd mode --------------------------------------------- */
2965
2966static int adaptive_keyboard_get_mode(void);
2967static int adaptive_keyboard_set_mode(int new_mode);
2968
2969enum ADAPTIVE_KEY_MODE {
2970 HOME_MODE,
2971 WEB_BROWSER_MODE,
2972 WEB_CONFERENCE_MODE,
2973 FUNCTION_MODE,
2974 LAYFLAT_MODE
2975};
2976
2977static ssize_t adaptive_kbd_mode_show(struct device *dev,
2978 struct device_attribute *attr,
2979 char *buf)
2980{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03002981 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01002982
2983 current_mode = adaptive_keyboard_get_mode();
2984 if (current_mode < 0)
2985 return current_mode;
2986
2987 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
2988}
2989
2990static ssize_t adaptive_kbd_mode_store(struct device *dev,
2991 struct device_attribute *attr,
2992 const char *buf, size_t count)
2993{
2994 unsigned long t;
2995 int res;
2996
2997 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2998 return -EINVAL;
2999
3000 res = adaptive_keyboard_set_mode(t);
3001 return (res < 0) ? res : count;
3002}
3003
3004static DEVICE_ATTR_RW(adaptive_kbd_mode);
3005
3006static struct attribute *adaptive_kbd_attributes[] = {
3007 &dev_attr_adaptive_kbd_mode.attr,
3008 NULL
3009};
3010
3011static const struct attribute_group adaptive_kbd_attr_group = {
3012 .attrs = adaptive_kbd_attributes,
3013};
3014
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003015/* --------------------------------------------------------------------- */
3016
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003017static struct attribute *hotkey_attributes[] __initdata = {
3018 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003019 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003020 &dev_attr_hotkey_bios_mask.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02003021 &dev_attr_wakeup_reason.attr,
3022 &dev_attr_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003023 &dev_attr_hotkey_mask.attr,
3024 &dev_attr_hotkey_all_mask.attr,
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003025 &dev_attr_hotkey_adaptive_all_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003026 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003027#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003028 &dev_attr_hotkey_source_mask.attr,
3029 &dev_attr_hotkey_poll_freq.attr,
3030#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003031};
3032
Johannes Berg19d337d2009-06-02 13:01:37 +02003033/*
3034 * Sync both the hw and sw blocking state of all switches
3035 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003036static void tpacpi_send_radiosw_update(void)
3037{
3038 int wlsw;
3039
Johannes Berg19d337d2009-06-02 13:01:37 +02003040 /*
3041 * We must sync all rfkill controllers *before* issuing any
3042 * rfkill input events, or we will race the rfkill core input
3043 * handler.
3044 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003045 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003046 * for the above.
3047 *
3048 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3049 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003050
Johannes Berg19d337d2009-06-02 13:01:37 +02003051 wlsw = hotkey_get_wlsw();
3052
3053 /* Sync hw blocking state first if it is hw-blocked */
3054 if (wlsw == TPACPI_RFK_RADIO_OFF)
3055 tpacpi_rfk_update_hwblock_state(true);
3056
3057 /* Sync sw blocking state */
3058 tpacpi_rfk_update_swstate_all();
3059
3060 /* Sync hw blocking state last if it is hw-unblocked */
3061 if (wlsw == TPACPI_RFK_RADIO_ON)
3062 tpacpi_rfk_update_hwblock_state(false);
3063
3064 /* Issue rfkill input event for WLSW switch */
3065 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003066 mutex_lock(&tpacpi_inputdev_send_mutex);
3067
3068 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003069 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003070 input_sync(tpacpi_inputdev);
3071
3072 mutex_unlock(&tpacpi_inputdev_send_mutex);
3073 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003074
3075 /*
3076 * this can be unconditional, as we will poll state again
3077 * if userspace uses the notify to read data
3078 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003079 hotkey_radio_sw_notify_change();
3080}
3081
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003082static void hotkey_exit(void)
3083{
3084#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003085 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003086 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003087 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003088#endif
3089
3090 if (hotkey_dev_attributes)
3091 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3092
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003093 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003094 "restoring original HKEY status and mask\n");
3095 /* yes, there is a bitwise or below, we want the
3096 * functions to be called even if one of them fail */
3097 if (((tp_features.hotkey_mask &&
3098 hotkey_mask_set(hotkey_orig_mask)) |
3099 hotkey_status_set(false)) != 0)
Joe Perches0978e012011-04-04 10:06:25 -07003100 pr_err("failed to restore hot key mask "
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003101 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003102}
3103
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003104static void __init hotkey_unmap(const unsigned int scancode)
3105{
3106 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3107 clear_bit(hotkey_keycode_map[scancode],
3108 tpacpi_inputdev->keybit);
3109 hotkey_keycode_map[scancode] = KEY_RESERVED;
3110 }
3111}
3112
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003113/*
3114 * HKEY quirks:
3115 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3116 */
3117
3118#define TPACPI_HK_Q_INIMASK 0x0001
3119
3120static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3121 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3122 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3123 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3124 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3125 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3126 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3127 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3128 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3129 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3130 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3131 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3132 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3133 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3134 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3135 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3136 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3137 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3138 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3139 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3140};
3141
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003142typedef u16 tpacpi_keymap_entry_t;
3143typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003144
Lyudeb3180022016-11-11 15:15:02 -05003145static int hotkey_init_tablet_mode(void)
3146{
Darren Hartcb2bf252016-12-14 14:45:48 -08003147 int in_tablet_mode = 0, res;
3148 char *type = NULL;
Lyudeb3180022016-11-11 15:15:02 -05003149
Lyudeb3180022016-11-11 15:15:02 -05003150 if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
Lyudeb03f4d42016-11-11 15:15:03 -05003151 /* For X41t, X60t, X61t Tablets... */
Lyudeb3180022016-11-11 15:15:02 -05003152 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3153 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3154 type = "MHKG";
Lyudeb03f4d42016-11-11 15:15:03 -05003155 } else if (acpi_evalf(ec_handle, &res, "CMMD", "qd")) {
3156 /* For X1 Yoga (2016) */
3157 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_CMMD;
3158 in_tablet_mode = res == TP_EC_CMMD_TABLET_MODE;
3159 type = "CMMD";
Lyudeb3180022016-11-11 15:15:02 -05003160 }
3161
3162 if (!tp_features.hotkey_tablet)
3163 return 0;
3164
3165 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3166 type, in_tablet_mode ? "tablet" : "laptop");
3167
3168 res = add_to_attr_set(hotkey_dev_attributes,
3169 &dev_attr_hotkey_tablet_mode.attr);
3170 if (res)
3171 return -1;
3172
3173 return in_tablet_mode;
3174}
3175
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003176static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003177{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003178 /* Requirements for changing the default keymaps:
3179 *
3180 * 1. Many of the keys are mapped to KEY_RESERVED for very
3181 * good reasons. Do not change them unless you have deep
3182 * knowledge on the IBM and Lenovo ThinkPad firmware for
3183 * the various ThinkPad models. The driver behaves
3184 * differently for KEY_RESERVED: such keys have their
3185 * hot key mask *unset* in mask_recommended, and also
3186 * in the initial hot key mask programmed into the
3187 * firmware at driver load time, which means the firm-
3188 * ware may react very differently if you change them to
3189 * something else;
3190 *
3191 * 2. You must be subscribed to the linux-thinkpad and
3192 * ibm-acpi-devel mailing lists, and you should read the
3193 * list archives since 2007 if you want to change the
3194 * keymaps. This requirement exists so that you will
3195 * know the past history of problems with the thinkpad-
3196 * acpi driver keymaps, and also that you will be
3197 * listening to any bug reports;
3198 *
3199 * 3. Do not send thinkpad-acpi specific patches directly to
3200 * for merging, *ever*. Send them to the linux-acpi
3201 * mailinglist for comments. Merging is to be done only
3202 * through acpi-test and the ACPI maintainer.
3203 *
3204 * If the above is too much to ask, don't change the keymap.
3205 * Ask the thinkpad-acpi maintainer to do it, instead.
3206 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003207
3208 enum keymap_index {
3209 TPACPI_KEYMAP_IBM_GENERIC = 0,
3210 TPACPI_KEYMAP_LENOVO_GENERIC,
3211 };
3212
3213 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3214 /* Generic keymap for IBM ThinkPads */
3215 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003216 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003217 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003218 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3219 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003220
3221 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003222 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3223 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3224 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003225
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003226 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003227 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003228 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003229
3230 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003231 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003232
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003233 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3234 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003235
3236 /* Volume: firmware always react to it and reprograms
3237 * the built-in *extra* mixer. Never map it to control
3238 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003239 KEY_RESERVED, /* 0x14: VOLUME UP */
3240 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3241 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003242
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003243 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003244
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003245 /* (assignments unknown, please report if found) */
3246 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3247 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003248
3249 /* No assignments, only used for Adaptive keyboards. */
3250 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3251 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3252 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3253 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3254 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003255 },
3256
3257 /* Generic keymap for Lenovo ThinkPads */
3258 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003259 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3260 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003261 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003262 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003263
3264 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003265 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3266 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3267 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003268
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003269 /* These should be enabled --only-- when ACPI video
3270 * is disabled (i.e. in "vendor" mode), and are handled
3271 * in a special way by the init code */
3272 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3273 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003274
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003275 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003276
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003277 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3278 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003279
3280 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3281 * react to it and reprograms the built-in *extra* mixer.
3282 * Never map it to control another mixer by default.
3283 *
3284 * T60?, T61, R60?, R61: firmware and EC tries to send
3285 * these over the regular keyboard, so these are no-ops,
3286 * but there are still weird bugs re. MUTE, so do not
3287 * change unless you get test reports from all Lenovo
3288 * models. May cause the BIOS to interfere with the
3289 * HDA mixer.
3290 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003291 KEY_RESERVED, /* 0x14: VOLUME UP */
3292 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3293 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003294
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003295 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003296
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003297 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003298 KEY_UNKNOWN, KEY_UNKNOWN,
3299
3300 /*
3301 * The mic mute button only sends 0x1a. It does not
3302 * automatically mute the mic or change the mute light.
3303 */
3304 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3305
3306 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003307 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003308
3309 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003310 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003311
3312 /*
3313 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3314 * The first item in this list is the Mute button which is
3315 * emitted with 0x103 through
3316 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3317 * symbol is held.
3318 * We'll need to offset those by 0x20.
3319 */
3320 KEY_RESERVED, /* Mute held, 0x103 */
3321 KEY_BRIGHTNESS_MIN, /* Backlight off */
3322 KEY_RESERVED, /* Clipping tool */
3323 KEY_RESERVED, /* Cloud */
3324 KEY_RESERVED,
3325 KEY_VOICECOMMAND, /* Voice */
3326 KEY_RESERVED,
3327 KEY_RESERVED, /* Gestures */
3328 KEY_RESERVED,
3329 KEY_RESERVED,
3330 KEY_RESERVED,
3331 KEY_CONFIG, /* Settings */
3332 KEY_RESERVED, /* New tab */
3333 KEY_REFRESH, /* Reload */
3334 KEY_BACK, /* Back */
3335 KEY_RESERVED, /* Microphone down */
3336 KEY_RESERVED, /* Microphone up */
3337 KEY_RESERVED, /* Microphone cancellation */
3338 KEY_RESERVED, /* Camera mode */
3339 KEY_RESERVED, /* Rotate display, 0x116 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003340 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003341 };
3342
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003343 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3344 /* Generic maps (fallback) */
3345 {
3346 .vendor = PCI_VENDOR_ID_IBM,
3347 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3348 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3349 },
3350 {
3351 .vendor = PCI_VENDOR_ID_LENOVO,
3352 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3353 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3354 },
3355 };
3356
3357#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003358#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003359
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003360 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003361 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003362 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003363 bool radiosw_state = false;
3364 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003365
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003366 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003367 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003368
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003369 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3370 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003371
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003372 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003373 BUG_ON(tpacpi_inputdev->open != NULL ||
3374 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003375
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003376 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003377 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003378
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003379#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003380 mutex_init(&hotkey_thread_data_mutex);
3381#endif
3382
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003383 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003384 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003385
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003386 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3387 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003388 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003389
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003390 if (!tp_features.hotkey)
3391 return 1;
3392
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003393 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3394 ARRAY_SIZE(tpacpi_hotkey_qtable));
3395
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003396 tpacpi_disable_brightness_delay();
3397
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003398 /* MUST have enough space for all attributes to be added to
3399 * hotkey_dev_attributes */
3400 hotkey_dev_attributes = create_attr_set(
3401 ARRAY_SIZE(hotkey_attributes) + 2,
3402 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003403 if (!hotkey_dev_attributes)
3404 return -ENOMEM;
3405 res = add_many_to_attr_set(hotkey_dev_attributes,
3406 hotkey_attributes,
3407 ARRAY_SIZE(hotkey_attributes));
3408 if (res)
3409 goto err_exit;
3410
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003411 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003412 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3413 for HKEY interface version 0x100 */
3414 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003415 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3416 "firmware HKEY interface version: 0x%x\n",
3417 hkeyv);
3418
3419 switch (hkeyv >> 8) {
3420 case 1:
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003421 /*
3422 * MHKV 0x100 in A31, R40, R40e,
3423 * T4x, X31, and later
3424 */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003425
3426 /* Paranoia check AND init hotkey_all_mask */
3427 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3428 "MHKA", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003429 pr_err("missing MHKA handler, please report this to %s\n",
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003430 TPACPI_MAIL);
3431 /* Fallback: pre-init for FN+F3,F4,F12 */
3432 hotkey_all_mask = 0x080cU;
3433 } else {
3434 tp_features.hotkey_mask = 1;
3435 }
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003436 break;
3437
3438 case 2:
3439 /*
3440 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3441 */
3442
3443 /* Paranoia check AND init hotkey_all_mask */
3444 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3445 "MHKA", "dd", 1)) {
3446 pr_err("missing MHKA handler, please report this to %s\n",
3447 TPACPI_MAIL);
3448 /* Fallback: pre-init for FN+F3,F4,F12 */
3449 hotkey_all_mask = 0x080cU;
3450 } else {
3451 tp_features.hotkey_mask = 1;
3452 }
3453
3454 /*
3455 * Check if we have an adaptive keyboard, like on the
3456 * Lenovo Carbon X1 2014 (2nd Gen).
3457 */
3458 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3459 "MHKA", "dd", 2)) {
3460 if (hotkey_adaptive_all_mask != 0) {
3461 tp_features.has_adaptive_kbd = true;
3462 res = sysfs_create_group(
3463 &tpacpi_pdev->dev.kobj,
3464 &adaptive_kbd_attr_group);
3465 if (res)
3466 goto err_exit;
3467 }
3468 } else {
3469 tp_features.has_adaptive_kbd = false;
3470 hotkey_adaptive_all_mask = 0x0U;
3471 }
3472 break;
3473
3474 default:
3475 pr_err("unknown version of the HKEY interface: 0x%x\n",
3476 hkeyv);
3477 pr_err("please report this to %s\n", TPACPI_MAIL);
3478 break;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003479 }
3480 }
3481
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003482 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3483 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003484 str_supported(tp_features.hotkey_mask));
3485
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003486 /* Init hotkey_all_mask if not initialized yet */
3487 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3488 (quirks & TPACPI_HK_Q_INIMASK))
3489 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003490
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003491 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003492 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003493 /* hotkey_source_mask *must* be zero for
3494 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003495 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003496 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003497 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003498
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003499 hotkey_orig_mask = hotkey_acpi_mask;
3500 } else {
3501 hotkey_orig_mask = hotkey_all_mask;
3502 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003503 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003504
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003505#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3506 if (dbg_wlswemul) {
3507 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003508 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003509 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003510 } else
3511#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003512 /* Not all thinkpads have a hardware radio switch */
3513 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3514 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003515 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003516 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003517 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003518 }
3519 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003520 res = add_to_attr_set(hotkey_dev_attributes,
3521 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003522
Lyudeb3180022016-11-11 15:15:02 -05003523 res = hotkey_init_tablet_mode();
3524 if (res < 0)
3525 goto err_exit;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003526
Lyudeb3180022016-11-11 15:15:02 -05003527 tabletsw_state = res;
3528
3529 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3530 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003531 if (res)
3532 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003533
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003534 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003535 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3536 GFP_KERNEL);
3537 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003538 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003539 res = -ENOMEM;
3540 goto err_exit;
3541 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003542
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003543 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3544 ARRAY_SIZE(tpacpi_keymap_qtable));
3545 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3546 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3547 "using keymap number %lu\n", keymap_id);
3548
3549 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3550 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003551
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003552 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003553 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3554 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3555 tpacpi_inputdev->keycode = hotkey_keycode_map;
3556 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3557 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003558 input_set_capability(tpacpi_inputdev, EV_KEY,
3559 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003560 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003561 if (i < sizeof(hotkey_reserved_mask)*8)
3562 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003563 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003564 }
3565
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003566 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003567 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003568 input_report_switch(tpacpi_inputdev,
3569 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003570 }
3571 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003572 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003573 input_report_switch(tpacpi_inputdev,
3574 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003575 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003576
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003577 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003578 * userspace. tpacpi_detect_brightness_capabilities() must have
3579 * been called before this point */
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02003580 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Joe Perches0978e012011-04-04 10:06:25 -07003581 pr_info("This ThinkPad has standard ACPI backlight "
3582 "brightness control, supported by the ACPI "
3583 "video driver\n");
3584 pr_notice("Disabling thinkpad-acpi brightness events "
3585 "by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003586
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003587 /* Disable brightness up/down on Lenovo thinkpads when
3588 * ACPI is handling them, otherwise it is plain impossible
3589 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003590 hotkey_reserved_mask |=
3591 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3592 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003593 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3594 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003595 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003596
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003597#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003598 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3599 & ~hotkey_all_mask
3600 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003601
3602 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3603 "hotkey source mask 0x%08x, polling freq %u\n",
3604 hotkey_source_mask, hotkey_poll_freq);
3605#endif
3606
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003607 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3608 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003609 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003610 if (res) {
3611 hotkey_exit();
3612 return res;
3613 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003614 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3615 | hotkey_driver_mask)
3616 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003617 if (res < 0 && res != -ENXIO) {
3618 hotkey_exit();
3619 return res;
3620 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003621 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3622 & ~hotkey_reserved_mask;
3623 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3624 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3625 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003626
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003627 tpacpi_inputdev->open = &hotkey_inputdev_open;
3628 tpacpi_inputdev->close = &hotkey_inputdev_close;
3629
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003630 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003631
3632 return 0;
3633
3634err_exit:
3635 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
Bastien Nocerab790cee2015-03-02 14:45:27 +01003636 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3637 &adaptive_kbd_attr_group);
3638
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003639 hotkey_dev_attributes = NULL;
3640
Jan van den Berg72a979f2014-09-17 00:01:08 +02003641 return (res < 0) ? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003642}
3643
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003644/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3645 * mode, Web conference mode, Function mode and Lay-flat mode.
3646 * We support Home mode and Function mode currently.
3647 *
3648 * Will consider support rest of modes in future.
3649 *
3650 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003651static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003652 HOME_MODE,
3653/* WEB_BROWSER_MODE = 2,
3654 WEB_CONFERENCE_MODE = 3, */
3655 FUNCTION_MODE
3656};
3657
3658#define DFR_CHANGE_ROW 0x101
3659#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003660#define FIRST_ADAPTIVE_KEY 0x103
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003661
3662/* press Fn key a while second, it will switch to Function Mode. Then
3663 * release Fn key, previous mode be restored.
3664 */
3665static bool adaptive_keyboard_mode_is_saved;
3666static int adaptive_keyboard_prev_mode;
3667
Bastien Noceraf74587f2015-03-02 14:45:22 +01003668static int adaptive_keyboard_get_mode(void)
3669{
3670 int mode = 0;
3671
3672 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3673 pr_err("Cannot read adaptive keyboard mode\n");
3674 return -EIO;
3675 }
3676
3677 return mode;
3678}
3679
3680static int adaptive_keyboard_set_mode(int new_mode)
3681{
3682 if (new_mode < 0 ||
3683 new_mode > LAYFLAT_MODE)
3684 return -EINVAL;
3685
3686 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3687 pr_err("Cannot set adaptive keyboard mode\n");
3688 return -EIO;
3689 }
3690
3691 return 0;
3692}
3693
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003694static int adaptive_keyboard_get_next_mode(int mode)
3695{
3696 size_t i;
3697 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3698
3699 for (i = 0; i <= max_mode; i++) {
3700 if (adaptive_keyboard_modes[i] == mode)
3701 break;
3702 }
3703
3704 if (i >= max_mode)
3705 i = 0;
3706 else
3707 i++;
3708
3709 return adaptive_keyboard_modes[i];
3710}
3711
3712static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3713{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003714 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003715 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003716 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003717
3718 switch (scancode) {
3719 case DFR_CHANGE_ROW:
3720 if (adaptive_keyboard_mode_is_saved) {
3721 new_mode = adaptive_keyboard_prev_mode;
3722 adaptive_keyboard_mode_is_saved = false;
3723 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003724 current_mode = adaptive_keyboard_get_mode();
3725 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003726 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003727 new_mode = adaptive_keyboard_get_next_mode(
3728 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003729 }
3730
Bastien Noceraf74587f2015-03-02 14:45:22 +01003731 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003732 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003733
3734 return true;
3735
3736 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003737 current_mode = adaptive_keyboard_get_mode();
3738 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003739 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003740
Bastien Noceraf74587f2015-03-02 14:45:22 +01003741 adaptive_keyboard_prev_mode = current_mode;
3742 adaptive_keyboard_mode_is_saved = true;
3743
3744 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3745 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003746 return true;
3747
3748 default:
Dan Carpenter741d98c2015-03-11 12:36:07 +03003749 if (scancode < FIRST_ADAPTIVE_KEY ||
3750 scancode >= FIRST_ADAPTIVE_KEY + TPACPI_HOTKEY_MAP_LEN -
Christian Kellner149c8c72017-02-28 17:10:56 +01003751 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
Bastien Nocera6a68d852015-03-02 14:45:31 +01003752 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3753 scancode);
3754 return false;
3755 }
Christian Kellner149c8c72017-02-28 17:10:56 +01003756 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3757 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
Bastien Nocera6a68d852015-03-02 14:45:31 +01003758 if (keycode != KEY_RESERVED) {
3759 mutex_lock(&tpacpi_inputdev_send_mutex);
3760
3761 input_report_key(tpacpi_inputdev, keycode, 1);
3762 input_sync(tpacpi_inputdev);
3763
3764 input_report_key(tpacpi_inputdev, keycode, 0);
3765 input_sync(tpacpi_inputdev);
3766
3767 mutex_unlock(&tpacpi_inputdev_send_mutex);
3768 }
3769 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003770 }
3771}
3772
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003773static bool hotkey_notify_hotkey(const u32 hkey,
3774 bool *send_acpi_ev,
3775 bool *ignore_acpi_ev)
3776{
3777 /* 0x1000-0x1FFF: key presses */
3778 unsigned int scancode = hkey & 0xfff;
3779 *send_acpi_ev = true;
3780 *ignore_acpi_ev = false;
3781
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03003782 /* HKEY event 0x1001 is scancode 0x00 */
Christian Kellner149c8c72017-02-28 17:10:56 +01003783 if (scancode > 0 && scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003784 scancode--;
3785 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003786 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003787 *send_acpi_ev = false;
3788 } else {
3789 *ignore_acpi_ev = true;
3790 }
3791 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003792 } else {
3793 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003794 }
3795 return false;
3796}
3797
3798static bool hotkey_notify_wakeup(const u32 hkey,
3799 bool *send_acpi_ev,
3800 bool *ignore_acpi_ev)
3801{
3802 /* 0x2000-0x2FFF: Wakeup reason */
3803 *send_acpi_ev = true;
3804 *ignore_acpi_ev = false;
3805
3806 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003807 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3808 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003809 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3810 *ignore_acpi_ev = true;
3811 break;
3812
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003813 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3814 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003815 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3816 *ignore_acpi_ev = true;
3817 break;
3818
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003819 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3820 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003821 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003822 /* how to auto-heal: */
3823 /* 2313: woke up from S3, go to S4/S5 */
3824 /* 2413: woke up from S4, go to S5 */
3825 break;
3826
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003827 default:
3828 return false;
3829 }
3830
3831 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003832 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003833 hotkey_wakeup_reason_notify_change();
3834 }
3835 return true;
3836}
3837
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003838static bool hotkey_notify_dockevent(const u32 hkey,
3839 bool *send_acpi_ev,
3840 bool *ignore_acpi_ev)
3841{
3842 /* 0x4000-0x4FFF: dock-related events */
3843 *send_acpi_ev = true;
3844 *ignore_acpi_ev = false;
3845
3846 switch (hkey) {
3847 case TP_HKEY_EV_UNDOCK_ACK:
3848 /* ACPI undock operation completed after wakeup */
3849 hotkey_autosleep_ack = 1;
3850 pr_info("undocked\n");
3851 hotkey_wakeup_hotunplug_complete_notify_change();
3852 return true;
3853
3854 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3855 pr_info("docked into hotplug port replicator\n");
3856 return true;
3857 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3858 pr_info("undocked from hotplug port replicator\n");
3859 return true;
3860
3861 default:
3862 return false;
3863 }
3864}
3865
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003866static bool hotkey_notify_usrevent(const u32 hkey,
3867 bool *send_acpi_ev,
3868 bool *ignore_acpi_ev)
3869{
3870 /* 0x5000-0x5FFF: human interface helpers */
3871 *send_acpi_ev = true;
3872 *ignore_acpi_ev = false;
3873
3874 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003875 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3876 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003877 return true;
3878
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003879 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3880 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003881 tpacpi_input_send_tabletsw();
3882 hotkey_tablet_mode_notify_change();
3883 *send_acpi_ev = false;
3884 return true;
3885
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003886 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3887 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3888 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003889 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003890 *ignore_acpi_ev = true;
3891 return true;
3892
3893 default:
3894 return false;
3895 }
3896}
3897
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003898static void thermal_dump_all_sensors(void);
3899
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003900static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003901 bool *send_acpi_ev,
3902 bool *ignore_acpi_ev)
3903{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003904 bool known = true;
3905
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003906 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003907 *send_acpi_ev = true;
3908 *ignore_acpi_ev = false;
3909
3910 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003911 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003912 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003913 /* recommended action: do nothing, we don't have
3914 * Lenovo ATM information */
3915 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003916 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003917 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003918 /* recommended action: warn user through gui */
3919 break;
3920 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003921 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003922 /* recommended action: immediate sleep/hibernate */
3923 break;
3924 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003925 pr_crit("THERMAL ALARM: "
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003926 "a sensor reports something is too hot!\n");
3927 /* recommended action: warn user through gui, that */
3928 /* some internal component is too hot */
3929 break;
3930 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003931 pr_alert("THERMAL EMERGENCY: "
3932 "a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003933 /* recommended action: immediate sleep/hibernate */
3934 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01003935 case TP_HKEY_EV_AC_CHANGED:
3936 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3937 * AC status changed; can be triggered by plugging or
3938 * unplugging AC adapter, docking or undocking. */
3939
3940 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003941
3942 case TP_HKEY_EV_KEY_NUMLOCK:
3943 case TP_HKEY_EV_KEY_FN:
Xavier Naveira67ab6242015-02-10 08:45:18 +01003944 case TP_HKEY_EV_KEY_FN_ESC:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003945 /* key press events, we just ignore them as long as the EC
3946 * is still reporting them in the normal keyboard stream */
3947 *send_acpi_ev = false;
3948 *ignore_acpi_ev = true;
3949 return true;
3950
Lyudeb03f4d42016-11-11 15:15:03 -05003951 case TP_HKEY_EV_TABLET_CHANGED:
3952 tpacpi_input_send_tabletsw();
3953 hotkey_tablet_mode_notify_change();
3954 *send_acpi_ev = false;
3955 break;
3956
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003957 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003958 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003959 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003960 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003961
3962 thermal_dump_all_sensors();
3963
3964 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003965}
3966
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003967static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3968{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003969 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003970 bool send_acpi_ev;
3971 bool ignore_acpi_ev;
3972 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003973
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003974 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07003975 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003976 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003977 acpi_bus_generate_netlink_event(
3978 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003979 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003980 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003981 return;
3982 }
3983
3984 while (1) {
3985 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07003986 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003987 return;
3988 }
3989
3990 if (hkey == 0) {
3991 /* queue empty */
3992 return;
3993 }
3994
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003995 send_acpi_ev = true;
3996 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003997
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003998 switch (hkey >> 12) {
3999 case 1:
4000 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004001 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4002 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004003 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004004 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004005 /* 0x2000-0x2FFF: Wakeup reason */
4006 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4007 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004008 break;
4009 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004010 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004011 switch (hkey) {
4012 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004013 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004014 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004015 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004016 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03004017 break;
4018 case TP_HKEY_EV_OPTDRV_EJ:
4019 /* FIXME: kick libata if SATA link offline */
4020 known_ev = true;
4021 break;
4022 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004023 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004024 }
4025 break;
4026 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03004027 /* 0x4000-0x4FFF: dock-related events */
4028 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4029 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004030 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004031 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02004032 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004033 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4034 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004035 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004036 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03004037 /* 0x6000-0x6FFF: thermal alarms/notices and
4038 * keyboard events */
4039 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02004040 &ignore_acpi_ev);
4041 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004042 case 7:
4043 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03004044 if (tp_features.hotkey_wlsw &&
4045 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004046 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004047 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004048 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004049 break;
4050 }
4051 /* fallthrough to default */
4052 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004053 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02004054 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004055 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07004056 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4057 pr_notice("please report the conditions when this "
4058 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004059 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004060
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004061 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03004062 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004063 acpi_bus_generate_netlink_event(
4064 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004065 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004066 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004067 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004068 }
4069}
4070
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02004071static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004072{
4073 /* Do these on suspend, we get the events on early resume! */
4074 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4075 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08004076
4077 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004078 if (tp_features.has_adaptive_kbd) {
4079 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4080 "GTRW", "dd", 0)) {
4081 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004082 }
4083 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004084}
4085
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004086static void hotkey_resume(void)
4087{
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03004088 tpacpi_disable_brightness_delay();
4089
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004090 if (hotkey_status_set(true) < 0 ||
4091 hotkey_mask_set(hotkey_acpi_mask) < 0)
Joe Perches0978e012011-04-04 10:06:25 -07004092 pr_err("error while attempting to reset the event "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004093 "firmware interface\n");
4094
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004095 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02004096 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004097 hotkey_wakeup_reason_notify_change();
4098 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004099 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004100
4101 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004102 if (tp_features.has_adaptive_kbd) {
4103 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4104 adaptive_keyboard_prev_mode)) {
4105 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004106 }
4107 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004108}
4109
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004110/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004111static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004113 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004115 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004116 seq_printf(m, "status:\t\tnot supported\n");
4117 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004118 }
4119
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004120 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004121 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004122 res = hotkey_status_get(&status);
4123 if (!res)
4124 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004125 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004126 if (res)
4127 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004129 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004130 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004131 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4132 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004134 seq_printf(m, "mask:\t\tnot supported\n");
4135 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 }
4137
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139}
4140
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004141static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004142{
4143 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004144 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Joe Perches0978e012011-04-04 10:06:25 -07004145 pr_fmt("hotkey enable/disable functionality has been "
4146 "removed from the driver. "
4147 "Hotkeys are always enabled.\n")))
4148 pr_err("Please remove the hotkey=enable module "
4149 "parameter, it is deprecated. "
4150 "Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004151}
4152
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004153static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004155 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004156 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004159 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 return -ENODEV;
4161
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004162 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004163 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004164
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004165 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004166
4167 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 while ((cmd = next_cmd(&buf))) {
4169 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004170 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004172 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004173 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa462009-09-12 15:22:16 -03004175 mask = (hotkey_all_mask | hotkey_source_mask)
4176 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4178 /* mask set */
4179 } else if (sscanf(cmd, "%x", &mask) == 1) {
4180 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004181 } else {
4182 res = -EINVAL;
4183 goto errexit;
4184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004186
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004187 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004188 tpacpi_disclose_usertask("procfs hotkey",
4189 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004190 res = hotkey_user_mask_set(mask);
4191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004193errexit:
4194 mutex_unlock(&hotkey_mutex);
4195 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004196}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004198static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004199 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4200 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Hui Wanga3c42a42016-11-08 16:13:23 +08004201 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004202 {"", 0},
4203};
4204
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004205static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004206 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004207 .notify = hotkey_notify,
4208 .handle = &hkey_handle,
4209 .type = ACPI_DEVICE_NOTIFY,
4210};
4211
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004212static struct ibm_struct hotkey_driver_data = {
4213 .name = "hotkey",
4214 .read = hotkey_read,
4215 .write = hotkey_write,
4216 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004217 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004218 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004219 .acpi = &ibm_hotkey_acpidriver,
4220};
4221
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004222/*************************************************************************
4223 * Bluetooth subdriver
4224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004226enum {
4227 /* ACPI GBDC/SBDC bits */
4228 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4229 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004230 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004231 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004232};
4233
4234enum {
4235 /* ACPI \BLTH commands */
4236 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4237 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4238 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4239 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4240 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004241};
4242
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004243#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4244
Johannes Berg19d337d2009-06-02 13:01:37 +02004245static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004246{
4247 int status;
4248
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004249#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4250 if (dbg_bluetoothemul)
4251 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004252 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004253#endif
4254
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004255 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4256 return -EIO;
4257
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004258 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004259 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004260}
4261
Johannes Berg19d337d2009-06-02 13:01:37 +02004262static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004263{
4264 int status;
4265
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004266 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004267 "will attempt to %s bluetooth\n",
4268 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004269
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004270#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4271 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004272 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004273 return 0;
4274 }
4275#endif
4276
Johannes Berg19d337d2009-06-02 13:01:37 +02004277 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004278 status = TP_ACPI_BLUETOOTH_RADIOSSW
4279 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4280 else
4281 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004282
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004283 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4284 return -EIO;
4285
4286 return 0;
4287}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004288
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004289/* sysfs bluetooth enable ---------------------------------------------- */
4290static ssize_t bluetooth_enable_show(struct device *dev,
4291 struct device_attribute *attr,
4292 char *buf)
4293{
Johannes Berg19d337d2009-06-02 13:01:37 +02004294 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4295 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004296}
4297
4298static ssize_t bluetooth_enable_store(struct device *dev,
4299 struct device_attribute *attr,
4300 const char *buf, size_t count)
4301{
Johannes Berg19d337d2009-06-02 13:01:37 +02004302 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4303 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004304}
4305
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004306static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004307
4308/* --------------------------------------------------------------------- */
4309
4310static struct attribute *bluetooth_attributes[] = {
4311 &dev_attr_bluetooth_enable.attr,
4312 NULL
4313};
4314
4315static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004316 .attrs = bluetooth_attributes,
4317};
4318
Johannes Berg19d337d2009-06-02 13:01:37 +02004319static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4320 .get_status = bluetooth_get_status,
4321 .set_status = bluetooth_set_status,
4322};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004323
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004324static void bluetooth_shutdown(void)
4325{
4326 /* Order firmware to save current state to NVRAM */
4327 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4328 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004329 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004330 else
4331 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004332 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004333}
4334
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004335static void bluetooth_exit(void)
4336{
4337 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4338 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004339
4340 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4341
4342 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004343}
4344
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004345static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004347 int res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004348 int status = 0;
4349
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004350 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4351 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004352
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004353 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004354
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004355 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4356 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004357 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004358 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004360 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4361 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004362 str_supported(tp_features.bluetooth),
4363 status);
4364
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004365#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4366 if (dbg_bluetoothemul) {
4367 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004368 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004369 } else
4370#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004371 if (tp_features.bluetooth &&
4372 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4373 /* no bluetooth hardware present in system */
4374 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004375 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004376 "bluetooth hardware not installed\n");
4377 }
4378
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004379 if (!tp_features.bluetooth)
4380 return 1;
4381
Johannes Berg19d337d2009-06-02 13:01:37 +02004382 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4383 &bluetooth_tprfk_ops,
4384 RFKILL_TYPE_BLUETOOTH,
4385 TPACPI_RFK_BLUETOOTH_SW_NAME,
4386 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004387 if (res)
4388 return res;
4389
Johannes Berg19d337d2009-06-02 13:01:37 +02004390 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4391 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004392 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004393 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004394 return res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004395 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004396
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398}
4399
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004400/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004401static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004403 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404}
4405
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004406static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407{
Johannes Berg19d337d2009-06-02 13:01:37 +02004408 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409}
4410
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004411static struct ibm_struct bluetooth_driver_data = {
4412 .name = "bluetooth",
4413 .read = bluetooth_read,
4414 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004415 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004416 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004417};
4418
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004419/*************************************************************************
4420 * Wan subdriver
4421 */
4422
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004423enum {
4424 /* ACPI GWAN/SWAN bits */
4425 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4426 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004427 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004428 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004429};
4430
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004431#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4432
Johannes Berg19d337d2009-06-02 13:01:37 +02004433static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004434{
4435 int status;
4436
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004437#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4438 if (dbg_wwanemul)
4439 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004440 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004441#endif
4442
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004443 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4444 return -EIO;
4445
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004446 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004447 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004448}
4449
Johannes Berg19d337d2009-06-02 13:01:37 +02004450static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004451{
4452 int status;
4453
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004454 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004455 "will attempt to %s wwan\n",
4456 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004457
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004458#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4459 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004460 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004461 return 0;
4462 }
4463#endif
4464
Johannes Berg19d337d2009-06-02 13:01:37 +02004465 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004466 status = TP_ACPI_WANCARD_RADIOSSW
4467 | TP_ACPI_WANCARD_RESUMECTRL;
4468 else
4469 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004470
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004471 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4472 return -EIO;
4473
4474 return 0;
4475}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004476
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004477/* sysfs wan enable ---------------------------------------------------- */
4478static ssize_t wan_enable_show(struct device *dev,
4479 struct device_attribute *attr,
4480 char *buf)
4481{
Johannes Berg19d337d2009-06-02 13:01:37 +02004482 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4483 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004484}
4485
4486static ssize_t wan_enable_store(struct device *dev,
4487 struct device_attribute *attr,
4488 const char *buf, size_t count)
4489{
Johannes Berg19d337d2009-06-02 13:01:37 +02004490 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4491 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004492}
4493
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004494static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4495 wan_enable_show, wan_enable_store);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004496
4497/* --------------------------------------------------------------------- */
4498
4499static struct attribute *wan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004500 &dev_attr_wwan_enable.attr,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004501 NULL
4502};
4503
4504static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004505 .attrs = wan_attributes,
4506};
4507
Johannes Berg19d337d2009-06-02 13:01:37 +02004508static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4509 .get_status = wan_get_status,
4510 .set_status = wan_set_status,
4511};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004512
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004513static void wan_shutdown(void)
4514{
4515 /* Order firmware to save current state to NVRAM */
4516 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4517 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004518 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004519 else
4520 vdbg_printk(TPACPI_DBG_RFKILL,
4521 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004522}
4523
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004524static void wan_exit(void)
4525{
4526 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4527 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004528
4529 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4530
4531 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004532}
4533
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004534static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004535{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004536 int res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004537 int status = 0;
4538
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004539 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4540 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004541
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004542 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004543
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004544 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004545 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004546
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004547 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4548 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004549 str_supported(tp_features.wan),
4550 status);
4551
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004552#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4553 if (dbg_wwanemul) {
4554 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004555 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004556 } else
4557#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004558 if (tp_features.wan &&
4559 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4560 /* no wan hardware present in system */
4561 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004562 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004563 "wan hardware not installed\n");
4564 }
4565
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004566 if (!tp_features.wan)
4567 return 1;
4568
Johannes Berg19d337d2009-06-02 13:01:37 +02004569 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4570 &wan_tprfk_ops,
4571 RFKILL_TYPE_WWAN,
4572 TPACPI_RFK_WWAN_SW_NAME,
4573 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004574 if (res)
4575 return res;
4576
Johannes Berg19d337d2009-06-02 13:01:37 +02004577 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4578 &wan_attr_group);
4579
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004580 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004581 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004582 return res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004583 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004584
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004585 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004586}
4587
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004588/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004589static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004590{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004591 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004592}
4593
4594static int wan_write(char *buf)
4595{
Johannes Berg19d337d2009-06-02 13:01:37 +02004596 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004597}
4598
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004599static struct ibm_struct wan_driver_data = {
4600 .name = "wan",
4601 .read = wan_read,
4602 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004603 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004604 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004605};
4606
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004607/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004608 * UWB subdriver
4609 */
4610
4611enum {
4612 /* ACPI GUWB/SUWB bits */
4613 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4614 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4615};
4616
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004617#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4618
Johannes Berg19d337d2009-06-02 13:01:37 +02004619static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004620{
4621 int status;
4622
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004623#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4624 if (dbg_uwbemul)
4625 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004626 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004627#endif
4628
4629 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4630 return -EIO;
4631
4632 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004633 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004634}
4635
Johannes Berg19d337d2009-06-02 13:01:37 +02004636static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004637{
4638 int status;
4639
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004640 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004641 "will attempt to %s UWB\n",
4642 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004643
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004644#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4645 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004646 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004647 return 0;
4648 }
4649#endif
4650
Johannes Berg19d337d2009-06-02 13:01:37 +02004651 if (state == TPACPI_RFK_RADIO_ON)
4652 status = TP_ACPI_UWB_RADIOSSW;
4653 else
4654 status = 0;
4655
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004656 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4657 return -EIO;
4658
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004659 return 0;
4660}
4661
4662/* --------------------------------------------------------------------- */
4663
Johannes Berg19d337d2009-06-02 13:01:37 +02004664static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4665 .get_status = uwb_get_status,
4666 .set_status = uwb_set_status,
4667};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004668
4669static void uwb_exit(void)
4670{
Johannes Berg19d337d2009-06-02 13:01:37 +02004671 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004672}
4673
4674static int __init uwb_init(struct ibm_init_struct *iibm)
4675{
4676 int res;
4677 int status = 0;
4678
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004679 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4680 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004681
4682 TPACPI_ACPIHANDLE_INIT(hkey);
4683
4684 tp_features.uwb = hkey_handle &&
4685 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4686
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004687 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4688 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004689 str_supported(tp_features.uwb),
4690 status);
4691
4692#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4693 if (dbg_uwbemul) {
4694 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004695 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004696 } else
4697#endif
4698 if (tp_features.uwb &&
4699 !(status & TP_ACPI_UWB_HWPRESENT)) {
4700 /* no uwb hardware present in system */
4701 tp_features.uwb = 0;
4702 dbg_printk(TPACPI_DBG_INIT,
4703 "uwb hardware not installed\n");
4704 }
4705
4706 if (!tp_features.uwb)
4707 return 1;
4708
4709 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004710 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004711 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004712 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004713 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004714 return res;
4715}
4716
4717static struct ibm_struct uwb_driver_data = {
4718 .name = "uwb",
4719 .exit = uwb_exit,
4720 .flags.experimental = 1,
4721};
4722
4723/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004724 * Video subdriver
4725 */
4726
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004727#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4728
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004729enum video_access_mode {
4730 TPACPI_VIDEO_NONE = 0,
4731 TPACPI_VIDEO_570, /* 570 */
4732 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4733 TPACPI_VIDEO_NEW, /* all others */
4734};
4735
4736enum { /* video status flags, based on VIDEO_570 */
4737 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4738 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4739 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4740};
4741
4742enum { /* TPACPI_VIDEO_570 constants */
4743 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4744 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4745 * video_status_flags */
4746 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4747 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4748};
4749
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004750static enum video_access_mode video_supported;
4751static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004752
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004753static int video_autosw_get(void);
4754static int video_autosw_set(int enable);
4755
Joe Perches112a6ee2011-04-04 10:06:24 -07004756TPACPI_HANDLE(vid, root,
4757 "\\_SB.PCI.AGP.VGA", /* 570 */
4758 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4759 "\\_SB.PCI0.VID0", /* 770e */
4760 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4761 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4762 "\\_SB.PCI0.AGP.VID", /* all others */
4763 ); /* R30, R31 */
4764
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004765TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004766
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004767static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004769 int ivga;
4770
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004771 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4772
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004773 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004774 if (tpacpi_is_ibm())
4775 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004776
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004777 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4778 /* G41, assume IVGA doesn't change */
4779 vid_handle = vid2_handle;
4780
4781 if (!vid_handle)
4782 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004783 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004784 else if (tpacpi_is_ibm() &&
4785 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004786 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004787 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004788 else if (tpacpi_is_ibm() &&
4789 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004790 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004791 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004792 else
4793 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004794 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004796 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4797 str_supported(video_supported != TPACPI_VIDEO_NONE),
4798 video_supported);
4799
Jan van den Berg72a979f2014-09-17 00:01:08 +02004800 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801}
4802
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004803static void video_exit(void)
4804{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004805 dbg_printk(TPACPI_DBG_EXIT,
4806 "restoring original video autoswitch mode\n");
4807 if (video_autosw_set(video_orig_autosw))
Joe Perches0978e012011-04-04 10:06:25 -07004808 pr_err("error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004809 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004810}
4811
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004812static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813{
4814 int status = 0;
4815 int i;
4816
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004817 switch (video_supported) {
4818 case TPACPI_VIDEO_570:
4819 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4820 TP_ACPI_VIDEO_570_PHSCMD))
4821 return -EIO;
4822 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4823 break;
4824 case TPACPI_VIDEO_770:
4825 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4826 return -EIO;
4827 if (i)
4828 status |= TP_ACPI_VIDEO_S_LCD;
4829 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4830 return -EIO;
4831 if (i)
4832 status |= TP_ACPI_VIDEO_S_CRT;
4833 break;
4834 case TPACPI_VIDEO_NEW:
4835 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4836 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4837 return -EIO;
4838 if (i)
4839 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004841 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4842 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4843 return -EIO;
4844 if (i)
4845 status |= TP_ACPI_VIDEO_S_LCD;
4846 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4847 return -EIO;
4848 if (i)
4849 status |= TP_ACPI_VIDEO_S_DVI;
4850 break;
4851 default:
4852 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
4855 return status;
4856}
4857
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004858static int video_outputsw_set(int status)
4859{
4860 int autosw;
4861 int res = 0;
4862
4863 switch (video_supported) {
4864 case TPACPI_VIDEO_570:
4865 res = acpi_evalf(NULL, NULL,
4866 "\\_SB.PHS2", "vdd",
4867 TP_ACPI_VIDEO_570_PHS2CMD,
4868 status | TP_ACPI_VIDEO_570_PHS2SET);
4869 break;
4870 case TPACPI_VIDEO_770:
4871 autosw = video_autosw_get();
4872 if (autosw < 0)
4873 return autosw;
4874
4875 res = video_autosw_set(1);
4876 if (res)
4877 return res;
4878 res = acpi_evalf(vid_handle, NULL,
4879 "ASWT", "vdd", status * 0x100, 0);
4880 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004881 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004882 return -EIO;
4883 }
4884 break;
4885 case TPACPI_VIDEO_NEW:
4886 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004887 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004888 break;
4889 default:
4890 return -ENOSYS;
4891 }
4892
Jan van den Berg72a979f2014-09-17 00:01:08 +02004893 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004894}
4895
4896static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004898 int autosw = 0;
4899
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004900 switch (video_supported) {
4901 case TPACPI_VIDEO_570:
4902 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4903 return -EIO;
4904 break;
4905 case TPACPI_VIDEO_770:
4906 case TPACPI_VIDEO_NEW:
4907 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4908 return -EIO;
4909 break;
4910 default:
4911 return -ENOSYS;
4912 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004913
4914 return autosw & 1;
4915}
4916
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004917static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004918{
Jan van den Berg72a979f2014-09-17 00:01:08 +02004919 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004920 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004921 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004922}
4923
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004924static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004925{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004926 int autosw = video_autosw_get();
4927 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004928
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004929 if (autosw < 0)
4930 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004931
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004932 switch (video_supported) {
4933 case TPACPI_VIDEO_570:
4934 res = video_autosw_set(1);
4935 if (res)
4936 return res;
4937 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4938 break;
4939 case TPACPI_VIDEO_770:
4940 case TPACPI_VIDEO_NEW:
4941 res = video_autosw_set(1);
4942 if (res)
4943 return res;
4944 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4945 break;
4946 default:
4947 return -ENOSYS;
4948 }
4949 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004950 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004951 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004952 }
4953
Jan van den Berg72a979f2014-09-17 00:01:08 +02004954 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004955}
4956
4957static int video_expand_toggle(void)
4958{
4959 switch (video_supported) {
4960 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004961 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004962 0 : -EIO;
4963 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004964 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004965 0 : -EIO;
4966 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004967 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004968 0 : -EIO;
4969 default:
4970 return -ENOSYS;
4971 }
4972 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004973}
4974
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004975static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004976{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004977 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004978
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004979 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004980 seq_printf(m, "status:\t\tnot supported\n");
4981 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004982 }
4983
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004984 /* Even reads can crash X.org, so... */
4985 if (!capable(CAP_SYS_ADMIN))
4986 return -EPERM;
4987
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004988 status = video_outputsw_get();
4989 if (status < 0)
4990 return status;
4991
4992 autosw = video_autosw_get();
4993 if (autosw < 0)
4994 return autosw;
4995
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004996 seq_printf(m, "status:\t\tsupported\n");
4997 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4998 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004999 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005000 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5001 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5002 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5003 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005004 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005005 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5006 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5007 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005008
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005009 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005010}
5011
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005012static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013{
5014 char *cmd;
5015 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005016 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005018 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005019 return -ENODEV;
5020
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03005021 /* Even reads can crash X.org, let alone writes... */
5022 if (!capable(CAP_SYS_ADMIN))
5023 return -EPERM;
5024
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005025 enable = 0;
5026 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
5028 while ((cmd = next_cmd(&buf))) {
5029 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005030 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005032 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005034 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005036 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005037 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005038 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005039 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005040 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005041 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005042 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005044 res = video_autosw_set(1);
5045 if (res)
5046 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005048 res = video_autosw_set(0);
5049 if (res)
5050 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005052 res = video_outputsw_cycle();
5053 if (res)
5054 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005056 res = video_expand_toggle();
5057 if (res)
5058 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 } else
5060 return -EINVAL;
5061 }
5062
5063 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005064 status = video_outputsw_get();
5065 if (status < 0)
5066 return status;
5067 res = video_outputsw_set((status & ~disable) | enable);
5068 if (res)
5069 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 }
5071
5072 return 0;
5073}
5074
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005075static struct ibm_struct video_driver_data = {
5076 .name = "video",
5077 .read = video_read,
5078 .write = video_write,
5079 .exit = video_exit,
5080};
5081
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005082#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5083
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005084/*************************************************************************
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005085 * Keyboard backlight subdriver
5086 */
5087
Hans de Goedec685e202017-02-09 16:44:13 +01005088static enum led_brightness kbdlight_brightness;
5089static DEFINE_MUTEX(kbdlight_mutex);
5090
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005091static int kbdlight_set_level(int level)
5092{
Hans de Goedec685e202017-02-09 16:44:13 +01005093 int ret = 0;
5094
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005095 if (!hkey_handle)
5096 return -ENXIO;
5097
Hans de Goedec685e202017-02-09 16:44:13 +01005098 mutex_lock(&kbdlight_mutex);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005099
Hans de Goedec685e202017-02-09 16:44:13 +01005100 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5101 ret = -EIO;
5102 else
5103 kbdlight_brightness = level;
5104
5105 mutex_unlock(&kbdlight_mutex);
5106
5107 return ret;
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005108}
5109
5110static int kbdlight_get_level(void)
5111{
5112 int status = 0;
5113
5114 if (!hkey_handle)
5115 return -ENXIO;
5116
5117 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5118 return -EIO;
5119
5120 if (status < 0)
5121 return status;
5122
5123 return status & 0x3;
5124}
5125
5126static bool kbdlight_is_supported(void)
5127{
5128 int status = 0;
5129
5130 if (!hkey_handle)
5131 return false;
5132
5133 if (!acpi_has_method(hkey_handle, "MLCG")) {
5134 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5135 return false;
5136 }
5137
5138 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5139 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5140 return false;
5141 }
5142
5143 if (status < 0) {
5144 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5145 return false;
5146 }
5147
5148 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5149 /*
5150 * Guessed test for keyboard backlight:
5151 *
5152 * Machines with backlight keyboard return:
5153 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5154 * b110100010010000000XX - ThinkPad x230
5155 * b010100000010000000XX - ThinkPad x240
5156 * b010100000010000000XX - ThinkPad W541
5157 * (XX is current backlight level)
5158 *
5159 * Machines without backlight keyboard return:
5160 * b10100001000000000000 - ThinkPad x230
5161 * b10110001000000000000 - ThinkPad E430
5162 * b00000000000000000000 - ThinkPad E450
5163 *
5164 * Candidate BITs for detection test (XOR):
5165 * b01000000001000000000
5166 * ^
5167 */
5168 return status & BIT(9);
5169}
5170
Hans de Goede86ec0c22017-02-09 16:44:12 +01005171static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005172 enum led_brightness brightness)
5173{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005174 return kbdlight_set_level(brightness);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005175}
5176
5177static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5178{
5179 int level;
5180
5181 level = kbdlight_get_level();
5182 if (level < 0)
5183 return 0;
5184
5185 return level;
5186}
5187
5188static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5189 .led_classdev = {
5190 .name = "tpacpi::kbd_backlight",
5191 .max_brightness = 2,
Hans de Goedec685e202017-02-09 16:44:13 +01005192 .flags = LED_BRIGHT_HW_CHANGED,
Hans de Goede86ec0c22017-02-09 16:44:12 +01005193 .brightness_set_blocking = &kbdlight_sysfs_set,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005194 .brightness_get = &kbdlight_sysfs_get,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005195 }
5196};
5197
5198static int __init kbdlight_init(struct ibm_init_struct *iibm)
5199{
5200 int rc;
5201
5202 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5203
5204 TPACPI_ACPIHANDLE_INIT(hkey);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005205
5206 if (!kbdlight_is_supported()) {
5207 tp_features.kbdlight = 0;
5208 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5209 return 1;
5210 }
5211
Hans de Goedec685e202017-02-09 16:44:13 +01005212 kbdlight_brightness = kbdlight_sysfs_get(NULL);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005213 tp_features.kbdlight = 1;
5214
5215 rc = led_classdev_register(&tpacpi_pdev->dev,
5216 &tpacpi_led_kbdlight.led_classdev);
5217 if (rc < 0) {
5218 tp_features.kbdlight = 0;
5219 return rc;
5220 }
5221
Hans de Goedec685e202017-02-09 16:44:13 +01005222 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5223 TP_ACPI_HKEY_KBD_LIGHT_MASK);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005224 return 0;
5225}
5226
5227static void kbdlight_exit(void)
5228{
5229 if (tp_features.kbdlight)
5230 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005231}
5232
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005233static int kbdlight_set_level_and_update(int level)
5234{
5235 int ret;
5236 struct led_classdev *led_cdev;
5237
5238 ret = kbdlight_set_level(level);
5239 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5240
5241 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5242 led_cdev->brightness = level;
5243
5244 return ret;
5245}
5246
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005247static int kbdlight_read(struct seq_file *m)
5248{
5249 int level;
5250
5251 if (!tp_features.kbdlight) {
5252 seq_printf(m, "status:\t\tnot supported\n");
5253 } else {
5254 level = kbdlight_get_level();
5255 if (level < 0)
5256 seq_printf(m, "status:\t\terror %d\n", level);
5257 else
5258 seq_printf(m, "status:\t\t%d\n", level);
5259 seq_printf(m, "commands:\t0, 1, 2\n");
5260 }
5261
5262 return 0;
5263}
5264
5265static int kbdlight_write(char *buf)
5266{
5267 char *cmd;
5268 int level = -1;
5269
5270 if (!tp_features.kbdlight)
5271 return -ENODEV;
5272
5273 while ((cmd = next_cmd(&buf))) {
5274 if (strlencmp(cmd, "0") == 0)
5275 level = 0;
5276 else if (strlencmp(cmd, "1") == 0)
5277 level = 1;
5278 else if (strlencmp(cmd, "2") == 0)
5279 level = 2;
5280 else
5281 return -EINVAL;
5282 }
5283
5284 if (level == -1)
5285 return -EINVAL;
5286
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005287 return kbdlight_set_level_and_update(level);
5288}
5289
5290static void kbdlight_suspend(void)
5291{
5292 struct led_classdev *led_cdev;
5293
5294 if (!tp_features.kbdlight)
5295 return;
5296
5297 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5298 led_update_brightness(led_cdev);
5299 led_classdev_suspend(led_cdev);
5300}
5301
5302static void kbdlight_resume(void)
5303{
5304 if (!tp_features.kbdlight)
5305 return;
5306
5307 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005308}
5309
5310static struct ibm_struct kbdlight_driver_data = {
5311 .name = "kbdlight",
5312 .read = kbdlight_read,
5313 .write = kbdlight_write,
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005314 .suspend = kbdlight_suspend,
5315 .resume = kbdlight_resume,
Pali Rohárbb28f3d52015-12-30 23:27:41 +01005316 .exit = kbdlight_exit,
5317};
5318
5319/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005320 * Light (thinklight) subdriver
5321 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005323TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5324TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005325
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005326static int light_get_status(void)
5327{
5328 int status = 0;
5329
5330 if (tp_features.light_status) {
5331 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5332 return -EIO;
5333 return (!!status);
5334 }
5335
5336 return -ENXIO;
5337}
5338
5339static int light_set_status(int status)
5340{
5341 int rc;
5342
5343 if (tp_features.light) {
5344 if (cmos_handle) {
5345 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005346 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005347 TP_CMOS_THINKLIGHT_ON :
5348 TP_CMOS_THINKLIGHT_OFF);
5349 } else {
5350 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005351 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005352 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005353 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005354 }
5355
5356 return -ENXIO;
5357}
5358
Hans de Goede86ec0c22017-02-09 16:44:12 +01005359static int light_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005360 enum led_brightness brightness)
5361{
Hans de Goede86ec0c22017-02-09 16:44:12 +01005362 return light_set_status((brightness != LED_OFF) ?
5363 TPACPI_LED_ON : TPACPI_LED_OFF);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005364}
5365
5366static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5367{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005368 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005369}
5370
5371static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5372 .led_classdev = {
5373 .name = "tpacpi::thinklight",
Hans de Goede86ec0c22017-02-09 16:44:12 +01005374 .brightness_set_blocking = &light_sysfs_set,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005375 .brightness_get = &light_sysfs_get,
5376 }
5377};
5378
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005379static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005381 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005382
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005383 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5384
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005385 if (tpacpi_is_ibm()) {
5386 TPACPI_ACPIHANDLE_INIT(ledb);
5387 TPACPI_ACPIHANDLE_INIT(lght);
5388 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005389 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005390
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005391 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005392 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005393
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005394 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005395 /* light status not supported on
5396 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005397 tp_features.light_status =
5398 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005400 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5401 str_supported(tp_features.light),
5402 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005403
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005404 if (!tp_features.light)
5405 return 1;
5406
5407 rc = led_classdev_register(&tpacpi_pdev->dev,
5408 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005409
5410 if (rc < 0) {
5411 tp_features.light = 0;
5412 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005413 } else {
5414 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005415 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005416
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005417 return rc;
5418}
5419
5420static void light_exit(void)
5421{
5422 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423}
5424
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005425static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005427 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005429 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005430 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005431 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005432 seq_printf(m, "status:\t\tunknown\n");
5433 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005434 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005435 status = light_get_status();
5436 if (status < 0)
5437 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005438 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5439 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443}
5444
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005445static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005448 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005449
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005450 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005451 return -ENODEV;
5452
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 while ((cmd = next_cmd(&buf))) {
5454 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005455 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005457 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 } else
5459 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 }
5461
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005462 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463}
5464
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005465static struct ibm_struct light_driver_data = {
5466 .name = "light",
5467 .read = light_read,
5468 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005469 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005470};
5471
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005472/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005473 * CMOS subdriver
5474 */
5475
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005476/* sysfs cmos_command -------------------------------------------------- */
5477static ssize_t cmos_command_store(struct device *dev,
5478 struct device_attribute *attr,
5479 const char *buf, size_t count)
5480{
5481 unsigned long cmos_cmd;
5482 int res;
5483
5484 if (parse_strtoul(buf, 21, &cmos_cmd))
5485 return -EINVAL;
5486
5487 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005488 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005489}
5490
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01005491static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005492
5493/* --------------------------------------------------------------------- */
5494
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005495static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005496{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005497 int res;
5498
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005499 vdbg_printk(TPACPI_DBG_INIT,
5500 "initializing cmos commands subdriver\n");
5501
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005502 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005503
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005504 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5505 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005506
5507 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5508 if (res)
5509 return res;
5510
Jan van den Berg72a979f2014-09-17 00:01:08 +02005511 return (cmos_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005512}
5513
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005514static void cmos_exit(void)
5515{
5516 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5517}
5518
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005519static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005521 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5522 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005524 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005526 seq_printf(m, "status:\t\tsupported\n");
5527 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 }
5529
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531}
5532
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005533static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534{
5535 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005536 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
5538 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005539 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5540 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 /* cmos_cmd set */
5542 } else
5543 return -EINVAL;
5544
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005545 res = issue_thinkpad_cmos_command(cmos_cmd);
5546 if (res)
5547 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 }
5549
5550 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005551}
5552
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005553static struct ibm_struct cmos_driver_data = {
5554 .name = "cmos",
5555 .read = cmos_read,
5556 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005557 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005558};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005559
5560/*************************************************************************
5561 * LED subdriver
5562 */
5563
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005564enum led_access_mode {
5565 TPACPI_LED_NONE = 0,
5566 TPACPI_LED_570, /* 570 */
5567 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5568 TPACPI_LED_NEW, /* all others */
5569};
5570
5571enum { /* For TPACPI_LED_OLD */
5572 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5573 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5574 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5575};
5576
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005577static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005578
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005579static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005580
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005581#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005582static struct tpacpi_led_classdev *tpacpi_leds;
5583static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005584static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005585 /* there's a limit of 19 chars + NULL before 2.6.26 */
5586 "tpacpi::power",
5587 "tpacpi:orange:batt",
5588 "tpacpi:green:batt",
5589 "tpacpi::dock_active",
5590 "tpacpi::bay_active",
5591 "tpacpi::dock_batt",
5592 "tpacpi::unknown_led",
5593 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005594 "tpacpi::dock_status1",
5595 "tpacpi::dock_status2",
5596 "tpacpi::unknown_led2",
5597 "tpacpi::unknown_led3",
5598 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005599};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005600#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005601
5602static inline bool tpacpi_is_led_restricted(const unsigned int led)
5603{
5604#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5605 return false;
5606#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005607 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005608#endif
5609}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005610
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005611static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005612{
5613 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005614 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005615
5616 switch (led_supported) {
5617 case TPACPI_LED_570:
5618 if (!acpi_evalf(ec_handle,
5619 &status, "GLED", "dd", 1 << led))
5620 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005621 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005622 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005623 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005624 TPACPI_LED_ON :
5625 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005626 tpacpi_led_state_cache[led] = led_s;
5627 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005628 default:
5629 return -ENXIO;
5630 }
5631
5632 /* not reached */
5633}
5634
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005635static int led_set_status(const unsigned int led,
5636 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005637{
5638 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005639 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5640 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005641
5642 int rc = 0;
5643
5644 switch (led_supported) {
5645 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005646 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005647 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005648 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005649 if (unlikely(tpacpi_is_led_restricted(led)))
5650 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005651 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5652 (1 << led), led_sled_arg1[ledstatus]))
5653 rc = -EIO;
5654 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005655 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005656 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005657 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005658 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005659 if (unlikely(tpacpi_is_led_restricted(led)))
5660 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005661 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5662 if (rc >= 0)
5663 rc = ec_write(TPACPI_LED_EC_HLBL,
5664 (ledstatus == TPACPI_LED_BLINK) << led);
5665 if (rc >= 0)
5666 rc = ec_write(TPACPI_LED_EC_HLCL,
5667 (ledstatus != TPACPI_LED_OFF) << led);
5668 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005669 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005670 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005671 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5672 return -EINVAL;
5673 if (unlikely(tpacpi_is_led_restricted(led)))
5674 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005675 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5676 led, led_led_arg1[ledstatus]))
5677 rc = -EIO;
5678 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005679 default:
5680 rc = -ENXIO;
5681 }
5682
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005683 if (!rc)
5684 tpacpi_led_state_cache[led] = ledstatus;
5685
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005686 return rc;
5687}
5688
Hans de Goede86ec0c22017-02-09 16:44:12 +01005689static int led_sysfs_set(struct led_classdev *led_cdev,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005690 enum led_brightness brightness)
5691{
5692 struct tpacpi_led_classdev *data = container_of(led_cdev,
5693 struct tpacpi_led_classdev, led_classdev);
Hans de Goede86ec0c22017-02-09 16:44:12 +01005694 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005695
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005696 if (brightness == LED_OFF)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005697 new_state = TPACPI_LED_OFF;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005698 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
Hans de Goede86ec0c22017-02-09 16:44:12 +01005699 new_state = TPACPI_LED_ON;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005700 else
Hans de Goede86ec0c22017-02-09 16:44:12 +01005701 new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005702
Hans de Goede86ec0c22017-02-09 16:44:12 +01005703 return led_set_status(data->led, new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005704}
5705
5706static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5707 unsigned long *delay_on, unsigned long *delay_off)
5708{
5709 struct tpacpi_led_classdev *data = container_of(led_cdev,
5710 struct tpacpi_led_classdev, led_classdev);
5711
5712 /* Can we choose the flash rate? */
5713 if (*delay_on == 0 && *delay_off == 0) {
5714 /* yes. set them to the hardware blink rate (1 Hz) */
5715 *delay_on = 500; /* ms */
5716 *delay_off = 500; /* ms */
5717 } else if ((*delay_on != 500) || (*delay_off != 500))
5718 return -EINVAL;
5719
Hans de Goede86ec0c22017-02-09 16:44:12 +01005720 return led_set_status(data->led, TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005721}
5722
5723static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5724{
5725 int rc;
5726
5727 struct tpacpi_led_classdev *data = container_of(led_cdev,
5728 struct tpacpi_led_classdev, led_classdev);
5729
5730 rc = led_get_status(data->led);
5731
5732 if (rc == TPACPI_LED_OFF || rc < 0)
5733 rc = LED_OFF; /* no error handling in led class :( */
5734 else
5735 rc = LED_FULL;
5736
5737 return rc;
5738}
5739
5740static void led_exit(void)
5741{
5742 unsigned int i;
5743
5744 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5745 if (tpacpi_leds[i].led_classdev.name)
5746 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5747 }
5748
5749 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005750}
5751
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005752static int __init tpacpi_init_led(unsigned int led)
5753{
5754 int rc;
5755
5756 tpacpi_leds[led].led = led;
5757
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005758 /* LEDs with no name don't get registered */
5759 if (!tpacpi_led_names[led])
5760 return 0;
5761
Hans de Goede86ec0c22017-02-09 16:44:12 +01005762 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005763 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5764 if (led_supported == TPACPI_LED_570)
5765 tpacpi_leds[led].led_classdev.brightness_get =
5766 &led_sysfs_get;
5767
5768 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5769
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005770 rc = led_classdev_register(&tpacpi_pdev->dev,
5771 &tpacpi_leds[led].led_classdev);
5772 if (rc < 0)
5773 tpacpi_leds[led].led_classdev.name = NULL;
5774
5775 return rc;
5776}
5777
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005778static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5779 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5780 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5781 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5782
5783 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5784 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5785 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5786 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5787 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5788 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5789 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5790 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5791
5792 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5793 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5794 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5795 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5796 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5797
5798 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5799 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5800 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5801 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5802
5803 /* (1) - may have excess leds enabled on MSB */
5804
5805 /* Defaults (order matters, keep last, don't reorder!) */
5806 { /* Lenovo */
5807 .vendor = PCI_VENDOR_ID_LENOVO,
5808 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5809 .quirks = 0x1fffU,
5810 },
5811 { /* IBM ThinkPads with no EC version string */
5812 .vendor = PCI_VENDOR_ID_IBM,
5813 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5814 .quirks = 0x00ffU,
5815 },
5816 { /* IBM ThinkPads with EC version string */
5817 .vendor = PCI_VENDOR_ID_IBM,
5818 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5819 .quirks = 0x00bfU,
5820 },
5821};
5822
5823#undef TPACPI_LEDQ_IBM
5824#undef TPACPI_LEDQ_LNV
5825
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005826static enum led_access_mode __init led_init_detect_mode(void)
5827{
5828 acpi_status status;
5829
5830 if (tpacpi_is_ibm()) {
5831 /* 570 */
5832 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5833 if (ACPI_SUCCESS(status))
5834 return TPACPI_LED_570;
5835
5836 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5837 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5838 if (ACPI_SUCCESS(status))
5839 return TPACPI_LED_OLD;
5840 }
5841
5842 /* most others */
5843 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5844 if (ACPI_SUCCESS(status))
5845 return TPACPI_LED_NEW;
5846
5847 /* R30, R31, and unknown firmwares */
5848 led_handle = NULL;
5849 return TPACPI_LED_NONE;
5850}
5851
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005852static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005853{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005854 unsigned int i;
5855 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005856 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005857
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005858 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5859
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005860 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005861
Adam Leefcb44e12013-06-07 16:20:08 +08005862 if (led_supported != TPACPI_LED_NONE) {
5863 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5864 ARRAY_SIZE(led_useful_qtable));
5865
5866 if (!useful_leds) {
5867 led_handle = NULL;
5868 led_supported = TPACPI_LED_NONE;
5869 }
5870 }
5871
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005872 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5873 str_supported(led_supported), led_supported);
5874
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005875 if (led_supported == TPACPI_LED_NONE)
5876 return 1;
5877
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005878 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5879 GFP_KERNEL);
5880 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005881 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005882 return -ENOMEM;
5883 }
5884
5885 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08005886 tpacpi_leds[i].led = -1;
5887
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005888 if (!tpacpi_is_led_restricted(i) &&
5889 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005890 rc = tpacpi_init_led(i);
5891 if (rc < 0) {
5892 led_exit();
5893 return rc;
5894 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005895 }
5896 }
5897
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005898#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Joe Perches0978e012011-04-04 10:06:25 -07005899 pr_notice("warning: userspace override of important "
5900 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005901#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005902 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005903}
5904
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005905#define str_led_status(s) \
5906 ((s) == TPACPI_LED_OFF ? "off" : \
5907 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005908
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005909static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005911 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005912 seq_printf(m, "status:\t\tnot supported\n");
5913 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005914 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005915 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005916
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005917 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005918 /* 570 */
5919 int i, status;
5920 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005921 status = led_get_status(i);
5922 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005923 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005924 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005925 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005926 }
5927 }
5928
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005929 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005930 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005932 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933}
5934
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005935static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936{
5937 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005938 int led, rc;
5939 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005940
5941 if (!led_supported)
5942 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943
5944 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08005945 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 return -EINVAL;
5947
Adam Leeedf2d772013-06-08 16:51:15 +08005948 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5949 tpacpi_leds[led].led < 0)
5950 return -ENODEV;
5951
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005952 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005953 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005955 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005956 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005957 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005958 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005959 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005960 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005961
5962 rc = led_set_status(led, s);
5963 if (rc < 0)
5964 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 }
5966
5967 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005968}
5969
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005970static struct ibm_struct led_driver_data = {
5971 .name = "led",
5972 .read = led_read,
5973 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005974 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005975};
5976
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005977/*************************************************************************
5978 * Beep subdriver
5979 */
5980
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005981TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005982
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005983#define TPACPI_BEEP_Q1 0x0001
5984
5985static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5986 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5987 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5988};
5989
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005990static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005991{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005992 unsigned long quirks;
5993
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005994 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5995
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005996 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005997
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005998 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5999 str_supported(beep_handle != NULL));
6000
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006001 quirks = tpacpi_check_quirks(beep_quirk_table,
6002 ARRAY_SIZE(beep_quirk_table));
6003
6004 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6005
Jan van den Berg72a979f2014-09-17 00:01:08 +02006006 return (beep_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006007}
6008
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006009static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006011 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006012 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006013 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006014 seq_printf(m, "status:\t\tsupported\n");
6015 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019}
6020
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006021static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022{
6023 char *cmd;
6024 int beep_cmd;
6025
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006026 if (!beep_handle)
6027 return -ENODEV;
6028
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006030 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6031 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 /* beep_cmd set */
6033 } else
6034 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006035 if (tp_features.beep_needs_two_args) {
6036 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6037 beep_cmd, 0))
6038 return -EIO;
6039 } else {
6040 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6041 beep_cmd))
6042 return -EIO;
6043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 }
6045
6046 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006047}
6048
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006049static struct ibm_struct beep_driver_data = {
6050 .name = "beep",
6051 .read = beep_read,
6052 .write = beep_write,
6053};
6054
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006055/*************************************************************************
6056 * Thermal subdriver
6057 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006058
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006059enum thermal_access_mode {
6060 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6061 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6062 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6063 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6064 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6065};
6066
6067enum { /* TPACPI_THERMAL_TPEC_* */
6068 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6069 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6070 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006071
6072 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006073};
6074
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006075
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006076#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6077struct ibm_thermal_sensors_struct {
6078 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6079};
6080
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006081static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006082
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006083/* idx is zero-based */
6084static int thermal_get_sensor(int idx, s32 *value)
6085{
6086 int t;
6087 s8 tmp;
6088 char tmpi[5];
6089
6090 t = TP_EC_THERMAL_TMP0;
6091
6092 switch (thermal_read_mode) {
6093#if TPACPI_MAX_THERMAL_SENSORS >= 16
6094 case TPACPI_THERMAL_TPEC_16:
6095 if (idx >= 8 && idx <= 15) {
6096 t = TP_EC_THERMAL_TMP8;
6097 idx -= 8;
6098 }
6099 /* fallthrough */
6100#endif
6101 case TPACPI_THERMAL_TPEC_8:
6102 if (idx <= 7) {
6103 if (!acpi_ec_read(t + idx, &tmp))
6104 return -EIO;
6105 *value = tmp * 1000;
6106 return 0;
6107 }
6108 break;
6109
6110 case TPACPI_THERMAL_ACPI_UPDT:
6111 if (idx <= 7) {
6112 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6113 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6114 return -EIO;
6115 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6116 return -EIO;
6117 *value = (t - 2732) * 100;
6118 return 0;
6119 }
6120 break;
6121
6122 case TPACPI_THERMAL_ACPI_TMP07:
6123 if (idx <= 7) {
6124 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6125 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6126 return -EIO;
6127 if (t > 127 || t < -127)
6128 t = TP_EC_THERMAL_TMP_NA;
6129 *value = t * 1000;
6130 return 0;
6131 }
6132 break;
6133
6134 case TPACPI_THERMAL_NONE:
6135 default:
6136 return -ENOSYS;
6137 }
6138
6139 return -EINVAL;
6140}
6141
6142static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6143{
6144 int res, i;
6145 int n;
6146
6147 n = 8;
6148 i = 0;
6149
6150 if (!s)
6151 return -EINVAL;
6152
6153 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6154 n = 16;
6155
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006156 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006157 res = thermal_get_sensor(i, &s->temp[i]);
6158 if (res)
6159 return res;
6160 }
6161
6162 return n;
6163}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006164
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006165static void thermal_dump_all_sensors(void)
6166{
6167 int n, i;
6168 struct ibm_thermal_sensors_struct t;
6169
6170 n = thermal_get_sensors(&t);
6171 if (n <= 0)
6172 return;
6173
Joe Perches0978e012011-04-04 10:06:25 -07006174 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006175
6176 for (i = 0; i < n; i++) {
6177 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07006178 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006179 else
Joe Perches0978e012011-04-04 10:06:25 -07006180 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006181 }
6182
Joe Perches0978e012011-04-04 10:06:25 -07006183 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006184}
6185
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006186/* sysfs temp##_input -------------------------------------------------- */
6187
6188static ssize_t thermal_temp_input_show(struct device *dev,
6189 struct device_attribute *attr,
6190 char *buf)
6191{
6192 struct sensor_device_attribute *sensor_attr =
6193 to_sensor_dev_attr(attr);
6194 int idx = sensor_attr->index;
6195 s32 value;
6196 int res;
6197
6198 res = thermal_get_sensor(idx, &value);
6199 if (res)
6200 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006201 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006202 return -ENXIO;
6203
6204 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6205}
6206
6207#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006208 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6209 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006210
6211static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6212 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6213 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6214 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6215 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6216 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6217 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6218 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6219 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6220 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6221 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6222 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6223 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6224 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6225 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6226 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6227 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6228};
6229
6230#define THERMAL_ATTRS(X) \
6231 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6232
6233static struct attribute *thermal_temp_input_attr[] = {
6234 THERMAL_ATTRS(8),
6235 THERMAL_ATTRS(9),
6236 THERMAL_ATTRS(10),
6237 THERMAL_ATTRS(11),
6238 THERMAL_ATTRS(12),
6239 THERMAL_ATTRS(13),
6240 THERMAL_ATTRS(14),
6241 THERMAL_ATTRS(15),
6242 THERMAL_ATTRS(0),
6243 THERMAL_ATTRS(1),
6244 THERMAL_ATTRS(2),
6245 THERMAL_ATTRS(3),
6246 THERMAL_ATTRS(4),
6247 THERMAL_ATTRS(5),
6248 THERMAL_ATTRS(6),
6249 THERMAL_ATTRS(7),
6250 NULL
6251};
6252
6253static const struct attribute_group thermal_temp_input16_group = {
6254 .attrs = thermal_temp_input_attr
6255};
6256
6257static const struct attribute_group thermal_temp_input8_group = {
6258 .attrs = &thermal_temp_input_attr[8]
6259};
6260
6261#undef THERMAL_SENSOR_ATTR_TEMP
6262#undef THERMAL_ATTRS
6263
6264/* --------------------------------------------------------------------- */
6265
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006266static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006267{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006268 u8 t, ta1, ta2;
6269 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006270 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006271 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006272
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006273 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6274
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006275 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006276
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03006277 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006278 /*
6279 * Direct EC access mode: sensors at registers
6280 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6281 * non-implemented, thermal sensors return 0x80 when
6282 * not available
6283 */
6284
6285 ta1 = ta2 = 0;
6286 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006287 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006288 ta1 |= t;
6289 } else {
6290 ta1 = 0;
6291 break;
6292 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006293 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006294 ta2 |= t;
6295 } else {
6296 ta1 = 0;
6297 break;
6298 }
6299 }
6300 if (ta1 == 0) {
6301 /* This is sheer paranoia, but we handle it anyway */
6302 if (acpi_tmp7) {
Joe Perches0978e012011-04-04 10:06:25 -07006303 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006304 "falling back to ACPI TMPx access "
6305 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006306 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006307 } else {
Joe Perches0978e012011-04-04 10:06:25 -07006308 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006309 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006310 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006311 }
6312 } else {
6313 thermal_read_mode =
6314 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006315 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006316 }
6317 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006318 if (tpacpi_is_ibm() &&
6319 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006320 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006321 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006322 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006323 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006324 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006325 }
6326 } else {
6327 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006328 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006329 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006330
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006331 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6332 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6333 thermal_read_mode);
6334
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006335 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006336 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006337 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006338 &thermal_temp_input16_group);
6339 if (res)
6340 return res;
6341 break;
6342 case TPACPI_THERMAL_TPEC_8:
6343 case TPACPI_THERMAL_ACPI_TMP07:
6344 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006345 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006346 &thermal_temp_input8_group);
6347 if (res)
6348 return res;
6349 break;
6350 case TPACPI_THERMAL_NONE:
6351 default:
6352 return 1;
6353 }
6354
6355 return 0;
6356}
6357
6358static void thermal_exit(void)
6359{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006360 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006361 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006362 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006363 &thermal_temp_input16_group);
6364 break;
6365 case TPACPI_THERMAL_TPEC_8:
6366 case TPACPI_THERMAL_ACPI_TMP07:
6367 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006368 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08006369 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006370 break;
6371 case TPACPI_THERMAL_NONE:
6372 default:
6373 break;
6374 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006375}
6376
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006377static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006378{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006379 int n, i;
6380 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006381
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006382 n = thermal_get_sensors(&t);
6383 if (unlikely(n < 0))
6384 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006385
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006386 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006387
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006388 if (n > 0) {
6389 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006390 seq_printf(m, "%d ", t.temp[i] / 1000);
6391 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006392 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006393 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006394
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006395 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006396}
6397
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006398static struct ibm_struct thermal_driver_data = {
6399 .name = "thermal",
6400 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006401 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006402};
6403
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006404/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006405 * Backlight/brightness subdriver
6406 */
Holger Macht8acb0252006-10-20 14:30:28 -07006407
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006408#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6409
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006410/*
6411 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6412 * CMOS NVRAM byte 0x5E, bits 0-3.
6413 *
6414 * EC HBRV (0x31) has the following layout
6415 * Bit 7: unknown function
6416 * Bit 6: unknown function
6417 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6418 * Bit 4: must be set to zero to avoid problems
6419 * Bit 3-0: backlight brightness level
6420 *
6421 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006422 *
6423 * WARNING: The X61 has been verified to use HBRV for something else, so
6424 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6425 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006426 */
6427
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006428enum {
6429 TP_EC_BACKLIGHT = 0x31,
6430
6431 /* TP_EC_BACKLIGHT bitmasks */
6432 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6433 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6434 TP_EC_BACKLIGHT_MAPSW = 0x20,
6435};
6436
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006437enum tpacpi_brightness_access_mode {
6438 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6439 TPACPI_BRGHT_MODE_EC, /* EC control */
6440 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6441 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6442 TPACPI_BRGHT_MODE_MAX
6443};
6444
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006445static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006446
6447static enum tpacpi_brightness_access_mode brightness_mode =
6448 TPACPI_BRGHT_MODE_MAX;
6449
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006450static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6451
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006452static struct mutex brightness_mutex;
6453
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006454/* NVRAM brightness access,
6455 * call with brightness_mutex held! */
6456static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006457{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006458 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006459
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006460 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6461 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6462 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006463 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006464
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006465 return lnvram;
6466}
6467
6468static void tpacpi_brightness_checkpoint_nvram(void)
6469{
6470 u8 lec = 0;
6471 u8 b_nvram;
6472
6473 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6474 return;
6475
6476 vdbg_printk(TPACPI_DBG_BRGHT,
6477 "trying to checkpoint backlight level to NVRAM...\n");
6478
6479 if (mutex_lock_killable(&brightness_mutex) < 0)
6480 return;
6481
6482 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6483 goto unlock;
6484 lec &= TP_EC_BACKLIGHT_LVLMSK;
6485 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6486
6487 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6488 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6489 /* NVRAM needs update */
6490 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6491 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6492 b_nvram |= lec;
6493 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6494 dbg_printk(TPACPI_DBG_BRGHT,
6495 "updated NVRAM backlight level to %u (0x%02x)\n",
6496 (unsigned int) lec, (unsigned int) b_nvram);
6497 } else
6498 vdbg_printk(TPACPI_DBG_BRGHT,
6499 "NVRAM backlight level already is %u (0x%02x)\n",
6500 (unsigned int) lec, (unsigned int) b_nvram);
6501
6502unlock:
6503 mutex_unlock(&brightness_mutex);
6504}
6505
6506
6507/* call with brightness_mutex held! */
6508static int tpacpi_brightness_get_raw(int *status)
6509{
6510 u8 lec = 0;
6511
6512 switch (brightness_mode) {
6513 case TPACPI_BRGHT_MODE_UCMS_STEP:
6514 *status = tpacpi_brightness_nvram_get();
6515 return 0;
6516 case TPACPI_BRGHT_MODE_EC:
6517 case TPACPI_BRGHT_MODE_ECNVRAM:
6518 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006519 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006520 *status = lec;
6521 return 0;
6522 default:
6523 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006524 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006525}
6526
6527/* call with brightness_mutex held! */
6528/* do NOT call with illegal backlight level value */
6529static int tpacpi_brightness_set_ec(unsigned int value)
6530{
6531 u8 lec = 0;
6532
6533 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6534 return -EIO;
6535
6536 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6537 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6538 (value & TP_EC_BACKLIGHT_LVLMSK))))
6539 return -EIO;
6540
6541 return 0;
6542}
6543
6544/* call with brightness_mutex held! */
6545static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6546{
6547 int cmos_cmd, inc;
6548 unsigned int current_value, i;
6549
6550 current_value = tpacpi_brightness_nvram_get();
6551
6552 if (value == current_value)
6553 return 0;
6554
6555 cmos_cmd = (value > current_value) ?
6556 TP_CMOS_BRIGHTNESS_UP :
6557 TP_CMOS_BRIGHTNESS_DOWN;
6558 inc = (value > current_value) ? 1 : -1;
6559
6560 for (i = current_value; i != value; i += inc)
6561 if (issue_thinkpad_cmos_command(cmos_cmd))
6562 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006563
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006564 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006565}
6566
6567/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006568static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006569{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006570 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006571
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006572 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006573 return -EINVAL;
6574
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006575 vdbg_printk(TPACPI_DBG_BRGHT,
6576 "set backlight level to %d\n", value);
6577
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006578 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006579 if (res < 0)
6580 return res;
6581
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006582 switch (brightness_mode) {
6583 case TPACPI_BRGHT_MODE_EC:
6584 case TPACPI_BRGHT_MODE_ECNVRAM:
6585 res = tpacpi_brightness_set_ec(value);
6586 break;
6587 case TPACPI_BRGHT_MODE_UCMS_STEP:
6588 res = tpacpi_brightness_set_ucmsstep(value);
6589 break;
6590 default:
6591 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006592 }
6593
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006594 mutex_unlock(&brightness_mutex);
6595 return res;
6596}
6597
6598/* sysfs backlight class ----------------------------------------------- */
6599
6600static int brightness_update_status(struct backlight_device *bd)
6601{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006602 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006603 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6604 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006605 bd->props.brightness : 0;
6606
6607 dbg_printk(TPACPI_DBG_BRGHT,
6608 "backlight: attempt to set level to %d\n",
6609 level);
6610
6611 /* it is the backlight class's job (caller) to handle
6612 * EINTR and other errors properly */
6613 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006614}
Holger Macht8acb0252006-10-20 14:30:28 -07006615
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006616static int brightness_get(struct backlight_device *bd)
6617{
6618 int status, res;
6619
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006620 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006621 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006622 return 0;
6623
6624 res = tpacpi_brightness_get_raw(&status);
6625
6626 mutex_unlock(&brightness_mutex);
6627
6628 if (res < 0)
6629 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006630
6631 return status & TP_EC_BACKLIGHT_LVLMSK;
6632}
6633
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006634static void tpacpi_brightness_notify_change(void)
6635{
6636 backlight_force_update(ibm_backlight_device,
6637 BACKLIGHT_UPDATE_HOTKEY);
6638}
6639
Lionel Debrouxacc24722010-11-16 14:14:02 +01006640static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006641 .get_brightness = brightness_get,
6642 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006643};
6644
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006645/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006646
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006647/*
6648 * Call _BCL method of video device. On some ThinkPads this will
6649 * switch the firmware to the ACPI brightness control mode.
6650 */
6651
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006652static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6653{
6654 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6655 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006656 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006657 int rc;
6658
Aaron Lu46445b62013-10-11 21:27:46 +08006659 if (acpi_bus_get_device(handle, &device))
6660 return 0;
6661
6662 rc = 0;
6663 list_for_each_entry(child, &device->children, node) {
6664 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6665 NULL, &buffer);
6666 if (ACPI_FAILURE(status))
6667 continue;
6668
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006669 obj = (union acpi_object *)buffer.pointer;
6670 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006671 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006672 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006673 rc = 0;
6674 } else {
6675 rc = obj->package.count;
6676 }
Aaron Lu46445b62013-10-11 21:27:46 +08006677 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006678 }
6679
6680 kfree(buffer.pointer);
6681 return rc;
6682}
6683
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006684
6685/*
6686 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6687 */
6688static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6689{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006690 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006691 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006692
Aaron Lu46445b62013-10-11 21:27:46 +08006693 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006694 if (video_device)
6695 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006696
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006697 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006698
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006699 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006700}
6701
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006702/*
6703 * These are only useful for models that have only one possibility
6704 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6705 * these quirks.
6706 */
6707#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6708#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6709#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6710
6711static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6712 /* Models with ATI GPUs known to require ECNVRAM mode */
6713 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6714
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006715 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006716 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006717 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006718 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006719 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6720
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006721 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006722 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006723 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6724 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006725
6726 /* Models with Intel GMA900 */
6727 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6728 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6729 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6730};
6731
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006732/*
6733 * Returns < 0 for error, otherwise sets tp_features.bright_*
6734 * and bright_maxlvl.
6735 */
6736static void __init tpacpi_detect_brightness_capabilities(void)
6737{
6738 unsigned int b;
6739
6740 vdbg_printk(TPACPI_DBG_INIT,
6741 "detecting firmware brightness interface capabilities\n");
6742
6743 /* we could run a quirks check here (same table used by
6744 * brightness_init) if needed */
6745
6746 /*
6747 * We always attempt to detect acpi support, so as to switch
6748 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6749 * going to publish a backlight interface
6750 */
6751 b = tpacpi_check_std_acpi_brightness_support();
6752 switch (b) {
6753 case 16:
6754 bright_maxlvl = 15;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006755 break;
6756 case 8:
6757 case 0:
6758 bright_maxlvl = 7;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006759 break;
6760 default:
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006761 tp_features.bright_unkfw = 1;
6762 bright_maxlvl = b - 1;
6763 }
Eric Curtin15c75622016-01-30 16:55:59 +00006764 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006765}
6766
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006767static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006768{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006769 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006770 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006771 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006772
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006773 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6774
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006775 mutex_init(&brightness_mutex);
6776
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006777 quirks = tpacpi_check_quirks(brightness_quirk_table,
6778 ARRAY_SIZE(brightness_quirk_table));
6779
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006780 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006781
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006782 /* if it is unknown, we don't handle it: it wouldn't be safe */
6783 if (tp_features.bright_unkfw)
6784 return 1;
6785
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006786 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006787 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006788 "brightness support disabled by "
6789 "module parameter\n");
6790 return 1;
6791 }
6792
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02006793 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006794 if (brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006795 pr_info("Standard ACPI backlight interface "
6796 "available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006797 return 1;
6798 } else if (brightness_enable == 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006799 pr_warn("Cannot enable backlight brightness support, "
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006800 "ACPI is already handling it. Refer to the "
Joe Perches0978e012011-04-04 10:06:25 -07006801 "acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006802 return 1;
6803 }
6804 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006805 pr_notice("Standard ACPI backlight interface not "
6806 "available, thinkpad_acpi native "
6807 "brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006808 }
6809
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006810 /*
6811 * Check for module parameter bogosity, note that we
6812 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6813 * able to detect "unspecified"
6814 */
6815 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006816 return -EINVAL;
6817
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006818 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6819 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6820 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006821 if (quirks & TPACPI_BRGHT_Q_EC)
6822 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6823 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006824 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6825
6826 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006827 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006828 brightness_mode);
6829 }
6830
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006831 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006832 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006833 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6834 brightness_mode == TPACPI_BRGHT_MODE_EC))
6835 return -EINVAL;
6836
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006837 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006838 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006839
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006840 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006841 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006842 props.max_brightness = bright_maxlvl;
6843 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006844 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6845 NULL, NULL,
6846 &ibm_backlight_data,
6847 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006848 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006849 int rc = PTR_ERR(ibm_backlight_device);
6850 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006851 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006852 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006853 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006854 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6855 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006856
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006857 if (quirks & TPACPI_BRGHT_Q_ASK) {
Joe Perches0978e012011-04-04 10:06:25 -07006858 pr_notice("brightness: will use unverified default: "
6859 "brightness_mode=%d\n", brightness_mode);
6860 pr_notice("brightness: please report to %s whether it works well "
6861 "or not on your ThinkPad\n", TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006862 }
6863
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006864 /* Added by mistake in early 2007. Probably useless, but it could
6865 * be working around some unknown firmware problem where the value
6866 * read at startup doesn't match the real hardware state... so leave
6867 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006868 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006869
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006870 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6871 "brightness: registering brightness hotkeys "
6872 "as change notification\n");
6873 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6874 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006875 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006876 return 0;
6877}
6878
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006879static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006880{
6881 tpacpi_brightness_checkpoint_nvram();
6882}
6883
6884static void brightness_shutdown(void)
6885{
6886 tpacpi_brightness_checkpoint_nvram();
6887}
6888
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006889static void brightness_exit(void)
6890{
6891 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006892 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006893 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006894 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006895 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006896
6897 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006898}
6899
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006900static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006901{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006902 int level;
6903
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006904 level = brightness_get(NULL);
6905 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006906 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006907 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006908 seq_printf(m, "level:\t\t%d\n", level);
6909 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006910 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6911 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006912 }
6913
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006914 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006915}
6916
6917static int brightness_write(char *buf)
6918{
6919 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006920 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006921 char *cmd;
6922
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006923 level = brightness_get(NULL);
6924 if (level < 0)
6925 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006926
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006927 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006928 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006929 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006930 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006931 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006932 if (level > 0)
6933 level--;
6934 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006935 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006936 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006937 } else
6938 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006939 }
6940
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006941 tpacpi_disclose_usertask("procfs brightness",
6942 "set level to %d\n", level);
6943
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006944 /*
6945 * Now we know what the final level should be, so we try to set it.
6946 * Doing it this way makes the syscall restartable in case of EINTR
6947 */
6948 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006949 if (!rc && ibm_backlight_device)
6950 backlight_force_update(ibm_backlight_device,
6951 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02006952 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006953}
6954
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006955static struct ibm_struct brightness_driver_data = {
6956 .name = "brightness",
6957 .read = brightness_read,
6958 .write = brightness_write,
6959 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006960 .suspend = brightness_suspend,
6961 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006962};
6963
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006964/*************************************************************************
6965 * Volume subdriver
6966 */
6967
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006968/*
6969 * IBM ThinkPads have a simple volume controller with MUTE gating.
6970 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6971 *
6972 * Since the *61 series (and probably also the later *60 series), Lenovo
6973 * ThinkPads only implement the MUTE gate.
6974 *
6975 * EC register 0x30
6976 * Bit 6: MUTE (1 mutes sound)
6977 * Bit 3-0: Volume
6978 * Other bits should be zero as far as we know.
6979 *
6980 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6981 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6982 * such as bit 7 which is used to detect repeated presses of MUTE,
6983 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07006984 *
6985 * On newer Lenovo ThinkPads, the EC can automatically change the volume
6986 * in response to user input. Unfortunately, this rarely works well.
6987 * The laptop changes the state of its internal MUTE gate and, on some
6988 * models, sends KEY_MUTE, causing any user code that responds to the
6989 * mute button to get confused. The hardware MUTE gate is also
6990 * unnecessary, since user code can handle the mute button without
6991 * kernel or EC help.
6992 *
6993 * To avoid confusing userspace, we simply disable all EC-based mute
6994 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006995 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006996
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006997#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6998
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006999#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7000#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7001#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7002
Takashi Iwaicab66612013-10-24 16:06:32 +02007003#if SNDRV_CARDS <= 32
7004#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7005#else
7006#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7007#endif
7008static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007009static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307010static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007011
7012struct tpacpi_alsa_data {
7013 struct snd_card *card;
7014 struct snd_ctl_elem_id *ctl_mute_id;
7015 struct snd_ctl_elem_id *ctl_vol_id;
7016};
7017
7018static struct snd_card *alsa_card;
7019
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007020enum {
7021 TP_EC_AUDIO = 0x30,
7022
7023 /* TP_EC_AUDIO bits */
7024 TP_EC_AUDIO_MUTESW = 6,
7025
7026 /* TP_EC_AUDIO bitmasks */
7027 TP_EC_AUDIO_LVL_MSK = 0x0F,
7028 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7029
7030 /* Maximum volume */
7031 TP_EC_VOLUME_MAX = 14,
7032};
7033
7034enum tpacpi_volume_access_mode {
7035 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7036 TPACPI_VOL_MODE_EC, /* Pure EC control */
7037 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7038 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7039 TPACPI_VOL_MODE_MAX
7040};
7041
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007042enum tpacpi_volume_capabilities {
7043 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7044 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7045 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7046 TPACPI_VOL_CAP_MAX
7047};
7048
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007049enum tpacpi_mute_btn_mode {
7050 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7051 /* We don't know what mode 1 is. */
7052 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7053 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7054};
7055
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007056static enum tpacpi_volume_access_mode volume_mode =
7057 TPACPI_VOL_MODE_MAX;
7058
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007059static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307060static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007061static bool software_mute_requested = true;
7062static bool software_mute_active;
7063static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007064
7065/*
7066 * Used to syncronize writers to TP_EC_AUDIO and
7067 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7068 */
7069static struct mutex volume_mutex;
7070
7071static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007072{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007073 u8 lec = 0;
7074 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007075 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007076
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007077 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7078 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007079 if (!volume_control_allowed)
7080 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007081 if (software_mute_active)
7082 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007083
7084 vdbg_printk(TPACPI_DBG_MIXER,
7085 "trying to checkpoint mixer state to NVRAM...\n");
7086
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007087 if (tp_features.mixer_no_level_control)
7088 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7089 else
7090 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7091
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007092 if (mutex_lock_killable(&volume_mutex) < 0)
7093 return;
7094
7095 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7096 goto unlock;
7097 lec &= ec_mask;
7098 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7099
7100 if (lec != (b_nvram & ec_mask)) {
7101 /* NVRAM needs update */
7102 b_nvram &= ~ec_mask;
7103 b_nvram |= lec;
7104 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7105 dbg_printk(TPACPI_DBG_MIXER,
7106 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7107 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007108 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007109 vdbg_printk(TPACPI_DBG_MIXER,
7110 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7111 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007112 }
7113
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007114unlock:
7115 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007116}
7117
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007118static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007119{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007120 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007121
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007122 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7123 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007124
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007125 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007126
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007127 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007128
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007129 return 0;
7130}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007131
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007132static int volume_get_status(u8 *status)
7133{
7134 return volume_get_status_ec(status);
7135}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007136
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007137static int volume_set_status_ec(const u8 status)
7138{
7139 if (!acpi_ec_write(TP_EC_AUDIO, status))
7140 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007141
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007142 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7143
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007144 /*
7145 * On X200s, and possibly on others, it can take a while for
7146 * reads to become correct.
7147 */
7148 msleep(1);
7149
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007150 return 0;
7151}
7152
7153static int volume_set_status(const u8 status)
7154{
7155 return volume_set_status_ec(status);
7156}
7157
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007158/* returns < 0 on error, 0 on no change, 1 on change */
7159static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007160{
7161 int rc;
7162 u8 s, n;
7163
7164 if (mutex_lock_killable(&volume_mutex) < 0)
7165 return -EINTR;
7166
7167 rc = volume_get_status_ec(&s);
7168 if (rc)
7169 goto unlock;
7170
7171 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7172 s & ~TP_EC_AUDIO_MUTESW_MSK;
7173
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007174 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007175 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007176 if (!rc)
7177 rc = 1;
7178 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007179
7180unlock:
7181 mutex_unlock(&volume_mutex);
7182 return rc;
7183}
7184
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007185static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007186{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007187 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007188 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007189 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007190}
7191
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007192static int volume_set_mute(const bool mute)
7193{
7194 int rc;
7195
7196 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7197 (mute) ? "" : "un");
7198
7199 rc = __volume_set_mute_ec(mute);
7200 return (rc < 0) ? rc : 0;
7201}
7202
7203/* returns < 0 on error, 0 on no change, 1 on change */
7204static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007205{
7206 int rc;
7207 u8 s, n;
7208
7209 if (vol > TP_EC_VOLUME_MAX)
7210 return -EINVAL;
7211
7212 if (mutex_lock_killable(&volume_mutex) < 0)
7213 return -EINTR;
7214
7215 rc = volume_get_status_ec(&s);
7216 if (rc)
7217 goto unlock;
7218
7219 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7220
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007221 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007222 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007223 if (!rc)
7224 rc = 1;
7225 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007226
7227unlock:
7228 mutex_unlock(&volume_mutex);
7229 return rc;
7230}
7231
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007232static int volume_set_software_mute(bool startup)
7233{
7234 int result;
7235
7236 if (!tpacpi_is_lenovo())
7237 return -ENODEV;
7238
7239 if (startup) {
7240 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7241 "HAUM", "qd"))
7242 return -EIO;
7243
7244 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7245 "Initial HAUM setting was %d\n",
7246 software_mute_orig_mode);
7247 }
7248
7249 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7250 (int)TP_EC_MUTE_BTN_NONE))
7251 return -EIO;
7252
7253 if (result != TP_EC_MUTE_BTN_NONE)
7254 pr_warn("Unexpected SAUM result %d\n",
7255 result);
7256
7257 /*
7258 * In software mute mode, the standard codec controls take
7259 * precendence, so we unmute the ThinkPad HW switch at
7260 * startup. Just on case there are SAUM-capable ThinkPads
7261 * with level controls, set max HW volume as well.
7262 */
7263 if (tp_features.mixer_no_level_control)
7264 result = volume_set_mute(false);
7265 else
7266 result = volume_set_status(TP_EC_VOLUME_MAX);
7267
7268 if (result != 0)
7269 pr_warn("Failed to unmute the HW mute switch\n");
7270
7271 return 0;
7272}
7273
7274static void volume_exit_software_mute(void)
7275{
7276 int r;
7277
7278 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7279 || r != software_mute_orig_mode)
7280 pr_warn("Failed to restore mute mode\n");
7281}
7282
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007283static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007284{
7285 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007286 "ALSA: trying to set volume level to %hu\n", vol);
7287 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007288}
7289
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007290static void volume_alsa_notify_change(void)
7291{
7292 struct tpacpi_alsa_data *d;
7293
7294 if (alsa_card && alsa_card->private_data) {
7295 d = alsa_card->private_data;
7296 if (d->ctl_mute_id)
7297 snd_ctl_notify(alsa_card,
7298 SNDRV_CTL_EVENT_MASK_VALUE,
7299 d->ctl_mute_id);
7300 if (d->ctl_vol_id)
7301 snd_ctl_notify(alsa_card,
7302 SNDRV_CTL_EVENT_MASK_VALUE,
7303 d->ctl_vol_id);
7304 }
7305}
7306
7307static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7308 struct snd_ctl_elem_info *uinfo)
7309{
7310 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7311 uinfo->count = 1;
7312 uinfo->value.integer.min = 0;
7313 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7314 return 0;
7315}
7316
7317static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7318 struct snd_ctl_elem_value *ucontrol)
7319{
7320 u8 s;
7321 int rc;
7322
7323 rc = volume_get_status(&s);
7324 if (rc < 0)
7325 return rc;
7326
7327 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7328 return 0;
7329}
7330
7331static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7332 struct snd_ctl_elem_value *ucontrol)
7333{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007334 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7335 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007336 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007337}
7338
7339#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7340
7341static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7342 struct snd_ctl_elem_value *ucontrol)
7343{
7344 u8 s;
7345 int rc;
7346
7347 rc = volume_get_status(&s);
7348 if (rc < 0)
7349 return rc;
7350
7351 ucontrol->value.integer.value[0] =
7352 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7353 return 0;
7354}
7355
7356static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7357 struct snd_ctl_elem_value *ucontrol)
7358{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007359 tpacpi_disclose_usertask("ALSA", "%smute\n",
7360 ucontrol->value.integer.value[0] ?
7361 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007362 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007363}
7364
Mathias Krausec3aa4722014-07-16 19:43:17 +02007365static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007366 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7367 .name = "Console Playback Volume",
7368 .index = 0,
7369 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7370 .info = volume_alsa_vol_info,
7371 .get = volume_alsa_vol_get,
7372};
7373
Mathias Krausec3aa4722014-07-16 19:43:17 +02007374static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007375 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7376 .name = "Console Playback Switch",
7377 .index = 0,
7378 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7379 .info = volume_alsa_mute_info,
7380 .get = volume_alsa_mute_get,
7381};
7382
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007383static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007384{
7385 tpacpi_volume_checkpoint_nvram();
7386}
7387
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007388static void volume_resume(void)
7389{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007390 if (software_mute_active) {
7391 if (volume_set_software_mute(false) < 0)
7392 pr_warn("Failed to restore software mute\n");
7393 } else {
7394 volume_alsa_notify_change();
7395 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007396}
7397
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007398static void volume_shutdown(void)
7399{
7400 tpacpi_volume_checkpoint_nvram();
7401}
7402
7403static void volume_exit(void)
7404{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007405 if (alsa_card) {
7406 snd_card_free(alsa_card);
7407 alsa_card = NULL;
7408 }
7409
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007410 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007411
7412 if (software_mute_active)
7413 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007414}
7415
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007416static int __init volume_create_alsa_mixer(void)
7417{
7418 struct snd_card *card;
7419 struct tpacpi_alsa_data *data;
7420 struct snd_kcontrol *ctl_vol;
7421 struct snd_kcontrol *ctl_mute;
7422 int rc;
7423
Takashi Iwai89235e52014-01-29 15:01:27 +01007424 rc = snd_card_new(&tpacpi_pdev->dev,
7425 alsa_index, alsa_id, THIS_MODULE,
7426 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007427 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007428 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007429 return 1;
7430 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007431
7432 BUG_ON(!card->private_data);
7433 data = card->private_data;
7434 data->card = card;
7435
7436 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7437 sizeof(card->driver));
7438 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7439 sizeof(card->shortname));
7440 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7441 (thinkpad_id.ec_version_str) ?
7442 thinkpad_id.ec_version_str : "(unknown)");
7443 snprintf(card->longname, sizeof(card->longname),
7444 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7445 (thinkpad_id.ec_version_str) ?
7446 thinkpad_id.ec_version_str : "unknown");
7447
7448 if (volume_control_allowed) {
7449 volume_alsa_control_vol.put = volume_alsa_vol_put;
7450 volume_alsa_control_vol.access =
7451 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7452
7453 volume_alsa_control_mute.put = volume_alsa_mute_put;
7454 volume_alsa_control_mute.access =
7455 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7456 }
7457
7458 if (!tp_features.mixer_no_level_control) {
7459 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7460 rc = snd_ctl_add(card, ctl_vol);
7461 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007462 pr_err("Failed to create ALSA volume control: %d\n",
7463 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007464 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007465 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007466 data->ctl_vol_id = &ctl_vol->id;
7467 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007468
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007469 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7470 rc = snd_ctl_add(card, ctl_mute);
7471 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007472 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007473 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007474 }
7475 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007476
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007477 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007478 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007479 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007480 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007481 }
7482
7483 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007484 return 0;
7485
7486err_exit:
7487 snd_card_free(card);
7488 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007489}
7490
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007491#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7492#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7493
7494static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7495 /* Whitelist volume level on all IBM by default */
7496 { .vendor = PCI_VENDOR_ID_IBM,
7497 .bios = TPACPI_MATCH_ANY,
7498 .ec = TPACPI_MATCH_ANY,
7499 .quirks = TPACPI_VOL_Q_LEVEL },
7500
7501 /* Lenovo models with volume control (needs confirmation) */
7502 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7503 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7504 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7505 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7506 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7507 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7508 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7509
7510 /* Whitelist mute-only on all Lenovo by default */
7511 { .vendor = PCI_VENDOR_ID_LENOVO,
7512 .bios = TPACPI_MATCH_ANY,
7513 .ec = TPACPI_MATCH_ANY,
7514 .quirks = TPACPI_VOL_Q_MUTEONLY }
7515};
7516
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007517static int __init volume_init(struct ibm_init_struct *iibm)
7518{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007519 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007520 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007521
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007522 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7523
7524 mutex_init(&volume_mutex);
7525
7526 /*
7527 * Check for module parameter bogosity, note that we
7528 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7529 * able to detect "unspecified"
7530 */
7531 if (volume_mode > TPACPI_VOL_MODE_MAX)
7532 return -EINVAL;
7533
7534 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Joe Perches0978e012011-04-04 10:06:25 -07007535 pr_err("UCMS step volume mode not implemented, "
7536 "please contact %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007537 return 1;
7538 }
7539
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007540 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7541 return -EINVAL;
7542
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007543 /*
7544 * The ALSA mixer is our primary interface.
7545 * When disabled, don't install the subdriver at all
7546 */
7547 if (!alsa_enable) {
7548 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7549 "ALSA mixer disabled by parameter, "
7550 "not loading volume subdriver...\n");
7551 return 1;
7552 }
7553
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007554 quirks = tpacpi_check_quirks(volume_quirk_table,
7555 ARRAY_SIZE(volume_quirk_table));
7556
7557 switch (volume_capabilities) {
7558 case TPACPI_VOL_CAP_AUTO:
7559 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7560 tp_features.mixer_no_level_control = 1;
7561 else if (quirks & TPACPI_VOL_Q_LEVEL)
7562 tp_features.mixer_no_level_control = 0;
7563 else
7564 return 1; /* no mixer */
7565 break;
7566 case TPACPI_VOL_CAP_VOLMUTE:
7567 tp_features.mixer_no_level_control = 0;
7568 break;
7569 case TPACPI_VOL_CAP_MUTEONLY:
7570 tp_features.mixer_no_level_control = 1;
7571 break;
7572 default:
7573 return 1;
7574 }
7575
7576 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7577 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7578 "using user-supplied volume_capabilities=%d\n",
7579 volume_capabilities);
7580
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007581 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7582 volume_mode == TPACPI_VOL_MODE_MAX) {
7583 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7584
7585 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7586 "driver auto-selected volume_mode=%d\n",
7587 volume_mode);
7588 } else {
7589 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7590 "using user-supplied volume_mode=%d\n",
7591 volume_mode);
7592 }
7593
7594 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007595 "mute is supported, volume control is %s\n",
7596 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007597
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007598 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7599 software_mute_active = true;
7600 } else {
7601 rc = volume_create_alsa_mixer();
7602 if (rc) {
7603 pr_err("Could not create the ALSA mixer interface\n");
7604 return rc;
7605 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007606
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007607 pr_info("Console audio control enabled, mode: %s\n",
7608 (volume_control_allowed) ?
7609 "override (read/write)" :
7610 "monitor (read only)");
7611 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007612
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007613 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7614 "registering volume hotkeys as change notification\n");
7615 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7616 | TP_ACPI_HKEY_VOLUP_MASK
7617 | TP_ACPI_HKEY_VOLDWN_MASK
7618 | TP_ACPI_HKEY_MUTE_MASK);
7619
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007620 return 0;
7621}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007622
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007623static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007624{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007625 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007626
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007627 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007628 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007629 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007630 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007631 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007632 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007633 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007634 status & TP_EC_AUDIO_LVL_MSK);
7635
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007636 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007637 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007638
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007639 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007640 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007641 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007642 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007643 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007644 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007645 "commands:\tlevel <level>"
7646 " (<level> is 0-%d)\n",
7647 TP_EC_VOLUME_MAX);
7648 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007649 }
7650 }
7651
7652 return 0;
7653}
7654
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007655static int volume_write(char *buf)
7656{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007657 u8 s;
7658 u8 new_level, new_mute;
7659 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007660 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007661 int rc;
7662
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007663 /*
7664 * We do allow volume control at driver startup, so that the
7665 * user can set initial state through the volume=... parameter hack.
7666 */
7667 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7668 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7669 tp_warned.volume_ctrl_forbidden = 1;
Joe Perches0978e012011-04-04 10:06:25 -07007670 pr_notice("Console audio control in monitor mode, "
7671 "changes are not allowed\n");
7672 pr_notice("Use the volume_control=1 module parameter "
7673 "to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007674 }
7675 return -EPERM;
7676 }
7677
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007678 rc = volume_get_status(&s);
7679 if (rc < 0)
7680 return rc;
7681
7682 new_level = s & TP_EC_AUDIO_LVL_MSK;
7683 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007684
7685 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007686 if (!tp_features.mixer_no_level_control) {
7687 if (strlencmp(cmd, "up") == 0) {
7688 if (new_mute)
7689 new_mute = 0;
7690 else if (new_level < TP_EC_VOLUME_MAX)
7691 new_level++;
7692 continue;
7693 } else if (strlencmp(cmd, "down") == 0) {
7694 if (new_mute)
7695 new_mute = 0;
7696 else if (new_level > 0)
7697 new_level--;
7698 continue;
7699 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7700 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7701 new_level = l;
7702 continue;
7703 }
7704 }
7705 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007706 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007707 else if (strlencmp(cmd, "unmute") == 0)
7708 new_mute = 0;
7709 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007710 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007711 }
7712
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007713 if (tp_features.mixer_no_level_control) {
7714 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7715 new_mute ? "" : "un");
7716 rc = volume_set_mute(!!new_mute);
7717 } else {
7718 tpacpi_disclose_usertask("procfs volume",
7719 "%smute and set level to %d\n",
7720 new_mute ? "" : "un", new_level);
7721 rc = volume_set_status(new_mute | new_level);
7722 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007723 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007724
7725 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007726}
7727
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007728static struct ibm_struct volume_driver_data = {
7729 .name = "volume",
7730 .read = volume_read,
7731 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007732 .exit = volume_exit,
7733 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007734 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007735 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007736};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007737
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007738#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7739
7740#define alsa_card NULL
7741
Tobias Klauserbb55a2e2016-11-25 14:18:07 +01007742static inline void volume_alsa_notify_change(void)
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007743{
7744}
7745
7746static int __init volume_init(struct ibm_init_struct *iibm)
7747{
Joe Perches0978e012011-04-04 10:06:25 -07007748 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007749
7750 return 1;
7751}
7752
7753static struct ibm_struct volume_driver_data = {
7754 .name = "volume",
7755};
7756
7757#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7758
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007759/*************************************************************************
7760 * Fan subdriver
7761 */
7762
7763/*
7764 * FAN ACCESS MODES
7765 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007766 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007767 * ACPI GFAN method: returns fan level
7768 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007769 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007770 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007771 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007772 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007773 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7774 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007775 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7776 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007777 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007778 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007779 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7780 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007781 *
7782 * Fan speed changes of any sort (including those caused by the
7783 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007784 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007785 * limited.
7786 *
7787 * Reading is not available if GFAN exists.
7788 * Writing is not available if SFAN exists.
7789 *
7790 * Bits
7791 * 7 automatic mode engaged;
7792 * (default operation mode of the ThinkPad)
7793 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007794 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007795 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007796 * the tachometer while the fan controller ramps up
7797 * the speed (which can take up to a few *minutes*).
7798 * Speeds up fan to 100% duty-cycle, which is far above
7799 * the standard RPM levels. It is not impossible that
7800 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007801 * 5-3 unused in some models. Extra bits for fan level
7802 * in others, but still useless as all values above
7803 * 7 map to the same speed as level 7 in these models.
7804 * 2-0 fan level (0..7 usually)
7805 * 0x00 = stop
7806 * 0x07 = max (set when temperatures critical)
7807 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007808 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007809 *
7810 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007811 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007812 * does so, its initial value is meaningless (0x07).
7813 *
7814 * For firmware bugs, refer to:
7815 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7816 *
7817 * ----
7818 *
7819 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7820 * Main fan tachometer reading (in RPM)
7821 *
7822 * This register is present on all ThinkPads with a new-style EC, and
7823 * it is known not to be present on the A21m/e, and T22, as there is
7824 * something else in offset 0x84 according to the ACPI DSDT. Other
7825 * ThinkPads from this same time period (and earlier) probably lack the
7826 * tachometer as well.
7827 *
Nick Andrew877d0312009-01-26 11:06:57 +01007828 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007829 * was never fixed by IBM to report the EC firmware version string
7830 * probably support the tachometer (like the early X models), so
7831 * detecting it is quite hard. We need more data to know for sure.
7832 *
7833 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7834 * might result.
7835 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007836 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7837 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007838 *
7839 * For firmware bugs, refer to:
7840 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7841 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007842 * ----
7843 *
7844 * ThinkPad EC register 0x31 bit 0 (only on select models)
7845 *
7846 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7847 * show the speed of the main fan. When bit 0 of EC register 0x31
7848 * is one, the tachometer registers show the speed of the auxiliary
7849 * fan.
7850 *
7851 * Fan control seems to affect both fans, regardless of the state
7852 * of this bit.
7853 *
7854 * So far, only the firmware for the X60/X61 non-tablet versions
7855 * seem to support this (firmware TP-7M).
7856 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007857 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007858 * ThinkPad X31, X40, X41. Not available in the X60.
7859 *
7860 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7861 * high speed. ACPI DSDT seems to map these three speeds to levels
7862 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7863 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7864 *
7865 * The speeds are stored on handles
7866 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7867 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007868 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007869 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7870 *
7871 * ACPI DSDT switches which set is in use depending on various
7872 * factors.
7873 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007874 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007875 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7876 * but the ACPI tables just mention level 7.
7877 */
7878
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007879enum { /* Fan control constants */
7880 fan_status_offset = 0x2f, /* EC register 0x2f */
7881 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7882 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007883 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7884 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007885
7886 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7887 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7888
7889 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7890};
7891
7892enum fan_status_access_mode {
7893 TPACPI_FAN_NONE = 0, /* No fan status or control */
7894 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7895 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7896};
7897
7898enum fan_control_access_mode {
7899 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7900 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7901 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7902 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7903};
7904
7905enum fan_control_commands {
7906 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7907 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7908 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7909 * and also watchdog cmd */
7910};
7911
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307912static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007913
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007914static enum fan_status_access_mode fan_status_access_mode;
7915static enum fan_control_access_mode fan_control_access_mode;
7916static enum fan_control_commands fan_control_commands;
7917
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007918static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007919static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007920static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007921static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007922
7923static struct mutex fan_mutex;
7924
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007925static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007926static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007927
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007928TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7929TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007930 "\\FSPD", /* 600e/x, 770e, 770x */
7931 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007932TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007933 "JFNS", /* 770x-JL */
7934 ); /* all others */
7935
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007936/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007937 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7938 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7939 * be in auto mode (0x80).
7940 *
7941 * This is corrected by any write to HFSP either by the driver, or
7942 * by the firmware.
7943 *
7944 * We assume 0x07 really means auto mode while this quirk is active,
7945 * as this is far more likely than the ThinkPad being in level 7,
7946 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007947 *
7948 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7949 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007950 */
7951
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007952static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007953{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007954 if (fan_control_initial_status == 0x07) {
Joe Perches0978e012011-04-04 10:06:25 -07007955 pr_notice("fan_init: initial fan status is unknown, "
7956 "assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007957 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007958 }
7959}
7960
7961static void fan_quirk1_handle(u8 *fan_status)
7962{
7963 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7964 if (*fan_status != fan_control_initial_status) {
7965 /* something changed the HFSP regisnter since
7966 * driver init time, so it is not undefined
7967 * anymore */
7968 tp_features.fan_ctrl_status_undef = 0;
7969 } else {
7970 /* Return most likely status. In fact, it
7971 * might be the only possible status */
7972 *fan_status = TP_EC_FAN_AUTO;
7973 }
7974 }
7975}
7976
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007977/* Select main fan on X60/X61, NOOP on others */
7978static bool fan_select_fan1(void)
7979{
7980 if (tp_features.second_fan) {
7981 u8 val;
7982
7983 if (ec_read(fan_select_offset, &val) < 0)
7984 return false;
7985 val &= 0xFEU;
7986 if (ec_write(fan_select_offset, val) < 0)
7987 return false;
7988 }
7989 return true;
7990}
7991
7992/* Select secondary fan on X60/X61 */
7993static bool fan_select_fan2(void)
7994{
7995 u8 val;
7996
7997 if (!tp_features.second_fan)
7998 return false;
7999
8000 if (ec_read(fan_select_offset, &val) < 0)
8001 return false;
8002 val |= 0x01U;
8003 if (ec_write(fan_select_offset, val) < 0)
8004 return false;
8005
8006 return true;
8007}
8008
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008009/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008010 * Call with fan_mutex held
8011 */
8012static void fan_update_desired_level(u8 status)
8013{
8014 if ((status &
8015 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8016 if (status > 7)
8017 fan_control_desired_level = 7;
8018 else
8019 fan_control_desired_level = status;
8020 }
8021}
8022
8023static int fan_get_status(u8 *status)
8024{
8025 u8 s;
8026
8027 /* TODO:
8028 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8029
8030 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07008031 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008032 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07008033 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008034
Dan Carpentereceeb432012-09-01 12:54:07 -07008035 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008036 return -EIO;
8037
8038 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07008039 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008040
8041 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07008042 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008043 case TPACPI_FAN_RD_TPEC:
8044 /* all except 570, 600e/x, 770e, 770x */
8045 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8046 return -EIO;
8047
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008048 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008049 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008050 fan_quirk1_handle(status);
8051 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008052
8053 break;
8054
8055 default:
8056 return -ENXIO;
8057 }
8058
8059 return 0;
8060}
8061
8062static int fan_get_status_safe(u8 *status)
8063{
8064 int rc;
8065 u8 s;
8066
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008067 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008068 return -ERESTARTSYS;
8069 rc = fan_get_status(&s);
8070 if (!rc)
8071 fan_update_desired_level(s);
8072 mutex_unlock(&fan_mutex);
8073
Dan Carpentera7718362016-04-15 17:46:34 +03008074 if (rc)
8075 return rc;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008076 if (status)
8077 *status = s;
8078
Dan Carpentera7718362016-04-15 17:46:34 +03008079 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008080}
8081
8082static int fan_get_speed(unsigned int *speed)
8083{
8084 u8 hi, lo;
8085
8086 switch (fan_status_access_mode) {
8087 case TPACPI_FAN_RD_TPEC:
8088 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008089 if (unlikely(!fan_select_fan1()))
8090 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008091 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8092 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8093 return -EIO;
8094
8095 if (likely(speed))
8096 *speed = (hi << 8) | lo;
8097
8098 break;
8099
8100 default:
8101 return -ENXIO;
8102 }
8103
8104 return 0;
8105}
8106
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008107static int fan2_get_speed(unsigned int *speed)
8108{
8109 u8 hi, lo;
8110 bool rc;
8111
8112 switch (fan_status_access_mode) {
8113 case TPACPI_FAN_RD_TPEC:
8114 /* all except 570, 600e/x, 770e, 770x */
8115 if (unlikely(!fan_select_fan2()))
8116 return -EIO;
8117 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8118 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8119 fan_select_fan1(); /* play it safe */
8120 if (rc)
8121 return -EIO;
8122
8123 if (likely(speed))
8124 *speed = (hi << 8) | lo;
8125
8126 break;
8127
8128 default:
8129 return -ENXIO;
8130 }
8131
8132 return 0;
8133}
8134
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008135static int fan_set_level(int level)
8136{
8137 if (!fan_control_allowed)
8138 return -EPERM;
8139
8140 switch (fan_control_access_mode) {
8141 case TPACPI_FAN_WR_ACPI_SFAN:
8142 if (level >= 0 && level <= 7) {
8143 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8144 return -EIO;
8145 } else
8146 return -EINVAL;
8147 break;
8148
8149 case TPACPI_FAN_WR_ACPI_FANS:
8150 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008151 if (!(level & TP_EC_FAN_AUTO) &&
8152 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008153 ((level < 0) || (level > 7)))
8154 return -EINVAL;
8155
8156 /* safety net should the EC not support AUTO
8157 * or FULLSPEED mode bits and just ignore them */
8158 if (level & TP_EC_FAN_FULLSPEED)
8159 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01008160 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008161 level |= 4; /* safety min speed 4 */
8162
8163 if (!acpi_ec_write(fan_status_offset, level))
8164 return -EIO;
8165 else
8166 tp_features.fan_ctrl_status_undef = 0;
8167 break;
8168
8169 default:
8170 return -ENXIO;
8171 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008172
8173 vdbg_printk(TPACPI_DBG_FAN,
8174 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008175 return 0;
8176}
8177
8178static int fan_set_level_safe(int level)
8179{
8180 int rc;
8181
8182 if (!fan_control_allowed)
8183 return -EPERM;
8184
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008185 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008186 return -ERESTARTSYS;
8187
8188 if (level == TPACPI_FAN_LAST_LEVEL)
8189 level = fan_control_desired_level;
8190
8191 rc = fan_set_level(level);
8192 if (!rc)
8193 fan_update_desired_level(level);
8194
8195 mutex_unlock(&fan_mutex);
8196 return rc;
8197}
8198
8199static int fan_set_enable(void)
8200{
8201 u8 s;
8202 int rc;
8203
8204 if (!fan_control_allowed)
8205 return -EPERM;
8206
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008207 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008208 return -ERESTARTSYS;
8209
8210 switch (fan_control_access_mode) {
8211 case TPACPI_FAN_WR_ACPI_FANS:
8212 case TPACPI_FAN_WR_TPEC:
8213 rc = fan_get_status(&s);
8214 if (rc < 0)
8215 break;
8216
8217 /* Don't go out of emergency fan mode */
8218 if (s != 7) {
8219 s &= 0x07;
8220 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8221 }
8222
8223 if (!acpi_ec_write(fan_status_offset, s))
8224 rc = -EIO;
8225 else {
8226 tp_features.fan_ctrl_status_undef = 0;
8227 rc = 0;
8228 }
8229 break;
8230
8231 case TPACPI_FAN_WR_ACPI_SFAN:
8232 rc = fan_get_status(&s);
8233 if (rc < 0)
8234 break;
8235
8236 s &= 0x07;
8237
8238 /* Set fan to at least level 4 */
8239 s |= 4;
8240
8241 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008242 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008243 else
8244 rc = 0;
8245 break;
8246
8247 default:
8248 rc = -ENXIO;
8249 }
8250
8251 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008252
8253 if (!rc)
8254 vdbg_printk(TPACPI_DBG_FAN,
8255 "fan control: set fan control register to 0x%02x\n",
8256 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008257 return rc;
8258}
8259
8260static int fan_set_disable(void)
8261{
8262 int rc;
8263
8264 if (!fan_control_allowed)
8265 return -EPERM;
8266
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008267 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008268 return -ERESTARTSYS;
8269
8270 rc = 0;
8271 switch (fan_control_access_mode) {
8272 case TPACPI_FAN_WR_ACPI_FANS:
8273 case TPACPI_FAN_WR_TPEC:
8274 if (!acpi_ec_write(fan_status_offset, 0x00))
8275 rc = -EIO;
8276 else {
8277 fan_control_desired_level = 0;
8278 tp_features.fan_ctrl_status_undef = 0;
8279 }
8280 break;
8281
8282 case TPACPI_FAN_WR_ACPI_SFAN:
8283 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8284 rc = -EIO;
8285 else
8286 fan_control_desired_level = 0;
8287 break;
8288
8289 default:
8290 rc = -ENXIO;
8291 }
8292
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008293 if (!rc)
8294 vdbg_printk(TPACPI_DBG_FAN,
8295 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008296
8297 mutex_unlock(&fan_mutex);
8298 return rc;
8299}
8300
8301static int fan_set_speed(int speed)
8302{
8303 int rc;
8304
8305 if (!fan_control_allowed)
8306 return -EPERM;
8307
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008308 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008309 return -ERESTARTSYS;
8310
8311 rc = 0;
8312 switch (fan_control_access_mode) {
8313 case TPACPI_FAN_WR_ACPI_FANS:
8314 if (speed >= 0 && speed <= 65535) {
8315 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8316 speed, speed, speed))
8317 rc = -EIO;
8318 } else
8319 rc = -EINVAL;
8320 break;
8321
8322 default:
8323 rc = -ENXIO;
8324 }
8325
8326 mutex_unlock(&fan_mutex);
8327 return rc;
8328}
8329
8330static void fan_watchdog_reset(void)
8331{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008332 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8333 return;
8334
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008335 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008336 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8337 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8338 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8339 else
8340 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008341}
8342
8343static void fan_watchdog_fire(struct work_struct *ignored)
8344{
8345 int rc;
8346
8347 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8348 return;
8349
Joe Perches0978e012011-04-04 10:06:25 -07008350 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008351 rc = fan_set_enable();
8352 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07008353 pr_err("fan watchdog: error %d while enabling fan, "
8354 "will try again later...\n", -rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008355 /* reschedule for later */
8356 fan_watchdog_reset();
8357 }
8358}
8359
8360/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008361 * SYSFS fan layout: hwmon compatible (device)
8362 *
8363 * pwm*_enable:
8364 * 0: "disengaged" mode
8365 * 1: manual mode
8366 * 2: native EC "auto" mode (recommended, hardware default)
8367 *
8368 * pwm*: set speed in manual mode, ignored otherwise.
8369 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8370 * interpolation.
8371 *
8372 * fan*_input: tachometer reading, RPM
8373 *
8374 *
8375 * SYSFS fan layout: extensions
8376 *
8377 * fan_watchdog (driver):
8378 * fan watchdog interval in seconds, 0 disables (default), max 120
8379 */
8380
8381/* sysfs fan pwm1_enable ----------------------------------------------- */
8382static ssize_t fan_pwm1_enable_show(struct device *dev,
8383 struct device_attribute *attr,
8384 char *buf)
8385{
8386 int res, mode;
8387 u8 status;
8388
8389 res = fan_get_status_safe(&status);
8390 if (res)
8391 return res;
8392
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008393 if (status & TP_EC_FAN_FULLSPEED) {
8394 mode = 0;
8395 } else if (status & TP_EC_FAN_AUTO) {
8396 mode = 2;
8397 } else
8398 mode = 1;
8399
8400 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8401}
8402
8403static ssize_t fan_pwm1_enable_store(struct device *dev,
8404 struct device_attribute *attr,
8405 const char *buf, size_t count)
8406{
8407 unsigned long t;
8408 int res, level;
8409
8410 if (parse_strtoul(buf, 2, &t))
8411 return -EINVAL;
8412
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008413 tpacpi_disclose_usertask("hwmon pwm1_enable",
8414 "set fan mode to %lu\n", t);
8415
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008416 switch (t) {
8417 case 0:
8418 level = TP_EC_FAN_FULLSPEED;
8419 break;
8420 case 1:
8421 level = TPACPI_FAN_LAST_LEVEL;
8422 break;
8423 case 2:
8424 level = TP_EC_FAN_AUTO;
8425 break;
8426 case 3:
8427 /* reserved for software-controlled auto mode */
8428 return -ENOSYS;
8429 default:
8430 return -EINVAL;
8431 }
8432
8433 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008434 if (res == -ENXIO)
8435 return -EINVAL;
8436 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008437 return res;
8438
8439 fan_watchdog_reset();
8440
8441 return count;
8442}
8443
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008444static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8445 fan_pwm1_enable_show, fan_pwm1_enable_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008446
8447/* sysfs fan pwm1 ------------------------------------------------------ */
8448static ssize_t fan_pwm1_show(struct device *dev,
8449 struct device_attribute *attr,
8450 char *buf)
8451{
8452 int res;
8453 u8 status;
8454
8455 res = fan_get_status_safe(&status);
8456 if (res)
8457 return res;
8458
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008459 if ((status &
8460 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8461 status = fan_control_desired_level;
8462
8463 if (status > 7)
8464 status = 7;
8465
8466 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8467}
8468
8469static ssize_t fan_pwm1_store(struct device *dev,
8470 struct device_attribute *attr,
8471 const char *buf, size_t count)
8472{
8473 unsigned long s;
8474 int rc;
8475 u8 status, newlevel;
8476
8477 if (parse_strtoul(buf, 255, &s))
8478 return -EINVAL;
8479
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008480 tpacpi_disclose_usertask("hwmon pwm1",
8481 "set fan speed to %lu\n", s);
8482
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008483 /* scale down from 0-255 to 0-7 */
8484 newlevel = (s >> 5) & 0x07;
8485
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008486 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008487 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008488
8489 rc = fan_get_status(&status);
8490 if (!rc && (status &
8491 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8492 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008493 if (rc == -ENXIO)
8494 rc = -EINVAL;
8495 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008496 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008497 fan_watchdog_reset();
8498 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008499 }
8500
8501 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008502 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008503}
8504
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008505static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008506
8507/* sysfs fan fan1_input ------------------------------------------------ */
8508static ssize_t fan_fan1_input_show(struct device *dev,
8509 struct device_attribute *attr,
8510 char *buf)
8511{
8512 int res;
8513 unsigned int speed;
8514
8515 res = fan_get_speed(&speed);
8516 if (res < 0)
8517 return res;
8518
8519 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8520}
8521
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008522static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008523
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008524/* sysfs fan fan2_input ------------------------------------------------ */
8525static ssize_t fan_fan2_input_show(struct device *dev,
8526 struct device_attribute *attr,
8527 char *buf)
8528{
8529 int res;
8530 unsigned int speed;
8531
8532 res = fan2_get_speed(&speed);
8533 if (res < 0)
8534 return res;
8535
8536 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8537}
8538
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008539static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008540
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008541/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008542static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
8543 char *buf)
8544{
8545 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8546}
8547
8548static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8549 const char *buf, size_t count)
8550{
8551 unsigned long t;
8552
8553 if (parse_strtoul(buf, 120, &t))
8554 return -EINVAL;
8555
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008556 if (!fan_control_allowed)
8557 return -EPERM;
8558
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008559 fan_watchdog_maxinterval = t;
8560 fan_watchdog_reset();
8561
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008562 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8563
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008564 return count;
8565}
8566
8567static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8568 fan_fan_watchdog_show, fan_fan_watchdog_store);
8569
8570/* --------------------------------------------------------------------- */
8571static struct attribute *fan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008572 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8573 &dev_attr_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008574 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008575 NULL
8576};
8577
8578static const struct attribute_group fan_attr_group = {
8579 .attrs = fan_attributes,
8580};
8581
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008582#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8583#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008584
8585#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8586 { .vendor = PCI_VENDOR_ID_IBM, \
8587 .bios = TPACPI_MATCH_ANY, \
8588 .ec = TPID(__id1, __id2), \
8589 .quirks = __quirks }
8590
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008591#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8592 { .vendor = PCI_VENDOR_ID_LENOVO, \
8593 .bios = TPACPI_MATCH_ANY, \
8594 .ec = TPID(__id1, __id2), \
8595 .quirks = __quirks }
8596
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008597static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8598 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8599 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8600 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8601 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008602 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008603};
8604
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008605#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008606#undef TPACPI_FAN_QI
8607
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008608static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008609{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008610 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008611 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008612
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008613 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8614 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008615
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008616 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008617 fan_status_access_mode = TPACPI_FAN_NONE;
8618 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008619 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008620 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008621 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008622 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008623 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008624
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008625 if (tpacpi_is_ibm()) {
8626 TPACPI_ACPIHANDLE_INIT(fans);
8627 TPACPI_ACPIHANDLE_INIT(gfan);
8628 TPACPI_ACPIHANDLE_INIT(sfan);
8629 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008630
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008631 quirks = tpacpi_check_quirks(fan_quirk_table,
8632 ARRAY_SIZE(fan_quirk_table));
8633
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008634 if (gfan_handle) {
8635 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008636 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008637 } else {
8638 /* all other ThinkPads: note that even old-style
8639 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008640 if (likely(acpi_ec_read(fan_status_offset,
8641 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008642 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008643 if (quirks & TPACPI_FAN_Q1)
8644 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008645 if (quirks & TPACPI_FAN_2FAN) {
8646 tp_features.second_fan = 1;
8647 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8648 "secondary fan support enabled\n");
8649 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008650 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008651 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008652 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008653 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008654 }
8655 }
8656
8657 if (sfan_handle) {
8658 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008659 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008660 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008661 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008662 } else {
8663 if (!gfan_handle) {
8664 /* gfan without sfan means no fan control */
8665 /* all other models implement TP EC 0x2f control */
8666
8667 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008668 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008669 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008670 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008671 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008672 TPACPI_FAN_CMD_SPEED |
8673 TPACPI_FAN_CMD_LEVEL |
8674 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008675 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008676 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008677 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008678 TPACPI_FAN_CMD_LEVEL |
8679 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008680 }
8681 }
8682 }
8683
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008684 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8685 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008686 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8687 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8688 fan_status_access_mode, fan_control_access_mode);
8689
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008690 /* fan control master switch */
8691 if (!fan_control_allowed) {
8692 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8693 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008694 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008695 "fan control features disabled by parameter\n");
8696 }
8697
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008698 /* update fan_control_desired_level */
8699 if (fan_status_access_mode != TPACPI_FAN_NONE)
8700 fan_get_status_safe(NULL);
8701
8702 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8703 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008704 if (tp_features.second_fan) {
8705 /* attach second fan tachometer */
8706 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008707 &dev_attr_fan2_input.attr;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008708 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008709 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008710 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008711 if (rc < 0)
8712 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008713
8714 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8715 &driver_attr_fan_watchdog);
8716 if (rc < 0) {
8717 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8718 &fan_attr_group);
8719 return rc;
8720 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008721 return 0;
8722 } else
8723 return 1;
8724}
8725
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008726static void fan_exit(void)
8727{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008728 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008729 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008730
8731 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008732 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008733 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8734 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008735
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008736 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008737 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008738}
8739
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008740static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008741{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008742 int rc;
8743
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008744 if (!fan_control_allowed)
8745 return;
8746
8747 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008748 fan_control_resume_level = 0;
8749 rc = fan_get_status_safe(&fan_control_resume_level);
8750 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008751 pr_notice("failed to read fan level for later "
8752 "restore during resume: %d\n", rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008753
8754 /* if it is undefined, don't attempt to restore it.
8755 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008756 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008757 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008758}
8759
8760static void fan_resume(void)
8761{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008762 u8 current_level = 7;
8763 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008764 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008765
8766 /* DSDT *always* updates status on resume */
8767 tp_features.fan_ctrl_status_undef = 0;
8768
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008769 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008770 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008771 (fan_get_status_safe(&current_level) < 0))
8772 return;
8773
8774 switch (fan_control_access_mode) {
8775 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008776 /* never decrease fan level */
8777 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008778 break;
8779 case TPACPI_FAN_WR_ACPI_FANS:
8780 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008781 /* never decrease fan level, scale is:
8782 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8783 *
8784 * We expect the firmware to set either 7 or AUTO, but we
8785 * handle FULLSPEED out of paranoia.
8786 *
8787 * So, we can safely only restore FULLSPEED or 7, anything
8788 * else could slow the fan. Restoring AUTO is useless, at
8789 * best that's exactly what the DSDT already set (it is the
8790 * slower it uses).
8791 *
8792 * Always keep in mind that the DSDT *will* have set the
8793 * fans to what the vendor supposes is the best level. We
8794 * muck with it only to speed the fan up.
8795 */
8796 if (fan_control_resume_level != 7 &&
8797 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8798 return;
8799 else
8800 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8801 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008802 break;
8803 default:
8804 return;
8805 }
8806 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008807 pr_notice("restoring fan level to 0x%02x\n",
8808 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008809 rc = fan_set_level_safe(fan_control_resume_level);
8810 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008811 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008812 }
8813}
8814
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008815static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008816{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008817 int rc;
8818 u8 status;
8819 unsigned int speed = 0;
8820
8821 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008822 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008823 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008824 rc = fan_get_status_safe(&status);
8825 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008826 return rc;
8827
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008828 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008829 "level:\t\t%d\n",
8830 (status != 0) ? "enabled" : "disabled", status);
8831 break;
8832
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008833 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008834 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008835 rc = fan_get_status_safe(&status);
8836 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008837 return rc;
8838
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008839 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008840 (status != 0) ? "enabled" : "disabled");
8841
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008842 rc = fan_get_speed(&speed);
8843 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008844 return rc;
8845
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008846 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008847
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008848 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008849 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008850 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008851 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008852 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008853 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008854 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008855 break;
8856
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008857 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008858 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008859 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008860 }
8861
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008862 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008863 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008864
8865 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008866 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008867 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008868 break;
8869
8870 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008871 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008872 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008873 break;
8874 }
8875 }
8876
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008877 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008878 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008879 "commands:\twatchdog <timeout> (<timeout> "
8880 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008881
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008882 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008883 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008884 " (<speed> is 0-65535)\n");
8885
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008886 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008887}
8888
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008889static int fan_write_cmd_level(const char *cmd, int *rc)
8890{
8891 int level;
8892
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008893 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008894 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008895 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008896 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008897 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008898 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008899 return 0;
8900
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008901 *rc = fan_set_level_safe(level);
8902 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008903 pr_err("level command accepted for unsupported access mode %d\n",
8904 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008905 else if (!*rc)
8906 tpacpi_disclose_usertask("procfs fan",
8907 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008908
8909 return 1;
8910}
8911
8912static int fan_write_cmd_enable(const char *cmd, int *rc)
8913{
8914 if (strlencmp(cmd, "enable") != 0)
8915 return 0;
8916
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008917 *rc = fan_set_enable();
8918 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008919 pr_err("enable command accepted for unsupported access mode %d\n",
8920 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008921 else if (!*rc)
8922 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008923
8924 return 1;
8925}
8926
8927static int fan_write_cmd_disable(const char *cmd, int *rc)
8928{
8929 if (strlencmp(cmd, "disable") != 0)
8930 return 0;
8931
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008932 *rc = fan_set_disable();
8933 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008934 pr_err("disable command accepted for unsupported access mode %d\n",
8935 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008936 else if (!*rc)
8937 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008938
8939 return 1;
8940}
8941
8942static int fan_write_cmd_speed(const char *cmd, int *rc)
8943{
8944 int speed;
8945
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008946 /* TODO:
8947 * Support speed <low> <medium> <high> ? */
8948
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008949 if (sscanf(cmd, "speed %d", &speed) != 1)
8950 return 0;
8951
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008952 *rc = fan_set_speed(speed);
8953 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008954 pr_err("speed command accepted for unsupported access mode %d\n",
8955 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008956 else if (!*rc)
8957 tpacpi_disclose_usertask("procfs fan",
8958 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008959
8960 return 1;
8961}
8962
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008963static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8964{
8965 int interval;
8966
8967 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8968 return 0;
8969
8970 if (interval < 0 || interval > 120)
8971 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008972 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008973 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008974 tpacpi_disclose_usertask("procfs fan",
8975 "set watchdog timer to %d\n",
8976 interval);
8977 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008978
8979 return 1;
8980}
8981
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008982static int fan_write(char *buf)
8983{
8984 char *cmd;
8985 int rc = 0;
8986
8987 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008988 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008989 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008990 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008991 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008992 fan_write_cmd_disable(cmd, &rc) ||
8993 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008994 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008995 fan_write_cmd_speed(cmd, &rc))
8996 )
8997 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008998 else if (!rc)
8999 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009000 }
9001
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02009002 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009003}
9004
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009005static struct ibm_struct fan_driver_data = {
9006 .name = "fan",
9007 .read = fan_read,
9008 .write = fan_write,
9009 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03009010 .suspend = fan_suspend,
9011 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009012};
9013
David Henningsson420f9732013-10-16 23:10:31 +02009014/*************************************************************************
9015 * Mute LED subdriver
9016 */
9017
9018
9019struct tp_led_table {
9020 acpi_string name;
9021 int on_value;
9022 int off_value;
9023 int state;
9024};
9025
9026static struct tp_led_table led_tables[] = {
9027 [TPACPI_LED_MUTE] = {
9028 .name = "SSMS",
9029 .on_value = 1,
9030 .off_value = 0,
9031 },
9032 [TPACPI_LED_MICMUTE] = {
9033 .name = "MMTS",
9034 .on_value = 2,
9035 .off_value = 0,
9036 },
9037};
9038
9039static int mute_led_on_off(struct tp_led_table *t, bool state)
9040{
9041 acpi_handle temp;
9042 int output;
9043
Axel Lin3526eca2016-09-19 09:33:51 +08009044 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
David Henningsson420f9732013-10-16 23:10:31 +02009045 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9046 return -EIO;
9047 }
9048
9049 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9050 state ? t->on_value : t->off_value))
9051 return -EIO;
9052
9053 t->state = state;
9054 return state;
9055}
9056
9057int tpacpi_led_set(int whichled, bool on)
9058{
9059 struct tp_led_table *t;
9060
9061 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9062 return -EINVAL;
9063
9064 t = &led_tables[whichled];
9065 if (t->state < 0 || t->state == on)
9066 return t->state;
9067 return mute_led_on_off(t, on);
9068}
9069EXPORT_SYMBOL_GPL(tpacpi_led_set);
9070
9071static int mute_led_init(struct ibm_init_struct *iibm)
9072{
9073 acpi_handle temp;
9074 int i;
9075
9076 for (i = 0; i < TPACPI_LED_MAX; i++) {
9077 struct tp_led_table *t = &led_tables[i];
9078 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9079 mute_led_on_off(t, false);
9080 else
9081 t->state = -ENODEV;
9082 }
9083 return 0;
9084}
9085
9086static void mute_led_exit(void)
9087{
9088 int i;
9089
9090 for (i = 0; i < TPACPI_LED_MAX; i++)
9091 tpacpi_led_set(i, false);
9092}
9093
Takashi Iwai119f4492014-02-12 16:32:45 +01009094static void mute_led_resume(void)
9095{
9096 int i;
9097
9098 for (i = 0; i < TPACPI_LED_MAX; i++) {
9099 struct tp_led_table *t = &led_tables[i];
9100 if (t->state >= 0)
9101 mute_led_on_off(t, t->state);
9102 }
9103}
9104
David Henningsson420f9732013-10-16 23:10:31 +02009105static struct ibm_struct mute_led_driver_data = {
9106 .name = "mute_led",
9107 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01009108 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02009109};
9110
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009111/****************************************************************************
9112 ****************************************************************************
9113 *
9114 * Infrastructure
9115 *
9116 ****************************************************************************
9117 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07009118
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009119/*
9120 * HKEY event callout for other subdrivers go here
9121 * (yes, it is ugly, but it is quick, safe, and gets the job done
9122 */
9123static void tpacpi_driver_event(const unsigned int hkey_event)
9124{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00009125 if (ibm_backlight_device) {
9126 switch (hkey_event) {
9127 case TP_HKEY_EV_BRGHT_UP:
9128 case TP_HKEY_EV_BRGHT_DOWN:
9129 tpacpi_brightness_notify_change();
9130 }
9131 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009132 if (alsa_card) {
9133 switch (hkey_event) {
9134 case TP_HKEY_EV_VOL_UP:
9135 case TP_HKEY_EV_VOL_DOWN:
9136 case TP_HKEY_EV_VOL_MUTE:
9137 volume_alsa_notify_change();
9138 }
9139 }
Hans de Goedec685e202017-02-09 16:44:13 +01009140 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9141 enum led_brightness brightness;
9142
9143 mutex_lock(&kbdlight_mutex);
9144
9145 /*
9146 * Check the brightness actually changed, setting the brightness
9147 * through kbdlight_set_level() also triggers this event.
9148 */
9149 brightness = kbdlight_sysfs_get(NULL);
9150 if (kbdlight_brightness != brightness) {
9151 kbdlight_brightness = brightness;
9152 led_classdev_notify_brightness_hw_changed(
9153 &tpacpi_led_kbdlight.led_classdev, brightness);
9154 }
9155
9156 mutex_unlock(&kbdlight_mutex);
9157 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009158}
9159
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009160static void hotkey_driver_event(const unsigned int scancode)
9161{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03009162 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009163}
9164
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009165/* sysfs name ---------------------------------------------------------- */
9166static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
9167 struct device_attribute *attr,
9168 char *buf)
9169{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009170 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009171}
9172
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009173static DEVICE_ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009174
9175/* --------------------------------------------------------------------- */
9176
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009177/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03009178static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009179
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009180/*
9181 * Module and infrastructure proble, init and exit handling
9182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009183
Rusty Russell90ab5ee2012-01-13 09:32:20 +10309184static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009185
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009186#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009187static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009188{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009189 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009190
Jan van den Berg72a979f2014-09-17 00:01:08 +02009191 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009192}
9193#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9194
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009195static void ibm_exit(struct ibm_struct *ibm)
9196{
9197 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9198
9199 list_del_init(&ibm->all_drivers);
9200
9201 if (ibm->flags.acpi_notify_installed) {
9202 dbg_printk(TPACPI_DBG_EXIT,
9203 "%s: acpi_remove_notify_handler\n", ibm->name);
9204 BUG_ON(!ibm->acpi);
9205 acpi_remove_notify_handler(*ibm->acpi->handle,
9206 ibm->acpi->type,
9207 dispatch_acpi_notify);
9208 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009209 }
9210
9211 if (ibm->flags.proc_created) {
9212 dbg_printk(TPACPI_DBG_EXIT,
9213 "%s: remove_proc_entry\n", ibm->name);
9214 remove_proc_entry(ibm->name, proc_dir);
9215 ibm->flags.proc_created = 0;
9216 }
9217
9218 if (ibm->flags.acpi_driver_registered) {
9219 dbg_printk(TPACPI_DBG_EXIT,
9220 "%s: acpi_bus_unregister_driver\n", ibm->name);
9221 BUG_ON(!ibm->acpi);
9222 acpi_bus_unregister_driver(ibm->acpi->driver);
9223 kfree(ibm->acpi->driver);
9224 ibm->acpi->driver = NULL;
9225 ibm->flags.acpi_driver_registered = 0;
9226 }
9227
9228 if (ibm->flags.init_called && ibm->exit) {
9229 ibm->exit();
9230 ibm->flags.init_called = 0;
9231 }
9232
9233 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9234}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009235
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009236static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009237{
9238 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009239 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009240 struct proc_dir_entry *entry;
9241
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009242 BUG_ON(ibm == NULL);
9243
9244 INIT_LIST_HEAD(&ibm->all_drivers);
9245
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009246 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009247 return 0;
9248
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009249 dbg_printk(TPACPI_DBG_INIT,
9250 "probing for %s\n", ibm->name);
9251
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009252 if (iibm->init) {
9253 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009254 if (ret > 0)
9255 return 0; /* probe failed */
9256 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009257 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009258
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009259 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009260 }
9261
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009262 if (ibm->acpi) {
9263 if (ibm->acpi->hid) {
9264 ret = register_tpacpi_subdriver(ibm);
9265 if (ret)
9266 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009267 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009268
9269 if (ibm->acpi->notify) {
9270 ret = setup_acpi_notify(ibm);
9271 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07009272 pr_notice("disabling subdriver %s\n",
9273 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009274 ret = 0;
9275 goto err_out;
9276 }
9277 if (ret < 0)
9278 goto err_out;
9279 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009280 }
9281
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009282 dbg_printk(TPACPI_DBG_INIT,
9283 "%s installed\n", ibm->name);
9284
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009285 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04009286 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009287
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009288 if (!mode)
9289 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009290 if (ibm->write)
9291 mode |= S_IWUSR;
9292 entry = proc_create_data(ibm->name, mode, proc_dir,
9293 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009294 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07009295 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009296 ret = -ENODEV;
9297 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009298 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009299 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009301
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009302 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9303
Linus Torvalds1da177e2005-04-16 15:20:36 -07009304 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009305
9306err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009307 dbg_printk(TPACPI_DBG_INIT,
9308 "%s: at error exit path with result %d\n",
9309 ibm->name, ret);
9310
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009311 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +02009312 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009313}
9314
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009315/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009316
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009317static bool __pure __init tpacpi_is_fw_digit(const char c)
9318{
9319 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9320}
9321
Jan van den Berg72a979f2014-09-17 00:01:08 +02009322static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009323 const char t)
9324{
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009325 /*
9326 * Most models: xxyTkkWW (#.##c)
9327 * Ancient 570/600 and -SL lacks (#.##c)
9328 */
9329 if (s && strlen(s) >= 8 &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009330 tpacpi_is_fw_digit(s[0]) &&
9331 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009332 s[2] == t &&
9333 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009334 tpacpi_is_fw_digit(s[4]) &&
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009335 tpacpi_is_fw_digit(s[5]))
9336 return true;
9337
9338 /* New models: xxxyTkkW (#.##c); T550 and some others */
9339 return s && strlen(s) >= 8 &&
9340 tpacpi_is_fw_digit(s[0]) &&
9341 tpacpi_is_fw_digit(s[1]) &&
9342 tpacpi_is_fw_digit(s[2]) &&
9343 s[3] == t &&
9344 (s[4] == 'T' || s[4] == 'N') &&
9345 tpacpi_is_fw_digit(s[5]) &&
9346 tpacpi_is_fw_digit(s[6]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009347}
9348
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009349/* returns 0 - probe ok, or < 0 - probe error.
9350 * Probe ok doesn't mean thinkpad found.
9351 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9352static int __must_check __init get_thinkpad_model_data(
9353 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009354{
Jeff Garzik18552562007-10-03 15:15:40 -04009355 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009356 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009357 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009358
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009359 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009360 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009361
9362 memset(tp, 0, sizeof(*tp));
9363
9364 if (dmi_name_in_vendors("IBM"))
9365 tp->vendor = PCI_VENDOR_ID_IBM;
9366 else if (dmi_name_in_vendors("LENOVO"))
9367 tp->vendor = PCI_VENDOR_ID_LENOVO;
9368 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009369 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009370
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009371 s = dmi_get_system_info(DMI_BIOS_VERSION);
9372 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9373 if (s && !tp->bios_version_str)
9374 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009375
9376 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009377 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9378 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009379 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009380
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009381 tp->bios_model = tp->bios_version_str[0]
9382 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009383 tp->bios_release = (tp->bios_version_str[4] << 8)
9384 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009385
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009386 /*
9387 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9388 * X32 or newer, all Z series; Some models must have an
9389 * up-to-date BIOS or they will not be detected.
9390 *
9391 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9392 */
9393 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009394 if (sscanf(dev->name,
9395 "IBM ThinkPad Embedded Controller -[%17c",
9396 ec_fw_string) == 1) {
9397 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9398 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009399
9400 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009401 if (!tp->ec_version_str)
9402 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009403
9404 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9405 tp->ec_model = ec_fw_string[0]
9406 | (ec_fw_string[1] << 8);
9407 tp->ec_release = (ec_fw_string[4] << 8)
9408 | ec_fw_string[5];
9409 } else {
Joe Perches0978e012011-04-04 10:06:25 -07009410 pr_notice("ThinkPad firmware release %s "
9411 "doesn't match the known patterns\n",
9412 ec_fw_string);
9413 pr_notice("please report this to %s\n",
9414 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009415 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009416 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009417 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009418 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009419
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009420 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009421 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009422 tp->model_str = kstrdup(s, GFP_KERNEL);
9423 if (!tp->model_str)
9424 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009425 } else {
9426 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009427 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009428 tp->model_str = kstrdup(s, GFP_KERNEL);
9429 if (!tp->model_str)
9430 return -ENOMEM;
9431 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009432 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03009433
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009434 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9435 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9436 if (s && !tp->nummodel_str)
9437 return -ENOMEM;
9438
9439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440}
9441
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009442static int __init probe_for_thinkpad(void)
9443{
9444 int is_thinkpad;
9445
9446 if (acpi_disabled)
9447 return -ENODEV;
9448
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009449 /* It would be dangerous to run the driver in this case */
9450 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9451 return -ENODEV;
9452
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009453 /*
9454 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009455 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009456 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009457 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9458 (thinkpad_id.ec_model != 0) ||
9459 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009460
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03009461 /* The EC handler is required */
9462 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009463 if (!ec_handle) {
9464 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07009465 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009466 return -ENODEV;
9467 }
9468
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009469 if (!is_thinkpad && !force_load)
9470 return -ENODEV;
9471
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009472 return 0;
9473}
9474
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009475static void __init thinkpad_acpi_init_banner(void)
9476{
Joe Perches0978e012011-04-04 10:06:25 -07009477 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9478 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009479
Joe Perches0978e012011-04-04 10:06:25 -07009480 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009481 (thinkpad_id.bios_version_str) ?
9482 thinkpad_id.bios_version_str : "unknown",
9483 (thinkpad_id.ec_version_str) ?
9484 thinkpad_id.ec_version_str : "unknown");
9485
9486 BUG_ON(!thinkpad_id.vendor);
9487
9488 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07009489 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009490 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9491 "IBM" : ((thinkpad_id.vendor ==
9492 PCI_VENDOR_ID_LENOVO) ?
9493 "Lenovo" : "Unknown vendor"),
9494 thinkpad_id.model_str,
9495 (thinkpad_id.nummodel_str) ?
9496 thinkpad_id.nummodel_str : "unknown");
9497}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009498
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009499/* Module init, exit, parameters */
9500
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009501static struct ibm_init_struct ibms_init[] __initdata = {
9502 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009503 .data = &thinkpad_acpi_driver_data,
9504 },
9505 {
9506 .init = hotkey_init,
9507 .data = &hotkey_driver_data,
9508 },
9509 {
9510 .init = bluetooth_init,
9511 .data = &bluetooth_driver_data,
9512 },
9513 {
9514 .init = wan_init,
9515 .data = &wan_driver_data,
9516 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009517 {
9518 .init = uwb_init,
9519 .data = &uwb_driver_data,
9520 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009521#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009522 {
9523 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009524 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009525 .data = &video_driver_data,
9526 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009527#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009528 {
Pali Rohárbb28f3d52015-12-30 23:27:41 +01009529 .init = kbdlight_init,
9530 .data = &kbdlight_driver_data,
9531 },
9532 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009533 .init = light_init,
9534 .data = &light_driver_data,
9535 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009536 {
9537 .init = cmos_init,
9538 .data = &cmos_driver_data,
9539 },
9540 {
9541 .init = led_init,
9542 .data = &led_driver_data,
9543 },
9544 {
9545 .init = beep_init,
9546 .data = &beep_driver_data,
9547 },
9548 {
9549 .init = thermal_init,
9550 .data = &thermal_driver_data,
9551 },
9552 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009553 .init = brightness_init,
9554 .data = &brightness_driver_data,
9555 },
9556 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009557 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009558 .data = &volume_driver_data,
9559 },
9560 {
9561 .init = fan_init,
9562 .data = &fan_driver_data,
9563 },
David Henningsson420f9732013-10-16 23:10:31 +02009564 {
9565 .init = mute_led_init,
9566 .data = &mute_led_driver_data,
9567 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009568};
9569
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009570static int __init set_ibm_param(const char *val, struct kernel_param *kp)
9571{
9572 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009573 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009574
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009575 if (!kp || !kp->name || !val)
9576 return -EINVAL;
9577
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009578 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9579 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009580 WARN_ON(ibm == NULL);
9581
9582 if (!ibm || !ibm->name)
9583 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009584
9585 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9586 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009587 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009588 strcpy(ibms_init[i].param, val);
9589 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009590 return 0;
9591 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009592 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009593
9594 return -EINVAL;
9595}
9596
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009597module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009598MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009599 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009600
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009601module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009602MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009603
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009604module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009605MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009606 "Attempts to load the driver even on a "
9607 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009608
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009609module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009610MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009611 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03009612
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009613module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009614MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009615 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00009616 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03009617
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009618module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009619MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009620 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02009621
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009622#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009623module_param_named(volume_mode, volume_mode, uint, 0444);
9624MODULE_PARM_DESC(volume_mode,
9625 "Selects volume control strategy: "
9626 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9627
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02009628module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9629MODULE_PARM_DESC(volume_capabilities,
9630 "Selects the mixer capabilites: "
9631 "0=auto, 1=volume and mute, 2=mute only");
9632
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02009633module_param_named(volume_control, volume_control_allowed, bool, 0444);
9634MODULE_PARM_DESC(volume_control,
9635 "Enables software override for the console audio "
9636 "control when true");
9637
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07009638module_param_named(software_mute, software_mute_requested, bool, 0444);
9639MODULE_PARM_DESC(software_mute,
9640 "Request full software mute control");
9641
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009642/* ALSA module API parameters */
9643module_param_named(index, alsa_index, int, 0444);
9644MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9645module_param_named(id, alsa_id, charp, 0444);
9646MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9647module_param_named(enable, alsa_enable, bool, 0444);
9648MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009649#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009650
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009651#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009652 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02009653 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009654 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009655
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009656TPACPI_PARAM(hotkey);
9657TPACPI_PARAM(bluetooth);
9658TPACPI_PARAM(video);
9659TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009660TPACPI_PARAM(cmos);
9661TPACPI_PARAM(led);
9662TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009663TPACPI_PARAM(brightness);
9664TPACPI_PARAM(volume);
9665TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009666
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009667#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009668module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009669MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9670module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9671MODULE_PARM_DESC(wlsw_state,
9672 "Initial state of the emulated WLSW switch");
9673
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009674module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009675MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9676module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9677MODULE_PARM_DESC(bluetooth_state,
9678 "Initial state of the emulated bluetooth switch");
9679
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009680module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009681MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9682module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9683MODULE_PARM_DESC(wwan_state,
9684 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009685
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009686module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009687MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9688module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9689MODULE_PARM_DESC(uwb_state,
9690 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009691#endif
9692
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009693static void thinkpad_acpi_module_exit(void)
9694{
9695 struct ibm_struct *ibm, *itmp;
9696
9697 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9698
9699 list_for_each_entry_safe_reverse(ibm, itmp,
9700 &tpacpi_all_drivers,
9701 all_drivers) {
9702 ibm_exit(ibm);
9703 }
9704
9705 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9706
9707 if (tpacpi_inputdev) {
9708 if (tp_features.input_device_registered)
9709 input_unregister_device(tpacpi_inputdev);
9710 else
9711 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +10009712 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009713 }
9714
9715 if (tpacpi_hwmon)
9716 hwmon_device_unregister(tpacpi_hwmon);
9717
9718 if (tp_features.sensors_pdev_attrs_registered)
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009719 device_remove_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009720 if (tpacpi_sensors_pdev)
9721 platform_device_unregister(tpacpi_sensors_pdev);
9722 if (tpacpi_pdev)
9723 platform_device_unregister(tpacpi_pdev);
9724
9725 if (tp_features.sensors_pdrv_attrs_registered)
9726 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9727 if (tp_features.platform_drv_attrs_registered)
9728 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9729
9730 if (tp_features.sensors_pdrv_registered)
9731 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9732
9733 if (tp_features.platform_drv_registered)
9734 platform_driver_unregister(&tpacpi_pdriver);
9735
9736 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009737 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009738
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009739 if (tpacpi_wq)
9740 destroy_workqueue(tpacpi_wq);
9741
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009742 kfree(thinkpad_id.bios_version_str);
9743 kfree(thinkpad_id.ec_version_str);
9744 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +10009745 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009746}
9747
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009748
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009749static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009750{
9751 int ret, i;
9752
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009753 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9754
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009755 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009756
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009757 ret = get_thinkpad_model_data(&thinkpad_id);
9758 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009759 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009760 thinkpad_acpi_module_exit();
9761 return ret;
9762 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009763 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009764 if (ret) {
9765 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009766 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009768
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009769 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009770
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009771 thinkpad_acpi_init_banner();
9772 tpacpi_check_outdated_fw();
9773
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009774 TPACPI_ACPIHANDLE_INIT(ecrd);
9775 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009777 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9778 if (!tpacpi_wq) {
9779 thinkpad_acpi_module_exit();
9780 return -ENOMEM;
9781 }
9782
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009783 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07009785 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009786 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009787 return -ENODEV;
9788 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009789
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009790 ret = platform_driver_register(&tpacpi_pdriver);
9791 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009792 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009793 thinkpad_acpi_module_exit();
9794 return ret;
9795 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009796 tp_features.platform_drv_registered = 1;
9797
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009798 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9799 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009800 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009801 thinkpad_acpi_module_exit();
9802 return ret;
9803 }
9804 tp_features.sensors_pdrv_registered = 1;
9805
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009806 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009807 if (!ret) {
9808 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009809 ret = tpacpi_create_driver_attributes(
9810 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009811 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009812 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009813 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009814 thinkpad_acpi_module_exit();
9815 return ret;
9816 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009817 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009818
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009819
9820 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009821 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009822 NULL, 0);
9823 if (IS_ERR(tpacpi_pdev)) {
9824 ret = PTR_ERR(tpacpi_pdev);
9825 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009826 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009827 thinkpad_acpi_module_exit();
9828 return ret;
9829 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009830 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009831 TPACPI_HWMON_DRVR_NAME,
9832 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009833 if (IS_ERR(tpacpi_sensors_pdev)) {
9834 ret = PTR_ERR(tpacpi_sensors_pdev);
9835 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009836 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009837 thinkpad_acpi_module_exit();
9838 return ret;
9839 }
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009840 ret = device_create_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009841 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009842 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009843 thinkpad_acpi_module_exit();
9844 return ret;
9845 }
9846 tp_features.sensors_pdev_attrs_registered = 1;
9847 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009848 if (IS_ERR(tpacpi_hwmon)) {
9849 ret = PTR_ERR(tpacpi_hwmon);
9850 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009851 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009852 thinkpad_acpi_module_exit();
9853 return ret;
9854 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009855 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009856 tpacpi_inputdev = input_allocate_device();
9857 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009858 thinkpad_acpi_module_exit();
9859 return -ENOMEM;
9860 } else {
9861 /* Prepare input device, but don't register */
9862 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009863 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009864 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009865 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009866 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9867 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009868 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009869 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009870
9871 /* Init subdriver dependencies */
9872 tpacpi_detect_brightness_capabilities();
9873
9874 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009875 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9876 ret = ibm_init(&ibms_init[i]);
9877 if (ret >= 0 && *ibms_init[i].param)
9878 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009879 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009880 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009881 return ret;
9882 }
9883 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009884
9885 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9886
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009887 ret = input_register_device(tpacpi_inputdev);
9888 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009889 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009890 thinkpad_acpi_module_exit();
9891 return ret;
9892 } else {
9893 tp_features.input_device_registered = 1;
9894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009895
9896 return 0;
9897}
9898
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009899MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9900
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009901/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009902 * This will autoload the driver in almost every ThinkPad
9903 * in widespread use.
9904 *
9905 * Only _VERY_ old models, like the 240, 240x and 570 lack
9906 * the HKEY event interface.
9907 */
9908MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9909
9910/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009911 * DMI matching for module autoloading
9912 *
9913 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9914 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9915 *
9916 * Only models listed in thinkwiki will be supported, so add yours
9917 * if it is not there yet.
9918 */
9919#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009920 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009921
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009922/* Ancient thinkpad BIOSes have to be identified by
9923 * BIOS type or model number, and there are far less
9924 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009925IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009926
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009927MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9928MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009929MODULE_DESCRIPTION(TPACPI_DESC);
9930MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009931MODULE_LICENSE("GPL");
9932
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009933module_init(thinkpad_acpi_module_init);
9934module_exit(thinkpad_acpi_module_exit);