blob: 60ee94e572423228d0c1710d642fbe6b4fb2091f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Joe Perches0978e012011-04-04 10:06:25 -070024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
David Henningsson420f9732013-10-16 23:10:31 +020026#define TPACPI_VERSION "0.25"
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020027#define TPACPI_SYSFS_VERSION 0x020700
Borislav Deianov78f81cc2005-08-17 00:00:00 -040028
29/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020031 * 2007-10-20 changelog trimmed down
32 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030033 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020035 *
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030039 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040040 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020042 *
43 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040044 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
51
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020052#include <linux/kernel.h>
53#include <linux/module.h>
54#include <linux/init.h>
55#include <linux/types.h>
56#include <linux/string.h>
57#include <linux/list.h>
58#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000059#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020060#include <linux/kthread.h>
61#include <linux/freezer.h>
62#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020064#include <linux/nvram.h>
65#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020066#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020067#include <linux/sysfs.h>
68#include <linux/backlight.h>
69#include <linux/fb.h>
70#include <linux/platform_device.h>
71#include <linux/hwmon.h>
72#include <linux/hwmon-sysfs.h>
73#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030074#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030075#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020076#include <linux/dmi.h>
77#include <linux/jiffies.h>
78#include <linux/workqueue.h>
Lv Zheng8b484632013-12-03 08:49:16 +080079#include <linux/acpi.h>
80#include <linux/pci_ids.h>
81#include <linux/thinkpad_acpi.h>
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020082#include <sound/core.h>
83#include <sound/control.h>
84#include <sound/initval.h>
Lv Zheng8b484632013-12-03 08:49:16 +080085#include <asm/uaccess.h>
Hans de Goedeb33c6ce2015-06-16 16:28:10 +020086#include <acpi/video.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020087
88/* ThinkPad CMOS commands */
89#define TP_CMOS_VOLUME_DOWN 0
90#define TP_CMOS_VOLUME_UP 1
91#define TP_CMOS_VOLUME_MUTE 2
92#define TP_CMOS_BRIGHTNESS_UP 4
93#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030094#define TP_CMOS_THINKLIGHT_ON 12
95#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020096
97/* NVRAM Addresses */
98enum tp_nvram_addr {
99 TP_NVRAM_ADDR_HK2 = 0x57,
100 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
101 TP_NVRAM_ADDR_VIDEO = 0x59,
102 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
103 TP_NVRAM_ADDR_MIXER = 0x60,
104};
105
106/* NVRAM bit masks */
107enum {
108 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
109 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
110 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
111 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
112 TP_NVRAM_MASK_THINKLIGHT = 0x10,
113 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
114 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
115 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
116 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
117 TP_NVRAM_MASK_MUTE = 0x40,
118 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
119 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
120 TP_NVRAM_POS_LEVEL_VOLUME = 0,
121};
122
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300123/* Misc NVRAM-related */
124enum {
125 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
126};
127
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200128/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400129#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
130#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300131#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200132
133/* Input IDs */
134#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
135#define TPACPI_HKEY_INPUT_VERSION 0x4101
136
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200137/* ACPI \WGSV commands */
138enum {
139 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
140 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
141 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
142 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
143};
144
145/* TP_ACPI_WGSV_GET_STATE bits */
146enum {
147 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
148 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
149 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
150 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
151 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
152 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
153 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
154 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
155 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
156 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
157};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200158
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300159/* HKEY events */
160enum tpacpi_hkey_event_t {
161 /* Hotkey-related */
162 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
163 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
164 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
165 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
166 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
167 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
168
169 /* Reasons for waking up from S3/S4 */
170 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
171 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
172 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
173 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
174 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
175 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
176
177 /* Auto-sleep after eject request */
178 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
179 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
180
181 /* Misc bay events */
182 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -0300183 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
184 or port replicator */
185 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
186 dock or port replicator */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300187
188 /* User-interface events */
189 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
190 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
191 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
192 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
193 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
194 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
195 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
196
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300197 /* Key-related user-interface events */
198 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
199 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
Xavier Naveira67ab6242015-02-10 08:45:18 +0100200 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300201
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300202 /* Thermal events */
203 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
204 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
205 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
206 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
207 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
208
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100209 /* AC-related events */
210 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300211
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300212 /* Misc */
213 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
214};
215
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200216/****************************************************************************
217 * Main driver
218 */
219
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200220#define TPACPI_NAME "thinkpad"
221#define TPACPI_DESC "ThinkPad ACPI Extras"
222#define TPACPI_FILE TPACPI_NAME "_acpi"
223#define TPACPI_URL "http://ibm-acpi.sf.net/"
224#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200225
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200226#define TPACPI_PROC_DIR "ibm"
227#define TPACPI_ACPI_EVENT_PREFIX "ibm"
228#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300229#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200230#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200231
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300232#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300233#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300234
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200235#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200236
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000237/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200238#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000239#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200240#define TPACPI_DBG_INIT 0x0001
241#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000242#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000243#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000244#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000245#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200246#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200247
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200248#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
249#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
250#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200251
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200252
253/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200254 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200255 */
256
257struct ibm_struct;
258
259struct tp_acpi_drv_struct {
260 const struct acpi_device_id *hid;
261 struct acpi_driver *driver;
262
263 void (*notify) (struct ibm_struct *, u32);
264 acpi_handle *handle;
265 u32 type;
266 struct acpi_device *device;
267};
268
269struct ibm_struct {
270 char *name;
271
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200272 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200273 int (*write) (char *);
274 void (*exit) (void);
275 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200276 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200277 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200278
279 struct list_head all_drivers;
280
281 struct tp_acpi_drv_struct *acpi;
282
283 struct {
284 u8 acpi_driver_registered:1;
285 u8 acpi_notify_installed:1;
286 u8 proc_created:1;
287 u8 init_called:1;
288 u8 experimental:1;
289 } flags;
290};
291
292struct ibm_init_struct {
293 char param[32];
294
295 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400296 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200297 struct ibm_struct *data;
298};
299
300static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200301 u32 bluetooth:1;
302 u32 hotkey:1;
303 u32 hotkey_mask:1;
304 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200305 u32 hotkey_tablet:1;
Pali Rohárbb28f3d2015-12-30 23:27:41 +0100306 u32 kbdlight:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200307 u32 light:1;
308 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300309 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300310 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200311 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200312 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200313 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300314 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300315 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200316 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200317 u32 input_device_registered:1;
318 u32 platform_drv_registered:1;
319 u32 platform_drv_attrs_registered:1;
320 u32 sensors_pdrv_registered:1;
321 u32 sensors_pdrv_attrs_registered:1;
322 u32 sensors_pdev_attrs_registered:1;
323 u32 hotkey_poll_active:1;
Bastien Noceraf23a5bc2015-03-02 14:45:16 +0100324 u32 has_adaptive_kbd:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200325} tp_features;
326
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300327static struct {
328 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200329 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300330} tp_warned;
331
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200332struct thinkpad_id_data {
333 unsigned int vendor; /* ThinkPad vendor:
334 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
335
336 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
337 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
338
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300339 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200340 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300341 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
342 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200343
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300344 char *model_str; /* ThinkPad T43 */
345 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200346};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200347static struct thinkpad_id_data thinkpad_id;
348
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300349static enum {
350 TPACPI_LIFE_INIT = 0,
351 TPACPI_LIFE_RUNNING,
352 TPACPI_LIFE_EXITING,
353} tpacpi_lifecycle;
354
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200355static int experimental;
356static u32 dbg_level;
357
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300358static struct workqueue_struct *tpacpi_wq;
359
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000360enum led_status_t {
361 TPACPI_LED_OFF = 0,
362 TPACPI_LED_ON,
363 TPACPI_LED_BLINK,
364};
365
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300366/* Special LED class that can defer work */
367struct tpacpi_led_classdev {
368 struct led_classdev led_classdev;
369 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000370 enum led_status_t new_state;
Adam Leeedf2d772013-06-08 16:51:15 +0800371 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300372};
373
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300374/* brightness level capabilities */
375static unsigned int bright_maxlvl; /* 0 = unknown */
376
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200377#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
378static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030379static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200380static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030381static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200382static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030383static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200384static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030385static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200386#endif
387
388
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000389/*************************************************************************
390 * Debugging helpers
391 */
392
Joe Perches0978e012011-04-04 10:06:25 -0700393#define dbg_printk(a_dbg_level, format, arg...) \
394do { \
395 if (dbg_level & (a_dbg_level)) \
396 printk(KERN_DEBUG pr_fmt("%s: " format), \
397 __func__, ##arg); \
398} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000399
400#ifdef CONFIG_THINKPAD_ACPI_DEBUG
401#define vdbg_printk dbg_printk
402static const char *str_supported(int is_supported);
403#else
Joe Perches0978e012011-04-04 10:06:25 -0700404static inline const char *str_supported(int is_supported) { return ""; }
405#define vdbg_printk(a_dbg_level, format, arg...) \
Joe Perchesefd85cf2015-08-26 11:13:38 -0700406 do { if (0) no_printk(format, ##arg); } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000407#endif
408
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000409static void tpacpi_log_usertask(const char * const what)
410{
Joe Perches0978e012011-04-04 10:06:25 -0700411 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
412 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000413}
414
Joe Perches0978e012011-04-04 10:06:25 -0700415#define tpacpi_disclose_usertask(what, format, arg...) \
416do { \
417 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
418 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
419 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
420 what, task_tgid_vnr(current), ## arg); \
421 } \
422} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000423
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300424/*
425 * Quirk handling helpers
426 *
427 * ThinkPad IDs and versions seen in the field so far
428 * are two-characters from the set [0-9A-Z], i.e. base 36.
429 *
430 * We use values well outside that range as specials.
431 */
432
433#define TPACPI_MATCH_ANY 0xffffU
434#define TPACPI_MATCH_UNKNOWN 0U
435
436/* TPID('1', 'Y') == 0x5931 */
437#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
438
439#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
440 { .vendor = PCI_VENDOR_ID_IBM, \
441 .bios = TPID(__id1, __id2), \
442 .ec = TPACPI_MATCH_ANY, \
443 .quirks = (__quirk) }
444
445#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
446 { .vendor = PCI_VENDOR_ID_LENOVO, \
447 .bios = TPID(__id1, __id2), \
448 .ec = TPACPI_MATCH_ANY, \
449 .quirks = (__quirk) }
450
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200451#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
452 { .vendor = PCI_VENDOR_ID_LENOVO, \
453 .bios = TPACPI_MATCH_ANY, \
454 .ec = TPID(__id1, __id2), \
455 .quirks = (__quirk) }
456
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300457struct tpacpi_quirk {
458 unsigned int vendor;
459 u16 bios;
460 u16 ec;
461 unsigned long quirks;
462};
463
464/**
465 * tpacpi_check_quirks() - search BIOS/EC version on a list
466 * @qlist: array of &struct tpacpi_quirk
467 * @qlist_size: number of elements in @qlist
468 *
469 * Iterates over a quirks list until one is found that matches the
470 * ThinkPad's vendor, BIOS and EC model.
471 *
472 * Returns 0 if nothing matches, otherwise returns the quirks field of
473 * the matching &struct tpacpi_quirk entry.
474 *
475 * The match criteria is: vendor, ec and bios much match.
476 */
477static unsigned long __init tpacpi_check_quirks(
478 const struct tpacpi_quirk *qlist,
479 unsigned int qlist_size)
480{
481 while (qlist_size) {
482 if ((qlist->vendor == thinkpad_id.vendor ||
483 qlist->vendor == TPACPI_MATCH_ANY) &&
484 (qlist->bios == thinkpad_id.bios_model ||
485 qlist->bios == TPACPI_MATCH_ANY) &&
486 (qlist->ec == thinkpad_id.ec_model ||
487 qlist->ec == TPACPI_MATCH_ANY))
488 return qlist->quirks;
489
490 qlist_size--;
491 qlist++;
492 }
493 return 0;
494}
495
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300496static inline bool __pure __init tpacpi_is_lenovo(void)
497{
498 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
499}
500
501static inline bool __pure __init tpacpi_is_ibm(void)
502{
503 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
504}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300505
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300506/****************************************************************************
507 ****************************************************************************
508 *
509 * ACPI Helpers and device model
510 *
511 ****************************************************************************
512 ****************************************************************************/
513
514/*************************************************************************
515 * ACPI basic handles
516 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300518static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300519static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200521#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700523 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300524 &parent##_handle; \
525 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200527TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
528TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200530TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
531 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400532 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
533 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300534 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400535
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200536TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400537 "^HKEY", /* R30, R31 */
538 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300539 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400540
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300541/*************************************************************************
542 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200543 */
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700546 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400549 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200550 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400551 struct acpi_buffer result, *resultp;
552 union acpi_object out_obj;
553 acpi_status status;
554 va_list ap;
555 char res_type;
556 int success;
557 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700560 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return 0;
562 }
563
564 if (*fmt == 'q') {
565 quiet = 1;
566 fmt++;
567 } else
568 quiet = 0;
569
570 res_type = *(fmt++);
571
572 params.count = 0;
573 params.pointer = &in_objs[0];
574
575 va_start(ap, fmt);
576 while (*fmt) {
577 char c = *(fmt++);
578 switch (c) {
579 case 'd': /* int */
580 in_objs[params.count].integer.value = va_arg(ap, int);
581 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
582 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400583 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 default:
Joe Perches0978e012011-04-04 10:06:25 -0700585 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 "with invalid format character '%c'\n", c);
Jesper Juhl21365852010-12-24 19:56:28 +0100587 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return 0;
589 }
590 }
591 va_end(ap);
592
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400593 if (res_type != 'v') {
594 result.length = sizeof(out_obj);
595 result.pointer = &out_obj;
596 resultp = &result;
597 } else
598 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400600 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400603 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300604 success = (status == AE_OK &&
605 out_obj.type == ACPI_TYPE_INTEGER);
606 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700607 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400609 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 success = status == AE_OK;
611 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400612 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 default:
Joe Perches0978e012011-04-04 10:06:25 -0700614 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 "with invalid format character '%c'\n", res_type);
616 return 0;
617 }
618
619 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700620 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300621 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 return success;
624}
625
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200626static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300627{
628 int v;
629
630 if (ecrd_handle) {
631 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
632 return 0;
633 *p = v;
634 } else {
635 if (ec_read(i, p) < 0)
636 return 0;
637 }
638
639 return 1;
640}
641
642static int acpi_ec_write(int i, u8 v)
643{
644 if (ecwr_handle) {
645 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
646 return 0;
647 } else {
648 if (ec_write(i, v) < 0)
649 return 0;
650 }
651
652 return 1;
653}
654
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300655static int issue_thinkpad_cmos_command(int cmos_cmd)
656{
657 if (!cmos_handle)
658 return -ENXIO;
659
660 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
661 return -EIO;
662
663 return 0;
664}
665
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300666/*************************************************************************
667 * ACPI device model
668 */
669
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200670#define TPACPI_ACPIHANDLE_INIT(object) \
671 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300672 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200673
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300674static void __init drv_acpi_handle_init(const char *name,
675 acpi_handle *handle, const acpi_handle parent,
676 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300677{
678 int i;
679 acpi_status status;
680
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300681 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
682 name);
683
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300684 for (i = 0; i < num_paths; i++) {
685 status = acpi_get_handle(parent, paths[i], handle);
686 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300687 dbg_printk(TPACPI_DBG_INIT,
688 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300689 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300690 return;
691 }
692 }
693
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300694 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
695 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300696 *handle = NULL;
697}
698
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300699static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
700 u32 level, void *context, void **return_value)
701{
Aaron Lu46445b62013-10-11 21:27:46 +0800702 struct acpi_device *dev;
703 if (!strcmp(context, "video")) {
704 if (acpi_bus_get_device(handle, &dev))
705 return AE_OK;
706 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
707 return AE_OK;
708 }
709
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300710 *(acpi_handle *)return_value = handle;
711
712 return AE_CTRL_TERMINATE;
713}
714
715static void __init tpacpi_acpi_handle_locate(const char *name,
716 const char *hid,
717 acpi_handle *handle)
718{
719 acpi_status status;
720 acpi_handle device_found;
721
Aaron Lu46445b62013-10-11 21:27:46 +0800722 BUG_ON(!name || !handle);
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300723 vdbg_printk(TPACPI_DBG_INIT,
724 "trying to locate ACPI handle for %s, using HID %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +0800725 name, hid ? hid : "NULL");
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300726
727 memset(&device_found, 0, sizeof(device_found));
728 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
729 (void *)name, &device_found);
730
731 *handle = NULL;
732
733 if (ACPI_SUCCESS(status)) {
734 *handle = device_found;
735 dbg_printk(TPACPI_DBG_INIT,
736 "Found ACPI handle for %s\n", name);
737 } else {
738 vdbg_printk(TPACPI_DBG_INIT,
739 "Could not locate an ACPI handle for %s: %s\n",
740 name, acpi_format_exception(status));
741 }
742}
743
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300744static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300745{
746 struct ibm_struct *ibm = data;
747
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300748 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
749 return;
750
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300751 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300752 return;
753
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300754 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300755}
756
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300757static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300758{
759 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300760 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300761
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300762 BUG_ON(!ibm->acpi);
763
764 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300765 return 0;
766
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300767 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300768 "setting up ACPI notify for %s\n", ibm->name);
769
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300770 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
771 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700772 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300773 return -ENODEV;
774 }
775
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700776 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300777 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200778 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300779 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300780
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300781 status = acpi_install_notify_handler(*ibm->acpi->handle,
782 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300783 if (ACPI_FAILURE(status)) {
784 if (status == AE_ALREADY_EXISTS) {
Joe Perches0978e012011-04-04 10:06:25 -0700785 pr_notice("another device driver is already "
786 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300787 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700788 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300789 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300790 }
791 return -ENODEV;
792 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300793 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300794 return 0;
795}
796
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300797static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300798{
799 return 0;
800}
801
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300802static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300803{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300804 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300805
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300806 dbg_printk(TPACPI_DBG_INIT,
807 "registering %s as an ACPI driver\n", ibm->name);
808
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300809 BUG_ON(!ibm->acpi);
810
811 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
812 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700813 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300814 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300815 }
816
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200817 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300818 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200819
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300820 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300821
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300822 rc = acpi_bus_register_driver(ibm->acpi->driver);
823 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700824 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200825 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300826 kfree(ibm->acpi->driver);
827 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300828 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300829 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300830
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300831 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300832}
833
834
835/****************************************************************************
836 ****************************************************************************
837 *
838 * Procfs Helpers
839 *
840 ****************************************************************************
841 ****************************************************************************/
842
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200843static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300844{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200845 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300846
847 if (!ibm || !ibm->read)
848 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200849 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300850}
851
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200852static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300853{
Al Virod9dda782013-03-31 18:16:14 -0400854 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200855}
856
857static ssize_t dispatch_proc_write(struct file *file,
858 const char __user *userbuf,
859 size_t count, loff_t *pos)
860{
Al Virod9dda782013-03-31 18:16:14 -0400861 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300862 char *kernbuf;
863 int ret;
864
865 if (!ibm || !ibm->write)
866 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300867 if (count > PAGE_SIZE - 2)
868 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300869
870 kernbuf = kmalloc(count + 2, GFP_KERNEL);
871 if (!kernbuf)
872 return -ENOMEM;
873
874 if (copy_from_user(kernbuf, userbuf, count)) {
875 kfree(kernbuf);
876 return -EFAULT;
877 }
878
879 kernbuf[count] = 0;
880 strcat(kernbuf, ",");
881 ret = ibm->write(kernbuf);
882 if (ret == 0)
883 ret = count;
884
885 kfree(kernbuf);
886
887 return ret;
888}
889
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200890static const struct file_operations dispatch_proc_fops = {
891 .owner = THIS_MODULE,
892 .open = dispatch_proc_open,
893 .read = seq_read,
894 .llseek = seq_lseek,
895 .release = single_release,
896 .write = dispatch_proc_write,
897};
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899static char *next_cmd(char **cmds)
900{
901 char *start = *cmds;
902 char *end;
903
904 while ((end = strchr(start, ',')) && end == start)
905 start = end + 1;
906
907 if (!end)
908 return NULL;
909
910 *end = 0;
911 *cmds = end + 1;
912 return start;
913}
914
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300915
916/****************************************************************************
917 ****************************************************************************
918 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300919 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300920 *
921 ****************************************************************************
922 ****************************************************************************/
923
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300924static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300925static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700926static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300927static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300928static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200929static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300930
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +0200931#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200932static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200933{
934 struct ibm_struct *ibm, *itmp;
935
936 list_for_each_entry_safe(ibm, itmp,
937 &tpacpi_all_drivers,
938 all_drivers) {
939 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200940 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200941 }
942
943 return 0;
944}
945
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200946static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300947{
948 struct ibm_struct *ibm, *itmp;
949
950 list_for_each_entry_safe(ibm, itmp,
951 &tpacpi_all_drivers,
952 all_drivers) {
953 if (ibm->resume)
954 (ibm->resume)();
955 }
956
957 return 0;
958}
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +0200959#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300960
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200961static SIMPLE_DEV_PM_OPS(tpacpi_pm,
962 tpacpi_suspend_handler, tpacpi_resume_handler);
963
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200964static void tpacpi_shutdown_handler(struct platform_device *pdev)
965{
966 struct ibm_struct *ibm, *itmp;
967
968 list_for_each_entry_safe(ibm, itmp,
969 &tpacpi_all_drivers,
970 all_drivers) {
971 if (ibm->shutdown)
972 (ibm->shutdown)();
973 }
974}
975
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300976static struct platform_driver tpacpi_pdriver = {
977 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200978 .name = TPACPI_DRVR_NAME,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200979 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300980 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200981 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300982};
983
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300984static struct platform_driver tpacpi_hwmon_pdriver = {
985 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200986 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300987 },
988};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300989
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300990/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300991 * sysfs support helpers
992 */
993
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200994struct attribute_set {
995 unsigned int members, max_members;
996 struct attribute_group group;
997};
998
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300999struct attribute_set_obj {
1000 struct attribute_set s;
1001 struct attribute *a;
1002} __attribute__((packed));
1003
1004static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001005 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001006{
1007 struct attribute_set_obj *sobj;
1008
1009 if (max_members == 0)
1010 return NULL;
1011
1012 /* Allocates space for implicit NULL at the end too */
1013 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1014 max_members * sizeof(struct attribute *),
1015 GFP_KERNEL);
1016 if (!sobj)
1017 return NULL;
1018 sobj->s.max_members = max_members;
1019 sobj->s.group.attrs = &sobj->a;
1020 sobj->s.group.name = name;
1021
1022 return &sobj->s;
1023}
1024
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001025#define destroy_attr_set(_set) \
1026 kfree(_set);
1027
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001028/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001029static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001030{
1031 if (!s || !attr)
1032 return -EINVAL;
1033
1034 if (s->members >= s->max_members)
1035 return -ENOMEM;
1036
1037 s->group.attrs[s->members] = attr;
1038 s->members++;
1039
1040 return 0;
1041}
1042
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001043static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001044 struct attribute **attr,
1045 unsigned int count)
1046{
1047 int i, res;
1048
1049 for (i = 0; i < count; i++) {
1050 res = add_to_attr_set(s, attr[i]);
1051 if (res)
1052 return res;
1053 }
1054
1055 return 0;
1056}
1057
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001058static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001059{
1060 sysfs_remove_group(kobj, &s->group);
1061 destroy_attr_set(s);
1062}
1063
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001064#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1065 sysfs_create_group(_kobj, &_attr_set->group)
1066
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001067static int parse_strtoul(const char *buf,
1068 unsigned long max, unsigned long *value)
1069{
1070 char *endp;
1071
André Goddard Rosae7d28602009-12-14 18:01:06 -08001072 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1073 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001074 if (*endp || *value > max)
1075 return -EINVAL;
1076
1077 return 0;
1078}
1079
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001080static void tpacpi_disable_brightness_delay(void)
1081{
1082 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001083 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001084}
1085
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001086static void printk_deprecated_attribute(const char * const what,
1087 const char * const details)
1088{
1089 tpacpi_log_usertask("deprecated sysfs attribute");
Joe Perches0978e012011-04-04 10:06:25 -07001090 pr_warn("WARNING: sysfs attribute %s is deprecated and "
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001091 "will be removed. %s\n",
1092 what, details);
1093}
1094
Johannes Berg19d337d2009-06-02 13:01:37 +02001095/*************************************************************************
1096 * rfkill and radio control support helpers
1097 */
1098
1099/*
1100 * ThinkPad-ACPI firmware handling model:
1101 *
1102 * WLSW (master wireless switch) is event-driven, and is common to all
1103 * firmware-controlled radios. It cannot be controlled, just monitored,
1104 * as expected. It overrides all radio state in firmware
1105 *
1106 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1107 * (TODO: verify how WLSW interacts with the returned radio state).
1108 *
1109 * The only time there are shadow radio state changes, is when
1110 * masked-off hotkeys are used.
1111 */
1112
1113/*
1114 * Internal driver API for radio state:
1115 *
1116 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1117 * bool: true means radio blocked (off)
1118 */
1119enum tpacpi_rfkill_state {
1120 TPACPI_RFK_RADIO_OFF = 0,
1121 TPACPI_RFK_RADIO_ON
1122};
1123
1124/* rfkill switches */
1125enum tpacpi_rfk_id {
1126 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1127 TPACPI_RFK_WWAN_SW_ID,
1128 TPACPI_RFK_UWB_SW_ID,
1129 TPACPI_RFK_SW_MAX
1130};
1131
1132static const char *tpacpi_rfkill_names[] = {
1133 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1134 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1135 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1136 [TPACPI_RFK_SW_MAX] = NULL
1137};
1138
1139/* ThinkPad-ACPI rfkill subdriver */
1140struct tpacpi_rfk {
1141 struct rfkill *rfkill;
1142 enum tpacpi_rfk_id id;
1143 const struct tpacpi_rfk_ops *ops;
1144};
1145
1146struct tpacpi_rfk_ops {
1147 /* firmware interface */
1148 int (*get_status)(void);
1149 int (*set_status)(const enum tpacpi_rfkill_state);
1150};
1151
1152static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1153
1154/* Query FW and update rfkill sw state for a given rfkill switch */
1155static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1156{
1157 int status;
1158
1159 if (!tp_rfk)
1160 return -ENODEV;
1161
1162 status = (tp_rfk->ops->get_status)();
1163 if (status < 0)
1164 return status;
1165
1166 rfkill_set_sw_state(tp_rfk->rfkill,
1167 (status == TPACPI_RFK_RADIO_OFF));
1168
1169 return status;
1170}
1171
1172/* Query FW and update rfkill sw state for all rfkill switches */
1173static void tpacpi_rfk_update_swstate_all(void)
1174{
1175 unsigned int i;
1176
1177 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1178 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1179}
1180
1181/*
1182 * Sync the HW-blocking state of all rfkill switches,
1183 * do notice it causes the rfkill core to schedule uevents
1184 */
1185static void tpacpi_rfk_update_hwblock_state(bool blocked)
1186{
1187 unsigned int i;
1188 struct tpacpi_rfk *tp_rfk;
1189
1190 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1191 tp_rfk = tpacpi_rfkill_switches[i];
1192 if (tp_rfk) {
1193 if (rfkill_set_hw_state(tp_rfk->rfkill,
1194 blocked)) {
1195 /* ignore -- we track sw block */
1196 }
1197 }
1198 }
1199}
1200
1201/* Call to get the WLSW state from the firmware */
1202static int hotkey_get_wlsw(void);
1203
1204/* Call to query WLSW state and update all rfkill switches */
1205static bool tpacpi_rfk_check_hwblock_state(void)
1206{
1207 int res = hotkey_get_wlsw();
1208 int hw_blocked;
1209
1210 /* When unknown or unsupported, we have to assume it is unblocked */
1211 if (res < 0)
1212 return false;
1213
1214 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1215 tpacpi_rfk_update_hwblock_state(hw_blocked);
1216
1217 return hw_blocked;
1218}
1219
1220static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1221{
1222 struct tpacpi_rfk *tp_rfk = data;
1223 int res;
1224
1225 dbg_printk(TPACPI_DBG_RFKILL,
1226 "request to change radio state to %s\n",
1227 blocked ? "blocked" : "unblocked");
1228
1229 /* try to set radio state */
1230 res = (tp_rfk->ops->set_status)(blocked ?
1231 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1232
1233 /* and update the rfkill core with whatever the FW really did */
1234 tpacpi_rfk_update_swstate(tp_rfk);
1235
1236 return (res < 0) ? res : 0;
1237}
1238
1239static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1240 .set_block = tpacpi_rfk_hook_set_block,
1241};
1242
1243static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1244 const struct tpacpi_rfk_ops *tp_rfkops,
1245 const enum rfkill_type rfktype,
1246 const char *name,
1247 const bool set_default)
1248{
1249 struct tpacpi_rfk *atp_rfk;
1250 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001251 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001252 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001253 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001254
1255 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1256
Johannes Berg19d337d2009-06-02 13:01:37 +02001257 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1258 if (atp_rfk)
1259 atp_rfk->rfkill = rfkill_alloc(name,
1260 &tpacpi_pdev->dev,
1261 rfktype,
1262 &tpacpi_rfk_rfkill_ops,
1263 atp_rfk);
1264 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001265 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001266 kfree(atp_rfk);
1267 return -ENOMEM;
1268 }
1269
1270 atp_rfk->id = id;
1271 atp_rfk->ops = tp_rfkops;
1272
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001273 sw_status = (tp_rfkops->get_status)();
1274 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001275 pr_err("failed to read initial state for %s, error %d\n",
1276 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001277 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001278 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001279 if (set_default) {
1280 /* try to keep the initial state, since we ask the
1281 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001282 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001283 }
1284 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001285 hw_state = tpacpi_rfk_check_hwblock_state();
1286 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001287
1288 res = rfkill_register(atp_rfk->rfkill);
1289 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001290 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001291 rfkill_destroy(atp_rfk->rfkill);
1292 kfree(atp_rfk);
1293 return res;
1294 }
1295
1296 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001297
Joe Perches0978e012011-04-04 10:06:25 -07001298 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001299 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001300 return 0;
1301}
1302
1303static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1304{
1305 struct tpacpi_rfk *tp_rfk;
1306
1307 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1308
1309 tp_rfk = tpacpi_rfkill_switches[id];
1310 if (tp_rfk) {
1311 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001312 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001313 tpacpi_rfkill_switches[id] = NULL;
1314 kfree(tp_rfk);
1315 }
1316}
1317
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001318static void printk_deprecated_rfkill_attribute(const char * const what)
1319{
1320 printk_deprecated_attribute(what,
1321 "Please switch to generic rfkill before year 2010");
1322}
1323
Johannes Berg19d337d2009-06-02 13:01:37 +02001324/* sysfs <radio> enable ------------------------------------------------ */
1325static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1326 struct device_attribute *attr,
1327 char *buf)
1328{
1329 int status;
1330
1331 printk_deprecated_rfkill_attribute(attr->attr.name);
1332
1333 /* This is in the ABI... */
1334 if (tpacpi_rfk_check_hwblock_state()) {
1335 status = TPACPI_RFK_RADIO_OFF;
1336 } else {
1337 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1338 if (status < 0)
1339 return status;
1340 }
1341
1342 return snprintf(buf, PAGE_SIZE, "%d\n",
1343 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1344}
1345
1346static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1347 struct device_attribute *attr,
1348 const char *buf, size_t count)
1349{
1350 unsigned long t;
1351 int res;
1352
1353 printk_deprecated_rfkill_attribute(attr->attr.name);
1354
1355 if (parse_strtoul(buf, 1, &t))
1356 return -EINVAL;
1357
1358 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1359
1360 /* This is in the ABI... */
1361 if (tpacpi_rfk_check_hwblock_state() && !!t)
1362 return -EPERM;
1363
1364 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1365 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1366 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1367
1368 return (res < 0) ? res : count;
1369}
1370
1371/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001372static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001373{
Johannes Berg19d337d2009-06-02 13:01:37 +02001374 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001375 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001376 else {
1377 int status;
1378
1379 /* This is in the ABI... */
1380 if (tpacpi_rfk_check_hwblock_state()) {
1381 status = TPACPI_RFK_RADIO_OFF;
1382 } else {
1383 status = tpacpi_rfk_update_swstate(
1384 tpacpi_rfkill_switches[id]);
1385 if (status < 0)
1386 return status;
1387 }
1388
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001389 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001390 (status == TPACPI_RFK_RADIO_ON) ?
1391 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001392 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001393 }
1394
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001395 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001396}
1397
1398static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1399{
1400 char *cmd;
1401 int status = -1;
1402 int res = 0;
1403
1404 if (id >= TPACPI_RFK_SW_MAX)
1405 return -ENODEV;
1406
1407 while ((cmd = next_cmd(&buf))) {
1408 if (strlencmp(cmd, "enable") == 0)
1409 status = TPACPI_RFK_RADIO_ON;
1410 else if (strlencmp(cmd, "disable") == 0)
1411 status = TPACPI_RFK_RADIO_OFF;
1412 else
1413 return -EINVAL;
1414 }
1415
1416 if (status != -1) {
1417 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1418 (status == TPACPI_RFK_RADIO_ON) ?
1419 "enable" : "disable",
1420 tpacpi_rfkill_names[id]);
1421 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1422 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1423 }
1424
1425 return res;
1426}
1427
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001428/*************************************************************************
1429 * thinkpad-acpi driver attributes
1430 */
1431
1432/* interface_version --------------------------------------------------- */
1433static ssize_t tpacpi_driver_interface_version_show(
1434 struct device_driver *drv,
1435 char *buf)
1436{
1437 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1438}
1439
1440static DRIVER_ATTR(interface_version, S_IRUGO,
1441 tpacpi_driver_interface_version_show, NULL);
1442
1443/* debug_level --------------------------------------------------------- */
1444static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1445 char *buf)
1446{
1447 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1448}
1449
1450static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1451 const char *buf, size_t count)
1452{
1453 unsigned long t;
1454
1455 if (parse_strtoul(buf, 0xffff, &t))
1456 return -EINVAL;
1457
1458 dbg_level = t;
1459
1460 return count;
1461}
1462
1463static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1464 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1465
1466/* version ------------------------------------------------------------- */
1467static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1468 char *buf)
1469{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001470 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1471 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001472}
1473
1474static DRIVER_ATTR(version, S_IRUGO,
1475 tpacpi_driver_version_show, NULL);
1476
1477/* --------------------------------------------------------------------- */
1478
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001479#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1480
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001481/* wlsw_emulstate ------------------------------------------------------ */
1482static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1483 char *buf)
1484{
1485 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1486}
1487
1488static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1489 const char *buf, size_t count)
1490{
1491 unsigned long t;
1492
1493 if (parse_strtoul(buf, 1, &t))
1494 return -EINVAL;
1495
Johannes Berg19d337d2009-06-02 13:01:37 +02001496 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001497 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001498 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1499 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001500
1501 return count;
1502}
1503
1504static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1505 tpacpi_driver_wlsw_emulstate_show,
1506 tpacpi_driver_wlsw_emulstate_store);
1507
1508/* bluetooth_emulstate ------------------------------------------------- */
1509static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1510 struct device_driver *drv,
1511 char *buf)
1512{
1513 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1514}
1515
1516static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1517 struct device_driver *drv,
1518 const char *buf, size_t count)
1519{
1520 unsigned long t;
1521
1522 if (parse_strtoul(buf, 1, &t))
1523 return -EINVAL;
1524
1525 tpacpi_bluetooth_emulstate = !!t;
1526
1527 return count;
1528}
1529
1530static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1531 tpacpi_driver_bluetooth_emulstate_show,
1532 tpacpi_driver_bluetooth_emulstate_store);
1533
1534/* wwan_emulstate ------------------------------------------------- */
1535static ssize_t tpacpi_driver_wwan_emulstate_show(
1536 struct device_driver *drv,
1537 char *buf)
1538{
1539 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1540}
1541
1542static ssize_t tpacpi_driver_wwan_emulstate_store(
1543 struct device_driver *drv,
1544 const char *buf, size_t count)
1545{
1546 unsigned long t;
1547
1548 if (parse_strtoul(buf, 1, &t))
1549 return -EINVAL;
1550
1551 tpacpi_wwan_emulstate = !!t;
1552
1553 return count;
1554}
1555
1556static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1557 tpacpi_driver_wwan_emulstate_show,
1558 tpacpi_driver_wwan_emulstate_store);
1559
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001560/* uwb_emulstate ------------------------------------------------- */
1561static ssize_t tpacpi_driver_uwb_emulstate_show(
1562 struct device_driver *drv,
1563 char *buf)
1564{
1565 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1566}
1567
1568static ssize_t tpacpi_driver_uwb_emulstate_store(
1569 struct device_driver *drv,
1570 const char *buf, size_t count)
1571{
1572 unsigned long t;
1573
1574 if (parse_strtoul(buf, 1, &t))
1575 return -EINVAL;
1576
1577 tpacpi_uwb_emulstate = !!t;
1578
1579 return count;
1580}
1581
1582static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1583 tpacpi_driver_uwb_emulstate_show,
1584 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001585#endif
1586
1587/* --------------------------------------------------------------------- */
1588
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001589static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001590 &driver_attr_debug_level, &driver_attr_version,
1591 &driver_attr_interface_version,
1592};
1593
1594static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1595{
1596 int i, res;
1597
1598 i = 0;
1599 res = 0;
1600 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1601 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1602 i++;
1603 }
1604
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001605#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1606 if (!res && dbg_wlswemul)
1607 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1608 if (!res && dbg_bluetoothemul)
1609 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1610 if (!res && dbg_wwanemul)
1611 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001612 if (!res && dbg_uwbemul)
1613 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001614#endif
1615
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001616 return res;
1617}
1618
1619static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1620{
1621 int i;
1622
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001623 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001624 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001625
1626#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1627 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1628 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1629 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001630 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001631#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001632}
1633
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001634/*************************************************************************
1635 * Firmware Data
1636 */
1637
1638/*
1639 * Table of recommended minimum BIOS versions
1640 *
1641 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001642 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001643 * bugs and bad ACPI behaviour on older versions
1644 *
1645 * 2. BIOS or EC fw with known bugs that trigger on Linux
1646 *
1647 * 3. BIOS with known reduced functionality in older versions
1648 *
1649 * We recommend the latest BIOS and EC version.
1650 * We only support the latest BIOS and EC fw version as a rule.
1651 *
1652 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1653 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001654 *
1655 * WARNING: we use this table also to detect that the machine is
1656 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001657 */
1658
1659#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1660 { .vendor = (__v), \
1661 .bios = TPID(__id1, __id2), \
1662 .ec = TPACPI_MATCH_ANY, \
1663 .quirks = TPACPI_MATCH_ANY << 16 \
1664 | (__bv1) << 8 | (__bv2) }
1665
1666#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001667 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001668 { .vendor = (__v), \
1669 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001670 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001671 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1672 | (__bv1) << 8 | (__bv2) }
1673
1674#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1675 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1676
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001677/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001678#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1679 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001680 __bv1, __bv2, TPID(__id1, __id2), \
1681 __ev1, __ev2), \
1682 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1683 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1684 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001685
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001686/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001687#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1688 __eid1, __eid2, __ev1, __ev2) \
1689 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001690 __bv1, __bv2, TPID(__eid1, __eid2), \
1691 __ev1, __ev2), \
1692 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1693 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1694 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001695
1696#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1697 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1698
1699#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1700 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001701 __bv1, __bv2, TPID(__id1, __id2), \
1702 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001703
1704#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1705 __eid1, __eid2, __ev1, __ev2) \
1706 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001707 __bv1, __bv2, TPID(__eid1, __eid2), \
1708 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001709
1710static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1711 /* Numeric models ------------------ */
1712 /* FW MODEL BIOS VERS */
1713 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1714 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1715 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1716 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1717 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1718 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1719 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1720 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1721 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1722
1723 /* A-series ------------------------- */
1724 /* FW MODEL BIOS VERS EC VERS */
1725 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1726 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1727 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1728 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1729 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1730 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1731 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1732 TPV_QI0('1', '3', '2', '0'), /* A22m */
1733 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1734 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1735 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1736
1737 /* G-series ------------------------- */
1738 /* FW MODEL BIOS VERS */
1739 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1740 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1741
1742 /* R-series, T-series --------------- */
1743 /* FW MODEL BIOS VERS EC VERS */
1744 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1745 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1746 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1747 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1748 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1749 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1750 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1751 T40/p, T41/p, T42/p (1) */
1752 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1753 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1754 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1755 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1756
1757 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1758 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1759 TPV_QI0('1', '6', '3', '2'), /* T22 */
1760 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1761 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1762 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1763
1764 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1765 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001766 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001767
1768 /* BIOS FW BIOS VERS EC FW EC VERS */
1769 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1770 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1771
1772 /* X-series ------------------------- */
1773 /* FW MODEL BIOS VERS EC VERS */
1774 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1775 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1776 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1777 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1778 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1779 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1780 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1781
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001782 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1783 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001784
1785 /* (0) - older versions lack DMI EC fw string and functionality */
1786 /* (1) - older versions known to lack functionality */
1787};
1788
1789#undef TPV_QL1
1790#undef TPV_QL0
1791#undef TPV_QI2
1792#undef TPV_QI1
1793#undef TPV_QI0
1794#undef TPV_Q_X
1795#undef TPV_Q
1796
1797static void __init tpacpi_check_outdated_fw(void)
1798{
1799 unsigned long fwvers;
1800 u16 ec_version, bios_version;
1801
1802 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1803 ARRAY_SIZE(tpacpi_bios_version_qtable));
1804
1805 if (!fwvers)
1806 return;
1807
1808 bios_version = fwvers & 0xffffU;
1809 ec_version = (fwvers >> 16) & 0xffffU;
1810
1811 /* note that unknown versions are set to 0x0000 and we use that */
1812 if ((bios_version > thinkpad_id.bios_release) ||
1813 (ec_version > thinkpad_id.ec_release &&
1814 ec_version != TPACPI_MATCH_ANY)) {
1815 /*
1816 * The changelogs would let us track down the exact
1817 * reason, but it is just too much of a pain to track
1818 * it. We only list BIOSes that are either really
1819 * broken, or really stable to begin with, so it is
1820 * best if the user upgrades the firmware anyway.
1821 */
Joe Perches0978e012011-04-04 10:06:25 -07001822 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1823 pr_warn("WARNING: This firmware may be missing critical bug "
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001824 "fixes and/or important features\n");
1825 }
1826}
1827
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001828static bool __init tpacpi_is_fw_known(void)
1829{
1830 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1831 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1832}
1833
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001834/****************************************************************************
1835 ****************************************************************************
1836 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001837 * Subdrivers
1838 *
1839 ****************************************************************************
1840 ****************************************************************************/
1841
1842/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001843 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001844 */
1845
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001846static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001848 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1849 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1850 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001853static struct ibm_struct thinkpad_acpi_driver_data = {
1854 .name = "driver",
1855 .read = thinkpad_acpi_driver_read,
1856};
1857
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001858/*************************************************************************
1859 * Hotkey subdriver
1860 */
1861
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001862/*
1863 * ThinkPad firmware event model
1864 *
1865 * The ThinkPad firmware has two main event interfaces: normal ACPI
1866 * notifications (which follow the ACPI standard), and a private event
1867 * interface.
1868 *
1869 * The private event interface also issues events for the hotkeys. As
1870 * the driver gained features, the event handling code ended up being
1871 * built around the hotkey subdriver. This will need to be refactored
1872 * to a more formal event API eventually.
1873 *
1874 * Some "hotkeys" are actually supposed to be used as event reports,
1875 * such as "brightness has changed", "volume has changed", depending on
1876 * the ThinkPad model and how the firmware is operating.
1877 *
1878 * Unlike other classes, hotkey-class events have mask/unmask control on
1879 * non-ancient firmware. However, how it behaves changes a lot with the
1880 * firmware model and version.
1881 */
1882
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001883enum { /* hot key scan codes (derived from ACPI DSDT) */
1884 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1885 TP_ACPI_HOTKEYSCAN_FNF2,
1886 TP_ACPI_HOTKEYSCAN_FNF3,
1887 TP_ACPI_HOTKEYSCAN_FNF4,
1888 TP_ACPI_HOTKEYSCAN_FNF5,
1889 TP_ACPI_HOTKEYSCAN_FNF6,
1890 TP_ACPI_HOTKEYSCAN_FNF7,
1891 TP_ACPI_HOTKEYSCAN_FNF8,
1892 TP_ACPI_HOTKEYSCAN_FNF9,
1893 TP_ACPI_HOTKEYSCAN_FNF10,
1894 TP_ACPI_HOTKEYSCAN_FNF11,
1895 TP_ACPI_HOTKEYSCAN_FNF12,
1896 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1897 TP_ACPI_HOTKEYSCAN_FNINSERT,
1898 TP_ACPI_HOTKEYSCAN_FNDELETE,
1899 TP_ACPI_HOTKEYSCAN_FNHOME,
1900 TP_ACPI_HOTKEYSCAN_FNEND,
1901 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1902 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1903 TP_ACPI_HOTKEYSCAN_FNSPACE,
1904 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1905 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1906 TP_ACPI_HOTKEYSCAN_MUTE,
1907 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001908 TP_ACPI_HOTKEYSCAN_UNK1,
1909 TP_ACPI_HOTKEYSCAN_UNK2,
1910 TP_ACPI_HOTKEYSCAN_UNK3,
1911 TP_ACPI_HOTKEYSCAN_UNK4,
1912 TP_ACPI_HOTKEYSCAN_UNK5,
1913 TP_ACPI_HOTKEYSCAN_UNK6,
1914 TP_ACPI_HOTKEYSCAN_UNK7,
1915 TP_ACPI_HOTKEYSCAN_UNK8,
1916
Bastien Nocera6a68d852015-03-02 14:45:31 +01001917 TP_ACPI_HOTKEYSCAN_MUTE2,
1918 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1919 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1920 TP_ACPI_HOTKEYSCAN_CLOUD,
1921 TP_ACPI_HOTKEYSCAN_UNK9,
1922 TP_ACPI_HOTKEYSCAN_VOICE,
1923 TP_ACPI_HOTKEYSCAN_UNK10,
1924 TP_ACPI_HOTKEYSCAN_GESTURES,
1925 TP_ACPI_HOTKEYSCAN_UNK11,
1926 TP_ACPI_HOTKEYSCAN_UNK12,
1927 TP_ACPI_HOTKEYSCAN_UNK13,
1928 TP_ACPI_HOTKEYSCAN_CONFIG,
1929 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1930 TP_ACPI_HOTKEYSCAN_RELOAD,
1931 TP_ACPI_HOTKEYSCAN_BACK,
1932 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1933 TP_ACPI_HOTKEYSCAN_MIC_UP,
1934 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1935 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1936 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1937
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001938 /* Hotkey keymap size */
1939 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001940};
1941
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001942enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001943 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1944 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1945};
1946
1947enum { /* Positions of some of the keys in hotkey masks */
1948 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1949 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1950 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1951 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1952 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1953 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1954 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1955 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1956 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1957 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1958 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1959};
1960
1961enum { /* NVRAM to ACPI HKEY group map */
1962 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1963 TP_ACPI_HKEY_ZOOM_MASK |
1964 TP_ACPI_HKEY_DISPSWTCH_MASK |
1965 TP_ACPI_HKEY_HIBERNATE_MASK,
1966 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1967 TP_ACPI_HKEY_BRGHTDWN_MASK,
1968 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1969 TP_ACPI_HKEY_VOLDWN_MASK |
1970 TP_ACPI_HKEY_MUTE_MASK,
1971};
1972
1973#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1974struct tp_nvram_state {
1975 u16 thinkpad_toggle:1;
1976 u16 zoom_toggle:1;
1977 u16 display_toggle:1;
1978 u16 thinklight_toggle:1;
1979 u16 hibernate_toggle:1;
1980 u16 displayexp_toggle:1;
1981 u16 display_state:1;
1982 u16 brightness_toggle:1;
1983 u16 volume_toggle:1;
1984 u16 mute:1;
1985
1986 u8 brightness_level;
1987 u8 volume_level;
1988};
1989
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001990/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001991static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001992
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001993/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001994 * Acquire mutex to write poller control variables as an
1995 * atomic block.
1996 *
1997 * Increment hotkey_config_change when changing them if you
1998 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001999 *
2000 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2001 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002002static struct mutex hotkey_thread_data_mutex;
2003static unsigned int hotkey_config_change;
2004
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002005/*
2006 * hotkey poller control variables
2007 *
2008 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002009 *
2010 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2011 * should be used only when the changes need to be taken as
2012 * a block, OR when one needs to force the kthread to forget
2013 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002014 */
2015static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2016static unsigned int hotkey_poll_freq = 10; /* Hz */
2017
2018#define HOTKEY_CONFIG_CRITICAL_START \
2019 do { \
2020 mutex_lock(&hotkey_thread_data_mutex); \
2021 hotkey_config_change++; \
2022 } while (0);
2023#define HOTKEY_CONFIG_CRITICAL_END \
2024 mutex_unlock(&hotkey_thread_data_mutex);
2025
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002026#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2027
2028#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002029#define HOTKEY_CONFIG_CRITICAL_START
2030#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002031
2032#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2033
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002034static struct mutex hotkey_mutex;
2035
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002036static enum { /* Reasons for waking up */
2037 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2038 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2039 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2040} hotkey_wakeup_reason;
2041
2042static int hotkey_autosleep_ack;
2043
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002044static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2045static u32 hotkey_all_mask; /* all events supported in fw */
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002046static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002047static u32 hotkey_reserved_mask; /* events better left disabled */
2048static u32 hotkey_driver_mask; /* events needed by the driver */
2049static u32 hotkey_user_mask; /* events visible to userspace */
2050static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002051
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002052static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002053
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03002054static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002055
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002056static void tpacpi_driver_event(const unsigned int hkey_event);
2057static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002058static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002059
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002060/* HKEY.MHKG() return bits */
2061#define TP_HOTKEY_TABLET_MASK (1 << 3)
2062
Johannes Berg19d337d2009-06-02 13:01:37 +02002063static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002064{
Johannes Berg19d337d2009-06-02 13:01:37 +02002065 int status;
2066
2067 if (!tp_features.hotkey_wlsw)
2068 return -ENODEV;
2069
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002070#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002071 if (dbg_wlswemul)
2072 return (tpacpi_wlsw_emulstate) ?
2073 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002074#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002075
2076 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002077 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002078
2079 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002080}
2081
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002082static int hotkey_get_tablet_mode(int *status)
2083{
2084 int s;
2085
2086 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2087 return -EIO;
2088
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002089 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2090 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002091}
2092
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002093/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002094 * Reads current event mask from firmware, and updates
2095 * hotkey_acpi_mask accordingly. Also resets any bits
2096 * from hotkey_user_mask that are unavailable to be
2097 * delivered (shadow requirement of the userspace ABI).
2098 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002099 * Call with hotkey_mutex held
2100 */
2101static int hotkey_mask_get(void)
2102{
2103 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002104 u32 m = 0;
2105
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002106 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002107 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002108
2109 hotkey_acpi_mask = m;
2110 } else {
2111 /* no mask support doesn't mean no event support... */
2112 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002113 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002114
2115 /* sync userspace-visible mask */
2116 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002117
2118 return 0;
2119}
2120
Jean Delvaredf6dd1b2015-04-27 09:45:06 +02002121static void hotkey_mask_warn_incomplete_mask(void)
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002122{
2123 /* log only what the user can fix... */
2124 const u32 wantedmask = hotkey_driver_mask &
2125 ~(hotkey_acpi_mask | hotkey_source_mask) &
2126 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2127
2128 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002129 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002130}
2131
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002132/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002133 * Set the firmware mask when supported
2134 *
2135 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2136 *
2137 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2138 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002139 * Call with hotkey_mutex held
2140 */
2141static int hotkey_mask_set(u32 mask)
2142{
2143 int i;
2144 int rc = 0;
2145
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002146 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002147
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002148 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002149 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002150 if (!acpi_evalf(hkey_handle,
2151 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002152 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002153 rc = -EIO;
2154 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002155 }
2156 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002157 }
2158
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002159 /*
2160 * We *must* make an inconditional call to hotkey_mask_get to
2161 * refresh hotkey_acpi_mask and update hotkey_user_mask
2162 *
2163 * Take the opportunity to also log when we cannot _enable_
2164 * a given event.
2165 */
2166 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Joe Perches0978e012011-04-04 10:06:25 -07002167 pr_notice("asked for hotkey mask 0x%08x, but "
2168 "firmware forced it to 0x%08x\n",
2169 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002170 }
2171
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002172 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2173 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002174
2175 return rc;
2176}
2177
2178/*
2179 * Sets hotkey_user_mask and tries to set the firmware mask
2180 *
2181 * Call with hotkey_mutex held
2182 */
2183static int hotkey_user_mask_set(const u32 mask)
2184{
2185 int rc;
2186
2187 /* Give people a chance to notice they are doing something that
2188 * is bound to go boom on their users sooner or later */
2189 if (!tp_warned.hotkey_mask_ff &&
2190 (mask == 0xffff || mask == 0xffffff ||
2191 mask == 0xffffffff)) {
2192 tp_warned.hotkey_mask_ff = 1;
Joe Perches0978e012011-04-04 10:06:25 -07002193 pr_notice("setting the hotkey mask to 0x%08x is likely "
2194 "not the best way to go about it\n", mask);
2195 pr_notice("please consider using the driver defaults, "
2196 "and refer to up-to-date thinkpad-acpi "
2197 "documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002198 }
2199
2200 /* Try to enable what the user asked for, plus whatever we need.
2201 * this syncs everything but won't enable bits in hotkey_user_mask */
2202 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2203
2204 /* Enable the available bits in hotkey_user_mask */
2205 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2206
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002207 return rc;
2208}
2209
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002210/*
2211 * Sets the driver hotkey mask.
2212 *
2213 * Can be called even if the hotkey subdriver is inactive
2214 */
2215static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2216{
2217 int rc;
2218
2219 /* Do the right thing if hotkey_init has not been called yet */
2220 if (!tp_features.hotkey) {
2221 hotkey_driver_mask = mask;
2222 return 0;
2223 }
2224
2225 mutex_lock(&hotkey_mutex);
2226
2227 HOTKEY_CONFIG_CRITICAL_START
2228 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002229#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002230 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002231#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002232 HOTKEY_CONFIG_CRITICAL_END
2233
2234 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2235 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002236 hotkey_poll_setup(true);
2237
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002238 mutex_unlock(&hotkey_mutex);
2239
2240 return rc;
2241}
2242
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002243static int hotkey_status_get(int *status)
2244{
2245 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2246 return -EIO;
2247
2248 return 0;
2249}
2250
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002251static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002252{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002253 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002254 return -EIO;
2255
2256 return 0;
2257}
2258
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002259static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002260{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002261 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002262
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002263 if (tp_features.hotkey_tablet &&
2264 !hotkey_get_tablet_mode(&state)) {
2265 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002266
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002267 input_report_switch(tpacpi_inputdev,
2268 SW_TABLET_MODE, !!state);
2269 input_sync(tpacpi_inputdev);
2270
2271 mutex_unlock(&tpacpi_inputdev_send_mutex);
2272 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002273}
2274
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002275/* Do NOT call without validating scancode first */
2276static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002277{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002278 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002279
2280 if (keycode != KEY_RESERVED) {
2281 mutex_lock(&tpacpi_inputdev_send_mutex);
2282
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002283 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002284 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002285 input_sync(tpacpi_inputdev);
2286
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002287 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002288 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002289 input_sync(tpacpi_inputdev);
2290
2291 mutex_unlock(&tpacpi_inputdev_send_mutex);
2292 }
2293}
2294
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002295/* Do NOT call without validating scancode first */
2296static void tpacpi_input_send_key_masked(const unsigned int scancode)
2297{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002298 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002299 if (hotkey_user_mask & (1 << scancode))
2300 tpacpi_input_send_key(scancode);
2301}
2302
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002303#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2304static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2305
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002306/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002307static void tpacpi_hotkey_send_key(unsigned int scancode)
2308{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002309 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002310}
2311
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002312static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002313{
2314 u8 d;
2315
2316 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2317 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2318 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2319 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2320 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2321 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2322 }
2323 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2324 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2325 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2326 }
2327 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2328 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2329 n->displayexp_toggle =
2330 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2331 }
2332 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2333 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2334 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2335 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2336 n->brightness_toggle =
2337 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2338 }
2339 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2340 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2341 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2342 >> TP_NVRAM_POS_LEVEL_VOLUME;
2343 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2344 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2345 }
2346}
2347
Behan Webstera4d44ba2014-02-12 21:58:46 +01002348#define TPACPI_COMPARE_KEY(__scancode, __member) \
2349do { \
2350 if ((event_mask & (1 << __scancode)) && \
2351 oldn->__member != newn->__member) \
2352 tpacpi_hotkey_send_key(__scancode); \
2353} while (0)
2354
2355#define TPACPI_MAY_SEND_KEY(__scancode) \
2356do { \
2357 if (event_mask & (1 << __scancode)) \
2358 tpacpi_hotkey_send_key(__scancode); \
2359} while (0)
2360
2361static void issue_volchange(const unsigned int oldvol,
2362 const unsigned int newvol,
2363 const u32 event_mask)
2364{
2365 unsigned int i = oldvol;
2366
2367 while (i > newvol) {
2368 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2369 i--;
2370 }
2371 while (i < newvol) {
2372 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2373 i++;
2374 }
2375}
2376
2377static void issue_brightnesschange(const unsigned int oldbrt,
2378 const unsigned int newbrt,
2379 const u32 event_mask)
2380{
2381 unsigned int i = oldbrt;
2382
2383 while (i > newbrt) {
2384 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2385 i--;
2386 }
2387 while (i < newbrt) {
2388 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2389 i++;
2390 }
2391}
2392
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002393static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2394 struct tp_nvram_state *newn,
2395 const u32 event_mask)
2396{
2397
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002398 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2399 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2400 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2401 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2402
2403 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2404
2405 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2406
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002407 /*
2408 * Handle volume
2409 *
2410 * This code is supposed to duplicate the IBM firmware behaviour:
2411 * - Pressing MUTE issues mute hotkey message, even when already mute
2412 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002413 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002414 * - The act of unmuting issues volume up/down notification,
2415 * depending which key was used to unmute
2416 *
2417 * We are constrained to what the NVRAM can tell us, which is not much
2418 * and certainly not enough if more than one volume hotkey was pressed
2419 * since the last poll cycle.
2420 *
2421 * Just to make our life interesting, some newer Lenovo ThinkPads have
2422 * bugs in the BIOS and may fail to update volume_toggle properly.
2423 */
2424 if (newn->mute) {
2425 /* muted */
2426 if (!oldn->mute ||
2427 oldn->volume_toggle != newn->volume_toggle ||
2428 oldn->volume_level != newn->volume_level) {
2429 /* recently muted, or repeated mute keypress, or
2430 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002431 issue_volchange(oldn->volume_level, newn->volume_level,
2432 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002433 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2434 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002435 } else {
2436 /* unmute */
2437 if (oldn->mute) {
2438 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002439 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002440 }
2441 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002442 issue_volchange(oldn->volume_level, newn->volume_level,
2443 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002444 } else if (oldn->volume_toggle != newn->volume_toggle) {
2445 /* repeated vol up/down keypress at end of scale ? */
2446 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002447 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002448 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2449 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002450 }
2451 }
2452
2453 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002454 if (oldn->brightness_level != newn->brightness_level) {
2455 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002456 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002457 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2458 /* repeated key presses that didn't change state */
2459 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002460 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002461 else if (newn->brightness_level >= bright_maxlvl
2462 && !tp_features.bright_unkfw)
2463 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002464 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002465
2466#undef TPACPI_COMPARE_KEY
2467#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002468}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002469
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002470/*
2471 * Polling driver
2472 *
2473 * We track all events in hotkey_source_mask all the time, since
2474 * most of them are edge-based. We only issue those requested by
2475 * hotkey_user_mask or hotkey_driver_mask, though.
2476 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002477static int hotkey_kthread(void *data)
2478{
2479 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002480 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002481 unsigned int si, so;
2482 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002483 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002484 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002485 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002486
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002487 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2488 goto exit;
2489
2490 set_freezable();
2491
2492 so = 0;
2493 si = 1;
2494 t = 0;
2495
2496 /* Initial state for compares */
2497 mutex_lock(&hotkey_thread_data_mutex);
2498 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002499 poll_mask = hotkey_source_mask;
2500 event_mask = hotkey_source_mask &
2501 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002502 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002503 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002504 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002505
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002506 while (!kthread_should_stop()) {
2507 if (t == 0) {
2508 if (likely(poll_freq))
2509 t = 1000/poll_freq;
2510 else
2511 t = 100; /* should never happen... */
2512 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002513 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002514 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002515 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002516
2517 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002518 continue;
2519
2520 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002521 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002522 /* forget old state on thaw or config change */
2523 si = so;
2524 t = 0;
2525 change_detector = hotkey_config_change;
2526 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002527 poll_mask = hotkey_source_mask;
2528 event_mask = hotkey_source_mask &
2529 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002530 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002531 mutex_unlock(&hotkey_thread_data_mutex);
2532
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002533 if (likely(poll_mask)) {
2534 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002535 if (likely(si != so)) {
2536 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002537 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002538 }
2539 }
2540
2541 so = si;
2542 si ^= 1;
2543 }
2544
2545exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002546 return 0;
2547}
2548
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002549/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002550static void hotkey_poll_stop_sync(void)
2551{
2552 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002553 kthread_stop(tpacpi_hotkey_task);
2554 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002555 }
2556}
2557
2558/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002559static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002560{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002561 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2562 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002563
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002564 if (hotkey_poll_freq > 0 &&
2565 (poll_driver_mask ||
2566 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002567 if (!tpacpi_hotkey_task) {
2568 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002569 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002570 if (IS_ERR(tpacpi_hotkey_task)) {
2571 tpacpi_hotkey_task = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07002572 pr_err("could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002573 "for hotkey polling\n");
2574 }
2575 }
2576 } else {
2577 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002578 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002579 hotkey_poll_freq == 0) {
Joe Perches0978e012011-04-04 10:06:25 -07002580 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2581 "require polling, which is currently "
2582 "disabled\n",
2583 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002584 }
2585 }
2586}
2587
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002588static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002589{
2590 mutex_lock(&hotkey_mutex);
2591 hotkey_poll_setup(may_warn);
2592 mutex_unlock(&hotkey_mutex);
2593}
2594
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002595/* call with hotkey_mutex held */
2596static void hotkey_poll_set_freq(unsigned int freq)
2597{
2598 if (!freq)
2599 hotkey_poll_stop_sync();
2600
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002601 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002602}
2603
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002604#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2605
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002606static void hotkey_poll_setup(const bool __unused)
2607{
2608}
2609
2610static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002611{
2612}
2613
2614#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2615
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002616static int hotkey_inputdev_open(struct input_dev *dev)
2617{
2618 switch (tpacpi_lifecycle) {
2619 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002620 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002621 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002622 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002623 case TPACPI_LIFE_EXITING:
2624 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002625 }
2626
2627 /* Should only happen if tpacpi_lifecycle is corrupt */
2628 BUG();
2629 return -EBUSY;
2630}
2631
2632static void hotkey_inputdev_close(struct input_dev *dev)
2633{
2634 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002635 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002636 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002637 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002638}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002639
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002640/* sysfs hotkey enable ------------------------------------------------- */
2641static ssize_t hotkey_enable_show(struct device *dev,
2642 struct device_attribute *attr,
2643 char *buf)
2644{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002645 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002646
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002647 printk_deprecated_attribute("hotkey_enable",
2648 "Hotkey reporting is always enabled");
2649
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002650 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002651 if (res)
2652 return res;
2653
2654 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2655}
2656
2657static ssize_t hotkey_enable_store(struct device *dev,
2658 struct device_attribute *attr,
2659 const char *buf, size_t count)
2660{
2661 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002662
2663 printk_deprecated_attribute("hotkey_enable",
2664 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002665
2666 if (parse_strtoul(buf, 1, &t))
2667 return -EINVAL;
2668
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002669 if (t == 0)
2670 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002671
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002672 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002673}
2674
Bastien Nocerab4dd04a2015-03-02 18:40:50 +01002675static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002676
2677/* sysfs hotkey mask --------------------------------------------------- */
2678static ssize_t hotkey_mask_show(struct device *dev,
2679 struct device_attribute *attr,
2680 char *buf)
2681{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002682 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002683}
2684
2685static ssize_t hotkey_mask_store(struct device *dev,
2686 struct device_attribute *attr,
2687 const char *buf, size_t count)
2688{
2689 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002690 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002691
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002692 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002693 return -EINVAL;
2694
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002695 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002696 return -ERESTARTSYS;
2697
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002698 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002699
2700#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002701 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002702#endif
2703
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002704 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002705
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002706 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2707
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002708 return (res) ? res : count;
2709}
2710
Bastien Nocerab4dd04a2015-03-02 18:40:50 +01002711static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002712
2713/* sysfs hotkey bios_enabled ------------------------------------------- */
2714static ssize_t hotkey_bios_enabled_show(struct device *dev,
2715 struct device_attribute *attr,
2716 char *buf)
2717{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002718 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002719}
2720
Bastien Nocerab4dd04a2015-03-02 18:40:50 +01002721static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002722
2723/* sysfs hotkey bios_mask ---------------------------------------------- */
2724static ssize_t hotkey_bios_mask_show(struct device *dev,
2725 struct device_attribute *attr,
2726 char *buf)
2727{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002728 printk_deprecated_attribute("hotkey_bios_mask",
2729 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002730 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002731}
2732
Bastien Nocerab4dd04a2015-03-02 18:40:50 +01002733static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002734
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002735/* sysfs hotkey all_mask ----------------------------------------------- */
2736static ssize_t hotkey_all_mask_show(struct device *dev,
2737 struct device_attribute *attr,
2738 char *buf)
2739{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002740 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2741 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002742}
2743
Bastien Nocerab4dd04a2015-03-02 18:40:50 +01002744static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002745
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04002746/* sysfs hotkey all_mask ----------------------------------------------- */
2747static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2748 struct device_attribute *attr,
2749 char *buf)
2750{
2751 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2752 hotkey_adaptive_all_mask | hotkey_source_mask);
2753}
2754
2755static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2756
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002757/* sysfs hotkey recommended_mask --------------------------------------- */
2758static ssize_t hotkey_recommended_mask_show(struct device *dev,
2759 struct device_attribute *attr,
2760 char *buf)
2761{
2762 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002763 (hotkey_all_mask | hotkey_source_mask)
2764 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002765}
2766
Bastien Nocerab4dd04a2015-03-02 18:40:50 +01002767static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002768
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002769#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2770
2771/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2772static ssize_t hotkey_source_mask_show(struct device *dev,
2773 struct device_attribute *attr,
2774 char *buf)
2775{
2776 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2777}
2778
2779static ssize_t hotkey_source_mask_store(struct device *dev,
2780 struct device_attribute *attr,
2781 const char *buf, size_t count)
2782{
2783 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002784 u32 r_ev;
2785 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002786
2787 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2788 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2789 return -EINVAL;
2790
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002791 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002792 return -ERESTARTSYS;
2793
2794 HOTKEY_CONFIG_CRITICAL_START
2795 hotkey_source_mask = t;
2796 HOTKEY_CONFIG_CRITICAL_END
2797
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002798 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2799 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002800 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002801
2802 /* check if events needed by the driver got disabled */
2803 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2804 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002805
2806 mutex_unlock(&hotkey_mutex);
2807
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002808 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07002809 pr_err("hotkey_source_mask: "
2810 "failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002811
2812 if (r_ev)
Joe Perches0978e012011-04-04 10:06:25 -07002813 pr_notice("hotkey_source_mask: "
2814 "some important events were disabled: 0x%04x\n",
2815 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002816
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002817 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2818
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002819 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002820}
2821
Bastien Nocerab4dd04a2015-03-02 18:40:50 +01002822static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002823
2824/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2825static ssize_t hotkey_poll_freq_show(struct device *dev,
2826 struct device_attribute *attr,
2827 char *buf)
2828{
2829 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2830}
2831
2832static ssize_t hotkey_poll_freq_store(struct device *dev,
2833 struct device_attribute *attr,
2834 const char *buf, size_t count)
2835{
2836 unsigned long t;
2837
2838 if (parse_strtoul(buf, 25, &t))
2839 return -EINVAL;
2840
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002841 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002842 return -ERESTARTSYS;
2843
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002844 hotkey_poll_set_freq(t);
2845 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002846
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002847 mutex_unlock(&hotkey_mutex);
2848
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002849 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2850
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002851 return count;
2852}
2853
Bastien Nocerab4dd04a2015-03-02 18:40:50 +01002854static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002855
2856#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2857
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002858/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002859static ssize_t hotkey_radio_sw_show(struct device *dev,
2860 struct device_attribute *attr,
2861 char *buf)
2862{
Johannes Berg19d337d2009-06-02 13:01:37 +02002863 int res;
2864 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002865 if (res < 0)
2866 return res;
2867
Johannes Berg19d337d2009-06-02 13:01:37 +02002868 /* Opportunistic update */
2869 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2870
2871 return snprintf(buf, PAGE_SIZE, "%d\n",
2872 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002873}
2874
Bastien Nocerab4dd04a2015-03-02 18:40:50 +01002875static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002876
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002877static void hotkey_radio_sw_notify_change(void)
2878{
2879 if (tp_features.hotkey_wlsw)
2880 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2881 "hotkey_radio_sw");
2882}
2883
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002884/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2885static ssize_t hotkey_tablet_mode_show(struct device *dev,
2886 struct device_attribute *attr,
2887 char *buf)
2888{
2889 int res, s;
2890 res = hotkey_get_tablet_mode(&s);
2891 if (res < 0)
2892 return res;
2893
2894 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2895}
2896
Bastien Nocerab4dd04a2015-03-02 18:40:50 +01002897static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002898
2899static void hotkey_tablet_mode_notify_change(void)
2900{
2901 if (tp_features.hotkey_tablet)
2902 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2903 "hotkey_tablet_mode");
2904}
2905
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002906/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002907static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2908 struct device_attribute *attr,
2909 char *buf)
2910{
2911 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2912}
2913
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002914static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002915
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002916static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002917{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002918 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2919 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002920}
2921
2922/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002923static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2924 struct device_attribute *attr,
2925 char *buf)
2926{
2927 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2928}
2929
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002930static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2931 hotkey_wakeup_hotunplug_complete_show, NULL);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002932
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002933static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002934{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002935 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2936 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002937}
2938
Bastien Nocerab790cee2015-03-02 14:45:27 +01002939/* sysfs adaptive kbd mode --------------------------------------------- */
2940
2941static int adaptive_keyboard_get_mode(void);
2942static int adaptive_keyboard_set_mode(int new_mode);
2943
2944enum ADAPTIVE_KEY_MODE {
2945 HOME_MODE,
2946 WEB_BROWSER_MODE,
2947 WEB_CONFERENCE_MODE,
2948 FUNCTION_MODE,
2949 LAYFLAT_MODE
2950};
2951
2952static ssize_t adaptive_kbd_mode_show(struct device *dev,
2953 struct device_attribute *attr,
2954 char *buf)
2955{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03002956 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01002957
2958 current_mode = adaptive_keyboard_get_mode();
2959 if (current_mode < 0)
2960 return current_mode;
2961
2962 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
2963}
2964
2965static ssize_t adaptive_kbd_mode_store(struct device *dev,
2966 struct device_attribute *attr,
2967 const char *buf, size_t count)
2968{
2969 unsigned long t;
2970 int res;
2971
2972 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2973 return -EINVAL;
2974
2975 res = adaptive_keyboard_set_mode(t);
2976 return (res < 0) ? res : count;
2977}
2978
2979static DEVICE_ATTR_RW(adaptive_kbd_mode);
2980
2981static struct attribute *adaptive_kbd_attributes[] = {
2982 &dev_attr_adaptive_kbd_mode.attr,
2983 NULL
2984};
2985
2986static const struct attribute_group adaptive_kbd_attr_group = {
2987 .attrs = adaptive_kbd_attributes,
2988};
2989
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002990/* --------------------------------------------------------------------- */
2991
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002992static struct attribute *hotkey_attributes[] __initdata = {
2993 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002994 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002995 &dev_attr_hotkey_bios_mask.attr,
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02002996 &dev_attr_wakeup_reason.attr,
2997 &dev_attr_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002998 &dev_attr_hotkey_mask.attr,
2999 &dev_attr_hotkey_all_mask.attr,
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003000 &dev_attr_hotkey_adaptive_all_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003001 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003002#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003003 &dev_attr_hotkey_source_mask.attr,
3004 &dev_attr_hotkey_poll_freq.attr,
3005#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003006};
3007
Johannes Berg19d337d2009-06-02 13:01:37 +02003008/*
3009 * Sync both the hw and sw blocking state of all switches
3010 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003011static void tpacpi_send_radiosw_update(void)
3012{
3013 int wlsw;
3014
Johannes Berg19d337d2009-06-02 13:01:37 +02003015 /*
3016 * We must sync all rfkill controllers *before* issuing any
3017 * rfkill input events, or we will race the rfkill core input
3018 * handler.
3019 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003020 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003021 * for the above.
3022 *
3023 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3024 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003025
Johannes Berg19d337d2009-06-02 13:01:37 +02003026 wlsw = hotkey_get_wlsw();
3027
3028 /* Sync hw blocking state first if it is hw-blocked */
3029 if (wlsw == TPACPI_RFK_RADIO_OFF)
3030 tpacpi_rfk_update_hwblock_state(true);
3031
3032 /* Sync sw blocking state */
3033 tpacpi_rfk_update_swstate_all();
3034
3035 /* Sync hw blocking state last if it is hw-unblocked */
3036 if (wlsw == TPACPI_RFK_RADIO_ON)
3037 tpacpi_rfk_update_hwblock_state(false);
3038
3039 /* Issue rfkill input event for WLSW switch */
3040 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003041 mutex_lock(&tpacpi_inputdev_send_mutex);
3042
3043 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003044 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003045 input_sync(tpacpi_inputdev);
3046
3047 mutex_unlock(&tpacpi_inputdev_send_mutex);
3048 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003049
3050 /*
3051 * this can be unconditional, as we will poll state again
3052 * if userspace uses the notify to read data
3053 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003054 hotkey_radio_sw_notify_change();
3055}
3056
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003057static void hotkey_exit(void)
3058{
3059#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003060 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003061 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003062 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003063#endif
3064
3065 if (hotkey_dev_attributes)
3066 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3067
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003068 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003069 "restoring original HKEY status and mask\n");
3070 /* yes, there is a bitwise or below, we want the
3071 * functions to be called even if one of them fail */
3072 if (((tp_features.hotkey_mask &&
3073 hotkey_mask_set(hotkey_orig_mask)) |
3074 hotkey_status_set(false)) != 0)
Joe Perches0978e012011-04-04 10:06:25 -07003075 pr_err("failed to restore hot key mask "
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003076 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003077}
3078
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003079static void __init hotkey_unmap(const unsigned int scancode)
3080{
3081 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3082 clear_bit(hotkey_keycode_map[scancode],
3083 tpacpi_inputdev->keybit);
3084 hotkey_keycode_map[scancode] = KEY_RESERVED;
3085 }
3086}
3087
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003088/*
3089 * HKEY quirks:
3090 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3091 */
3092
3093#define TPACPI_HK_Q_INIMASK 0x0001
3094
3095static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3096 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3097 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3098 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3099 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3100 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3101 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3102 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3103 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3104 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3105 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3106 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3107 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3108 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3109 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3110 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3111 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3112 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3113 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3114 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3115};
3116
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003117typedef u16 tpacpi_keymap_entry_t;
3118typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003119
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003120static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003121{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003122 /* Requirements for changing the default keymaps:
3123 *
3124 * 1. Many of the keys are mapped to KEY_RESERVED for very
3125 * good reasons. Do not change them unless you have deep
3126 * knowledge on the IBM and Lenovo ThinkPad firmware for
3127 * the various ThinkPad models. The driver behaves
3128 * differently for KEY_RESERVED: such keys have their
3129 * hot key mask *unset* in mask_recommended, and also
3130 * in the initial hot key mask programmed into the
3131 * firmware at driver load time, which means the firm-
3132 * ware may react very differently if you change them to
3133 * something else;
3134 *
3135 * 2. You must be subscribed to the linux-thinkpad and
3136 * ibm-acpi-devel mailing lists, and you should read the
3137 * list archives since 2007 if you want to change the
3138 * keymaps. This requirement exists so that you will
3139 * know the past history of problems with the thinkpad-
3140 * acpi driver keymaps, and also that you will be
3141 * listening to any bug reports;
3142 *
3143 * 3. Do not send thinkpad-acpi specific patches directly to
3144 * for merging, *ever*. Send them to the linux-acpi
3145 * mailinglist for comments. Merging is to be done only
3146 * through acpi-test and the ACPI maintainer.
3147 *
3148 * If the above is too much to ask, don't change the keymap.
3149 * Ask the thinkpad-acpi maintainer to do it, instead.
3150 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003151
3152 enum keymap_index {
3153 TPACPI_KEYMAP_IBM_GENERIC = 0,
3154 TPACPI_KEYMAP_LENOVO_GENERIC,
3155 };
3156
3157 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3158 /* Generic keymap for IBM ThinkPads */
3159 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003160 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003161 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003162 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3163 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003164
3165 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003166 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3167 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3168 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003169
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003170 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003171 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003172 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003173
3174 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003175 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003176
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003177 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3178 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003179
3180 /* Volume: firmware always react to it and reprograms
3181 * the built-in *extra* mixer. Never map it to control
3182 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003183 KEY_RESERVED, /* 0x14: VOLUME UP */
3184 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3185 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003186
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003187 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003188
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003189 /* (assignments unknown, please report if found) */
3190 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3191 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003192
3193 /* No assignments, only used for Adaptive keyboards. */
3194 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3195 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3196 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3197 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3198 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003199 },
3200
3201 /* Generic keymap for Lenovo ThinkPads */
3202 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003203 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3204 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003205 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003206 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003207
3208 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003209 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3210 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3211 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003212
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003213 /* These should be enabled --only-- when ACPI video
3214 * is disabled (i.e. in "vendor" mode), and are handled
3215 * in a special way by the init code */
3216 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3217 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003218
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003219 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003220
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003221 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3222 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003223
3224 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3225 * react to it and reprograms the built-in *extra* mixer.
3226 * Never map it to control another mixer by default.
3227 *
3228 * T60?, T61, R60?, R61: firmware and EC tries to send
3229 * these over the regular keyboard, so these are no-ops,
3230 * but there are still weird bugs re. MUTE, so do not
3231 * change unless you get test reports from all Lenovo
3232 * models. May cause the BIOS to interfere with the
3233 * HDA mixer.
3234 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003235 KEY_RESERVED, /* 0x14: VOLUME UP */
3236 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3237 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003238
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003239 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003240
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003241 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003242 KEY_UNKNOWN, KEY_UNKNOWN,
3243
3244 /*
3245 * The mic mute button only sends 0x1a. It does not
3246 * automatically mute the mic or change the mute light.
3247 */
3248 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3249
3250 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003251 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003252
3253 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003254 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003255
3256 /*
3257 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3258 * The first item in this list is the Mute button which is
3259 * emitted with 0x103 through
3260 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3261 * symbol is held.
3262 * We'll need to offset those by 0x20.
3263 */
3264 KEY_RESERVED, /* Mute held, 0x103 */
3265 KEY_BRIGHTNESS_MIN, /* Backlight off */
3266 KEY_RESERVED, /* Clipping tool */
3267 KEY_RESERVED, /* Cloud */
3268 KEY_RESERVED,
3269 KEY_VOICECOMMAND, /* Voice */
3270 KEY_RESERVED,
3271 KEY_RESERVED, /* Gestures */
3272 KEY_RESERVED,
3273 KEY_RESERVED,
3274 KEY_RESERVED,
3275 KEY_CONFIG, /* Settings */
3276 KEY_RESERVED, /* New tab */
3277 KEY_REFRESH, /* Reload */
3278 KEY_BACK, /* Back */
3279 KEY_RESERVED, /* Microphone down */
3280 KEY_RESERVED, /* Microphone up */
3281 KEY_RESERVED, /* Microphone cancellation */
3282 KEY_RESERVED, /* Camera mode */
3283 KEY_RESERVED, /* Rotate display, 0x116 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003284 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003285 };
3286
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003287 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3288 /* Generic maps (fallback) */
3289 {
3290 .vendor = PCI_VENDOR_ID_IBM,
3291 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3292 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3293 },
3294 {
3295 .vendor = PCI_VENDOR_ID_LENOVO,
3296 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3297 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3298 },
3299 };
3300
3301#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003302#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003303
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003304 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003305 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003306 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003307 bool radiosw_state = false;
3308 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003309
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003310 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003311 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003312
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003313 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3314 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003315
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003316 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003317 BUG_ON(tpacpi_inputdev->open != NULL ||
3318 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003319
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003320 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003321 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003322
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003323#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003324 mutex_init(&hotkey_thread_data_mutex);
3325#endif
3326
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003327 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003328 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003329
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003330 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3331 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003332 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003333
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003334 if (!tp_features.hotkey)
3335 return 1;
3336
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003337 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3338 ARRAY_SIZE(tpacpi_hotkey_qtable));
3339
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003340 tpacpi_disable_brightness_delay();
3341
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003342 /* MUST have enough space for all attributes to be added to
3343 * hotkey_dev_attributes */
3344 hotkey_dev_attributes = create_attr_set(
3345 ARRAY_SIZE(hotkey_attributes) + 2,
3346 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003347 if (!hotkey_dev_attributes)
3348 return -ENOMEM;
3349 res = add_many_to_attr_set(hotkey_dev_attributes,
3350 hotkey_attributes,
3351 ARRAY_SIZE(hotkey_attributes));
3352 if (res)
3353 goto err_exit;
3354
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003355 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003356 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3357 for HKEY interface version 0x100 */
3358 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003359 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3360 "firmware HKEY interface version: 0x%x\n",
3361 hkeyv);
3362
3363 switch (hkeyv >> 8) {
3364 case 1:
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003365 /*
3366 * MHKV 0x100 in A31, R40, R40e,
3367 * T4x, X31, and later
3368 */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003369
3370 /* Paranoia check AND init hotkey_all_mask */
3371 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3372 "MHKA", "qd")) {
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003373 pr_err("missing MHKA handler, please report this to %s\n",
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003374 TPACPI_MAIL);
3375 /* Fallback: pre-init for FN+F3,F4,F12 */
3376 hotkey_all_mask = 0x080cU;
3377 } else {
3378 tp_features.hotkey_mask = 1;
3379 }
Dennis Wassenberg0118c2d2016-06-08 10:54:25 -04003380 break;
3381
3382 case 2:
3383 /*
3384 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3385 */
3386
3387 /* Paranoia check AND init hotkey_all_mask */
3388 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3389 "MHKA", "dd", 1)) {
3390 pr_err("missing MHKA handler, please report this to %s\n",
3391 TPACPI_MAIL);
3392 /* Fallback: pre-init for FN+F3,F4,F12 */
3393 hotkey_all_mask = 0x080cU;
3394 } else {
3395 tp_features.hotkey_mask = 1;
3396 }
3397
3398 /*
3399 * Check if we have an adaptive keyboard, like on the
3400 * Lenovo Carbon X1 2014 (2nd Gen).
3401 */
3402 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3403 "MHKA", "dd", 2)) {
3404 if (hotkey_adaptive_all_mask != 0) {
3405 tp_features.has_adaptive_kbd = true;
3406 res = sysfs_create_group(
3407 &tpacpi_pdev->dev.kobj,
3408 &adaptive_kbd_attr_group);
3409 if (res)
3410 goto err_exit;
3411 }
3412 } else {
3413 tp_features.has_adaptive_kbd = false;
3414 hotkey_adaptive_all_mask = 0x0U;
3415 }
3416 break;
3417
3418 default:
3419 pr_err("unknown version of the HKEY interface: 0x%x\n",
3420 hkeyv);
3421 pr_err("please report this to %s\n", TPACPI_MAIL);
3422 break;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003423 }
3424 }
3425
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003426 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3427 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003428 str_supported(tp_features.hotkey_mask));
3429
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003430 /* Init hotkey_all_mask if not initialized yet */
3431 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3432 (quirks & TPACPI_HK_Q_INIMASK))
3433 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003434
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003435 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003436 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003437 /* hotkey_source_mask *must* be zero for
3438 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003439 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003440 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003441 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003442
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003443 hotkey_orig_mask = hotkey_acpi_mask;
3444 } else {
3445 hotkey_orig_mask = hotkey_all_mask;
3446 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003447 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003448
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003449#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3450 if (dbg_wlswemul) {
3451 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003452 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003453 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003454 } else
3455#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003456 /* Not all thinkpads have a hardware radio switch */
3457 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3458 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003459 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003460 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003461 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003462 }
3463 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003464 res = add_to_attr_set(hotkey_dev_attributes,
3465 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003466
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003467 /* For X41t, X60t, X61t Tablets... */
3468 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3469 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003470 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Joe Perches0978e012011-04-04 10:06:25 -07003471 pr_info("possible tablet mode switch found; "
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003472 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003473 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003474 res = add_to_attr_set(hotkey_dev_attributes,
3475 &dev_attr_hotkey_tablet_mode.attr);
3476 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003477
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003478 if (!res)
3479 res = register_attr_set_with_sysfs(
3480 hotkey_dev_attributes,
3481 &tpacpi_pdev->dev.kobj);
3482 if (res)
3483 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003484
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003485 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003486 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3487 GFP_KERNEL);
3488 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003489 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003490 res = -ENOMEM;
3491 goto err_exit;
3492 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003493
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003494 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3495 ARRAY_SIZE(tpacpi_keymap_qtable));
3496 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3497 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3498 "using keymap number %lu\n", keymap_id);
3499
3500 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3501 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003502
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003503 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003504 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3505 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3506 tpacpi_inputdev->keycode = hotkey_keycode_map;
3507 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3508 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003509 input_set_capability(tpacpi_inputdev, EV_KEY,
3510 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003511 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003512 if (i < sizeof(hotkey_reserved_mask)*8)
3513 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003514 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003515 }
3516
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003517 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003518 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003519 input_report_switch(tpacpi_inputdev,
3520 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003521 }
3522 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003523 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003524 input_report_switch(tpacpi_inputdev,
3525 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003526 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003527
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003528 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003529 * userspace. tpacpi_detect_brightness_capabilities() must have
3530 * been called before this point */
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02003531 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Joe Perches0978e012011-04-04 10:06:25 -07003532 pr_info("This ThinkPad has standard ACPI backlight "
3533 "brightness control, supported by the ACPI "
3534 "video driver\n");
3535 pr_notice("Disabling thinkpad-acpi brightness events "
3536 "by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003537
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003538 /* Disable brightness up/down on Lenovo thinkpads when
3539 * ACPI is handling them, otherwise it is plain impossible
3540 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003541 hotkey_reserved_mask |=
3542 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3543 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003544 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3545 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003546 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003547
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003548#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003549 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3550 & ~hotkey_all_mask
3551 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003552
3553 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3554 "hotkey source mask 0x%08x, polling freq %u\n",
3555 hotkey_source_mask, hotkey_poll_freq);
3556#endif
3557
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003558 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3559 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003560 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003561 if (res) {
3562 hotkey_exit();
3563 return res;
3564 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003565 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3566 | hotkey_driver_mask)
3567 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003568 if (res < 0 && res != -ENXIO) {
3569 hotkey_exit();
3570 return res;
3571 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003572 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3573 & ~hotkey_reserved_mask;
3574 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3575 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3576 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003577
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003578 tpacpi_inputdev->open = &hotkey_inputdev_open;
3579 tpacpi_inputdev->close = &hotkey_inputdev_close;
3580
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003581 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003582
3583 return 0;
3584
3585err_exit:
3586 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
Bastien Nocerab790cee2015-03-02 14:45:27 +01003587 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3588 &adaptive_kbd_attr_group);
3589
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003590 hotkey_dev_attributes = NULL;
3591
Jan van den Berg72a979f2014-09-17 00:01:08 +02003592 return (res < 0) ? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003593}
3594
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003595/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3596 * mode, Web conference mode, Function mode and Lay-flat mode.
3597 * We support Home mode and Function mode currently.
3598 *
3599 * Will consider support rest of modes in future.
3600 *
3601 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003602static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003603 HOME_MODE,
3604/* WEB_BROWSER_MODE = 2,
3605 WEB_CONFERENCE_MODE = 3, */
3606 FUNCTION_MODE
3607};
3608
3609#define DFR_CHANGE_ROW 0x101
3610#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003611#define FIRST_ADAPTIVE_KEY 0x103
3612#define ADAPTIVE_KEY_OFFSET 0x020
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003613
3614/* press Fn key a while second, it will switch to Function Mode. Then
3615 * release Fn key, previous mode be restored.
3616 */
3617static bool adaptive_keyboard_mode_is_saved;
3618static int adaptive_keyboard_prev_mode;
3619
Bastien Noceraf74587f2015-03-02 14:45:22 +01003620static int adaptive_keyboard_get_mode(void)
3621{
3622 int mode = 0;
3623
3624 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3625 pr_err("Cannot read adaptive keyboard mode\n");
3626 return -EIO;
3627 }
3628
3629 return mode;
3630}
3631
3632static int adaptive_keyboard_set_mode(int new_mode)
3633{
3634 if (new_mode < 0 ||
3635 new_mode > LAYFLAT_MODE)
3636 return -EINVAL;
3637
3638 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3639 pr_err("Cannot set adaptive keyboard mode\n");
3640 return -EIO;
3641 }
3642
3643 return 0;
3644}
3645
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003646static int adaptive_keyboard_get_next_mode(int mode)
3647{
3648 size_t i;
3649 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3650
3651 for (i = 0; i <= max_mode; i++) {
3652 if (adaptive_keyboard_modes[i] == mode)
3653 break;
3654 }
3655
3656 if (i >= max_mode)
3657 i = 0;
3658 else
3659 i++;
3660
3661 return adaptive_keyboard_modes[i];
3662}
3663
3664static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3665{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003666 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003667 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003668 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003669
3670 switch (scancode) {
3671 case DFR_CHANGE_ROW:
3672 if (adaptive_keyboard_mode_is_saved) {
3673 new_mode = adaptive_keyboard_prev_mode;
3674 adaptive_keyboard_mode_is_saved = false;
3675 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003676 current_mode = adaptive_keyboard_get_mode();
3677 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003678 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003679 new_mode = adaptive_keyboard_get_next_mode(
3680 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003681 }
3682
Bastien Noceraf74587f2015-03-02 14:45:22 +01003683 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003684 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003685
3686 return true;
3687
3688 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003689 current_mode = adaptive_keyboard_get_mode();
3690 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003691 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003692
Bastien Noceraf74587f2015-03-02 14:45:22 +01003693 adaptive_keyboard_prev_mode = current_mode;
3694 adaptive_keyboard_mode_is_saved = true;
3695
3696 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3697 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003698 return true;
3699
3700 default:
Dan Carpenter741d98c2015-03-11 12:36:07 +03003701 if (scancode < FIRST_ADAPTIVE_KEY ||
3702 scancode >= FIRST_ADAPTIVE_KEY + TPACPI_HOTKEY_MAP_LEN -
3703 ADAPTIVE_KEY_OFFSET) {
Bastien Nocera6a68d852015-03-02 14:45:31 +01003704 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3705 scancode);
3706 return false;
3707 }
3708 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY + ADAPTIVE_KEY_OFFSET];
3709 if (keycode != KEY_RESERVED) {
3710 mutex_lock(&tpacpi_inputdev_send_mutex);
3711
3712 input_report_key(tpacpi_inputdev, keycode, 1);
3713 input_sync(tpacpi_inputdev);
3714
3715 input_report_key(tpacpi_inputdev, keycode, 0);
3716 input_sync(tpacpi_inputdev);
3717
3718 mutex_unlock(&tpacpi_inputdev_send_mutex);
3719 }
3720 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003721 }
3722}
3723
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003724static bool hotkey_notify_hotkey(const u32 hkey,
3725 bool *send_acpi_ev,
3726 bool *ignore_acpi_ev)
3727{
3728 /* 0x1000-0x1FFF: key presses */
3729 unsigned int scancode = hkey & 0xfff;
3730 *send_acpi_ev = true;
3731 *ignore_acpi_ev = false;
3732
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03003733 /* HKEY event 0x1001 is scancode 0x00 */
3734 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003735 scancode--;
3736 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003737 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003738 *send_acpi_ev = false;
3739 } else {
3740 *ignore_acpi_ev = true;
3741 }
3742 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003743 } else {
3744 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003745 }
3746 return false;
3747}
3748
3749static bool hotkey_notify_wakeup(const u32 hkey,
3750 bool *send_acpi_ev,
3751 bool *ignore_acpi_ev)
3752{
3753 /* 0x2000-0x2FFF: Wakeup reason */
3754 *send_acpi_ev = true;
3755 *ignore_acpi_ev = false;
3756
3757 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003758 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3759 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003760 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3761 *ignore_acpi_ev = true;
3762 break;
3763
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003764 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3765 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003766 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3767 *ignore_acpi_ev = true;
3768 break;
3769
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003770 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3771 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003772 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003773 /* how to auto-heal: */
3774 /* 2313: woke up from S3, go to S4/S5 */
3775 /* 2413: woke up from S4, go to S5 */
3776 break;
3777
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003778 default:
3779 return false;
3780 }
3781
3782 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003783 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003784 hotkey_wakeup_reason_notify_change();
3785 }
3786 return true;
3787}
3788
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003789static bool hotkey_notify_dockevent(const u32 hkey,
3790 bool *send_acpi_ev,
3791 bool *ignore_acpi_ev)
3792{
3793 /* 0x4000-0x4FFF: dock-related events */
3794 *send_acpi_ev = true;
3795 *ignore_acpi_ev = false;
3796
3797 switch (hkey) {
3798 case TP_HKEY_EV_UNDOCK_ACK:
3799 /* ACPI undock operation completed after wakeup */
3800 hotkey_autosleep_ack = 1;
3801 pr_info("undocked\n");
3802 hotkey_wakeup_hotunplug_complete_notify_change();
3803 return true;
3804
3805 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3806 pr_info("docked into hotplug port replicator\n");
3807 return true;
3808 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3809 pr_info("undocked from hotplug port replicator\n");
3810 return true;
3811
3812 default:
3813 return false;
3814 }
3815}
3816
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003817static bool hotkey_notify_usrevent(const u32 hkey,
3818 bool *send_acpi_ev,
3819 bool *ignore_acpi_ev)
3820{
3821 /* 0x5000-0x5FFF: human interface helpers */
3822 *send_acpi_ev = true;
3823 *ignore_acpi_ev = false;
3824
3825 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003826 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3827 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003828 return true;
3829
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003830 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3831 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003832 tpacpi_input_send_tabletsw();
3833 hotkey_tablet_mode_notify_change();
3834 *send_acpi_ev = false;
3835 return true;
3836
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003837 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3838 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3839 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003840 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003841 *ignore_acpi_ev = true;
3842 return true;
3843
3844 default:
3845 return false;
3846 }
3847}
3848
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003849static void thermal_dump_all_sensors(void);
3850
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003851static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003852 bool *send_acpi_ev,
3853 bool *ignore_acpi_ev)
3854{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003855 bool known = true;
3856
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003857 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003858 *send_acpi_ev = true;
3859 *ignore_acpi_ev = false;
3860
3861 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003862 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003863 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003864 /* recommended action: do nothing, we don't have
3865 * Lenovo ATM information */
3866 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003867 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003868 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003869 /* recommended action: warn user through gui */
3870 break;
3871 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003872 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003873 /* recommended action: immediate sleep/hibernate */
3874 break;
3875 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003876 pr_crit("THERMAL ALARM: "
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003877 "a sensor reports something is too hot!\n");
3878 /* recommended action: warn user through gui, that */
3879 /* some internal component is too hot */
3880 break;
3881 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003882 pr_alert("THERMAL EMERGENCY: "
3883 "a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003884 /* recommended action: immediate sleep/hibernate */
3885 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01003886 case TP_HKEY_EV_AC_CHANGED:
3887 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3888 * AC status changed; can be triggered by plugging or
3889 * unplugging AC adapter, docking or undocking. */
3890
3891 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003892
3893 case TP_HKEY_EV_KEY_NUMLOCK:
3894 case TP_HKEY_EV_KEY_FN:
Xavier Naveira67ab6242015-02-10 08:45:18 +01003895 case TP_HKEY_EV_KEY_FN_ESC:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003896 /* key press events, we just ignore them as long as the EC
3897 * is still reporting them in the normal keyboard stream */
3898 *send_acpi_ev = false;
3899 *ignore_acpi_ev = true;
3900 return true;
3901
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003902 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003903 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003904 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003905 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003906
3907 thermal_dump_all_sensors();
3908
3909 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003910}
3911
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003912static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3913{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003914 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003915 bool send_acpi_ev;
3916 bool ignore_acpi_ev;
3917 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003918
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003919 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07003920 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003921 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003922 acpi_bus_generate_netlink_event(
3923 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003924 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003925 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003926 return;
3927 }
3928
3929 while (1) {
3930 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07003931 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003932 return;
3933 }
3934
3935 if (hkey == 0) {
3936 /* queue empty */
3937 return;
3938 }
3939
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003940 send_acpi_ev = true;
3941 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003942
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003943 switch (hkey >> 12) {
3944 case 1:
3945 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003946 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3947 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003948 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003949 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003950 /* 0x2000-0x2FFF: Wakeup reason */
3951 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3952 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003953 break;
3954 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003955 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003956 switch (hkey) {
3957 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003958 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07003959 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003960 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003961 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003962 break;
3963 case TP_HKEY_EV_OPTDRV_EJ:
3964 /* FIXME: kick libata if SATA link offline */
3965 known_ev = true;
3966 break;
3967 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003968 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003969 }
3970 break;
3971 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003972 /* 0x4000-0x4FFF: dock-related events */
3973 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
3974 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003975 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003976 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003977 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003978 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3979 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003980 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003981 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003982 /* 0x6000-0x6FFF: thermal alarms/notices and
3983 * keyboard events */
3984 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003985 &ignore_acpi_ev);
3986 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003987 case 7:
3988 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003989 if (tp_features.hotkey_wlsw &&
3990 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003991 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003992 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003993 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003994 break;
3995 }
3996 /* fallthrough to default */
3997 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003998 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003999 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02004000 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07004001 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4002 pr_notice("please report the conditions when this "
4003 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03004004 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004005
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004006 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03004007 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004008 acpi_bus_generate_netlink_event(
4009 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004010 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004011 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03004012 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004013 }
4014}
4015
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02004016static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004017{
4018 /* Do these on suspend, we get the events on early resume! */
4019 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4020 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08004021
4022 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004023 if (tp_features.has_adaptive_kbd) {
4024 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4025 "GTRW", "dd", 0)) {
4026 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004027 }
4028 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004029}
4030
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004031static void hotkey_resume(void)
4032{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03004033 tpacpi_disable_brightness_delay();
4034
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004035 if (hotkey_status_set(true) < 0 ||
4036 hotkey_mask_set(hotkey_acpi_mask) < 0)
Joe Perches0978e012011-04-04 10:06:25 -07004037 pr_err("error while attempting to reset the event "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004038 "firmware interface\n");
4039
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03004040 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02004041 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02004042 hotkey_wakeup_reason_notify_change();
4043 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004044 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004045
4046 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004047 if (tp_features.has_adaptive_kbd) {
4048 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4049 adaptive_keyboard_prev_mode)) {
4050 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004051 }
4052 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004053}
4054
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004055/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004056static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004058 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004060 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004061 seq_printf(m, "status:\t\tnot supported\n");
4062 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004063 }
4064
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004065 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004066 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004067 res = hotkey_status_get(&status);
4068 if (!res)
4069 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004070 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004071 if (res)
4072 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004074 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004075 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004076 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4077 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004079 seq_printf(m, "mask:\t\tnot supported\n");
4080 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 }
4082
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084}
4085
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004086static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004087{
4088 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004089 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Joe Perches0978e012011-04-04 10:06:25 -07004090 pr_fmt("hotkey enable/disable functionality has been "
4091 "removed from the driver. "
4092 "Hotkeys are always enabled.\n")))
4093 pr_err("Please remove the hotkey=enable module "
4094 "parameter, it is deprecated. "
4095 "Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004096}
4097
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004098static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004100 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004101 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004104 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 return -ENODEV;
4106
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004107 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004108 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004109
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004110 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004111
4112 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 while ((cmd = next_cmd(&buf))) {
4114 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004115 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004117 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004118 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03004120 mask = (hotkey_all_mask | hotkey_source_mask)
4121 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4123 /* mask set */
4124 } else if (sscanf(cmd, "%x", &mask) == 1) {
4125 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004126 } else {
4127 res = -EINVAL;
4128 goto errexit;
4129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004131
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004132 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004133 tpacpi_disclose_usertask("procfs hotkey",
4134 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004135 res = hotkey_user_mask_set(mask);
4136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004138errexit:
4139 mutex_unlock(&hotkey_mutex);
4140 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004141}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004143static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004144 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4145 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004146 {"", 0},
4147};
4148
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004149static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004150 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004151 .notify = hotkey_notify,
4152 .handle = &hkey_handle,
4153 .type = ACPI_DEVICE_NOTIFY,
4154};
4155
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004156static struct ibm_struct hotkey_driver_data = {
4157 .name = "hotkey",
4158 .read = hotkey_read,
4159 .write = hotkey_write,
4160 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004161 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004162 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004163 .acpi = &ibm_hotkey_acpidriver,
4164};
4165
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004166/*************************************************************************
4167 * Bluetooth subdriver
4168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004170enum {
4171 /* ACPI GBDC/SBDC bits */
4172 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4173 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004174 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004175 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004176};
4177
4178enum {
4179 /* ACPI \BLTH commands */
4180 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4181 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4182 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4183 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4184 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004185};
4186
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004187#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4188
Johannes Berg19d337d2009-06-02 13:01:37 +02004189static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004190{
4191 int status;
4192
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004193#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4194 if (dbg_bluetoothemul)
4195 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004196 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004197#endif
4198
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004199 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4200 return -EIO;
4201
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004202 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004203 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004204}
4205
Johannes Berg19d337d2009-06-02 13:01:37 +02004206static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004207{
4208 int status;
4209
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004210 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004211 "will attempt to %s bluetooth\n",
4212 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004213
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004214#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4215 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004216 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004217 return 0;
4218 }
4219#endif
4220
Johannes Berg19d337d2009-06-02 13:01:37 +02004221 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004222 status = TP_ACPI_BLUETOOTH_RADIOSSW
4223 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4224 else
4225 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004226
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004227 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4228 return -EIO;
4229
4230 return 0;
4231}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004232
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004233/* sysfs bluetooth enable ---------------------------------------------- */
4234static ssize_t bluetooth_enable_show(struct device *dev,
4235 struct device_attribute *attr,
4236 char *buf)
4237{
Johannes Berg19d337d2009-06-02 13:01:37 +02004238 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4239 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004240}
4241
4242static ssize_t bluetooth_enable_store(struct device *dev,
4243 struct device_attribute *attr,
4244 const char *buf, size_t count)
4245{
Johannes Berg19d337d2009-06-02 13:01:37 +02004246 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4247 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004248}
4249
Bastien Nocerab4dd04a2015-03-02 18:40:50 +01004250static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004251
4252/* --------------------------------------------------------------------- */
4253
4254static struct attribute *bluetooth_attributes[] = {
4255 &dev_attr_bluetooth_enable.attr,
4256 NULL
4257};
4258
4259static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004260 .attrs = bluetooth_attributes,
4261};
4262
Johannes Berg19d337d2009-06-02 13:01:37 +02004263static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4264 .get_status = bluetooth_get_status,
4265 .set_status = bluetooth_set_status,
4266};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004267
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004268static void bluetooth_shutdown(void)
4269{
4270 /* Order firmware to save current state to NVRAM */
4271 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4272 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004273 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004274 else
4275 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004276 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004277}
4278
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004279static void bluetooth_exit(void)
4280{
4281 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4282 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004283
4284 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4285
4286 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004287}
4288
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004289static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004291 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004292 int status = 0;
4293
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004294 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4295 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004296
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004297 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004298
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004299 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4300 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004301 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004302 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004304 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4305 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004306 str_supported(tp_features.bluetooth),
4307 status);
4308
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004309#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4310 if (dbg_bluetoothemul) {
4311 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004312 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004313 } else
4314#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004315 if (tp_features.bluetooth &&
4316 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4317 /* no bluetooth hardware present in system */
4318 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004319 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004320 "bluetooth hardware not installed\n");
4321 }
4322
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004323 if (!tp_features.bluetooth)
4324 return 1;
4325
Johannes Berg19d337d2009-06-02 13:01:37 +02004326 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4327 &bluetooth_tprfk_ops,
4328 RFKILL_TYPE_BLUETOOTH,
4329 TPACPI_RFK_BLUETOOTH_SW_NAME,
4330 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004331 if (res)
4332 return res;
4333
Johannes Berg19d337d2009-06-02 13:01:37 +02004334 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4335 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004336 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004337 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004338 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004339 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004340
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342}
4343
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004344/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004345static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004347 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348}
4349
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004350static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351{
Johannes Berg19d337d2009-06-02 13:01:37 +02004352 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353}
4354
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004355static struct ibm_struct bluetooth_driver_data = {
4356 .name = "bluetooth",
4357 .read = bluetooth_read,
4358 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004359 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004360 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004361};
4362
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004363/*************************************************************************
4364 * Wan subdriver
4365 */
4366
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004367enum {
4368 /* ACPI GWAN/SWAN bits */
4369 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4370 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004371 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004372 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004373};
4374
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004375#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4376
Johannes Berg19d337d2009-06-02 13:01:37 +02004377static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004378{
4379 int status;
4380
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004381#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4382 if (dbg_wwanemul)
4383 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004384 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004385#endif
4386
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004387 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4388 return -EIO;
4389
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004390 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004391 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004392}
4393
Johannes Berg19d337d2009-06-02 13:01:37 +02004394static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004395{
4396 int status;
4397
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004398 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004399 "will attempt to %s wwan\n",
4400 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004401
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004402#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4403 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004404 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004405 return 0;
4406 }
4407#endif
4408
Johannes Berg19d337d2009-06-02 13:01:37 +02004409 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004410 status = TP_ACPI_WANCARD_RADIOSSW
4411 | TP_ACPI_WANCARD_RESUMECTRL;
4412 else
4413 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004414
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004415 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4416 return -EIO;
4417
4418 return 0;
4419}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004420
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004421/* sysfs wan enable ---------------------------------------------------- */
4422static ssize_t wan_enable_show(struct device *dev,
4423 struct device_attribute *attr,
4424 char *buf)
4425{
Johannes Berg19d337d2009-06-02 13:01:37 +02004426 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4427 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004428}
4429
4430static ssize_t wan_enable_store(struct device *dev,
4431 struct device_attribute *attr,
4432 const char *buf, size_t count)
4433{
Johannes Berg19d337d2009-06-02 13:01:37 +02004434 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4435 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004436}
4437
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004438static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4439 wan_enable_show, wan_enable_store);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004440
4441/* --------------------------------------------------------------------- */
4442
4443static struct attribute *wan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02004444 &dev_attr_wwan_enable.attr,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004445 NULL
4446};
4447
4448static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004449 .attrs = wan_attributes,
4450};
4451
Johannes Berg19d337d2009-06-02 13:01:37 +02004452static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4453 .get_status = wan_get_status,
4454 .set_status = wan_set_status,
4455};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004456
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004457static void wan_shutdown(void)
4458{
4459 /* Order firmware to save current state to NVRAM */
4460 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4461 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004462 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004463 else
4464 vdbg_printk(TPACPI_DBG_RFKILL,
4465 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004466}
4467
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004468static void wan_exit(void)
4469{
4470 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4471 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004472
4473 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4474
4475 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004476}
4477
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004478static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004479{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004480 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004481 int status = 0;
4482
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004483 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4484 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004485
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004486 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004487
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004488 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004489 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004490
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004491 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4492 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004493 str_supported(tp_features.wan),
4494 status);
4495
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004496#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4497 if (dbg_wwanemul) {
4498 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004499 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004500 } else
4501#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004502 if (tp_features.wan &&
4503 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4504 /* no wan hardware present in system */
4505 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004506 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004507 "wan hardware not installed\n");
4508 }
4509
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004510 if (!tp_features.wan)
4511 return 1;
4512
Johannes Berg19d337d2009-06-02 13:01:37 +02004513 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4514 &wan_tprfk_ops,
4515 RFKILL_TYPE_WWAN,
4516 TPACPI_RFK_WWAN_SW_NAME,
4517 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004518 if (res)
4519 return res;
4520
Johannes Berg19d337d2009-06-02 13:01:37 +02004521 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4522 &wan_attr_group);
4523
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004524 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004525 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004526 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004527 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004528
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004529 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004530}
4531
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004532/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004533static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004534{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004535 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004536}
4537
4538static int wan_write(char *buf)
4539{
Johannes Berg19d337d2009-06-02 13:01:37 +02004540 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004541}
4542
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004543static struct ibm_struct wan_driver_data = {
4544 .name = "wan",
4545 .read = wan_read,
4546 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004547 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004548 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004549};
4550
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004551/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004552 * UWB subdriver
4553 */
4554
4555enum {
4556 /* ACPI GUWB/SUWB bits */
4557 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4558 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4559};
4560
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004561#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4562
Johannes Berg19d337d2009-06-02 13:01:37 +02004563static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004564{
4565 int status;
4566
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004567#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4568 if (dbg_uwbemul)
4569 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004570 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004571#endif
4572
4573 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4574 return -EIO;
4575
4576 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004577 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004578}
4579
Johannes Berg19d337d2009-06-02 13:01:37 +02004580static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004581{
4582 int status;
4583
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004584 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004585 "will attempt to %s UWB\n",
4586 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004587
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004588#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4589 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004590 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004591 return 0;
4592 }
4593#endif
4594
Johannes Berg19d337d2009-06-02 13:01:37 +02004595 if (state == TPACPI_RFK_RADIO_ON)
4596 status = TP_ACPI_UWB_RADIOSSW;
4597 else
4598 status = 0;
4599
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004600 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4601 return -EIO;
4602
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004603 return 0;
4604}
4605
4606/* --------------------------------------------------------------------- */
4607
Johannes Berg19d337d2009-06-02 13:01:37 +02004608static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4609 .get_status = uwb_get_status,
4610 .set_status = uwb_set_status,
4611};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004612
4613static void uwb_exit(void)
4614{
Johannes Berg19d337d2009-06-02 13:01:37 +02004615 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004616}
4617
4618static int __init uwb_init(struct ibm_init_struct *iibm)
4619{
4620 int res;
4621 int status = 0;
4622
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004623 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4624 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004625
4626 TPACPI_ACPIHANDLE_INIT(hkey);
4627
4628 tp_features.uwb = hkey_handle &&
4629 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4630
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004631 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4632 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004633 str_supported(tp_features.uwb),
4634 status);
4635
4636#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4637 if (dbg_uwbemul) {
4638 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004639 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004640 } else
4641#endif
4642 if (tp_features.uwb &&
4643 !(status & TP_ACPI_UWB_HWPRESENT)) {
4644 /* no uwb hardware present in system */
4645 tp_features.uwb = 0;
4646 dbg_printk(TPACPI_DBG_INIT,
4647 "uwb hardware not installed\n");
4648 }
4649
4650 if (!tp_features.uwb)
4651 return 1;
4652
4653 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004654 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004655 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004656 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004657 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004658 return res;
4659}
4660
4661static struct ibm_struct uwb_driver_data = {
4662 .name = "uwb",
4663 .exit = uwb_exit,
4664 .flags.experimental = 1,
4665};
4666
4667/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004668 * Video subdriver
4669 */
4670
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004671#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4672
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004673enum video_access_mode {
4674 TPACPI_VIDEO_NONE = 0,
4675 TPACPI_VIDEO_570, /* 570 */
4676 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4677 TPACPI_VIDEO_NEW, /* all others */
4678};
4679
4680enum { /* video status flags, based on VIDEO_570 */
4681 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4682 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4683 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4684};
4685
4686enum { /* TPACPI_VIDEO_570 constants */
4687 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4688 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4689 * video_status_flags */
4690 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4691 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4692};
4693
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004694static enum video_access_mode video_supported;
4695static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004696
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004697static int video_autosw_get(void);
4698static int video_autosw_set(int enable);
4699
Joe Perches112a6ee2011-04-04 10:06:24 -07004700TPACPI_HANDLE(vid, root,
4701 "\\_SB.PCI.AGP.VGA", /* 570 */
4702 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4703 "\\_SB.PCI0.VID0", /* 770e */
4704 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4705 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4706 "\\_SB.PCI0.AGP.VID", /* all others */
4707 ); /* R30, R31 */
4708
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004709TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004710
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004711static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004713 int ivga;
4714
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004715 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4716
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004717 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004718 if (tpacpi_is_ibm())
4719 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004720
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004721 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4722 /* G41, assume IVGA doesn't change */
4723 vid_handle = vid2_handle;
4724
4725 if (!vid_handle)
4726 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004727 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004728 else if (tpacpi_is_ibm() &&
4729 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004730 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004731 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004732 else if (tpacpi_is_ibm() &&
4733 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004734 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004735 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004736 else
4737 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004738 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004740 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4741 str_supported(video_supported != TPACPI_VIDEO_NONE),
4742 video_supported);
4743
Jan van den Berg72a979f2014-09-17 00:01:08 +02004744 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745}
4746
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004747static void video_exit(void)
4748{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004749 dbg_printk(TPACPI_DBG_EXIT,
4750 "restoring original video autoswitch mode\n");
4751 if (video_autosw_set(video_orig_autosw))
Joe Perches0978e012011-04-04 10:06:25 -07004752 pr_err("error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004753 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004754}
4755
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004756static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757{
4758 int status = 0;
4759 int i;
4760
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004761 switch (video_supported) {
4762 case TPACPI_VIDEO_570:
4763 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4764 TP_ACPI_VIDEO_570_PHSCMD))
4765 return -EIO;
4766 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4767 break;
4768 case TPACPI_VIDEO_770:
4769 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4770 return -EIO;
4771 if (i)
4772 status |= TP_ACPI_VIDEO_S_LCD;
4773 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4774 return -EIO;
4775 if (i)
4776 status |= TP_ACPI_VIDEO_S_CRT;
4777 break;
4778 case TPACPI_VIDEO_NEW:
4779 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4780 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4781 return -EIO;
4782 if (i)
4783 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004785 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4786 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4787 return -EIO;
4788 if (i)
4789 status |= TP_ACPI_VIDEO_S_LCD;
4790 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4791 return -EIO;
4792 if (i)
4793 status |= TP_ACPI_VIDEO_S_DVI;
4794 break;
4795 default:
4796 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
4799 return status;
4800}
4801
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004802static int video_outputsw_set(int status)
4803{
4804 int autosw;
4805 int res = 0;
4806
4807 switch (video_supported) {
4808 case TPACPI_VIDEO_570:
4809 res = acpi_evalf(NULL, NULL,
4810 "\\_SB.PHS2", "vdd",
4811 TP_ACPI_VIDEO_570_PHS2CMD,
4812 status | TP_ACPI_VIDEO_570_PHS2SET);
4813 break;
4814 case TPACPI_VIDEO_770:
4815 autosw = video_autosw_get();
4816 if (autosw < 0)
4817 return autosw;
4818
4819 res = video_autosw_set(1);
4820 if (res)
4821 return res;
4822 res = acpi_evalf(vid_handle, NULL,
4823 "ASWT", "vdd", status * 0x100, 0);
4824 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004825 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004826 return -EIO;
4827 }
4828 break;
4829 case TPACPI_VIDEO_NEW:
4830 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004831 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004832 break;
4833 default:
4834 return -ENOSYS;
4835 }
4836
Jan van den Berg72a979f2014-09-17 00:01:08 +02004837 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004838}
4839
4840static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004842 int autosw = 0;
4843
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004844 switch (video_supported) {
4845 case TPACPI_VIDEO_570:
4846 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4847 return -EIO;
4848 break;
4849 case TPACPI_VIDEO_770:
4850 case TPACPI_VIDEO_NEW:
4851 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4852 return -EIO;
4853 break;
4854 default:
4855 return -ENOSYS;
4856 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004857
4858 return autosw & 1;
4859}
4860
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004861static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004862{
Jan van den Berg72a979f2014-09-17 00:01:08 +02004863 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004864 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004865 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004866}
4867
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004868static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004869{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004870 int autosw = video_autosw_get();
4871 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004872
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004873 if (autosw < 0)
4874 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004875
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004876 switch (video_supported) {
4877 case TPACPI_VIDEO_570:
4878 res = video_autosw_set(1);
4879 if (res)
4880 return res;
4881 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4882 break;
4883 case TPACPI_VIDEO_770:
4884 case TPACPI_VIDEO_NEW:
4885 res = video_autosw_set(1);
4886 if (res)
4887 return res;
4888 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4889 break;
4890 default:
4891 return -ENOSYS;
4892 }
4893 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004894 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004895 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004896 }
4897
Jan van den Berg72a979f2014-09-17 00:01:08 +02004898 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004899}
4900
4901static int video_expand_toggle(void)
4902{
4903 switch (video_supported) {
4904 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004905 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004906 0 : -EIO;
4907 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004908 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004909 0 : -EIO;
4910 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004911 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004912 0 : -EIO;
4913 default:
4914 return -ENOSYS;
4915 }
4916 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004917}
4918
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004919static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004920{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004921 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004922
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004923 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004924 seq_printf(m, "status:\t\tnot supported\n");
4925 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004926 }
4927
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004928 /* Even reads can crash X.org, so... */
4929 if (!capable(CAP_SYS_ADMIN))
4930 return -EPERM;
4931
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004932 status = video_outputsw_get();
4933 if (status < 0)
4934 return status;
4935
4936 autosw = video_autosw_get();
4937 if (autosw < 0)
4938 return autosw;
4939
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004940 seq_printf(m, "status:\t\tsupported\n");
4941 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4942 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004943 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004944 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4945 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4946 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4947 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004948 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004949 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4950 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4951 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004952
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004953 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004954}
4955
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004956static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957{
4958 char *cmd;
4959 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004960 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004962 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004963 return -ENODEV;
4964
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004965 /* Even reads can crash X.org, let alone writes... */
4966 if (!capable(CAP_SYS_ADMIN))
4967 return -EPERM;
4968
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004969 enable = 0;
4970 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
4972 while ((cmd = next_cmd(&buf))) {
4973 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004974 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004976 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004978 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004980 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004981 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004982 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004983 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004984 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004985 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004986 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004988 res = video_autosw_set(1);
4989 if (res)
4990 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004992 res = video_autosw_set(0);
4993 if (res)
4994 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004996 res = video_outputsw_cycle();
4997 if (res)
4998 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005000 res = video_expand_toggle();
5001 if (res)
5002 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 } else
5004 return -EINVAL;
5005 }
5006
5007 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03005008 status = video_outputsw_get();
5009 if (status < 0)
5010 return status;
5011 res = video_outputsw_set((status & ~disable) | enable);
5012 if (res)
5013 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 }
5015
5016 return 0;
5017}
5018
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005019static struct ibm_struct video_driver_data = {
5020 .name = "video",
5021 .read = video_read,
5022 .write = video_write,
5023 .exit = video_exit,
5024};
5025
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005026#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5027
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005028/*************************************************************************
Pali Rohárbb28f3d2015-12-30 23:27:41 +01005029 * Keyboard backlight subdriver
5030 */
5031
5032static int kbdlight_set_level(int level)
5033{
5034 if (!hkey_handle)
5035 return -ENXIO;
5036
5037 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5038 return -EIO;
5039
5040 return 0;
5041}
5042
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005043static int kbdlight_set_level_and_update(int level);
5044
Pali Rohárbb28f3d2015-12-30 23:27:41 +01005045static int kbdlight_get_level(void)
5046{
5047 int status = 0;
5048
5049 if (!hkey_handle)
5050 return -ENXIO;
5051
5052 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5053 return -EIO;
5054
5055 if (status < 0)
5056 return status;
5057
5058 return status & 0x3;
5059}
5060
5061static bool kbdlight_is_supported(void)
5062{
5063 int status = 0;
5064
5065 if (!hkey_handle)
5066 return false;
5067
5068 if (!acpi_has_method(hkey_handle, "MLCG")) {
5069 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5070 return false;
5071 }
5072
5073 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5074 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5075 return false;
5076 }
5077
5078 if (status < 0) {
5079 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5080 return false;
5081 }
5082
5083 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5084 /*
5085 * Guessed test for keyboard backlight:
5086 *
5087 * Machines with backlight keyboard return:
5088 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5089 * b110100010010000000XX - ThinkPad x230
5090 * b010100000010000000XX - ThinkPad x240
5091 * b010100000010000000XX - ThinkPad W541
5092 * (XX is current backlight level)
5093 *
5094 * Machines without backlight keyboard return:
5095 * b10100001000000000000 - ThinkPad x230
5096 * b10110001000000000000 - ThinkPad E430
5097 * b00000000000000000000 - ThinkPad E450
5098 *
5099 * Candidate BITs for detection test (XOR):
5100 * b01000000001000000000
5101 * ^
5102 */
5103 return status & BIT(9);
5104}
5105
5106static void kbdlight_set_worker(struct work_struct *work)
5107{
5108 struct tpacpi_led_classdev *data =
5109 container_of(work, struct tpacpi_led_classdev, work);
5110
5111 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005112 kbdlight_set_level_and_update(data->new_state);
Pali Rohárbb28f3d2015-12-30 23:27:41 +01005113}
5114
5115static void kbdlight_sysfs_set(struct led_classdev *led_cdev,
5116 enum led_brightness brightness)
5117{
5118 struct tpacpi_led_classdev *data =
5119 container_of(led_cdev,
5120 struct tpacpi_led_classdev,
5121 led_classdev);
5122 data->new_state = brightness;
5123 queue_work(tpacpi_wq, &data->work);
5124}
5125
5126static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5127{
5128 int level;
5129
5130 level = kbdlight_get_level();
5131 if (level < 0)
5132 return 0;
5133
5134 return level;
5135}
5136
5137static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5138 .led_classdev = {
5139 .name = "tpacpi::kbd_backlight",
5140 .max_brightness = 2,
5141 .brightness_set = &kbdlight_sysfs_set,
5142 .brightness_get = &kbdlight_sysfs_get,
Pali Rohárbb28f3d2015-12-30 23:27:41 +01005143 }
5144};
5145
5146static int __init kbdlight_init(struct ibm_init_struct *iibm)
5147{
5148 int rc;
5149
5150 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5151
5152 TPACPI_ACPIHANDLE_INIT(hkey);
5153 INIT_WORK(&tpacpi_led_kbdlight.work, kbdlight_set_worker);
5154
5155 if (!kbdlight_is_supported()) {
5156 tp_features.kbdlight = 0;
5157 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5158 return 1;
5159 }
5160
5161 tp_features.kbdlight = 1;
5162
5163 rc = led_classdev_register(&tpacpi_pdev->dev,
5164 &tpacpi_led_kbdlight.led_classdev);
5165 if (rc < 0) {
5166 tp_features.kbdlight = 0;
5167 return rc;
5168 }
5169
5170 return 0;
5171}
5172
5173static void kbdlight_exit(void)
5174{
5175 if (tp_features.kbdlight)
5176 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5177 flush_workqueue(tpacpi_wq);
5178}
5179
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005180static int kbdlight_set_level_and_update(int level)
5181{
5182 int ret;
5183 struct led_classdev *led_cdev;
5184
5185 ret = kbdlight_set_level(level);
5186 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5187
5188 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5189 led_cdev->brightness = level;
5190
5191 return ret;
5192}
5193
Pali Rohárbb28f3d2015-12-30 23:27:41 +01005194static int kbdlight_read(struct seq_file *m)
5195{
5196 int level;
5197
5198 if (!tp_features.kbdlight) {
5199 seq_printf(m, "status:\t\tnot supported\n");
5200 } else {
5201 level = kbdlight_get_level();
5202 if (level < 0)
5203 seq_printf(m, "status:\t\terror %d\n", level);
5204 else
5205 seq_printf(m, "status:\t\t%d\n", level);
5206 seq_printf(m, "commands:\t0, 1, 2\n");
5207 }
5208
5209 return 0;
5210}
5211
5212static int kbdlight_write(char *buf)
5213{
5214 char *cmd;
5215 int level = -1;
5216
5217 if (!tp_features.kbdlight)
5218 return -ENODEV;
5219
5220 while ((cmd = next_cmd(&buf))) {
5221 if (strlencmp(cmd, "0") == 0)
5222 level = 0;
5223 else if (strlencmp(cmd, "1") == 0)
5224 level = 1;
5225 else if (strlencmp(cmd, "2") == 0)
5226 level = 2;
5227 else
5228 return -EINVAL;
5229 }
5230
5231 if (level == -1)
5232 return -EINVAL;
5233
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005234 return kbdlight_set_level_and_update(level);
5235}
5236
5237static void kbdlight_suspend(void)
5238{
5239 struct led_classdev *led_cdev;
5240
5241 if (!tp_features.kbdlight)
5242 return;
5243
5244 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5245 led_update_brightness(led_cdev);
5246 led_classdev_suspend(led_cdev);
5247}
5248
5249static void kbdlight_resume(void)
5250{
5251 if (!tp_features.kbdlight)
5252 return;
5253
5254 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
Pali Rohárbb28f3d2015-12-30 23:27:41 +01005255}
5256
5257static struct ibm_struct kbdlight_driver_data = {
5258 .name = "kbdlight",
5259 .read = kbdlight_read,
5260 .write = kbdlight_write,
Marco Trevisan (Treviño)afcedeb2016-05-24 00:39:51 +02005261 .suspend = kbdlight_suspend,
5262 .resume = kbdlight_resume,
Pali Rohárbb28f3d2015-12-30 23:27:41 +01005263 .exit = kbdlight_exit,
5264};
5265
5266/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005267 * Light (thinklight) subdriver
5268 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005270TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5271TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005272
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005273static int light_get_status(void)
5274{
5275 int status = 0;
5276
5277 if (tp_features.light_status) {
5278 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5279 return -EIO;
5280 return (!!status);
5281 }
5282
5283 return -ENXIO;
5284}
5285
5286static int light_set_status(int status)
5287{
5288 int rc;
5289
5290 if (tp_features.light) {
5291 if (cmos_handle) {
5292 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005293 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005294 TP_CMOS_THINKLIGHT_ON :
5295 TP_CMOS_THINKLIGHT_OFF);
5296 } else {
5297 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005298 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005299 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005300 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005301 }
5302
5303 return -ENXIO;
5304}
5305
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005306static void light_set_status_worker(struct work_struct *work)
5307{
5308 struct tpacpi_led_classdev *data =
5309 container_of(work, struct tpacpi_led_classdev, work);
5310
5311 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005312 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005313}
5314
5315static void light_sysfs_set(struct led_classdev *led_cdev,
5316 enum led_brightness brightness)
5317{
5318 struct tpacpi_led_classdev *data =
5319 container_of(led_cdev,
5320 struct tpacpi_led_classdev,
5321 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005322 data->new_state = (brightness != LED_OFF) ?
5323 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005324 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005325}
5326
5327static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5328{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005329 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005330}
5331
5332static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5333 .led_classdev = {
5334 .name = "tpacpi::thinklight",
5335 .brightness_set = &light_sysfs_set,
5336 .brightness_get = &light_sysfs_get,
5337 }
5338};
5339
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005340static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005342 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005343
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005344 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5345
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005346 if (tpacpi_is_ibm()) {
5347 TPACPI_ACPIHANDLE_INIT(ledb);
5348 TPACPI_ACPIHANDLE_INIT(lght);
5349 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005350 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005351 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005352
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005353 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005354 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005355
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005356 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005357 /* light status not supported on
5358 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005359 tp_features.light_status =
5360 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005362 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5363 str_supported(tp_features.light),
5364 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005365
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005366 if (!tp_features.light)
5367 return 1;
5368
5369 rc = led_classdev_register(&tpacpi_pdev->dev,
5370 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005371
5372 if (rc < 0) {
5373 tp_features.light = 0;
5374 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005375 } else {
5376 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005377 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005378
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005379 return rc;
5380}
5381
5382static void light_exit(void)
5383{
5384 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Tejun Heo6d394e12012-12-21 17:56:58 -08005385 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386}
5387
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005388static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005390 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005392 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005393 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005394 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005395 seq_printf(m, "status:\t\tunknown\n");
5396 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005397 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005398 status = light_get_status();
5399 if (status < 0)
5400 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005401 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5402 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406}
5407
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005408static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005411 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005412
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005413 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005414 return -ENODEV;
5415
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 while ((cmd = next_cmd(&buf))) {
5417 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005418 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005420 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 } else
5422 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 }
5424
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005425 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426}
5427
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005428static struct ibm_struct light_driver_data = {
5429 .name = "light",
5430 .read = light_read,
5431 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005432 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005433};
5434
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005435/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005436 * CMOS subdriver
5437 */
5438
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005439/* sysfs cmos_command -------------------------------------------------- */
5440static ssize_t cmos_command_store(struct device *dev,
5441 struct device_attribute *attr,
5442 const char *buf, size_t count)
5443{
5444 unsigned long cmos_cmd;
5445 int res;
5446
5447 if (parse_strtoul(buf, 21, &cmos_cmd))
5448 return -EINVAL;
5449
5450 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005451 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005452}
5453
Bastien Nocerab4dd04a2015-03-02 18:40:50 +01005454static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005455
5456/* --------------------------------------------------------------------- */
5457
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005458static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005459{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005460 int res;
5461
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005462 vdbg_printk(TPACPI_DBG_INIT,
5463 "initializing cmos commands subdriver\n");
5464
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005465 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005466
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005467 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5468 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005469
5470 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5471 if (res)
5472 return res;
5473
Jan van den Berg72a979f2014-09-17 00:01:08 +02005474 return (cmos_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005475}
5476
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005477static void cmos_exit(void)
5478{
5479 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5480}
5481
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005482static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005484 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5485 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005487 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005489 seq_printf(m, "status:\t\tsupported\n");
5490 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 }
5492
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494}
5495
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005496static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497{
5498 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005499 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
5501 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005502 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5503 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 /* cmos_cmd set */
5505 } else
5506 return -EINVAL;
5507
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005508 res = issue_thinkpad_cmos_command(cmos_cmd);
5509 if (res)
5510 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 }
5512
5513 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005514}
5515
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005516static struct ibm_struct cmos_driver_data = {
5517 .name = "cmos",
5518 .read = cmos_read,
5519 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005520 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005521};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005522
5523/*************************************************************************
5524 * LED subdriver
5525 */
5526
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005527enum led_access_mode {
5528 TPACPI_LED_NONE = 0,
5529 TPACPI_LED_570, /* 570 */
5530 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5531 TPACPI_LED_NEW, /* all others */
5532};
5533
5534enum { /* For TPACPI_LED_OLD */
5535 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5536 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5537 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5538};
5539
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005540static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005541
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005542static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005543
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005544#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005545static struct tpacpi_led_classdev *tpacpi_leds;
5546static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005547static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005548 /* there's a limit of 19 chars + NULL before 2.6.26 */
5549 "tpacpi::power",
5550 "tpacpi:orange:batt",
5551 "tpacpi:green:batt",
5552 "tpacpi::dock_active",
5553 "tpacpi::bay_active",
5554 "tpacpi::dock_batt",
5555 "tpacpi::unknown_led",
5556 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005557 "tpacpi::dock_status1",
5558 "tpacpi::dock_status2",
5559 "tpacpi::unknown_led2",
5560 "tpacpi::unknown_led3",
5561 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005562};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005563#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005564
5565static inline bool tpacpi_is_led_restricted(const unsigned int led)
5566{
5567#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5568 return false;
5569#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005570 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005571#endif
5572}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005573
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005574static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005575{
5576 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005577 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005578
5579 switch (led_supported) {
5580 case TPACPI_LED_570:
5581 if (!acpi_evalf(ec_handle,
5582 &status, "GLED", "dd", 1 << led))
5583 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005584 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005585 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005586 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005587 TPACPI_LED_ON :
5588 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005589 tpacpi_led_state_cache[led] = led_s;
5590 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005591 default:
5592 return -ENXIO;
5593 }
5594
5595 /* not reached */
5596}
5597
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005598static int led_set_status(const unsigned int led,
5599 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005600{
5601 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005602 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5603 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005604
5605 int rc = 0;
5606
5607 switch (led_supported) {
5608 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005609 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005610 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005611 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005612 if (unlikely(tpacpi_is_led_restricted(led)))
5613 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005614 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5615 (1 << led), led_sled_arg1[ledstatus]))
5616 rc = -EIO;
5617 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005618 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005619 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005620 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005621 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005622 if (unlikely(tpacpi_is_led_restricted(led)))
5623 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005624 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5625 if (rc >= 0)
5626 rc = ec_write(TPACPI_LED_EC_HLBL,
5627 (ledstatus == TPACPI_LED_BLINK) << led);
5628 if (rc >= 0)
5629 rc = ec_write(TPACPI_LED_EC_HLCL,
5630 (ledstatus != TPACPI_LED_OFF) << led);
5631 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005632 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005633 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005634 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5635 return -EINVAL;
5636 if (unlikely(tpacpi_is_led_restricted(led)))
5637 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005638 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5639 led, led_led_arg1[ledstatus]))
5640 rc = -EIO;
5641 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005642 default:
5643 rc = -ENXIO;
5644 }
5645
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005646 if (!rc)
5647 tpacpi_led_state_cache[led] = ledstatus;
5648
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005649 return rc;
5650}
5651
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005652static void led_set_status_worker(struct work_struct *work)
5653{
5654 struct tpacpi_led_classdev *data =
5655 container_of(work, struct tpacpi_led_classdev, work);
5656
5657 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005658 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005659}
5660
5661static void led_sysfs_set(struct led_classdev *led_cdev,
5662 enum led_brightness brightness)
5663{
5664 struct tpacpi_led_classdev *data = container_of(led_cdev,
5665 struct tpacpi_led_classdev, led_classdev);
5666
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005667 if (brightness == LED_OFF)
5668 data->new_state = TPACPI_LED_OFF;
5669 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5670 data->new_state = TPACPI_LED_ON;
5671 else
5672 data->new_state = TPACPI_LED_BLINK;
5673
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005674 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005675}
5676
5677static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5678 unsigned long *delay_on, unsigned long *delay_off)
5679{
5680 struct tpacpi_led_classdev *data = container_of(led_cdev,
5681 struct tpacpi_led_classdev, led_classdev);
5682
5683 /* Can we choose the flash rate? */
5684 if (*delay_on == 0 && *delay_off == 0) {
5685 /* yes. set them to the hardware blink rate (1 Hz) */
5686 *delay_on = 500; /* ms */
5687 *delay_off = 500; /* ms */
5688 } else if ((*delay_on != 500) || (*delay_off != 500))
5689 return -EINVAL;
5690
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005691 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005692 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005693
5694 return 0;
5695}
5696
5697static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5698{
5699 int rc;
5700
5701 struct tpacpi_led_classdev *data = container_of(led_cdev,
5702 struct tpacpi_led_classdev, led_classdev);
5703
5704 rc = led_get_status(data->led);
5705
5706 if (rc == TPACPI_LED_OFF || rc < 0)
5707 rc = LED_OFF; /* no error handling in led class :( */
5708 else
5709 rc = LED_FULL;
5710
5711 return rc;
5712}
5713
5714static void led_exit(void)
5715{
5716 unsigned int i;
5717
5718 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5719 if (tpacpi_leds[i].led_classdev.name)
5720 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5721 }
5722
Li Dongyange03e3892012-07-25 10:45:07 +10005723 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005724 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005725}
5726
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005727static int __init tpacpi_init_led(unsigned int led)
5728{
5729 int rc;
5730
5731 tpacpi_leds[led].led = led;
5732
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005733 /* LEDs with no name don't get registered */
5734 if (!tpacpi_led_names[led])
5735 return 0;
5736
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005737 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5738 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5739 if (led_supported == TPACPI_LED_570)
5740 tpacpi_leds[led].led_classdev.brightness_get =
5741 &led_sysfs_get;
5742
5743 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5744
5745 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5746
5747 rc = led_classdev_register(&tpacpi_pdev->dev,
5748 &tpacpi_leds[led].led_classdev);
5749 if (rc < 0)
5750 tpacpi_leds[led].led_classdev.name = NULL;
5751
5752 return rc;
5753}
5754
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005755static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5756 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5757 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5758 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5759
5760 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5761 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5762 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5763 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5764 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5765 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5766 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5767 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5768
5769 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5770 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5771 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5772 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5773 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5774
5775 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5776 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5777 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5778 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5779
5780 /* (1) - may have excess leds enabled on MSB */
5781
5782 /* Defaults (order matters, keep last, don't reorder!) */
5783 { /* Lenovo */
5784 .vendor = PCI_VENDOR_ID_LENOVO,
5785 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5786 .quirks = 0x1fffU,
5787 },
5788 { /* IBM ThinkPads with no EC version string */
5789 .vendor = PCI_VENDOR_ID_IBM,
5790 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5791 .quirks = 0x00ffU,
5792 },
5793 { /* IBM ThinkPads with EC version string */
5794 .vendor = PCI_VENDOR_ID_IBM,
5795 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5796 .quirks = 0x00bfU,
5797 },
5798};
5799
5800#undef TPACPI_LEDQ_IBM
5801#undef TPACPI_LEDQ_LNV
5802
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005803static enum led_access_mode __init led_init_detect_mode(void)
5804{
5805 acpi_status status;
5806
5807 if (tpacpi_is_ibm()) {
5808 /* 570 */
5809 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5810 if (ACPI_SUCCESS(status))
5811 return TPACPI_LED_570;
5812
5813 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5814 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5815 if (ACPI_SUCCESS(status))
5816 return TPACPI_LED_OLD;
5817 }
5818
5819 /* most others */
5820 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5821 if (ACPI_SUCCESS(status))
5822 return TPACPI_LED_NEW;
5823
5824 /* R30, R31, and unknown firmwares */
5825 led_handle = NULL;
5826 return TPACPI_LED_NONE;
5827}
5828
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005829static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005830{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005831 unsigned int i;
5832 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005833 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005834
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005835 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5836
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005837 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005838
Adam Leefcb44e12013-06-07 16:20:08 +08005839 if (led_supported != TPACPI_LED_NONE) {
5840 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5841 ARRAY_SIZE(led_useful_qtable));
5842
5843 if (!useful_leds) {
5844 led_handle = NULL;
5845 led_supported = TPACPI_LED_NONE;
5846 }
5847 }
5848
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005849 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5850 str_supported(led_supported), led_supported);
5851
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005852 if (led_supported == TPACPI_LED_NONE)
5853 return 1;
5854
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005855 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5856 GFP_KERNEL);
5857 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005858 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005859 return -ENOMEM;
5860 }
5861
5862 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08005863 tpacpi_leds[i].led = -1;
5864
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005865 if (!tpacpi_is_led_restricted(i) &&
5866 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005867 rc = tpacpi_init_led(i);
5868 if (rc < 0) {
5869 led_exit();
5870 return rc;
5871 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005872 }
5873 }
5874
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005875#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Joe Perches0978e012011-04-04 10:06:25 -07005876 pr_notice("warning: userspace override of important "
5877 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005878#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005879 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005880}
5881
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005882#define str_led_status(s) \
5883 ((s) == TPACPI_LED_OFF ? "off" : \
5884 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005885
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005886static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005888 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005889 seq_printf(m, "status:\t\tnot supported\n");
5890 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005891 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005892 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005893
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005894 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005895 /* 570 */
5896 int i, status;
5897 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005898 status = led_get_status(i);
5899 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005900 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005901 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005902 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005903 }
5904 }
5905
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005906 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005907 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005909 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910}
5911
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005912static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913{
5914 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005915 int led, rc;
5916 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005917
5918 if (!led_supported)
5919 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920
5921 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08005922 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 return -EINVAL;
5924
Adam Leeedf2d772013-06-08 16:51:15 +08005925 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5926 tpacpi_leds[led].led < 0)
5927 return -ENODEV;
5928
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005929 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005930 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005932 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005933 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005934 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005935 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005936 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005937 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005938
5939 rc = led_set_status(led, s);
5940 if (rc < 0)
5941 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 }
5943
5944 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005945}
5946
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005947static struct ibm_struct led_driver_data = {
5948 .name = "led",
5949 .read = led_read,
5950 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005951 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005952};
5953
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005954/*************************************************************************
5955 * Beep subdriver
5956 */
5957
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005958TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005959
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005960#define TPACPI_BEEP_Q1 0x0001
5961
5962static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5963 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5964 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5965};
5966
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005967static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005968{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005969 unsigned long quirks;
5970
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005971 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5972
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005973 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005974
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005975 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5976 str_supported(beep_handle != NULL));
5977
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005978 quirks = tpacpi_check_quirks(beep_quirk_table,
5979 ARRAY_SIZE(beep_quirk_table));
5980
5981 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5982
Jan van den Berg72a979f2014-09-17 00:01:08 +02005983 return (beep_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005984}
5985
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005986static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005988 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005989 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005990 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005991 seq_printf(m, "status:\t\tsupported\n");
5992 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005995 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996}
5997
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005998static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999{
6000 char *cmd;
6001 int beep_cmd;
6002
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006003 if (!beep_handle)
6004 return -ENODEV;
6005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006007 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6008 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 /* beep_cmd set */
6010 } else
6011 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03006012 if (tp_features.beep_needs_two_args) {
6013 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6014 beep_cmd, 0))
6015 return -EIO;
6016 } else {
6017 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6018 beep_cmd))
6019 return -EIO;
6020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 }
6022
6023 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006024}
6025
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006026static struct ibm_struct beep_driver_data = {
6027 .name = "beep",
6028 .read = beep_read,
6029 .write = beep_write,
6030};
6031
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006032/*************************************************************************
6033 * Thermal subdriver
6034 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006035
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006036enum thermal_access_mode {
6037 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6038 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6039 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6040 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6041 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6042};
6043
6044enum { /* TPACPI_THERMAL_TPEC_* */
6045 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6046 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6047 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006048
6049 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006050};
6051
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006052
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006053#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6054struct ibm_thermal_sensors_struct {
6055 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6056};
6057
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006058static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006059
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006060/* idx is zero-based */
6061static int thermal_get_sensor(int idx, s32 *value)
6062{
6063 int t;
6064 s8 tmp;
6065 char tmpi[5];
6066
6067 t = TP_EC_THERMAL_TMP0;
6068
6069 switch (thermal_read_mode) {
6070#if TPACPI_MAX_THERMAL_SENSORS >= 16
6071 case TPACPI_THERMAL_TPEC_16:
6072 if (idx >= 8 && idx <= 15) {
6073 t = TP_EC_THERMAL_TMP8;
6074 idx -= 8;
6075 }
6076 /* fallthrough */
6077#endif
6078 case TPACPI_THERMAL_TPEC_8:
6079 if (idx <= 7) {
6080 if (!acpi_ec_read(t + idx, &tmp))
6081 return -EIO;
6082 *value = tmp * 1000;
6083 return 0;
6084 }
6085 break;
6086
6087 case TPACPI_THERMAL_ACPI_UPDT:
6088 if (idx <= 7) {
6089 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6090 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6091 return -EIO;
6092 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6093 return -EIO;
6094 *value = (t - 2732) * 100;
6095 return 0;
6096 }
6097 break;
6098
6099 case TPACPI_THERMAL_ACPI_TMP07:
6100 if (idx <= 7) {
6101 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6102 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6103 return -EIO;
6104 if (t > 127 || t < -127)
6105 t = TP_EC_THERMAL_TMP_NA;
6106 *value = t * 1000;
6107 return 0;
6108 }
6109 break;
6110
6111 case TPACPI_THERMAL_NONE:
6112 default:
6113 return -ENOSYS;
6114 }
6115
6116 return -EINVAL;
6117}
6118
6119static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6120{
6121 int res, i;
6122 int n;
6123
6124 n = 8;
6125 i = 0;
6126
6127 if (!s)
6128 return -EINVAL;
6129
6130 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6131 n = 16;
6132
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006133 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006134 res = thermal_get_sensor(i, &s->temp[i]);
6135 if (res)
6136 return res;
6137 }
6138
6139 return n;
6140}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006141
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006142static void thermal_dump_all_sensors(void)
6143{
6144 int n, i;
6145 struct ibm_thermal_sensors_struct t;
6146
6147 n = thermal_get_sensors(&t);
6148 if (n <= 0)
6149 return;
6150
Joe Perches0978e012011-04-04 10:06:25 -07006151 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006152
6153 for (i = 0; i < n; i++) {
6154 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07006155 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006156 else
Joe Perches0978e012011-04-04 10:06:25 -07006157 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006158 }
6159
Joe Perches0978e012011-04-04 10:06:25 -07006160 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006161}
6162
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006163/* sysfs temp##_input -------------------------------------------------- */
6164
6165static ssize_t thermal_temp_input_show(struct device *dev,
6166 struct device_attribute *attr,
6167 char *buf)
6168{
6169 struct sensor_device_attribute *sensor_attr =
6170 to_sensor_dev_attr(attr);
6171 int idx = sensor_attr->index;
6172 s32 value;
6173 int res;
6174
6175 res = thermal_get_sensor(idx, &value);
6176 if (res)
6177 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00006178 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006179 return -ENXIO;
6180
6181 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6182}
6183
6184#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006185 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6186 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006187
6188static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6189 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6190 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6191 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6192 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6193 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6194 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6195 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6196 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6197 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6198 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6199 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6200 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6201 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6202 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6203 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6204 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6205};
6206
6207#define THERMAL_ATTRS(X) \
6208 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6209
6210static struct attribute *thermal_temp_input_attr[] = {
6211 THERMAL_ATTRS(8),
6212 THERMAL_ATTRS(9),
6213 THERMAL_ATTRS(10),
6214 THERMAL_ATTRS(11),
6215 THERMAL_ATTRS(12),
6216 THERMAL_ATTRS(13),
6217 THERMAL_ATTRS(14),
6218 THERMAL_ATTRS(15),
6219 THERMAL_ATTRS(0),
6220 THERMAL_ATTRS(1),
6221 THERMAL_ATTRS(2),
6222 THERMAL_ATTRS(3),
6223 THERMAL_ATTRS(4),
6224 THERMAL_ATTRS(5),
6225 THERMAL_ATTRS(6),
6226 THERMAL_ATTRS(7),
6227 NULL
6228};
6229
6230static const struct attribute_group thermal_temp_input16_group = {
6231 .attrs = thermal_temp_input_attr
6232};
6233
6234static const struct attribute_group thermal_temp_input8_group = {
6235 .attrs = &thermal_temp_input_attr[8]
6236};
6237
6238#undef THERMAL_SENSOR_ATTR_TEMP
6239#undef THERMAL_ATTRS
6240
6241/* --------------------------------------------------------------------- */
6242
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006243static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006244{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006245 u8 t, ta1, ta2;
6246 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006247 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006248 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006249
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006250 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6251
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006252 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006253
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03006254 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006255 /*
6256 * Direct EC access mode: sensors at registers
6257 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6258 * non-implemented, thermal sensors return 0x80 when
6259 * not available
6260 */
6261
6262 ta1 = ta2 = 0;
6263 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006264 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006265 ta1 |= t;
6266 } else {
6267 ta1 = 0;
6268 break;
6269 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03006270 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006271 ta2 |= t;
6272 } else {
6273 ta1 = 0;
6274 break;
6275 }
6276 }
6277 if (ta1 == 0) {
6278 /* This is sheer paranoia, but we handle it anyway */
6279 if (acpi_tmp7) {
Joe Perches0978e012011-04-04 10:06:25 -07006280 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006281 "falling back to ACPI TMPx access "
6282 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006283 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006284 } else {
Joe Perches0978e012011-04-04 10:06:25 -07006285 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006286 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006287 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006288 }
6289 } else {
6290 thermal_read_mode =
6291 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006292 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006293 }
6294 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006295 if (tpacpi_is_ibm() &&
6296 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006297 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006298 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006299 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006300 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006301 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006302 }
6303 } else {
6304 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006305 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006306 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006307
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006308 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6309 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6310 thermal_read_mode);
6311
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006312 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006313 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006314 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006315 &thermal_temp_input16_group);
6316 if (res)
6317 return res;
6318 break;
6319 case TPACPI_THERMAL_TPEC_8:
6320 case TPACPI_THERMAL_ACPI_TMP07:
6321 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006322 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006323 &thermal_temp_input8_group);
6324 if (res)
6325 return res;
6326 break;
6327 case TPACPI_THERMAL_NONE:
6328 default:
6329 return 1;
6330 }
6331
6332 return 0;
6333}
6334
6335static void thermal_exit(void)
6336{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006337 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006338 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006339 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006340 &thermal_temp_input16_group);
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 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08006346 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006347 break;
6348 case TPACPI_THERMAL_NONE:
6349 default:
6350 break;
6351 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006352}
6353
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006354static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006355{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006356 int n, i;
6357 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006358
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006359 n = thermal_get_sensors(&t);
6360 if (unlikely(n < 0))
6361 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006362
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006363 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006364
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006365 if (n > 0) {
6366 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006367 seq_printf(m, "%d ", t.temp[i] / 1000);
6368 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006369 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006370 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006371
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006372 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006373}
6374
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006375static struct ibm_struct thermal_driver_data = {
6376 .name = "thermal",
6377 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006378 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006379};
6380
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006381/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006382 * Backlight/brightness subdriver
6383 */
Holger Macht8acb0252006-10-20 14:30:28 -07006384
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006385#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6386
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006387/*
6388 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6389 * CMOS NVRAM byte 0x5E, bits 0-3.
6390 *
6391 * EC HBRV (0x31) has the following layout
6392 * Bit 7: unknown function
6393 * Bit 6: unknown function
6394 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6395 * Bit 4: must be set to zero to avoid problems
6396 * Bit 3-0: backlight brightness level
6397 *
6398 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006399 *
6400 * WARNING: The X61 has been verified to use HBRV for something else, so
6401 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6402 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006403 */
6404
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006405enum {
6406 TP_EC_BACKLIGHT = 0x31,
6407
6408 /* TP_EC_BACKLIGHT bitmasks */
6409 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6410 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6411 TP_EC_BACKLIGHT_MAPSW = 0x20,
6412};
6413
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006414enum tpacpi_brightness_access_mode {
6415 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6416 TPACPI_BRGHT_MODE_EC, /* EC control */
6417 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6418 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6419 TPACPI_BRGHT_MODE_MAX
6420};
6421
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03006422static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006423
6424static enum tpacpi_brightness_access_mode brightness_mode =
6425 TPACPI_BRGHT_MODE_MAX;
6426
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006427static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6428
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006429static struct mutex brightness_mutex;
6430
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006431/* NVRAM brightness access,
6432 * call with brightness_mutex held! */
6433static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006434{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006435 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006436
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006437 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6438 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6439 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006440 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006441
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006442 return lnvram;
6443}
6444
6445static void tpacpi_brightness_checkpoint_nvram(void)
6446{
6447 u8 lec = 0;
6448 u8 b_nvram;
6449
6450 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6451 return;
6452
6453 vdbg_printk(TPACPI_DBG_BRGHT,
6454 "trying to checkpoint backlight level to NVRAM...\n");
6455
6456 if (mutex_lock_killable(&brightness_mutex) < 0)
6457 return;
6458
6459 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6460 goto unlock;
6461 lec &= TP_EC_BACKLIGHT_LVLMSK;
6462 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6463
6464 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6465 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6466 /* NVRAM needs update */
6467 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6468 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6469 b_nvram |= lec;
6470 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6471 dbg_printk(TPACPI_DBG_BRGHT,
6472 "updated NVRAM backlight level to %u (0x%02x)\n",
6473 (unsigned int) lec, (unsigned int) b_nvram);
6474 } else
6475 vdbg_printk(TPACPI_DBG_BRGHT,
6476 "NVRAM backlight level already is %u (0x%02x)\n",
6477 (unsigned int) lec, (unsigned int) b_nvram);
6478
6479unlock:
6480 mutex_unlock(&brightness_mutex);
6481}
6482
6483
6484/* call with brightness_mutex held! */
6485static int tpacpi_brightness_get_raw(int *status)
6486{
6487 u8 lec = 0;
6488
6489 switch (brightness_mode) {
6490 case TPACPI_BRGHT_MODE_UCMS_STEP:
6491 *status = tpacpi_brightness_nvram_get();
6492 return 0;
6493 case TPACPI_BRGHT_MODE_EC:
6494 case TPACPI_BRGHT_MODE_ECNVRAM:
6495 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006496 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006497 *status = lec;
6498 return 0;
6499 default:
6500 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006501 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006502}
6503
6504/* call with brightness_mutex held! */
6505/* do NOT call with illegal backlight level value */
6506static int tpacpi_brightness_set_ec(unsigned int value)
6507{
6508 u8 lec = 0;
6509
6510 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6511 return -EIO;
6512
6513 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6514 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6515 (value & TP_EC_BACKLIGHT_LVLMSK))))
6516 return -EIO;
6517
6518 return 0;
6519}
6520
6521/* call with brightness_mutex held! */
6522static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6523{
6524 int cmos_cmd, inc;
6525 unsigned int current_value, i;
6526
6527 current_value = tpacpi_brightness_nvram_get();
6528
6529 if (value == current_value)
6530 return 0;
6531
6532 cmos_cmd = (value > current_value) ?
6533 TP_CMOS_BRIGHTNESS_UP :
6534 TP_CMOS_BRIGHTNESS_DOWN;
6535 inc = (value > current_value) ? 1 : -1;
6536
6537 for (i = current_value; i != value; i += inc)
6538 if (issue_thinkpad_cmos_command(cmos_cmd))
6539 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006540
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006541 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006542}
6543
6544/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006545static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006546{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006547 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006548
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006549 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006550 return -EINVAL;
6551
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006552 vdbg_printk(TPACPI_DBG_BRGHT,
6553 "set backlight level to %d\n", value);
6554
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006555 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006556 if (res < 0)
6557 return res;
6558
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006559 switch (brightness_mode) {
6560 case TPACPI_BRGHT_MODE_EC:
6561 case TPACPI_BRGHT_MODE_ECNVRAM:
6562 res = tpacpi_brightness_set_ec(value);
6563 break;
6564 case TPACPI_BRGHT_MODE_UCMS_STEP:
6565 res = tpacpi_brightness_set_ucmsstep(value);
6566 break;
6567 default:
6568 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006569 }
6570
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006571 mutex_unlock(&brightness_mutex);
6572 return res;
6573}
6574
6575/* sysfs backlight class ----------------------------------------------- */
6576
6577static int brightness_update_status(struct backlight_device *bd)
6578{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006579 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006580 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6581 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006582 bd->props.brightness : 0;
6583
6584 dbg_printk(TPACPI_DBG_BRGHT,
6585 "backlight: attempt to set level to %d\n",
6586 level);
6587
6588 /* it is the backlight class's job (caller) to handle
6589 * EINTR and other errors properly */
6590 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006591}
Holger Macht8acb0252006-10-20 14:30:28 -07006592
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006593static int brightness_get(struct backlight_device *bd)
6594{
6595 int status, res;
6596
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006597 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006598 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006599 return 0;
6600
6601 res = tpacpi_brightness_get_raw(&status);
6602
6603 mutex_unlock(&brightness_mutex);
6604
6605 if (res < 0)
6606 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006607
6608 return status & TP_EC_BACKLIGHT_LVLMSK;
6609}
6610
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006611static void tpacpi_brightness_notify_change(void)
6612{
6613 backlight_force_update(ibm_backlight_device,
6614 BACKLIGHT_UPDATE_HOTKEY);
6615}
6616
Lionel Debrouxacc24722010-11-16 14:14:02 +01006617static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006618 .get_brightness = brightness_get,
6619 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006620};
6621
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006622/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006623
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006624/*
6625 * Call _BCL method of video device. On some ThinkPads this will
6626 * switch the firmware to the ACPI brightness control mode.
6627 */
6628
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006629static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6630{
6631 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6632 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006633 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006634 int rc;
6635
Aaron Lu46445b62013-10-11 21:27:46 +08006636 if (acpi_bus_get_device(handle, &device))
6637 return 0;
6638
6639 rc = 0;
6640 list_for_each_entry(child, &device->children, node) {
6641 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6642 NULL, &buffer);
6643 if (ACPI_FAILURE(status))
6644 continue;
6645
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006646 obj = (union acpi_object *)buffer.pointer;
6647 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006648 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006649 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006650 rc = 0;
6651 } else {
6652 rc = obj->package.count;
6653 }
Aaron Lu46445b62013-10-11 21:27:46 +08006654 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006655 }
6656
6657 kfree(buffer.pointer);
6658 return rc;
6659}
6660
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006661
6662/*
6663 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6664 */
6665static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6666{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006667 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006668 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006669
Aaron Lu46445b62013-10-11 21:27:46 +08006670 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006671 if (video_device)
6672 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006673
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006674 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006675
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006676 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006677}
6678
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006679/*
6680 * These are only useful for models that have only one possibility
6681 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6682 * these quirks.
6683 */
6684#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6685#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6686#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6687
6688static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6689 /* Models with ATI GPUs known to require ECNVRAM mode */
6690 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6691
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006692 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006693 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006694 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006695 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006696 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6697
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006698 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006699 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006700 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6701 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006702
6703 /* Models with Intel GMA900 */
6704 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6705 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6706 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6707};
6708
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006709/*
6710 * Returns < 0 for error, otherwise sets tp_features.bright_*
6711 * and bright_maxlvl.
6712 */
6713static void __init tpacpi_detect_brightness_capabilities(void)
6714{
6715 unsigned int b;
6716
6717 vdbg_printk(TPACPI_DBG_INIT,
6718 "detecting firmware brightness interface capabilities\n");
6719
6720 /* we could run a quirks check here (same table used by
6721 * brightness_init) if needed */
6722
6723 /*
6724 * We always attempt to detect acpi support, so as to switch
6725 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6726 * going to publish a backlight interface
6727 */
6728 b = tpacpi_check_std_acpi_brightness_support();
6729 switch (b) {
6730 case 16:
6731 bright_maxlvl = 15;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006732 break;
6733 case 8:
6734 case 0:
6735 bright_maxlvl = 7;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006736 break;
6737 default:
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006738 tp_features.bright_unkfw = 1;
6739 bright_maxlvl = b - 1;
6740 }
Eric Curtin15c75622016-01-30 16:55:59 +00006741 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006742}
6743
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006744static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006745{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006746 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006747 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006748 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006749
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006750 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6751
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006752 mutex_init(&brightness_mutex);
6753
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006754 quirks = tpacpi_check_quirks(brightness_quirk_table,
6755 ARRAY_SIZE(brightness_quirk_table));
6756
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006757 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006758
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006759 /* if it is unknown, we don't handle it: it wouldn't be safe */
6760 if (tp_features.bright_unkfw)
6761 return 1;
6762
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006763 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006764 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006765 "brightness support disabled by "
6766 "module parameter\n");
6767 return 1;
6768 }
6769
Hans de Goedeb33c6ce2015-06-16 16:28:10 +02006770 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006771 if (brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006772 pr_info("Standard ACPI backlight interface "
6773 "available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006774 return 1;
6775 } else if (brightness_enable == 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006776 pr_warn("Cannot enable backlight brightness support, "
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006777 "ACPI is already handling it. Refer to the "
Joe Perches0978e012011-04-04 10:06:25 -07006778 "acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006779 return 1;
6780 }
6781 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006782 pr_notice("Standard ACPI backlight interface not "
6783 "available, thinkpad_acpi native "
6784 "brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006785 }
6786
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006787 /*
6788 * Check for module parameter bogosity, note that we
6789 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6790 * able to detect "unspecified"
6791 */
6792 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006793 return -EINVAL;
6794
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006795 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6796 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6797 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006798 if (quirks & TPACPI_BRGHT_Q_EC)
6799 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6800 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006801 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6802
6803 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006804 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006805 brightness_mode);
6806 }
6807
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006808 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006809 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006810 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6811 brightness_mode == TPACPI_BRGHT_MODE_EC))
6812 return -EINVAL;
6813
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006814 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006815 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006816
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006817 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006818 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006819 props.max_brightness = bright_maxlvl;
6820 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006821 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6822 NULL, NULL,
6823 &ibm_backlight_data,
6824 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006825 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006826 int rc = PTR_ERR(ibm_backlight_device);
6827 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006828 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006829 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006830 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006831 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6832 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006833
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006834 if (quirks & TPACPI_BRGHT_Q_ASK) {
Joe Perches0978e012011-04-04 10:06:25 -07006835 pr_notice("brightness: will use unverified default: "
6836 "brightness_mode=%d\n", brightness_mode);
6837 pr_notice("brightness: please report to %s whether it works well "
6838 "or not on your ThinkPad\n", TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006839 }
6840
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006841 /* Added by mistake in early 2007. Probably useless, but it could
6842 * be working around some unknown firmware problem where the value
6843 * read at startup doesn't match the real hardware state... so leave
6844 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006845 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006846
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006847 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6848 "brightness: registering brightness hotkeys "
6849 "as change notification\n");
6850 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6851 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006852 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006853 return 0;
6854}
6855
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006856static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006857{
6858 tpacpi_brightness_checkpoint_nvram();
6859}
6860
6861static void brightness_shutdown(void)
6862{
6863 tpacpi_brightness_checkpoint_nvram();
6864}
6865
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006866static void brightness_exit(void)
6867{
6868 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006869 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006870 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006871 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006872 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006873
6874 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006875}
6876
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006877static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006878{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006879 int level;
6880
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006881 level = brightness_get(NULL);
6882 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006883 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006884 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006885 seq_printf(m, "level:\t\t%d\n", level);
6886 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006887 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6888 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006889 }
6890
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006891 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006892}
6893
6894static int brightness_write(char *buf)
6895{
6896 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006897 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006898 char *cmd;
6899
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006900 level = brightness_get(NULL);
6901 if (level < 0)
6902 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006903
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006904 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006905 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006906 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006907 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006908 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006909 if (level > 0)
6910 level--;
6911 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006912 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006913 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006914 } else
6915 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006916 }
6917
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006918 tpacpi_disclose_usertask("procfs brightness",
6919 "set level to %d\n", level);
6920
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006921 /*
6922 * Now we know what the final level should be, so we try to set it.
6923 * Doing it this way makes the syscall restartable in case of EINTR
6924 */
6925 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006926 if (!rc && ibm_backlight_device)
6927 backlight_force_update(ibm_backlight_device,
6928 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02006929 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006930}
6931
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006932static struct ibm_struct brightness_driver_data = {
6933 .name = "brightness",
6934 .read = brightness_read,
6935 .write = brightness_write,
6936 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006937 .suspend = brightness_suspend,
6938 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006939};
6940
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006941/*************************************************************************
6942 * Volume subdriver
6943 */
6944
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006945/*
6946 * IBM ThinkPads have a simple volume controller with MUTE gating.
6947 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6948 *
6949 * Since the *61 series (and probably also the later *60 series), Lenovo
6950 * ThinkPads only implement the MUTE gate.
6951 *
6952 * EC register 0x30
6953 * Bit 6: MUTE (1 mutes sound)
6954 * Bit 3-0: Volume
6955 * Other bits should be zero as far as we know.
6956 *
6957 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6958 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6959 * such as bit 7 which is used to detect repeated presses of MUTE,
6960 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07006961 *
6962 * On newer Lenovo ThinkPads, the EC can automatically change the volume
6963 * in response to user input. Unfortunately, this rarely works well.
6964 * The laptop changes the state of its internal MUTE gate and, on some
6965 * models, sends KEY_MUTE, causing any user code that responds to the
6966 * mute button to get confused. The hardware MUTE gate is also
6967 * unnecessary, since user code can handle the mute button without
6968 * kernel or EC help.
6969 *
6970 * To avoid confusing userspace, we simply disable all EC-based mute
6971 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006972 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006973
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006974#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6975
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006976#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6977#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6978#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6979
Takashi Iwaicab66612013-10-24 16:06:32 +02006980#if SNDRV_CARDS <= 32
6981#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
6982#else
6983#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
6984#endif
6985static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006986static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10306987static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006988
6989struct tpacpi_alsa_data {
6990 struct snd_card *card;
6991 struct snd_ctl_elem_id *ctl_mute_id;
6992 struct snd_ctl_elem_id *ctl_vol_id;
6993};
6994
6995static struct snd_card *alsa_card;
6996
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006997enum {
6998 TP_EC_AUDIO = 0x30,
6999
7000 /* TP_EC_AUDIO bits */
7001 TP_EC_AUDIO_MUTESW = 6,
7002
7003 /* TP_EC_AUDIO bitmasks */
7004 TP_EC_AUDIO_LVL_MSK = 0x0F,
7005 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7006
7007 /* Maximum volume */
7008 TP_EC_VOLUME_MAX = 14,
7009};
7010
7011enum tpacpi_volume_access_mode {
7012 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7013 TPACPI_VOL_MODE_EC, /* Pure EC control */
7014 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7015 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7016 TPACPI_VOL_MODE_MAX
7017};
7018
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007019enum tpacpi_volume_capabilities {
7020 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7021 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7022 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7023 TPACPI_VOL_CAP_MAX
7024};
7025
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007026enum tpacpi_mute_btn_mode {
7027 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7028 /* We don't know what mode 1 is. */
7029 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7030 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7031};
7032
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007033static enum tpacpi_volume_access_mode volume_mode =
7034 TPACPI_VOL_MODE_MAX;
7035
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007036static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307037static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007038static bool software_mute_requested = true;
7039static bool software_mute_active;
7040static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007041
7042/*
7043 * Used to syncronize writers to TP_EC_AUDIO and
7044 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7045 */
7046static struct mutex volume_mutex;
7047
7048static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007049{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007050 u8 lec = 0;
7051 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007052 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007053
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007054 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7055 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007056 if (!volume_control_allowed)
7057 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007058 if (software_mute_active)
7059 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007060
7061 vdbg_printk(TPACPI_DBG_MIXER,
7062 "trying to checkpoint mixer state to NVRAM...\n");
7063
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007064 if (tp_features.mixer_no_level_control)
7065 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7066 else
7067 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7068
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007069 if (mutex_lock_killable(&volume_mutex) < 0)
7070 return;
7071
7072 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7073 goto unlock;
7074 lec &= ec_mask;
7075 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7076
7077 if (lec != (b_nvram & ec_mask)) {
7078 /* NVRAM needs update */
7079 b_nvram &= ~ec_mask;
7080 b_nvram |= lec;
7081 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7082 dbg_printk(TPACPI_DBG_MIXER,
7083 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7084 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007085 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007086 vdbg_printk(TPACPI_DBG_MIXER,
7087 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7088 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007089 }
7090
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007091unlock:
7092 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007093}
7094
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007095static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007096{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007097 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007098
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007099 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7100 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007101
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007102 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007103
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007104 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007105
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007106 return 0;
7107}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007108
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007109static int volume_get_status(u8 *status)
7110{
7111 return volume_get_status_ec(status);
7112}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007113
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007114static int volume_set_status_ec(const u8 status)
7115{
7116 if (!acpi_ec_write(TP_EC_AUDIO, status))
7117 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007118
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007119 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7120
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007121 /*
7122 * On X200s, and possibly on others, it can take a while for
7123 * reads to become correct.
7124 */
7125 msleep(1);
7126
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007127 return 0;
7128}
7129
7130static int volume_set_status(const u8 status)
7131{
7132 return volume_set_status_ec(status);
7133}
7134
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007135/* returns < 0 on error, 0 on no change, 1 on change */
7136static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007137{
7138 int rc;
7139 u8 s, n;
7140
7141 if (mutex_lock_killable(&volume_mutex) < 0)
7142 return -EINTR;
7143
7144 rc = volume_get_status_ec(&s);
7145 if (rc)
7146 goto unlock;
7147
7148 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7149 s & ~TP_EC_AUDIO_MUTESW_MSK;
7150
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007151 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007152 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007153 if (!rc)
7154 rc = 1;
7155 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007156
7157unlock:
7158 mutex_unlock(&volume_mutex);
7159 return rc;
7160}
7161
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007162static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007163{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007164 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007165 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007166 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007167}
7168
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007169static int volume_set_mute(const bool mute)
7170{
7171 int rc;
7172
7173 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7174 (mute) ? "" : "un");
7175
7176 rc = __volume_set_mute_ec(mute);
7177 return (rc < 0) ? rc : 0;
7178}
7179
7180/* returns < 0 on error, 0 on no change, 1 on change */
7181static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007182{
7183 int rc;
7184 u8 s, n;
7185
7186 if (vol > TP_EC_VOLUME_MAX)
7187 return -EINVAL;
7188
7189 if (mutex_lock_killable(&volume_mutex) < 0)
7190 return -EINTR;
7191
7192 rc = volume_get_status_ec(&s);
7193 if (rc)
7194 goto unlock;
7195
7196 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7197
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007198 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007199 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007200 if (!rc)
7201 rc = 1;
7202 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007203
7204unlock:
7205 mutex_unlock(&volume_mutex);
7206 return rc;
7207}
7208
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007209static int volume_set_software_mute(bool startup)
7210{
7211 int result;
7212
7213 if (!tpacpi_is_lenovo())
7214 return -ENODEV;
7215
7216 if (startup) {
7217 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7218 "HAUM", "qd"))
7219 return -EIO;
7220
7221 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7222 "Initial HAUM setting was %d\n",
7223 software_mute_orig_mode);
7224 }
7225
7226 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7227 (int)TP_EC_MUTE_BTN_NONE))
7228 return -EIO;
7229
7230 if (result != TP_EC_MUTE_BTN_NONE)
7231 pr_warn("Unexpected SAUM result %d\n",
7232 result);
7233
7234 /*
7235 * In software mute mode, the standard codec controls take
7236 * precendence, so we unmute the ThinkPad HW switch at
7237 * startup. Just on case there are SAUM-capable ThinkPads
7238 * with level controls, set max HW volume as well.
7239 */
7240 if (tp_features.mixer_no_level_control)
7241 result = volume_set_mute(false);
7242 else
7243 result = volume_set_status(TP_EC_VOLUME_MAX);
7244
7245 if (result != 0)
7246 pr_warn("Failed to unmute the HW mute switch\n");
7247
7248 return 0;
7249}
7250
7251static void volume_exit_software_mute(void)
7252{
7253 int r;
7254
7255 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7256 || r != software_mute_orig_mode)
7257 pr_warn("Failed to restore mute mode\n");
7258}
7259
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007260static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007261{
7262 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007263 "ALSA: trying to set volume level to %hu\n", vol);
7264 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007265}
7266
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007267static void volume_alsa_notify_change(void)
7268{
7269 struct tpacpi_alsa_data *d;
7270
7271 if (alsa_card && alsa_card->private_data) {
7272 d = alsa_card->private_data;
7273 if (d->ctl_mute_id)
7274 snd_ctl_notify(alsa_card,
7275 SNDRV_CTL_EVENT_MASK_VALUE,
7276 d->ctl_mute_id);
7277 if (d->ctl_vol_id)
7278 snd_ctl_notify(alsa_card,
7279 SNDRV_CTL_EVENT_MASK_VALUE,
7280 d->ctl_vol_id);
7281 }
7282}
7283
7284static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7285 struct snd_ctl_elem_info *uinfo)
7286{
7287 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7288 uinfo->count = 1;
7289 uinfo->value.integer.min = 0;
7290 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7291 return 0;
7292}
7293
7294static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7295 struct snd_ctl_elem_value *ucontrol)
7296{
7297 u8 s;
7298 int rc;
7299
7300 rc = volume_get_status(&s);
7301 if (rc < 0)
7302 return rc;
7303
7304 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7305 return 0;
7306}
7307
7308static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7309 struct snd_ctl_elem_value *ucontrol)
7310{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007311 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7312 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007313 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007314}
7315
7316#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7317
7318static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7319 struct snd_ctl_elem_value *ucontrol)
7320{
7321 u8 s;
7322 int rc;
7323
7324 rc = volume_get_status(&s);
7325 if (rc < 0)
7326 return rc;
7327
7328 ucontrol->value.integer.value[0] =
7329 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7330 return 0;
7331}
7332
7333static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7334 struct snd_ctl_elem_value *ucontrol)
7335{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007336 tpacpi_disclose_usertask("ALSA", "%smute\n",
7337 ucontrol->value.integer.value[0] ?
7338 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007339 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007340}
7341
Mathias Krausec3aa4722014-07-16 19:43:17 +02007342static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007343 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7344 .name = "Console Playback Volume",
7345 .index = 0,
7346 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7347 .info = volume_alsa_vol_info,
7348 .get = volume_alsa_vol_get,
7349};
7350
Mathias Krausec3aa4722014-07-16 19:43:17 +02007351static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007352 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7353 .name = "Console Playback Switch",
7354 .index = 0,
7355 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7356 .info = volume_alsa_mute_info,
7357 .get = volume_alsa_mute_get,
7358};
7359
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007360static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007361{
7362 tpacpi_volume_checkpoint_nvram();
7363}
7364
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007365static void volume_resume(void)
7366{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007367 if (software_mute_active) {
7368 if (volume_set_software_mute(false) < 0)
7369 pr_warn("Failed to restore software mute\n");
7370 } else {
7371 volume_alsa_notify_change();
7372 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007373}
7374
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007375static void volume_shutdown(void)
7376{
7377 tpacpi_volume_checkpoint_nvram();
7378}
7379
7380static void volume_exit(void)
7381{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007382 if (alsa_card) {
7383 snd_card_free(alsa_card);
7384 alsa_card = NULL;
7385 }
7386
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007387 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007388
7389 if (software_mute_active)
7390 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007391}
7392
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007393static int __init volume_create_alsa_mixer(void)
7394{
7395 struct snd_card *card;
7396 struct tpacpi_alsa_data *data;
7397 struct snd_kcontrol *ctl_vol;
7398 struct snd_kcontrol *ctl_mute;
7399 int rc;
7400
Takashi Iwai89235e52014-01-29 15:01:27 +01007401 rc = snd_card_new(&tpacpi_pdev->dev,
7402 alsa_index, alsa_id, THIS_MODULE,
7403 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007404 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007405 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007406 return 1;
7407 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007408
7409 BUG_ON(!card->private_data);
7410 data = card->private_data;
7411 data->card = card;
7412
7413 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7414 sizeof(card->driver));
7415 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7416 sizeof(card->shortname));
7417 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7418 (thinkpad_id.ec_version_str) ?
7419 thinkpad_id.ec_version_str : "(unknown)");
7420 snprintf(card->longname, sizeof(card->longname),
7421 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7422 (thinkpad_id.ec_version_str) ?
7423 thinkpad_id.ec_version_str : "unknown");
7424
7425 if (volume_control_allowed) {
7426 volume_alsa_control_vol.put = volume_alsa_vol_put;
7427 volume_alsa_control_vol.access =
7428 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7429
7430 volume_alsa_control_mute.put = volume_alsa_mute_put;
7431 volume_alsa_control_mute.access =
7432 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7433 }
7434
7435 if (!tp_features.mixer_no_level_control) {
7436 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7437 rc = snd_ctl_add(card, ctl_vol);
7438 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007439 pr_err("Failed to create ALSA volume control: %d\n",
7440 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007441 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007442 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007443 data->ctl_vol_id = &ctl_vol->id;
7444 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007445
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007446 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7447 rc = snd_ctl_add(card, ctl_mute);
7448 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007449 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007450 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007451 }
7452 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007453
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007454 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007455 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007456 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007457 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007458 }
7459
7460 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007461 return 0;
7462
7463err_exit:
7464 snd_card_free(card);
7465 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007466}
7467
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007468#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7469#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7470
7471static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7472 /* Whitelist volume level on all IBM by default */
7473 { .vendor = PCI_VENDOR_ID_IBM,
7474 .bios = TPACPI_MATCH_ANY,
7475 .ec = TPACPI_MATCH_ANY,
7476 .quirks = TPACPI_VOL_Q_LEVEL },
7477
7478 /* Lenovo models with volume control (needs confirmation) */
7479 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7480 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7481 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7482 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7483 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7484 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7485 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7486
7487 /* Whitelist mute-only on all Lenovo by default */
7488 { .vendor = PCI_VENDOR_ID_LENOVO,
7489 .bios = TPACPI_MATCH_ANY,
7490 .ec = TPACPI_MATCH_ANY,
7491 .quirks = TPACPI_VOL_Q_MUTEONLY }
7492};
7493
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007494static int __init volume_init(struct ibm_init_struct *iibm)
7495{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007496 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007497 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007498
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007499 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7500
7501 mutex_init(&volume_mutex);
7502
7503 /*
7504 * Check for module parameter bogosity, note that we
7505 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7506 * able to detect "unspecified"
7507 */
7508 if (volume_mode > TPACPI_VOL_MODE_MAX)
7509 return -EINVAL;
7510
7511 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Joe Perches0978e012011-04-04 10:06:25 -07007512 pr_err("UCMS step volume mode not implemented, "
7513 "please contact %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007514 return 1;
7515 }
7516
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007517 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7518 return -EINVAL;
7519
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007520 /*
7521 * The ALSA mixer is our primary interface.
7522 * When disabled, don't install the subdriver at all
7523 */
7524 if (!alsa_enable) {
7525 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7526 "ALSA mixer disabled by parameter, "
7527 "not loading volume subdriver...\n");
7528 return 1;
7529 }
7530
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007531 quirks = tpacpi_check_quirks(volume_quirk_table,
7532 ARRAY_SIZE(volume_quirk_table));
7533
7534 switch (volume_capabilities) {
7535 case TPACPI_VOL_CAP_AUTO:
7536 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7537 tp_features.mixer_no_level_control = 1;
7538 else if (quirks & TPACPI_VOL_Q_LEVEL)
7539 tp_features.mixer_no_level_control = 0;
7540 else
7541 return 1; /* no mixer */
7542 break;
7543 case TPACPI_VOL_CAP_VOLMUTE:
7544 tp_features.mixer_no_level_control = 0;
7545 break;
7546 case TPACPI_VOL_CAP_MUTEONLY:
7547 tp_features.mixer_no_level_control = 1;
7548 break;
7549 default:
7550 return 1;
7551 }
7552
7553 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7554 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7555 "using user-supplied volume_capabilities=%d\n",
7556 volume_capabilities);
7557
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007558 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7559 volume_mode == TPACPI_VOL_MODE_MAX) {
7560 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7561
7562 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7563 "driver auto-selected volume_mode=%d\n",
7564 volume_mode);
7565 } else {
7566 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7567 "using user-supplied volume_mode=%d\n",
7568 volume_mode);
7569 }
7570
7571 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007572 "mute is supported, volume control is %s\n",
7573 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007574
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007575 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7576 software_mute_active = true;
7577 } else {
7578 rc = volume_create_alsa_mixer();
7579 if (rc) {
7580 pr_err("Could not create the ALSA mixer interface\n");
7581 return rc;
7582 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007583
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007584 pr_info("Console audio control enabled, mode: %s\n",
7585 (volume_control_allowed) ?
7586 "override (read/write)" :
7587 "monitor (read only)");
7588 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007589
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007590 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7591 "registering volume hotkeys as change notification\n");
7592 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7593 | TP_ACPI_HKEY_VOLUP_MASK
7594 | TP_ACPI_HKEY_VOLDWN_MASK
7595 | TP_ACPI_HKEY_MUTE_MASK);
7596
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007597 return 0;
7598}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007599
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007600static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007601{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007602 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007603
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007604 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007605 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007606 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007607 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007608 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007609 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007610 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007611 status & TP_EC_AUDIO_LVL_MSK);
7612
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007613 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007614 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007615
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007616 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007617 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007618 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007619 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007620 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007621 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007622 "commands:\tlevel <level>"
7623 " (<level> is 0-%d)\n",
7624 TP_EC_VOLUME_MAX);
7625 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007626 }
7627 }
7628
7629 return 0;
7630}
7631
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007632static int volume_write(char *buf)
7633{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007634 u8 s;
7635 u8 new_level, new_mute;
7636 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007637 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007638 int rc;
7639
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007640 /*
7641 * We do allow volume control at driver startup, so that the
7642 * user can set initial state through the volume=... parameter hack.
7643 */
7644 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7645 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7646 tp_warned.volume_ctrl_forbidden = 1;
Joe Perches0978e012011-04-04 10:06:25 -07007647 pr_notice("Console audio control in monitor mode, "
7648 "changes are not allowed\n");
7649 pr_notice("Use the volume_control=1 module parameter "
7650 "to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007651 }
7652 return -EPERM;
7653 }
7654
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007655 rc = volume_get_status(&s);
7656 if (rc < 0)
7657 return rc;
7658
7659 new_level = s & TP_EC_AUDIO_LVL_MSK;
7660 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007661
7662 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007663 if (!tp_features.mixer_no_level_control) {
7664 if (strlencmp(cmd, "up") == 0) {
7665 if (new_mute)
7666 new_mute = 0;
7667 else if (new_level < TP_EC_VOLUME_MAX)
7668 new_level++;
7669 continue;
7670 } else if (strlencmp(cmd, "down") == 0) {
7671 if (new_mute)
7672 new_mute = 0;
7673 else if (new_level > 0)
7674 new_level--;
7675 continue;
7676 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7677 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7678 new_level = l;
7679 continue;
7680 }
7681 }
7682 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007683 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007684 else if (strlencmp(cmd, "unmute") == 0)
7685 new_mute = 0;
7686 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007687 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007688 }
7689
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007690 if (tp_features.mixer_no_level_control) {
7691 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7692 new_mute ? "" : "un");
7693 rc = volume_set_mute(!!new_mute);
7694 } else {
7695 tpacpi_disclose_usertask("procfs volume",
7696 "%smute and set level to %d\n",
7697 new_mute ? "" : "un", new_level);
7698 rc = volume_set_status(new_mute | new_level);
7699 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007700 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007701
7702 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007703}
7704
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007705static struct ibm_struct volume_driver_data = {
7706 .name = "volume",
7707 .read = volume_read,
7708 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007709 .exit = volume_exit,
7710 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007711 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007712 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007713};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007714
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007715#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7716
7717#define alsa_card NULL
7718
7719static void inline volume_alsa_notify_change(void)
7720{
7721}
7722
7723static int __init volume_init(struct ibm_init_struct *iibm)
7724{
Joe Perches0978e012011-04-04 10:06:25 -07007725 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007726
7727 return 1;
7728}
7729
7730static struct ibm_struct volume_driver_data = {
7731 .name = "volume",
7732};
7733
7734#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7735
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007736/*************************************************************************
7737 * Fan subdriver
7738 */
7739
7740/*
7741 * FAN ACCESS MODES
7742 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007743 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007744 * ACPI GFAN method: returns fan level
7745 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007746 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007747 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007748 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007749 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007750 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7751 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007752 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7753 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007754 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007755 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007756 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7757 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007758 *
7759 * Fan speed changes of any sort (including those caused by the
7760 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007761 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007762 * limited.
7763 *
7764 * Reading is not available if GFAN exists.
7765 * Writing is not available if SFAN exists.
7766 *
7767 * Bits
7768 * 7 automatic mode engaged;
7769 * (default operation mode of the ThinkPad)
7770 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007771 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007772 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007773 * the tachometer while the fan controller ramps up
7774 * the speed (which can take up to a few *minutes*).
7775 * Speeds up fan to 100% duty-cycle, which is far above
7776 * the standard RPM levels. It is not impossible that
7777 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007778 * 5-3 unused in some models. Extra bits for fan level
7779 * in others, but still useless as all values above
7780 * 7 map to the same speed as level 7 in these models.
7781 * 2-0 fan level (0..7 usually)
7782 * 0x00 = stop
7783 * 0x07 = max (set when temperatures critical)
7784 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007785 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007786 *
7787 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007788 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007789 * does so, its initial value is meaningless (0x07).
7790 *
7791 * For firmware bugs, refer to:
7792 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7793 *
7794 * ----
7795 *
7796 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7797 * Main fan tachometer reading (in RPM)
7798 *
7799 * This register is present on all ThinkPads with a new-style EC, and
7800 * it is known not to be present on the A21m/e, and T22, as there is
7801 * something else in offset 0x84 according to the ACPI DSDT. Other
7802 * ThinkPads from this same time period (and earlier) probably lack the
7803 * tachometer as well.
7804 *
Nick Andrew877d0312009-01-26 11:06:57 +01007805 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007806 * was never fixed by IBM to report the EC firmware version string
7807 * probably support the tachometer (like the early X models), so
7808 * detecting it is quite hard. We need more data to know for sure.
7809 *
7810 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7811 * might result.
7812 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007813 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7814 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007815 *
7816 * For firmware bugs, refer to:
7817 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7818 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007819 * ----
7820 *
7821 * ThinkPad EC register 0x31 bit 0 (only on select models)
7822 *
7823 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7824 * show the speed of the main fan. When bit 0 of EC register 0x31
7825 * is one, the tachometer registers show the speed of the auxiliary
7826 * fan.
7827 *
7828 * Fan control seems to affect both fans, regardless of the state
7829 * of this bit.
7830 *
7831 * So far, only the firmware for the X60/X61 non-tablet versions
7832 * seem to support this (firmware TP-7M).
7833 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007834 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007835 * ThinkPad X31, X40, X41. Not available in the X60.
7836 *
7837 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7838 * high speed. ACPI DSDT seems to map these three speeds to levels
7839 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7840 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7841 *
7842 * The speeds are stored on handles
7843 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7844 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007845 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007846 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7847 *
7848 * ACPI DSDT switches which set is in use depending on various
7849 * factors.
7850 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007851 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007852 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7853 * but the ACPI tables just mention level 7.
7854 */
7855
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007856enum { /* Fan control constants */
7857 fan_status_offset = 0x2f, /* EC register 0x2f */
7858 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7859 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007860 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7861 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007862
7863 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7864 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7865
7866 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7867};
7868
7869enum fan_status_access_mode {
7870 TPACPI_FAN_NONE = 0, /* No fan status or control */
7871 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7872 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7873};
7874
7875enum fan_control_access_mode {
7876 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7877 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7878 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7879 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7880};
7881
7882enum fan_control_commands {
7883 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7884 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7885 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7886 * and also watchdog cmd */
7887};
7888
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307889static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007890
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007891static enum fan_status_access_mode fan_status_access_mode;
7892static enum fan_control_access_mode fan_control_access_mode;
7893static enum fan_control_commands fan_control_commands;
7894
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007895static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007896static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007897static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007898static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007899
7900static struct mutex fan_mutex;
7901
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007902static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007903static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007904
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007905TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7906TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007907 "\\FSPD", /* 600e/x, 770e, 770x */
7908 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007909TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007910 "JFNS", /* 770x-JL */
7911 ); /* all others */
7912
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007913/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007914 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7915 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7916 * be in auto mode (0x80).
7917 *
7918 * This is corrected by any write to HFSP either by the driver, or
7919 * by the firmware.
7920 *
7921 * We assume 0x07 really means auto mode while this quirk is active,
7922 * as this is far more likely than the ThinkPad being in level 7,
7923 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007924 *
7925 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7926 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007927 */
7928
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007929static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007930{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007931 if (fan_control_initial_status == 0x07) {
Joe Perches0978e012011-04-04 10:06:25 -07007932 pr_notice("fan_init: initial fan status is unknown, "
7933 "assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007934 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007935 }
7936}
7937
7938static void fan_quirk1_handle(u8 *fan_status)
7939{
7940 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7941 if (*fan_status != fan_control_initial_status) {
7942 /* something changed the HFSP regisnter since
7943 * driver init time, so it is not undefined
7944 * anymore */
7945 tp_features.fan_ctrl_status_undef = 0;
7946 } else {
7947 /* Return most likely status. In fact, it
7948 * might be the only possible status */
7949 *fan_status = TP_EC_FAN_AUTO;
7950 }
7951 }
7952}
7953
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007954/* Select main fan on X60/X61, NOOP on others */
7955static bool fan_select_fan1(void)
7956{
7957 if (tp_features.second_fan) {
7958 u8 val;
7959
7960 if (ec_read(fan_select_offset, &val) < 0)
7961 return false;
7962 val &= 0xFEU;
7963 if (ec_write(fan_select_offset, val) < 0)
7964 return false;
7965 }
7966 return true;
7967}
7968
7969/* Select secondary fan on X60/X61 */
7970static bool fan_select_fan2(void)
7971{
7972 u8 val;
7973
7974 if (!tp_features.second_fan)
7975 return false;
7976
7977 if (ec_read(fan_select_offset, &val) < 0)
7978 return false;
7979 val |= 0x01U;
7980 if (ec_write(fan_select_offset, val) < 0)
7981 return false;
7982
7983 return true;
7984}
7985
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007986/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007987 * Call with fan_mutex held
7988 */
7989static void fan_update_desired_level(u8 status)
7990{
7991 if ((status &
7992 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7993 if (status > 7)
7994 fan_control_desired_level = 7;
7995 else
7996 fan_control_desired_level = status;
7997 }
7998}
7999
8000static int fan_get_status(u8 *status)
8001{
8002 u8 s;
8003
8004 /* TODO:
8005 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8006
8007 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07008008 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008009 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07008010 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008011
Dan Carpentereceeb432012-09-01 12:54:07 -07008012 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008013 return -EIO;
8014
8015 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07008016 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008017
8018 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07008019 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008020 case TPACPI_FAN_RD_TPEC:
8021 /* all except 570, 600e/x, 770e, 770x */
8022 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8023 return -EIO;
8024
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008025 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008026 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02008027 fan_quirk1_handle(status);
8028 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008029
8030 break;
8031
8032 default:
8033 return -ENXIO;
8034 }
8035
8036 return 0;
8037}
8038
8039static int fan_get_status_safe(u8 *status)
8040{
8041 int rc;
8042 u8 s;
8043
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008044 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008045 return -ERESTARTSYS;
8046 rc = fan_get_status(&s);
8047 if (!rc)
8048 fan_update_desired_level(s);
8049 mutex_unlock(&fan_mutex);
8050
Dan Carpentera7718362016-04-15 17:46:34 +03008051 if (rc)
8052 return rc;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008053 if (status)
8054 *status = s;
8055
Dan Carpentera7718362016-04-15 17:46:34 +03008056 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008057}
8058
8059static int fan_get_speed(unsigned int *speed)
8060{
8061 u8 hi, lo;
8062
8063 switch (fan_status_access_mode) {
8064 case TPACPI_FAN_RD_TPEC:
8065 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008066 if (unlikely(!fan_select_fan1()))
8067 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008068 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8069 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8070 return -EIO;
8071
8072 if (likely(speed))
8073 *speed = (hi << 8) | lo;
8074
8075 break;
8076
8077 default:
8078 return -ENXIO;
8079 }
8080
8081 return 0;
8082}
8083
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008084static int fan2_get_speed(unsigned int *speed)
8085{
8086 u8 hi, lo;
8087 bool rc;
8088
8089 switch (fan_status_access_mode) {
8090 case TPACPI_FAN_RD_TPEC:
8091 /* all except 570, 600e/x, 770e, 770x */
8092 if (unlikely(!fan_select_fan2()))
8093 return -EIO;
8094 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8095 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8096 fan_select_fan1(); /* play it safe */
8097 if (rc)
8098 return -EIO;
8099
8100 if (likely(speed))
8101 *speed = (hi << 8) | lo;
8102
8103 break;
8104
8105 default:
8106 return -ENXIO;
8107 }
8108
8109 return 0;
8110}
8111
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008112static int fan_set_level(int level)
8113{
8114 if (!fan_control_allowed)
8115 return -EPERM;
8116
8117 switch (fan_control_access_mode) {
8118 case TPACPI_FAN_WR_ACPI_SFAN:
8119 if (level >= 0 && level <= 7) {
8120 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8121 return -EIO;
8122 } else
8123 return -EINVAL;
8124 break;
8125
8126 case TPACPI_FAN_WR_ACPI_FANS:
8127 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008128 if (!(level & TP_EC_FAN_AUTO) &&
8129 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008130 ((level < 0) || (level > 7)))
8131 return -EINVAL;
8132
8133 /* safety net should the EC not support AUTO
8134 * or FULLSPEED mode bits and just ignore them */
8135 if (level & TP_EC_FAN_FULLSPEED)
8136 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01008137 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008138 level |= 4; /* safety min speed 4 */
8139
8140 if (!acpi_ec_write(fan_status_offset, level))
8141 return -EIO;
8142 else
8143 tp_features.fan_ctrl_status_undef = 0;
8144 break;
8145
8146 default:
8147 return -ENXIO;
8148 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008149
8150 vdbg_printk(TPACPI_DBG_FAN,
8151 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008152 return 0;
8153}
8154
8155static int fan_set_level_safe(int level)
8156{
8157 int rc;
8158
8159 if (!fan_control_allowed)
8160 return -EPERM;
8161
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008162 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008163 return -ERESTARTSYS;
8164
8165 if (level == TPACPI_FAN_LAST_LEVEL)
8166 level = fan_control_desired_level;
8167
8168 rc = fan_set_level(level);
8169 if (!rc)
8170 fan_update_desired_level(level);
8171
8172 mutex_unlock(&fan_mutex);
8173 return rc;
8174}
8175
8176static int fan_set_enable(void)
8177{
8178 u8 s;
8179 int rc;
8180
8181 if (!fan_control_allowed)
8182 return -EPERM;
8183
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008184 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008185 return -ERESTARTSYS;
8186
8187 switch (fan_control_access_mode) {
8188 case TPACPI_FAN_WR_ACPI_FANS:
8189 case TPACPI_FAN_WR_TPEC:
8190 rc = fan_get_status(&s);
8191 if (rc < 0)
8192 break;
8193
8194 /* Don't go out of emergency fan mode */
8195 if (s != 7) {
8196 s &= 0x07;
8197 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8198 }
8199
8200 if (!acpi_ec_write(fan_status_offset, s))
8201 rc = -EIO;
8202 else {
8203 tp_features.fan_ctrl_status_undef = 0;
8204 rc = 0;
8205 }
8206 break;
8207
8208 case TPACPI_FAN_WR_ACPI_SFAN:
8209 rc = fan_get_status(&s);
8210 if (rc < 0)
8211 break;
8212
8213 s &= 0x07;
8214
8215 /* Set fan to at least level 4 */
8216 s |= 4;
8217
8218 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008219 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008220 else
8221 rc = 0;
8222 break;
8223
8224 default:
8225 rc = -ENXIO;
8226 }
8227
8228 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008229
8230 if (!rc)
8231 vdbg_printk(TPACPI_DBG_FAN,
8232 "fan control: set fan control register to 0x%02x\n",
8233 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008234 return rc;
8235}
8236
8237static int fan_set_disable(void)
8238{
8239 int rc;
8240
8241 if (!fan_control_allowed)
8242 return -EPERM;
8243
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008244 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008245 return -ERESTARTSYS;
8246
8247 rc = 0;
8248 switch (fan_control_access_mode) {
8249 case TPACPI_FAN_WR_ACPI_FANS:
8250 case TPACPI_FAN_WR_TPEC:
8251 if (!acpi_ec_write(fan_status_offset, 0x00))
8252 rc = -EIO;
8253 else {
8254 fan_control_desired_level = 0;
8255 tp_features.fan_ctrl_status_undef = 0;
8256 }
8257 break;
8258
8259 case TPACPI_FAN_WR_ACPI_SFAN:
8260 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8261 rc = -EIO;
8262 else
8263 fan_control_desired_level = 0;
8264 break;
8265
8266 default:
8267 rc = -ENXIO;
8268 }
8269
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008270 if (!rc)
8271 vdbg_printk(TPACPI_DBG_FAN,
8272 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008273
8274 mutex_unlock(&fan_mutex);
8275 return rc;
8276}
8277
8278static int fan_set_speed(int speed)
8279{
8280 int rc;
8281
8282 if (!fan_control_allowed)
8283 return -EPERM;
8284
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008285 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008286 return -ERESTARTSYS;
8287
8288 rc = 0;
8289 switch (fan_control_access_mode) {
8290 case TPACPI_FAN_WR_ACPI_FANS:
8291 if (speed >= 0 && speed <= 65535) {
8292 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8293 speed, speed, speed))
8294 rc = -EIO;
8295 } else
8296 rc = -EINVAL;
8297 break;
8298
8299 default:
8300 rc = -ENXIO;
8301 }
8302
8303 mutex_unlock(&fan_mutex);
8304 return rc;
8305}
8306
8307static void fan_watchdog_reset(void)
8308{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008309 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8310 return;
8311
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008312 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008313 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8314 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8315 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8316 else
8317 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008318}
8319
8320static void fan_watchdog_fire(struct work_struct *ignored)
8321{
8322 int rc;
8323
8324 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8325 return;
8326
Joe Perches0978e012011-04-04 10:06:25 -07008327 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008328 rc = fan_set_enable();
8329 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07008330 pr_err("fan watchdog: error %d while enabling fan, "
8331 "will try again later...\n", -rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008332 /* reschedule for later */
8333 fan_watchdog_reset();
8334 }
8335}
8336
8337/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008338 * SYSFS fan layout: hwmon compatible (device)
8339 *
8340 * pwm*_enable:
8341 * 0: "disengaged" mode
8342 * 1: manual mode
8343 * 2: native EC "auto" mode (recommended, hardware default)
8344 *
8345 * pwm*: set speed in manual mode, ignored otherwise.
8346 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8347 * interpolation.
8348 *
8349 * fan*_input: tachometer reading, RPM
8350 *
8351 *
8352 * SYSFS fan layout: extensions
8353 *
8354 * fan_watchdog (driver):
8355 * fan watchdog interval in seconds, 0 disables (default), max 120
8356 */
8357
8358/* sysfs fan pwm1_enable ----------------------------------------------- */
8359static ssize_t fan_pwm1_enable_show(struct device *dev,
8360 struct device_attribute *attr,
8361 char *buf)
8362{
8363 int res, mode;
8364 u8 status;
8365
8366 res = fan_get_status_safe(&status);
8367 if (res)
8368 return res;
8369
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008370 if (status & TP_EC_FAN_FULLSPEED) {
8371 mode = 0;
8372 } else if (status & TP_EC_FAN_AUTO) {
8373 mode = 2;
8374 } else
8375 mode = 1;
8376
8377 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8378}
8379
8380static ssize_t fan_pwm1_enable_store(struct device *dev,
8381 struct device_attribute *attr,
8382 const char *buf, size_t count)
8383{
8384 unsigned long t;
8385 int res, level;
8386
8387 if (parse_strtoul(buf, 2, &t))
8388 return -EINVAL;
8389
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008390 tpacpi_disclose_usertask("hwmon pwm1_enable",
8391 "set fan mode to %lu\n", t);
8392
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008393 switch (t) {
8394 case 0:
8395 level = TP_EC_FAN_FULLSPEED;
8396 break;
8397 case 1:
8398 level = TPACPI_FAN_LAST_LEVEL;
8399 break;
8400 case 2:
8401 level = TP_EC_FAN_AUTO;
8402 break;
8403 case 3:
8404 /* reserved for software-controlled auto mode */
8405 return -ENOSYS;
8406 default:
8407 return -EINVAL;
8408 }
8409
8410 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008411 if (res == -ENXIO)
8412 return -EINVAL;
8413 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008414 return res;
8415
8416 fan_watchdog_reset();
8417
8418 return count;
8419}
8420
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008421static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8422 fan_pwm1_enable_show, fan_pwm1_enable_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008423
8424/* sysfs fan pwm1 ------------------------------------------------------ */
8425static ssize_t fan_pwm1_show(struct device *dev,
8426 struct device_attribute *attr,
8427 char *buf)
8428{
8429 int res;
8430 u8 status;
8431
8432 res = fan_get_status_safe(&status);
8433 if (res)
8434 return res;
8435
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008436 if ((status &
8437 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8438 status = fan_control_desired_level;
8439
8440 if (status > 7)
8441 status = 7;
8442
8443 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8444}
8445
8446static ssize_t fan_pwm1_store(struct device *dev,
8447 struct device_attribute *attr,
8448 const char *buf, size_t count)
8449{
8450 unsigned long s;
8451 int rc;
8452 u8 status, newlevel;
8453
8454 if (parse_strtoul(buf, 255, &s))
8455 return -EINVAL;
8456
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008457 tpacpi_disclose_usertask("hwmon pwm1",
8458 "set fan speed to %lu\n", s);
8459
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008460 /* scale down from 0-255 to 0-7 */
8461 newlevel = (s >> 5) & 0x07;
8462
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008463 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008464 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008465
8466 rc = fan_get_status(&status);
8467 if (!rc && (status &
8468 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8469 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008470 if (rc == -ENXIO)
8471 rc = -EINVAL;
8472 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008473 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008474 fan_watchdog_reset();
8475 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008476 }
8477
8478 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008479 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008480}
8481
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008482static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008483
8484/* sysfs fan fan1_input ------------------------------------------------ */
8485static ssize_t fan_fan1_input_show(struct device *dev,
8486 struct device_attribute *attr,
8487 char *buf)
8488{
8489 int res;
8490 unsigned int speed;
8491
8492 res = fan_get_speed(&speed);
8493 if (res < 0)
8494 return res;
8495
8496 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8497}
8498
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008499static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008500
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008501/* sysfs fan fan2_input ------------------------------------------------ */
8502static ssize_t fan_fan2_input_show(struct device *dev,
8503 struct device_attribute *attr,
8504 char *buf)
8505{
8506 int res;
8507 unsigned int speed;
8508
8509 res = fan2_get_speed(&speed);
8510 if (res < 0)
8511 return res;
8512
8513 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8514}
8515
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008516static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008517
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008518/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008519static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
8520 char *buf)
8521{
8522 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8523}
8524
8525static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8526 const char *buf, size_t count)
8527{
8528 unsigned long t;
8529
8530 if (parse_strtoul(buf, 120, &t))
8531 return -EINVAL;
8532
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008533 if (!fan_control_allowed)
8534 return -EPERM;
8535
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008536 fan_watchdog_maxinterval = t;
8537 fan_watchdog_reset();
8538
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008539 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8540
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008541 return count;
8542}
8543
8544static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8545 fan_fan_watchdog_show, fan_fan_watchdog_store);
8546
8547/* --------------------------------------------------------------------- */
8548static struct attribute *fan_attributes[] = {
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008549 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8550 &dev_attr_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008551 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008552 NULL
8553};
8554
8555static const struct attribute_group fan_attr_group = {
8556 .attrs = fan_attributes,
8557};
8558
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008559#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8560#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008561
8562#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8563 { .vendor = PCI_VENDOR_ID_IBM, \
8564 .bios = TPACPI_MATCH_ANY, \
8565 .ec = TPID(__id1, __id2), \
8566 .quirks = __quirks }
8567
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008568#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8569 { .vendor = PCI_VENDOR_ID_LENOVO, \
8570 .bios = TPACPI_MATCH_ANY, \
8571 .ec = TPID(__id1, __id2), \
8572 .quirks = __quirks }
8573
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008574static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8575 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8576 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8577 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8578 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008579 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008580};
8581
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008582#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008583#undef TPACPI_FAN_QI
8584
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008585static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008586{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008587 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008588 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008589
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008590 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8591 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008592
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008593 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008594 fan_status_access_mode = TPACPI_FAN_NONE;
8595 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008596 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008597 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008598 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008599 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008600 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008601
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008602 if (tpacpi_is_ibm()) {
8603 TPACPI_ACPIHANDLE_INIT(fans);
8604 TPACPI_ACPIHANDLE_INIT(gfan);
8605 TPACPI_ACPIHANDLE_INIT(sfan);
8606 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008607
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008608 quirks = tpacpi_check_quirks(fan_quirk_table,
8609 ARRAY_SIZE(fan_quirk_table));
8610
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008611 if (gfan_handle) {
8612 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008613 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008614 } else {
8615 /* all other ThinkPads: note that even old-style
8616 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008617 if (likely(acpi_ec_read(fan_status_offset,
8618 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008619 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008620 if (quirks & TPACPI_FAN_Q1)
8621 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008622 if (quirks & TPACPI_FAN_2FAN) {
8623 tp_features.second_fan = 1;
8624 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8625 "secondary fan support enabled\n");
8626 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008627 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008628 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008629 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008630 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008631 }
8632 }
8633
8634 if (sfan_handle) {
8635 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008636 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008637 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008638 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008639 } else {
8640 if (!gfan_handle) {
8641 /* gfan without sfan means no fan control */
8642 /* all other models implement TP EC 0x2f control */
8643
8644 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008645 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008646 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008647 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008648 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008649 TPACPI_FAN_CMD_SPEED |
8650 TPACPI_FAN_CMD_LEVEL |
8651 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008652 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008653 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008654 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008655 TPACPI_FAN_CMD_LEVEL |
8656 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008657 }
8658 }
8659 }
8660
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008661 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8662 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008663 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8664 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8665 fan_status_access_mode, fan_control_access_mode);
8666
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008667 /* fan control master switch */
8668 if (!fan_control_allowed) {
8669 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8670 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008671 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008672 "fan control features disabled by parameter\n");
8673 }
8674
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008675 /* update fan_control_desired_level */
8676 if (fan_status_access_mode != TPACPI_FAN_NONE)
8677 fan_get_status_safe(NULL);
8678
8679 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8680 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008681 if (tp_features.second_fan) {
8682 /* attach second fan tachometer */
8683 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02008684 &dev_attr_fan2_input.attr;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008685 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008686 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008687 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008688 if (rc < 0)
8689 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008690
8691 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8692 &driver_attr_fan_watchdog);
8693 if (rc < 0) {
8694 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8695 &fan_attr_group);
8696 return rc;
8697 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008698 return 0;
8699 } else
8700 return 1;
8701}
8702
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008703static void fan_exit(void)
8704{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008705 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008706 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008707
8708 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008709 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008710 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8711 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008712
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008713 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008714 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008715}
8716
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008717static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008718{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008719 int rc;
8720
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008721 if (!fan_control_allowed)
8722 return;
8723
8724 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008725 fan_control_resume_level = 0;
8726 rc = fan_get_status_safe(&fan_control_resume_level);
8727 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008728 pr_notice("failed to read fan level for later "
8729 "restore during resume: %d\n", rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008730
8731 /* if it is undefined, don't attempt to restore it.
8732 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008733 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008734 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008735}
8736
8737static void fan_resume(void)
8738{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008739 u8 current_level = 7;
8740 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008741 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008742
8743 /* DSDT *always* updates status on resume */
8744 tp_features.fan_ctrl_status_undef = 0;
8745
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008746 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008747 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008748 (fan_get_status_safe(&current_level) < 0))
8749 return;
8750
8751 switch (fan_control_access_mode) {
8752 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008753 /* never decrease fan level */
8754 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008755 break;
8756 case TPACPI_FAN_WR_ACPI_FANS:
8757 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008758 /* never decrease fan level, scale is:
8759 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8760 *
8761 * We expect the firmware to set either 7 or AUTO, but we
8762 * handle FULLSPEED out of paranoia.
8763 *
8764 * So, we can safely only restore FULLSPEED or 7, anything
8765 * else could slow the fan. Restoring AUTO is useless, at
8766 * best that's exactly what the DSDT already set (it is the
8767 * slower it uses).
8768 *
8769 * Always keep in mind that the DSDT *will* have set the
8770 * fans to what the vendor supposes is the best level. We
8771 * muck with it only to speed the fan up.
8772 */
8773 if (fan_control_resume_level != 7 &&
8774 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8775 return;
8776 else
8777 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8778 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008779 break;
8780 default:
8781 return;
8782 }
8783 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008784 pr_notice("restoring fan level to 0x%02x\n",
8785 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008786 rc = fan_set_level_safe(fan_control_resume_level);
8787 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008788 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008789 }
8790}
8791
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008792static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008793{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008794 int rc;
8795 u8 status;
8796 unsigned int speed = 0;
8797
8798 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008799 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008800 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008801 rc = fan_get_status_safe(&status);
8802 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008803 return rc;
8804
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008805 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008806 "level:\t\t%d\n",
8807 (status != 0) ? "enabled" : "disabled", status);
8808 break;
8809
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008810 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008811 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008812 rc = fan_get_status_safe(&status);
8813 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008814 return rc;
8815
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008816 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008817 (status != 0) ? "enabled" : "disabled");
8818
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008819 rc = fan_get_speed(&speed);
8820 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008821 return rc;
8822
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008823 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008824
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008825 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008826 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008827 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008828 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008829 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008830 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008831 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008832 break;
8833
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008834 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008835 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008836 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008837 }
8838
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008839 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008840 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008841
8842 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008843 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008844 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008845 break;
8846
8847 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008848 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008849 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008850 break;
8851 }
8852 }
8853
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008854 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008855 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008856 "commands:\twatchdog <timeout> (<timeout> "
8857 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008858
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008859 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008860 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008861 " (<speed> is 0-65535)\n");
8862
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008863 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008864}
8865
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008866static int fan_write_cmd_level(const char *cmd, int *rc)
8867{
8868 int level;
8869
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008870 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008871 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008872 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008873 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008874 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008875 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008876 return 0;
8877
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008878 *rc = fan_set_level_safe(level);
8879 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008880 pr_err("level command accepted for unsupported access mode %d\n",
8881 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008882 else if (!*rc)
8883 tpacpi_disclose_usertask("procfs fan",
8884 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008885
8886 return 1;
8887}
8888
8889static int fan_write_cmd_enable(const char *cmd, int *rc)
8890{
8891 if (strlencmp(cmd, "enable") != 0)
8892 return 0;
8893
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008894 *rc = fan_set_enable();
8895 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008896 pr_err("enable command accepted for unsupported access mode %d\n",
8897 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008898 else if (!*rc)
8899 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008900
8901 return 1;
8902}
8903
8904static int fan_write_cmd_disable(const char *cmd, int *rc)
8905{
8906 if (strlencmp(cmd, "disable") != 0)
8907 return 0;
8908
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008909 *rc = fan_set_disable();
8910 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008911 pr_err("disable command accepted for unsupported access mode %d\n",
8912 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008913 else if (!*rc)
8914 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008915
8916 return 1;
8917}
8918
8919static int fan_write_cmd_speed(const char *cmd, int *rc)
8920{
8921 int speed;
8922
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008923 /* TODO:
8924 * Support speed <low> <medium> <high> ? */
8925
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008926 if (sscanf(cmd, "speed %d", &speed) != 1)
8927 return 0;
8928
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008929 *rc = fan_set_speed(speed);
8930 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008931 pr_err("speed command accepted for unsupported access mode %d\n",
8932 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008933 else if (!*rc)
8934 tpacpi_disclose_usertask("procfs fan",
8935 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008936
8937 return 1;
8938}
8939
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008940static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8941{
8942 int interval;
8943
8944 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8945 return 0;
8946
8947 if (interval < 0 || interval > 120)
8948 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008949 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008950 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008951 tpacpi_disclose_usertask("procfs fan",
8952 "set watchdog timer to %d\n",
8953 interval);
8954 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008955
8956 return 1;
8957}
8958
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008959static int fan_write(char *buf)
8960{
8961 char *cmd;
8962 int rc = 0;
8963
8964 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008965 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008966 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008967 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008968 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008969 fan_write_cmd_disable(cmd, &rc) ||
8970 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008971 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008972 fan_write_cmd_speed(cmd, &rc))
8973 )
8974 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008975 else if (!rc)
8976 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008977 }
8978
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008979 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008980}
8981
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008982static struct ibm_struct fan_driver_data = {
8983 .name = "fan",
8984 .read = fan_read,
8985 .write = fan_write,
8986 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008987 .suspend = fan_suspend,
8988 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008989};
8990
David Henningsson420f9732013-10-16 23:10:31 +02008991/*************************************************************************
8992 * Mute LED subdriver
8993 */
8994
8995
8996struct tp_led_table {
8997 acpi_string name;
8998 int on_value;
8999 int off_value;
9000 int state;
9001};
9002
9003static struct tp_led_table led_tables[] = {
9004 [TPACPI_LED_MUTE] = {
9005 .name = "SSMS",
9006 .on_value = 1,
9007 .off_value = 0,
9008 },
9009 [TPACPI_LED_MICMUTE] = {
9010 .name = "MMTS",
9011 .on_value = 2,
9012 .off_value = 0,
9013 },
9014};
9015
9016static int mute_led_on_off(struct tp_led_table *t, bool state)
9017{
9018 acpi_handle temp;
9019 int output;
9020
9021 if (!ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp))) {
9022 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9023 return -EIO;
9024 }
9025
9026 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9027 state ? t->on_value : t->off_value))
9028 return -EIO;
9029
9030 t->state = state;
9031 return state;
9032}
9033
9034int tpacpi_led_set(int whichled, bool on)
9035{
9036 struct tp_led_table *t;
9037
9038 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9039 return -EINVAL;
9040
9041 t = &led_tables[whichled];
9042 if (t->state < 0 || t->state == on)
9043 return t->state;
9044 return mute_led_on_off(t, on);
9045}
9046EXPORT_SYMBOL_GPL(tpacpi_led_set);
9047
9048static int mute_led_init(struct ibm_init_struct *iibm)
9049{
9050 acpi_handle temp;
9051 int i;
9052
9053 for (i = 0; i < TPACPI_LED_MAX; i++) {
9054 struct tp_led_table *t = &led_tables[i];
9055 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9056 mute_led_on_off(t, false);
9057 else
9058 t->state = -ENODEV;
9059 }
9060 return 0;
9061}
9062
9063static void mute_led_exit(void)
9064{
9065 int i;
9066
9067 for (i = 0; i < TPACPI_LED_MAX; i++)
9068 tpacpi_led_set(i, false);
9069}
9070
Takashi Iwai119f4492014-02-12 16:32:45 +01009071static void mute_led_resume(void)
9072{
9073 int i;
9074
9075 for (i = 0; i < TPACPI_LED_MAX; i++) {
9076 struct tp_led_table *t = &led_tables[i];
9077 if (t->state >= 0)
9078 mute_led_on_off(t, t->state);
9079 }
9080}
9081
David Henningsson420f9732013-10-16 23:10:31 +02009082static struct ibm_struct mute_led_driver_data = {
9083 .name = "mute_led",
9084 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01009085 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02009086};
9087
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009088/****************************************************************************
9089 ****************************************************************************
9090 *
9091 * Infrastructure
9092 *
9093 ****************************************************************************
9094 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07009095
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009096/*
9097 * HKEY event callout for other subdrivers go here
9098 * (yes, it is ugly, but it is quick, safe, and gets the job done
9099 */
9100static void tpacpi_driver_event(const unsigned int hkey_event)
9101{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00009102 if (ibm_backlight_device) {
9103 switch (hkey_event) {
9104 case TP_HKEY_EV_BRGHT_UP:
9105 case TP_HKEY_EV_BRGHT_DOWN:
9106 tpacpi_brightness_notify_change();
9107 }
9108 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009109 if (alsa_card) {
9110 switch (hkey_event) {
9111 case TP_HKEY_EV_VOL_UP:
9112 case TP_HKEY_EV_VOL_DOWN:
9113 case TP_HKEY_EV_VOL_MUTE:
9114 volume_alsa_notify_change();
9115 }
9116 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009117}
9118
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009119static void hotkey_driver_event(const unsigned int scancode)
9120{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03009121 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03009122}
9123
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009124/* sysfs name ---------------------------------------------------------- */
9125static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
9126 struct device_attribute *attr,
9127 char *buf)
9128{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009129 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009130}
9131
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009132static DEVICE_ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009133
9134/* --------------------------------------------------------------------- */
9135
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009136/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03009137static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009138
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009139/*
9140 * Module and infrastructure proble, init and exit handling
9141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009142
Rusty Russell90ab5ee2012-01-13 09:32:20 +10309143static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009144
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009145#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009146static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009147{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009148 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009149
Jan van den Berg72a979f2014-09-17 00:01:08 +02009150 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009151}
9152#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9153
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009154static void ibm_exit(struct ibm_struct *ibm)
9155{
9156 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9157
9158 list_del_init(&ibm->all_drivers);
9159
9160 if (ibm->flags.acpi_notify_installed) {
9161 dbg_printk(TPACPI_DBG_EXIT,
9162 "%s: acpi_remove_notify_handler\n", ibm->name);
9163 BUG_ON(!ibm->acpi);
9164 acpi_remove_notify_handler(*ibm->acpi->handle,
9165 ibm->acpi->type,
9166 dispatch_acpi_notify);
9167 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009168 }
9169
9170 if (ibm->flags.proc_created) {
9171 dbg_printk(TPACPI_DBG_EXIT,
9172 "%s: remove_proc_entry\n", ibm->name);
9173 remove_proc_entry(ibm->name, proc_dir);
9174 ibm->flags.proc_created = 0;
9175 }
9176
9177 if (ibm->flags.acpi_driver_registered) {
9178 dbg_printk(TPACPI_DBG_EXIT,
9179 "%s: acpi_bus_unregister_driver\n", ibm->name);
9180 BUG_ON(!ibm->acpi);
9181 acpi_bus_unregister_driver(ibm->acpi->driver);
9182 kfree(ibm->acpi->driver);
9183 ibm->acpi->driver = NULL;
9184 ibm->flags.acpi_driver_registered = 0;
9185 }
9186
9187 if (ibm->flags.init_called && ibm->exit) {
9188 ibm->exit();
9189 ibm->flags.init_called = 0;
9190 }
9191
9192 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9193}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009194
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009195static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009196{
9197 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009198 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009199 struct proc_dir_entry *entry;
9200
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009201 BUG_ON(ibm == NULL);
9202
9203 INIT_LIST_HEAD(&ibm->all_drivers);
9204
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009205 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009206 return 0;
9207
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009208 dbg_printk(TPACPI_DBG_INIT,
9209 "probing for %s\n", ibm->name);
9210
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009211 if (iibm->init) {
9212 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009213 if (ret > 0)
9214 return 0; /* probe failed */
9215 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009216 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009217
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009218 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009219 }
9220
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009221 if (ibm->acpi) {
9222 if (ibm->acpi->hid) {
9223 ret = register_tpacpi_subdriver(ibm);
9224 if (ret)
9225 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009226 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009227
9228 if (ibm->acpi->notify) {
9229 ret = setup_acpi_notify(ibm);
9230 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07009231 pr_notice("disabling subdriver %s\n",
9232 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03009233 ret = 0;
9234 goto err_out;
9235 }
9236 if (ret < 0)
9237 goto err_out;
9238 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009239 }
9240
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009241 dbg_printk(TPACPI_DBG_INIT,
9242 "%s installed\n", ibm->name);
9243
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009244 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04009245 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009246
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009247 if (!mode)
9248 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02009249 if (ibm->write)
9250 mode |= S_IWUSR;
9251 entry = proc_create_data(ibm->name, mode, proc_dir,
9252 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009253 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07009254 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009255 ret = -ENODEV;
9256 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009257 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03009258 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009260
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009261 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9262
Linus Torvalds1da177e2005-04-16 15:20:36 -07009263 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009264
9265err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03009266 dbg_printk(TPACPI_DBG_INIT,
9267 "%s: at error exit path with result %d\n",
9268 ibm->name, ret);
9269
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009270 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +02009271 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009272}
9273
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009274/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009275
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009276static bool __pure __init tpacpi_is_fw_digit(const char c)
9277{
9278 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9279}
9280
Jan van den Berg72a979f2014-09-17 00:01:08 +02009281static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009282 const char t)
9283{
Adam Lee1b0eb5bc2015-02-11 13:43:10 +08009284 /*
9285 * Most models: xxyTkkWW (#.##c)
9286 * Ancient 570/600 and -SL lacks (#.##c)
9287 */
9288 if (s && strlen(s) >= 8 &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009289 tpacpi_is_fw_digit(s[0]) &&
9290 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009291 s[2] == t &&
9292 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009293 tpacpi_is_fw_digit(s[4]) &&
Adam Lee1b0eb5bc2015-02-11 13:43:10 +08009294 tpacpi_is_fw_digit(s[5]))
9295 return true;
9296
9297 /* New models: xxxyTkkW (#.##c); T550 and some others */
9298 return s && strlen(s) >= 8 &&
9299 tpacpi_is_fw_digit(s[0]) &&
9300 tpacpi_is_fw_digit(s[1]) &&
9301 tpacpi_is_fw_digit(s[2]) &&
9302 s[3] == t &&
9303 (s[4] == 'T' || s[4] == 'N') &&
9304 tpacpi_is_fw_digit(s[5]) &&
9305 tpacpi_is_fw_digit(s[6]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009306}
9307
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009308/* returns 0 - probe ok, or < 0 - probe error.
9309 * Probe ok doesn't mean thinkpad found.
9310 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9311static int __must_check __init get_thinkpad_model_data(
9312 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009313{
Jeff Garzik18552562007-10-03 15:15:40 -04009314 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009315 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009316 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009317
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009318 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009319 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009320
9321 memset(tp, 0, sizeof(*tp));
9322
9323 if (dmi_name_in_vendors("IBM"))
9324 tp->vendor = PCI_VENDOR_ID_IBM;
9325 else if (dmi_name_in_vendors("LENOVO"))
9326 tp->vendor = PCI_VENDOR_ID_LENOVO;
9327 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009328 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009329
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009330 s = dmi_get_system_info(DMI_BIOS_VERSION);
9331 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9332 if (s && !tp->bios_version_str)
9333 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009334
9335 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009336 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9337 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009338 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009339
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009340 tp->bios_model = tp->bios_version_str[0]
9341 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009342 tp->bios_release = (tp->bios_version_str[4] << 8)
9343 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009344
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009345 /*
9346 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9347 * X32 or newer, all Z series; Some models must have an
9348 * up-to-date BIOS or they will not be detected.
9349 *
9350 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9351 */
9352 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009353 if (sscanf(dev->name,
9354 "IBM ThinkPad Embedded Controller -[%17c",
9355 ec_fw_string) == 1) {
9356 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9357 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009358
9359 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009360 if (!tp->ec_version_str)
9361 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009362
9363 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9364 tp->ec_model = ec_fw_string[0]
9365 | (ec_fw_string[1] << 8);
9366 tp->ec_release = (ec_fw_string[4] << 8)
9367 | ec_fw_string[5];
9368 } else {
Joe Perches0978e012011-04-04 10:06:25 -07009369 pr_notice("ThinkPad firmware release %s "
9370 "doesn't match the known patterns\n",
9371 ec_fw_string);
9372 pr_notice("please report this to %s\n",
9373 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009374 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009375 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009376 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009377 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009378
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009379 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009380 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009381 tp->model_str = kstrdup(s, GFP_KERNEL);
9382 if (!tp->model_str)
9383 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009384 } else {
9385 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009386 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009387 tp->model_str = kstrdup(s, GFP_KERNEL);
9388 if (!tp->model_str)
9389 return -ENOMEM;
9390 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009391 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03009392
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009393 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9394 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9395 if (s && !tp->nummodel_str)
9396 return -ENOMEM;
9397
9398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399}
9400
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009401static int __init probe_for_thinkpad(void)
9402{
9403 int is_thinkpad;
9404
9405 if (acpi_disabled)
9406 return -ENODEV;
9407
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009408 /* It would be dangerous to run the driver in this case */
9409 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9410 return -ENODEV;
9411
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009412 /*
9413 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009414 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009415 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009416 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9417 (thinkpad_id.ec_model != 0) ||
9418 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009419
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03009420 /* The EC handler is required */
9421 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009422 if (!ec_handle) {
9423 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07009424 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009425 return -ENODEV;
9426 }
9427
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009428 if (!is_thinkpad && !force_load)
9429 return -ENODEV;
9430
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009431 return 0;
9432}
9433
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009434static void __init thinkpad_acpi_init_banner(void)
9435{
Joe Perches0978e012011-04-04 10:06:25 -07009436 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9437 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009438
Joe Perches0978e012011-04-04 10:06:25 -07009439 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009440 (thinkpad_id.bios_version_str) ?
9441 thinkpad_id.bios_version_str : "unknown",
9442 (thinkpad_id.ec_version_str) ?
9443 thinkpad_id.ec_version_str : "unknown");
9444
9445 BUG_ON(!thinkpad_id.vendor);
9446
9447 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07009448 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009449 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9450 "IBM" : ((thinkpad_id.vendor ==
9451 PCI_VENDOR_ID_LENOVO) ?
9452 "Lenovo" : "Unknown vendor"),
9453 thinkpad_id.model_str,
9454 (thinkpad_id.nummodel_str) ?
9455 thinkpad_id.nummodel_str : "unknown");
9456}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009457
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009458/* Module init, exit, parameters */
9459
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009460static struct ibm_init_struct ibms_init[] __initdata = {
9461 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009462 .data = &thinkpad_acpi_driver_data,
9463 },
9464 {
9465 .init = hotkey_init,
9466 .data = &hotkey_driver_data,
9467 },
9468 {
9469 .init = bluetooth_init,
9470 .data = &bluetooth_driver_data,
9471 },
9472 {
9473 .init = wan_init,
9474 .data = &wan_driver_data,
9475 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009476 {
9477 .init = uwb_init,
9478 .data = &uwb_driver_data,
9479 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009480#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009481 {
9482 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009483 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009484 .data = &video_driver_data,
9485 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009486#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009487 {
Pali Rohárbb28f3d2015-12-30 23:27:41 +01009488 .init = kbdlight_init,
9489 .data = &kbdlight_driver_data,
9490 },
9491 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009492 .init = light_init,
9493 .data = &light_driver_data,
9494 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009495 {
9496 .init = cmos_init,
9497 .data = &cmos_driver_data,
9498 },
9499 {
9500 .init = led_init,
9501 .data = &led_driver_data,
9502 },
9503 {
9504 .init = beep_init,
9505 .data = &beep_driver_data,
9506 },
9507 {
9508 .init = thermal_init,
9509 .data = &thermal_driver_data,
9510 },
9511 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009512 .init = brightness_init,
9513 .data = &brightness_driver_data,
9514 },
9515 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009516 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009517 .data = &volume_driver_data,
9518 },
9519 {
9520 .init = fan_init,
9521 .data = &fan_driver_data,
9522 },
David Henningsson420f9732013-10-16 23:10:31 +02009523 {
9524 .init = mute_led_init,
9525 .data = &mute_led_driver_data,
9526 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009527};
9528
Kees Cook24da2c82017-10-17 19:04:42 -07009529static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009530{
9531 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009532 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009533
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009534 if (!kp || !kp->name || !val)
9535 return -EINVAL;
9536
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009537 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9538 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009539 WARN_ON(ibm == NULL);
9540
9541 if (!ibm || !ibm->name)
9542 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009543
9544 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9545 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009546 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009547 strcpy(ibms_init[i].param, val);
9548 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009549 return 0;
9550 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009551 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009552
9553 return -EINVAL;
9554}
9555
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009556module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009557MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009558 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009559
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009560module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009561MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009562
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009563module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009564MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009565 "Attempts to load the driver even on a "
9566 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009567
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009568module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009569MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009570 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03009571
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009572module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009573MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009574 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00009575 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03009576
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009577module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009578MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009579 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02009580
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009581#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009582module_param_named(volume_mode, volume_mode, uint, 0444);
9583MODULE_PARM_DESC(volume_mode,
9584 "Selects volume control strategy: "
9585 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9586
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02009587module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9588MODULE_PARM_DESC(volume_capabilities,
9589 "Selects the mixer capabilites: "
9590 "0=auto, 1=volume and mute, 2=mute only");
9591
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02009592module_param_named(volume_control, volume_control_allowed, bool, 0444);
9593MODULE_PARM_DESC(volume_control,
9594 "Enables software override for the console audio "
9595 "control when true");
9596
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07009597module_param_named(software_mute, software_mute_requested, bool, 0444);
9598MODULE_PARM_DESC(software_mute,
9599 "Request full software mute control");
9600
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009601/* ALSA module API parameters */
9602module_param_named(index, alsa_index, int, 0444);
9603MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9604module_param_named(id, alsa_id, charp, 0444);
9605MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9606module_param_named(enable, alsa_enable, bool, 0444);
9607MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009608#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009609
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009610#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009611 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02009612 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009613 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009614
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009615TPACPI_PARAM(hotkey);
9616TPACPI_PARAM(bluetooth);
9617TPACPI_PARAM(video);
9618TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009619TPACPI_PARAM(cmos);
9620TPACPI_PARAM(led);
9621TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009622TPACPI_PARAM(brightness);
9623TPACPI_PARAM(volume);
9624TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009625
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009626#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009627module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009628MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9629module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9630MODULE_PARM_DESC(wlsw_state,
9631 "Initial state of the emulated WLSW switch");
9632
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009633module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009634MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9635module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9636MODULE_PARM_DESC(bluetooth_state,
9637 "Initial state of the emulated bluetooth switch");
9638
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009639module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009640MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9641module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9642MODULE_PARM_DESC(wwan_state,
9643 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009644
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009645module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009646MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9647module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9648MODULE_PARM_DESC(uwb_state,
9649 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009650#endif
9651
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009652static void thinkpad_acpi_module_exit(void)
9653{
9654 struct ibm_struct *ibm, *itmp;
9655
9656 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9657
9658 list_for_each_entry_safe_reverse(ibm, itmp,
9659 &tpacpi_all_drivers,
9660 all_drivers) {
9661 ibm_exit(ibm);
9662 }
9663
9664 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9665
9666 if (tpacpi_inputdev) {
9667 if (tp_features.input_device_registered)
9668 input_unregister_device(tpacpi_inputdev);
9669 else
9670 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +10009671 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009672 }
9673
9674 if (tpacpi_hwmon)
9675 hwmon_device_unregister(tpacpi_hwmon);
9676
9677 if (tp_features.sensors_pdev_attrs_registered)
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009678 device_remove_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009679 if (tpacpi_sensors_pdev)
9680 platform_device_unregister(tpacpi_sensors_pdev);
9681 if (tpacpi_pdev)
9682 platform_device_unregister(tpacpi_pdev);
9683
9684 if (tp_features.sensors_pdrv_attrs_registered)
9685 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9686 if (tp_features.platform_drv_attrs_registered)
9687 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9688
9689 if (tp_features.sensors_pdrv_registered)
9690 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9691
9692 if (tp_features.platform_drv_registered)
9693 platform_driver_unregister(&tpacpi_pdriver);
9694
9695 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009696 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009697
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009698 if (tpacpi_wq)
9699 destroy_workqueue(tpacpi_wq);
9700
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009701 kfree(thinkpad_id.bios_version_str);
9702 kfree(thinkpad_id.ec_version_str);
9703 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +10009704 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009705}
9706
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009707
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009708static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009709{
9710 int ret, i;
9711
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009712 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9713
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009714 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009715
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009716 ret = get_thinkpad_model_data(&thinkpad_id);
9717 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009718 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009719 thinkpad_acpi_module_exit();
9720 return ret;
9721 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009722 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009723 if (ret) {
9724 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009725 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009727
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009728 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009729
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009730 thinkpad_acpi_init_banner();
9731 tpacpi_check_outdated_fw();
9732
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009733 TPACPI_ACPIHANDLE_INIT(ecrd);
9734 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009736 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9737 if (!tpacpi_wq) {
9738 thinkpad_acpi_module_exit();
9739 return -ENOMEM;
9740 }
9741
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009742 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009743 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07009744 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009745 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009746 return -ENODEV;
9747 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009748
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009749 ret = platform_driver_register(&tpacpi_pdriver);
9750 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009751 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009752 thinkpad_acpi_module_exit();
9753 return ret;
9754 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009755 tp_features.platform_drv_registered = 1;
9756
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009757 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9758 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009759 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009760 thinkpad_acpi_module_exit();
9761 return ret;
9762 }
9763 tp_features.sensors_pdrv_registered = 1;
9764
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009765 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009766 if (!ret) {
9767 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009768 ret = tpacpi_create_driver_attributes(
9769 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009770 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009771 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009772 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009773 thinkpad_acpi_module_exit();
9774 return ret;
9775 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009776 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009777
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009778
9779 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009780 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009781 NULL, 0);
9782 if (IS_ERR(tpacpi_pdev)) {
9783 ret = PTR_ERR(tpacpi_pdev);
9784 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009785 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009786 thinkpad_acpi_module_exit();
9787 return ret;
9788 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009789 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009790 TPACPI_HWMON_DRVR_NAME,
9791 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009792 if (IS_ERR(tpacpi_sensors_pdev)) {
9793 ret = PTR_ERR(tpacpi_sensors_pdev);
9794 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009795 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009796 thinkpad_acpi_module_exit();
9797 return ret;
9798 }
Bjørn Mork5fb73bc2015-05-19 19:45:03 +02009799 ret = device_create_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009800 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009801 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009802 thinkpad_acpi_module_exit();
9803 return ret;
9804 }
9805 tp_features.sensors_pdev_attrs_registered = 1;
9806 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009807 if (IS_ERR(tpacpi_hwmon)) {
9808 ret = PTR_ERR(tpacpi_hwmon);
9809 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009810 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009811 thinkpad_acpi_module_exit();
9812 return ret;
9813 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009814 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009815 tpacpi_inputdev = input_allocate_device();
9816 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009817 thinkpad_acpi_module_exit();
9818 return -ENOMEM;
9819 } else {
9820 /* Prepare input device, but don't register */
9821 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009822 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009823 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009824 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009825 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9826 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009827 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009828 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009829
9830 /* Init subdriver dependencies */
9831 tpacpi_detect_brightness_capabilities();
9832
9833 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009834 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9835 ret = ibm_init(&ibms_init[i]);
9836 if (ret >= 0 && *ibms_init[i].param)
9837 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009838 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009839 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840 return ret;
9841 }
9842 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009843
9844 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9845
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009846 ret = input_register_device(tpacpi_inputdev);
9847 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009848 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009849 thinkpad_acpi_module_exit();
9850 return ret;
9851 } else {
9852 tp_features.input_device_registered = 1;
9853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009854
9855 return 0;
9856}
9857
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009858MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9859
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009860/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009861 * This will autoload the driver in almost every ThinkPad
9862 * in widespread use.
9863 *
9864 * Only _VERY_ old models, like the 240, 240x and 570 lack
9865 * the HKEY event interface.
9866 */
9867MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9868
9869/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009870 * DMI matching for module autoloading
9871 *
9872 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9873 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9874 *
9875 * Only models listed in thinkwiki will be supported, so add yours
9876 * if it is not there yet.
9877 */
9878#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009879 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009880
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009881/* Ancient thinkpad BIOSes have to be identified by
9882 * BIOS type or model number, and there are far less
9883 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009884IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009885
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009886MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9887MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009888MODULE_DESCRIPTION(TPACPI_DESC);
9889MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009890MODULE_LICENSE("GPL");
9891
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009892module_init(thinkpad_acpi_module_init);
9893module_exit(thinkpad_acpi_module_exit);