]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/platform/x86/thinkpad_acpi.c
platform/x86: thinkpad_acpi: Add audio mute LED classdev support
[mirror_ubuntu-jammy-kernel.git] / drivers / platform / x86 / thinkpad_acpi.c
CommitLineData
1da177e4 1/*
643f12db 2 * thinkpad_acpi.c - ThinkPad ACPI Extras
1da177e4
LT
3 *
4 *
78f81cc4 5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
1c762ca4 6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
1da177e4
LT
7 *
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
a62bc916
HMH
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
78f81cc4
BD
22 */
23
0978e012
JP
24#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
2801b968 26#define TPACPI_VERSION "0.26"
6b99e356 27#define TPACPI_SYSFS_VERSION 0x030000
78f81cc4
BD
28
29/*
1da177e4 30 * Changelog:
4b45cc07
HMH
31 * 2007-10-20 changelog trimmed down
32 *
643f12db
HMH
33 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
f9ff43a6
HMH
35 *
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.
837ca6dd 39 *
78f81cc4
BD
40 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
4b45cc07
HMH
42 *
43 * 2005-01-16 0.9 use MODULE_VERSION
78f81cc4
BD
44 * 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>
1da177e4
LT
50 */
51
0c78039f
HMH
52#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>
73a94d86 59#include <linux/sched.h>
7a36094d 60#include <linux/sched/signal.h>
0c78039f
HMH
61#include <linux/kthread.h>
62#include <linux/freezer.h>
63#include <linux/delay.h>
5a0e3ad6 64#include <linux/slab.h>
0c78039f
HMH
65#include <linux/nvram.h>
66#include <linux/proc_fs.h>
887965e6 67#include <linux/seq_file.h>
0c78039f
HMH
68#include <linux/sysfs.h>
69#include <linux/backlight.h>
2801b968 70#include <linux/bitops.h>
0c78039f
HMH
71#include <linux/fb.h>
72#include <linux/platform_device.h>
73#include <linux/hwmon.h>
74#include <linux/hwmon-sysfs.h>
75#include <linux/input.h>
4fa6811b 76#include <linux/leds.h>
0e74dc26 77#include <linux/rfkill.h>
0c78039f
HMH
78#include <linux/dmi.h>
79#include <linux/jiffies.h>
80#include <linux/workqueue.h>
8b48463f
LZ
81#include <linux/acpi.h>
82#include <linux/pci_ids.h>
2801b968 83#include <linux/power_supply.h>
8b48463f 84#include <linux/thinkpad_acpi.h>
0d204c34
HMH
85#include <sound/core.h>
86#include <sound/control.h>
87#include <sound/initval.h>
7c0f6ba6 88#include <linux/uaccess.h>
2801b968 89#include <acpi/battery.h>
b33c6ce5 90#include <acpi/video.h>
0c78039f
HMH
91
92/* ThinkPad CMOS commands */
93#define TP_CMOS_VOLUME_DOWN 0
94#define TP_CMOS_VOLUME_UP 1
95#define TP_CMOS_VOLUME_MUTE 2
96#define TP_CMOS_BRIGHTNESS_UP 4
97#define TP_CMOS_BRIGHTNESS_DOWN 5
4fa6811b
HMH
98#define TP_CMOS_THINKLIGHT_ON 12
99#define TP_CMOS_THINKLIGHT_OFF 13
0c78039f
HMH
100
101/* NVRAM Addresses */
102enum tp_nvram_addr {
103 TP_NVRAM_ADDR_HK2 = 0x57,
104 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
105 TP_NVRAM_ADDR_VIDEO = 0x59,
106 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
107 TP_NVRAM_ADDR_MIXER = 0x60,
108};
109
110/* NVRAM bit masks */
111enum {
112 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
113 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
114 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
115 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
116 TP_NVRAM_MASK_THINKLIGHT = 0x10,
117 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
118 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
119 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
120 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
121 TP_NVRAM_MASK_MUTE = 0x40,
122 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
123 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
124 TP_NVRAM_POS_LEVEL_VOLUME = 0,
125};
126
5d756db9
HMH
127/* Misc NVRAM-related */
128enum {
129 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
130};
131
b21a15f6 132/* ACPI HIDs */
9fbdaeb4
MI
133#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
134#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
a3c42a46 135#define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
437e470c 136#define TPACPI_ACPI_EC_HID "PNP0C09"
b21a15f6
HMH
137
138/* Input IDs */
139#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
140#define TPACPI_HKEY_INPUT_VERSION 0x4101
141
153f8220
HMH
142/* ACPI \WGSV commands */
143enum {
144 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
145 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
146 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
147 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
148};
149
150/* TP_ACPI_WGSV_GET_STATE bits */
151enum {
152 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
153 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
154 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
155 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
156 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
157 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
158 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
159 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
160 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
161 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
162};
b21a15f6 163
67bcae6e
HMH
164/* HKEY events */
165enum tpacpi_hkey_event_t {
166 /* Hotkey-related */
167 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
168 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
169 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
c685e20d 170 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
67bcae6e
HMH
171 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
172 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
173 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
174
175 /* Reasons for waking up from S3/S4 */
176 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
177 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
178 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
179 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
180 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
181 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
182
183 /* Auto-sleep after eject request */
184 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
185 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
186
187 /* Misc bay events */
188 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
a50245af
HMH
189 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
190 or port replicator */
191 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
192 dock or port replicator */
67bcae6e
HMH
193
194 /* User-interface events */
195 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
196 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
197 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
198 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
b03f4d49
L
199 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
200 * enter/leave tablet mode
201 */
67bcae6e
HMH
202 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
203 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
204 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
205
2d43f671
HMH
206 /* Key-related user-interface events */
207 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
208 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
67ab6248 209 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
2d43f671 210
67bcae6e
HMH
211 /* Thermal events */
212 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
213 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
214 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
215 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
6e6bc5f6
HMH
216 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
217 TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
218 * command completed. Related to
219 * AML DYTC */
220 TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
221 * changed. Related to AML GMTS */
67bcae6e 222
6f62bc3e
RH
223 /* AC-related events */
224 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
2d43f671 225
587d8628
DH
226 /* Further user-interface events */
227 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
228 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
229
67bcae6e
HMH
230 /* Misc */
231 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
232};
233
b21a15f6
HMH
234/****************************************************************************
235 * Main driver
236 */
237
e0c7dfe7
HMH
238#define TPACPI_NAME "thinkpad"
239#define TPACPI_DESC "ThinkPad ACPI Extras"
240#define TPACPI_FILE TPACPI_NAME "_acpi"
241#define TPACPI_URL "http://ibm-acpi.sf.net/"
242#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
b21a15f6 243
e0c7dfe7
HMH
244#define TPACPI_PROC_DIR "ibm"
245#define TPACPI_ACPI_EVENT_PREFIX "ibm"
246#define TPACPI_DRVR_NAME TPACPI_FILE
95e57ab2 247#define TPACPI_DRVR_SHORTNAME "tpacpi"
e0c7dfe7 248#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
b21a15f6 249
95e57ab2 250#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
e0e3c061 251#define TPACPI_WORKQUEUE_NAME "ktpacpid"
95e57ab2 252
e0c7dfe7 253#define TPACPI_MAX_ACPI_ARGS 3
b21a15f6 254
7ff8d62f 255/* Debugging printk groups */
0c78039f 256#define TPACPI_DBG_ALL 0xffff
73a94d86 257#define TPACPI_DBG_DISCLOSETASK 0x8000
0c78039f
HMH
258#define TPACPI_DBG_INIT 0x0001
259#define TPACPI_DBG_EXIT 0x0002
bee4cd9b 260#define TPACPI_DBG_RFKILL 0x0004
56e2c200 261#define TPACPI_DBG_HKEY 0x0008
74a60c0f 262#define TPACPI_DBG_FAN 0x0010
0e501834 263#define TPACPI_DBG_BRGHT 0x0020
329e4e18 264#define TPACPI_DBG_MIXER 0x0040
0c78039f 265
35ff8b9f
HMH
266#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
267#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
268#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
0c78039f 269
0c78039f
HMH
270
271/****************************************************************************
b21a15f6 272 * Driver-wide structs and misc. variables
0c78039f
HMH
273 */
274
275struct ibm_struct;
276
277struct tp_acpi_drv_struct {
278 const struct acpi_device_id *hid;
279 struct acpi_driver *driver;
280
281 void (*notify) (struct ibm_struct *, u32);
282 acpi_handle *handle;
283 u32 type;
284 struct acpi_device *device;
285};
286
287struct ibm_struct {
288 char *name;
289
887965e6 290 int (*read) (struct seq_file *);
0c78039f
HMH
291 int (*write) (char *);
292 void (*exit) (void);
293 void (*resume) (void);
fd3c3a42 294 void (*suspend) (void);
90d9d3c7 295 void (*shutdown) (void);
0c78039f
HMH
296
297 struct list_head all_drivers;
298
299 struct tp_acpi_drv_struct *acpi;
300
301 struct {
302 u8 acpi_driver_registered:1;
303 u8 acpi_notify_installed:1;
304 u8 proc_created:1;
305 u8 init_called:1;
306 u8 experimental:1;
307 } flags;
308};
309
310struct ibm_init_struct {
311 char param[32];
312
313 int (*init) (struct ibm_init_struct *);
d161a13f 314 umode_t base_procfs_mode;
0c78039f
HMH
315 struct ibm_struct *data;
316};
317
318static struct {
0c78039f
HMH
319 u32 bluetooth:1;
320 u32 hotkey:1;
321 u32 hotkey_mask:1;
322 u32 hotkey_wlsw:1;
b3180028
L
323 enum {
324 TP_HOTKEY_TABLET_NONE = 0,
325 TP_HOTKEY_TABLET_USES_MHKG,
dda3ec0a 326 TP_HOTKEY_TABLET_USES_GMMS,
b3180028 327 } hotkey_tablet;
bb28f3d5 328 u32 kbdlight:1;
0c78039f
HMH
329 u32 light:1;
330 u32 light_status:1;
b5972796 331 u32 bright_acpimode:1;
77775838 332 u32 bright_unkfw:1;
0c78039f 333 u32 wan:1;
0045c0aa 334 u32 uwb:1;
0c78039f 335 u32 fan_ctrl_status_undef:1;
d7377247 336 u32 second_fan:1;
60201732 337 u32 beep_needs_two_args:1;
a112ceee 338 u32 mixer_no_level_control:1;
1a32ebb2 339 u32 battery_force_primary:1;
0c78039f
HMH
340 u32 input_device_registered:1;
341 u32 platform_drv_registered:1;
342 u32 platform_drv_attrs_registered:1;
343 u32 sensors_pdrv_registered:1;
344 u32 sensors_pdrv_attrs_registered:1;
345 u32 sensors_pdev_attrs_registered:1;
346 u32 hotkey_poll_active:1;
f23a5bcb 347 u32 has_adaptive_kbd:1;
0c78039f
HMH
348} tp_features;
349
92889022
HMH
350static struct {
351 u16 hotkey_mask_ff:1;
c7ac6291 352 u16 volume_ctrl_forbidden:1;
92889022
HMH
353} tp_warned;
354
0c78039f
HMH
355struct thinkpad_id_data {
356 unsigned int vendor; /* ThinkPad vendor:
357 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
358
359 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
360 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
361
846a416b
JW
362 u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
363 u32 ec_model;
364 u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
050df107 365 u16 ec_release;
0c78039f 366
8c74adbc
HMH
367 char *model_str; /* ThinkPad T43 */
368 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
0c78039f 369};
0c78039f
HMH
370static struct thinkpad_id_data thinkpad_id;
371
8fef502e
HMH
372static enum {
373 TPACPI_LIFE_INIT = 0,
374 TPACPI_LIFE_RUNNING,
375 TPACPI_LIFE_EXITING,
376} tpacpi_lifecycle;
377
b21a15f6
HMH
378static int experimental;
379static u32 dbg_level;
380
e0e3c061
HMH
381static struct workqueue_struct *tpacpi_wq;
382
75bd3bf2
HMH
383enum led_status_t {
384 TPACPI_LED_OFF = 0,
385 TPACPI_LED_ON,
386 TPACPI_LED_BLINK,
387};
388
86ec0c2c 389/* tpacpi LED class */
4fa6811b
HMH
390struct tpacpi_led_classdev {
391 struct led_classdev led_classdev;
edf2d778 392 int led;
4fa6811b
HMH
393};
394
77775838
HMH
395/* brightness level capabilities */
396static unsigned int bright_maxlvl; /* 0 = unknown */
397
a73f3091
HMH
398#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
399static int dbg_wlswemul;
90ab5ee9 400static bool tpacpi_wlsw_emulstate;
a73f3091 401static int dbg_bluetoothemul;
90ab5ee9 402static bool tpacpi_bluetooth_emulstate;
a73f3091 403static int dbg_wwanemul;
90ab5ee9 404static bool tpacpi_wwan_emulstate;
0045c0aa 405static int dbg_uwbemul;
90ab5ee9 406static bool tpacpi_uwb_emulstate;
a73f3091
HMH
407#endif
408
409
3dcc2c3b
HMH
410/*************************************************************************
411 * Debugging helpers
412 */
413
0978e012
JP
414#define dbg_printk(a_dbg_level, format, arg...) \
415do { \
416 if (dbg_level & (a_dbg_level)) \
417 printk(KERN_DEBUG pr_fmt("%s: " format), \
418 __func__, ##arg); \
419} while (0)
3dcc2c3b
HMH
420
421#ifdef CONFIG_THINKPAD_ACPI_DEBUG
422#define vdbg_printk dbg_printk
423static const char *str_supported(int is_supported);
424#else
0978e012
JP
425static inline const char *str_supported(int is_supported) { return ""; }
426#define vdbg_printk(a_dbg_level, format, arg...) \
efd85cf8 427 do { if (0) no_printk(format, ##arg); } while (0)
3dcc2c3b
HMH
428#endif
429
73a94d86
HMH
430static void tpacpi_log_usertask(const char * const what)
431{
0978e012
JP
432 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
433 what, task_tgid_vnr(current));
73a94d86
HMH
434}
435
0978e012
JP
436#define tpacpi_disclose_usertask(what, format, arg...) \
437do { \
438 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
439 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
440 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
441 what, task_tgid_vnr(current), ## arg); \
442 } \
443} while (0)
3dcc2c3b 444
7d95a3d5
HMH
445/*
446 * Quirk handling helpers
447 *
846a416b
JW
448 * ThinkPad IDs and versions seen in the field so far are
449 * two or three characters from the set [0-9A-Z], i.e. base 36.
7d95a3d5
HMH
450 *
451 * We use values well outside that range as specials.
452 */
453
846a416b
JW
454#define TPACPI_MATCH_ANY 0xffffffffU
455#define TPACPI_MATCH_ANY_VERSION 0xffffU
7d95a3d5
HMH
456#define TPACPI_MATCH_UNKNOWN 0U
457
846a416b
JW
458/* TPID('1', 'Y') == 0x3159 */
459#define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
460#define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
461#define TPVER TPID
7d95a3d5
HMH
462
463#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
464 { .vendor = PCI_VENDOR_ID_IBM, \
465 .bios = TPID(__id1, __id2), \
466 .ec = TPACPI_MATCH_ANY, \
467 .quirks = (__quirk) }
468
469#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
470 { .vendor = PCI_VENDOR_ID_LENOVO, \
471 .bios = TPID(__id1, __id2), \
472 .ec = TPACPI_MATCH_ANY, \
473 .quirks = (__quirk) }
474
1a32ebb2
JW
475#define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
476 { .vendor = PCI_VENDOR_ID_LENOVO, \
477 .bios = TPID3(__id1, __id2, __id3), \
478 .ec = TPACPI_MATCH_ANY, \
479 .quirks = (__quirk) }
480
a112ceee
HMH
481#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
482 { .vendor = PCI_VENDOR_ID_LENOVO, \
483 .bios = TPACPI_MATCH_ANY, \
484 .ec = TPID(__id1, __id2), \
485 .quirks = (__quirk) }
486
7d95a3d5
HMH
487struct tpacpi_quirk {
488 unsigned int vendor;
846a416b
JW
489 u32 bios;
490 u32 ec;
7d95a3d5
HMH
491 unsigned long quirks;
492};
493
494/**
495 * tpacpi_check_quirks() - search BIOS/EC version on a list
496 * @qlist: array of &struct tpacpi_quirk
497 * @qlist_size: number of elements in @qlist
498 *
499 * Iterates over a quirks list until one is found that matches the
500 * ThinkPad's vendor, BIOS and EC model.
501 *
502 * Returns 0 if nothing matches, otherwise returns the quirks field of
503 * the matching &struct tpacpi_quirk entry.
504 *
505 * The match criteria is: vendor, ec and bios much match.
506 */
507static unsigned long __init tpacpi_check_quirks(
508 const struct tpacpi_quirk *qlist,
509 unsigned int qlist_size)
510{
511 while (qlist_size) {
512 if ((qlist->vendor == thinkpad_id.vendor ||
513 qlist->vendor == TPACPI_MATCH_ANY) &&
514 (qlist->bios == thinkpad_id.bios_model ||
515 qlist->bios == TPACPI_MATCH_ANY) &&
516 (qlist->ec == thinkpad_id.ec_model ||
517 qlist->ec == TPACPI_MATCH_ANY))
518 return qlist->quirks;
519
520 qlist_size--;
521 qlist++;
522 }
523 return 0;
524}
525
e28393c0
HMH
526static inline bool __pure __init tpacpi_is_lenovo(void)
527{
528 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
529}
530
531static inline bool __pure __init tpacpi_is_ibm(void)
532{
533 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
534}
7d95a3d5 535
56b6aeb0
HMH
536/****************************************************************************
537 ****************************************************************************
538 *
539 * ACPI Helpers and device model
540 *
541 ****************************************************************************
542 ****************************************************************************/
543
544/*************************************************************************
545 * ACPI basic handles
546 */
1da177e4 547
94954cc6 548static acpi_handle root_handle;
437e470c 549static acpi_handle ec_handle;
1da177e4 550
e0c7dfe7 551#define TPACPI_HANDLE(object, parent, paths...) \
1da177e4 552 static acpi_handle object##_handle; \
3bd01896 553 static const acpi_handle * const object##_parent __initconst = \
ef07a5ab
HMH
554 &parent##_handle; \
555 static char *object##_paths[] __initdata = { paths }
1da177e4 556
e0c7dfe7
HMH
557TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
558TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
78f81cc4 559
35ff8b9f
HMH
560TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
561 /* T4x, X31, X40 */
78f81cc4
BD
562 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
563 "\\CMS", /* R40, R40e */
56b6aeb0 564 ); /* all others */
78f81cc4 565
e0c7dfe7 566TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
78f81cc4
BD
567 "^HKEY", /* R30, R31 */
568 "HKEY", /* all others */
56b6aeb0 569 ); /* 570 */
78f81cc4 570
56b6aeb0
HMH
571/*************************************************************************
572 * ACPI helpers
a8b7a662
HMH
573 */
574
1da177e4 575static int acpi_evalf(acpi_handle handle,
eceeb437 576 int *res, char *method, char *fmt, ...)
1da177e4
LT
577{
578 char *fmt0 = fmt;
78f81cc4 579 struct acpi_object_list params;
e0c7dfe7 580 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
78f81cc4
BD
581 struct acpi_buffer result, *resultp;
582 union acpi_object out_obj;
583 acpi_status status;
584 va_list ap;
585 char res_type;
586 int success;
587 int quiet;
1da177e4
LT
588
589 if (!*fmt) {
0978e012 590 pr_err("acpi_evalf() called with empty format\n");
1da177e4
LT
591 return 0;
592 }
593
594 if (*fmt == 'q') {
595 quiet = 1;
596 fmt++;
597 } else
598 quiet = 0;
599
600 res_type = *(fmt++);
601
602 params.count = 0;
603 params.pointer = &in_objs[0];
604
605 va_start(ap, fmt);
606 while (*fmt) {
607 char c = *(fmt++);
608 switch (c) {
609 case 'd': /* int */
610 in_objs[params.count].integer.value = va_arg(ap, int);
611 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
612 break;
78f81cc4 613 /* add more types as needed */
1da177e4 614 default:
5cac62ac
AS
615 pr_err("acpi_evalf() called with invalid format character '%c'\n",
616 c);
21365851 617 va_end(ap);
1da177e4
LT
618 return 0;
619 }
620 }
621 va_end(ap);
622
78f81cc4
BD
623 if (res_type != 'v') {
624 result.length = sizeof(out_obj);
625 result.pointer = &out_obj;
626 resultp = &result;
627 } else
628 resultp = NULL;
1da177e4 629
78f81cc4 630 status = acpi_evaluate_object(handle, method, &params, resultp);
1da177e4
LT
631
632 switch (res_type) {
78f81cc4 633 case 'd': /* int */
263f4a30
HMH
634 success = (status == AE_OK &&
635 out_obj.type == ACPI_TYPE_INTEGER);
636 if (success && res)
eceeb437 637 *res = out_obj.integer.value;
1da177e4 638 break;
78f81cc4 639 case 'v': /* void */
1da177e4
LT
640 success = status == AE_OK;
641 break;
78f81cc4 642 /* add more types as needed */
1da177e4 643 default:
5cac62ac
AS
644 pr_err("acpi_evalf() called with invalid format character '%c'\n",
645 res_type);
1da177e4
LT
646 return 0;
647 }
648
56b6aeb0 649 if (!success && !quiet)
0978e012 650 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
263f4a30 651 method, fmt0, acpi_format_exception(status));
56b6aeb0
HMH
652
653 return success;
654}
655
35ff8b9f 656static int acpi_ec_read(int i, u8 *p)
56b6aeb0
HMH
657{
658 int v;
659
660 if (ecrd_handle) {
661 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
662 return 0;
663 *p = v;
664 } else {
665 if (ec_read(i, p) < 0)
666 return 0;
667 }
668
669 return 1;
670}
671
672static int acpi_ec_write(int i, u8 v)
673{
674 if (ecwr_handle) {
675 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
676 return 0;
677 } else {
678 if (ec_write(i, v) < 0)
679 return 0;
680 }
681
682 return 1;
683}
684
c9bea99c
HMH
685static int issue_thinkpad_cmos_command(int cmos_cmd)
686{
687 if (!cmos_handle)
688 return -ENXIO;
689
690 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
691 return -EIO;
692
693 return 0;
694}
695
56b6aeb0
HMH
696/*************************************************************************
697 * ACPI device model
698 */
699
35ff8b9f
HMH
700#define TPACPI_ACPIHANDLE_INIT(object) \
701 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
ef07a5ab 702 object##_paths, ARRAY_SIZE(object##_paths))
f74a27d4 703
ef07a5ab
HMH
704static void __init drv_acpi_handle_init(const char *name,
705 acpi_handle *handle, const acpi_handle parent,
706 char **paths, const int num_paths)
56b6aeb0
HMH
707{
708 int i;
709 acpi_status status;
710
5ae930e6
HMH
711 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
712 name);
713
56b6aeb0
HMH
714 for (i = 0; i < num_paths; i++) {
715 status = acpi_get_handle(parent, paths[i], handle);
716 if (ACPI_SUCCESS(status)) {
5ae930e6
HMH
717 dbg_printk(TPACPI_DBG_INIT,
718 "Found ACPI handle %s for %s\n",
ef07a5ab 719 paths[i], name);
56b6aeb0
HMH
720 return;
721 }
722 }
723
5ae930e6
HMH
724 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
725 name);
56b6aeb0
HMH
726 *handle = NULL;
727}
728
437e470c
HMH
729static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
730 u32 level, void *context, void **return_value)
731{
46445b6b
AL
732 struct acpi_device *dev;
733 if (!strcmp(context, "video")) {
734 if (acpi_bus_get_device(handle, &dev))
735 return AE_OK;
736 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
737 return AE_OK;
738 }
739
437e470c
HMH
740 *(acpi_handle *)return_value = handle;
741
742 return AE_CTRL_TERMINATE;
743}
744
745static void __init tpacpi_acpi_handle_locate(const char *name,
746 const char *hid,
747 acpi_handle *handle)
748{
749 acpi_status status;
750 acpi_handle device_found;
751
46445b6b 752 BUG_ON(!name || !handle);
437e470c
HMH
753 vdbg_printk(TPACPI_DBG_INIT,
754 "trying to locate ACPI handle for %s, using HID %s\n",
46445b6b 755 name, hid ? hid : "NULL");
437e470c
HMH
756
757 memset(&device_found, 0, sizeof(device_found));
758 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
759 (void *)name, &device_found);
760
761 *handle = NULL;
762
763 if (ACPI_SUCCESS(status)) {
764 *handle = device_found;
765 dbg_printk(TPACPI_DBG_INIT,
766 "Found ACPI handle for %s\n", name);
767 } else {
768 vdbg_printk(TPACPI_DBG_INIT,
769 "Could not locate an ACPI handle for %s: %s\n",
770 name, acpi_format_exception(status));
771 }
772}
773
8d376cd6 774static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
56b6aeb0
HMH
775{
776 struct ibm_struct *ibm = data;
777
8fef502e
HMH
778 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
779 return;
780
8d376cd6 781 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
56b6aeb0
HMH
782 return;
783
8d376cd6 784 ibm->acpi->notify(ibm, event);
56b6aeb0
HMH
785}
786
8d376cd6 787static int __init setup_acpi_notify(struct ibm_struct *ibm)
56b6aeb0
HMH
788{
789 acpi_status status;
5ae930e6 790 int rc;
56b6aeb0 791
8d376cd6
HMH
792 BUG_ON(!ibm->acpi);
793
794 if (!*ibm->acpi->handle)
56b6aeb0
HMH
795 return 0;
796
5ae930e6 797 vdbg_printk(TPACPI_DBG_INIT,
fe08bc4b
HMH
798 "setting up ACPI notify for %s\n", ibm->name);
799
5ae930e6
HMH
800 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
801 if (rc < 0) {
0978e012 802 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
56b6aeb0
HMH
803 return -ENODEV;
804 }
805
db89b4f0 806 ibm->acpi->device->driver_data = ibm;
8d376cd6 807 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
e0c7dfe7 808 TPACPI_ACPI_EVENT_PREFIX,
643f12db 809 ibm->name);
56b6aeb0 810
8d376cd6
HMH
811 status = acpi_install_notify_handler(*ibm->acpi->handle,
812 ibm->acpi->type, dispatch_acpi_notify, ibm);
56b6aeb0
HMH
813 if (ACPI_FAILURE(status)) {
814 if (status == AE_ALREADY_EXISTS) {
5cac62ac
AS
815 pr_notice("another device driver is already handling %s events\n",
816 ibm->name);
56b6aeb0 817 } else {
0978e012 818 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
72f19921 819 ibm->name, acpi_format_exception(status));
56b6aeb0
HMH
820 }
821 return -ENODEV;
822 }
8d376cd6 823 ibm->flags.acpi_notify_installed = 1;
56b6aeb0
HMH
824 return 0;
825}
826
8d376cd6 827static int __init tpacpi_device_add(struct acpi_device *device)
56b6aeb0
HMH
828{
829 return 0;
830}
831
6700121b 832static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
56b6aeb0 833{
5ae930e6 834 int rc;
56b6aeb0 835
fe08bc4b
HMH
836 dbg_printk(TPACPI_DBG_INIT,
837 "registering %s as an ACPI driver\n", ibm->name);
838
8d376cd6
HMH
839 BUG_ON(!ibm->acpi);
840
841 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
842 if (!ibm->acpi->driver) {
0978e012 843 pr_err("failed to allocate memory for ibm->acpi->driver\n");
5fba344c 844 return -ENOMEM;
56b6aeb0
HMH
845 }
846
e0c7dfe7 847 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
8d376cd6 848 ibm->acpi->driver->ids = ibm->acpi->hid;
1ba90e3a 849
8d376cd6 850 ibm->acpi->driver->ops.add = &tpacpi_device_add;
56b6aeb0 851
5ae930e6
HMH
852 rc = acpi_bus_register_driver(ibm->acpi->driver);
853 if (rc < 0) {
0978e012 854 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
1ba90e3a 855 ibm->name, rc);
8d376cd6
HMH
856 kfree(ibm->acpi->driver);
857 ibm->acpi->driver = NULL;
5ae930e6 858 } else if (!rc)
8d376cd6 859 ibm->flags.acpi_driver_registered = 1;
56b6aeb0 860
5ae930e6 861 return rc;
56b6aeb0
HMH
862}
863
864
865/****************************************************************************
866 ****************************************************************************
867 *
868 * Procfs Helpers
869 *
870 ****************************************************************************
871 ****************************************************************************/
872
887965e6 873static int dispatch_proc_show(struct seq_file *m, void *v)
56b6aeb0 874{
887965e6 875 struct ibm_struct *ibm = m->private;
56b6aeb0
HMH
876
877 if (!ibm || !ibm->read)
878 return -EINVAL;
887965e6
AD
879 return ibm->read(m);
880}
56b6aeb0 881
887965e6
AD
882static int dispatch_proc_open(struct inode *inode, struct file *file)
883{
d9dda78b 884 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
56b6aeb0
HMH
885}
886
887965e6 887static ssize_t dispatch_proc_write(struct file *file,
35ff8b9f 888 const char __user *userbuf,
887965e6 889 size_t count, loff_t *pos)
56b6aeb0 890{
d9dda78b 891 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
56b6aeb0
HMH
892 char *kernbuf;
893 int ret;
894
895 if (!ibm || !ibm->write)
896 return -EINVAL;
5b05d469
MB
897 if (count > PAGE_SIZE - 2)
898 return -EINVAL;
56b6aeb0
HMH
899
900 kernbuf = kmalloc(count + 2, GFP_KERNEL);
901 if (!kernbuf)
902 return -ENOMEM;
1da177e4 903
56b6aeb0
HMH
904 if (copy_from_user(kernbuf, userbuf, count)) {
905 kfree(kernbuf);
906 return -EFAULT;
907 }
1da177e4 908
56b6aeb0
HMH
909 kernbuf[count] = 0;
910 strcat(kernbuf, ",");
911 ret = ibm->write(kernbuf);
912 if (ret == 0)
913 ret = count;
1da177e4 914
56b6aeb0
HMH
915 kfree(kernbuf);
916
917 return ret;
1da177e4
LT
918}
919
887965e6
AD
920static const struct file_operations dispatch_proc_fops = {
921 .owner = THIS_MODULE,
922 .open = dispatch_proc_open,
923 .read = seq_read,
924 .llseek = seq_lseek,
925 .release = single_release,
926 .write = dispatch_proc_write,
927};
928
1da177e4
LT
929static char *next_cmd(char **cmds)
930{
931 char *start = *cmds;
932 char *end;
933
934 while ((end = strchr(start, ',')) && end == start)
935 start = end + 1;
936
937 if (!end)
938 return NULL;
939
940 *end = 0;
941 *cmds = end + 1;
942 return start;
943}
944
56b6aeb0 945
54ae1501
HMH
946/****************************************************************************
947 ****************************************************************************
948 *
7f5d1cd6 949 * Device model: input, hwmon and platform
54ae1501
HMH
950 *
951 ****************************************************************************
952 ****************************************************************************/
953
94954cc6 954static struct platform_device *tpacpi_pdev;
7fd40029 955static struct platform_device *tpacpi_sensors_pdev;
1beeffe4 956static struct device *tpacpi_hwmon;
7f5d1cd6 957static struct input_dev *tpacpi_inputdev;
8523ed6f 958static struct mutex tpacpi_inputdev_send_mutex;
b21a15f6 959static LIST_HEAD(tpacpi_all_drivers);
e295e850 960
3567a4e2 961#ifdef CONFIG_PM_SLEEP
4959a781 962static int tpacpi_suspend_handler(struct device *dev)
083f1760
HMH
963{
964 struct ibm_struct *ibm, *itmp;
965
966 list_for_each_entry_safe(ibm, itmp,
967 &tpacpi_all_drivers,
968 all_drivers) {
969 if (ibm->suspend)
fd3c3a42 970 (ibm->suspend)();
083f1760
HMH
971 }
972
973 return 0;
974}
975
4959a781 976static int tpacpi_resume_handler(struct device *dev)
e295e850
HMH
977{
978 struct ibm_struct *ibm, *itmp;
979
980 list_for_each_entry_safe(ibm, itmp,
981 &tpacpi_all_drivers,
982 all_drivers) {
983 if (ibm->resume)
984 (ibm->resume)();
985 }
986
987 return 0;
988}
3567a4e2 989#endif
e295e850 990
4959a781
RW
991static SIMPLE_DEV_PM_OPS(tpacpi_pm,
992 tpacpi_suspend_handler, tpacpi_resume_handler);
993
90d9d3c7
HMH
994static void tpacpi_shutdown_handler(struct platform_device *pdev)
995{
996 struct ibm_struct *ibm, *itmp;
997
998 list_for_each_entry_safe(ibm, itmp,
999 &tpacpi_all_drivers,
1000 all_drivers) {
1001 if (ibm->shutdown)
1002 (ibm->shutdown)();
1003 }
1004}
1005
54ae1501
HMH
1006static struct platform_driver tpacpi_pdriver = {
1007 .driver = {
e0c7dfe7 1008 .name = TPACPI_DRVR_NAME,
4959a781 1009 .pm = &tpacpi_pm,
54ae1501 1010 },
90d9d3c7 1011 .shutdown = tpacpi_shutdown_handler,
54ae1501
HMH
1012};
1013
7fd40029
HMH
1014static struct platform_driver tpacpi_hwmon_pdriver = {
1015 .driver = {
e0c7dfe7 1016 .name = TPACPI_HWMON_DRVR_NAME,
7fd40029
HMH
1017 },
1018};
54ae1501 1019
176750d6 1020/*************************************************************************
b21a15f6 1021 * sysfs support helpers
176750d6
HMH
1022 */
1023
b21a15f6
HMH
1024struct attribute_set {
1025 unsigned int members, max_members;
1026 struct attribute_group group;
176750d6
HMH
1027};
1028
7252374a
HMH
1029struct attribute_set_obj {
1030 struct attribute_set s;
1031 struct attribute *a;
1032} __attribute__((packed));
1033
1034static struct attribute_set *create_attr_set(unsigned int max_members,
35ff8b9f 1035 const char *name)
7252374a
HMH
1036{
1037 struct attribute_set_obj *sobj;
1038
1039 if (max_members == 0)
1040 return NULL;
1041
1042 /* Allocates space for implicit NULL at the end too */
1043 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1044 max_members * sizeof(struct attribute *),
1045 GFP_KERNEL);
1046 if (!sobj)
1047 return NULL;
1048 sobj->s.max_members = max_members;
1049 sobj->s.group.attrs = &sobj->a;
1050 sobj->s.group.name = name;
1051
1052 return &sobj->s;
1053}
1054
f74a27d4
HMH
1055#define destroy_attr_set(_set) \
1056 kfree(_set);
1057
7252374a 1058/* not multi-threaded safe, use it in a single thread per set */
35ff8b9f 1059static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
7252374a
HMH
1060{
1061 if (!s || !attr)
1062 return -EINVAL;
1063
1064 if (s->members >= s->max_members)
1065 return -ENOMEM;
1066
1067 s->group.attrs[s->members] = attr;
1068 s->members++;
1069
1070 return 0;
1071}
1072
35ff8b9f 1073static int add_many_to_attr_set(struct attribute_set *s,
7252374a
HMH
1074 struct attribute **attr,
1075 unsigned int count)
1076{
1077 int i, res;
1078
1079 for (i = 0; i < count; i++) {
1080 res = add_to_attr_set(s, attr[i]);
1081 if (res)
1082 return res;
1083 }
1084
1085 return 0;
1086}
1087
35ff8b9f 1088static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
7252374a
HMH
1089{
1090 sysfs_remove_group(kobj, &s->group);
1091 destroy_attr_set(s);
1092}
1093
f74a27d4
HMH
1094#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1095 sysfs_create_group(_kobj, &_attr_set->group)
1096
7252374a
HMH
1097static int parse_strtoul(const char *buf,
1098 unsigned long max, unsigned long *value)
1099{
1100 char *endp;
1101
e7d2860b
AGR
1102 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1103 endp = skip_spaces(endp);
7252374a
HMH
1104 if (*endp || *value > max)
1105 return -EINVAL;
1106
1107 return 0;
1108}
1109
d64c81c4
HMH
1110static void tpacpi_disable_brightness_delay(void)
1111{
1112 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
0978e012 1113 pr_notice("ACPI backlight control delay disabled\n");
d64c81c4
HMH
1114}
1115
19d337df
JB
1116static void printk_deprecated_attribute(const char * const what,
1117 const char * const details)
1118{
1119 tpacpi_log_usertask("deprecated sysfs attribute");
5cac62ac 1120 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
19d337df
JB
1121 what, details);
1122}
1123
1124/*************************************************************************
1125 * rfkill and radio control support helpers
1126 */
1127
1128/*
1129 * ThinkPad-ACPI firmware handling model:
1130 *
1131 * WLSW (master wireless switch) is event-driven, and is common to all
1132 * firmware-controlled radios. It cannot be controlled, just monitored,
1133 * as expected. It overrides all radio state in firmware
1134 *
1135 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1136 * (TODO: verify how WLSW interacts with the returned radio state).
1137 *
1138 * The only time there are shadow radio state changes, is when
1139 * masked-off hotkeys are used.
1140 */
1141
1142/*
1143 * Internal driver API for radio state:
1144 *
1145 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1146 * bool: true means radio blocked (off)
1147 */
1148enum tpacpi_rfkill_state {
1149 TPACPI_RFK_RADIO_OFF = 0,
1150 TPACPI_RFK_RADIO_ON
1151};
1152
1153/* rfkill switches */
1154enum tpacpi_rfk_id {
1155 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1156 TPACPI_RFK_WWAN_SW_ID,
1157 TPACPI_RFK_UWB_SW_ID,
1158 TPACPI_RFK_SW_MAX
1159};
1160
1161static const char *tpacpi_rfkill_names[] = {
1162 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1163 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1164 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1165 [TPACPI_RFK_SW_MAX] = NULL
1166};
1167
1168/* ThinkPad-ACPI rfkill subdriver */
1169struct tpacpi_rfk {
1170 struct rfkill *rfkill;
1171 enum tpacpi_rfk_id id;
1172 const struct tpacpi_rfk_ops *ops;
1173};
1174
1175struct tpacpi_rfk_ops {
1176 /* firmware interface */
1177 int (*get_status)(void);
1178 int (*set_status)(const enum tpacpi_rfkill_state);
1179};
1180
1181static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1182
1183/* Query FW and update rfkill sw state for a given rfkill switch */
1184static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1185{
1186 int status;
1187
1188 if (!tp_rfk)
1189 return -ENODEV;
1190
1191 status = (tp_rfk->ops->get_status)();
1192 if (status < 0)
1193 return status;
1194
1195 rfkill_set_sw_state(tp_rfk->rfkill,
1196 (status == TPACPI_RFK_RADIO_OFF));
1197
1198 return status;
1199}
1200
1201/* Query FW and update rfkill sw state for all rfkill switches */
1202static void tpacpi_rfk_update_swstate_all(void)
1203{
1204 unsigned int i;
1205
1206 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1207 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1208}
1209
1210/*
1211 * Sync the HW-blocking state of all rfkill switches,
1212 * do notice it causes the rfkill core to schedule uevents
1213 */
1214static void tpacpi_rfk_update_hwblock_state(bool blocked)
1215{
1216 unsigned int i;
1217 struct tpacpi_rfk *tp_rfk;
1218
1219 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1220 tp_rfk = tpacpi_rfkill_switches[i];
1221 if (tp_rfk) {
1222 if (rfkill_set_hw_state(tp_rfk->rfkill,
1223 blocked)) {
1224 /* ignore -- we track sw block */
1225 }
1226 }
1227 }
1228}
1229
1230/* Call to get the WLSW state from the firmware */
1231static int hotkey_get_wlsw(void);
1232
1233/* Call to query WLSW state and update all rfkill switches */
1234static bool tpacpi_rfk_check_hwblock_state(void)
1235{
1236 int res = hotkey_get_wlsw();
1237 int hw_blocked;
1238
1239 /* When unknown or unsupported, we have to assume it is unblocked */
1240 if (res < 0)
1241 return false;
1242
1243 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1244 tpacpi_rfk_update_hwblock_state(hw_blocked);
1245
1246 return hw_blocked;
1247}
1248
1249static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1250{
1251 struct tpacpi_rfk *tp_rfk = data;
1252 int res;
1253
1254 dbg_printk(TPACPI_DBG_RFKILL,
1255 "request to change radio state to %s\n",
1256 blocked ? "blocked" : "unblocked");
1257
1258 /* try to set radio state */
1259 res = (tp_rfk->ops->set_status)(blocked ?
1260 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1261
1262 /* and update the rfkill core with whatever the FW really did */
1263 tpacpi_rfk_update_swstate(tp_rfk);
1264
1265 return (res < 0) ? res : 0;
1266}
1267
1268static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1269 .set_block = tpacpi_rfk_hook_set_block,
1270};
1271
1272static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1273 const struct tpacpi_rfk_ops *tp_rfkops,
0e74dc26
HMH
1274 const enum rfkill_type rfktype,
1275 const char *name,
19d337df 1276 const bool set_default)
0e74dc26 1277{
19d337df 1278 struct tpacpi_rfk *atp_rfk;
0e74dc26 1279 int res;
06d5caf4 1280 bool sw_state = false;
5451a923 1281 bool hw_state;
06d5caf4 1282 int sw_status;
90d9d3c7 1283
19d337df
JB
1284 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1285
19d337df
JB
1286 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1287 if (atp_rfk)
1288 atp_rfk->rfkill = rfkill_alloc(name,
1289 &tpacpi_pdev->dev,
1290 rfktype,
1291 &tpacpi_rfk_rfkill_ops,
1292 atp_rfk);
1293 if (!atp_rfk || !atp_rfk->rfkill) {
0978e012 1294 pr_err("failed to allocate memory for rfkill class\n");
19d337df 1295 kfree(atp_rfk);
0e74dc26
HMH
1296 return -ENOMEM;
1297 }
1298
19d337df
JB
1299 atp_rfk->id = id;
1300 atp_rfk->ops = tp_rfkops;
0e74dc26 1301
06d5caf4
AJ
1302 sw_status = (tp_rfkops->get_status)();
1303 if (sw_status < 0) {
0978e012
JP
1304 pr_err("failed to read initial state for %s, error %d\n",
1305 name, sw_status);
b3fa1329 1306 } else {
06d5caf4 1307 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
b3fa1329
AJ
1308 if (set_default) {
1309 /* try to keep the initial state, since we ask the
1310 * firmware to preserve it across S5 in NVRAM */
06d5caf4 1311 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
b3fa1329
AJ
1312 }
1313 }
5451a923
HMH
1314 hw_state = tpacpi_rfk_check_hwblock_state();
1315 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
19d337df
JB
1316
1317 res = rfkill_register(atp_rfk->rfkill);
0e74dc26 1318 if (res < 0) {
0978e012 1319 pr_err("failed to register %s rfkill switch: %d\n", name, res);
19d337df
JB
1320 rfkill_destroy(atp_rfk->rfkill);
1321 kfree(atp_rfk);
0e74dc26
HMH
1322 return res;
1323 }
1324
19d337df 1325 tpacpi_rfkill_switches[id] = atp_rfk;
5451a923 1326
0978e012 1327 pr_info("rfkill switch %s: radio is %sblocked\n",
5451a923 1328 name, (sw_state || hw_state) ? "" : "un");
0e74dc26
HMH
1329 return 0;
1330}
1331
19d337df 1332static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
73a94d86 1333{
19d337df
JB
1334 struct tpacpi_rfk *tp_rfk;
1335
1336 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1337
1338 tp_rfk = tpacpi_rfkill_switches[id];
1339 if (tp_rfk) {
1340 rfkill_unregister(tp_rfk->rfkill);
5f0dadb4 1341 rfkill_destroy(tp_rfk->rfkill);
19d337df
JB
1342 tpacpi_rfkill_switches[id] = NULL;
1343 kfree(tp_rfk);
1344 }
73a94d86
HMH
1345}
1346
1347static void printk_deprecated_rfkill_attribute(const char * const what)
1348{
1349 printk_deprecated_attribute(what,
1350 "Please switch to generic rfkill before year 2010");
1351}
1352
19d337df
JB
1353/* sysfs <radio> enable ------------------------------------------------ */
1354static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1355 struct device_attribute *attr,
1356 char *buf)
1357{
1358 int status;
1359
1360 printk_deprecated_rfkill_attribute(attr->attr.name);
1361
1362 /* This is in the ABI... */
1363 if (tpacpi_rfk_check_hwblock_state()) {
1364 status = TPACPI_RFK_RADIO_OFF;
1365 } else {
1366 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1367 if (status < 0)
1368 return status;
1369 }
1370
1371 return snprintf(buf, PAGE_SIZE, "%d\n",
1372 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1373}
1374
1375static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1376 struct device_attribute *attr,
1377 const char *buf, size_t count)
1378{
1379 unsigned long t;
1380 int res;
1381
1382 printk_deprecated_rfkill_attribute(attr->attr.name);
1383
1384 if (parse_strtoul(buf, 1, &t))
1385 return -EINVAL;
1386
1387 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1388
1389 /* This is in the ABI... */
1390 if (tpacpi_rfk_check_hwblock_state() && !!t)
1391 return -EPERM;
1392
1393 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1394 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1395 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1396
1397 return (res < 0) ? res : count;
1398}
1399
1400/* procfs -------------------------------------------------------------- */
887965e6 1401static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
19d337df 1402{
19d337df 1403 if (id >= TPACPI_RFK_SW_MAX)
887965e6 1404 seq_printf(m, "status:\t\tnot supported\n");
19d337df
JB
1405 else {
1406 int status;
1407
1408 /* This is in the ABI... */
1409 if (tpacpi_rfk_check_hwblock_state()) {
1410 status = TPACPI_RFK_RADIO_OFF;
1411 } else {
1412 status = tpacpi_rfk_update_swstate(
1413 tpacpi_rfkill_switches[id]);
1414 if (status < 0)
1415 return status;
1416 }
1417
887965e6 1418 seq_printf(m, "status:\t\t%s\n",
19d337df
JB
1419 (status == TPACPI_RFK_RADIO_ON) ?
1420 "enabled" : "disabled");
887965e6 1421 seq_printf(m, "commands:\tenable, disable\n");
19d337df
JB
1422 }
1423
887965e6 1424 return 0;
19d337df
JB
1425}
1426
1427static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1428{
1429 char *cmd;
1430 int status = -1;
1431 int res = 0;
1432
1433 if (id >= TPACPI_RFK_SW_MAX)
1434 return -ENODEV;
1435
1436 while ((cmd = next_cmd(&buf))) {
1437 if (strlencmp(cmd, "enable") == 0)
1438 status = TPACPI_RFK_RADIO_ON;
1439 else if (strlencmp(cmd, "disable") == 0)
1440 status = TPACPI_RFK_RADIO_OFF;
1441 else
1442 return -EINVAL;
1443 }
1444
1445 if (status != -1) {
1446 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1447 (status == TPACPI_RFK_RADIO_ON) ?
1448 "enable" : "disable",
1449 tpacpi_rfkill_names[id]);
1450 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1451 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1452 }
1453
1454 return res;
1455}
1456
56b6aeb0 1457/*************************************************************************
b21a15f6 1458 * thinkpad-acpi driver attributes
56b6aeb0
HMH
1459 */
1460
b21a15f6 1461/* interface_version --------------------------------------------------- */
ac3054c4 1462static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1da177e4 1463{
b21a15f6
HMH
1464 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1465}
ac3054c4 1466static DRIVER_ATTR_RO(interface_version);
b21a15f6
HMH
1467
1468/* debug_level --------------------------------------------------------- */
ac3054c4 1469static ssize_t debug_level_show(struct device_driver *drv, char *buf)
b21a15f6
HMH
1470{
1471 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1472}
1473
ac3054c4
GKH
1474static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1475 size_t count)
b21a15f6
HMH
1476{
1477 unsigned long t;
1478
1479 if (parse_strtoul(buf, 0xffff, &t))
1480 return -EINVAL;
1481
1482 dbg_level = t;
1483
1484 return count;
1485}
ac3054c4 1486static DRIVER_ATTR_RW(debug_level);
b21a15f6
HMH
1487
1488/* version ------------------------------------------------------------- */
ac3054c4 1489static ssize_t version_show(struct device_driver *drv, char *buf)
b21a15f6 1490{
35ff8b9f
HMH
1491 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1492 TPACPI_DESC, TPACPI_VERSION);
b21a15f6 1493}
ac3054c4 1494static DRIVER_ATTR_RO(version);
b21a15f6
HMH
1495
1496/* --------------------------------------------------------------------- */
1497
a73f3091
HMH
1498#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1499
a73f3091 1500/* wlsw_emulstate ------------------------------------------------------ */
ac3054c4 1501static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
a73f3091
HMH
1502{
1503 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1504}
1505
ac3054c4
GKH
1506static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1507 size_t count)
a73f3091
HMH
1508{
1509 unsigned long t;
1510
1511 if (parse_strtoul(buf, 1, &t))
1512 return -EINVAL;
1513
19d337df 1514 if (tpacpi_wlsw_emulstate != !!t) {
a73f3091 1515 tpacpi_wlsw_emulstate = !!t;
19d337df
JB
1516 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1517 }
a73f3091
HMH
1518
1519 return count;
1520}
ac3054c4 1521static DRIVER_ATTR_RW(wlsw_emulstate);
a73f3091
HMH
1522
1523/* bluetooth_emulstate ------------------------------------------------- */
ac3054c4 1524static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
a73f3091
HMH
1525{
1526 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1527}
1528
ac3054c4
GKH
1529static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1530 const char *buf, size_t count)
a73f3091
HMH
1531{
1532 unsigned long t;
1533
1534 if (parse_strtoul(buf, 1, &t))
1535 return -EINVAL;
1536
1537 tpacpi_bluetooth_emulstate = !!t;
1538
1539 return count;
1540}
ac3054c4 1541static DRIVER_ATTR_RW(bluetooth_emulstate);
a73f3091
HMH
1542
1543/* wwan_emulstate ------------------------------------------------- */
ac3054c4 1544static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
a73f3091
HMH
1545{
1546 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1547}
1548
ac3054c4
GKH
1549static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1550 size_t count)
a73f3091
HMH
1551{
1552 unsigned long t;
1553
1554 if (parse_strtoul(buf, 1, &t))
1555 return -EINVAL;
1556
1557 tpacpi_wwan_emulstate = !!t;
1558
1559 return count;
1560}
ac3054c4 1561static DRIVER_ATTR_RW(wwan_emulstate);
a73f3091 1562
0045c0aa 1563/* uwb_emulstate ------------------------------------------------- */
ac3054c4 1564static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
0045c0aa
HMH
1565{
1566 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1567}
1568
ac3054c4
GKH
1569static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1570 size_t count)
0045c0aa
HMH
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}
ac3054c4 1581static DRIVER_ATTR_RW(uwb_emulstate);
a73f3091
HMH
1582#endif
1583
1584/* --------------------------------------------------------------------- */
1585
35ff8b9f 1586static struct driver_attribute *tpacpi_driver_attributes[] = {
b21a15f6
HMH
1587 &driver_attr_debug_level, &driver_attr_version,
1588 &driver_attr_interface_version,
1589};
1590
1591static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1592{
1593 int i, res;
1594
1595 i = 0;
1596 res = 0;
1597 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1598 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1599 i++;
1600 }
1601
a73f3091
HMH
1602#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1603 if (!res && dbg_wlswemul)
1604 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1605 if (!res && dbg_bluetoothemul)
1606 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1607 if (!res && dbg_wwanemul)
1608 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
0045c0aa
HMH
1609 if (!res && dbg_uwbemul)
1610 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
a73f3091
HMH
1611#endif
1612
b21a15f6
HMH
1613 return res;
1614}
1615
1616static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1617{
1618 int i;
1619
35ff8b9f 1620 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
b21a15f6 1621 driver_remove_file(drv, tpacpi_driver_attributes[i]);
a73f3091
HMH
1622
1623#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1624 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1625 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1626 driver_remove_file(drv, &driver_attr_wwan_emulstate);
0045c0aa 1627 driver_remove_file(drv, &driver_attr_uwb_emulstate);
a73f3091 1628#endif
b21a15f6
HMH
1629}
1630
600a99fa
HMH
1631/*************************************************************************
1632 * Firmware Data
1633 */
1634
1635/*
1636 * Table of recommended minimum BIOS versions
1637 *
1638 * Reasons for listing:
9ddc5b6f 1639 * 1. Stable BIOS, listed because the unknown amount of
600a99fa
HMH
1640 * bugs and bad ACPI behaviour on older versions
1641 *
1642 * 2. BIOS or EC fw with known bugs that trigger on Linux
1643 *
1644 * 3. BIOS with known reduced functionality in older versions
1645 *
1646 * We recommend the latest BIOS and EC version.
1647 * We only support the latest BIOS and EC fw version as a rule.
1648 *
1649 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1650 * Information from users in ThinkWiki
e675abaf
HMH
1651 *
1652 * WARNING: we use this table also to detect that the machine is
1653 * a ThinkPad in some cases, so don't remove entries lightly.
600a99fa
HMH
1654 */
1655
1656#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1657 { .vendor = (__v), \
1658 .bios = TPID(__id1, __id2), \
1659 .ec = TPACPI_MATCH_ANY, \
846a416b
JW
1660 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1661 | TPVER(__bv1, __bv2) }
600a99fa
HMH
1662
1663#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
275014ae 1664 __eid, __ev1, __ev2) \
600a99fa
HMH
1665 { .vendor = (__v), \
1666 .bios = TPID(__bid1, __bid2), \
275014ae 1667 .ec = __eid, \
846a416b
JW
1668 .quirks = TPVER(__ev1, __ev2) << 16 \
1669 | TPVER(__bv1, __bv2) }
600a99fa
HMH
1670
1671#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1672 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1673
275014ae 1674/* Outdated IBM BIOSes often lack the EC id string */
600a99fa
HMH
1675#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1676 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
275014ae
HMH
1677 __bv1, __bv2, TPID(__id1, __id2), \
1678 __ev1, __ev2), \
1679 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1680 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1681 __ev1, __ev2)
600a99fa 1682
275014ae 1683/* Outdated IBM BIOSes often lack the EC id string */
600a99fa
HMH
1684#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1685 __eid1, __eid2, __ev1, __ev2) \
1686 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
275014ae
HMH
1687 __bv1, __bv2, TPID(__eid1, __eid2), \
1688 __ev1, __ev2), \
1689 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1690 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1691 __ev1, __ev2)
600a99fa
HMH
1692
1693#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1694 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1695
1696#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1697 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
275014ae
HMH
1698 __bv1, __bv2, TPID(__id1, __id2), \
1699 __ev1, __ev2)
600a99fa
HMH
1700
1701#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1702 __eid1, __eid2, __ev1, __ev2) \
1703 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
275014ae
HMH
1704 __bv1, __bv2, TPID(__eid1, __eid2), \
1705 __ev1, __ev2)
600a99fa
HMH
1706
1707static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1708 /* Numeric models ------------------ */
1709 /* FW MODEL BIOS VERS */
1710 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1711 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1712 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1713 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1714 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1715 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1716 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1717 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1718 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1719
1720 /* A-series ------------------------- */
1721 /* FW MODEL BIOS VERS EC VERS */
1722 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1723 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1724 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1725 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1726 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1727 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1728 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1729 TPV_QI0('1', '3', '2', '0'), /* A22m */
1730 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1731 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1732 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1733
1734 /* G-series ------------------------- */
1735 /* FW MODEL BIOS VERS */
1736 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1737 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1738
1739 /* R-series, T-series --------------- */
1740 /* FW MODEL BIOS VERS EC VERS */
1741 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1742 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1743 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1744 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1745 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1746 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1747 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1748 T40/p, T41/p, T42/p (1) */
1749 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1750 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1751 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1752 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1753
1754 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1755 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1756 TPV_QI0('1', '6', '3', '2'), /* T22 */
1757 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1758 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1759 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1760
1761 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1762 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
90765c6a 1763 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
600a99fa
HMH
1764
1765 /* BIOS FW BIOS VERS EC FW EC VERS */
1766 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1767 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1768
1769 /* X-series ------------------------- */
1770 /* FW MODEL BIOS VERS EC VERS */
1771 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1772 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1773 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1774 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1775 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1776 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1777 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1778
90765c6a
HMH
1779 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1780 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
600a99fa
HMH
1781
1782 /* (0) - older versions lack DMI EC fw string and functionality */
1783 /* (1) - older versions known to lack functionality */
1784};
1785
1786#undef TPV_QL1
1787#undef TPV_QL0
1788#undef TPV_QI2
1789#undef TPV_QI1
1790#undef TPV_QI0
1791#undef TPV_Q_X
1792#undef TPV_Q
1793
1794static void __init tpacpi_check_outdated_fw(void)
1795{
1796 unsigned long fwvers;
1797 u16 ec_version, bios_version;
1798
1799 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1800 ARRAY_SIZE(tpacpi_bios_version_qtable));
1801
1802 if (!fwvers)
1803 return;
1804
1805 bios_version = fwvers & 0xffffU;
1806 ec_version = (fwvers >> 16) & 0xffffU;
1807
1808 /* note that unknown versions are set to 0x0000 and we use that */
1809 if ((bios_version > thinkpad_id.bios_release) ||
1810 (ec_version > thinkpad_id.ec_release &&
846a416b 1811 ec_version != TPACPI_MATCH_ANY_VERSION)) {
600a99fa
HMH
1812 /*
1813 * The changelogs would let us track down the exact
1814 * reason, but it is just too much of a pain to track
1815 * it. We only list BIOSes that are either really
1816 * broken, or really stable to begin with, so it is
1817 * best if the user upgrades the firmware anyway.
1818 */
0978e012 1819 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
5cac62ac 1820 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
600a99fa
HMH
1821 }
1822}
1823
e675abaf
HMH
1824static bool __init tpacpi_is_fw_known(void)
1825{
1826 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1827 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1828}
1829
b21a15f6
HMH
1830/****************************************************************************
1831 ****************************************************************************
1832 *
1833 * Subdrivers
1834 *
1835 ****************************************************************************
1836 ****************************************************************************/
1837
1838/*************************************************************************
7a43f788 1839 * thinkpad-acpi metadata subdriver
b21a15f6
HMH
1840 */
1841
887965e6 1842static int thinkpad_acpi_driver_read(struct seq_file *m)
1da177e4 1843{
887965e6
AD
1844 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1845 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1846 return 0;
1da177e4
LT
1847}
1848
a5763f22
HMH
1849static struct ibm_struct thinkpad_acpi_driver_data = {
1850 .name = "driver",
1851 .read = thinkpad_acpi_driver_read,
1852};
1853
56b6aeb0
HMH
1854/*************************************************************************
1855 * Hotkey subdriver
1856 */
1857
0d922e3b
HMH
1858/*
1859 * ThinkPad firmware event model
1860 *
1861 * The ThinkPad firmware has two main event interfaces: normal ACPI
1862 * notifications (which follow the ACPI standard), and a private event
1863 * interface.
1864 *
1865 * The private event interface also issues events for the hotkeys. As
1866 * the driver gained features, the event handling code ended up being
1867 * built around the hotkey subdriver. This will need to be refactored
1868 * to a more formal event API eventually.
1869 *
1870 * Some "hotkeys" are actually supposed to be used as event reports,
1871 * such as "brightness has changed", "volume has changed", depending on
1872 * the ThinkPad model and how the firmware is operating.
1873 *
1874 * Unlike other classes, hotkey-class events have mask/unmask control on
1875 * non-ancient firmware. However, how it behaves changes a lot with the
1876 * firmware model and version.
1877 */
1878
f74a27d4
HMH
1879enum { /* hot key scan codes (derived from ACPI DSDT) */
1880 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1881 TP_ACPI_HOTKEYSCAN_FNF2,
1882 TP_ACPI_HOTKEYSCAN_FNF3,
1883 TP_ACPI_HOTKEYSCAN_FNF4,
1884 TP_ACPI_HOTKEYSCAN_FNF5,
1885 TP_ACPI_HOTKEYSCAN_FNF6,
1886 TP_ACPI_HOTKEYSCAN_FNF7,
1887 TP_ACPI_HOTKEYSCAN_FNF8,
1888 TP_ACPI_HOTKEYSCAN_FNF9,
1889 TP_ACPI_HOTKEYSCAN_FNF10,
1890 TP_ACPI_HOTKEYSCAN_FNF11,
1891 TP_ACPI_HOTKEYSCAN_FNF12,
1892 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1893 TP_ACPI_HOTKEYSCAN_FNINSERT,
1894 TP_ACPI_HOTKEYSCAN_FNDELETE,
1895 TP_ACPI_HOTKEYSCAN_FNHOME,
1896 TP_ACPI_HOTKEYSCAN_FNEND,
1897 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1898 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1899 TP_ACPI_HOTKEYSCAN_FNSPACE,
1900 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1901 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1902 TP_ACPI_HOTKEYSCAN_MUTE,
1903 TP_ACPI_HOTKEYSCAN_THINKPAD,
34a656d2
HMH
1904 TP_ACPI_HOTKEYSCAN_UNK1,
1905 TP_ACPI_HOTKEYSCAN_UNK2,
1906 TP_ACPI_HOTKEYSCAN_UNK3,
1907 TP_ACPI_HOTKEYSCAN_UNK4,
1908 TP_ACPI_HOTKEYSCAN_UNK5,
1909 TP_ACPI_HOTKEYSCAN_UNK6,
1910 TP_ACPI_HOTKEYSCAN_UNK7,
1911 TP_ACPI_HOTKEYSCAN_UNK8,
1912
149c8c75
CK
1913 /* Adaptive keyboard keycodes */
1914 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1915 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
6a68d855
BN
1916 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1917 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1918 TP_ACPI_HOTKEYSCAN_CLOUD,
1919 TP_ACPI_HOTKEYSCAN_UNK9,
1920 TP_ACPI_HOTKEYSCAN_VOICE,
1921 TP_ACPI_HOTKEYSCAN_UNK10,
1922 TP_ACPI_HOTKEYSCAN_GESTURES,
1923 TP_ACPI_HOTKEYSCAN_UNK11,
1924 TP_ACPI_HOTKEYSCAN_UNK12,
1925 TP_ACPI_HOTKEYSCAN_UNK13,
1926 TP_ACPI_HOTKEYSCAN_CONFIG,
1927 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1928 TP_ACPI_HOTKEYSCAN_RELOAD,
1929 TP_ACPI_HOTKEYSCAN_BACK,
1930 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1931 TP_ACPI_HOTKEYSCAN_MIC_UP,
1932 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1933 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1934 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1935
696c6523
CK
1936 /* Lenovo extended keymap, starting at 0x1300 */
1937 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1938 /* first new observed key (star, favorites) is 0x1311 */
1939 TP_ACPI_HOTKEYSCAN_STAR = 69,
1940 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
cb5c1978 1941 TP_ACPI_HOTKEYSCAN_CALCULATOR,
696c6523
CK
1942 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1943 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1944
34a656d2
HMH
1945 /* Hotkey keymap size */
1946 TPACPI_HOTKEY_MAP_LEN
f74a27d4
HMH
1947};
1948
0d922e3b 1949enum { /* Keys/events available through NVRAM polling */
01e88f25
HMH
1950 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1951 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1952};
1953
1954enum { /* Positions of some of the keys in hotkey masks */
1955 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1956 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1957 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1958 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1959 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
c685e20d 1960 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
01e88f25
HMH
1961 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1962 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1963 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1964 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1965 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1966};
1967
1968enum { /* NVRAM to ACPI HKEY group map */
1969 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1970 TP_ACPI_HKEY_ZOOM_MASK |
1971 TP_ACPI_HKEY_DISPSWTCH_MASK |
1972 TP_ACPI_HKEY_HIBERNATE_MASK,
1973 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1974 TP_ACPI_HKEY_BRGHTDWN_MASK,
1975 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1976 TP_ACPI_HKEY_VOLDWN_MASK |
1977 TP_ACPI_HKEY_MUTE_MASK,
1978};
1979
1980#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1981struct tp_nvram_state {
1982 u16 thinkpad_toggle:1;
1983 u16 zoom_toggle:1;
1984 u16 display_toggle:1;
1985 u16 thinklight_toggle:1;
1986 u16 hibernate_toggle:1;
1987 u16 displayexp_toggle:1;
1988 u16 display_state:1;
1989 u16 brightness_toggle:1;
1990 u16 volume_toggle:1;
1991 u16 mute:1;
1992
1993 u8 brightness_level;
1994 u8 volume_level;
1995};
1996
db25f16d 1997/* kthread for the hotkey poller */
01e88f25 1998static struct task_struct *tpacpi_hotkey_task;
db25f16d 1999
db25f16d 2000/*
0d922e3b
HMH
2001 * Acquire mutex to write poller control variables as an
2002 * atomic block.
2003 *
2004 * Increment hotkey_config_change when changing them if you
2005 * want the kthread to forget old state.
db25f16d
HMH
2006 *
2007 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2008 */
01e88f25
HMH
2009static struct mutex hotkey_thread_data_mutex;
2010static unsigned int hotkey_config_change;
2011
db25f16d
HMH
2012/*
2013 * hotkey poller control variables
2014 *
2015 * Must be atomic or readers will also need to acquire mutex
0d922e3b
HMH
2016 *
2017 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2018 * should be used only when the changes need to be taken as
2019 * a block, OR when one needs to force the kthread to forget
2020 * old state.
db25f16d
HMH
2021 */
2022static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2023static unsigned int hotkey_poll_freq = 10; /* Hz */
2024
2025#define HOTKEY_CONFIG_CRITICAL_START \
2026 do { \
2027 mutex_lock(&hotkey_thread_data_mutex); \
2028 hotkey_config_change++; \
2029 } while (0);
2030#define HOTKEY_CONFIG_CRITICAL_END \
2031 mutex_unlock(&hotkey_thread_data_mutex);
2032
01e88f25
HMH
2033#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2034
2035#define hotkey_source_mask 0U
db25f16d
HMH
2036#define HOTKEY_CONFIG_CRITICAL_START
2037#define HOTKEY_CONFIG_CRITICAL_END
01e88f25
HMH
2038
2039#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2040
f74a27d4
HMH
2041static struct mutex hotkey_mutex;
2042
a713b4d7
HMH
2043static enum { /* Reasons for waking up */
2044 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2045 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2046 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2047} hotkey_wakeup_reason;
2048
2049static int hotkey_autosleep_ack;
2050
0d922e3b
HMH
2051static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2052static u32 hotkey_all_mask; /* all events supported in fw */
0118c2d3 2053static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
0d922e3b
HMH
2054static u32 hotkey_reserved_mask; /* events better left disabled */
2055static u32 hotkey_driver_mask; /* events needed by the driver */
2056static u32 hotkey_user_mask; /* events visible to userspace */
2057static u32 hotkey_acpi_mask; /* events enabled in firmware */
6a38abbf 2058
edf0e0e5 2059static u16 *hotkey_keycode_map;
78f81cc4 2060
94954cc6 2061static struct attribute_set *hotkey_dev_attributes;
a0416420 2062
8b468c0c
HMH
2063static void tpacpi_driver_event(const unsigned int hkey_event);
2064static void hotkey_driver_event(const unsigned int scancode);
7f0cf712 2065static void hotkey_poll_setup(const bool may_warn);
8b468c0c 2066
6c231bd5
HMH
2067/* HKEY.MHKG() return bits */
2068#define TP_HOTKEY_TABLET_MASK (1 << 3)
dda3ec0a
BB
2069enum {
2070 TP_ACPI_MULTI_MODE_INVALID = 0,
2071 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
2072 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
2073 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
2074 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
2075 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
2076 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
2077 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
2078};
2079
2080enum {
2081 /* The following modes are considered tablet mode for the purpose of
2082 * reporting the status to userspace. i.e. in all these modes it makes
2083 * sense to disable the laptop input devices such as touchpad and
2084 * keyboard.
2085 */
2086 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
2087 TP_ACPI_MULTI_MODE_STAND |
2088 TP_ACPI_MULTI_MODE_TENT |
2089 TP_ACPI_MULTI_MODE_STAND_TENT,
2090};
6c231bd5 2091
19d337df 2092static int hotkey_get_wlsw(void)
74941a69 2093{
19d337df
JB
2094 int status;
2095
2096 if (!tp_features.hotkey_wlsw)
2097 return -ENODEV;
2098
a73f3091 2099#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
19d337df
JB
2100 if (dbg_wlswemul)
2101 return (tpacpi_wlsw_emulstate) ?
2102 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
a73f3091 2103#endif
19d337df
JB
2104
2105 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
74941a69 2106 return -EIO;
19d337df
JB
2107
2108 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
74941a69
HMH
2109}
2110
dda3ec0a
BB
2111static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
2112{
2113 int type = (s >> 16) & 0xffff;
2114 int value = s & 0xffff;
2115 int mode = TP_ACPI_MULTI_MODE_INVALID;
2116 int valid_modes = 0;
2117
2118 if (has_tablet_mode)
2119 *has_tablet_mode = 0;
2120
2121 switch (type) {
2122 case 1:
2123 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2124 TP_ACPI_MULTI_MODE_TABLET |
2125 TP_ACPI_MULTI_MODE_STAND_TENT;
2126 break;
2127 case 2:
2128 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2129 TP_ACPI_MULTI_MODE_FLAT |
2130 TP_ACPI_MULTI_MODE_TABLET |
2131 TP_ACPI_MULTI_MODE_STAND |
2132 TP_ACPI_MULTI_MODE_TENT;
2133 break;
2134 case 3:
2135 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2136 TP_ACPI_MULTI_MODE_FLAT;
2137 break;
2138 case 4:
dda3ec0a 2139 case 5:
26befef0
BB
2140 /* In mode 4, FLAT is not specified as a valid mode. However,
2141 * it can be seen at least on the X1 Yoga 2nd Generation.
2142 */
dda3ec0a
BB
2143 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2144 TP_ACPI_MULTI_MODE_FLAT |
2145 TP_ACPI_MULTI_MODE_TABLET |
2146 TP_ACPI_MULTI_MODE_STAND |
2147 TP_ACPI_MULTI_MODE_TENT;
2148 break;
2149 default:
2150 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2151 type, value, TPACPI_MAIL);
2152 return 0;
2153 }
2154
2155 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2156 *has_tablet_mode = 1;
2157
2158 switch (value) {
2159 case 1:
2160 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2161 break;
2162 case 2:
2163 mode = TP_ACPI_MULTI_MODE_FLAT;
2164 break;
2165 case 3:
2166 mode = TP_ACPI_MULTI_MODE_TABLET;
2167 break;
2168 case 4:
2169 if (type == 1)
2170 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2171 else
2172 mode = TP_ACPI_MULTI_MODE_STAND;
2173 break;
2174 case 5:
2175 mode = TP_ACPI_MULTI_MODE_TENT;
2176 break;
2177 default:
2178 if (type == 5 && value == 0xffff) {
2179 pr_warn("Multi mode status is undetected, assuming laptop\n");
2180 return 0;
2181 }
2182 }
2183
2184 if (!(mode & valid_modes)) {
2185 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2186 value, type, TPACPI_MAIL);
2187 return 0;
2188 }
2189
2190 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2191}
2192
6c231bd5
HMH
2193static int hotkey_get_tablet_mode(int *status)
2194{
2195 int s;
2196
b03f4d49
L
2197 switch (tp_features.hotkey_tablet) {
2198 case TP_HOTKEY_TABLET_USES_MHKG:
2199 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2200 return -EIO;
2201
2202 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2203 break;
dda3ec0a
BB
2204 case TP_HOTKEY_TABLET_USES_GMMS:
2205 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
b03f4d49
L
2206 return -EIO;
2207
dda3ec0a 2208 *status = hotkey_gmms_get_tablet_mode(s, NULL);
b03f4d49
L
2209 break;
2210 default:
2211 break;
2212 }
6c231bd5 2213
cee47f5a 2214 return 0;
6c231bd5
HMH
2215}
2216
b2c985e7 2217/*
0d922e3b
HMH
2218 * Reads current event mask from firmware, and updates
2219 * hotkey_acpi_mask accordingly. Also resets any bits
2220 * from hotkey_user_mask that are unavailable to be
2221 * delivered (shadow requirement of the userspace ABI).
2222 *
b2c985e7
HMH
2223 * Call with hotkey_mutex held
2224 */
2225static int hotkey_mask_get(void)
2226{
2227 if (tp_features.hotkey_mask) {
0d922e3b
HMH
2228 u32 m = 0;
2229
01e88f25 2230 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
b2c985e7 2231 return -EIO;
0d922e3b
HMH
2232
2233 hotkey_acpi_mask = m;
2234 } else {
2235 /* no mask support doesn't mean no event support... */
2236 hotkey_acpi_mask = hotkey_all_mask;
b2c985e7 2237 }
0d922e3b
HMH
2238
2239 /* sync userspace-visible mask */
2240 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
b2c985e7
HMH
2241
2242 return 0;
2243}
2244
df6dd1b3 2245static void hotkey_mask_warn_incomplete_mask(void)
0d922e3b
HMH
2246{
2247 /* log only what the user can fix... */
2248 const u32 wantedmask = hotkey_driver_mask &
2249 ~(hotkey_acpi_mask | hotkey_source_mask) &
2250 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2251
2252 if (wantedmask)
0978e012 2253 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
0d922e3b
HMH
2254}
2255
b2c985e7 2256/*
0d922e3b
HMH
2257 * Set the firmware mask when supported
2258 *
2259 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2260 *
2261 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2262 *
b2c985e7
HMH
2263 * Call with hotkey_mutex held
2264 */
2265static int hotkey_mask_set(u32 mask)
2266{
2267 int i;
2268 int rc = 0;
2269
0d922e3b 2270 const u32 fwmask = mask & ~hotkey_source_mask;
92889022 2271
0d922e3b 2272 if (tp_features.hotkey_mask) {
b2c985e7 2273 for (i = 0; i < 32; i++) {
b2c985e7
HMH
2274 if (!acpi_evalf(hkey_handle,
2275 NULL, "MHKM", "vdd", i + 1,
0d922e3b 2276 !!(mask & (1 << i)))) {
b2c985e7
HMH
2277 rc = -EIO;
2278 break;
b2c985e7
HMH
2279 }
2280 }
0d922e3b 2281 }
b2c985e7 2282
0d922e3b
HMH
2283 /*
2284 * We *must* make an inconditional call to hotkey_mask_get to
2285 * refresh hotkey_acpi_mask and update hotkey_user_mask
2286 *
2287 * Take the opportunity to also log when we cannot _enable_
2288 * a given event.
2289 */
2290 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
5cac62ac 2291 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
0978e012 2292 fwmask, hotkey_acpi_mask);
b2c985e7
HMH
2293 }
2294
6b30eb7d
HMH
2295 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2296 hotkey_mask_warn_incomplete_mask();
0d922e3b
HMH
2297
2298 return rc;
2299}
2300
2301/*
2302 * Sets hotkey_user_mask and tries to set the firmware mask
2303 *
2304 * Call with hotkey_mutex held
2305 */
2306static int hotkey_user_mask_set(const u32 mask)
2307{
2308 int rc;
2309
2310 /* Give people a chance to notice they are doing something that
2311 * is bound to go boom on their users sooner or later */
2312 if (!tp_warned.hotkey_mask_ff &&
2313 (mask == 0xffff || mask == 0xffffff ||
2314 mask == 0xffffffff)) {
2315 tp_warned.hotkey_mask_ff = 1;
5cac62ac
AS
2316 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2317 mask);
2318 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
0d922e3b
HMH
2319 }
2320
2321 /* Try to enable what the user asked for, plus whatever we need.
2322 * this syncs everything but won't enable bits in hotkey_user_mask */
2323 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2324
2325 /* Enable the available bits in hotkey_user_mask */
2326 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2327
b2c985e7
HMH
2328 return rc;
2329}
2330
8b468c0c
HMH
2331/*
2332 * Sets the driver hotkey mask.
2333 *
2334 * Can be called even if the hotkey subdriver is inactive
2335 */
2336static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2337{
2338 int rc;
2339
2340 /* Do the right thing if hotkey_init has not been called yet */
2341 if (!tp_features.hotkey) {
2342 hotkey_driver_mask = mask;
2343 return 0;
2344 }
2345
2346 mutex_lock(&hotkey_mutex);
2347
2348 HOTKEY_CONFIG_CRITICAL_START
2349 hotkey_driver_mask = mask;
b684a363 2350#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
8b468c0c 2351 hotkey_source_mask |= (mask & ~hotkey_all_mask);
b684a363 2352#endif
8b468c0c
HMH
2353 HOTKEY_CONFIG_CRITICAL_END
2354
2355 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2356 ~hotkey_source_mask);
7f0cf712
HMH
2357 hotkey_poll_setup(true);
2358
8b468c0c
HMH
2359 mutex_unlock(&hotkey_mutex);
2360
2361 return rc;
2362}
2363
b2c985e7
HMH
2364static int hotkey_status_get(int *status)
2365{
2366 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2367 return -EIO;
2368
2369 return 0;
2370}
2371
2586d566 2372static int hotkey_status_set(bool enable)
b2c985e7 2373{
2586d566 2374 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
b2c985e7
HMH
2375 return -EIO;
2376
2377 return 0;
2378}
2379
6c231bd5 2380static void tpacpi_input_send_tabletsw(void)
b3ec6f91 2381{
6c231bd5 2382 int state;
b3ec6f91 2383
6c231bd5
HMH
2384 if (tp_features.hotkey_tablet &&
2385 !hotkey_get_tablet_mode(&state)) {
2386 mutex_lock(&tpacpi_inputdev_send_mutex);
b3ec6f91 2387
6c231bd5
HMH
2388 input_report_switch(tpacpi_inputdev,
2389 SW_TABLET_MODE, !!state);
2390 input_sync(tpacpi_inputdev);
2391
2392 mutex_unlock(&tpacpi_inputdev_send_mutex);
2393 }
b3ec6f91
HMH
2394}
2395
0d922e3b
HMH
2396/* Do NOT call without validating scancode first */
2397static void tpacpi_input_send_key(const unsigned int scancode)
b7c8c200 2398{
0d922e3b 2399 const unsigned int keycode = hotkey_keycode_map[scancode];
b7c8c200
HMH
2400
2401 if (keycode != KEY_RESERVED) {
2402 mutex_lock(&tpacpi_inputdev_send_mutex);
2403
5ffba7e6 2404 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
b7c8c200 2405 input_report_key(tpacpi_inputdev, keycode, 1);
b7c8c200
HMH
2406 input_sync(tpacpi_inputdev);
2407
5ffba7e6 2408 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
b7c8c200 2409 input_report_key(tpacpi_inputdev, keycode, 0);
b7c8c200
HMH
2410 input_sync(tpacpi_inputdev);
2411
2412 mutex_unlock(&tpacpi_inputdev_send_mutex);
2413 }
2414}
2415
0d922e3b
HMH
2416/* Do NOT call without validating scancode first */
2417static void tpacpi_input_send_key_masked(const unsigned int scancode)
2418{
8b468c0c 2419 hotkey_driver_event(scancode);
0d922e3b
HMH
2420 if (hotkey_user_mask & (1 << scancode))
2421 tpacpi_input_send_key(scancode);
2422}
2423
01e88f25
HMH
2424#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2425static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2426
0d922e3b 2427/* Do NOT call without validating scancode first */
01e88f25
HMH
2428static void tpacpi_hotkey_send_key(unsigned int scancode)
2429{
0d922e3b 2430 tpacpi_input_send_key_masked(scancode);
01e88f25
HMH
2431}
2432
0d922e3b 2433static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
01e88f25
HMH
2434{
2435 u8 d;
2436
2437 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2438 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2439 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2440 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2441 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2442 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2443 }
c685e20d 2444 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
01e88f25
HMH
2445 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2446 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2447 }
2448 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2449 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2450 n->displayexp_toggle =
2451 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2452 }
2453 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2454 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2455 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2456 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2457 n->brightness_toggle =
2458 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2459 }
2460 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2461 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2462 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2463 >> TP_NVRAM_POS_LEVEL_VOLUME;
2464 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2465 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2466 }
2467}
2468
2469#define TPACPI_COMPARE_KEY(__scancode, __member) \
a4d44ba1
BW
2470do { \
2471 if ((event_mask & (1 << __scancode)) && \
2472 oldn->__member != newn->__member) \
2473 tpacpi_hotkey_send_key(__scancode); \
2474} while (0)
01e88f25
HMH
2475
2476#define TPACPI_MAY_SEND_KEY(__scancode) \
a4d44ba1
BW
2477do { \
2478 if (event_mask & (1 << __scancode)) \
2479 tpacpi_hotkey_send_key(__scancode); \
2480} while (0)
2481
2482static void issue_volchange(const unsigned int oldvol,
2483 const unsigned int newvol,
2484 const u32 event_mask)
2485{
2486 unsigned int i = oldvol;
2487
2488 while (i > newvol) {
2489 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2490 i--;
5d756db9 2491 }
a4d44ba1
BW
2492 while (i < newvol) {
2493 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2494 i++;
2495 }
2496}
5d756db9 2497
a4d44ba1
BW
2498static void issue_brightnesschange(const unsigned int oldbrt,
2499 const unsigned int newbrt,
2500 const u32 event_mask)
2501{
2502 unsigned int i = oldbrt;
28999022 2503
a4d44ba1
BW
2504 while (i > newbrt) {
2505 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2506 i--;
28999022 2507 }
a4d44ba1
BW
2508 while (i < newbrt) {
2509 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2510 i++;
2511 }
2512}
2513
2514static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2515 struct tp_nvram_state *newn,
2516 const u32 event_mask)
2517{
28999022 2518
01e88f25
HMH
2519 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2520 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2521 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2522 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2523
2524 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2525
2526 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2527
5d756db9
HMH
2528 /*
2529 * Handle volume
2530 *
2531 * This code is supposed to duplicate the IBM firmware behaviour:
2532 * - Pressing MUTE issues mute hotkey message, even when already mute
2533 * - Pressing Volume up/down issues volume up/down hotkey messages,
c8440336 2534 * even when already at maximum or minimum volume
5d756db9
HMH
2535 * - The act of unmuting issues volume up/down notification,
2536 * depending which key was used to unmute
2537 *
2538 * We are constrained to what the NVRAM can tell us, which is not much
2539 * and certainly not enough if more than one volume hotkey was pressed
2540 * since the last poll cycle.
2541 *
2542 * Just to make our life interesting, some newer Lenovo ThinkPads have
2543 * bugs in the BIOS and may fail to update volume_toggle properly.
2544 */
2545 if (newn->mute) {
2546 /* muted */
2547 if (!oldn->mute ||
2548 oldn->volume_toggle != newn->volume_toggle ||
2549 oldn->volume_level != newn->volume_level) {
2550 /* recently muted, or repeated mute keypress, or
2551 * multiple presses ending in mute */
a4d44ba1
BW
2552 issue_volchange(oldn->volume_level, newn->volume_level,
2553 event_mask);
01e88f25
HMH
2554 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2555 }
5d756db9
HMH
2556 } else {
2557 /* unmute */
2558 if (oldn->mute) {
2559 /* recently unmuted, issue 'unmute' keypress */
01e88f25 2560 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
5d756db9
HMH
2561 }
2562 if (oldn->volume_level != newn->volume_level) {
a4d44ba1
BW
2563 issue_volchange(oldn->volume_level, newn->volume_level,
2564 event_mask);
5d756db9
HMH
2565 } else if (oldn->volume_toggle != newn->volume_toggle) {
2566 /* repeated vol up/down keypress at end of scale ? */
2567 if (newn->volume_level == 0)
01e88f25 2568 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
5d756db9
HMH
2569 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2570 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
01e88f25
HMH
2571 }
2572 }
2573
2574 /* handle brightness */
28999022
HMH
2575 if (oldn->brightness_level != newn->brightness_level) {
2576 issue_brightnesschange(oldn->brightness_level,
a4d44ba1 2577 newn->brightness_level, event_mask);
28999022
HMH
2578 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2579 /* repeated key presses that didn't change state */
2580 if (newn->brightness_level == 0)
01e88f25 2581 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
28999022
HMH
2582 else if (newn->brightness_level >= bright_maxlvl
2583 && !tp_features.bright_unkfw)
2584 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
01e88f25 2585 }
01e88f25
HMH
2586
2587#undef TPACPI_COMPARE_KEY
2588#undef TPACPI_MAY_SEND_KEY
0d922e3b 2589}
01e88f25 2590
0d922e3b
HMH
2591/*
2592 * Polling driver
2593 *
2594 * We track all events in hotkey_source_mask all the time, since
2595 * most of them are edge-based. We only issue those requested by
2596 * hotkey_user_mask or hotkey_driver_mask, though.
2597 */
01e88f25
HMH
2598static int hotkey_kthread(void *data)
2599{
2600 struct tp_nvram_state s[2];
0d922e3b 2601 u32 poll_mask, event_mask;
01e88f25
HMH
2602 unsigned int si, so;
2603 unsigned long t;
8a32c441 2604 unsigned int change_detector;
db25f16d 2605 unsigned int poll_freq;
8a32c441 2606 bool was_frozen;
01e88f25 2607
01e88f25
HMH
2608 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2609 goto exit;
2610
2611 set_freezable();
2612
2613 so = 0;
2614 si = 1;
2615 t = 0;
2616
2617 /* Initial state for compares */
2618 mutex_lock(&hotkey_thread_data_mutex);
2619 change_detector = hotkey_config_change;
0d922e3b
HMH
2620 poll_mask = hotkey_source_mask;
2621 event_mask = hotkey_source_mask &
2622 (hotkey_driver_mask | hotkey_user_mask);
db25f16d 2623 poll_freq = hotkey_poll_freq;
01e88f25 2624 mutex_unlock(&hotkey_thread_data_mutex);
0d922e3b 2625 hotkey_read_nvram(&s[so], poll_mask);
01e88f25 2626
db25f16d
HMH
2627 while (!kthread_should_stop()) {
2628 if (t == 0) {
2629 if (likely(poll_freq))
2630 t = 1000/poll_freq;
2631 else
2632 t = 100; /* should never happen... */
2633 }
01e88f25 2634 t = msleep_interruptible(t);
8a32c441 2635 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
01e88f25 2636 break;
8a32c441
TH
2637
2638 if (t > 0 && !was_frozen)
01e88f25
HMH
2639 continue;
2640
2641 mutex_lock(&hotkey_thread_data_mutex);
8a32c441 2642 if (was_frozen || hotkey_config_change != change_detector) {
01e88f25
HMH
2643 /* forget old state on thaw or config change */
2644 si = so;
2645 t = 0;
2646 change_detector = hotkey_config_change;
2647 }
0d922e3b
HMH
2648 poll_mask = hotkey_source_mask;
2649 event_mask = hotkey_source_mask &
2650 (hotkey_driver_mask | hotkey_user_mask);
db25f16d 2651 poll_freq = hotkey_poll_freq;
01e88f25
HMH
2652 mutex_unlock(&hotkey_thread_data_mutex);
2653
0d922e3b
HMH
2654 if (likely(poll_mask)) {
2655 hotkey_read_nvram(&s[si], poll_mask);
01e88f25
HMH
2656 if (likely(si != so)) {
2657 hotkey_compare_and_issue_event(&s[so], &s[si],
0d922e3b 2658 event_mask);
01e88f25
HMH
2659 }
2660 }
2661
2662 so = si;
2663 si ^= 1;
2664 }
2665
2666exit:
01e88f25
HMH
2667 return 0;
2668}
2669
db25f16d 2670/* call with hotkey_mutex held */
01e88f25
HMH
2671static void hotkey_poll_stop_sync(void)
2672{
2673 if (tpacpi_hotkey_task) {
01e88f25
HMH
2674 kthread_stop(tpacpi_hotkey_task);
2675 tpacpi_hotkey_task = NULL;
01e88f25
HMH
2676 }
2677}
2678
2679/* call with hotkey_mutex held */
7f0cf712 2680static void hotkey_poll_setup(const bool may_warn)
01e88f25 2681{
0d922e3b
HMH
2682 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2683 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
db25f16d 2684
0d922e3b
HMH
2685 if (hotkey_poll_freq > 0 &&
2686 (poll_driver_mask ||
2687 (poll_user_mask && tpacpi_inputdev->users > 0))) {
01e88f25
HMH
2688 if (!tpacpi_hotkey_task) {
2689 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
95e57ab2 2690 NULL, TPACPI_NVRAM_KTHREAD_NAME);
01e88f25
HMH
2691 if (IS_ERR(tpacpi_hotkey_task)) {
2692 tpacpi_hotkey_task = NULL;
5cac62ac 2693 pr_err("could not create kernel thread for hotkey polling\n");
01e88f25
HMH
2694 }
2695 }
2696 } else {
2697 hotkey_poll_stop_sync();
0d922e3b 2698 if (may_warn && (poll_driver_mask || poll_user_mask) &&
db25f16d 2699 hotkey_poll_freq == 0) {
5cac62ac 2700 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
0978e012 2701 poll_user_mask, poll_driver_mask);
01e88f25
HMH
2702 }
2703 }
2704}
2705
7f0cf712 2706static void hotkey_poll_setup_safe(const bool may_warn)
01e88f25
HMH
2707{
2708 mutex_lock(&hotkey_mutex);
2709 hotkey_poll_setup(may_warn);
2710 mutex_unlock(&hotkey_mutex);
2711}
2712
db25f16d
HMH
2713/* call with hotkey_mutex held */
2714static void hotkey_poll_set_freq(unsigned int freq)
2715{
2716 if (!freq)
2717 hotkey_poll_stop_sync();
2718
db25f16d 2719 hotkey_poll_freq = freq;
db25f16d
HMH
2720}
2721
1bc6b9cd
HMH
2722#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2723
7f0cf712
HMH
2724static void hotkey_poll_setup(const bool __unused)
2725{
2726}
2727
2728static void hotkey_poll_setup_safe(const bool __unused)
1bc6b9cd
HMH
2729{
2730}
2731
2732#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2733
01e88f25
HMH
2734static int hotkey_inputdev_open(struct input_dev *dev)
2735{
2736 switch (tpacpi_lifecycle) {
2737 case TPACPI_LIFE_INIT:
01e88f25 2738 case TPACPI_LIFE_RUNNING:
db25f16d 2739 hotkey_poll_setup_safe(false);
01e88f25 2740 return 0;
b589ea4c
HMH
2741 case TPACPI_LIFE_EXITING:
2742 return -EBUSY;
01e88f25
HMH
2743 }
2744
2745 /* Should only happen if tpacpi_lifecycle is corrupt */
2746 BUG();
2747 return -EBUSY;
2748}
2749
2750static void hotkey_inputdev_close(struct input_dev *dev)
2751{
2752 /* disable hotkey polling when possible */
b589ea4c 2753 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
0d922e3b 2754 !(hotkey_source_mask & hotkey_driver_mask))
db25f16d 2755 hotkey_poll_setup_safe(false);
01e88f25 2756}
01e88f25 2757
a0416420
HMH
2758/* sysfs hotkey enable ------------------------------------------------- */
2759static ssize_t hotkey_enable_show(struct device *dev,
2760 struct device_attribute *attr,
2761 char *buf)
2762{
ae92bd17 2763 int res, status;
a0416420 2764
2586d566
HMH
2765 printk_deprecated_attribute("hotkey_enable",
2766 "Hotkey reporting is always enabled");
2767
b2c985e7 2768 res = hotkey_status_get(&status);
a0416420
HMH
2769 if (res)
2770 return res;
2771
2772 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2773}
2774
2775static ssize_t hotkey_enable_store(struct device *dev,
2776 struct device_attribute *attr,
2777 const char *buf, size_t count)
2778{
2779 unsigned long t;
2586d566
HMH
2780
2781 printk_deprecated_attribute("hotkey_enable",
2782 "Hotkeys can be disabled through hotkey_mask");
a0416420
HMH
2783
2784 if (parse_strtoul(buf, 1, &t))
2785 return -EINVAL;
2786
2586d566
HMH
2787 if (t == 0)
2788 return -EPERM;
a0416420 2789
2586d566 2790 return count;
a0416420
HMH
2791}
2792
b4dd04ac 2793static DEVICE_ATTR_RW(hotkey_enable);
a0416420
HMH
2794
2795/* sysfs hotkey mask --------------------------------------------------- */
2796static ssize_t hotkey_mask_show(struct device *dev,
2797 struct device_attribute *attr,
2798 char *buf)
2799{
0d922e3b 2800 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
a0416420
HMH
2801}
2802
2803static ssize_t hotkey_mask_store(struct device *dev,
2804 struct device_attribute *attr,
2805 const char *buf, size_t count)
2806{
2807 unsigned long t;
b2c985e7 2808 int res;
a0416420 2809
ae92bd17 2810 if (parse_strtoul(buf, 0xffffffffUL, &t))
a0416420
HMH
2811 return -EINVAL;
2812
7646ea88 2813 if (mutex_lock_killable(&hotkey_mutex))
b2c985e7
HMH
2814 return -ERESTARTSYS;
2815
0d922e3b 2816 res = hotkey_user_mask_set(t);
01e88f25
HMH
2817
2818#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
db25f16d 2819 hotkey_poll_setup(true);
01e88f25
HMH
2820#endif
2821
b2c985e7 2822 mutex_unlock(&hotkey_mutex);
a0416420 2823
56e2c200
HMH
2824 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2825
a0416420
HMH
2826 return (res) ? res : count;
2827}
2828
b4dd04ac 2829static DEVICE_ATTR_RW(hotkey_mask);
a0416420
HMH
2830
2831/* sysfs hotkey bios_enabled ------------------------------------------- */
2832static ssize_t hotkey_bios_enabled_show(struct device *dev,
2833 struct device_attribute *attr,
2834 char *buf)
2835{
2586d566 2836 return sprintf(buf, "0\n");
a0416420
HMH
2837}
2838
b4dd04ac 2839static DEVICE_ATTR_RO(hotkey_bios_enabled);
a0416420
HMH
2840
2841/* sysfs hotkey bios_mask ---------------------------------------------- */
2842static ssize_t hotkey_bios_mask_show(struct device *dev,
2843 struct device_attribute *attr,
2844 char *buf)
2845{
06777be6
HMH
2846 printk_deprecated_attribute("hotkey_bios_mask",
2847 "This attribute is useless.");
ae92bd17 2848 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
a0416420
HMH
2849}
2850
b4dd04ac 2851static DEVICE_ATTR_RO(hotkey_bios_mask);
a0416420 2852
9b010de5
HMH
2853/* sysfs hotkey all_mask ----------------------------------------------- */
2854static ssize_t hotkey_all_mask_show(struct device *dev,
2855 struct device_attribute *attr,
2856 char *buf)
2857{
01e88f25
HMH
2858 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2859 hotkey_all_mask | hotkey_source_mask);
9b010de5
HMH
2860}
2861
b4dd04ac 2862static DEVICE_ATTR_RO(hotkey_all_mask);
9b010de5 2863
0118c2d3
DW
2864/* sysfs hotkey all_mask ----------------------------------------------- */
2865static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2866 struct device_attribute *attr,
2867 char *buf)
2868{
2869 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2870 hotkey_adaptive_all_mask | hotkey_source_mask);
2871}
2872
2873static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2874
9b010de5
HMH
2875/* sysfs hotkey recommended_mask --------------------------------------- */
2876static ssize_t hotkey_recommended_mask_show(struct device *dev,
2877 struct device_attribute *attr,
2878 char *buf)
2879{
2880 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
01e88f25
HMH
2881 (hotkey_all_mask | hotkey_source_mask)
2882 & ~hotkey_reserved_mask);
9b010de5
HMH
2883}
2884
b4dd04ac 2885static DEVICE_ATTR_RO(hotkey_recommended_mask);
9b010de5 2886
01e88f25
HMH
2887#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2888
2889/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2890static ssize_t hotkey_source_mask_show(struct device *dev,
2891 struct device_attribute *attr,
2892 char *buf)
2893{
2894 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2895}
2896
2897static ssize_t hotkey_source_mask_store(struct device *dev,
2898 struct device_attribute *attr,
2899 const char *buf, size_t count)
2900{
2901 unsigned long t;
0d922e3b
HMH
2902 u32 r_ev;
2903 int rc;
01e88f25
HMH
2904
2905 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2906 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2907 return -EINVAL;
2908
7646ea88 2909 if (mutex_lock_killable(&hotkey_mutex))
01e88f25
HMH
2910 return -ERESTARTSYS;
2911
2912 HOTKEY_CONFIG_CRITICAL_START
2913 hotkey_source_mask = t;
2914 HOTKEY_CONFIG_CRITICAL_END
2915
0d922e3b
HMH
2916 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2917 ~hotkey_source_mask);
db25f16d 2918 hotkey_poll_setup(true);
0d922e3b
HMH
2919
2920 /* check if events needed by the driver got disabled */
2921 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2922 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
01e88f25
HMH
2923
2924 mutex_unlock(&hotkey_mutex);
2925
0d922e3b 2926 if (rc < 0)
5cac62ac 2927 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
0d922e3b
HMH
2928
2929 if (r_ev)
5cac62ac 2930 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
0978e012 2931 r_ev);
0d922e3b 2932
56e2c200
HMH
2933 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2934
0d922e3b 2935 return (rc < 0) ? rc : count;
01e88f25
HMH
2936}
2937
b4dd04ac 2938static DEVICE_ATTR_RW(hotkey_source_mask);
01e88f25
HMH
2939
2940/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2941static ssize_t hotkey_poll_freq_show(struct device *dev,
2942 struct device_attribute *attr,
2943 char *buf)
2944{
2945 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2946}
2947
2948static ssize_t hotkey_poll_freq_store(struct device *dev,
2949 struct device_attribute *attr,
2950 const char *buf, size_t count)
2951{
2952 unsigned long t;
2953
2954 if (parse_strtoul(buf, 25, &t))
2955 return -EINVAL;
2956
7646ea88 2957 if (mutex_lock_killable(&hotkey_mutex))
01e88f25
HMH
2958 return -ERESTARTSYS;
2959
db25f16d
HMH
2960 hotkey_poll_set_freq(t);
2961 hotkey_poll_setup(true);
01e88f25 2962
01e88f25
HMH
2963 mutex_unlock(&hotkey_mutex);
2964
56e2c200
HMH
2965 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2966
01e88f25
HMH
2967 return count;
2968}
2969
b4dd04ac 2970static DEVICE_ATTR_RW(hotkey_poll_freq);
01e88f25
HMH
2971
2972#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2973
50ebec09 2974/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
74941a69
HMH
2975static ssize_t hotkey_radio_sw_show(struct device *dev,
2976 struct device_attribute *attr,
2977 char *buf)
2978{
19d337df
JB
2979 int res;
2980 res = hotkey_get_wlsw();
74941a69
HMH
2981 if (res < 0)
2982 return res;
2983
19d337df
JB
2984 /* Opportunistic update */
2985 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2986
2987 return snprintf(buf, PAGE_SIZE, "%d\n",
2988 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
74941a69
HMH
2989}
2990
b4dd04ac 2991static DEVICE_ATTR_RO(hotkey_radio_sw);
74941a69 2992
50ebec09
HMH
2993static void hotkey_radio_sw_notify_change(void)
2994{
2995 if (tp_features.hotkey_wlsw)
2996 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2997 "hotkey_radio_sw");
2998}
2999
6c231bd5
HMH
3000/* sysfs hotkey tablet mode (pollable) --------------------------------- */
3001static ssize_t hotkey_tablet_mode_show(struct device *dev,
3002 struct device_attribute *attr,
3003 char *buf)
3004{
3005 int res, s;
3006 res = hotkey_get_tablet_mode(&s);
3007 if (res < 0)
3008 return res;
3009
3010 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
3011}
3012
b4dd04ac 3013static DEVICE_ATTR_RO(hotkey_tablet_mode);
6c231bd5
HMH
3014
3015static void hotkey_tablet_mode_notify_change(void)
3016{
3017 if (tp_features.hotkey_tablet)
3018 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3019 "hotkey_tablet_mode");
3020}
3021
50ebec09 3022/* sysfs wakeup reason (pollable) -------------------------------------- */
a713b4d7
HMH
3023static ssize_t hotkey_wakeup_reason_show(struct device *dev,
3024 struct device_attribute *attr,
3025 char *buf)
3026{
3027 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
3028}
3029
5fb73bc2 3030static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
a713b4d7 3031
1d5a2b54 3032static void hotkey_wakeup_reason_notify_change(void)
50ebec09 3033{
0d922e3b
HMH
3034 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3035 "wakeup_reason");
50ebec09
HMH
3036}
3037
3038/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
a713b4d7
HMH
3039static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
3040 struct device_attribute *attr,
3041 char *buf)
3042{
3043 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
3044}
3045
5fb73bc2
BM
3046static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
3047 hotkey_wakeup_hotunplug_complete_show, NULL);
a713b4d7 3048
1d5a2b54 3049static void hotkey_wakeup_hotunplug_complete_notify_change(void)
50ebec09 3050{
0d922e3b
HMH
3051 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3052 "wakeup_hotunplug_complete");
50ebec09
HMH
3053}
3054
b790ceeb
BN
3055/* sysfs adaptive kbd mode --------------------------------------------- */
3056
3057static int adaptive_keyboard_get_mode(void);
3058static int adaptive_keyboard_set_mode(int new_mode);
3059
3060enum ADAPTIVE_KEY_MODE {
3061 HOME_MODE,
3062 WEB_BROWSER_MODE,
3063 WEB_CONFERENCE_MODE,
3064 FUNCTION_MODE,
3065 LAYFLAT_MODE
3066};
3067
3068static ssize_t adaptive_kbd_mode_show(struct device *dev,
3069 struct device_attribute *attr,
3070 char *buf)
3071{
abf9dc0d 3072 int current_mode;
b790ceeb
BN
3073
3074 current_mode = adaptive_keyboard_get_mode();
3075 if (current_mode < 0)
3076 return current_mode;
3077
3078 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
3079}
3080
3081static ssize_t adaptive_kbd_mode_store(struct device *dev,
3082 struct device_attribute *attr,
3083 const char *buf, size_t count)
3084{
3085 unsigned long t;
3086 int res;
3087
3088 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
3089 return -EINVAL;
3090
3091 res = adaptive_keyboard_set_mode(t);
3092 return (res < 0) ? res : count;
3093}
3094
3095static DEVICE_ATTR_RW(adaptive_kbd_mode);
3096
3097static struct attribute *adaptive_kbd_attributes[] = {
3098 &dev_attr_adaptive_kbd_mode.attr,
3099 NULL
3100};
3101
3102static const struct attribute_group adaptive_kbd_attr_group = {
3103 .attrs = adaptive_kbd_attributes,
3104};
3105
a0416420
HMH
3106/* --------------------------------------------------------------------- */
3107
ff80f137
HMH
3108static struct attribute *hotkey_attributes[] __initdata = {
3109 &dev_attr_hotkey_enable.attr,
01e88f25 3110 &dev_attr_hotkey_bios_enabled.attr,
0d922e3b 3111 &dev_attr_hotkey_bios_mask.attr,
5fb73bc2
BM
3112 &dev_attr_wakeup_reason.attr,
3113 &dev_attr_wakeup_hotunplug_complete.attr,
01e88f25
HMH
3114 &dev_attr_hotkey_mask.attr,
3115 &dev_attr_hotkey_all_mask.attr,
0118c2d3 3116 &dev_attr_hotkey_adaptive_all_mask.attr,
01e88f25 3117 &dev_attr_hotkey_recommended_mask.attr,
0d922e3b 3118#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
01e88f25
HMH
3119 &dev_attr_hotkey_source_mask.attr,
3120 &dev_attr_hotkey_poll_freq.attr,
3121#endif
ff80f137
HMH
3122};
3123
19d337df
JB
3124/*
3125 * Sync both the hw and sw blocking state of all switches
3126 */
733e27c1
HMH
3127static void tpacpi_send_radiosw_update(void)
3128{
3129 int wlsw;
3130
19d337df
JB
3131 /*
3132 * We must sync all rfkill controllers *before* issuing any
3133 * rfkill input events, or we will race the rfkill core input
3134 * handler.
3135 *
c8440336 3136 * tpacpi_inputdev_send_mutex works as a synchronization point
19d337df
JB
3137 * for the above.
3138 *
3139 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3140 */
3141
3142 wlsw = hotkey_get_wlsw();
3143
3144 /* Sync hw blocking state first if it is hw-blocked */
3145 if (wlsw == TPACPI_RFK_RADIO_OFF)
3146 tpacpi_rfk_update_hwblock_state(true);
0e74dc26 3147
19d337df
JB
3148 /* Sync sw blocking state */
3149 tpacpi_rfk_update_swstate_all();
3150
3151 /* Sync hw blocking state last if it is hw-unblocked */
3152 if (wlsw == TPACPI_RFK_RADIO_ON)
3153 tpacpi_rfk_update_hwblock_state(false);
3154
3155 /* Issue rfkill input event for WLSW switch */
3156 if (!(wlsw < 0)) {
733e27c1
HMH
3157 mutex_lock(&tpacpi_inputdev_send_mutex);
3158
3159 input_report_switch(tpacpi_inputdev,
19d337df 3160 SW_RFKILL_ALL, (wlsw > 0));
733e27c1
HMH
3161 input_sync(tpacpi_inputdev);
3162
3163 mutex_unlock(&tpacpi_inputdev_send_mutex);
3164 }
19d337df
JB
3165
3166 /*
3167 * this can be unconditional, as we will poll state again
3168 * if userspace uses the notify to read data
3169 */
733e27c1
HMH
3170 hotkey_radio_sw_notify_change();
3171}
3172
9c0a76e1
HMH
3173static void hotkey_exit(void)
3174{
3175#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
db25f16d 3176 mutex_lock(&hotkey_mutex);
9c0a76e1 3177 hotkey_poll_stop_sync();
db25f16d 3178 mutex_unlock(&hotkey_mutex);
9c0a76e1
HMH
3179#endif
3180
3181 if (hotkey_dev_attributes)
3182 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3183
4be73005 3184 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
0d922e3b
HMH
3185 "restoring original HKEY status and mask\n");
3186 /* yes, there is a bitwise or below, we want the
3187 * functions to be called even if one of them fail */
3188 if (((tp_features.hotkey_mask &&
3189 hotkey_mask_set(hotkey_orig_mask)) |
3190 hotkey_status_set(false)) != 0)
5cac62ac 3191 pr_err("failed to restore hot key mask to BIOS defaults\n");
9c0a76e1
HMH
3192}
3193
de4c8cc7
HMH
3194static void __init hotkey_unmap(const unsigned int scancode)
3195{
3196 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3197 clear_bit(hotkey_keycode_map[scancode],
3198 tpacpi_inputdev->keybit);
3199 hotkey_keycode_map[scancode] = KEY_RESERVED;
3200 }
3201}
3202
0d922e3b
HMH
3203/*
3204 * HKEY quirks:
3205 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3206 */
3207
3208#define TPACPI_HK_Q_INIMASK 0x0001
3209
3210static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3211 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3212 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3213 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3214 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3215 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3216 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3217 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3218 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3219 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3220 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3221 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3222 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3223 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3224 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3225 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3226 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3227 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3228 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3229 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3230};
3231
fc6e7568
HMH
3232typedef u16 tpacpi_keymap_entry_t;
3233typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
d1e14dca 3234
b3180028
L
3235static int hotkey_init_tablet_mode(void)
3236{
cb2bf251
DH
3237 int in_tablet_mode = 0, res;
3238 char *type = NULL;
b3180028 3239
dda3ec0a
BB
3240 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3241 int has_tablet_mode;
3242
3243 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3244 &has_tablet_mode);
3245 if (has_tablet_mode)
3246 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3247 type = "GMMS";
3248 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
b03f4d49 3249 /* For X41t, X60t, X61t Tablets... */
b3180028
L
3250 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3251 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3252 type = "MHKG";
3253 }
3254
3255 if (!tp_features.hotkey_tablet)
3256 return 0;
3257
3258 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3259 type, in_tablet_mode ? "tablet" : "laptop");
3260
3261 res = add_to_attr_set(hotkey_dev_attributes,
3262 &dev_attr_hotkey_tablet_mode.attr);
3263 if (res)
3264 return -1;
3265
3266 return in_tablet_mode;
3267}
3268
a5763f22 3269static int __init hotkey_init(struct ibm_init_struct *iibm)
56b6aeb0 3270{
0f089147
HMH
3271 /* Requirements for changing the default keymaps:
3272 *
3273 * 1. Many of the keys are mapped to KEY_RESERVED for very
3274 * good reasons. Do not change them unless you have deep
3275 * knowledge on the IBM and Lenovo ThinkPad firmware for
3276 * the various ThinkPad models. The driver behaves
3277 * differently for KEY_RESERVED: such keys have their
3278 * hot key mask *unset* in mask_recommended, and also
3279 * in the initial hot key mask programmed into the
3280 * firmware at driver load time, which means the firm-
3281 * ware may react very differently if you change them to
3282 * something else;
3283 *
3284 * 2. You must be subscribed to the linux-thinkpad and
3285 * ibm-acpi-devel mailing lists, and you should read the
3286 * list archives since 2007 if you want to change the
3287 * keymaps. This requirement exists so that you will
3288 * know the past history of problems with the thinkpad-
3289 * acpi driver keymaps, and also that you will be
3290 * listening to any bug reports;
3291 *
3292 * 3. Do not send thinkpad-acpi specific patches directly to
3293 * for merging, *ever*. Send them to the linux-acpi
3294 * mailinglist for comments. Merging is to be done only
3295 * through acpi-test and the ACPI maintainer.
3296 *
3297 * If the above is too much to ask, don't change the keymap.
3298 * Ask the thinkpad-acpi maintainer to do it, instead.
3299 */
d1e14dca
HMH
3300
3301 enum keymap_index {
3302 TPACPI_KEYMAP_IBM_GENERIC = 0,
3303 TPACPI_KEYMAP_LENOVO_GENERIC,
3304 };
3305
3306 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3307 /* Generic keymap for IBM ThinkPads */
3308 [TPACPI_KEYMAP_IBM_GENERIC] = {
edf0e0e5 3309 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
d1e14dca 3310 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
edf0e0e5
HMH
3311 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3312 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
0f089147
HMH
3313
3314 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
edf0e0e5
HMH
3315 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3316 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3317 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
0f089147 3318
0d922e3b 3319 /* brightness: firmware always reacts to them */
e927c08d 3320 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
e927c08d 3321 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
0f089147
HMH
3322
3323 /* Thinklight: firmware always react to it */
edf0e0e5 3324 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
0f089147 3325
edf0e0e5
HMH
3326 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3327 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
0f089147
HMH
3328
3329 /* Volume: firmware always react to it and reprograms
3330 * the built-in *extra* mixer. Never map it to control
3331 * another mixer by default. */
e927c08d
HMH
3332 KEY_RESERVED, /* 0x14: VOLUME UP */
3333 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3334 KEY_RESERVED, /* 0x16: MUTE */
0f089147 3335
edf0e0e5 3336 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
0f089147 3337
edf0e0e5
HMH
3338 /* (assignments unknown, please report if found) */
3339 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3340 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
6a68d855
BN
3341
3342 /* No assignments, only used for Adaptive keyboards. */
3343 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3344 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3345 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3346 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3347 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
696c6523
CK
3348
3349 /* No assignment, used for newer Lenovo models */
3350 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3351 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3352 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3353 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3354 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3355 KEY_UNKNOWN, KEY_UNKNOWN
3356
d1e14dca
HMH
3357 },
3358
3359 /* Generic keymap for Lenovo ThinkPads */
3360 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
edf0e0e5
HMH
3361 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3362 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2b754262 3363 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
edf0e0e5 3364 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
0f089147
HMH
3365
3366 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
edf0e0e5
HMH
3367 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3368 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3369 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
0f089147 3370
de4c8cc7
HMH
3371 /* These should be enabled --only-- when ACPI video
3372 * is disabled (i.e. in "vendor" mode), and are handled
3373 * in a special way by the init code */
3374 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3375 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
0f089147 3376
edf0e0e5 3377 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
0f089147 3378
edf0e0e5
HMH
3379 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3380 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
0f089147
HMH
3381
3382 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3383 * react to it and reprograms the built-in *extra* mixer.
3384 * Never map it to control another mixer by default.
3385 *
3386 * T60?, T61, R60?, R61: firmware and EC tries to send
3387 * these over the regular keyboard, so these are no-ops,
3388 * but there are still weird bugs re. MUTE, so do not
3389 * change unless you get test reports from all Lenovo
3390 * models. May cause the BIOS to interfere with the
3391 * HDA mixer.
3392 */
e927c08d
HMH
3393 KEY_RESERVED, /* 0x14: VOLUME UP */
3394 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3395 KEY_RESERVED, /* 0x16: MUTE */
0f089147 3396
edf0e0e5 3397 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
0f089147 3398
33009557
AL
3399 /* (assignments unknown, please report if found) */
3400 KEY_UNKNOWN, KEY_UNKNOWN,
3401
3402 /*
3403 * The mic mute button only sends 0x1a. It does not
3404 * automatically mute the mic or change the mute light.
3405 */
3406 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3407
edf0e0e5 3408 /* (assignments unknown, please report if found) */
33009557 3409 KEY_UNKNOWN,
8b9dd4fa
HG
3410
3411 /* Extra keys in use since the X240 / T440 / T540 */
4beb81d1 3412 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
6a68d855
BN
3413
3414 /*
3415 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3416 * The first item in this list is the Mute button which is
3417 * emitted with 0x103 through
3418 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3419 * symbol is held.
3420 * We'll need to offset those by 0x20.
3421 */
3422 KEY_RESERVED, /* Mute held, 0x103 */
3423 KEY_BRIGHTNESS_MIN, /* Backlight off */
3424 KEY_RESERVED, /* Clipping tool */
3425 KEY_RESERVED, /* Cloud */
3426 KEY_RESERVED,
3427 KEY_VOICECOMMAND, /* Voice */
3428 KEY_RESERVED,
3429 KEY_RESERVED, /* Gestures */
3430 KEY_RESERVED,
3431 KEY_RESERVED,
3432 KEY_RESERVED,
3433 KEY_CONFIG, /* Settings */
3434 KEY_RESERVED, /* New tab */
3435 KEY_REFRESH, /* Reload */
3436 KEY_BACK, /* Back */
3437 KEY_RESERVED, /* Microphone down */
3438 KEY_RESERVED, /* Microphone up */
3439 KEY_RESERVED, /* Microphone cancellation */
3440 KEY_RESERVED, /* Camera mode */
3441 KEY_RESERVED, /* Rotate display, 0x116 */
696c6523
CK
3442
3443 /*
3444 * These are found in 2017 models (e.g. T470s, X270).
3445 * The lowest known value is 0x311, which according to
3446 * the manual should launch a user defined favorite
3447 * application.
3448 *
3449 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3450 * corresponding to 0x34.
3451 */
3452
3453 /* (assignments unknown, please report if found) */
3454 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3455 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3456 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3457 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3458 KEY_UNKNOWN,
3459
3460 KEY_FAVORITES, /* Favorite app, 0x311 */
3461 KEY_RESERVED, /* Clipping tool */
cb5c1978 3462 KEY_CALC, /* Calculator (above numpad, P52) */
696c6523
CK
3463 KEY_BLUETOOTH, /* Bluetooth */
3464 KEY_KEYBOARD /* Keyboard, 0x315 */
d1e14dca 3465 },
edf0e0e5
HMH
3466 };
3467
d1e14dca
HMH
3468 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3469 /* Generic maps (fallback) */
3470 {
3471 .vendor = PCI_VENDOR_ID_IBM,
3472 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3473 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3474 },
3475 {
3476 .vendor = PCI_VENDOR_ID_LENOVO,
3477 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3478 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3479 },
3480 };
3481
3482#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
fc6e7568 3483#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
edf0e0e5 3484
6a38abbf 3485 int res, i;
74941a69 3486 int status;
1b6521dc 3487 int hkeyv;
d89a727a
HMH
3488 bool radiosw_state = false;
3489 bool tabletsw_state = false;
b86c4722 3490
0d922e3b 3491 unsigned long quirks;
d1e14dca 3492 unsigned long keymap_id;
0d922e3b 3493
56e2c200
HMH
3494 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3495 "initializing hotkey subdriver\n");
fe08bc4b 3496
6a38abbf 3497 BUG_ON(!tpacpi_inputdev);
01e88f25
HMH
3498 BUG_ON(tpacpi_inputdev->open != NULL ||
3499 tpacpi_inputdev->close != NULL);
6a38abbf 3500
e0c7dfe7 3501 TPACPI_ACPIHANDLE_INIT(hkey);
40ca9fdf 3502 mutex_init(&hotkey_mutex);
5fba344c 3503
01e88f25 3504#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
01e88f25
HMH
3505 mutex_init(&hotkey_thread_data_mutex);
3506#endif
3507
56b6aeb0 3508 /* hotkey not supported on 570 */
d8fd94d9 3509 tp_features.hotkey = hkey_handle != NULL;
56b6aeb0 3510
56e2c200
HMH
3511 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3512 "hotkeys are %s\n",
d8fd94d9 3513 str_supported(tp_features.hotkey));
fe08bc4b 3514
9c0a76e1
HMH
3515 if (!tp_features.hotkey)
3516 return 1;
a0416420 3517
0d922e3b
HMH
3518 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3519 ARRAY_SIZE(tpacpi_hotkey_qtable));
3520
d64c81c4
HMH
3521 tpacpi_disable_brightness_delay();
3522
0d922e3b
HMH
3523 /* MUST have enough space for all attributes to be added to
3524 * hotkey_dev_attributes */
3525 hotkey_dev_attributes = create_attr_set(
3526 ARRAY_SIZE(hotkey_attributes) + 2,
3527 NULL);
9c0a76e1
HMH
3528 if (!hotkey_dev_attributes)
3529 return -ENOMEM;
3530 res = add_many_to_attr_set(hotkey_dev_attributes,
3531 hotkey_attributes,
3532 ARRAY_SIZE(hotkey_attributes));
3533 if (res)
3534 goto err_exit;
3535
0d922e3b 3536 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
9c0a76e1
HMH
3537 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3538 for HKEY interface version 0x100 */
3539 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
0118c2d3
DW
3540 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3541 "firmware HKEY interface version: 0x%x\n",
3542 hkeyv);
3543
3544 switch (hkeyv >> 8) {
3545 case 1:
9c0a76e1
HMH
3546 /*
3547 * MHKV 0x100 in A31, R40, R40e,
3548 * T4x, X31, and later
3549 */
0d922e3b
HMH
3550
3551 /* Paranoia check AND init hotkey_all_mask */
3552 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3553 "MHKA", "qd")) {
0118c2d3
DW
3554 pr_err("missing MHKA handler, please report this to %s\n",
3555 TPACPI_MAIL);
3556 /* Fallback: pre-init for FN+F3,F4,F12 */
3557 hotkey_all_mask = 0x080cU;
3558 } else {
3559 tp_features.hotkey_mask = 1;
3560 }
3561 break;
3562
3563 case 2:
3564 /*
3565 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3566 */
3567
3568 /* Paranoia check AND init hotkey_all_mask */
3569 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3570 "MHKA", "dd", 1)) {
3571 pr_err("missing MHKA handler, please report this to %s\n",
0d922e3b
HMH
3572 TPACPI_MAIL);
3573 /* Fallback: pre-init for FN+F3,F4,F12 */
3574 hotkey_all_mask = 0x080cU;
3575 } else {
3576 tp_features.hotkey_mask = 1;
3577 }
0118c2d3
DW
3578
3579 /*
3580 * Check if we have an adaptive keyboard, like on the
3581 * Lenovo Carbon X1 2014 (2nd Gen).
3582 */
3583 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3584 "MHKA", "dd", 2)) {
3585 if (hotkey_adaptive_all_mask != 0) {
3586 tp_features.has_adaptive_kbd = true;
3587 res = sysfs_create_group(
3588 &tpacpi_pdev->dev.kobj,
3589 &adaptive_kbd_attr_group);
3590 if (res)
3591 goto err_exit;
3592 }
3593 } else {
3594 tp_features.has_adaptive_kbd = false;
3595 hotkey_adaptive_all_mask = 0x0U;
3596 }
3597 break;
3598
3599 default:
3600 pr_err("unknown version of the HKEY interface: 0x%x\n",
3601 hkeyv);
3602 pr_err("please report this to %s\n", TPACPI_MAIL);
3603 break;
1b6521dc 3604 }
9c0a76e1 3605 }
56b6aeb0 3606
56e2c200
HMH
3607 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3608 "hotkey masks are %s\n",
9c0a76e1 3609 str_supported(tp_features.hotkey_mask));
fe08bc4b 3610
0d922e3b
HMH
3611 /* Init hotkey_all_mask if not initialized yet */
3612 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3613 (quirks & TPACPI_HK_Q_INIMASK))
3614 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
9b010de5 3615
0d922e3b 3616 /* Init hotkey_acpi_mask and hotkey_orig_mask */
9c0a76e1 3617 if (tp_features.hotkey_mask) {
0d922e3b
HMH
3618 /* hotkey_source_mask *must* be zero for
3619 * the first hotkey_mask_get to return hotkey_orig_mask */
9c0a76e1
HMH
3620 res = hotkey_mask_get();
3621 if (res)
3622 goto err_exit;
3623
0d922e3b
HMH
3624 hotkey_orig_mask = hotkey_acpi_mask;
3625 } else {
3626 hotkey_orig_mask = hotkey_all_mask;
3627 hotkey_acpi_mask = hotkey_all_mask;
9c0a76e1 3628 }
74941a69 3629
a73f3091
HMH
3630#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3631 if (dbg_wlswemul) {
3632 tp_features.hotkey_wlsw = 1;
d89a727a 3633 radiosw_state = !!tpacpi_wlsw_emulstate;
0978e012 3634 pr_info("radio switch emulation enabled\n");
a73f3091
HMH
3635 } else
3636#endif
9c0a76e1
HMH
3637 /* Not all thinkpads have a hardware radio switch */
3638 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3639 tp_features.hotkey_wlsw = 1;
d89a727a 3640 radiosw_state = !!status;
0978e012 3641 pr_info("radio switch found; radios are %s\n",
9c0a76e1 3642 enabled(status, 0));
3a872080
HMH
3643 }
3644 if (tp_features.hotkey_wlsw)
9c0a76e1
HMH
3645 res = add_to_attr_set(hotkey_dev_attributes,
3646 &dev_attr_hotkey_radio_sw.attr);
74941a69 3647
b3180028
L
3648 res = hotkey_init_tablet_mode();
3649 if (res < 0)
3650 goto err_exit;
3651
3652 tabletsw_state = res;
6c231bd5 3653
b3180028
L
3654 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3655 &tpacpi_pdev->dev.kobj);
9c0a76e1
HMH
3656 if (res)
3657 goto err_exit;
6a38abbf 3658
9c0a76e1 3659 /* Set up key map */
9c0a76e1
HMH
3660 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3661 GFP_KERNEL);
3662 if (!hotkey_keycode_map) {
0978e012 3663 pr_err("failed to allocate memory for key map\n");
9c0a76e1
HMH
3664 res = -ENOMEM;
3665 goto err_exit;
3666 }
edf0e0e5 3667
d1e14dca
HMH
3668 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3669 ARRAY_SIZE(tpacpi_keymap_qtable));
3670 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3671 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3672 "using keymap number %lu\n", keymap_id);
3673
3674 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3675 TPACPI_HOTKEY_MAP_SIZE);
edf0e0e5 3676
792979c8 3677 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
9c0a76e1
HMH
3678 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3679 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3680 tpacpi_inputdev->keycode = hotkey_keycode_map;
3681 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3682 if (hotkey_keycode_map[i] != KEY_RESERVED) {
792979c8
HMH
3683 input_set_capability(tpacpi_inputdev, EV_KEY,
3684 hotkey_keycode_map[i]);
9c0a76e1
HMH
3685 } else {
3686 if (i < sizeof(hotkey_reserved_mask)*8)
3687 hotkey_reserved_mask |= 1 << i;
6a38abbf 3688 }
9c0a76e1 3689 }
6a38abbf 3690
9c0a76e1 3691 if (tp_features.hotkey_wlsw) {
792979c8 3692 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
d89a727a
HMH
3693 input_report_switch(tpacpi_inputdev,
3694 SW_RFKILL_ALL, radiosw_state);
9c0a76e1
HMH
3695 }
3696 if (tp_features.hotkey_tablet) {
792979c8 3697 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
d89a727a
HMH
3698 input_report_switch(tpacpi_inputdev,
3699 SW_TABLET_MODE, tabletsw_state);
9c0a76e1 3700 }
5c29d58f 3701
9c0a76e1 3702 /* Do not issue duplicate brightness change events to
77775838
HMH
3703 * userspace. tpacpi_detect_brightness_capabilities() must have
3704 * been called before this point */
b33c6ce5 3705 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
5cac62ac
AS
3706 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3707 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
9c0a76e1 3708
de4c8cc7
HMH
3709 /* Disable brightness up/down on Lenovo thinkpads when
3710 * ACPI is handling them, otherwise it is plain impossible
3711 * for userspace to do something even remotely sane */
9c0a76e1
HMH
3712 hotkey_reserved_mask |=
3713 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3714 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
de4c8cc7
HMH
3715 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3716 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
9c0a76e1 3717 }
ff80f137 3718
230d8cf2 3719#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
0d922e3b
HMH
3720 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3721 & ~hotkey_all_mask
3722 & ~hotkey_reserved_mask;
230d8cf2
HMH
3723
3724 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3725 "hotkey source mask 0x%08x, polling freq %u\n",
3726 hotkey_source_mask, hotkey_poll_freq);
3727#endif
3728
56e2c200
HMH
3729 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3730 "enabling firmware HKEY event interface...\n");
2586d566 3731 res = hotkey_status_set(true);
9c0a76e1
HMH
3732 if (res) {
3733 hotkey_exit();
3734 return res;
3735 }
0d922e3b
HMH
3736 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3737 | hotkey_driver_mask)
3738 & ~hotkey_source_mask);
9c0a76e1
HMH
3739 if (res < 0 && res != -ENXIO) {
3740 hotkey_exit();
3741 return res;
3742 }
0d922e3b
HMH
3743 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3744 & ~hotkey_reserved_mask;
3745 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3746 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3747 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
01e88f25 3748
9c0a76e1
HMH
3749 tpacpi_inputdev->open = &hotkey_inputdev_open;
3750 tpacpi_inputdev->close = &hotkey_inputdev_close;
56b6aeb0 3751
db25f16d 3752 hotkey_poll_setup_safe(true);
56b6aeb0 3753
9c0a76e1 3754 return 0;
01e88f25 3755
9c0a76e1
HMH
3756err_exit:
3757 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
b790ceeb
BN
3758 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3759 &adaptive_kbd_attr_group);
3760
9c0a76e1 3761 hotkey_dev_attributes = NULL;
a0416420 3762
72a979f0 3763 return (res < 0) ? res : 1;
56b6aeb0
HMH
3764}
3765
3a9d20bd
SS
3766/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3767 * mode, Web conference mode, Function mode and Lay-flat mode.
3768 * We support Home mode and Function mode currently.
3769 *
3770 * Will consider support rest of modes in future.
3771 *
3772 */
b201a47f 3773static const int adaptive_keyboard_modes[] = {
3a9d20bd
SS
3774 HOME_MODE,
3775/* WEB_BROWSER_MODE = 2,
3776 WEB_CONFERENCE_MODE = 3, */
3777 FUNCTION_MODE
3778};
3779
3780#define DFR_CHANGE_ROW 0x101
3781#define DFR_SHOW_QUICKVIEW_ROW 0x102
6a68d855 3782#define FIRST_ADAPTIVE_KEY 0x103
3a9d20bd
SS
3783
3784/* press Fn key a while second, it will switch to Function Mode. Then
3785 * release Fn key, previous mode be restored.
3786 */
3787static bool adaptive_keyboard_mode_is_saved;
3788static int adaptive_keyboard_prev_mode;
3789
f74587fb
BN
3790static int adaptive_keyboard_get_mode(void)
3791{
3792 int mode = 0;
3793
3794 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3795 pr_err("Cannot read adaptive keyboard mode\n");
3796 return -EIO;
3797 }
3798
3799 return mode;
3800}
3801
3802static int adaptive_keyboard_set_mode(int new_mode)
3803{
3804 if (new_mode < 0 ||
3805 new_mode > LAYFLAT_MODE)
3806 return -EINVAL;
3807
3808 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3809 pr_err("Cannot set adaptive keyboard mode\n");
3810 return -EIO;
3811 }
3812
3813 return 0;
3814}
3815
3a9d20bd
SS
3816static int adaptive_keyboard_get_next_mode(int mode)
3817{
3818 size_t i;
3819 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3820
3821 for (i = 0; i <= max_mode; i++) {
3822 if (adaptive_keyboard_modes[i] == mode)
3823 break;
3824 }
3825
3826 if (i >= max_mode)
3827 i = 0;
3828 else
3829 i++;
3830
3831 return adaptive_keyboard_modes[i];
3832}
3833
3834static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3835{
abf9dc0d 3836 int current_mode = 0;
3a9d20bd 3837 int new_mode = 0;
6a68d855 3838 int keycode;
3a9d20bd
SS
3839
3840 switch (scancode) {
3841 case DFR_CHANGE_ROW:
3842 if (adaptive_keyboard_mode_is_saved) {
3843 new_mode = adaptive_keyboard_prev_mode;
3844 adaptive_keyboard_mode_is_saved = false;
3845 } else {
f74587fb
BN
3846 current_mode = adaptive_keyboard_get_mode();
3847 if (current_mode < 0)
3a9d20bd 3848 return false;
f74587fb
BN
3849 new_mode = adaptive_keyboard_get_next_mode(
3850 current_mode);
3a9d20bd
SS
3851 }
3852
f74587fb 3853 if (adaptive_keyboard_set_mode(new_mode) < 0)
3a9d20bd 3854 return false;
3a9d20bd
SS
3855
3856 return true;
3857
3858 case DFR_SHOW_QUICKVIEW_ROW:
f74587fb
BN
3859 current_mode = adaptive_keyboard_get_mode();
3860 if (current_mode < 0)
3a9d20bd 3861 return false;
3a9d20bd 3862
f74587fb
BN
3863 adaptive_keyboard_prev_mode = current_mode;
3864 adaptive_keyboard_mode_is_saved = true;
3865
3866 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3867 return false;
3a9d20bd
SS
3868 return true;
3869
3870 default:
741d98c7 3871 if (scancode < FIRST_ADAPTIVE_KEY ||
696c6523
CK
3872 scancode >= FIRST_ADAPTIVE_KEY +
3873 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3874 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
6a68d855 3875 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
5cac62ac 3876 scancode);
6a68d855
BN
3877 return false;
3878 }
149c8c75
CK
3879 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3880 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
6a68d855
BN
3881 if (keycode != KEY_RESERVED) {
3882 mutex_lock(&tpacpi_inputdev_send_mutex);
3883
3884 input_report_key(tpacpi_inputdev, keycode, 1);
3885 input_sync(tpacpi_inputdev);
3886
3887 input_report_key(tpacpi_inputdev, keycode, 0);
3888 input_sync(tpacpi_inputdev);
3889
3890 mutex_unlock(&tpacpi_inputdev_send_mutex);
3891 }
3892 return true;
3a9d20bd
SS
3893 }
3894}
3895
3827e7a3
HMH
3896static bool hotkey_notify_hotkey(const u32 hkey,
3897 bool *send_acpi_ev,
3898 bool *ignore_acpi_ev)
3899{
3900 /* 0x1000-0x1FFF: key presses */
3901 unsigned int scancode = hkey & 0xfff;
3902 *send_acpi_ev = true;
3903 *ignore_acpi_ev = false;
3904
696c6523
CK
3905 /*
3906 * Original events are in the 0x10XX range, the adaptive keyboard
3907 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3908 * models, additional keys are emitted through 0x13XX.
3909 */
3910 switch ((hkey >> 8) & 0xf) {
3911 case 0:
3912 if (scancode > 0 &&
3913 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3914 /* HKEY event 0x1001 is scancode 0x00 */
3915 scancode--;
3916 if (!(hotkey_source_mask & (1 << scancode))) {
3917 tpacpi_input_send_key_masked(scancode);
3918 *send_acpi_ev = false;
3919 } else {
3920 *ignore_acpi_ev = true;
3921 }
3922 return true;
3827e7a3 3923 }
696c6523
CK
3924 break;
3925
3926 case 1:
3a9d20bd 3927 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
696c6523
CK
3928
3929 case 3:
3930 /* Extended keycodes start at 0x300 and our offset into the map
3931 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3932 * will be positive, but might not be in the correct range.
3933 */
3934 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3935 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3936 scancode < TPACPI_HOTKEY_MAP_LEN) {
3937 tpacpi_input_send_key(scancode);
3938 return true;
3939 }
3940 break;
3827e7a3 3941 }
696c6523 3942
3827e7a3
HMH
3943 return false;
3944}
3945
3946static bool hotkey_notify_wakeup(const u32 hkey,
3947 bool *send_acpi_ev,
3948 bool *ignore_acpi_ev)
3949{
3950 /* 0x2000-0x2FFF: Wakeup reason */
3951 *send_acpi_ev = true;
3952 *ignore_acpi_ev = false;
3953
3954 switch (hkey) {
67bcae6e
HMH
3955 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3956 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3827e7a3
HMH
3957 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3958 *ignore_acpi_ev = true;
3959 break;
3960
67bcae6e
HMH
3961 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3962 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3827e7a3
HMH
3963 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3964 *ignore_acpi_ev = true;
3965 break;
3966
67bcae6e
HMH
3967 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3968 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
0978e012 3969 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
106b4e66
HMH
3970 /* how to auto-heal: */
3971 /* 2313: woke up from S3, go to S4/S5 */
3972 /* 2413: woke up from S4, go to S5 */
3973 break;
3974
3827e7a3
HMH
3975 default:
3976 return false;
3977 }
3978
3979 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
0978e012 3980 pr_info("woke up due to a hot-unplug request...\n");
3827e7a3
HMH
3981 hotkey_wakeup_reason_notify_change();
3982 }
3983 return true;
3984}
3985
a50245af
HMH
3986static bool hotkey_notify_dockevent(const u32 hkey,
3987 bool *send_acpi_ev,
3988 bool *ignore_acpi_ev)
3989{
3990 /* 0x4000-0x4FFF: dock-related events */
3991 *send_acpi_ev = true;
3992 *ignore_acpi_ev = false;
3993
3994 switch (hkey) {
3995 case TP_HKEY_EV_UNDOCK_ACK:
3996 /* ACPI undock operation completed after wakeup */
3997 hotkey_autosleep_ack = 1;
3998 pr_info("undocked\n");
3999 hotkey_wakeup_hotunplug_complete_notify_change();
4000 return true;
4001
4002 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
4003 pr_info("docked into hotplug port replicator\n");
4004 return true;
4005 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
4006 pr_info("undocked from hotplug port replicator\n");
4007 return true;
4008
4009 default:
4010 return false;
4011 }
4012}
4013
3827e7a3
HMH
4014static bool hotkey_notify_usrevent(const u32 hkey,
4015 bool *send_acpi_ev,
4016 bool *ignore_acpi_ev)
4017{
4018 /* 0x5000-0x5FFF: human interface helpers */
4019 *send_acpi_ev = true;
4020 *ignore_acpi_ev = false;
4021
4022 switch (hkey) {
67bcae6e
HMH
4023 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
4024 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3827e7a3
HMH
4025 return true;
4026
67bcae6e
HMH
4027 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
4028 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3827e7a3
HMH
4029 tpacpi_input_send_tabletsw();
4030 hotkey_tablet_mode_notify_change();
4031 *send_acpi_ev = false;
4032 return true;
4033
67bcae6e
HMH
4034 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
4035 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
4036 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
176dd985 4037 /* do not propagate these events */
3827e7a3
HMH
4038 *ignore_acpi_ev = true;
4039 return true;
4040
4041 default:
4042 return false;
4043 }
4044}
4045
9ebd9e83
HMH
4046static void thermal_dump_all_sensors(void);
4047
2d43f671 4048static bool hotkey_notify_6xxx(const u32 hkey,
106b4e66
HMH
4049 bool *send_acpi_ev,
4050 bool *ignore_acpi_ev)
4051{
2d43f671 4052 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
106b4e66
HMH
4053 *send_acpi_ev = true;
4054 *ignore_acpi_ev = false;
4055
4056 switch (hkey) {
9ebd9e83 4057 case TP_HKEY_EV_THM_TABLE_CHANGED:
6e6bc5f6
HMH
4058 pr_debug("EC reports: Thermal Table has changed\n");
4059 /* recommended action: do nothing, we don't have
4060 * Lenovo ATM information */
4061 return true;
4062 case TP_HKEY_EV_THM_CSM_COMPLETED:
4063 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
4064 /* recommended action: do nothing, we don't have
4065 * Lenovo ATM information */
4066 return true;
4067 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
4068 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
9ebd9e83
HMH
4069 /* recommended action: do nothing, we don't have
4070 * Lenovo ATM information */
4071 return true;
67bcae6e 4072 case TP_HKEY_EV_ALARM_BAT_HOT:
0978e012 4073 pr_crit("THERMAL ALARM: battery is too hot!\n");
106b4e66 4074 /* recommended action: warn user through gui */
9ebd9e83 4075 break;
67bcae6e 4076 case TP_HKEY_EV_ALARM_BAT_XHOT:
0978e012 4077 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
106b4e66 4078 /* recommended action: immediate sleep/hibernate */
9ebd9e83 4079 break;
67bcae6e 4080 case TP_HKEY_EV_ALARM_SENSOR_HOT:
5cac62ac 4081 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
106b4e66
HMH
4082 /* recommended action: warn user through gui, that */
4083 /* some internal component is too hot */
9ebd9e83 4084 break;
67bcae6e 4085 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
5cac62ac 4086 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
106b4e66 4087 /* recommended action: immediate sleep/hibernate */
9ebd9e83 4088 break;
6f62bc3e
RH
4089 case TP_HKEY_EV_AC_CHANGED:
4090 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4091 * AC status changed; can be triggered by plugging or
4092 * unplugging AC adapter, docking or undocking. */
4093
4094 /* fallthrough */
2d43f671
HMH
4095
4096 case TP_HKEY_EV_KEY_NUMLOCK:
4097 case TP_HKEY_EV_KEY_FN:
67ab6248 4098 case TP_HKEY_EV_KEY_FN_ESC:
2d43f671
HMH
4099 /* key press events, we just ignore them as long as the EC
4100 * is still reporting them in the normal keyboard stream */
4101 *send_acpi_ev = false;
4102 *ignore_acpi_ev = true;
4103 return true;
4104
b03f4d49
L
4105 case TP_HKEY_EV_TABLET_CHANGED:
4106 tpacpi_input_send_tabletsw();
4107 hotkey_tablet_mode_notify_change();
4108 *send_acpi_ev = false;
edd1ed73 4109 return true;
b03f4d49 4110
587d8628
DH
4111 case TP_HKEY_EV_PALM_DETECTED:
4112 case TP_HKEY_EV_PALM_UNDETECTED:
4113 /* palm detected hovering the keyboard, forward to user-space
4114 * via netlink for consumption */
4115 return true;
4116
106b4e66 4117 default:
fd75ba2b
HMH
4118 /* report simply as unknown, no sensor dump */
4119 return false;
106b4e66 4120 }
9ebd9e83
HMH
4121
4122 thermal_dump_all_sensors();
fd75ba2b 4123 return true;
106b4e66
HMH
4124}
4125
56b6aeb0
HMH
4126static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4127{
6a38abbf 4128 u32 hkey;
3827e7a3
HMH
4129 bool send_acpi_ev;
4130 bool ignore_acpi_ev;
4131 bool known_ev;
3eea123d
HMH
4132
4133 if (event != 0x80) {
0978e012 4134 pr_err("unknown HKEY notification event %d\n", event);
3eea123d 4135 /* forward it to userspace, maybe it knows how to handle it */
35ff8b9f
HMH
4136 acpi_bus_generate_netlink_event(
4137 ibm->acpi->device->pnp.device_class,
e0b36fc5 4138 dev_name(&ibm->acpi->device->dev),
35ff8b9f 4139 event, 0);
3eea123d
HMH
4140 return;
4141 }
4142
4143 while (1) {
4144 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
0978e012 4145 pr_err("failed to retrieve HKEY event\n");
3eea123d
HMH
4146 return;
4147 }
4148
4149 if (hkey == 0) {
4150 /* queue empty */
4151 return;
4152 }
4153
3827e7a3
HMH
4154 send_acpi_ev = true;
4155 ignore_acpi_ev = false;
56b6aeb0 4156
ff80f137
HMH
4157 switch (hkey >> 12) {
4158 case 1:
4159 /* 0x1000-0x1FFF: key presses */
3827e7a3
HMH
4160 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4161 &ignore_acpi_ev);
ff80f137 4162 break;
a713b4d7 4163 case 2:
3827e7a3
HMH
4164 /* 0x2000-0x2FFF: Wakeup reason */
4165 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4166 &ignore_acpi_ev);
a713b4d7
HMH
4167 break;
4168 case 3:
3827e7a3 4169 /* 0x3000-0x3FFF: bay-related wakeups */
bf8b29c8
HMH
4170 switch (hkey) {
4171 case TP_HKEY_EV_BAYEJ_ACK:
a713b4d7 4172 hotkey_autosleep_ack = 1;
0978e012 4173 pr_info("bay ejected\n");
50ebec09 4174 hotkey_wakeup_hotunplug_complete_notify_change();
3827e7a3 4175 known_ev = true;
bf8b29c8
HMH
4176 break;
4177 case TP_HKEY_EV_OPTDRV_EJ:
4178 /* FIXME: kick libata if SATA link offline */
4179 known_ev = true;
4180 break;
4181 default:
3827e7a3 4182 known_ev = false;
a713b4d7
HMH
4183 }
4184 break;
4185 case 4:
a50245af
HMH
4186 /* 0x4000-0x4FFF: dock-related events */
4187 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4188 &ignore_acpi_ev);
a713b4d7 4189 break;
ff80f137 4190 case 5:
d1edb2b5 4191 /* 0x5000-0x5FFF: human interface helpers */
3827e7a3
HMH
4192 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4193 &ignore_acpi_ev);
ff80f137 4194 break;
106b4e66 4195 case 6:
2d43f671
HMH
4196 /* 0x6000-0x6FFF: thermal alarms/notices and
4197 * keyboard events */
4198 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
106b4e66
HMH
4199 &ignore_acpi_ev);
4200 break;
ff80f137
HMH
4201 case 7:
4202 /* 0x7000-0x7FFF: misc */
67bcae6e
HMH
4203 if (tp_features.hotkey_wlsw &&
4204 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
733e27c1 4205 tpacpi_send_radiosw_update();
3b64b51d 4206 send_acpi_ev = 0;
3827e7a3 4207 known_ev = true;
6a38abbf 4208 break;
6a38abbf 4209 }
ff80f137
HMH
4210 /* fallthrough to default */
4211 default:
3827e7a3 4212 known_ev = false;
3b64b51d 4213 }
3827e7a3 4214 if (!known_ev) {
0978e012 4215 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
5cac62ac
AS
4216 pr_notice("please report the conditions when this event happened to %s\n",
4217 TPACPI_MAIL);
6a38abbf 4218 }
ff80f137 4219
3eea123d 4220 /* netlink events */
3e5ce914 4221 if (!ignore_acpi_ev && send_acpi_ev) {
35ff8b9f
HMH
4222 acpi_bus_generate_netlink_event(
4223 ibm->acpi->device->pnp.device_class,
e0b36fc5 4224 dev_name(&ibm->acpi->device->dev),
35ff8b9f 4225 event, hkey);
3eea123d 4226 }
56b6aeb0
HMH
4227 }
4228}
4229
fd3c3a42 4230static void hotkey_suspend(void)
a713b4d7
HMH
4231{
4232 /* Do these on suspend, we get the events on early resume! */
4233 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4234 hotkey_autosleep_ack = 0;
330947b8
SS
4235
4236 /* save previous mode of adaptive keyboard of X1 Carbon */
f23a5bcb
BN
4237 if (tp_features.has_adaptive_kbd) {
4238 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4239 "GTRW", "dd", 0)) {
4240 pr_err("Cannot read adaptive keyboard mode.\n");
330947b8
SS
4241 }
4242 }
a713b4d7
HMH
4243}
4244
5c29d58f
HMH
4245static void hotkey_resume(void)
4246{
d64c81c4
HMH
4247 tpacpi_disable_brightness_delay();
4248
0d922e3b
HMH
4249 if (hotkey_status_set(true) < 0 ||
4250 hotkey_mask_set(hotkey_acpi_mask) < 0)
5cac62ac 4251 pr_err("error while attempting to reset the event firmware interface\n");
0d922e3b 4252
733e27c1 4253 tpacpi_send_radiosw_update();
6c231bd5 4254 hotkey_tablet_mode_notify_change();
50ebec09
HMH
4255 hotkey_wakeup_reason_notify_change();
4256 hotkey_wakeup_hotunplug_complete_notify_change();
db25f16d 4257 hotkey_poll_setup_safe(false);
330947b8
SS
4258
4259 /* restore previous mode of adapive keyboard of X1 Carbon */
f23a5bcb
BN
4260 if (tp_features.has_adaptive_kbd) {
4261 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4262 adaptive_keyboard_prev_mode)) {
4263 pr_err("Cannot set adaptive keyboard mode.\n");
330947b8
SS
4264 }
4265 }
5c29d58f
HMH
4266}
4267
a0416420 4268/* procfs -------------------------------------------------------------- */
887965e6 4269static int hotkey_read(struct seq_file *m)
1da177e4 4270{
ae92bd17 4271 int res, status;
1da177e4 4272
d8fd94d9 4273 if (!tp_features.hotkey) {
887965e6
AD
4274 seq_printf(m, "status:\t\tnot supported\n");
4275 return 0;
78f81cc4
BD
4276 }
4277
7646ea88 4278 if (mutex_lock_killable(&hotkey_mutex))
fc589a3c 4279 return -ERESTARTSYS;
b2c985e7
HMH
4280 res = hotkey_status_get(&status);
4281 if (!res)
4282 res = hotkey_mask_get();
40ca9fdf 4283 mutex_unlock(&hotkey_mutex);
b86c4722
HMH
4284 if (res)
4285 return res;
1da177e4 4286
887965e6 4287 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
0d922e3b 4288 if (hotkey_all_mask) {
887965e6
AD
4289 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4290 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
1da177e4 4291 } else {
887965e6
AD
4292 seq_printf(m, "mask:\t\tnot supported\n");
4293 seq_printf(m, "commands:\tenable, disable, reset\n");
1da177e4
LT
4294 }
4295
887965e6 4296 return 0;
1da177e4
LT
4297}
4298
406e988b 4299static void hotkey_enabledisable_warn(bool enable)
2586d566
HMH
4300{
4301 tpacpi_log_usertask("procfs hotkey enable/disable");
406e988b 4302 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
5cac62ac
AS
4303 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4304 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
2586d566
HMH
4305}
4306
78f81cc4 4307static int hotkey_write(char *buf)
1da177e4 4308{
2586d566 4309 int res;
ae92bd17 4310 u32 mask;
1da177e4 4311 char *cmd;
1da177e4 4312
d8fd94d9 4313 if (!tp_features.hotkey)
1da177e4
LT
4314 return -ENODEV;
4315
7646ea88 4316 if (mutex_lock_killable(&hotkey_mutex))
fc589a3c 4317 return -ERESTARTSYS;
40ca9fdf 4318
0d922e3b 4319 mask = hotkey_user_mask;
78f81cc4 4320
40ca9fdf 4321 res = 0;
1da177e4
LT
4322 while ((cmd = next_cmd(&buf))) {
4323 if (strlencmp(cmd, "enable") == 0) {
406e988b 4324 hotkey_enabledisable_warn(1);
1da177e4 4325 } else if (strlencmp(cmd, "disable") == 0) {
406e988b 4326 hotkey_enabledisable_warn(0);
2586d566 4327 res = -EPERM;
1da177e4 4328 } else if (strlencmp(cmd, "reset") == 0) {
20c9aa46
HMH
4329 mask = (hotkey_all_mask | hotkey_source_mask)
4330 & ~hotkey_reserved_mask;
1da177e4
LT
4331 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4332 /* mask set */
4333 } else if (sscanf(cmd, "%x", &mask) == 1) {
4334 /* mask set */
40ca9fdf
HMH
4335 } else {
4336 res = -EINVAL;
4337 goto errexit;
4338 }
1da177e4 4339 }
56e2c200 4340
0d922e3b 4341 if (!res) {
56e2c200
HMH
4342 tpacpi_disclose_usertask("procfs hotkey",
4343 "set mask to 0x%08x\n", mask);
0d922e3b
HMH
4344 res = hotkey_user_mask_set(mask);
4345 }
1da177e4 4346
40ca9fdf
HMH
4347errexit:
4348 mutex_unlock(&hotkey_mutex);
4349 return res;
78f81cc4 4350}
1da177e4 4351
1ba90e3a 4352static const struct acpi_device_id ibm_htk_device_ids[] = {
9fbdaeb4
MI
4353 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4354 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
a3c42a46 4355 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
1ba90e3a
TR
4356 {"", 0},
4357};
4358
8d376cd6 4359static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1ba90e3a 4360 .hid = ibm_htk_device_ids,
8d376cd6
HMH
4361 .notify = hotkey_notify,
4362 .handle = &hkey_handle,
4363 .type = ACPI_DEVICE_NOTIFY,
4364};
4365
a5763f22
HMH
4366static struct ibm_struct hotkey_driver_data = {
4367 .name = "hotkey",
a5763f22
HMH
4368 .read = hotkey_read,
4369 .write = hotkey_write,
4370 .exit = hotkey_exit,
5c29d58f 4371 .resume = hotkey_resume,
a713b4d7 4372 .suspend = hotkey_suspend,
8d376cd6 4373 .acpi = &ibm_hotkey_acpidriver,
a5763f22
HMH
4374};
4375
56b6aeb0
HMH
4376/*************************************************************************
4377 * Bluetooth subdriver
4378 */
1da177e4 4379
f74a27d4
HMH
4380enum {
4381 /* ACPI GBDC/SBDC bits */
4382 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4383 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
153f8220 4384 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
08fedfc9 4385 0 = disable, 1 = enable */
153f8220
HMH
4386};
4387
4388enum {
4389 /* ACPI \BLTH commands */
4390 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4391 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4392 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4393 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4394 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
f74a27d4
HMH
4395};
4396
bee4cd9b
HMH
4397#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4398
19d337df 4399static int bluetooth_get_status(void)
07431ec8
HMH
4400{
4401 int status;
4402
a73f3091
HMH
4403#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4404 if (dbg_bluetoothemul)
4405 return (tpacpi_bluetooth_emulstate) ?
19d337df 4406 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
a73f3091
HMH
4407#endif
4408
07431ec8
HMH
4409 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4410 return -EIO;
4411
0e74dc26 4412 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
19d337df 4413 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
07431ec8
HMH
4414}
4415
19d337df 4416static int bluetooth_set_status(enum tpacpi_rfkill_state state)
0e74dc26
HMH
4417{
4418 int status;
4419
bee4cd9b 4420 vdbg_printk(TPACPI_DBG_RFKILL,
19d337df
JB
4421 "will attempt to %s bluetooth\n",
4422 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
bee4cd9b 4423
a73f3091
HMH
4424#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4425 if (dbg_bluetoothemul) {
19d337df 4426 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
a73f3091
HMH
4427 return 0;
4428 }
4429#endif
4430
19d337df 4431 if (state == TPACPI_RFK_RADIO_ON)
08fedfc9
HMH
4432 status = TP_ACPI_BLUETOOTH_RADIOSSW
4433 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4434 else
4435 status = 0;
19d337df 4436
07431ec8
HMH
4437 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4438 return -EIO;
4439
4440 return 0;
4441}
f74a27d4 4442
d3a6ade4
HMH
4443/* sysfs bluetooth enable ---------------------------------------------- */
4444static ssize_t bluetooth_enable_show(struct device *dev,
4445 struct device_attribute *attr,
4446 char *buf)
4447{
19d337df
JB
4448 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4449 attr, buf);
d3a6ade4
HMH
4450}
4451
4452static ssize_t bluetooth_enable_store(struct device *dev,
4453 struct device_attribute *attr,
4454 const char *buf, size_t count)
4455{
19d337df
JB
4456 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4457 attr, buf, count);
d3a6ade4
HMH
4458}
4459
b4dd04ac 4460static DEVICE_ATTR_RW(bluetooth_enable);
d3a6ade4
HMH
4461
4462/* --------------------------------------------------------------------- */
4463
4464static struct attribute *bluetooth_attributes[] = {
4465 &dev_attr_bluetooth_enable.attr,
4466 NULL
4467};
4468
4469static const struct attribute_group bluetooth_attr_group = {
d3a6ade4
HMH
4470 .attrs = bluetooth_attributes,
4471};
4472
19d337df
JB
4473static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4474 .get_status = bluetooth_get_status,
4475 .set_status = bluetooth_set_status,
4476};
0e74dc26 4477
90d9d3c7
HMH
4478static void bluetooth_shutdown(void)
4479{
4480 /* Order firmware to save current state to NVRAM */
4481 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4482 TP_ACPI_BLTH_SAVE_STATE))
0978e012 4483 pr_notice("failed to save bluetooth state to NVRAM\n");
bee4cd9b
HMH
4484 else
4485 vdbg_printk(TPACPI_DBG_RFKILL,
1f01358c 4486 "bluetooth state saved to NVRAM\n");
90d9d3c7
HMH
4487}
4488
07431ec8
HMH
4489static void bluetooth_exit(void)
4490{
4491 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4492 &bluetooth_attr_group);
19d337df
JB
4493
4494 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4495
4496 bluetooth_shutdown();
07431ec8
HMH
4497}
4498
a5763f22 4499static int __init bluetooth_init(struct ibm_init_struct *iibm)
1da177e4 4500{
d3a6ade4 4501 int res;
d6fdd1e9
HMH
4502 int status = 0;
4503
bee4cd9b
HMH
4504 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4505 "initializing bluetooth subdriver\n");
fe08bc4b 4506
e0c7dfe7 4507 TPACPI_ACPIHANDLE_INIT(hkey);
5fba344c 4508
78f81cc4
BD
4509 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4510 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
d8fd94d9 4511 tp_features.bluetooth = hkey_handle &&
d6fdd1e9
HMH
4512 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4513
bee4cd9b
HMH
4514 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4515 "bluetooth is %s, status 0x%02x\n",
d6fdd1e9
HMH
4516 str_supported(tp_features.bluetooth),
4517 status);
4518
a73f3091
HMH
4519#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4520 if (dbg_bluetoothemul) {
4521 tp_features.bluetooth = 1;
0978e012 4522 pr_info("bluetooth switch emulation enabled\n");
a73f3091
HMH
4523 } else
4524#endif
3a872080
HMH
4525 if (tp_features.bluetooth &&
4526 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4527 /* no bluetooth hardware present in system */
4528 tp_features.bluetooth = 0;
bee4cd9b 4529 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3a872080
HMH
4530 "bluetooth hardware not installed\n");
4531 }
4532
0e74dc26
HMH
4533 if (!tp_features.bluetooth)
4534 return 1;
4535
0e74dc26 4536 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
19d337df 4537 &bluetooth_tprfk_ops,
0e74dc26 4538 RFKILL_TYPE_BLUETOOTH,
bee4cd9b 4539 TPACPI_RFK_BLUETOOTH_SW_NAME,
19d337df
JB
4540 true);
4541 if (res)
4542 return res;
4543
4544 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4545 &bluetooth_attr_group);
0e74dc26 4546 if (res) {
19d337df 4547 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
0e74dc26 4548 return res;
d6fdd1e9 4549 }
fe08bc4b 4550
0e74dc26 4551 return 0;
1da177e4
LT
4552}
4553
d3a6ade4 4554/* procfs -------------------------------------------------------------- */
887965e6 4555static int bluetooth_read(struct seq_file *m)
1da177e4 4556{
887965e6 4557 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
1da177e4
LT
4558}
4559
78f81cc4 4560static int bluetooth_write(char *buf)
1da177e4 4561{
19d337df 4562 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
1da177e4
LT
4563}
4564
a5763f22
HMH
4565static struct ibm_struct bluetooth_driver_data = {
4566 .name = "bluetooth",
4567 .read = bluetooth_read,
4568 .write = bluetooth_write,
d3a6ade4 4569 .exit = bluetooth_exit,
90d9d3c7 4570 .shutdown = bluetooth_shutdown,
a5763f22
HMH
4571};
4572
56b6aeb0
HMH
4573/*************************************************************************
4574 * Wan subdriver
4575 */
4576
f74a27d4
HMH
4577enum {
4578 /* ACPI GWAN/SWAN bits */
4579 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4580 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
153f8220 4581 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
08fedfc9 4582 0 = disable, 1 = enable */
f74a27d4
HMH
4583};
4584
bee4cd9b
HMH
4585#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4586
19d337df 4587static int wan_get_status(void)
07431ec8
HMH
4588{
4589 int status;
4590
a73f3091
HMH
4591#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4592 if (dbg_wwanemul)
4593 return (tpacpi_wwan_emulstate) ?
19d337df 4594 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
a73f3091
HMH
4595#endif
4596
07431ec8
HMH
4597 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4598 return -EIO;
4599
0e74dc26 4600 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
19d337df 4601 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
0e74dc26
HMH
4602}
4603
19d337df 4604static int wan_set_status(enum tpacpi_rfkill_state state)
07431ec8
HMH
4605{
4606 int status;
4607
bee4cd9b 4608 vdbg_printk(TPACPI_DBG_RFKILL,
19d337df
JB
4609 "will attempt to %s wwan\n",
4610 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
bee4cd9b 4611
a73f3091
HMH
4612#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4613 if (dbg_wwanemul) {
19d337df 4614 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
a73f3091
HMH
4615 return 0;
4616 }
4617#endif
4618
19d337df 4619 if (state == TPACPI_RFK_RADIO_ON)
08fedfc9
HMH
4620 status = TP_ACPI_WANCARD_RADIOSSW
4621 | TP_ACPI_WANCARD_RESUMECTRL;
4622 else
4623 status = 0;
19d337df 4624
07431ec8
HMH
4625 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4626 return -EIO;
4627
4628 return 0;
4629}
f74a27d4 4630
d3a6ade4
HMH
4631/* sysfs wan enable ---------------------------------------------------- */
4632static ssize_t wan_enable_show(struct device *dev,
4633 struct device_attribute *attr,
4634 char *buf)
4635{
19d337df
JB
4636 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4637 attr, buf);
d3a6ade4
HMH
4638}
4639
4640static ssize_t wan_enable_store(struct device *dev,
4641 struct device_attribute *attr,
4642 const char *buf, size_t count)
4643{
19d337df
JB
4644 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4645 attr, buf, count);
d3a6ade4
HMH
4646}
4647
5fb73bc2
BM
4648static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4649 wan_enable_show, wan_enable_store);
d3a6ade4
HMH
4650
4651/* --------------------------------------------------------------------- */
4652
4653static struct attribute *wan_attributes[] = {
5fb73bc2 4654 &dev_attr_wwan_enable.attr,
d3a6ade4
HMH
4655 NULL
4656};
4657
4658static const struct attribute_group wan_attr_group = {
d3a6ade4
HMH
4659 .attrs = wan_attributes,
4660};
4661
19d337df
JB
4662static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4663 .get_status = wan_get_status,
4664 .set_status = wan_set_status,
4665};
0e74dc26 4666
90d9d3c7
HMH
4667static void wan_shutdown(void)
4668{
4669 /* Order firmware to save current state to NVRAM */
4670 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4671 TP_ACPI_WGSV_SAVE_STATE))
0978e012 4672 pr_notice("failed to save WWAN state to NVRAM\n");
bee4cd9b
HMH
4673 else
4674 vdbg_printk(TPACPI_DBG_RFKILL,
4675 "WWAN state saved to NVRAM\n");
90d9d3c7
HMH
4676}
4677
07431ec8
HMH
4678static void wan_exit(void)
4679{
4680 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4681 &wan_attr_group);
19d337df
JB
4682
4683 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4684
4685 wan_shutdown();
07431ec8
HMH
4686}
4687
a5763f22 4688static int __init wan_init(struct ibm_init_struct *iibm)
42adb53c 4689{
d3a6ade4 4690 int res;
d6fdd1e9
HMH
4691 int status = 0;
4692
bee4cd9b
HMH
4693 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4694 "initializing wan subdriver\n");
fe08bc4b 4695
e0c7dfe7 4696 TPACPI_ACPIHANDLE_INIT(hkey);
5fba344c 4697
d8fd94d9 4698 tp_features.wan = hkey_handle &&
d6fdd1e9
HMH
4699 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4700
bee4cd9b
HMH
4701 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4702 "wan is %s, status 0x%02x\n",
d6fdd1e9
HMH
4703 str_supported(tp_features.wan),
4704 status);
4705
a73f3091
HMH
4706#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4707 if (dbg_wwanemul) {
4708 tp_features.wan = 1;
0978e012 4709 pr_info("wwan switch emulation enabled\n");
a73f3091
HMH
4710 } else
4711#endif
3a872080
HMH
4712 if (tp_features.wan &&
4713 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4714 /* no wan hardware present in system */
4715 tp_features.wan = 0;
bee4cd9b 4716 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3a872080
HMH
4717 "wan hardware not installed\n");
4718 }
4719
0e74dc26
HMH
4720 if (!tp_features.wan)
4721 return 1;
4722
0e74dc26 4723 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
19d337df 4724 &wan_tprfk_ops,
0e74dc26 4725 RFKILL_TYPE_WWAN,
bee4cd9b 4726 TPACPI_RFK_WWAN_SW_NAME,
19d337df
JB
4727 true);
4728 if (res)
4729 return res;
4730
4731 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4732 &wan_attr_group);
4733
0e74dc26 4734 if (res) {
19d337df 4735 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
0e74dc26 4736 return res;
d6fdd1e9 4737 }
fe08bc4b 4738
0e74dc26 4739 return 0;
42adb53c
JF
4740}
4741
d3a6ade4 4742/* procfs -------------------------------------------------------------- */
887965e6 4743static int wan_read(struct seq_file *m)
42adb53c 4744{
887965e6 4745 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
42adb53c
JF
4746}
4747
4748static int wan_write(char *buf)
4749{
19d337df 4750 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
42adb53c
JF
4751}
4752
a5763f22
HMH
4753static struct ibm_struct wan_driver_data = {
4754 .name = "wan",
4755 .read = wan_read,
4756 .write = wan_write,
d3a6ade4 4757 .exit = wan_exit,
90d9d3c7 4758 .shutdown = wan_shutdown,
a5763f22
HMH
4759};
4760
0045c0aa
HMH
4761/*************************************************************************
4762 * UWB subdriver
4763 */
4764
4765enum {
4766 /* ACPI GUWB/SUWB bits */
4767 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4768 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4769};
4770
bee4cd9b
HMH
4771#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4772
19d337df 4773static int uwb_get_status(void)
0045c0aa
HMH
4774{
4775 int status;
4776
0045c0aa
HMH
4777#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4778 if (dbg_uwbemul)
4779 return (tpacpi_uwb_emulstate) ?
19d337df 4780 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
0045c0aa
HMH
4781#endif
4782
4783 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4784 return -EIO;
4785
4786 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
19d337df 4787 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
0045c0aa
HMH
4788}
4789
19d337df 4790static int uwb_set_status(enum tpacpi_rfkill_state state)
0045c0aa
HMH
4791{
4792 int status;
4793
bee4cd9b 4794 vdbg_printk(TPACPI_DBG_RFKILL,
19d337df
JB
4795 "will attempt to %s UWB\n",
4796 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
bee4cd9b 4797
0045c0aa
HMH
4798#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4799 if (dbg_uwbemul) {
19d337df 4800 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
0045c0aa
HMH
4801 return 0;
4802 }
4803#endif
4804
19d337df
JB
4805 if (state == TPACPI_RFK_RADIO_ON)
4806 status = TP_ACPI_UWB_RADIOSSW;
4807 else
4808 status = 0;
4809
0045c0aa
HMH
4810 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4811 return -EIO;
4812
0045c0aa
HMH
4813 return 0;
4814}
4815
4816/* --------------------------------------------------------------------- */
4817
19d337df
JB
4818static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4819 .get_status = uwb_get_status,
4820 .set_status = uwb_set_status,
4821};
0045c0aa
HMH
4822
4823static void uwb_exit(void)
4824{
19d337df 4825 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
0045c0aa
HMH
4826}
4827
4828static int __init uwb_init(struct ibm_init_struct *iibm)
4829{
4830 int res;
4831 int status = 0;
4832
bee4cd9b
HMH
4833 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4834 "initializing uwb subdriver\n");
0045c0aa
HMH
4835
4836 TPACPI_ACPIHANDLE_INIT(hkey);
4837
4838 tp_features.uwb = hkey_handle &&
4839 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4840
bee4cd9b
HMH
4841 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4842 "uwb is %s, status 0x%02x\n",
0045c0aa
HMH
4843 str_supported(tp_features.uwb),
4844 status);
4845
4846#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4847 if (dbg_uwbemul) {
4848 tp_features.uwb = 1;
0978e012 4849 pr_info("uwb switch emulation enabled\n");
0045c0aa
HMH
4850 } else
4851#endif
4852 if (tp_features.uwb &&
4853 !(status & TP_ACPI_UWB_HWPRESENT)) {
4854 /* no uwb hardware present in system */
4855 tp_features.uwb = 0;
4856 dbg_printk(TPACPI_DBG_INIT,
4857 "uwb hardware not installed\n");
4858 }
4859
4860 if (!tp_features.uwb)
4861 return 1;
4862
4863 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
19d337df 4864 &uwb_tprfk_ops,
0045c0aa 4865 RFKILL_TYPE_UWB,
bee4cd9b 4866 TPACPI_RFK_UWB_SW_NAME,
19d337df 4867 false);
0045c0aa
HMH
4868 return res;
4869}
4870
4871static struct ibm_struct uwb_driver_data = {
4872 .name = "uwb",
4873 .exit = uwb_exit,
4874 .flags.experimental = 1,
4875};
4876
56b6aeb0
HMH
4877/*************************************************************************
4878 * Video subdriver
4879 */
4880
d7c1d17d
HMH
4881#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4882
f74a27d4
HMH
4883enum video_access_mode {
4884 TPACPI_VIDEO_NONE = 0,
4885 TPACPI_VIDEO_570, /* 570 */
4886 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4887 TPACPI_VIDEO_NEW, /* all others */
4888};
4889
4890enum { /* video status flags, based on VIDEO_570 */
4891 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4892 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4893 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4894};
4895
4896enum { /* TPACPI_VIDEO_570 constants */
4897 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4898 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4899 * video_status_flags */
4900 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4901 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4902};
4903
9a8e1738
HMH
4904static enum video_access_mode video_supported;
4905static int video_orig_autosw;
78f81cc4 4906
f74a27d4
HMH
4907static int video_autosw_get(void);
4908static int video_autosw_set(int enable);
4909
112a6ee0
JP
4910TPACPI_HANDLE(vid, root,
4911 "\\_SB.PCI.AGP.VGA", /* 570 */
4912 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4913 "\\_SB.PCI0.VID0", /* 770e */
4914 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4915 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4916 "\\_SB.PCI0.AGP.VID", /* all others */
4917 ); /* R30, R31 */
4918
e0c7dfe7 4919TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
56b6aeb0 4920
a5763f22 4921static int __init video_init(struct ibm_init_struct *iibm)
1da177e4 4922{
78f81cc4
BD
4923 int ivga;
4924
fe08bc4b
HMH
4925 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4926
e0c7dfe7 4927 TPACPI_ACPIHANDLE_INIT(vid);
e28393c0
HMH
4928 if (tpacpi_is_ibm())
4929 TPACPI_ACPIHANDLE_INIT(vid2);
5fba344c 4930
78f81cc4
BD
4931 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4932 /* G41, assume IVGA doesn't change */
4933 vid_handle = vid2_handle;
4934
4935 if (!vid_handle)
4936 /* video switching not supported on R30, R31 */
efa27145 4937 video_supported = TPACPI_VIDEO_NONE;
e28393c0
HMH
4938 else if (tpacpi_is_ibm() &&
4939 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
78f81cc4 4940 /* 570 */
efa27145 4941 video_supported = TPACPI_VIDEO_570;
e28393c0
HMH
4942 else if (tpacpi_is_ibm() &&
4943 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
78f81cc4 4944 /* 600e/x, 770e, 770x */
efa27145 4945 video_supported = TPACPI_VIDEO_770;
78f81cc4
BD
4946 else
4947 /* all others */
efa27145 4948 video_supported = TPACPI_VIDEO_NEW;
1da177e4 4949
fe08bc4b
HMH
4950 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4951 str_supported(video_supported != TPACPI_VIDEO_NONE),
4952 video_supported);
4953
72a979f0 4954 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
1da177e4
LT
4955}
4956
56b6aeb0
HMH
4957static void video_exit(void)
4958{
83f34724
HMH
4959 dbg_printk(TPACPI_DBG_EXIT,
4960 "restoring original video autoswitch mode\n");
4961 if (video_autosw_set(video_orig_autosw))
5cac62ac 4962 pr_err("error while trying to restore original video autoswitch mode\n");
56b6aeb0
HMH
4963}
4964
83f34724 4965static int video_outputsw_get(void)
1da177e4
LT
4966{
4967 int status = 0;
4968 int i;
4969
83f34724
HMH
4970 switch (video_supported) {
4971 case TPACPI_VIDEO_570:
4972 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4973 TP_ACPI_VIDEO_570_PHSCMD))
4974 return -EIO;
4975 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4976 break;
4977 case TPACPI_VIDEO_770:
4978 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4979 return -EIO;
4980 if (i)
4981 status |= TP_ACPI_VIDEO_S_LCD;
4982 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4983 return -EIO;
4984 if (i)
4985 status |= TP_ACPI_VIDEO_S_CRT;
4986 break;
4987 case TPACPI_VIDEO_NEW:
4988 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4989 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4990 return -EIO;
4991 if (i)
4992 status |= TP_ACPI_VIDEO_S_CRT;
4993
4994 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4995 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4996 return -EIO;
4997 if (i)
4998 status |= TP_ACPI_VIDEO_S_LCD;
4999 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
5000 return -EIO;
5001 if (i)
5002 status |= TP_ACPI_VIDEO_S_DVI;
5003 break;
5004 default:
5005 return -ENOSYS;
78f81cc4
BD
5006 }
5007
5008 return status;
5009}
1da177e4 5010
83f34724 5011static int video_outputsw_set(int status)
78f81cc4 5012{
83f34724
HMH
5013 int autosw;
5014 int res = 0;
5015
5016 switch (video_supported) {
5017 case TPACPI_VIDEO_570:
5018 res = acpi_evalf(NULL, NULL,
5019 "\\_SB.PHS2", "vdd",
5020 TP_ACPI_VIDEO_570_PHS2CMD,
5021 status | TP_ACPI_VIDEO_570_PHS2SET);
5022 break;
5023 case TPACPI_VIDEO_770:
5024 autosw = video_autosw_get();
5025 if (autosw < 0)
5026 return autosw;
1da177e4 5027
83f34724
HMH
5028 res = video_autosw_set(1);
5029 if (res)
5030 return res;
5031 res = acpi_evalf(vid_handle, NULL,
5032 "ASWT", "vdd", status * 0x100, 0);
5033 if (!autosw && video_autosw_set(autosw)) {
0978e012 5034 pr_err("video auto-switch left enabled due to error\n");
83f34724
HMH
5035 return -EIO;
5036 }
5037 break;
5038 case TPACPI_VIDEO_NEW:
5039 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
35ff8b9f 5040 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
83f34724
HMH
5041 break;
5042 default:
5043 return -ENOSYS;
5044 }
1da177e4 5045
72a979f0 5046 return (res) ? 0 : -EIO;
1da177e4
LT
5047}
5048
83f34724 5049static int video_autosw_get(void)
78f81cc4 5050{
83f34724 5051 int autosw = 0;
78f81cc4 5052
83f34724
HMH
5053 switch (video_supported) {
5054 case TPACPI_VIDEO_570:
5055 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5056 return -EIO;
5057 break;
5058 case TPACPI_VIDEO_770:
5059 case TPACPI_VIDEO_NEW:
5060 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5061 return -EIO;
5062 break;
5063 default:
5064 return -ENOSYS;
5065 }
78f81cc4 5066
83f34724 5067 return autosw & 1;
78f81cc4
BD
5068}
5069
83f34724 5070static int video_autosw_set(int enable)
78f81cc4 5071{
72a979f0 5072 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
83f34724
HMH
5073 return -EIO;
5074 return 0;
78f81cc4
BD
5075}
5076
83f34724 5077static int video_outputsw_cycle(void)
78f81cc4 5078{
83f34724
HMH
5079 int autosw = video_autosw_get();
5080 int res;
78f81cc4 5081
83f34724
HMH
5082 if (autosw < 0)
5083 return autosw;
78f81cc4 5084
83f34724
HMH
5085 switch (video_supported) {
5086 case TPACPI_VIDEO_570:
5087 res = video_autosw_set(1);
5088 if (res)
5089 return res;
5090 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5091 break;
5092 case TPACPI_VIDEO_770:
5093 case TPACPI_VIDEO_NEW:
5094 res = video_autosw_set(1);
5095 if (res)
5096 return res;
5097 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5098 break;
5099 default:
5100 return -ENOSYS;
5101 }
5102 if (!autosw && video_autosw_set(autosw)) {
0978e012 5103 pr_err("video auto-switch left enabled due to error\n");
83f34724 5104 return -EIO;
78f81cc4
BD
5105 }
5106
72a979f0 5107 return (res) ? 0 : -EIO;
83f34724
HMH
5108}
5109
5110static int video_expand_toggle(void)
5111{
5112 switch (video_supported) {
5113 case TPACPI_VIDEO_570:
72a979f0 5114 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
83f34724
HMH
5115 0 : -EIO;
5116 case TPACPI_VIDEO_770:
72a979f0 5117 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
83f34724
HMH
5118 0 : -EIO;
5119 case TPACPI_VIDEO_NEW:
72a979f0 5120 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
83f34724
HMH
5121 0 : -EIO;
5122 default:
5123 return -ENOSYS;
5124 }
5125 /* not reached */
78f81cc4
BD
5126}
5127
887965e6 5128static int video_read(struct seq_file *m)
56b6aeb0 5129{
83f34724 5130 int status, autosw;
56b6aeb0 5131
83f34724 5132 if (video_supported == TPACPI_VIDEO_NONE) {
887965e6
AD
5133 seq_printf(m, "status:\t\tnot supported\n");
5134 return 0;
56b6aeb0
HMH
5135 }
5136
b525c06c
HMH
5137 /* Even reads can crash X.org, so... */
5138 if (!capable(CAP_SYS_ADMIN))
5139 return -EPERM;
5140
83f34724
HMH
5141 status = video_outputsw_get();
5142 if (status < 0)
5143 return status;
5144
5145 autosw = video_autosw_get();
5146 if (autosw < 0)
5147 return autosw;
5148
887965e6
AD
5149 seq_printf(m, "status:\t\tsupported\n");
5150 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5151 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
efa27145 5152 if (video_supported == TPACPI_VIDEO_NEW)
887965e6
AD
5153 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5154 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5155 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5156 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
efa27145 5157 if (video_supported == TPACPI_VIDEO_NEW)
887965e6
AD
5158 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5159 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5160 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
56b6aeb0 5161
887965e6 5162 return 0;
56b6aeb0
HMH
5163}
5164
78f81cc4 5165static int video_write(char *buf)
1da177e4
LT
5166{
5167 char *cmd;
5168 int enable, disable, status;
83f34724 5169 int res;
1da177e4 5170
83f34724 5171 if (video_supported == TPACPI_VIDEO_NONE)
78f81cc4
BD
5172 return -ENODEV;
5173
b525c06c
HMH
5174 /* Even reads can crash X.org, let alone writes... */
5175 if (!capable(CAP_SYS_ADMIN))
5176 return -EPERM;
5177
83f34724
HMH
5178 enable = 0;
5179 disable = 0;
1da177e4
LT
5180
5181 while ((cmd = next_cmd(&buf))) {
5182 if (strlencmp(cmd, "lcd_enable") == 0) {
83f34724 5183 enable |= TP_ACPI_VIDEO_S_LCD;
1da177e4 5184 } else if (strlencmp(cmd, "lcd_disable") == 0) {
83f34724 5185 disable |= TP_ACPI_VIDEO_S_LCD;
1da177e4 5186 } else if (strlencmp(cmd, "crt_enable") == 0) {
83f34724 5187 enable |= TP_ACPI_VIDEO_S_CRT;
1da177e4 5188 } else if (strlencmp(cmd, "crt_disable") == 0) {
83f34724 5189 disable |= TP_ACPI_VIDEO_S_CRT;
efa27145 5190 } else if (video_supported == TPACPI_VIDEO_NEW &&
78f81cc4 5191 strlencmp(cmd, "dvi_enable") == 0) {
83f34724 5192 enable |= TP_ACPI_VIDEO_S_DVI;
efa27145 5193 } else if (video_supported == TPACPI_VIDEO_NEW &&
78f81cc4 5194 strlencmp(cmd, "dvi_disable") == 0) {
83f34724 5195 disable |= TP_ACPI_VIDEO_S_DVI;
1da177e4 5196 } else if (strlencmp(cmd, "auto_enable") == 0) {
83f34724
HMH
5197 res = video_autosw_set(1);
5198 if (res)
5199 return res;
1da177e4 5200 } else if (strlencmp(cmd, "auto_disable") == 0) {
83f34724
HMH
5201 res = video_autosw_set(0);
5202 if (res)
5203 return res;
1da177e4 5204 } else if (strlencmp(cmd, "video_switch") == 0) {
83f34724
HMH
5205 res = video_outputsw_cycle();
5206 if (res)
5207 return res;
1da177e4 5208 } else if (strlencmp(cmd, "expand_toggle") == 0) {
83f34724
HMH
5209 res = video_expand_toggle();
5210 if (res)
5211 return res;
1da177e4
LT
5212 } else
5213 return -EINVAL;
5214 }
5215
5216 if (enable || disable) {
83f34724
HMH
5217 status = video_outputsw_get();
5218 if (status < 0)
5219 return status;
5220 res = video_outputsw_set((status & ~disable) | enable);
5221 if (res)
5222 return res;
1da177e4
LT
5223 }
5224
5225 return 0;
5226}
5227
a5763f22
HMH
5228static struct ibm_struct video_driver_data = {
5229 .name = "video",
5230 .read = video_read,
5231 .write = video_write,
5232 .exit = video_exit,
5233};
5234
d7c1d17d
HMH
5235#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5236
bb28f3d5
PR
5237/*************************************************************************
5238 * Keyboard backlight subdriver
5239 */
5240
c685e20d
HG
5241static enum led_brightness kbdlight_brightness;
5242static DEFINE_MUTEX(kbdlight_mutex);
5243
bb28f3d5
PR
5244static int kbdlight_set_level(int level)
5245{
c685e20d
HG
5246 int ret = 0;
5247
bb28f3d5
PR
5248 if (!hkey_handle)
5249 return -ENXIO;
5250
c685e20d
HG
5251 mutex_lock(&kbdlight_mutex);
5252
bb28f3d5 5253 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
c685e20d
HG
5254 ret = -EIO;
5255 else
5256 kbdlight_brightness = level;
bb28f3d5 5257
c685e20d
HG
5258 mutex_unlock(&kbdlight_mutex);
5259
5260 return ret;
bb28f3d5
PR
5261}
5262
5263static int kbdlight_get_level(void)
5264{
5265 int status = 0;
5266
5267 if (!hkey_handle)
5268 return -ENXIO;
5269
5270 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5271 return -EIO;
5272
5273 if (status < 0)
5274 return status;
5275
5276 return status & 0x3;
5277}
5278
5279static bool kbdlight_is_supported(void)
5280{
5281 int status = 0;
5282
5283 if (!hkey_handle)
5284 return false;
5285
5286 if (!acpi_has_method(hkey_handle, "MLCG")) {
5287 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5288 return false;
5289 }
5290
5291 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5292 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5293 return false;
5294 }
5295
5296 if (status < 0) {
5297 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5298 return false;
5299 }
5300
5301 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5302 /*
5303 * Guessed test for keyboard backlight:
5304 *
5305 * Machines with backlight keyboard return:
5306 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5307 * b110100010010000000XX - ThinkPad x230
5308 * b010100000010000000XX - ThinkPad x240
5309 * b010100000010000000XX - ThinkPad W541
5310 * (XX is current backlight level)
5311 *
5312 * Machines without backlight keyboard return:
5313 * b10100001000000000000 - ThinkPad x230
5314 * b10110001000000000000 - ThinkPad E430
5315 * b00000000000000000000 - ThinkPad E450
5316 *
5317 * Candidate BITs for detection test (XOR):
5318 * b01000000001000000000
5319 * ^
5320 */
5321 return status & BIT(9);
5322}
5323
86ec0c2c 5324static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
bb28f3d5
PR
5325 enum led_brightness brightness)
5326{
86ec0c2c 5327 return kbdlight_set_level(brightness);
bb28f3d5
PR
5328}
5329
5330static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5331{
5332 int level;
5333
5334 level = kbdlight_get_level();
5335 if (level < 0)
5336 return 0;
5337
5338 return level;
5339}
5340
5341static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5342 .led_classdev = {
5343 .name = "tpacpi::kbd_backlight",
5344 .max_brightness = 2,
c685e20d 5345 .flags = LED_BRIGHT_HW_CHANGED,
86ec0c2c 5346 .brightness_set_blocking = &kbdlight_sysfs_set,
bb28f3d5 5347 .brightness_get = &kbdlight_sysfs_get,
bb28f3d5
PR
5348 }
5349};
5350
5351static int __init kbdlight_init(struct ibm_init_struct *iibm)
5352{
5353 int rc;
5354
5355 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5356
5357 TPACPI_ACPIHANDLE_INIT(hkey);
bb28f3d5
PR
5358
5359 if (!kbdlight_is_supported()) {
5360 tp_features.kbdlight = 0;
5361 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5362 return 1;
5363 }
5364
c685e20d 5365 kbdlight_brightness = kbdlight_sysfs_get(NULL);
bb28f3d5
PR
5366 tp_features.kbdlight = 1;
5367
5368 rc = led_classdev_register(&tpacpi_pdev->dev,
5369 &tpacpi_led_kbdlight.led_classdev);
5370 if (rc < 0) {
5371 tp_features.kbdlight = 0;
5372 return rc;
5373 }
5374
c685e20d
HG
5375 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5376 TP_ACPI_HKEY_KBD_LIGHT_MASK);
bb28f3d5
PR
5377 return 0;
5378}
5379
5380static void kbdlight_exit(void)
5381{
5382 if (tp_features.kbdlight)
5383 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
bb28f3d5
PR
5384}
5385
afcedebc
MTT
5386static int kbdlight_set_level_and_update(int level)
5387{
5388 int ret;
5389 struct led_classdev *led_cdev;
5390
5391 ret = kbdlight_set_level(level);
5392 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5393
5394 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5395 led_cdev->brightness = level;
5396
5397 return ret;
5398}
5399
bb28f3d5
PR
5400static int kbdlight_read(struct seq_file *m)
5401{
5402 int level;
5403
5404 if (!tp_features.kbdlight) {
5405 seq_printf(m, "status:\t\tnot supported\n");
5406 } else {
5407 level = kbdlight_get_level();
5408 if (level < 0)
5409 seq_printf(m, "status:\t\terror %d\n", level);
5410 else
5411 seq_printf(m, "status:\t\t%d\n", level);
5412 seq_printf(m, "commands:\t0, 1, 2\n");
5413 }
5414
5415 return 0;
5416}
5417
5418static int kbdlight_write(char *buf)
5419{
5420 char *cmd;
5421 int level = -1;
5422
5423 if (!tp_features.kbdlight)
5424 return -ENODEV;
5425
5426 while ((cmd = next_cmd(&buf))) {
5427 if (strlencmp(cmd, "0") == 0)
5428 level = 0;
5429 else if (strlencmp(cmd, "1") == 0)
5430 level = 1;
5431 else if (strlencmp(cmd, "2") == 0)
5432 level = 2;
5433 else
5434 return -EINVAL;
5435 }
5436
5437 if (level == -1)
5438 return -EINVAL;
5439
afcedebc
MTT
5440 return kbdlight_set_level_and_update(level);
5441}
5442
5443static void kbdlight_suspend(void)
5444{
5445 struct led_classdev *led_cdev;
5446
5447 if (!tp_features.kbdlight)
5448 return;
5449
5450 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5451 led_update_brightness(led_cdev);
5452 led_classdev_suspend(led_cdev);
5453}
5454
5455static void kbdlight_resume(void)
5456{
5457 if (!tp_features.kbdlight)
5458 return;
5459
5460 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
bb28f3d5
PR
5461}
5462
5463static struct ibm_struct kbdlight_driver_data = {
5464 .name = "kbdlight",
5465 .read = kbdlight_read,
5466 .write = kbdlight_write,
afcedebc
MTT
5467 .suspend = kbdlight_suspend,
5468 .resume = kbdlight_resume,
bb28f3d5
PR
5469 .exit = kbdlight_exit,
5470};
5471
56b6aeb0
HMH
5472/*************************************************************************
5473 * Light (thinklight) subdriver
5474 */
1da177e4 5475
e0c7dfe7
HMH
5476TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5477TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
56b6aeb0 5478
4fa6811b
HMH
5479static int light_get_status(void)
5480{
5481 int status = 0;
5482
5483 if (tp_features.light_status) {
5484 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5485 return -EIO;
5486 return (!!status);
5487 }
5488
5489 return -ENXIO;
5490}
5491
5492static int light_set_status(int status)
5493{
5494 int rc;
5495
5496 if (tp_features.light) {
5497 if (cmos_handle) {
5498 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
72a979f0 5499 (status) ?
4fa6811b
HMH
5500 TP_CMOS_THINKLIGHT_ON :
5501 TP_CMOS_THINKLIGHT_OFF);
5502 } else {
5503 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
72a979f0 5504 (status) ? 1 : 0);
4fa6811b 5505 }
72a979f0 5506 return (rc) ? 0 : -EIO;
4fa6811b
HMH
5507 }
5508
5509 return -ENXIO;
5510}
5511
86ec0c2c 5512static int light_sysfs_set(struct led_classdev *led_cdev,
e306501d
HMH
5513 enum led_brightness brightness)
5514{
86ec0c2c
HG
5515 return light_set_status((brightness != LED_OFF) ?
5516 TPACPI_LED_ON : TPACPI_LED_OFF);
e306501d
HMH
5517}
5518
5519static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5520{
72a979f0 5521 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
e306501d
HMH
5522}
5523
5524static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5525 .led_classdev = {
5526 .name = "tpacpi::thinklight",
86ec0c2c 5527 .brightness_set_blocking = &light_sysfs_set,
e306501d
HMH
5528 .brightness_get = &light_sysfs_get,
5529 }
5530};
5531
a5763f22 5532static int __init light_init(struct ibm_init_struct *iibm)
1da177e4 5533{
9c0a76e1 5534 int rc;
e306501d 5535
fe08bc4b
HMH
5536 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5537
e28393c0
HMH
5538 if (tpacpi_is_ibm()) {
5539 TPACPI_ACPIHANDLE_INIT(ledb);
5540 TPACPI_ACPIHANDLE_INIT(lght);
5541 }
e0c7dfe7 5542 TPACPI_ACPIHANDLE_INIT(cmos);
5fba344c 5543
78f81cc4 5544 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
d8fd94d9 5545 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
78f81cc4 5546
d8fd94d9 5547 if (tp_features.light)
78f81cc4
BD
5548 /* light status not supported on
5549 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
d8fd94d9
HMH
5550 tp_features.light_status =
5551 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1da177e4 5552
9c0a76e1
HMH
5553 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5554 str_supported(tp_features.light),
5555 str_supported(tp_features.light_status));
fe08bc4b 5556
9c0a76e1
HMH
5557 if (!tp_features.light)
5558 return 1;
5559
5560 rc = led_classdev_register(&tpacpi_pdev->dev,
5561 &tpacpi_led_thinklight.led_classdev);
e306501d
HMH
5562
5563 if (rc < 0) {
5564 tp_features.light = 0;
5565 tp_features.light_status = 0;
9c0a76e1
HMH
5566 } else {
5567 rc = 0;
e306501d 5568 }
9c0a76e1 5569
e306501d
HMH
5570 return rc;
5571}
5572
5573static void light_exit(void)
5574{
5575 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
1da177e4
LT
5576}
5577
887965e6 5578static int light_read(struct seq_file *m)
1da177e4 5579{
4fa6811b 5580 int status;
1da177e4 5581
d8fd94d9 5582 if (!tp_features.light) {
887965e6 5583 seq_printf(m, "status:\t\tnot supported\n");
d8fd94d9 5584 } else if (!tp_features.light_status) {
887965e6
AD
5585 seq_printf(m, "status:\t\tunknown\n");
5586 seq_printf(m, "commands:\ton, off\n");
78f81cc4 5587 } else {
4fa6811b
HMH
5588 status = light_get_status();
5589 if (status < 0)
5590 return status;
887965e6
AD
5591 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5592 seq_printf(m, "commands:\ton, off\n");
78f81cc4 5593 }
1da177e4 5594
887965e6 5595 return 0;
1da177e4
LT
5596}
5597
78f81cc4 5598static int light_write(char *buf)
1da177e4 5599{
1da177e4 5600 char *cmd;
4fa6811b 5601 int newstatus = 0;
78f81cc4 5602
d8fd94d9 5603 if (!tp_features.light)
78f81cc4
BD
5604 return -ENODEV;
5605
1da177e4
LT
5606 while ((cmd = next_cmd(&buf))) {
5607 if (strlencmp(cmd, "on") == 0) {
4fa6811b 5608 newstatus = 1;
1da177e4 5609 } else if (strlencmp(cmd, "off") == 0) {
4fa6811b 5610 newstatus = 0;
1da177e4
LT
5611 } else
5612 return -EINVAL;
1da177e4
LT
5613 }
5614
4fa6811b 5615 return light_set_status(newstatus);
1da177e4
LT
5616}
5617
a5763f22
HMH
5618static struct ibm_struct light_driver_data = {
5619 .name = "light",
5620 .read = light_read,
5621 .write = light_write,
e306501d 5622 .exit = light_exit,
a5763f22
HMH
5623};
5624
56b6aeb0
HMH
5625/*************************************************************************
5626 * CMOS subdriver
5627 */
5628
b616004c
HMH
5629/* sysfs cmos_command -------------------------------------------------- */
5630static ssize_t cmos_command_store(struct device *dev,
5631 struct device_attribute *attr,
5632 const char *buf, size_t count)
5633{
5634 unsigned long cmos_cmd;
5635 int res;
5636
5637 if (parse_strtoul(buf, 21, &cmos_cmd))
5638 return -EINVAL;
5639
5640 res = issue_thinkpad_cmos_command(cmos_cmd);
72a979f0 5641 return (res) ? res : count;
b616004c
HMH
5642}
5643
b4dd04ac 5644static DEVICE_ATTR_WO(cmos_command);
b616004c
HMH
5645
5646/* --------------------------------------------------------------------- */
5647
a5763f22 5648static int __init cmos_init(struct ibm_init_struct *iibm)
5fba344c 5649{
b616004c
HMH
5650 int res;
5651
fe08bc4b
HMH
5652 vdbg_printk(TPACPI_DBG_INIT,
5653 "initializing cmos commands subdriver\n");
5654
e0c7dfe7 5655 TPACPI_ACPIHANDLE_INIT(cmos);
5fba344c 5656
fe08bc4b
HMH
5657 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5658 str_supported(cmos_handle != NULL));
b616004c
HMH
5659
5660 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5661 if (res)
5662 return res;
5663
72a979f0 5664 return (cmos_handle) ? 0 : 1;
5fba344c
HMH
5665}
5666
b616004c
HMH
5667static void cmos_exit(void)
5668{
5669 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5670}
5671
887965e6 5672static int cmos_read(struct seq_file *m)
1da177e4 5673{
78f81cc4
BD
5674 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5675 R30, R31, T20-22, X20-21 */
1da177e4 5676 if (!cmos_handle)
887965e6 5677 seq_printf(m, "status:\t\tnot supported\n");
1da177e4 5678 else {
887965e6
AD
5679 seq_printf(m, "status:\t\tsupported\n");
5680 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
1da177e4
LT
5681 }
5682
887965e6 5683 return 0;
1da177e4
LT
5684}
5685
78f81cc4 5686static int cmos_write(char *buf)
1da177e4
LT
5687{
5688 char *cmd;
c9bea99c 5689 int cmos_cmd, res;
1da177e4
LT
5690
5691 while ((cmd = next_cmd(&buf))) {
78f81cc4
BD
5692 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5693 cmos_cmd >= 0 && cmos_cmd <= 21) {
1da177e4
LT
5694 /* cmos_cmd set */
5695 } else
5696 return -EINVAL;
5697
c9bea99c
HMH
5698 res = issue_thinkpad_cmos_command(cmos_cmd);
5699 if (res)
5700 return res;
1da177e4
LT
5701 }
5702
5703 return 0;
78f81cc4
BD
5704}
5705
a5763f22
HMH
5706static struct ibm_struct cmos_driver_data = {
5707 .name = "cmos",
5708 .read = cmos_read,
5709 .write = cmos_write,
b616004c 5710 .exit = cmos_exit,
a5763f22 5711};
56b6aeb0
HMH
5712
5713/*************************************************************************
5714 * LED subdriver
5715 */
5716
f74a27d4
HMH
5717enum led_access_mode {
5718 TPACPI_LED_NONE = 0,
5719 TPACPI_LED_570, /* 570 */
5720 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5721 TPACPI_LED_NEW, /* all others */
5722};
5723
5724enum { /* For TPACPI_LED_OLD */
5725 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5726 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5727 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5728};
5729
9a8e1738 5730static enum led_access_mode led_supported;
78f81cc4 5731
2cbb5c8f 5732static acpi_handle led_handle;
56b6aeb0 5733
f21179a4 5734#define TPACPI_LED_NUMLEDS 16
af116101
HMH
5735static struct tpacpi_led_classdev *tpacpi_leds;
5736static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
e3aa51fe 5737static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
af116101
HMH
5738 /* there's a limit of 19 chars + NULL before 2.6.26 */
5739 "tpacpi::power",
5740 "tpacpi:orange:batt",
5741 "tpacpi:green:batt",
5742 "tpacpi::dock_active",
5743 "tpacpi::bay_active",
5744 "tpacpi::dock_batt",
5745 "tpacpi::unknown_led",
5746 "tpacpi::standby",
f21179a4
HMH
5747 "tpacpi::dock_status1",
5748 "tpacpi::dock_status2",
5749 "tpacpi::unknown_led2",
5750 "tpacpi::unknown_led3",
5751 "tpacpi::thinkvantage",
af116101 5752};
f21179a4 5753#define TPACPI_SAFE_LEDS 0x1081U
a4d5effc
HMH
5754
5755static inline bool tpacpi_is_led_restricted(const unsigned int led)
5756{
5757#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5758 return false;
5759#else
f21179a4 5760 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
a4d5effc
HMH
5761#endif
5762}
af116101 5763
24e45bbe 5764static int led_get_status(const unsigned int led)
4fa6811b
HMH
5765{
5766 int status;
af116101 5767 enum led_status_t led_s;
4fa6811b
HMH
5768
5769 switch (led_supported) {
5770 case TPACPI_LED_570:
5771 if (!acpi_evalf(ec_handle,
5772 &status, "GLED", "dd", 1 << led))
5773 return -EIO;
72a979f0 5774 led_s = (status == 0) ?
4fa6811b 5775 TPACPI_LED_OFF :
72a979f0 5776 ((status == 1) ?
4fa6811b
HMH
5777 TPACPI_LED_ON :
5778 TPACPI_LED_BLINK);
af116101
HMH
5779 tpacpi_led_state_cache[led] = led_s;
5780 return led_s;
4fa6811b
HMH
5781 default:
5782 return -ENXIO;
5783 }
5784
5785 /* not reached */
5786}
5787
24e45bbe
HMH
5788static int led_set_status(const unsigned int led,
5789 const enum led_status_t ledstatus)
4fa6811b
HMH
5790{
5791 /* off, on, blink. Index is led_status_t */
24e45bbe
HMH
5792 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5793 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4fa6811b
HMH
5794
5795 int rc = 0;
5796
5797 switch (led_supported) {
5798 case TPACPI_LED_570:
24e45bbe 5799 /* 570 */
a4d5effc 5800 if (unlikely(led > 7))
24e45bbe 5801 return -EINVAL;
a4d5effc
HMH
5802 if (unlikely(tpacpi_is_led_restricted(led)))
5803 return -EPERM;
24e45bbe
HMH
5804 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5805 (1 << led), led_sled_arg1[ledstatus]))
5806 rc = -EIO;
5807 break;
4fa6811b 5808 case TPACPI_LED_OLD:
24e45bbe 5809 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
a4d5effc 5810 if (unlikely(led > 7))
24e45bbe 5811 return -EINVAL;
a4d5effc
HMH
5812 if (unlikely(tpacpi_is_led_restricted(led)))
5813 return -EPERM;
24e45bbe
HMH
5814 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5815 if (rc >= 0)
5816 rc = ec_write(TPACPI_LED_EC_HLBL,
5817 (ledstatus == TPACPI_LED_BLINK) << led);
5818 if (rc >= 0)
5819 rc = ec_write(TPACPI_LED_EC_HLCL,
5820 (ledstatus != TPACPI_LED_OFF) << led);
5821 break;
4fa6811b 5822 case TPACPI_LED_NEW:
24e45bbe 5823 /* all others */
a4d5effc
HMH
5824 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5825 return -EINVAL;
5826 if (unlikely(tpacpi_is_led_restricted(led)))
5827 return -EPERM;
24e45bbe
HMH
5828 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5829 led, led_led_arg1[ledstatus]))
5830 rc = -EIO;
5831 break;
4fa6811b
HMH
5832 default:
5833 rc = -ENXIO;
5834 }
5835
af116101
HMH
5836 if (!rc)
5837 tpacpi_led_state_cache[led] = ledstatus;
5838
5839 return rc;
5840}
5841
86ec0c2c 5842static int led_sysfs_set(struct led_classdev *led_cdev,
af116101
HMH
5843 enum led_brightness brightness)
5844{
5845 struct tpacpi_led_classdev *data = container_of(led_cdev,
5846 struct tpacpi_led_classdev, led_classdev);
86ec0c2c 5847 enum led_status_t new_state;
af116101 5848
75bd3bf2 5849 if (brightness == LED_OFF)
86ec0c2c 5850 new_state = TPACPI_LED_OFF;
75bd3bf2 5851 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
86ec0c2c 5852 new_state = TPACPI_LED_ON;
75bd3bf2 5853 else
86ec0c2c 5854 new_state = TPACPI_LED_BLINK;
75bd3bf2 5855
86ec0c2c 5856 return led_set_status(data->led, new_state);
af116101
HMH
5857}
5858
5859static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5860 unsigned long *delay_on, unsigned long *delay_off)
5861{
5862 struct tpacpi_led_classdev *data = container_of(led_cdev,
5863 struct tpacpi_led_classdev, led_classdev);
5864
5865 /* Can we choose the flash rate? */
5866 if (*delay_on == 0 && *delay_off == 0) {
5867 /* yes. set them to the hardware blink rate (1 Hz) */
5868 *delay_on = 500; /* ms */
5869 *delay_off = 500; /* ms */
5870 } else if ((*delay_on != 500) || (*delay_off != 500))
5871 return -EINVAL;
5872
86ec0c2c 5873 return led_set_status(data->led, TPACPI_LED_BLINK);
af116101
HMH
5874}
5875
5876static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5877{
5878 int rc;
5879
5880 struct tpacpi_led_classdev *data = container_of(led_cdev,
5881 struct tpacpi_led_classdev, led_classdev);
5882
5883 rc = led_get_status(data->led);
5884
5885 if (rc == TPACPI_LED_OFF || rc < 0)
5886 rc = LED_OFF; /* no error handling in led class :( */
5887 else
5888 rc = LED_FULL;
5889
4fa6811b
HMH
5890 return rc;
5891}
5892
af116101
HMH
5893static void led_exit(void)
5894{
5895 unsigned int i;
5896
5897 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5898 if (tpacpi_leds[i].led_classdev.name)
5899 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5900 }
5901
5902 kfree(tpacpi_leds);
af116101
HMH
5903}
5904
a4d5effc
HMH
5905static int __init tpacpi_init_led(unsigned int led)
5906{
5907 int rc;
5908
5909 tpacpi_leds[led].led = led;
5910
f21179a4
HMH
5911 /* LEDs with no name don't get registered */
5912 if (!tpacpi_led_names[led])
5913 return 0;
5914
86ec0c2c 5915 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
a4d5effc
HMH
5916 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5917 if (led_supported == TPACPI_LED_570)
5918 tpacpi_leds[led].led_classdev.brightness_get =
5919 &led_sysfs_get;
5920
5921 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5922
a4d5effc
HMH
5923 rc = led_classdev_register(&tpacpi_pdev->dev,
5924 &tpacpi_leds[led].led_classdev);
5925 if (rc < 0)
5926 tpacpi_leds[led].led_classdev.name = NULL;
5927
5928 return rc;
5929}
5930
f21179a4
HMH
5931static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5932 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5933 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5934 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5935
5936 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5937 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5938 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5939 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5940 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5941 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5942 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5943 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5944
5945 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5946 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5947 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5948 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5949 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5950
5951 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5952 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5953 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5954 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5955
5956 /* (1) - may have excess leds enabled on MSB */
5957
5958 /* Defaults (order matters, keep last, don't reorder!) */
5959 { /* Lenovo */
5960 .vendor = PCI_VENDOR_ID_LENOVO,
5961 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5962 .quirks = 0x1fffU,
5963 },
5964 { /* IBM ThinkPads with no EC version string */
5965 .vendor = PCI_VENDOR_ID_IBM,
5966 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5967 .quirks = 0x00ffU,
5968 },
5969 { /* IBM ThinkPads with EC version string */
5970 .vendor = PCI_VENDOR_ID_IBM,
5971 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5972 .quirks = 0x00bfU,
5973 },
5974};
5975
5976#undef TPACPI_LEDQ_IBM
5977#undef TPACPI_LEDQ_LNV
5978
2cbb5c8f
HMH
5979static enum led_access_mode __init led_init_detect_mode(void)
5980{
5981 acpi_status status;
5982
5983 if (tpacpi_is_ibm()) {
5984 /* 570 */
5985 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5986 if (ACPI_SUCCESS(status))
5987 return TPACPI_LED_570;
5988
5989 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5990 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5991 if (ACPI_SUCCESS(status))
5992 return TPACPI_LED_OLD;
5993 }
5994
5995 /* most others */
5996 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5997 if (ACPI_SUCCESS(status))
5998 return TPACPI_LED_NEW;
5999
6000 /* R30, R31, and unknown firmwares */
6001 led_handle = NULL;
6002 return TPACPI_LED_NONE;
6003}
6004
a5763f22 6005static int __init led_init(struct ibm_init_struct *iibm)
78f81cc4 6006{
af116101
HMH
6007 unsigned int i;
6008 int rc;
f21179a4 6009 unsigned long useful_leds;
af116101 6010
fe08bc4b
HMH
6011 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
6012
2cbb5c8f 6013 led_supported = led_init_detect_mode();
78f81cc4 6014
fcb44e12
AL
6015 if (led_supported != TPACPI_LED_NONE) {
6016 useful_leds = tpacpi_check_quirks(led_useful_qtable,
6017 ARRAY_SIZE(led_useful_qtable));
6018
6019 if (!useful_leds) {
6020 led_handle = NULL;
6021 led_supported = TPACPI_LED_NONE;
6022 }
6023 }
6024
fe08bc4b
HMH
6025 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
6026 str_supported(led_supported), led_supported);
6027
f21179a4
HMH
6028 if (led_supported == TPACPI_LED_NONE)
6029 return 1;
6030
6396bb22 6031 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
af116101
HMH
6032 GFP_KERNEL);
6033 if (!tpacpi_leds) {
0978e012 6034 pr_err("Out of memory for LED data\n");
af116101
HMH
6035 return -ENOMEM;
6036 }
6037
6038 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
edf2d778
AL
6039 tpacpi_leds[i].led = -1;
6040
f21179a4
HMH
6041 if (!tpacpi_is_led_restricted(i) &&
6042 test_bit(i, &useful_leds)) {
a4d5effc
HMH
6043 rc = tpacpi_init_led(i);
6044 if (rc < 0) {
6045 led_exit();
6046 return rc;
6047 }
af116101
HMH
6048 }
6049 }
6050
a4d5effc 6051#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5cac62ac 6052 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
a4d5effc 6053#endif
f21179a4 6054 return 0;
78f81cc4
BD
6055}
6056
4fa6811b
HMH
6057#define str_led_status(s) \
6058 ((s) == TPACPI_LED_OFF ? "off" : \
6059 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
78f81cc4 6060
887965e6 6061static int led_read(struct seq_file *m)
1da177e4 6062{
78f81cc4 6063 if (!led_supported) {
887965e6
AD
6064 seq_printf(m, "status:\t\tnot supported\n");
6065 return 0;
78f81cc4 6066 }
887965e6 6067 seq_printf(m, "status:\t\tsupported\n");
78f81cc4 6068
efa27145 6069 if (led_supported == TPACPI_LED_570) {
78f81cc4
BD
6070 /* 570 */
6071 int i, status;
6072 for (i = 0; i < 8; i++) {
4fa6811b
HMH
6073 status = led_get_status(i);
6074 if (status < 0)
78f81cc4 6075 return -EIO;
887965e6 6076 seq_printf(m, "%d:\t\t%s\n",
4fa6811b 6077 i, str_led_status(status));
78f81cc4
BD
6078 }
6079 }
6080
5cac62ac 6081 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
1da177e4 6082
887965e6 6083 return 0;
1da177e4
LT
6084}
6085
78f81cc4 6086static int led_write(char *buf)
1da177e4
LT
6087{
6088 char *cmd;
4fa6811b
HMH
6089 int led, rc;
6090 enum led_status_t s;
78f81cc4
BD
6091
6092 if (!led_supported)
6093 return -ENODEV;
1da177e4
LT
6094
6095 while ((cmd = next_cmd(&buf))) {
edf2d778 6096 if (sscanf(cmd, "%d", &led) != 1)
1da177e4
LT
6097 return -EINVAL;
6098
edf2d778
AL
6099 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
6100 tpacpi_leds[led].led < 0)
6101 return -ENODEV;
6102
78f81cc4 6103 if (strstr(cmd, "off")) {
4fa6811b 6104 s = TPACPI_LED_OFF;
1da177e4 6105 } else if (strstr(cmd, "on")) {
4fa6811b 6106 s = TPACPI_LED_ON;
78f81cc4 6107 } else if (strstr(cmd, "blink")) {
4fa6811b 6108 s = TPACPI_LED_BLINK;
78f81cc4 6109 } else {
4fa6811b 6110 return -EINVAL;
78f81cc4 6111 }
4fa6811b
HMH
6112
6113 rc = led_set_status(led, s);
6114 if (rc < 0)
6115 return rc;
78f81cc4
BD
6116 }
6117
6118 return 0;
6119}
6120
a5763f22
HMH
6121static struct ibm_struct led_driver_data = {
6122 .name = "led",
6123 .read = led_read,
6124 .write = led_write,
af116101 6125 .exit = led_exit,
a5763f22
HMH
6126};
6127
56b6aeb0
HMH
6128/*************************************************************************
6129 * Beep subdriver
6130 */
6131
e0c7dfe7 6132TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
56b6aeb0 6133
60201732
HMH
6134#define TPACPI_BEEP_Q1 0x0001
6135
6136static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6137 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6138 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6139};
6140
a5763f22 6141static int __init beep_init(struct ibm_init_struct *iibm)
5fba344c 6142{
60201732
HMH
6143 unsigned long quirks;
6144
fe08bc4b
HMH
6145 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6146
e0c7dfe7 6147 TPACPI_ACPIHANDLE_INIT(beep);
5fba344c 6148
fe08bc4b
HMH
6149 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6150 str_supported(beep_handle != NULL));
6151
60201732
HMH
6152 quirks = tpacpi_check_quirks(beep_quirk_table,
6153 ARRAY_SIZE(beep_quirk_table));
6154
6155 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6156
72a979f0 6157 return (beep_handle) ? 0 : 1;
5fba344c
HMH
6158}
6159
887965e6 6160static int beep_read(struct seq_file *m)
78f81cc4 6161{
78f81cc4 6162 if (!beep_handle)
887965e6 6163 seq_printf(m, "status:\t\tnot supported\n");
78f81cc4 6164 else {
887965e6
AD
6165 seq_printf(m, "status:\t\tsupported\n");
6166 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
78f81cc4
BD
6167 }
6168
887965e6 6169 return 0;
78f81cc4
BD
6170}
6171
6172static int beep_write(char *buf)
6173{
6174 char *cmd;
6175 int beep_cmd;
6176
6177 if (!beep_handle)
6178 return -ENODEV;
6179
6180 while ((cmd = next_cmd(&buf))) {
6181 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6182 beep_cmd >= 0 && beep_cmd <= 17) {
6183 /* beep_cmd set */
6184 } else
6185 return -EINVAL;
60201732
HMH
6186 if (tp_features.beep_needs_two_args) {
6187 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6188 beep_cmd, 0))
6189 return -EIO;
6190 } else {
6191 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6192 beep_cmd))
6193 return -EIO;
6194 }
78f81cc4
BD
6195 }
6196
6197 return 0;
6198}
6199
a5763f22
HMH
6200static struct ibm_struct beep_driver_data = {
6201 .name = "beep",
6202 .read = beep_read,
6203 .write = beep_write,
6204};
6205
56b6aeb0
HMH
6206/*************************************************************************
6207 * Thermal subdriver
6208 */
78f81cc4 6209
f74a27d4
HMH
6210enum thermal_access_mode {
6211 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6212 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6213 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6214 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6215 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6216};
6217
6218enum { /* TPACPI_THERMAL_TPEC_* */
6219 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6220 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6221 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
9ebd9e83
HMH
6222
6223 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
f74a27d4
HMH
6224};
6225
9ebd9e83 6226
f74a27d4
HMH
6227#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6228struct ibm_thermal_sensors_struct {
6229 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6230};
6231
a26f878a 6232static enum thermal_access_mode thermal_read_mode;
78f81cc4 6233
b21a15f6
HMH
6234/* idx is zero-based */
6235static int thermal_get_sensor(int idx, s32 *value)
6236{
6237 int t;
6238 s8 tmp;
6239 char tmpi[5];
6240
6241 t = TP_EC_THERMAL_TMP0;
6242
6243 switch (thermal_read_mode) {
6244#if TPACPI_MAX_THERMAL_SENSORS >= 16
6245 case TPACPI_THERMAL_TPEC_16:
6246 if (idx >= 8 && idx <= 15) {
6247 t = TP_EC_THERMAL_TMP8;
6248 idx -= 8;
6249 }
6250 /* fallthrough */
6251#endif
6252 case TPACPI_THERMAL_TPEC_8:
6253 if (idx <= 7) {
6254 if (!acpi_ec_read(t + idx, &tmp))
6255 return -EIO;
6256 *value = tmp * 1000;
6257 return 0;
6258 }
6259 break;
6260
6261 case TPACPI_THERMAL_ACPI_UPDT:
6262 if (idx <= 7) {
6263 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6264 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6265 return -EIO;
6266 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6267 return -EIO;
6268 *value = (t - 2732) * 100;
6269 return 0;
6270 }
6271 break;
6272
6273 case TPACPI_THERMAL_ACPI_TMP07:
6274 if (idx <= 7) {
6275 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6276 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6277 return -EIO;
6278 if (t > 127 || t < -127)
6279 t = TP_EC_THERMAL_TMP_NA;
6280 *value = t * 1000;
6281 return 0;
6282 }
6283 break;
6284
6285 case TPACPI_THERMAL_NONE:
6286 default:
6287 return -ENOSYS;
6288 }
6289
6290 return -EINVAL;
6291}
6292
6293static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6294{
6295 int res, i;
6296 int n;
6297
6298 n = 8;
6299 i = 0;
6300
6301 if (!s)
6302 return -EINVAL;
6303
6304 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6305 n = 16;
6306
35ff8b9f 6307 for (i = 0 ; i < n; i++) {
b21a15f6
HMH
6308 res = thermal_get_sensor(i, &s->temp[i]);
6309 if (res)
6310 return res;
6311 }
6312
6313 return n;
6314}
f74a27d4 6315
9ebd9e83
HMH
6316static void thermal_dump_all_sensors(void)
6317{
6318 int n, i;
6319 struct ibm_thermal_sensors_struct t;
6320
6321 n = thermal_get_sensors(&t);
6322 if (n <= 0)
6323 return;
6324
0978e012 6325 pr_notice("temperatures (Celsius):");
9ebd9e83
HMH
6326
6327 for (i = 0; i < n; i++) {
6328 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
0978e012 6329 pr_cont(" %d", (int)(t.temp[i] / 1000));
9ebd9e83 6330 else
0978e012 6331 pr_cont(" N/A");
9ebd9e83
HMH
6332 }
6333
0978e012 6334 pr_cont("\n");
9ebd9e83
HMH
6335}
6336
2c37aa4e
HMH
6337/* sysfs temp##_input -------------------------------------------------- */
6338
6339static ssize_t thermal_temp_input_show(struct device *dev,
6340 struct device_attribute *attr,
6341 char *buf)
6342{
6343 struct sensor_device_attribute *sensor_attr =
6344 to_sensor_dev_attr(attr);
6345 int idx = sensor_attr->index;
6346 s32 value;
6347 int res;
6348
6349 res = thermal_get_sensor(idx, &value);
6350 if (res)
6351 return res;
9ebd9e83 6352 if (value == TPACPI_THERMAL_SENSOR_NA)
2c37aa4e
HMH
6353 return -ENXIO;
6354
6355 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6356}
6357
6358#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
35ff8b9f
HMH
6359 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6360 thermal_temp_input_show, NULL, _idxB)
2c37aa4e
HMH
6361
6362static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6363 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6364 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6365 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6366 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6367 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6368 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6369 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6370 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6371 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6372 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6373 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6374 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6375 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6376 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6377 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6378 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6379};
6380
6381#define THERMAL_ATTRS(X) \
6382 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6383
6384static struct attribute *thermal_temp_input_attr[] = {
6385 THERMAL_ATTRS(8),
6386 THERMAL_ATTRS(9),
6387 THERMAL_ATTRS(10),
6388 THERMAL_ATTRS(11),
6389 THERMAL_ATTRS(12),
6390 THERMAL_ATTRS(13),
6391 THERMAL_ATTRS(14),
6392 THERMAL_ATTRS(15),
6393 THERMAL_ATTRS(0),
6394 THERMAL_ATTRS(1),
6395 THERMAL_ATTRS(2),
6396 THERMAL_ATTRS(3),
6397 THERMAL_ATTRS(4),
6398 THERMAL_ATTRS(5),
6399 THERMAL_ATTRS(6),
6400 THERMAL_ATTRS(7),
6401 NULL
6402};
6403
6404static const struct attribute_group thermal_temp_input16_group = {
6405 .attrs = thermal_temp_input_attr
6406};
6407
6408static const struct attribute_group thermal_temp_input8_group = {
6409 .attrs = &thermal_temp_input_attr[8]
6410};
6411
6412#undef THERMAL_SENSOR_ATTR_TEMP
6413#undef THERMAL_ATTRS
6414
6415/* --------------------------------------------------------------------- */
6416
a5763f22 6417static int __init thermal_init(struct ibm_init_struct *iibm)
78f81cc4 6418{
60eb0b35
HMH
6419 u8 t, ta1, ta2;
6420 int i;
5fba344c 6421 int acpi_tmp7;
2c37aa4e 6422 int res;
5fba344c 6423
fe08bc4b
HMH
6424 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6425
5fba344c 6426 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
60eb0b35 6427
3d6f99ca 6428 if (thinkpad_id.ec_model) {
60eb0b35
HMH
6429 /*
6430 * Direct EC access mode: sensors at registers
6431 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6432 * non-implemented, thermal sensors return 0x80 when
6433 * not available
6434 */
78f81cc4 6435
60eb0b35
HMH
6436 ta1 = ta2 = 0;
6437 for (i = 0; i < 8; i++) {
04cc862c 6438 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
60eb0b35
HMH
6439 ta1 |= t;
6440 } else {
6441 ta1 = 0;
6442 break;
6443 }
04cc862c 6444 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
60eb0b35
HMH
6445 ta2 |= t;
6446 } else {
6447 ta1 = 0;
6448 break;
6449 }
6450 }
6451 if (ta1 == 0) {
6452 /* This is sheer paranoia, but we handle it anyway */
6453 if (acpi_tmp7) {
5cac62ac 6454 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
efa27145 6455 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
60eb0b35 6456 } else {
5cac62ac 6457 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
efa27145 6458 thermal_read_mode = TPACPI_THERMAL_NONE;
60eb0b35
HMH
6459 }
6460 } else {
6461 thermal_read_mode =
6462 (ta2 != 0) ?
efa27145 6463 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
60eb0b35
HMH
6464 }
6465 } else if (acpi_tmp7) {
e28393c0
HMH
6466 if (tpacpi_is_ibm() &&
6467 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
a26f878a 6468 /* 600e/x, 770e, 770x */
efa27145 6469 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
a26f878a 6470 } else {
e28393c0 6471 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
efa27145 6472 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
a26f878a
HMH
6473 }
6474 } else {
6475 /* temperatures not supported on 570, G4x, R30, R31, R32 */
efa27145 6476 thermal_read_mode = TPACPI_THERMAL_NONE;
a26f878a 6477 }
78f81cc4 6478
fe08bc4b
HMH
6479 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6480 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6481 thermal_read_mode);
6482
35ff8b9f 6483 switch (thermal_read_mode) {
2c37aa4e 6484 case TPACPI_THERMAL_TPEC_16:
6b99e356 6485 res = sysfs_create_group(&tpacpi_hwmon->kobj,
2c37aa4e
HMH
6486 &thermal_temp_input16_group);
6487 if (res)
6488 return res;
6489 break;
6490 case TPACPI_THERMAL_TPEC_8:
6491 case TPACPI_THERMAL_ACPI_TMP07:
6492 case TPACPI_THERMAL_ACPI_UPDT:
6b99e356 6493 res = sysfs_create_group(&tpacpi_hwmon->kobj,
2c37aa4e
HMH
6494 &thermal_temp_input8_group);
6495 if (res)
6496 return res;
6497 break;
6498 case TPACPI_THERMAL_NONE:
6499 default:
6500 return 1;
6501 }
6502
6503 return 0;
6504}
6505
6506static void thermal_exit(void)
6507{
35ff8b9f 6508 switch (thermal_read_mode) {
2c37aa4e 6509 case TPACPI_THERMAL_TPEC_16:
6b99e356 6510 sysfs_remove_group(&tpacpi_hwmon->kobj,
2c37aa4e
HMH
6511 &thermal_temp_input16_group);
6512 break;
6513 case TPACPI_THERMAL_TPEC_8:
6514 case TPACPI_THERMAL_ACPI_TMP07:
6515 case TPACPI_THERMAL_ACPI_UPDT:
6b99e356 6516 sysfs_remove_group(&tpacpi_hwmon->kobj,
f04d5e01 6517 &thermal_temp_input8_group);
2c37aa4e
HMH
6518 break;
6519 case TPACPI_THERMAL_NONE:
6520 default:
6521 break;
6522 }
78f81cc4
BD
6523}
6524
887965e6 6525static int thermal_read(struct seq_file *m)
a26f878a 6526{
a26f878a
HMH
6527 int n, i;
6528 struct ibm_thermal_sensors_struct t;
6529
6530 n = thermal_get_sensors(&t);
6531 if (unlikely(n < 0))
6532 return n;
6533
887965e6 6534 seq_printf(m, "temperatures:\t");
a26f878a
HMH
6535
6536 if (n > 0) {
6537 for (i = 0; i < (n - 1); i++)
887965e6
AD
6538 seq_printf(m, "%d ", t.temp[i] / 1000);
6539 seq_printf(m, "%d\n", t.temp[i] / 1000);
a26f878a 6540 } else
887965e6 6541 seq_printf(m, "not supported\n");
78f81cc4 6542
887965e6 6543 return 0;
78f81cc4
BD
6544}
6545
a5763f22
HMH
6546static struct ibm_struct thermal_driver_data = {
6547 .name = "thermal",
6548 .read = thermal_read,
2c37aa4e 6549 .exit = thermal_exit,
a5763f22
HMH
6550};
6551
56b6aeb0
HMH
6552/*************************************************************************
6553 * Backlight/brightness subdriver
6554 */
6555
f74a27d4
HMH
6556#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6557
0e501834
HMH
6558/*
6559 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6560 * CMOS NVRAM byte 0x5E, bits 0-3.
6561 *
6562 * EC HBRV (0x31) has the following layout
6563 * Bit 7: unknown function
6564 * Bit 6: unknown function
6565 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6566 * Bit 4: must be set to zero to avoid problems
6567 * Bit 3-0: backlight brightness level
6568 *
6569 * brightness_get_raw returns status data in the HBRV layout
d7880f10
HMH
6570 *
6571 * WARNING: The X61 has been verified to use HBRV for something else, so
6572 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6573 * testing on the very early *60 Lenovo models...
0e501834
HMH
6574 */
6575
e11aecf1
HMH
6576enum {
6577 TP_EC_BACKLIGHT = 0x31,
6578
6579 /* TP_EC_BACKLIGHT bitmasks */
6580 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6581 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6582 TP_EC_BACKLIGHT_MAPSW = 0x20,
6583};
6584
0e501834
HMH
6585enum tpacpi_brightness_access_mode {
6586 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6587 TPACPI_BRGHT_MODE_EC, /* EC control */
6588 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6589 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6590 TPACPI_BRGHT_MODE_MAX
6591};
6592
94954cc6 6593static struct backlight_device *ibm_backlight_device;
0e501834
HMH
6594
6595static enum tpacpi_brightness_access_mode brightness_mode =
6596 TPACPI_BRGHT_MODE_MAX;
6597
f74a27d4
HMH
6598static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6599
b21a15f6 6600static struct mutex brightness_mutex;
56b6aeb0 6601
0e501834
HMH
6602/* NVRAM brightness access,
6603 * call with brightness_mutex held! */
6604static unsigned int tpacpi_brightness_nvram_get(void)
b21a15f6 6605{
0e501834 6606 u8 lnvram;
b21a15f6 6607
0e501834
HMH
6608 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6609 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6610 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
77775838 6611 lnvram &= bright_maxlvl;
0e501834
HMH
6612
6613 return lnvram;
6614}
6615
6616static void tpacpi_brightness_checkpoint_nvram(void)
6617{
6618 u8 lec = 0;
6619 u8 b_nvram;
6620
6621 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6622 return;
6623
6624 vdbg_printk(TPACPI_DBG_BRGHT,
6625 "trying to checkpoint backlight level to NVRAM...\n");
6626
6627 if (mutex_lock_killable(&brightness_mutex) < 0)
6628 return;
6629
6630 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6631 goto unlock;
6632 lec &= TP_EC_BACKLIGHT_LVLMSK;
6633 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6634
6635 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6636 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6637 /* NVRAM needs update */
6638 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6639 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6640 b_nvram |= lec;
6641 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6642 dbg_printk(TPACPI_DBG_BRGHT,
6643 "updated NVRAM backlight level to %u (0x%02x)\n",
6644 (unsigned int) lec, (unsigned int) b_nvram);
6645 } else
6646 vdbg_printk(TPACPI_DBG_BRGHT,
6647 "NVRAM backlight level already is %u (0x%02x)\n",
6648 (unsigned int) lec, (unsigned int) b_nvram);
6649
6650unlock:
6651 mutex_unlock(&brightness_mutex);
6652}
6653
6654
6655/* call with brightness_mutex held! */
6656static int tpacpi_brightness_get_raw(int *status)
6657{
6658 u8 lec = 0;
6659
6660 switch (brightness_mode) {
6661 case TPACPI_BRGHT_MODE_UCMS_STEP:
6662 *status = tpacpi_brightness_nvram_get();
6663 return 0;
6664 case TPACPI_BRGHT_MODE_EC:
6665 case TPACPI_BRGHT_MODE_ECNVRAM:
6666 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
2d5e94d7 6667 return -EIO;
0e501834
HMH
6668 *status = lec;
6669 return 0;
6670 default:
6671 return -ENXIO;
b21a15f6 6672 }
0e501834
HMH
6673}
6674
6675/* call with brightness_mutex held! */
6676/* do NOT call with illegal backlight level value */
6677static int tpacpi_brightness_set_ec(unsigned int value)
6678{
6679 u8 lec = 0;
6680
6681 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6682 return -EIO;
6683
6684 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6685 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6686 (value & TP_EC_BACKLIGHT_LVLMSK))))
6687 return -EIO;
6688
6689 return 0;
6690}
6691
6692/* call with brightness_mutex held! */
6693static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6694{
6695 int cmos_cmd, inc;
6696 unsigned int current_value, i;
6697
6698 current_value = tpacpi_brightness_nvram_get();
6699
6700 if (value == current_value)
6701 return 0;
6702
6703 cmos_cmd = (value > current_value) ?
6704 TP_CMOS_BRIGHTNESS_UP :
6705 TP_CMOS_BRIGHTNESS_DOWN;
6706 inc = (value > current_value) ? 1 : -1;
6707
6708 for (i = current_value; i != value; i += inc)
6709 if (issue_thinkpad_cmos_command(cmos_cmd))
6710 return -EIO;
b21a15f6 6711
e11aecf1 6712 return 0;
b21a15f6
HMH
6713}
6714
6715/* May return EINTR which can always be mapped to ERESTARTSYS */
0e501834 6716static int brightness_set(unsigned int value)
b21a15f6 6717{
0e501834 6718 int res;
b21a15f6 6719
bd3c7b9e 6720 if (value > bright_maxlvl)
b21a15f6
HMH
6721 return -EINVAL;
6722
0e501834
HMH
6723 vdbg_printk(TPACPI_DBG_BRGHT,
6724 "set backlight level to %d\n", value);
6725
7646ea88 6726 res = mutex_lock_killable(&brightness_mutex);
b21a15f6
HMH
6727 if (res < 0)
6728 return res;
6729
0e501834
HMH
6730 switch (brightness_mode) {
6731 case TPACPI_BRGHT_MODE_EC:
6732 case TPACPI_BRGHT_MODE_ECNVRAM:
6733 res = tpacpi_brightness_set_ec(value);
6734 break;
6735 case TPACPI_BRGHT_MODE_UCMS_STEP:
6736 res = tpacpi_brightness_set_ucmsstep(value);
6737 break;
6738 default:
6739 res = -ENXIO;
b21a15f6
HMH
6740 }
6741
b21a15f6
HMH
6742 mutex_unlock(&brightness_mutex);
6743 return res;
6744}
6745
6746/* sysfs backlight class ----------------------------------------------- */
6747
6748static int brightness_update_status(struct backlight_device *bd)
6749{
0e501834 6750 unsigned int level =
b21a15f6
HMH
6751 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6752 bd->props.power == FB_BLANK_UNBLANK) ?
0e501834
HMH
6753 bd->props.brightness : 0;
6754
6755 dbg_printk(TPACPI_DBG_BRGHT,
6756 "backlight: attempt to set level to %d\n",
6757 level);
6758
6759 /* it is the backlight class's job (caller) to handle
6760 * EINTR and other errors properly */
6761 return brightness_set(level);
b21a15f6
HMH
6762}
6763
e11aecf1 6764static int brightness_get(struct backlight_device *bd)
a3f104c0 6765{
e11aecf1 6766 int status, res;
a3f104c0 6767
0e501834 6768 res = mutex_lock_killable(&brightness_mutex);
e11aecf1 6769 if (res < 0)
0e501834
HMH
6770 return 0;
6771
6772 res = tpacpi_brightness_get_raw(&status);
6773
6774 mutex_unlock(&brightness_mutex);
6775
6776 if (res < 0)
6777 return 0;
e11e211a 6778
e11aecf1 6779 return status & TP_EC_BACKLIGHT_LVLMSK;
e11e211a
HMH
6780}
6781
347a2686
HMH
6782static void tpacpi_brightness_notify_change(void)
6783{
6784 backlight_force_update(ibm_backlight_device,
6785 BACKLIGHT_UPDATE_HOTKEY);
6786}
6787
acc2472e 6788static const struct backlight_ops ibm_backlight_data = {
35ff8b9f
HMH
6789 .get_brightness = brightness_get,
6790 .update_status = brightness_update_status,
56b6aeb0 6791};
e11e211a 6792
b21a15f6 6793/* --------------------------------------------------------------------- */
e11e211a 6794
122f2672
HMH
6795/*
6796 * Call _BCL method of video device. On some ThinkPads this will
6797 * switch the firmware to the ACPI brightness control mode.
6798 */
6799
77775838
HMH
6800static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6801{
6802 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6803 union acpi_object *obj;
46445b6b 6804 struct acpi_device *device, *child;
77775838
HMH
6805 int rc;
6806
46445b6b
AL
6807 if (acpi_bus_get_device(handle, &device))
6808 return 0;
6809
6810 rc = 0;
6811 list_for_each_entry(child, &device->children, node) {
6812 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6813 NULL, &buffer);
6814 if (ACPI_FAILURE(status))
6815 continue;
6816
77775838
HMH
6817 obj = (union acpi_object *)buffer.pointer;
6818 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
0978e012 6819 pr_err("Unknown _BCL data, please report this to %s\n",
46445b6b 6820 TPACPI_MAIL);
77775838
HMH
6821 rc = 0;
6822 } else {
6823 rc = obj->package.count;
6824 }
46445b6b 6825 break;
77775838
HMH
6826 }
6827
6828 kfree(buffer.pointer);
6829 return rc;
6830}
6831
77775838
HMH
6832
6833/*
6834 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6835 */
6836static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6837{
122f2672 6838 acpi_handle video_device;
77775838 6839 int bcl_levels = 0;
77775838 6840
46445b6b 6841 tpacpi_acpi_handle_locate("video", NULL, &video_device);
122f2672
HMH
6842 if (video_device)
6843 bcl_levels = tpacpi_query_bcl_levels(video_device);
77775838 6844
122f2672 6845 tp_features.bright_acpimode = (bcl_levels > 0);
77775838 6846
122f2672 6847 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
77775838
HMH
6848}
6849
59fe4fe3
HMH
6850/*
6851 * These are only useful for models that have only one possibility
6852 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6853 * these quirks.
6854 */
6855#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6856#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6857#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6858
6859static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6860 /* Models with ATI GPUs known to require ECNVRAM mode */
6861 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6862
6da25bf5 6863 /* Models with ATI GPUs that can use ECNVRAM */
7d1894d8 6864 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
59fe4fe3 6865 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
7d1894d8 6866 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
59fe4fe3
HMH
6867 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6868
6da25bf5 6869 /* Models with Intel Extreme Graphics 2 */
7d1894d8 6870 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
a9f8eacc
HMH
6871 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6872 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
59fe4fe3
HMH
6873
6874 /* Models with Intel GMA900 */
6875 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6876 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6877 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6878};
6879
77775838
HMH
6880/*
6881 * Returns < 0 for error, otherwise sets tp_features.bright_*
6882 * and bright_maxlvl.
6883 */
6884static void __init tpacpi_detect_brightness_capabilities(void)
6885{
6886 unsigned int b;
6887
6888 vdbg_printk(TPACPI_DBG_INIT,
6889 "detecting firmware brightness interface capabilities\n");
6890
6891 /* we could run a quirks check here (same table used by
6892 * brightness_init) if needed */
6893
6894 /*
6895 * We always attempt to detect acpi support, so as to switch
6896 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6897 * going to publish a backlight interface
6898 */
6899 b = tpacpi_check_std_acpi_brightness_support();
6900 switch (b) {
6901 case 16:
6902 bright_maxlvl = 15;
77775838
HMH
6903 break;
6904 case 8:
6905 case 0:
6906 bright_maxlvl = 7;
77775838
HMH
6907 break;
6908 default:
77775838
HMH
6909 tp_features.bright_unkfw = 1;
6910 bright_maxlvl = b - 1;
6911 }
15c75626 6912 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
77775838
HMH
6913}
6914
a5763f22 6915static int __init brightness_init(struct ibm_init_struct *iibm)
56b6aeb0 6916{
a19a6ee6 6917 struct backlight_properties props;
56b6aeb0 6918 int b;
59fe4fe3 6919 unsigned long quirks;
56b6aeb0 6920
fe08bc4b
HMH
6921 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6922
f432255e
HMH
6923 mutex_init(&brightness_mutex);
6924
59fe4fe3
HMH
6925 quirks = tpacpi_check_quirks(brightness_quirk_table,
6926 ARRAY_SIZE(brightness_quirk_table));
6927
77775838
HMH
6928 /* tpacpi_detect_brightness_capabilities() must have run already */
6929
6930 /* if it is unknown, we don't handle it: it wouldn't be safe */
6931 if (tp_features.bright_unkfw)
6932 return 1;
2dba1b5d 6933
b5972796 6934 if (!brightness_enable) {
0e501834 6935 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5cac62ac 6936 "brightness support disabled by module parameter\n");
b5972796
HMH
6937 return 1;
6938 }
6939
b33c6ce5 6940 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
217f0963 6941 if (brightness_enable > 1) {
5cac62ac 6942 pr_info("Standard ACPI backlight interface available, not loading native one\n");
217f0963
HMH
6943 return 1;
6944 } else if (brightness_enable == 1) {
5cac62ac 6945 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
217f0963
HMH
6946 return 1;
6947 }
6948 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
5cac62ac 6949 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
217f0963
HMH
6950 }
6951
0e501834
HMH
6952 /*
6953 * Check for module parameter bogosity, note that we
6954 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6955 * able to detect "unspecified"
6956 */
6957 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6958 return -EINVAL;
24d3b774 6959
0e501834
HMH
6960 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6961 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6962 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
59fe4fe3
HMH
6963 if (quirks & TPACPI_BRGHT_Q_EC)
6964 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6965 else
0e501834 6966 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
24d3b774 6967
0e501834 6968 dbg_printk(TPACPI_DBG_BRGHT,
59fe4fe3 6969 "driver auto-selected brightness_mode=%d\n",
0e501834
HMH
6970 brightness_mode);
6971 }
24d3b774 6972
d7880f10 6973 /* Safety */
e28393c0 6974 if (!tpacpi_is_ibm() &&
d7880f10
HMH
6975 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6976 brightness_mode == TPACPI_BRGHT_MODE_EC))
6977 return -EINVAL;
6978
0e501834 6979 if (tpacpi_brightness_get_raw(&b) < 0)
24d3b774 6980 return 1;
56b6aeb0 6981
a19a6ee6 6982 memset(&props, 0, sizeof(struct backlight_properties));
bb7ca747 6983 props.type = BACKLIGHT_PLATFORM;
77775838
HMH
6984 props.max_brightness = bright_maxlvl;
6985 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
a19a6ee6
MG
6986 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6987 NULL, NULL,
6988 &ibm_backlight_data,
6989 &props);
56b6aeb0 6990 if (IS_ERR(ibm_backlight_device)) {
435c47e2
HMH
6991 int rc = PTR_ERR(ibm_backlight_device);
6992 ibm_backlight_device = NULL;
0978e012 6993 pr_err("Could not register backlight device\n");
435c47e2 6994 return rc;
56b6aeb0 6995 }
0e501834
HMH
6996 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6997 "brightness is supported\n");
56b6aeb0 6998
59fe4fe3 6999 if (quirks & TPACPI_BRGHT_Q_ASK) {
5cac62ac
AS
7000 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
7001 brightness_mode);
7002 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
7003 TPACPI_MAIL);
59fe4fe3
HMH
7004 }
7005
7d9745cf
HMH
7006 /* Added by mistake in early 2007. Probably useless, but it could
7007 * be working around some unknown firmware problem where the value
7008 * read at startup doesn't match the real hardware state... so leave
7009 * it in place just in case */
56b6aeb0
HMH
7010 backlight_update_status(ibm_backlight_device);
7011
347a2686 7012 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5cac62ac 7013 "brightness: registering brightness hotkeys as change notification\n");
347a2686
HMH
7014 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7015 | TP_ACPI_HKEY_BRGHTUP_MASK
3098064d 7016 | TP_ACPI_HKEY_BRGHTDWN_MASK);
56b6aeb0
HMH
7017 return 0;
7018}
7019
fd3c3a42 7020static void brightness_suspend(void)
0e501834
HMH
7021{
7022 tpacpi_brightness_checkpoint_nvram();
7023}
7024
7025static void brightness_shutdown(void)
7026{
7027 tpacpi_brightness_checkpoint_nvram();
7028}
7029
56b6aeb0
HMH
7030static void brightness_exit(void)
7031{
7032 if (ibm_backlight_device) {
0e501834 7033 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
fe08bc4b 7034 "calling backlight_device_unregister()\n");
56b6aeb0 7035 backlight_device_unregister(ibm_backlight_device);
56b6aeb0 7036 }
0e501834
HMH
7037
7038 tpacpi_brightness_checkpoint_nvram();
56b6aeb0
HMH
7039}
7040
887965e6 7041static int brightness_read(struct seq_file *m)
56b6aeb0 7042{
56b6aeb0
HMH
7043 int level;
7044
35ff8b9f
HMH
7045 level = brightness_get(NULL);
7046 if (level < 0) {
887965e6 7047 seq_printf(m, "level:\t\tunreadable\n");
56b6aeb0 7048 } else {
887965e6
AD
7049 seq_printf(m, "level:\t\t%d\n", level);
7050 seq_printf(m, "commands:\tup, down\n");
77775838
HMH
7051 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7052 bright_maxlvl);
56b6aeb0
HMH
7053 }
7054
887965e6 7055 return 0;
56b6aeb0
HMH
7056}
7057
8acb0250
HM
7058static int brightness_write(char *buf)
7059{
7060 int level;
4273af8d 7061 int rc;
1da177e4 7062 char *cmd;
1da177e4 7063
4273af8d
HMH
7064 level = brightness_get(NULL);
7065 if (level < 0)
7066 return level;
78f81cc4 7067
4273af8d 7068 while ((cmd = next_cmd(&buf))) {
78f81cc4 7069 if (strlencmp(cmd, "up") == 0) {
77775838 7070 if (level < bright_maxlvl)
4273af8d 7071 level++;
78f81cc4 7072 } else if (strlencmp(cmd, "down") == 0) {
4273af8d
HMH
7073 if (level > 0)
7074 level--;
7075 } else if (sscanf(cmd, "level %d", &level) == 1 &&
77775838 7076 level >= 0 && level <= bright_maxlvl) {
4273af8d 7077 /* new level set */
78f81cc4
BD
7078 } else
7079 return -EINVAL;
78f81cc4
BD
7080 }
7081
0e501834
HMH
7082 tpacpi_disclose_usertask("procfs brightness",
7083 "set level to %d\n", level);
7084
4273af8d
HMH
7085 /*
7086 * Now we know what the final level should be, so we try to set it.
7087 * Doing it this way makes the syscall restartable in case of EINTR
7088 */
7089 rc = brightness_set(level);
347a2686
HMH
7090 if (!rc && ibm_backlight_device)
7091 backlight_force_update(ibm_backlight_device,
7092 BACKLIGHT_UPDATE_SYSFS);
72a979f0 7093 return (rc == -EINTR) ? -ERESTARTSYS : rc;
78f81cc4
BD
7094}
7095
a5763f22
HMH
7096static struct ibm_struct brightness_driver_data = {
7097 .name = "brightness",
7098 .read = brightness_read,
7099 .write = brightness_write,
7100 .exit = brightness_exit,
0e501834
HMH
7101 .suspend = brightness_suspend,
7102 .shutdown = brightness_shutdown,
a5763f22
HMH
7103};
7104
56b6aeb0
HMH
7105/*************************************************************************
7106 * Volume subdriver
7107 */
fb87a811 7108
329e4e18
HMH
7109/*
7110 * IBM ThinkPads have a simple volume controller with MUTE gating.
7111 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7112 *
7113 * Since the *61 series (and probably also the later *60 series), Lenovo
7114 * ThinkPads only implement the MUTE gate.
7115 *
7116 * EC register 0x30
7117 * Bit 6: MUTE (1 mutes sound)
7118 * Bit 3-0: Volume
7119 * Other bits should be zero as far as we know.
7120 *
7121 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7122 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7123 * such as bit 7 which is used to detect repeated presses of MUTE,
7124 * and we leave them unchanged.
9a417ec0
AL
7125 *
7126 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7127 * in response to user input. Unfortunately, this rarely works well.
7128 * The laptop changes the state of its internal MUTE gate and, on some
7129 * models, sends KEY_MUTE, causing any user code that responds to the
7130 * mute button to get confused. The hardware MUTE gate is also
7131 * unnecessary, since user code can handle the mute button without
7132 * kernel or EC help.
7133 *
7134 * To avoid confusing userspace, we simply disable all EC-based mute
7135 * and volume controls when possible.
329e4e18
HMH
7136 */
7137
ff850c33
HMH
7138#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7139
0d204c34
HMH
7140#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7141#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7142#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7143
cab66613
TI
7144#if SNDRV_CARDS <= 32
7145#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7146#else
7147#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7148#endif
7149static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
0d204c34 7150static char *alsa_id = "ThinkPadEC";
90ab5ee9 7151static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
0d204c34
HMH
7152
7153struct tpacpi_alsa_data {
7154 struct snd_card *card;
7155 struct snd_ctl_elem_id *ctl_mute_id;
7156 struct snd_ctl_elem_id *ctl_vol_id;
7157};
7158
7159static struct snd_card *alsa_card;
7160
329e4e18
HMH
7161enum {
7162 TP_EC_AUDIO = 0x30,
7163
7164 /* TP_EC_AUDIO bits */
7165 TP_EC_AUDIO_MUTESW = 6,
7166
7167 /* TP_EC_AUDIO bitmasks */
7168 TP_EC_AUDIO_LVL_MSK = 0x0F,
7169 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7170
7171 /* Maximum volume */
7172 TP_EC_VOLUME_MAX = 14,
7173};
7174
7175enum tpacpi_volume_access_mode {
7176 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7177 TPACPI_VOL_MODE_EC, /* Pure EC control */
7178 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7179 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7180 TPACPI_VOL_MODE_MAX
7181};
7182
a112ceee
HMH
7183enum tpacpi_volume_capabilities {
7184 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7185 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7186 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7187 TPACPI_VOL_CAP_MAX
7188};
7189
9a417ec0
AL
7190enum tpacpi_mute_btn_mode {
7191 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7192 /* We don't know what mode 1 is. */
7193 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7194 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7195};
7196
329e4e18
HMH
7197static enum tpacpi_volume_access_mode volume_mode =
7198 TPACPI_VOL_MODE_MAX;
7199
a112ceee 7200static enum tpacpi_volume_capabilities volume_capabilities;
90ab5ee9 7201static bool volume_control_allowed;
9a417ec0
AL
7202static bool software_mute_requested = true;
7203static bool software_mute_active;
7204static int software_mute_orig_mode;
f74a27d4 7205
329e4e18
HMH
7206/*
7207 * Used to syncronize writers to TP_EC_AUDIO and
7208 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7209 */
7210static struct mutex volume_mutex;
7211
7212static void tpacpi_volume_checkpoint_nvram(void)
78f81cc4 7213{
329e4e18
HMH
7214 u8 lec = 0;
7215 u8 b_nvram;
a112ceee 7216 u8 ec_mask;
329e4e18
HMH
7217
7218 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7219 return;
c7ac6291
HMH
7220 if (!volume_control_allowed)
7221 return;
9a417ec0
AL
7222 if (software_mute_active)
7223 return;
329e4e18
HMH
7224
7225 vdbg_printk(TPACPI_DBG_MIXER,
7226 "trying to checkpoint mixer state to NVRAM...\n");
78f81cc4 7227
a112ceee
HMH
7228 if (tp_features.mixer_no_level_control)
7229 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7230 else
7231 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7232
329e4e18
HMH
7233 if (mutex_lock_killable(&volume_mutex) < 0)
7234 return;
7235
7236 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7237 goto unlock;
7238 lec &= ec_mask;
7239 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7240
7241 if (lec != (b_nvram & ec_mask)) {
7242 /* NVRAM needs update */
7243 b_nvram &= ~ec_mask;
7244 b_nvram |= lec;
7245 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7246 dbg_printk(TPACPI_DBG_MIXER,
7247 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7248 (unsigned int) lec, (unsigned int) b_nvram);
78f81cc4 7249 } else {
329e4e18
HMH
7250 vdbg_printk(TPACPI_DBG_MIXER,
7251 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7252 (unsigned int) lec, (unsigned int) b_nvram);
78f81cc4
BD
7253 }
7254
329e4e18
HMH
7255unlock:
7256 mutex_unlock(&volume_mutex);
78f81cc4
BD
7257}
7258
329e4e18 7259static int volume_get_status_ec(u8 *status)
78f81cc4 7260{
329e4e18 7261 u8 s;
78f81cc4 7262
329e4e18
HMH
7263 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7264 return -EIO;
78f81cc4 7265
329e4e18 7266 *status = s;
1da177e4 7267
329e4e18 7268 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
35ff8b9f 7269
329e4e18
HMH
7270 return 0;
7271}
78f81cc4 7272
329e4e18
HMH
7273static int volume_get_status(u8 *status)
7274{
7275 return volume_get_status_ec(status);
7276}
78f81cc4 7277
329e4e18
HMH
7278static int volume_set_status_ec(const u8 status)
7279{
7280 if (!acpi_ec_write(TP_EC_AUDIO, status))
7281 return -EIO;
78f81cc4 7282
329e4e18
HMH
7283 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7284
9a417ec0
AL
7285 /*
7286 * On X200s, and possibly on others, it can take a while for
7287 * reads to become correct.
7288 */
7289 msleep(1);
7290
329e4e18
HMH
7291 return 0;
7292}
7293
7294static int volume_set_status(const u8 status)
7295{
7296 return volume_set_status_ec(status);
7297}
7298
88cc8377
HMH
7299/* returns < 0 on error, 0 on no change, 1 on change */
7300static int __volume_set_mute_ec(const bool mute)
329e4e18
HMH
7301{
7302 int rc;
7303 u8 s, n;
7304
7305 if (mutex_lock_killable(&volume_mutex) < 0)
7306 return -EINTR;
7307
7308 rc = volume_get_status_ec(&s);
7309 if (rc)
7310 goto unlock;
7311
7312 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7313 s & ~TP_EC_AUDIO_MUTESW_MSK;
7314
88cc8377 7315 if (n != s) {
329e4e18 7316 rc = volume_set_status_ec(n);
88cc8377
HMH
7317 if (!rc)
7318 rc = 1;
7319 }
329e4e18
HMH
7320
7321unlock:
7322 mutex_unlock(&volume_mutex);
7323 return rc;
7324}
7325
88cc8377
HMH
7326static int volume_alsa_set_mute(const bool mute)
7327{
7328 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7329 (mute) ? "" : "un");
7330 return __volume_set_mute_ec(mute);
7331}
7332
329e4e18
HMH
7333static int volume_set_mute(const bool mute)
7334{
88cc8377
HMH
7335 int rc;
7336
329e4e18
HMH
7337 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7338 (mute) ? "" : "un");
88cc8377
HMH
7339
7340 rc = __volume_set_mute_ec(mute);
7341 return (rc < 0) ? rc : 0;
329e4e18
HMH
7342}
7343
88cc8377
HMH
7344/* returns < 0 on error, 0 on no change, 1 on change */
7345static int __volume_set_volume_ec(const u8 vol)
329e4e18
HMH
7346{
7347 int rc;
7348 u8 s, n;
7349
7350 if (vol > TP_EC_VOLUME_MAX)
7351 return -EINVAL;
7352
7353 if (mutex_lock_killable(&volume_mutex) < 0)
7354 return -EINTR;
7355
7356 rc = volume_get_status_ec(&s);
7357 if (rc)
7358 goto unlock;
7359
7360 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7361
88cc8377 7362 if (n != s) {
329e4e18 7363 rc = volume_set_status_ec(n);
88cc8377
HMH
7364 if (!rc)
7365 rc = 1;
7366 }
329e4e18
HMH
7367
7368unlock:
7369 mutex_unlock(&volume_mutex);
7370 return rc;
7371}
7372
9a417ec0
AL
7373static int volume_set_software_mute(bool startup)
7374{
7375 int result;
7376
7377 if (!tpacpi_is_lenovo())
7378 return -ENODEV;
7379
7380 if (startup) {
7381 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7382 "HAUM", "qd"))
7383 return -EIO;
7384
7385 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7386 "Initial HAUM setting was %d\n",
7387 software_mute_orig_mode);
7388 }
7389
7390 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7391 (int)TP_EC_MUTE_BTN_NONE))
7392 return -EIO;
7393
7394 if (result != TP_EC_MUTE_BTN_NONE)
7395 pr_warn("Unexpected SAUM result %d\n",
7396 result);
7397
7398 /*
7399 * In software mute mode, the standard codec controls take
7400 * precendence, so we unmute the ThinkPad HW switch at
7401 * startup. Just on case there are SAUM-capable ThinkPads
7402 * with level controls, set max HW volume as well.
7403 */
7404 if (tp_features.mixer_no_level_control)
7405 result = volume_set_mute(false);
7406 else
7407 result = volume_set_status(TP_EC_VOLUME_MAX);
7408
7409 if (result != 0)
7410 pr_warn("Failed to unmute the HW mute switch\n");
7411
7412 return 0;
7413}
7414
7415static void volume_exit_software_mute(void)
7416{
7417 int r;
7418
7419 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7420 || r != software_mute_orig_mode)
7421 pr_warn("Failed to restore mute mode\n");
7422}
7423
88cc8377 7424static int volume_alsa_set_volume(const u8 vol)
329e4e18
HMH
7425{
7426 dbg_printk(TPACPI_DBG_MIXER,
88cc8377
HMH
7427 "ALSA: trying to set volume level to %hu\n", vol);
7428 return __volume_set_volume_ec(vol);
329e4e18
HMH
7429}
7430
0d204c34
HMH
7431static void volume_alsa_notify_change(void)
7432{
7433 struct tpacpi_alsa_data *d;
7434
7435 if (alsa_card && alsa_card->private_data) {
7436 d = alsa_card->private_data;
7437 if (d->ctl_mute_id)
7438 snd_ctl_notify(alsa_card,
7439 SNDRV_CTL_EVENT_MASK_VALUE,
7440 d->ctl_mute_id);
7441 if (d->ctl_vol_id)
7442 snd_ctl_notify(alsa_card,
7443 SNDRV_CTL_EVENT_MASK_VALUE,
7444 d->ctl_vol_id);
7445 }
7446}
7447
7448static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7449 struct snd_ctl_elem_info *uinfo)
7450{
7451 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7452 uinfo->count = 1;
7453 uinfo->value.integer.min = 0;
7454 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7455 return 0;
7456}
7457
7458static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7459 struct snd_ctl_elem_value *ucontrol)
7460{
7461 u8 s;
7462 int rc;
7463
7464 rc = volume_get_status(&s);
7465 if (rc < 0)
7466 return rc;
7467
7468 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7469 return 0;
7470}
7471
7472static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7473 struct snd_ctl_elem_value *ucontrol)
7474{
38e11cde
HMH
7475 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7476 ucontrol->value.integer.value[0]);
88cc8377 7477 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
0d204c34
HMH
7478}
7479
7480#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7481
7482static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7483 struct snd_ctl_elem_value *ucontrol)
7484{
7485 u8 s;
7486 int rc;
7487
7488 rc = volume_get_status(&s);
7489 if (rc < 0)
7490 return rc;
7491
7492 ucontrol->value.integer.value[0] =
7493 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7494 return 0;
7495}
7496
7497static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7498 struct snd_ctl_elem_value *ucontrol)
7499{
38e11cde
HMH
7500 tpacpi_disclose_usertask("ALSA", "%smute\n",
7501 ucontrol->value.integer.value[0] ?
7502 "un" : "");
88cc8377 7503 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
0d204c34
HMH
7504}
7505
c3aa4723 7506static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
0d204c34
HMH
7507 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7508 .name = "Console Playback Volume",
7509 .index = 0,
7510 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7511 .info = volume_alsa_vol_info,
7512 .get = volume_alsa_vol_get,
7513};
7514
c3aa4723 7515static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
0d204c34
HMH
7516 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7517 .name = "Console Playback Switch",
7518 .index = 0,
7519 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7520 .info = volume_alsa_mute_info,
7521 .get = volume_alsa_mute_get,
7522};
7523
fd3c3a42 7524static void volume_suspend(void)
329e4e18
HMH
7525{
7526 tpacpi_volume_checkpoint_nvram();
7527}
7528
0d204c34
HMH
7529static void volume_resume(void)
7530{
9a417ec0
AL
7531 if (software_mute_active) {
7532 if (volume_set_software_mute(false) < 0)
7533 pr_warn("Failed to restore software mute\n");
7534 } else {
7535 volume_alsa_notify_change();
7536 }
0d204c34
HMH
7537}
7538
329e4e18
HMH
7539static void volume_shutdown(void)
7540{
7541 tpacpi_volume_checkpoint_nvram();
7542}
7543
7544static void volume_exit(void)
7545{
0d204c34
HMH
7546 if (alsa_card) {
7547 snd_card_free(alsa_card);
7548 alsa_card = NULL;
7549 }
7550
329e4e18 7551 tpacpi_volume_checkpoint_nvram();
9a417ec0
AL
7552
7553 if (software_mute_active)
7554 volume_exit_software_mute();
329e4e18
HMH
7555}
7556
0d204c34
HMH
7557static int __init volume_create_alsa_mixer(void)
7558{
7559 struct snd_card *card;
7560 struct tpacpi_alsa_data *data;
7561 struct snd_kcontrol *ctl_vol;
7562 struct snd_kcontrol *ctl_mute;
7563 int rc;
7564
89235e55
TI
7565 rc = snd_card_new(&tpacpi_pdev->dev,
7566 alsa_index, alsa_id, THIS_MODULE,
7567 sizeof(struct tpacpi_alsa_data), &card);
74c75c18 7568 if (rc < 0 || !card) {
0978e012 7569 pr_err("Failed to create ALSA card structures: %d\n", rc);
74c75c18
HMH
7570 return 1;
7571 }
0d204c34
HMH
7572
7573 BUG_ON(!card->private_data);
7574 data = card->private_data;
7575 data->card = card;
7576
7577 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7578 sizeof(card->driver));
7579 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7580 sizeof(card->shortname));
7581 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7582 (thinkpad_id.ec_version_str) ?
7583 thinkpad_id.ec_version_str : "(unknown)");
7584 snprintf(card->longname, sizeof(card->longname),
7585 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7586 (thinkpad_id.ec_version_str) ?
7587 thinkpad_id.ec_version_str : "unknown");
7588
7589 if (volume_control_allowed) {
7590 volume_alsa_control_vol.put = volume_alsa_vol_put;
7591 volume_alsa_control_vol.access =
7592 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7593
7594 volume_alsa_control_mute.put = volume_alsa_mute_put;
7595 volume_alsa_control_mute.access =
7596 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7597 }
7598
7599 if (!tp_features.mixer_no_level_control) {
7600 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7601 rc = snd_ctl_add(card, ctl_vol);
7602 if (rc < 0) {
0978e012
JP
7603 pr_err("Failed to create ALSA volume control: %d\n",
7604 rc);
74c75c18 7605 goto err_exit;
78f81cc4 7606 }
0d204c34
HMH
7607 data->ctl_vol_id = &ctl_vol->id;
7608 }
78f81cc4 7609
0d204c34
HMH
7610 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7611 rc = snd_ctl_add(card, ctl_mute);
7612 if (rc < 0) {
0978e012 7613 pr_err("Failed to create ALSA mute control: %d\n", rc);
74c75c18 7614 goto err_exit;
0d204c34
HMH
7615 }
7616 data->ctl_mute_id = &ctl_mute->id;
35ff8b9f 7617
0d204c34 7618 rc = snd_card_register(card);
0d204c34 7619 if (rc < 0) {
0978e012 7620 pr_err("Failed to register ALSA card: %d\n", rc);
74c75c18 7621 goto err_exit;
0d204c34
HMH
7622 }
7623
7624 alsa_card = card;
74c75c18
HMH
7625 return 0;
7626
7627err_exit:
7628 snd_card_free(card);
7629 return 1;
0d204c34
HMH
7630}
7631
a112ceee
HMH
7632#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7633#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7634
7635static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7636 /* Whitelist volume level on all IBM by default */
7637 { .vendor = PCI_VENDOR_ID_IBM,
7638 .bios = TPACPI_MATCH_ANY,
7639 .ec = TPACPI_MATCH_ANY,
7640 .quirks = TPACPI_VOL_Q_LEVEL },
7641
7642 /* Lenovo models with volume control (needs confirmation) */
7643 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7644 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7645 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7646 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7647 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7648 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7649 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7650
7651 /* Whitelist mute-only on all Lenovo by default */
7652 { .vendor = PCI_VENDOR_ID_LENOVO,
7653 .bios = TPACPI_MATCH_ANY,
7654 .ec = TPACPI_MATCH_ANY,
7655 .quirks = TPACPI_VOL_Q_MUTEONLY }
7656};
7657
329e4e18
HMH
7658static int __init volume_init(struct ibm_init_struct *iibm)
7659{
a112ceee 7660 unsigned long quirks;
0d204c34 7661 int rc;
a112ceee 7662
329e4e18
HMH
7663 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7664
7665 mutex_init(&volume_mutex);
7666
7667 /*
7668 * Check for module parameter bogosity, note that we
7669 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7670 * able to detect "unspecified"
7671 */
7672 if (volume_mode > TPACPI_VOL_MODE_MAX)
7673 return -EINVAL;
7674
7675 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
5cac62ac
AS
7676 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7677 TPACPI_MAIL);
329e4e18
HMH
7678 return 1;
7679 }
7680
a112ceee
HMH
7681 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7682 return -EINVAL;
7683
0d204c34
HMH
7684 /*
7685 * The ALSA mixer is our primary interface.
7686 * When disabled, don't install the subdriver at all
7687 */
7688 if (!alsa_enable) {
7689 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
5cac62ac 7690 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
0d204c34
HMH
7691 return 1;
7692 }
7693
a112ceee
HMH
7694 quirks = tpacpi_check_quirks(volume_quirk_table,
7695 ARRAY_SIZE(volume_quirk_table));
7696
7697 switch (volume_capabilities) {
7698 case TPACPI_VOL_CAP_AUTO:
7699 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7700 tp_features.mixer_no_level_control = 1;
7701 else if (quirks & TPACPI_VOL_Q_LEVEL)
7702 tp_features.mixer_no_level_control = 0;
7703 else
7704 return 1; /* no mixer */
7705 break;
7706 case TPACPI_VOL_CAP_VOLMUTE:
7707 tp_features.mixer_no_level_control = 0;
7708 break;
7709 case TPACPI_VOL_CAP_MUTEONLY:
7710 tp_features.mixer_no_level_control = 1;
7711 break;
7712 default:
7713 return 1;
7714 }
7715
7716 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7717 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7718 "using user-supplied volume_capabilities=%d\n",
7719 volume_capabilities);
7720
329e4e18
HMH
7721 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7722 volume_mode == TPACPI_VOL_MODE_MAX) {
7723 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7724
7725 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7726 "driver auto-selected volume_mode=%d\n",
7727 volume_mode);
7728 } else {
7729 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7730 "using user-supplied volume_mode=%d\n",
7731 volume_mode);
7732 }
7733
7734 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
a112ceee
HMH
7735 "mute is supported, volume control is %s\n",
7736 str_supported(!tp_features.mixer_no_level_control));
329e4e18 7737
9a417ec0
AL
7738 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7739 software_mute_active = true;
7740 } else {
7741 rc = volume_create_alsa_mixer();
7742 if (rc) {
7743 pr_err("Could not create the ALSA mixer interface\n");
7744 return rc;
7745 }
0d204c34 7746
9a417ec0
AL
7747 pr_info("Console audio control enabled, mode: %s\n",
7748 (volume_control_allowed) ?
7749 "override (read/write)" :
7750 "monitor (read only)");
7751 }
c7ac6291 7752
0d204c34
HMH
7753 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7754 "registering volume hotkeys as change notification\n");
7755 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7756 | TP_ACPI_HKEY_VOLUP_MASK
7757 | TP_ACPI_HKEY_VOLDWN_MASK
7758 | TP_ACPI_HKEY_MUTE_MASK);
7759
329e4e18
HMH
7760 return 0;
7761}
f74a27d4 7762
887965e6 7763static int volume_read(struct seq_file *m)
78f81cc4 7764{
329e4e18 7765 u8 status;
78f81cc4 7766
329e4e18 7767 if (volume_get_status(&status) < 0) {
887965e6 7768 seq_printf(m, "level:\t\tunreadable\n");
78f81cc4 7769 } else {
a112ceee 7770 if (tp_features.mixer_no_level_control)
887965e6 7771 seq_printf(m, "level:\t\tunsupported\n");
a112ceee 7772 else
887965e6 7773 seq_printf(m, "level:\t\t%d\n",
a112ceee
HMH
7774 status & TP_EC_AUDIO_LVL_MSK);
7775
887965e6 7776 seq_printf(m, "mute:\t\t%s\n",
329e4e18 7777 onoff(status, TP_EC_AUDIO_MUTESW));
a112ceee 7778
c7ac6291 7779 if (volume_control_allowed) {
887965e6 7780 seq_printf(m, "commands:\tunmute, mute\n");
c7ac6291 7781 if (!tp_features.mixer_no_level_control) {
5cac62ac
AS
7782 seq_printf(m, "commands:\tup, down\n");
7783 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7784 TP_EC_VOLUME_MAX);
c7ac6291 7785 }
78f81cc4
BD
7786 }
7787 }
7788
7789 return 0;
7790}
7791
78f81cc4
BD
7792static int volume_write(char *buf)
7793{
329e4e18
HMH
7794 u8 s;
7795 u8 new_level, new_mute;
7796 int l;
78f81cc4 7797 char *cmd;
329e4e18 7798 int rc;
78f81cc4 7799
c7ac6291
HMH
7800 /*
7801 * We do allow volume control at driver startup, so that the
7802 * user can set initial state through the volume=... parameter hack.
7803 */
7804 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7805 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7806 tp_warned.volume_ctrl_forbidden = 1;
5cac62ac
AS
7807 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7808 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
c7ac6291
HMH
7809 }
7810 return -EPERM;
7811 }
7812
329e4e18
HMH
7813 rc = volume_get_status(&s);
7814 if (rc < 0)
7815 return rc;
78f81cc4 7816
329e4e18
HMH
7817 new_level = s & TP_EC_AUDIO_LVL_MSK;
7818 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7819
7820 while ((cmd = next_cmd(&buf))) {
a112ceee
HMH
7821 if (!tp_features.mixer_no_level_control) {
7822 if (strlencmp(cmd, "up") == 0) {
7823 if (new_mute)
7824 new_mute = 0;
7825 else if (new_level < TP_EC_VOLUME_MAX)
7826 new_level++;
7827 continue;
7828 } else if (strlencmp(cmd, "down") == 0) {
7829 if (new_mute)
7830 new_mute = 0;
7831 else if (new_level > 0)
7832 new_level--;
7833 continue;
7834 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7835 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7836 new_level = l;
7837 continue;
7838 }
7839 }
7840 if (strlencmp(cmd, "mute") == 0)
329e4e18 7841 new_mute = TP_EC_AUDIO_MUTESW_MSK;
a112ceee
HMH
7842 else if (strlencmp(cmd, "unmute") == 0)
7843 new_mute = 0;
7844 else
1da177e4 7845 return -EINVAL;
329e4e18 7846 }
1da177e4 7847
a112ceee
HMH
7848 if (tp_features.mixer_no_level_control) {
7849 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7850 new_mute ? "" : "un");
7851 rc = volume_set_mute(!!new_mute);
7852 } else {
7853 tpacpi_disclose_usertask("procfs volume",
7854 "%smute and set level to %d\n",
7855 new_mute ? "" : "un", new_level);
7856 rc = volume_set_status(new_mute | new_level);
7857 }
0d204c34 7858 volume_alsa_notify_change();
78f81cc4 7859
329e4e18 7860 return (rc == -EINTR) ? -ERESTARTSYS : rc;
78f81cc4
BD
7861}
7862
a5763f22
HMH
7863static struct ibm_struct volume_driver_data = {
7864 .name = "volume",
7865 .read = volume_read,
7866 .write = volume_write,
329e4e18
HMH
7867 .exit = volume_exit,
7868 .suspend = volume_suspend,
0d204c34 7869 .resume = volume_resume,
329e4e18 7870 .shutdown = volume_shutdown,
a5763f22 7871};
56b6aeb0 7872
ff850c33
HMH
7873#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7874
7875#define alsa_card NULL
7876
bb55a2ee 7877static inline void volume_alsa_notify_change(void)
ff850c33
HMH
7878{
7879}
7880
7881static int __init volume_init(struct ibm_init_struct *iibm)
7882{
0978e012 7883 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
ff850c33
HMH
7884
7885 return 1;
7886}
7887
7888static struct ibm_struct volume_driver_data = {
7889 .name = "volume",
7890};
7891
7892#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7893
56b6aeb0
HMH
7894/*************************************************************************
7895 * Fan subdriver
7896 */
7897
7898/*
7899 * FAN ACCESS MODES
7900 *
efa27145 7901 * TPACPI_FAN_RD_ACPI_GFAN:
56b6aeb0
HMH
7902 * ACPI GFAN method: returns fan level
7903 *
efa27145 7904 * see TPACPI_FAN_WR_ACPI_SFAN
f51d1a39 7905 * EC 0x2f (HFSP) not available if GFAN exists
56b6aeb0 7906 *
efa27145 7907 * TPACPI_FAN_WR_ACPI_SFAN:
56b6aeb0
HMH
7908 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7909 *
f51d1a39
HMH
7910 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7911 * it for writing.
56b6aeb0 7912 *
efa27145 7913 * TPACPI_FAN_WR_TPEC:
f51d1a39
HMH
7914 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7915 * Supported on almost all ThinkPads
56b6aeb0
HMH
7916 *
7917 * Fan speed changes of any sort (including those caused by the
7918 * disengaged mode) are usually done slowly by the firmware as the
9ddc5b6f 7919 * maximum amount of fan duty cycle change per second seems to be
56b6aeb0
HMH
7920 * limited.
7921 *
7922 * Reading is not available if GFAN exists.
7923 * Writing is not available if SFAN exists.
7924 *
7925 * Bits
7926 * 7 automatic mode engaged;
7927 * (default operation mode of the ThinkPad)
7928 * fan level is ignored in this mode.
f51d1a39 7929 * 6 full speed mode (takes precedence over bit 7);
56b6aeb0 7930 * not available on all thinkpads. May disable
f51d1a39
HMH
7931 * the tachometer while the fan controller ramps up
7932 * the speed (which can take up to a few *minutes*).
7933 * Speeds up fan to 100% duty-cycle, which is far above
7934 * the standard RPM levels. It is not impossible that
7935 * it could cause hardware damage.
56b6aeb0
HMH
7936 * 5-3 unused in some models. Extra bits for fan level
7937 * in others, but still useless as all values above
7938 * 7 map to the same speed as level 7 in these models.
7939 * 2-0 fan level (0..7 usually)
7940 * 0x00 = stop
7941 * 0x07 = max (set when temperatures critical)
7942 * Some ThinkPads may have other levels, see
efa27145 7943 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
56b6aeb0
HMH
7944 *
7945 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
b595076a 7946 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
56b6aeb0
HMH
7947 * does so, its initial value is meaningless (0x07).
7948 *
7949 * For firmware bugs, refer to:
7950 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7951 *
7952 * ----
7953 *
7954 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7955 * Main fan tachometer reading (in RPM)
7956 *
7957 * This register is present on all ThinkPads with a new-style EC, and
7958 * it is known not to be present on the A21m/e, and T22, as there is
7959 * something else in offset 0x84 according to the ACPI DSDT. Other
7960 * ThinkPads from this same time period (and earlier) probably lack the
7961 * tachometer as well.
7962 *
877d0310 7963 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
56b6aeb0
HMH
7964 * was never fixed by IBM to report the EC firmware version string
7965 * probably support the tachometer (like the early X models), so
7966 * detecting it is quite hard. We need more data to know for sure.
7967 *
7968 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7969 * might result.
7970 *
f51d1a39
HMH
7971 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7972 * mode.
56b6aeb0
HMH
7973 *
7974 * For firmware bugs, refer to:
7975 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7976 *
d7377247
HMH
7977 * ----
7978 *
7979 * ThinkPad EC register 0x31 bit 0 (only on select models)
7980 *
7981 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7982 * show the speed of the main fan. When bit 0 of EC register 0x31
7983 * is one, the tachometer registers show the speed of the auxiliary
7984 * fan.
7985 *
7986 * Fan control seems to affect both fans, regardless of the state
7987 * of this bit.
7988 *
7989 * So far, only the firmware for the X60/X61 non-tablet versions
7990 * seem to support this (firmware TP-7M).
7991 *
efa27145 7992 * TPACPI_FAN_WR_ACPI_FANS:
56b6aeb0
HMH
7993 * ThinkPad X31, X40, X41. Not available in the X60.
7994 *
7995 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7996 * high speed. ACPI DSDT seems to map these three speeds to levels
7997 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7998 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7999 *
8000 * The speeds are stored on handles
8001 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
8002 *
af901ca1 8003 * There are three default speed sets, accessible as handles:
56b6aeb0
HMH
8004 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
8005 *
8006 * ACPI DSDT switches which set is in use depending on various
8007 * factors.
8008 *
efa27145 8009 * TPACPI_FAN_WR_TPEC is also available and should be used to
56b6aeb0
HMH
8010 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
8011 * but the ACPI tables just mention level 7.
8012 */
8013
f74a27d4
HMH
8014enum { /* Fan control constants */
8015 fan_status_offset = 0x2f, /* EC register 0x2f */
8016 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
8017 * 0x84 must be read before 0x85 */
d7377247
HMH
8018 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
8019 bit 0 selects which fan is active */
f74a27d4
HMH
8020
8021 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
8022 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
8023
8024 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
8025};
8026
8027enum fan_status_access_mode {
8028 TPACPI_FAN_NONE = 0, /* No fan status or control */
8029 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
8030 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8031};
8032
8033enum fan_control_access_mode {
8034 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8035 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8036 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8037 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8038};
8039
8040enum fan_control_commands {
8041 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8042 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8043 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8044 * and also watchdog cmd */
8045};
8046
90ab5ee9 8047static bool fan_control_allowed;
f74a27d4 8048
69ba91cb
HMH
8049static enum fan_status_access_mode fan_status_access_mode;
8050static enum fan_control_access_mode fan_control_access_mode;
8051static enum fan_control_commands fan_control_commands;
78f81cc4 8052
778b4d74 8053static u8 fan_control_initial_status;
fe98a52c 8054static u8 fan_control_desired_level;
0081b162 8055static u8 fan_control_resume_level;
f74a27d4
HMH
8056static int fan_watchdog_maxinterval;
8057
8058static struct mutex fan_mutex;
778b4d74 8059
25c68a33 8060static void fan_watchdog_fire(struct work_struct *ignored);
25c68a33 8061static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
16663a87 8062
e0c7dfe7
HMH
8063TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8064TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
56b6aeb0
HMH
8065 "\\FSPD", /* 600e/x, 770e, 770x */
8066 ); /* all others */
e0c7dfe7 8067TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
56b6aeb0
HMH
8068 "JFNS", /* 770x-JL */
8069 ); /* all others */
8070
1c2ece75
HMH
8071/*
8072 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8073 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8074 * be in auto mode (0x80).
8075 *
8076 * This is corrected by any write to HFSP either by the driver, or
8077 * by the firmware.
8078 *
8079 * We assume 0x07 really means auto mode while this quirk is active,
8080 * as this is far more likely than the ThinkPad being in level 7,
8081 * which is only used by the firmware during thermal emergencies.
7d95a3d5
HMH
8082 *
8083 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8084 * TP-70 (T43, R52), which are known to be buggy.
1c2ece75
HMH
8085 */
8086
7d95a3d5 8087static void fan_quirk1_setup(void)
1c2ece75 8088{
1c2ece75 8089 if (fan_control_initial_status == 0x07) {
5cac62ac 8090 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
7d95a3d5 8091 tp_features.fan_ctrl_status_undef = 1;
1c2ece75
HMH
8092 }
8093}
8094
8095static void fan_quirk1_handle(u8 *fan_status)
8096{
8097 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8098 if (*fan_status != fan_control_initial_status) {
8099 /* something changed the HFSP regisnter since
8100 * driver init time, so it is not undefined
8101 * anymore */
8102 tp_features.fan_ctrl_status_undef = 0;
8103 } else {
8104 /* Return most likely status. In fact, it
8105 * might be the only possible status */
8106 *fan_status = TP_EC_FAN_AUTO;
8107 }
8108 }
8109}
8110
d7377247
HMH
8111/* Select main fan on X60/X61, NOOP on others */
8112static bool fan_select_fan1(void)
8113{
8114 if (tp_features.second_fan) {
8115 u8 val;
8116
8117 if (ec_read(fan_select_offset, &val) < 0)
8118 return false;
8119 val &= 0xFEU;
8120 if (ec_write(fan_select_offset, val) < 0)
8121 return false;
8122 }
8123 return true;
8124}
8125
8126/* Select secondary fan on X60/X61 */
8127static bool fan_select_fan2(void)
8128{
8129 u8 val;
8130
8131 if (!tp_features.second_fan)
8132 return false;
8133
8134 if (ec_read(fan_select_offset, &val) < 0)
8135 return false;
8136 val |= 0x01U;
8137 if (ec_write(fan_select_offset, val) < 0)
8138 return false;
8139
8140 return true;
8141}
8142
fe98a52c 8143/*
b21a15f6 8144 * Call with fan_mutex held
fe98a52c 8145 */
b21a15f6 8146static void fan_update_desired_level(u8 status)
fe98a52c 8147{
b21a15f6
HMH
8148 if ((status &
8149 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8150 if (status > 7)
8151 fan_control_desired_level = 7;
8152 else
8153 fan_control_desired_level = status;
8154 }
8155}
fe98a52c 8156
b21a15f6
HMH
8157static int fan_get_status(u8 *status)
8158{
8159 u8 s;
fe98a52c 8160
b21a15f6
HMH
8161 /* TODO:
8162 * Add TPACPI_FAN_RD_ACPI_FANS ? */
fe98a52c 8163
b21a15f6 8164 switch (fan_status_access_mode) {
eceeb437 8165 case TPACPI_FAN_RD_ACPI_GFAN: {
b21a15f6 8166 /* 570, 600e/x, 770e, 770x */
eceeb437 8167 int res;
fe98a52c 8168
eceeb437 8169 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
b21a15f6 8170 return -EIO;
fe98a52c 8171
b21a15f6 8172 if (likely(status))
eceeb437 8173 *status = res & 0x07;
fe98a52c 8174
b21a15f6 8175 break;
eceeb437 8176 }
b21a15f6
HMH
8177 case TPACPI_FAN_RD_TPEC:
8178 /* all except 570, 600e/x, 770e, 770x */
8179 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8180 return -EIO;
8181
1c2ece75 8182 if (likely(status)) {
b21a15f6 8183 *status = s;
1c2ece75
HMH
8184 fan_quirk1_handle(status);
8185 }
fe98a52c 8186
fe98a52c 8187 break;
b21a15f6 8188
fe98a52c 8189 default:
b21a15f6 8190 return -ENXIO;
fe98a52c
HMH
8191 }
8192
b21a15f6
HMH
8193 return 0;
8194}
fe98a52c 8195
b21a15f6
HMH
8196static int fan_get_status_safe(u8 *status)
8197{
8198 int rc;
8199 u8 s;
fe98a52c 8200
7646ea88 8201 if (mutex_lock_killable(&fan_mutex))
b21a15f6
HMH
8202 return -ERESTARTSYS;
8203 rc = fan_get_status(&s);
8204 if (!rc)
8205 fan_update_desired_level(s);
8206 mutex_unlock(&fan_mutex);
fe98a52c 8207
a7718360
DC
8208 if (rc)
8209 return rc;
b21a15f6
HMH
8210 if (status)
8211 *status = s;
fe98a52c 8212
a7718360 8213 return 0;
b21a15f6
HMH
8214}
8215
8216static int fan_get_speed(unsigned int *speed)
fe98a52c 8217{
b21a15f6 8218 u8 hi, lo;
fe98a52c 8219
b21a15f6
HMH
8220 switch (fan_status_access_mode) {
8221 case TPACPI_FAN_RD_TPEC:
8222 /* all except 570, 600e/x, 770e, 770x */
d7377247
HMH
8223 if (unlikely(!fan_select_fan1()))
8224 return -EIO;
b21a15f6
HMH
8225 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8226 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8227 return -EIO;
fe98a52c 8228
b21a15f6
HMH
8229 if (likely(speed))
8230 *speed = (hi << 8) | lo;
fe98a52c 8231
b21a15f6 8232 break;
fe98a52c 8233
b21a15f6
HMH
8234 default:
8235 return -ENXIO;
8236 }
fe98a52c 8237
b21a15f6 8238 return 0;
fe98a52c
HMH
8239}
8240
d7377247
HMH
8241static int fan2_get_speed(unsigned int *speed)
8242{
8243 u8 hi, lo;
8244 bool rc;
8245
8246 switch (fan_status_access_mode) {
8247 case TPACPI_FAN_RD_TPEC:
8248 /* all except 570, 600e/x, 770e, 770x */
8249 if (unlikely(!fan_select_fan2()))
8250 return -EIO;
8251 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8252 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8253 fan_select_fan1(); /* play it safe */
8254 if (rc)
8255 return -EIO;
8256
8257 if (likely(speed))
8258 *speed = (hi << 8) | lo;
8259
8260 break;
8261
8262 default:
8263 return -ENXIO;
8264 }
8265
8266 return 0;
8267}
8268
b21a15f6 8269static int fan_set_level(int level)
fe98a52c 8270{
b21a15f6
HMH
8271 if (!fan_control_allowed)
8272 return -EPERM;
fe98a52c 8273
b21a15f6
HMH
8274 switch (fan_control_access_mode) {
8275 case TPACPI_FAN_WR_ACPI_SFAN:
8276 if (level >= 0 && level <= 7) {
8277 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8278 return -EIO;
8279 } else
8280 return -EINVAL;
8281 break;
fe98a52c 8282
b21a15f6
HMH
8283 case TPACPI_FAN_WR_ACPI_FANS:
8284 case TPACPI_FAN_WR_TPEC:
0081b162
HMH
8285 if (!(level & TP_EC_FAN_AUTO) &&
8286 !(level & TP_EC_FAN_FULLSPEED) &&
b21a15f6
HMH
8287 ((level < 0) || (level > 7)))
8288 return -EINVAL;
fe98a52c 8289
b21a15f6
HMH
8290 /* safety net should the EC not support AUTO
8291 * or FULLSPEED mode bits and just ignore them */
8292 if (level & TP_EC_FAN_FULLSPEED)
8293 level |= 7; /* safety min speed 7 */
547266e4 8294 else if (level & TP_EC_FAN_AUTO)
b21a15f6 8295 level |= 4; /* safety min speed 4 */
fe98a52c 8296
b21a15f6
HMH
8297 if (!acpi_ec_write(fan_status_offset, level))
8298 return -EIO;
8299 else
8300 tp_features.fan_ctrl_status_undef = 0;
8301 break;
fe98a52c 8302
b21a15f6
HMH
8303 default:
8304 return -ENXIO;
8305 }
74a60c0f
HMH
8306
8307 vdbg_printk(TPACPI_DBG_FAN,
8308 "fan control: set fan control register to 0x%02x\n", level);
b21a15f6 8309 return 0;
fe98a52c
HMH
8310}
8311
b21a15f6 8312static int fan_set_level_safe(int level)
fe98a52c 8313{
b21a15f6 8314 int rc;
fe98a52c 8315
b21a15f6
HMH
8316 if (!fan_control_allowed)
8317 return -EPERM;
fe98a52c 8318
7646ea88 8319 if (mutex_lock_killable(&fan_mutex))
b21a15f6 8320 return -ERESTARTSYS;
fe98a52c 8321
b21a15f6
HMH
8322 if (level == TPACPI_FAN_LAST_LEVEL)
8323 level = fan_control_desired_level;
fe98a52c 8324
b21a15f6
HMH
8325 rc = fan_set_level(level);
8326 if (!rc)
8327 fan_update_desired_level(level);
8328
8329 mutex_unlock(&fan_mutex);
8330 return rc;
fe98a52c
HMH
8331}
8332
b21a15f6 8333static int fan_set_enable(void)
fe98a52c 8334{
b21a15f6
HMH
8335 u8 s;
8336 int rc;
fe98a52c 8337
ecf2a80a
HMH
8338 if (!fan_control_allowed)
8339 return -EPERM;
8340
7646ea88 8341 if (mutex_lock_killable(&fan_mutex))
b21a15f6 8342 return -ERESTARTSYS;
fe98a52c 8343
b21a15f6
HMH
8344 switch (fan_control_access_mode) {
8345 case TPACPI_FAN_WR_ACPI_FANS:
8346 case TPACPI_FAN_WR_TPEC:
8347 rc = fan_get_status(&s);
8348 if (rc < 0)
8349 break;
fe98a52c 8350
b21a15f6
HMH
8351 /* Don't go out of emergency fan mode */
8352 if (s != 7) {
8353 s &= 0x07;
8354 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8355 }
fe98a52c 8356
b21a15f6
HMH
8357 if (!acpi_ec_write(fan_status_offset, s))
8358 rc = -EIO;
8359 else {
8360 tp_features.fan_ctrl_status_undef = 0;
8361 rc = 0;
8362 }
8363 break;
fe98a52c 8364
b21a15f6
HMH
8365 case TPACPI_FAN_WR_ACPI_SFAN:
8366 rc = fan_get_status(&s);
8367 if (rc < 0)
8368 break;
fe98a52c 8369
b21a15f6 8370 s &= 0x07;
fe98a52c 8371
b21a15f6
HMH
8372 /* Set fan to at least level 4 */
8373 s |= 4;
fe08bc4b 8374
b21a15f6 8375 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
35ff8b9f 8376 rc = -EIO;
b21a15f6
HMH
8377 else
8378 rc = 0;
8379 break;
78f81cc4 8380
b21a15f6
HMH
8381 default:
8382 rc = -ENXIO;
8383 }
5fba344c 8384
b21a15f6 8385 mutex_unlock(&fan_mutex);
74a60c0f
HMH
8386
8387 if (!rc)
8388 vdbg_printk(TPACPI_DBG_FAN,
8389 "fan control: set fan control register to 0x%02x\n",
8390 s);
b21a15f6 8391 return rc;
69ba91cb 8392}
78f81cc4 8393
b21a15f6 8394static int fan_set_disable(void)
78f81cc4 8395{
b21a15f6 8396 int rc;
c52f0aa5 8397
b21a15f6
HMH
8398 if (!fan_control_allowed)
8399 return -EPERM;
78f81cc4 8400
7646ea88 8401 if (mutex_lock_killable(&fan_mutex))
b21a15f6 8402 return -ERESTARTSYS;
3ef8a609 8403
b21a15f6
HMH
8404 rc = 0;
8405 switch (fan_control_access_mode) {
8406 case TPACPI_FAN_WR_ACPI_FANS:
8407 case TPACPI_FAN_WR_TPEC:
8408 if (!acpi_ec_write(fan_status_offset, 0x00))
8409 rc = -EIO;
8410 else {
8411 fan_control_desired_level = 0;
8412 tp_features.fan_ctrl_status_undef = 0;
8413 }
3ef8a609
HMH
8414 break;
8415
b21a15f6
HMH
8416 case TPACPI_FAN_WR_ACPI_SFAN:
8417 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8418 rc = -EIO;
8419 else
8420 fan_control_desired_level = 0;
c52f0aa5
HMH
8421 break;
8422
8423 default:
b21a15f6 8424 rc = -ENXIO;
78f81cc4
BD
8425 }
8426
74a60c0f
HMH
8427 if (!rc)
8428 vdbg_printk(TPACPI_DBG_FAN,
8429 "fan control: set fan control register to 0\n");
fe98a52c 8430
fe98a52c 8431 mutex_unlock(&fan_mutex);
fe98a52c
HMH
8432 return rc;
8433}
8434
b21a15f6 8435static int fan_set_speed(int speed)
c52f0aa5 8436{
b21a15f6 8437 int rc;
c52f0aa5 8438
b21a15f6
HMH
8439 if (!fan_control_allowed)
8440 return -EPERM;
3ef8a609 8441
7646ea88 8442 if (mutex_lock_killable(&fan_mutex))
b21a15f6 8443 return -ERESTARTSYS;
c52f0aa5 8444
b21a15f6
HMH
8445 rc = 0;
8446 switch (fan_control_access_mode) {
8447 case TPACPI_FAN_WR_ACPI_FANS:
8448 if (speed >= 0 && speed <= 65535) {
8449 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8450 speed, speed, speed))
8451 rc = -EIO;
8452 } else
8453 rc = -EINVAL;
c52f0aa5
HMH
8454 break;
8455
8456 default:
b21a15f6 8457 rc = -ENXIO;
c52f0aa5
HMH
8458 }
8459
b21a15f6
HMH
8460 mutex_unlock(&fan_mutex);
8461 return rc;
16663a87
HMH
8462}
8463
8464static void fan_watchdog_reset(void)
8465{
4985cd0a
HMH
8466 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8467 return;
8468
8fef502e 8469 if (fan_watchdog_maxinterval > 0 &&
41f63c53
TH
8470 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8471 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8472 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8473 else
8474 cancel_delayed_work(&fan_watchdog_task);
16663a87
HMH
8475}
8476
b21a15f6 8477static void fan_watchdog_fire(struct work_struct *ignored)
78f81cc4 8478{
b21a15f6 8479 int rc;
18ad7996 8480
b21a15f6
HMH
8481 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8482 return;
a12095c2 8483
0978e012 8484 pr_notice("fan watchdog: enabling fan\n");
b21a15f6
HMH
8485 rc = fan_set_enable();
8486 if (rc < 0) {
5cac62ac
AS
8487 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8488 rc);
b21a15f6
HMH
8489 /* reschedule for later */
8490 fan_watchdog_reset();
8491 }
8492}
eaa7571b 8493
b21a15f6
HMH
8494/*
8495 * SYSFS fan layout: hwmon compatible (device)
8496 *
8497 * pwm*_enable:
8498 * 0: "disengaged" mode
8499 * 1: manual mode
8500 * 2: native EC "auto" mode (recommended, hardware default)
8501 *
8502 * pwm*: set speed in manual mode, ignored otherwise.
8503 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8504 * interpolation.
8505 *
8506 * fan*_input: tachometer reading, RPM
8507 *
8508 *
8509 * SYSFS fan layout: extensions
8510 *
8511 * fan_watchdog (driver):
8512 * fan watchdog interval in seconds, 0 disables (default), max 120
8513 */
8514
8515/* sysfs fan pwm1_enable ----------------------------------------------- */
8516static ssize_t fan_pwm1_enable_show(struct device *dev,
8517 struct device_attribute *attr,
8518 char *buf)
8519{
8520 int res, mode;
8521 u8 status;
8522
8523 res = fan_get_status_safe(&status);
8524 if (res)
8525 return res;
8526
b21a15f6
HMH
8527 if (status & TP_EC_FAN_FULLSPEED) {
8528 mode = 0;
8529 } else if (status & TP_EC_FAN_AUTO) {
8530 mode = 2;
8531 } else
8532 mode = 1;
8533
8534 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8535}
a12095c2 8536
b21a15f6
HMH
8537static ssize_t fan_pwm1_enable_store(struct device *dev,
8538 struct device_attribute *attr,
8539 const char *buf, size_t count)
8540{
8541 unsigned long t;
8542 int res, level;
8543
8544 if (parse_strtoul(buf, 2, &t))
8545 return -EINVAL;
8546
74a60c0f
HMH
8547 tpacpi_disclose_usertask("hwmon pwm1_enable",
8548 "set fan mode to %lu\n", t);
8549
b21a15f6
HMH
8550 switch (t) {
8551 case 0:
8552 level = TP_EC_FAN_FULLSPEED;
8553 break;
8554 case 1:
8555 level = TPACPI_FAN_LAST_LEVEL;
8556 break;
8557 case 2:
8558 level = TP_EC_FAN_AUTO;
8559 break;
8560 case 3:
8561 /* reserved for software-controlled auto mode */
8562 return -ENOSYS;
18ad7996 8563 default:
b21a15f6 8564 return -EINVAL;
18ad7996 8565 }
b21a15f6
HMH
8566
8567 res = fan_set_level_safe(level);
8568 if (res == -ENXIO)
8569 return -EINVAL;
8570 else if (res < 0)
8571 return res;
8572
8573 fan_watchdog_reset();
8574
8575 return count;
18ad7996
HMH
8576}
8577
5fb73bc2
BM
8578static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8579 fan_pwm1_enable_show, fan_pwm1_enable_store);
b21a15f6
HMH
8580
8581/* sysfs fan pwm1 ------------------------------------------------------ */
8582static ssize_t fan_pwm1_show(struct device *dev,
8583 struct device_attribute *attr,
8584 char *buf)
fe98a52c 8585{
b21a15f6
HMH
8586 int res;
8587 u8 status;
fe98a52c 8588
b21a15f6
HMH
8589 res = fan_get_status_safe(&status);
8590 if (res)
8591 return res;
ecf2a80a 8592
b21a15f6
HMH
8593 if ((status &
8594 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8595 status = fan_control_desired_level;
fe98a52c 8596
b21a15f6
HMH
8597 if (status > 7)
8598 status = 7;
fe98a52c 8599
b21a15f6 8600 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
fe98a52c
HMH
8601}
8602
b21a15f6
HMH
8603static ssize_t fan_pwm1_store(struct device *dev,
8604 struct device_attribute *attr,
8605 const char *buf, size_t count)
18ad7996 8606{
b21a15f6 8607 unsigned long s;
1c6a334e 8608 int rc;
b21a15f6 8609 u8 status, newlevel;
1c6a334e 8610
b21a15f6
HMH
8611 if (parse_strtoul(buf, 255, &s))
8612 return -EINVAL;
8613
74a60c0f
HMH
8614 tpacpi_disclose_usertask("hwmon pwm1",
8615 "set fan speed to %lu\n", s);
8616
b21a15f6
HMH
8617 /* scale down from 0-255 to 0-7 */
8618 newlevel = (s >> 5) & 0x07;
ecf2a80a 8619
7646ea88 8620 if (mutex_lock_killable(&fan_mutex))
fc589a3c 8621 return -ERESTARTSYS;
40ca9fdf 8622
b21a15f6
HMH
8623 rc = fan_get_status(&status);
8624 if (!rc && (status &
8625 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8626 rc = fan_set_level(newlevel);
8627 if (rc == -ENXIO)
8628 rc = -EINVAL;
8629 else if (!rc) {
8630 fan_update_desired_level(newlevel);
8631 fan_watchdog_reset();
eaa7571b 8632 }
b21a15f6 8633 }
1c6a334e 8634
b21a15f6 8635 mutex_unlock(&fan_mutex);
72a979f0 8636 return (rc) ? rc : count;
b21a15f6 8637}
1c6a334e 8638
5fb73bc2 8639static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
1c6a334e 8640
b21a15f6
HMH
8641/* sysfs fan fan1_input ------------------------------------------------ */
8642static ssize_t fan_fan1_input_show(struct device *dev,
8643 struct device_attribute *attr,
8644 char *buf)
8645{
8646 int res;
8647 unsigned int speed;
1c6a334e 8648
b21a15f6
HMH
8649 res = fan_get_speed(&speed);
8650 if (res < 0)
8651 return res;
1c6a334e 8652
b21a15f6
HMH
8653 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8654}
18ad7996 8655
5fb73bc2 8656static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
40ca9fdf 8657
d7377247
HMH
8658/* sysfs fan fan2_input ------------------------------------------------ */
8659static ssize_t fan_fan2_input_show(struct device *dev,
8660 struct device_attribute *attr,
8661 char *buf)
8662{
8663 int res;
8664 unsigned int speed;
8665
8666 res = fan2_get_speed(&speed);
8667 if (res < 0)
8668 return res;
8669
8670 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8671}
8672
5fb73bc2 8673static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
d7377247 8674
b21a15f6 8675/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
ac3054c4 8676static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
b21a15f6
HMH
8677{
8678 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
18ad7996
HMH
8679}
8680
ac3054c4
GKH
8681static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8682 size_t count)
18ad7996 8683{
b21a15f6
HMH
8684 unsigned long t;
8685
8686 if (parse_strtoul(buf, 120, &t))
8687 return -EINVAL;
40ca9fdf 8688
ecf2a80a
HMH
8689 if (!fan_control_allowed)
8690 return -EPERM;
8691
b21a15f6
HMH
8692 fan_watchdog_maxinterval = t;
8693 fan_watchdog_reset();
40ca9fdf 8694
74a60c0f
HMH
8695 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8696
b21a15f6
HMH
8697 return count;
8698}
ac3054c4 8699static DRIVER_ATTR_RW(fan_watchdog);
b21a15f6
HMH
8700
8701/* --------------------------------------------------------------------- */
8702static struct attribute *fan_attributes[] = {
5fb73bc2
BM
8703 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8704 &dev_attr_fan1_input.attr,
d7377247 8705 NULL, /* for fan2_input */
b21a15f6
HMH
8706 NULL
8707};
8708
8709static const struct attribute_group fan_attr_group = {
8710 .attrs = fan_attributes,
8711};
8712
d7377247
HMH
8713#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8714#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7d95a3d5
HMH
8715
8716#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8717 { .vendor = PCI_VENDOR_ID_IBM, \
8718 .bios = TPACPI_MATCH_ANY, \
8719 .ec = TPID(__id1, __id2), \
8720 .quirks = __quirks }
8721
d7377247
HMH
8722#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8723 { .vendor = PCI_VENDOR_ID_LENOVO, \
8724 .bios = TPACPI_MATCH_ANY, \
8725 .ec = TPID(__id1, __id2), \
8726 .quirks = __quirks }
8727
a986c75a
AK
8728#define TPACPI_FAN_QB(__id1, __id2, __quirks) \
8729 { .vendor = PCI_VENDOR_ID_LENOVO, \
8730 .bios = TPID(__id1, __id2), \
8731 .ec = TPACPI_MATCH_ANY, \
8732 .quirks = __quirks }
8733
7d95a3d5
HMH
8734static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8735 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8736 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8737 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8738 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
d7377247 8739 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
a986c75a 8740 TPACPI_FAN_QB('N', '1', TPACPI_FAN_2FAN),
7d95a3d5
HMH
8741};
8742
d7377247 8743#undef TPACPI_FAN_QL
7d95a3d5 8744#undef TPACPI_FAN_QI
a986c75a 8745#undef TPACPI_FAN_QB
7d95a3d5 8746
b21a15f6
HMH
8747static int __init fan_init(struct ibm_init_struct *iibm)
8748{
8749 int rc;
7d95a3d5 8750 unsigned long quirks;
b21a15f6 8751
74a60c0f
HMH
8752 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8753 "initializing fan subdriver\n");
b21a15f6
HMH
8754
8755 mutex_init(&fan_mutex);
8756 fan_status_access_mode = TPACPI_FAN_NONE;
8757 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8758 fan_control_commands = 0;
8759 fan_watchdog_maxinterval = 0;
8760 tp_features.fan_ctrl_status_undef = 0;
d7377247 8761 tp_features.second_fan = 0;
b21a15f6
HMH
8762 fan_control_desired_level = 7;
8763
e28393c0
HMH
8764 if (tpacpi_is_ibm()) {
8765 TPACPI_ACPIHANDLE_INIT(fans);
8766 TPACPI_ACPIHANDLE_INIT(gfan);
8767 TPACPI_ACPIHANDLE_INIT(sfan);
8768 }
b21a15f6 8769
7d95a3d5
HMH
8770 quirks = tpacpi_check_quirks(fan_quirk_table,
8771 ARRAY_SIZE(fan_quirk_table));
8772
b21a15f6
HMH
8773 if (gfan_handle) {
8774 /* 570, 600e/x, 770e, 770x */
8775 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8776 } else {
8777 /* all other ThinkPads: note that even old-style
8778 * ThinkPad ECs supports the fan control register */
8779 if (likely(acpi_ec_read(fan_status_offset,
8780 &fan_control_initial_status))) {
8781 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7d95a3d5
HMH
8782 if (quirks & TPACPI_FAN_Q1)
8783 fan_quirk1_setup();
d7377247
HMH
8784 if (quirks & TPACPI_FAN_2FAN) {
8785 tp_features.second_fan = 1;
8786 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8787 "secondary fan support enabled\n");
8788 }
b21a15f6 8789 } else {
5cac62ac 8790 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
b21a15f6
HMH
8791 return 1;
8792 }
8793 }
8794
8795 if (sfan_handle) {
8796 /* 570, 770x-JL */
8797 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8798 fan_control_commands |=
8799 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8800 } else {
8801 if (!gfan_handle) {
8802 /* gfan without sfan means no fan control */
8803 /* all other models implement TP EC 0x2f control */
8804
8805 if (fans_handle) {
8806 /* X31, X40, X41 */
8807 fan_control_access_mode =
8808 TPACPI_FAN_WR_ACPI_FANS;
8809 fan_control_commands |=
8810 TPACPI_FAN_CMD_SPEED |
8811 TPACPI_FAN_CMD_LEVEL |
8812 TPACPI_FAN_CMD_ENABLE;
8813 } else {
8814 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8815 fan_control_commands |=
8816 TPACPI_FAN_CMD_LEVEL |
8817 TPACPI_FAN_CMD_ENABLE;
8818 }
fe98a52c 8819 }
b21a15f6 8820 }
18ad7996 8821
74a60c0f
HMH
8822 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8823 "fan is %s, modes %d, %d\n",
b21a15f6
HMH
8824 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8825 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8826 fan_status_access_mode, fan_control_access_mode);
1c6a334e 8827
b21a15f6
HMH
8828 /* fan control master switch */
8829 if (!fan_control_allowed) {
8830 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8831 fan_control_commands = 0;
74a60c0f 8832 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
b21a15f6 8833 "fan control features disabled by parameter\n");
18ad7996 8834 }
40ca9fdf 8835
b21a15f6
HMH
8836 /* update fan_control_desired_level */
8837 if (fan_status_access_mode != TPACPI_FAN_NONE)
8838 fan_get_status_safe(NULL);
fe98a52c 8839
b21a15f6
HMH
8840 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8841 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
d7377247
HMH
8842 if (tp_features.second_fan) {
8843 /* attach second fan tachometer */
8844 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
5fb73bc2 8845 &dev_attr_fan2_input.attr;
d7377247 8846 }
6b99e356 8847 rc = sysfs_create_group(&tpacpi_hwmon->kobj,
b21a15f6 8848 &fan_attr_group);
b21a15f6
HMH
8849 if (rc < 0)
8850 return rc;
9c0a76e1
HMH
8851
8852 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8853 &driver_attr_fan_watchdog);
8854 if (rc < 0) {
6b99e356 8855 sysfs_remove_group(&tpacpi_hwmon->kobj,
9c0a76e1
HMH
8856 &fan_attr_group);
8857 return rc;
8858 }
b21a15f6
HMH
8859 return 0;
8860 } else
8861 return 1;
56b6aeb0
HMH
8862}
8863
b21a15f6 8864static void fan_exit(void)
56b6aeb0 8865{
74a60c0f 8866 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
35ff8b9f 8867 "cancelling any pending fan watchdog tasks\n");
56b6aeb0 8868
b21a15f6 8869 /* FIXME: can we really do this unconditionally? */
6b99e356 8870 sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
35ff8b9f
HMH
8871 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8872 &driver_attr_fan_watchdog);
40ca9fdf 8873
b21a15f6 8874 cancel_delayed_work(&fan_watchdog_task);
e0e3c061 8875 flush_workqueue(tpacpi_wq);
56b6aeb0
HMH
8876}
8877
fd3c3a42 8878static void fan_suspend(void)
75700e53 8879{
0081b162
HMH
8880 int rc;
8881
75700e53
HMH
8882 if (!fan_control_allowed)
8883 return;
8884
8885 /* Store fan status in cache */
0081b162
HMH
8886 fan_control_resume_level = 0;
8887 rc = fan_get_status_safe(&fan_control_resume_level);
8888 if (rc < 0)
5cac62ac
AS
8889 pr_notice("failed to read fan level for later restore during resume: %d\n",
8890 rc);
0081b162
HMH
8891
8892 /* if it is undefined, don't attempt to restore it.
8893 * KEEP THIS LAST */
75700e53 8894 if (tp_features.fan_ctrl_status_undef)
0081b162 8895 fan_control_resume_level = 0;
75700e53
HMH
8896}
8897
8898static void fan_resume(void)
8899{
75700e53
HMH
8900 u8 current_level = 7;
8901 bool do_set = false;
0081b162 8902 int rc;
75700e53
HMH
8903
8904 /* DSDT *always* updates status on resume */
8905 tp_features.fan_ctrl_status_undef = 0;
8906
75700e53 8907 if (!fan_control_allowed ||
0081b162 8908 !fan_control_resume_level ||
75700e53
HMH
8909 (fan_get_status_safe(&current_level) < 0))
8910 return;
8911
8912 switch (fan_control_access_mode) {
8913 case TPACPI_FAN_WR_ACPI_SFAN:
0081b162
HMH
8914 /* never decrease fan level */
8915 do_set = (fan_control_resume_level > current_level);
75700e53
HMH
8916 break;
8917 case TPACPI_FAN_WR_ACPI_FANS:
8918 case TPACPI_FAN_WR_TPEC:
0081b162
HMH
8919 /* never decrease fan level, scale is:
8920 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8921 *
8922 * We expect the firmware to set either 7 or AUTO, but we
8923 * handle FULLSPEED out of paranoia.
8924 *
8925 * So, we can safely only restore FULLSPEED or 7, anything
8926 * else could slow the fan. Restoring AUTO is useless, at
8927 * best that's exactly what the DSDT already set (it is the
8928 * slower it uses).
8929 *
8930 * Always keep in mind that the DSDT *will* have set the
8931 * fans to what the vendor supposes is the best level. We
8932 * muck with it only to speed the fan up.
8933 */
8934 if (fan_control_resume_level != 7 &&
8935 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8936 return;
8937 else
8938 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8939 (current_level != fan_control_resume_level);
75700e53
HMH
8940 break;
8941 default:
8942 return;
8943 }
8944 if (do_set) {
0978e012
JP
8945 pr_notice("restoring fan level to 0x%02x\n",
8946 fan_control_resume_level);
0081b162
HMH
8947 rc = fan_set_level_safe(fan_control_resume_level);
8948 if (rc < 0)
0978e012 8949 pr_notice("failed to restore fan level: %d\n", rc);
75700e53
HMH
8950 }
8951}
8952
887965e6 8953static int fan_read(struct seq_file *m)
56b6aeb0 8954{
56b6aeb0
HMH
8955 int rc;
8956 u8 status;
8957 unsigned int speed = 0;
8958
8959 switch (fan_status_access_mode) {
efa27145 8960 case TPACPI_FAN_RD_ACPI_GFAN:
56b6aeb0 8961 /* 570, 600e/x, 770e, 770x */
35ff8b9f
HMH
8962 rc = fan_get_status_safe(&status);
8963 if (rc < 0)
56b6aeb0
HMH
8964 return rc;
8965
887965e6 8966 seq_printf(m, "status:\t\t%s\n"
56b6aeb0
HMH
8967 "level:\t\t%d\n",
8968 (status != 0) ? "enabled" : "disabled", status);
8969 break;
8970
efa27145 8971 case TPACPI_FAN_RD_TPEC:
56b6aeb0 8972 /* all except 570, 600e/x, 770e, 770x */
35ff8b9f
HMH
8973 rc = fan_get_status_safe(&status);
8974 if (rc < 0)
56b6aeb0
HMH
8975 return rc;
8976
887965e6 8977 seq_printf(m, "status:\t\t%s\n",
56b6aeb0
HMH
8978 (status != 0) ? "enabled" : "disabled");
8979
35ff8b9f
HMH
8980 rc = fan_get_speed(&speed);
8981 if (rc < 0)
56b6aeb0
HMH
8982 return rc;
8983
887965e6 8984 seq_printf(m, "speed:\t\t%d\n", speed);
56b6aeb0 8985
efa27145 8986 if (status & TP_EC_FAN_FULLSPEED)
56b6aeb0 8987 /* Disengaged mode takes precedence */
887965e6 8988 seq_printf(m, "level:\t\tdisengaged\n");
efa27145 8989 else if (status & TP_EC_FAN_AUTO)
887965e6 8990 seq_printf(m, "level:\t\tauto\n");
56b6aeb0 8991 else
887965e6 8992 seq_printf(m, "level:\t\t%d\n", status);
56b6aeb0
HMH
8993 break;
8994
efa27145 8995 case TPACPI_FAN_NONE:
56b6aeb0 8996 default:
887965e6 8997 seq_printf(m, "status:\t\tnot supported\n");
56b6aeb0
HMH
8998 }
8999
efa27145 9000 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
887965e6 9001 seq_printf(m, "commands:\tlevel <level>");
56b6aeb0
HMH
9002
9003 switch (fan_control_access_mode) {
efa27145 9004 case TPACPI_FAN_WR_ACPI_SFAN:
887965e6 9005 seq_printf(m, " (<level> is 0-7)\n");
56b6aeb0
HMH
9006 break;
9007
9008 default:
5cac62ac 9009 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
56b6aeb0
HMH
9010 break;
9011 }
9012 }
18ad7996 9013
efa27145 9014 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
887965e6 9015 seq_printf(m, "commands:\tenable, disable\n"
5cac62ac 9016 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
1da177e4 9017
efa27145 9018 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5cac62ac 9019 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
56b6aeb0 9020
887965e6 9021 return 0;
78f81cc4
BD
9022}
9023
18ad7996
HMH
9024static int fan_write_cmd_level(const char *cmd, int *rc)
9025{
9026 int level;
9027
a12095c2 9028 if (strlencmp(cmd, "level auto") == 0)
efa27145 9029 level = TP_EC_FAN_AUTO;
fe98a52c 9030 else if ((strlencmp(cmd, "level disengaged") == 0) |
35ff8b9f 9031 (strlencmp(cmd, "level full-speed") == 0))
efa27145 9032 level = TP_EC_FAN_FULLSPEED;
a12095c2 9033 else if (sscanf(cmd, "level %d", &level) != 1)
18ad7996
HMH
9034 return 0;
9035
35ff8b9f
HMH
9036 *rc = fan_set_level_safe(level);
9037 if (*rc == -ENXIO)
0978e012
JP
9038 pr_err("level command accepted for unsupported access mode %d\n",
9039 fan_control_access_mode);
74a60c0f
HMH
9040 else if (!*rc)
9041 tpacpi_disclose_usertask("procfs fan",
9042 "set level to %d\n", level);
18ad7996
HMH
9043
9044 return 1;
9045}
9046
9047static int fan_write_cmd_enable(const char *cmd, int *rc)
9048{
9049 if (strlencmp(cmd, "enable") != 0)
9050 return 0;
9051
35ff8b9f
HMH
9052 *rc = fan_set_enable();
9053 if (*rc == -ENXIO)
0978e012
JP
9054 pr_err("enable command accepted for unsupported access mode %d\n",
9055 fan_control_access_mode);
74a60c0f
HMH
9056 else if (!*rc)
9057 tpacpi_disclose_usertask("procfs fan", "enable\n");
18ad7996
HMH
9058
9059 return 1;
9060}
9061
9062static int fan_write_cmd_disable(const char *cmd, int *rc)
9063{
9064 if (strlencmp(cmd, "disable") != 0)
9065 return 0;
9066
35ff8b9f
HMH
9067 *rc = fan_set_disable();
9068 if (*rc == -ENXIO)
0978e012
JP
9069 pr_err("disable command accepted for unsupported access mode %d\n",
9070 fan_control_access_mode);
74a60c0f
HMH
9071 else if (!*rc)
9072 tpacpi_disclose_usertask("procfs fan", "disable\n");
18ad7996
HMH
9073
9074 return 1;
9075}
9076
9077static int fan_write_cmd_speed(const char *cmd, int *rc)
9078{
9079 int speed;
9080
a8b7a662
HMH
9081 /* TODO:
9082 * Support speed <low> <medium> <high> ? */
9083
18ad7996
HMH
9084 if (sscanf(cmd, "speed %d", &speed) != 1)
9085 return 0;
9086
35ff8b9f
HMH
9087 *rc = fan_set_speed(speed);
9088 if (*rc == -ENXIO)
0978e012
JP
9089 pr_err("speed command accepted for unsupported access mode %d\n",
9090 fan_control_access_mode);
74a60c0f
HMH
9091 else if (!*rc)
9092 tpacpi_disclose_usertask("procfs fan",
9093 "set speed to %d\n", speed);
18ad7996
HMH
9094
9095 return 1;
9096}
9097
16663a87
HMH
9098static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9099{
9100 int interval;
9101
9102 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9103 return 0;
9104
9105 if (interval < 0 || interval > 120)
9106 *rc = -EINVAL;
74a60c0f 9107 else {
16663a87 9108 fan_watchdog_maxinterval = interval;
74a60c0f
HMH
9109 tpacpi_disclose_usertask("procfs fan",
9110 "set watchdog timer to %d\n",
9111 interval);
9112 }
16663a87
HMH
9113
9114 return 1;
9115}
9116
18ad7996
HMH
9117static int fan_write(char *buf)
9118{
9119 char *cmd;
9120 int rc = 0;
9121
9122 while (!rc && (cmd = next_cmd(&buf))) {
efa27145 9123 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
18ad7996 9124 fan_write_cmd_level(cmd, &rc)) &&
efa27145 9125 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
18ad7996 9126 (fan_write_cmd_enable(cmd, &rc) ||
16663a87
HMH
9127 fan_write_cmd_disable(cmd, &rc) ||
9128 fan_write_cmd_watchdog(cmd, &rc))) &&
efa27145 9129 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
18ad7996
HMH
9130 fan_write_cmd_speed(cmd, &rc))
9131 )
9132 rc = -EINVAL;
16663a87
HMH
9133 else if (!rc)
9134 fan_watchdog_reset();
18ad7996
HMH
9135 }
9136
9137 return rc;
9138}
9139
a5763f22
HMH
9140static struct ibm_struct fan_driver_data = {
9141 .name = "fan",
9142 .read = fan_read,
9143 .write = fan_write,
9144 .exit = fan_exit,
75700e53
HMH
9145 .suspend = fan_suspend,
9146 .resume = fan_resume,
a5763f22
HMH
9147};
9148
420f9739
DH
9149/*************************************************************************
9150 * Mute LED subdriver
9151 */
9152
9153
9154struct tp_led_table {
9155 acpi_string name;
9156 int on_value;
9157 int off_value;
9158 int state;
9159};
9160
9161static struct tp_led_table led_tables[] = {
9162 [TPACPI_LED_MUTE] = {
9163 .name = "SSMS",
9164 .on_value = 1,
9165 .off_value = 0,
9166 },
9167 [TPACPI_LED_MICMUTE] = {
9168 .name = "MMTS",
9169 .on_value = 2,
9170 .off_value = 0,
9171 },
9172};
9173
9174static int mute_led_on_off(struct tp_led_table *t, bool state)
9175{
9176 acpi_handle temp;
9177 int output;
9178
3526ecad 9179 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
420f9739
DH
9180 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9181 return -EIO;
9182 }
9183
9184 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9185 state ? t->on_value : t->off_value))
9186 return -EIO;
9187
9188 t->state = state;
9189 return state;
9190}
9191
9192int tpacpi_led_set(int whichled, bool on)
9193{
9194 struct tp_led_table *t;
9195
9196 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9197 return -EINVAL;
9198
9199 t = &led_tables[whichled];
9200 if (t->state < 0 || t->state == on)
9201 return t->state;
9202 return mute_led_on_off(t, on);
9203}
9204EXPORT_SYMBOL_GPL(tpacpi_led_set);
9205
4e1d0924
TI
9206static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
9207 enum led_brightness brightness)
9208{
9209 return tpacpi_led_set(TPACPI_LED_MUTE, brightness != LED_OFF);
9210}
9211
9212static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
9213 enum led_brightness brightness)
9214{
9215 return tpacpi_led_set(TPACPI_LED_MICMUTE, brightness != LED_OFF);
9216}
9217
9218static struct led_classdev mute_led_cdev[] = {
9219 [TPACPI_LED_MUTE] = {
9220 .name = "platform::mute",
9221 .max_brightness = 1,
9222 .brightness_set_blocking = tpacpi_led_mute_set,
9223 .default_trigger = "audio-mute",
9224 },
9225 [TPACPI_LED_MICMUTE] = {
9226 .name = "platform::micmute",
9227 .max_brightness = 1,
9228 .brightness_set_blocking = tpacpi_led_micmute_set,
9229 .default_trigger = "audio-micmute",
9230 },
9231};
9232
420f9739
DH
9233static int mute_led_init(struct ibm_init_struct *iibm)
9234{
4e1d0924
TI
9235 static enum led_audio types[] = {
9236 [TPACPI_LED_MUTE] = LED_AUDIO_MUTE,
9237 [TPACPI_LED_MICMUTE] = LED_AUDIO_MICMUTE,
9238 };
420f9739 9239 acpi_handle temp;
4e1d0924 9240 int i, err;
420f9739
DH
9241
9242 for (i = 0; i < TPACPI_LED_MAX; i++) {
9243 struct tp_led_table *t = &led_tables[i];
4e1d0924 9244 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
420f9739 9245 t->state = -ENODEV;
4e1d0924
TI
9246 continue;
9247 }
9248
9249 mute_led_cdev[i].brightness = ledtrig_audio_get(types[i]);
9250 err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
9251 if (err < 0) {
9252 while (i--) {
9253 if (led_tables[i].state >= 0)
9254 led_classdev_unregister(&mute_led_cdev[i]);
9255 }
9256 return err;
9257 }
420f9739
DH
9258 }
9259 return 0;
9260}
9261
9262static void mute_led_exit(void)
9263{
9264 int i;
9265
4e1d0924
TI
9266 for (i = 0; i < TPACPI_LED_MAX; i++) {
9267 if (led_tables[i].state >= 0) {
9268 led_classdev_unregister(&mute_led_cdev[i]);
9269 tpacpi_led_set(i, false);
9270 }
9271 }
420f9739
DH
9272}
9273
119f4498
TI
9274static void mute_led_resume(void)
9275{
9276 int i;
9277
9278 for (i = 0; i < TPACPI_LED_MAX; i++) {
9279 struct tp_led_table *t = &led_tables[i];
9280 if (t->state >= 0)
9281 mute_led_on_off(t, t->state);
9282 }
9283}
9284
420f9739
DH
9285static struct ibm_struct mute_led_driver_data = {
9286 .name = "mute_led",
9287 .exit = mute_led_exit,
119f4498 9288 .resume = mute_led_resume,
420f9739
DH
9289};
9290
2801b968
OG
9291/*
9292 * Battery Wear Control Driver
9293 * Contact: Ognjen Galic <smclt30p@gmail.com>
9294 */
9295
9296/* Metadata */
9297
9298#define GET_START "BCTG"
9299#define SET_START "BCCS"
9300#define GET_STOP "BCSG"
9301#define SET_STOP "BCSS"
9302
9303#define START_ATTR "charge_start_threshold"
9304#define STOP_ATTR "charge_stop_threshold"
9305
9306enum {
9307 BAT_ANY = 0,
9308 BAT_PRIMARY = 1,
9309 BAT_SECONDARY = 2
9310};
9311
9312enum {
9313 /* Error condition bit */
9314 METHOD_ERR = BIT(31),
9315};
9316
9317enum {
9318 /* This is used in the get/set helpers */
9319 THRESHOLD_START,
9320 THRESHOLD_STOP,
9321};
9322
9323struct tpacpi_battery_data {
9324 int charge_start;
9325 int start_support;
9326 int charge_stop;
9327 int stop_support;
9328};
9329
9330struct tpacpi_battery_driver_data {
9331 struct tpacpi_battery_data batteries[3];
9332 int individual_addressing;
9333};
9334
9335static struct tpacpi_battery_driver_data battery_info;
9336
9337/* ACPI helpers/functions/probes */
9338
9339/**
9340 * This evaluates a ACPI method call specific to the battery
9341 * ACPI extension. The specifics are that an error is marked
9342 * in the 32rd bit of the response, so we just check that here.
9343 */
9344static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9345{
9346 int response;
9347
9348 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9349 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9350 return AE_ERROR;
9351 }
9352 if (response & METHOD_ERR) {
9353 acpi_handle_err(hkey_handle,
9354 "%s evaluated but flagged as error", method);
9355 return AE_ERROR;
9356 }
9357 *ret = response;
9358 return AE_OK;
9359}
9360
9361static int tpacpi_battery_get(int what, int battery, int *ret)
9362{
9363 switch (what) {
9364 case THRESHOLD_START:
9365 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9366 return -ENODEV;
9367
9368 /* The value is in the low 8 bits of the response */
9369 *ret = *ret & 0xFF;
9370 return 0;
9371 case THRESHOLD_STOP:
9372 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9373 return -ENODEV;
9374 /* Value is in lower 8 bits */
9375 *ret = *ret & 0xFF;
9376 /*
9377 * On the stop value, if we return 0 that
9378 * does not make any sense. 0 means Default, which
9379 * means that charging stops at 100%, so we return
9380 * that.
9381 */
9382 if (*ret == 0)
9383 *ret = 100;
9384 return 0;
9385 default:
9386 pr_crit("wrong parameter: %d", what);
9387 return -EINVAL;
9388 }
9389}
9390
9391static int tpacpi_battery_set(int what, int battery, int value)
9392{
9393 int param, ret;
9394 /* The first 8 bits are the value of the threshold */
9395 param = value;
9396 /* The battery ID is in bits 8-9, 2 bits */
9397 param |= battery << 8;
9398
9399 switch (what) {
9400 case THRESHOLD_START:
9401 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9402 pr_err("failed to set charge threshold on battery %d",
9403 battery);
9404 return -ENODEV;
9405 }
9406 return 0;
9407 case THRESHOLD_STOP:
9408 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9409 pr_err("failed to set stop threshold: %d", battery);
9410 return -ENODEV;
9411 }
9412 return 0;
9413 default:
9414 pr_crit("wrong parameter: %d", what);
9415 return -EINVAL;
9416 }
9417}
9418
9419static int tpacpi_battery_probe(int battery)
9420{
9421 int ret = 0;
9422
d22296d9
TW
9423 memset(&battery_info.batteries[battery], 0,
9424 sizeof(battery_info.batteries[battery]));
9425
2801b968
OG
9426 /*
9427 * 1) Get the current start threshold
9428 * 2) Check for support
9429 * 3) Get the current stop threshold
9430 * 4) Check for support
9431 */
9432 if (acpi_has_method(hkey_handle, GET_START)) {
9433 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9434 pr_err("Error probing battery %d\n", battery);
9435 return -ENODEV;
9436 }
9437 /* Individual addressing is in bit 9 */
9438 if (ret & BIT(9))
9439 battery_info.individual_addressing = true;
9440 /* Support is marked in bit 8 */
9441 if (ret & BIT(8))
9442 battery_info.batteries[battery].start_support = 1;
9443 else
9444 return -ENODEV;
9445 if (tpacpi_battery_get(THRESHOLD_START, battery,
9446 &battery_info.batteries[battery].charge_start)) {
9447 pr_err("Error probing battery %d\n", battery);
9448 return -ENODEV;
9449 }
9450 }
9451 if (acpi_has_method(hkey_handle, GET_STOP)) {
9452 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9453 pr_err("Error probing battery stop; %d\n", battery);
9454 return -ENODEV;
9455 }
9456 /* Support is marked in bit 8 */
9457 if (ret & BIT(8))
9458 battery_info.batteries[battery].stop_support = 1;
9459 else
9460 return -ENODEV;
9461 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9462 &battery_info.batteries[battery].charge_stop)) {
9463 pr_err("Error probing battery stop: %d\n", battery);
9464 return -ENODEV;
9465 }
9466 }
9467 pr_info("battery %d registered (start %d, stop %d)",
9468 battery,
9469 battery_info.batteries[battery].charge_start,
9470 battery_info.batteries[battery].charge_stop);
9471
9472 return 0;
9473}
9474
9475/* General helper functions */
9476
9477static int tpacpi_battery_get_id(const char *battery_name)
9478{
9479
1a32ebb2
JW
9480 if (strcmp(battery_name, "BAT0") == 0 ||
9481 tp_features.battery_force_primary)
2801b968
OG
9482 return BAT_PRIMARY;
9483 if (strcmp(battery_name, "BAT1") == 0)
9484 return BAT_SECONDARY;
9485 /*
9486 * If for some reason the battery is not BAT0 nor is it
9487 * BAT1, we will assume it's the default, first battery,
9488 * AKA primary.
9489 */
9490 pr_warn("unknown battery %s, assuming primary", battery_name);
9491 return BAT_PRIMARY;
9492}
9493
9494/* sysfs interface */
9495
9496static ssize_t tpacpi_battery_store(int what,
9497 struct device *dev,
9498 const char *buf, size_t count)
9499{
9500 struct power_supply *supply = to_power_supply(dev);
9501 unsigned long value;
9502 int battery, rval;
9503 /*
9504 * Some systems have support for more than
9505 * one battery. If that is the case,
9506 * tpacpi_battery_probe marked that addressing
9507 * them individually is supported, so we do that
9508 * based on the device struct.
9509 *
9510 * On systems that are not supported, we assume
9511 * the primary as most of the ACPI calls fail
9512 * with "Any Battery" as the parameter.
9513 */
9514 if (battery_info.individual_addressing)
9515 /* BAT_PRIMARY or BAT_SECONDARY */
9516 battery = tpacpi_battery_get_id(supply->desc->name);
9517 else
9518 battery = BAT_PRIMARY;
9519
9520 rval = kstrtoul(buf, 10, &value);
9521 if (rval)
9522 return rval;
9523
9524 switch (what) {
9525 case THRESHOLD_START:
9526 if (!battery_info.batteries[battery].start_support)
9527 return -ENODEV;
9528 /* valid values are [0, 99] */
9529 if (value < 0 || value > 99)
9530 return -EINVAL;
9531 if (value > battery_info.batteries[battery].charge_stop)
9532 return -EINVAL;
9533 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9534 return -ENODEV;
9535 battery_info.batteries[battery].charge_start = value;
9536 return count;
9537
9538 case THRESHOLD_STOP:
9539 if (!battery_info.batteries[battery].stop_support)
9540 return -ENODEV;
9541 /* valid values are [1, 100] */
9542 if (value < 1 || value > 100)
9543 return -EINVAL;
9544 if (value < battery_info.batteries[battery].charge_start)
9545 return -EINVAL;
9546 battery_info.batteries[battery].charge_stop = value;
9547 /*
9548 * When 100 is passed to stop, we need to flip
9549 * it to 0 as that the EC understands that as
9550 * "Default", which will charge to 100%
9551 */
9552 if (value == 100)
9553 value = 0;
9554 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9555 return -EINVAL;
9556 return count;
9557 default:
9558 pr_crit("Wrong parameter: %d", what);
9559 return -EINVAL;
9560 }
9561 return count;
9562}
9563
9564static ssize_t tpacpi_battery_show(int what,
9565 struct device *dev,
9566 char *buf)
9567{
9568 struct power_supply *supply = to_power_supply(dev);
9569 int ret, battery;
9570 /*
9571 * Some systems have support for more than
9572 * one battery. If that is the case,
9573 * tpacpi_battery_probe marked that addressing
9574 * them individually is supported, so we;
9575 * based on the device struct.
9576 *
9577 * On systems that are not supported, we assume
9578 * the primary as most of the ACPI calls fail
9579 * with "Any Battery" as the parameter.
9580 */
9581 if (battery_info.individual_addressing)
9582 /* BAT_PRIMARY or BAT_SECONDARY */
9583 battery = tpacpi_battery_get_id(supply->desc->name);
9584 else
9585 battery = BAT_PRIMARY;
9586 if (tpacpi_battery_get(what, battery, &ret))
9587 return -ENODEV;
9588 return sprintf(buf, "%d\n", ret);
9589}
9590
9591static ssize_t charge_start_threshold_show(struct device *device,
9592 struct device_attribute *attr,
9593 char *buf)
9594{
9595 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9596}
9597
9598static ssize_t charge_stop_threshold_show(struct device *device,
9599 struct device_attribute *attr,
9600 char *buf)
9601{
9602 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9603}
9604
9605static ssize_t charge_start_threshold_store(struct device *dev,
9606 struct device_attribute *attr,
9607 const char *buf, size_t count)
9608{
9609 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9610}
9611
9612static ssize_t charge_stop_threshold_store(struct device *dev,
9613 struct device_attribute *attr,
9614 const char *buf, size_t count)
9615{
9616 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9617}
9618
9619static DEVICE_ATTR_RW(charge_start_threshold);
9620static DEVICE_ATTR_RW(charge_stop_threshold);
9621
9622static struct attribute *tpacpi_battery_attrs[] = {
9623 &dev_attr_charge_start_threshold.attr,
9624 &dev_attr_charge_stop_threshold.attr,
9625 NULL,
9626};
9627
9628ATTRIBUTE_GROUPS(tpacpi_battery);
9629
9630/* ACPI battery hooking */
9631
9632static int tpacpi_battery_add(struct power_supply *battery)
9633{
9634 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9635
9636 if (tpacpi_battery_probe(batteryid))
9637 return -ENODEV;
9638 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9639 return -ENODEV;
9640 return 0;
9641}
9642
9643static int tpacpi_battery_remove(struct power_supply *battery)
9644{
9645 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9646 return 0;
9647}
9648
9649static struct acpi_battery_hook battery_hook = {
9650 .add_battery = tpacpi_battery_add,
9651 .remove_battery = tpacpi_battery_remove,
9652 .name = "ThinkPad Battery Extension",
9653};
9654
9655/* Subdriver init/exit */
9656
1a32ebb2
JW
9657static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9658 /*
9659 * Individual addressing is broken on models that expose the
9660 * primary battery as BAT1.
9661 */
6640ee62
JW
9662 TPACPI_Q_LNV('J', '7', true), /* B5400 */
9663 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
9664 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
1a32ebb2 9665 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
6640ee62 9666 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
1a32ebb2
JW
9667};
9668
2801b968
OG
9669static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9670{
d22296d9
TW
9671 memset(&battery_info, 0, sizeof(battery_info));
9672
1a32ebb2
JW
9673 tp_features.battery_force_primary = tpacpi_check_quirks(
9674 battery_quirk_table,
9675 ARRAY_SIZE(battery_quirk_table));
9676
2801b968
OG
9677 battery_hook_register(&battery_hook);
9678 return 0;
9679}
9680
9681static void tpacpi_battery_exit(void)
9682{
9683 battery_hook_unregister(&battery_hook);
9684}
9685
9686static struct ibm_struct battery_driver_data = {
9687 .name = "battery",
9688 .exit = tpacpi_battery_exit,
9689};
9690
56b6aeb0
HMH
9691/****************************************************************************
9692 ****************************************************************************
9693 *
9694 * Infrastructure
9695 *
9696 ****************************************************************************
9697 ****************************************************************************/
9698
8b468c0c
HMH
9699/*
9700 * HKEY event callout for other subdrivers go here
9701 * (yes, it is ugly, but it is quick, safe, and gets the job done
9702 */
9703static void tpacpi_driver_event(const unsigned int hkey_event)
9704{
347a2686
HMH
9705 if (ibm_backlight_device) {
9706 switch (hkey_event) {
9707 case TP_HKEY_EV_BRGHT_UP:
9708 case TP_HKEY_EV_BRGHT_DOWN:
9709 tpacpi_brightness_notify_change();
9710 }
9711 }
0d204c34
HMH
9712 if (alsa_card) {
9713 switch (hkey_event) {
9714 case TP_HKEY_EV_VOL_UP:
9715 case TP_HKEY_EV_VOL_DOWN:
9716 case TP_HKEY_EV_VOL_MUTE:
9717 volume_alsa_notify_change();
9718 }
9719 }
c685e20d
HG
9720 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9721 enum led_brightness brightness;
9722
9723 mutex_lock(&kbdlight_mutex);
9724
9725 /*
9726 * Check the brightness actually changed, setting the brightness
9727 * through kbdlight_set_level() also triggers this event.
9728 */
9729 brightness = kbdlight_sysfs_get(NULL);
9730 if (kbdlight_brightness != brightness) {
9731 kbdlight_brightness = brightness;
9732 led_classdev_notify_brightness_hw_changed(
9733 &tpacpi_led_kbdlight.led_classdev, brightness);
9734 }
9735
9736 mutex_unlock(&kbdlight_mutex);
9737 }
8b468c0c
HMH
9738}
9739
8b468c0c
HMH
9740static void hotkey_driver_event(const unsigned int scancode)
9741{
67bcae6e 9742 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8b468c0c
HMH
9743}
9744
7fd40029
HMH
9745/* --------------------------------------------------------------------- */
9746
56b6aeb0 9747/* /proc support */
94954cc6 9748static struct proc_dir_entry *proc_dir;
16663a87 9749
56b6aeb0
HMH
9750/*
9751 * Module and infrastructure proble, init and exit handling
9752 */
1da177e4 9753
90ab5ee9 9754static bool force_load;
b21a15f6 9755
fe08bc4b 9756#ifdef CONFIG_THINKPAD_ACPI_DEBUG
a5763f22 9757static const char * __init str_supported(int is_supported)
fe08bc4b 9758{
a5763f22 9759 static char text_unsupported[] __initdata = "not supported";
fe08bc4b 9760
72a979f0 9761 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
fe08bc4b
HMH
9762}
9763#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9764
b21a15f6
HMH
9765static void ibm_exit(struct ibm_struct *ibm)
9766{
9767 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9768
9769 list_del_init(&ibm->all_drivers);
9770
9771 if (ibm->flags.acpi_notify_installed) {
9772 dbg_printk(TPACPI_DBG_EXIT,
9773 "%s: acpi_remove_notify_handler\n", ibm->name);
9774 BUG_ON(!ibm->acpi);
9775 acpi_remove_notify_handler(*ibm->acpi->handle,
9776 ibm->acpi->type,
9777 dispatch_acpi_notify);
9778 ibm->flags.acpi_notify_installed = 0;
b21a15f6
HMH
9779 }
9780
9781 if (ibm->flags.proc_created) {
9782 dbg_printk(TPACPI_DBG_EXIT,
9783 "%s: remove_proc_entry\n", ibm->name);
9784 remove_proc_entry(ibm->name, proc_dir);
9785 ibm->flags.proc_created = 0;
9786 }
9787
9788 if (ibm->flags.acpi_driver_registered) {
9789 dbg_printk(TPACPI_DBG_EXIT,
9790 "%s: acpi_bus_unregister_driver\n", ibm->name);
9791 BUG_ON(!ibm->acpi);
9792 acpi_bus_unregister_driver(ibm->acpi->driver);
9793 kfree(ibm->acpi->driver);
9794 ibm->acpi->driver = NULL;
9795 ibm->flags.acpi_driver_registered = 0;
9796 }
9797
9798 if (ibm->flags.init_called && ibm->exit) {
9799 ibm->exit();
9800 ibm->flags.init_called = 0;
9801 }
9802
9803 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9804}
f74a27d4 9805
a5763f22 9806static int __init ibm_init(struct ibm_init_struct *iibm)
1da177e4
LT
9807{
9808 int ret;
a5763f22 9809 struct ibm_struct *ibm = iibm->data;
1da177e4
LT
9810 struct proc_dir_entry *entry;
9811
a5763f22
HMH
9812 BUG_ON(ibm == NULL);
9813
9814 INIT_LIST_HEAD(&ibm->all_drivers);
9815
92641177 9816 if (ibm->flags.experimental && !experimental)
1da177e4
LT
9817 return 0;
9818
fe08bc4b
HMH
9819 dbg_printk(TPACPI_DBG_INIT,
9820 "probing for %s\n", ibm->name);
9821
a5763f22
HMH
9822 if (iibm->init) {
9823 ret = iibm->init(iibm);
5fba344c
HMH
9824 if (ret > 0)
9825 return 0; /* probe failed */
9826 if (ret)
1da177e4 9827 return ret;
a5763f22 9828
92641177 9829 ibm->flags.init_called = 1;
1da177e4
LT
9830 }
9831
8d376cd6
HMH
9832 if (ibm->acpi) {
9833 if (ibm->acpi->hid) {
9834 ret = register_tpacpi_subdriver(ibm);
9835 if (ret)
9836 goto err_out;
9837 }
5fba344c 9838
8d376cd6
HMH
9839 if (ibm->acpi->notify) {
9840 ret = setup_acpi_notify(ibm);
9841 if (ret == -ENODEV) {
0978e012
JP
9842 pr_notice("disabling subdriver %s\n",
9843 ibm->name);
8d376cd6
HMH
9844 ret = 0;
9845 goto err_out;
9846 }
9847 if (ret < 0)
9848 goto err_out;
5fba344c 9849 }
5fba344c
HMH
9850 }
9851
fe08bc4b
HMH
9852 dbg_printk(TPACPI_DBG_INIT,
9853 "%s installed\n", ibm->name);
9854
78f81cc4 9855 if (ibm->read) {
d161a13f 9856 umode_t mode = iibm->base_procfs_mode;
887965e6 9857
b525c06c
HMH
9858 if (!mode)
9859 mode = S_IRUGO;
887965e6
AD
9860 if (ibm->write)
9861 mode |= S_IWUSR;
9862 entry = proc_create_data(ibm->name, mode, proc_dir,
9863 &dispatch_proc_fops, ibm);
78f81cc4 9864 if (!entry) {
0978e012 9865 pr_err("unable to create proc entry %s\n", ibm->name);
5fba344c
HMH
9866 ret = -ENODEV;
9867 goto err_out;
78f81cc4 9868 }
92641177 9869 ibm->flags.proc_created = 1;
78f81cc4 9870 }
1da177e4 9871
a5763f22
HMH
9872 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9873
1da177e4 9874 return 0;
5fba344c
HMH
9875
9876err_out:
fe08bc4b
HMH
9877 dbg_printk(TPACPI_DBG_INIT,
9878 "%s: at error exit path with result %d\n",
9879 ibm->name, ret);
9880
5fba344c 9881 ibm_exit(ibm);
72a979f0 9882 return (ret < 0) ? ret : 0;
1da177e4
LT
9883}
9884
56b6aeb0
HMH
9885/* Probing */
9886
846a416b
JW
9887static char __init tpacpi_parse_fw_id(const char * const s,
9888 u32 *model, u16 *release)
050df107 9889{
846a416b
JW
9890 int i;
9891
9892 if (!s || strlen(s) < 8)
9893 goto invalid;
9894
9895 for (i = 0; i < 8; i++)
9896 if (!((s[i] >= '0' && s[i] <= '9') ||
9897 (s[i] >= 'A' && s[i] <= 'Z')))
9898 goto invalid;
050df107 9899
1b0eb5bc
AL
9900 /*
9901 * Most models: xxyTkkWW (#.##c)
9902 * Ancient 570/600 and -SL lacks (#.##c)
9903 */
846a416b
JW
9904 if (s[3] == 'T' || s[3] == 'N') {
9905 *model = TPID(s[0], s[1]);
9906 *release = TPVER(s[4], s[5]);
9907 return s[2];
1b0eb5bc
AL
9908
9909 /* New models: xxxyTkkW (#.##c); T550 and some others */
846a416b
JW
9910 } else if (s[4] == 'T' || s[4] == 'N') {
9911 *model = TPID3(s[0], s[1], s[2]);
9912 *release = TPVER(s[5], s[6]);
9913 return s[3];
9914 }
9915
9916invalid:
9917 return '\0';
050df107
HMH
9918}
9919
bf20e740
HMH
9920/* returns 0 - probe ok, or < 0 - probe error.
9921 * Probe ok doesn't mean thinkpad found.
9922 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9923static int __must_check __init get_thinkpad_model_data(
9924 struct thinkpad_id_data *tp)
1da177e4 9925{
1855256c 9926 const struct dmi_device *dev = NULL;
56b6aeb0 9927 char ec_fw_string[18];
bf20e740 9928 char const *s;
846a416b 9929 char t;
1da177e4 9930
d5a2f2f1 9931 if (!tp)
bf20e740 9932 return -EINVAL;
d5a2f2f1
HMH
9933
9934 memset(tp, 0, sizeof(*tp));
9935
9936 if (dmi_name_in_vendors("IBM"))
9937 tp->vendor = PCI_VENDOR_ID_IBM;
9938 else if (dmi_name_in_vendors("LENOVO"))
9939 tp->vendor = PCI_VENDOR_ID_LENOVO;
9940 else
bf20e740 9941 return 0;
d5a2f2f1 9942
bf20e740
HMH
9943 s = dmi_get_system_info(DMI_BIOS_VERSION);
9944 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9945 if (s && !tp->bios_version_str)
9946 return -ENOMEM;
050df107
HMH
9947
9948 /* Really ancient ThinkPad 240X will fail this, which is fine */
846a416b
JW
9949 t = tpacpi_parse_fw_id(tp->bios_version_str,
9950 &tp->bios_model, &tp->bios_release);
9951 if (t != 'E' && t != 'C')
bf20e740 9952 return 0;
050df107 9953
56b6aeb0
HMH
9954 /*
9955 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9956 * X32 or newer, all Z series; Some models must have an
9957 * up-to-date BIOS or they will not be detected.
9958 *
9959 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9960 */
9961 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
9962 if (sscanf(dev->name,
9963 "IBM ThinkPad Embedded Controller -[%17c",
9964 ec_fw_string) == 1) {
9965 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9966 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
d5a2f2f1
HMH
9967
9968 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
bf20e740
HMH
9969 if (!tp->ec_version_str)
9970 return -ENOMEM;
050df107 9971
846a416b
JW
9972 t = tpacpi_parse_fw_id(ec_fw_string,
9973 &tp->ec_model, &tp->ec_release);
9974 if (t != 'H') {
5cac62ac 9975 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
0978e012
JP
9976 ec_fw_string);
9977 pr_notice("please report this to %s\n",
9978 TPACPI_MAIL);
050df107 9979 }
d5a2f2f1 9980 break;
78f81cc4 9981 }
1da177e4 9982 }
d5a2f2f1 9983
bf20e740 9984 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
40f5c777 9985 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
bf20e740
HMH
9986 tp->model_str = kstrdup(s, GFP_KERNEL);
9987 if (!tp->model_str)
9988 return -ENOMEM;
a4f46bb9
MI
9989 } else {
9990 s = dmi_get_system_info(DMI_BIOS_VENDOR);
40f5c777 9991 if (s && !(strncasecmp(s, "Lenovo", 6))) {
a4f46bb9
MI
9992 tp->model_str = kstrdup(s, GFP_KERNEL);
9993 if (!tp->model_str)
9994 return -ENOMEM;
9995 }
d5a2f2f1 9996 }
8c74adbc 9997
bf20e740
HMH
9998 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9999 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
10000 if (s && !tp->nummodel_str)
10001 return -ENOMEM;
10002
10003 return 0;
1da177e4
LT
10004}
10005
5fba344c
HMH
10006static int __init probe_for_thinkpad(void)
10007{
10008 int is_thinkpad;
10009
10010 if (acpi_disabled)
10011 return -ENODEV;
10012
e28393c0
HMH
10013 /* It would be dangerous to run the driver in this case */
10014 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
10015 return -ENODEV;
10016
5fba344c
HMH
10017 /*
10018 * Non-ancient models have better DMI tagging, but very old models
e675abaf 10019 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
5fba344c 10020 */
e675abaf
HMH
10021 is_thinkpad = (thinkpad_id.model_str != NULL) ||
10022 (thinkpad_id.ec_model != 0) ||
10023 tpacpi_is_fw_known();
5fba344c 10024
437e470c
HMH
10025 /* The EC handler is required */
10026 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
5fba344c
HMH
10027 if (!ec_handle) {
10028 if (is_thinkpad)
0978e012 10029 pr_err("Not yet supported ThinkPad detected!\n");
5fba344c
HMH
10030 return -ENODEV;
10031 }
10032
0dcef77c
HMH
10033 if (!is_thinkpad && !force_load)
10034 return -ENODEV;
10035
5fba344c
HMH
10036 return 0;
10037}
10038
7a43f788
HMH
10039static void __init thinkpad_acpi_init_banner(void)
10040{
0978e012
JP
10041 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
10042 pr_info("%s\n", TPACPI_URL);
7a43f788 10043
0978e012 10044 pr_info("ThinkPad BIOS %s, EC %s\n",
7a43f788
HMH
10045 (thinkpad_id.bios_version_str) ?
10046 thinkpad_id.bios_version_str : "unknown",
10047 (thinkpad_id.ec_version_str) ?
10048 thinkpad_id.ec_version_str : "unknown");
10049
10050 BUG_ON(!thinkpad_id.vendor);
10051
10052 if (thinkpad_id.model_str)
0978e012 10053 pr_info("%s %s, model %s\n",
7a43f788
HMH
10054 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
10055 "IBM" : ((thinkpad_id.vendor ==
10056 PCI_VENDOR_ID_LENOVO) ?
10057 "Lenovo" : "Unknown vendor"),
10058 thinkpad_id.model_str,
10059 (thinkpad_id.nummodel_str) ?
10060 thinkpad_id.nummodel_str : "unknown");
10061}
5fba344c 10062
56b6aeb0 10063/* Module init, exit, parameters */
1da177e4 10064
a5763f22
HMH
10065static struct ibm_init_struct ibms_init[] __initdata = {
10066 {
a5763f22
HMH
10067 .data = &thinkpad_acpi_driver_data,
10068 },
10069 {
10070 .init = hotkey_init,
10071 .data = &hotkey_driver_data,
10072 },
10073 {
10074 .init = bluetooth_init,
10075 .data = &bluetooth_driver_data,
10076 },
10077 {
10078 .init = wan_init,
10079 .data = &wan_driver_data,
10080 },
0045c0aa
HMH
10081 {
10082 .init = uwb_init,
10083 .data = &uwb_driver_data,
10084 },
d7c1d17d 10085#ifdef CONFIG_THINKPAD_ACPI_VIDEO
a5763f22
HMH
10086 {
10087 .init = video_init,
b525c06c 10088 .base_procfs_mode = S_IRUSR,
a5763f22
HMH
10089 .data = &video_driver_data,
10090 },
d7c1d17d 10091#endif
bb28f3d5
PR
10092 {
10093 .init = kbdlight_init,
10094 .data = &kbdlight_driver_data,
10095 },
a5763f22
HMH
10096 {
10097 .init = light_init,
10098 .data = &light_driver_data,
10099 },
a5763f22
HMH
10100 {
10101 .init = cmos_init,
10102 .data = &cmos_driver_data,
10103 },
10104 {
10105 .init = led_init,
10106 .data = &led_driver_data,
10107 },
10108 {
10109 .init = beep_init,
10110 .data = &beep_driver_data,
10111 },
10112 {
10113 .init = thermal_init,
10114 .data = &thermal_driver_data,
10115 },
a5763f22
HMH
10116 {
10117 .init = brightness_init,
10118 .data = &brightness_driver_data,
10119 },
10120 {
329e4e18 10121 .init = volume_init,
a5763f22
HMH
10122 .data = &volume_driver_data,
10123 },
10124 {
10125 .init = fan_init,
10126 .data = &fan_driver_data,
10127 },
420f9739
DH
10128 {
10129 .init = mute_led_init,
10130 .data = &mute_led_driver_data,
10131 },
2801b968
OG
10132 {
10133 .init = tpacpi_battery_init,
10134 .data = &battery_driver_data,
10135 },
a5763f22
HMH
10136};
10137
e4dca7b7 10138static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
1da177e4
LT
10139{
10140 unsigned int i;
a5763f22 10141 struct ibm_struct *ibm;
1da177e4 10142
59f91ff1
HMH
10143 if (!kp || !kp->name || !val)
10144 return -EINVAL;
10145
a5763f22
HMH
10146 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10147 ibm = ibms_init[i].data;
59f91ff1
HMH
10148 WARN_ON(ibm == NULL);
10149
10150 if (!ibm || !ibm->name)
10151 continue;
a5763f22
HMH
10152
10153 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
10154 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
78f81cc4 10155 return -ENOSPC;
a5763f22
HMH
10156 strcpy(ibms_init[i].param, val);
10157 strcat(ibms_init[i].param, ",");
78f81cc4
BD
10158 return 0;
10159 }
a5763f22 10160 }
1da177e4 10161
1da177e4
LT
10162 return -EINVAL;
10163}
10164
b09c7225 10165module_param(experimental, int, 0444);
f68080f8 10166MODULE_PARM_DESC(experimental,
35ff8b9f 10167 "Enables experimental features when non-zero");
56b6aeb0 10168
132ce091 10169module_param_named(debug, dbg_level, uint, 0);
f68080f8 10170MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
132ce091 10171
b09c7225 10172module_param(force_load, bool, 0444);
f68080f8 10173MODULE_PARM_DESC(force_load,
5cac62ac 10174 "Attempts to load the driver even on a mis-identified ThinkPad when true");
0dcef77c 10175
b09c7225 10176module_param_named(fan_control, fan_control_allowed, bool, 0444);
f68080f8 10177MODULE_PARM_DESC(fan_control,
35ff8b9f 10178 "Enables setting fan parameters features when true");
ecf2a80a 10179
b09c7225 10180module_param_named(brightness_mode, brightness_mode, uint, 0444);
f68080f8 10181MODULE_PARM_DESC(brightness_mode,
5cac62ac 10182 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
24d3b774 10183
b09c7225 10184module_param(brightness_enable, uint, 0444);
f68080f8 10185MODULE_PARM_DESC(brightness_enable,
35ff8b9f 10186 "Enables backlight control when 1, disables when 0");
87cc537a 10187
ff850c33 10188#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
329e4e18
HMH
10189module_param_named(volume_mode, volume_mode, uint, 0444);
10190MODULE_PARM_DESC(volume_mode,
5cac62ac 10191 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
329e4e18 10192
a112ceee
HMH
10193module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
10194MODULE_PARM_DESC(volume_capabilities,
5cac62ac 10195 "Selects the mixer capabilites: 0=auto, 1=volume and mute, 2=mute only");
a112ceee 10196
c7ac6291
HMH
10197module_param_named(volume_control, volume_control_allowed, bool, 0444);
10198MODULE_PARM_DESC(volume_control,
5cac62ac 10199 "Enables software override for the console audio control when true");
c7ac6291 10200
9a417ec0
AL
10201module_param_named(software_mute, software_mute_requested, bool, 0444);
10202MODULE_PARM_DESC(software_mute,
10203 "Request full software mute control");
10204
0d204c34
HMH
10205/* ALSA module API parameters */
10206module_param_named(index, alsa_index, int, 0444);
10207MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
10208module_param_named(id, alsa_id, charp, 0444);
10209MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
10210module_param_named(enable, alsa_enable, bool, 0444);
10211MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
ff850c33 10212#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
0d204c34 10213
9b4bbbd2 10214/* The module parameter can't be read back, that's why 0 is used here */
e0c7dfe7 10215#define TPACPI_PARAM(feature) \
f68080f8 10216 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5cac62ac 10217 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
1da177e4 10218
e0c7dfe7
HMH
10219TPACPI_PARAM(hotkey);
10220TPACPI_PARAM(bluetooth);
10221TPACPI_PARAM(video);
10222TPACPI_PARAM(light);
e0c7dfe7
HMH
10223TPACPI_PARAM(cmos);
10224TPACPI_PARAM(led);
10225TPACPI_PARAM(beep);
e0c7dfe7
HMH
10226TPACPI_PARAM(brightness);
10227TPACPI_PARAM(volume);
10228TPACPI_PARAM(fan);
78f81cc4 10229
a73f3091 10230#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
b09c7225 10231module_param(dbg_wlswemul, uint, 0444);
a73f3091
HMH
10232MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
10233module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
10234MODULE_PARM_DESC(wlsw_state,
10235 "Initial state of the emulated WLSW switch");
10236
b09c7225 10237module_param(dbg_bluetoothemul, uint, 0444);
a73f3091
HMH
10238MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
10239module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
10240MODULE_PARM_DESC(bluetooth_state,
10241 "Initial state of the emulated bluetooth switch");
10242
b09c7225 10243module_param(dbg_wwanemul, uint, 0444);
a73f3091
HMH
10244MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
10245module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
10246MODULE_PARM_DESC(wwan_state,
10247 "Initial state of the emulated WWAN switch");
0045c0aa 10248
b09c7225 10249module_param(dbg_uwbemul, uint, 0444);
0045c0aa
HMH
10250MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
10251module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
10252MODULE_PARM_DESC(uwb_state,
10253 "Initial state of the emulated UWB switch");
a73f3091
HMH
10254#endif
10255
b21a15f6
HMH
10256static void thinkpad_acpi_module_exit(void)
10257{
10258 struct ibm_struct *ibm, *itmp;
10259
10260 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
10261
10262 list_for_each_entry_safe_reverse(ibm, itmp,
10263 &tpacpi_all_drivers,
10264 all_drivers) {
10265 ibm_exit(ibm);
10266 }
10267
10268 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
10269
10270 if (tpacpi_inputdev) {
10271 if (tp_features.input_device_registered)
10272 input_unregister_device(tpacpi_inputdev);
10273 else
10274 input_free_device(tpacpi_inputdev);
00d39597 10275 kfree(hotkey_keycode_map);
b21a15f6
HMH
10276 }
10277
10278 if (tpacpi_hwmon)
10279 hwmon_device_unregister(tpacpi_hwmon);
10280
b21a15f6
HMH
10281 if (tpacpi_sensors_pdev)
10282 platform_device_unregister(tpacpi_sensors_pdev);
10283 if (tpacpi_pdev)
10284 platform_device_unregister(tpacpi_pdev);
10285
10286 if (tp_features.sensors_pdrv_attrs_registered)
10287 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
10288 if (tp_features.platform_drv_attrs_registered)
10289 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
10290
10291 if (tp_features.sensors_pdrv_registered)
10292 platform_driver_unregister(&tpacpi_hwmon_pdriver);
10293
10294 if (tp_features.platform_drv_registered)
10295 platform_driver_unregister(&tpacpi_pdriver);
10296
10297 if (proc_dir)
e0c7dfe7 10298 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
b21a15f6 10299
e0e3c061
HMH
10300 if (tpacpi_wq)
10301 destroy_workqueue(tpacpi_wq);
10302
b21a15f6
HMH
10303 kfree(thinkpad_id.bios_version_str);
10304 kfree(thinkpad_id.ec_version_str);
10305 kfree(thinkpad_id.model_str);
d2be15bd 10306 kfree(thinkpad_id.nummodel_str);
b21a15f6
HMH
10307}
10308
f74a27d4 10309
1def7115 10310static int __init thinkpad_acpi_module_init(void)
1da177e4
LT
10311{
10312 int ret, i;
10313
8fef502e
HMH
10314 tpacpi_lifecycle = TPACPI_LIFE_INIT;
10315
54ae1501 10316 /* Driver-level probe */
d5a2f2f1 10317
bf20e740
HMH
10318 ret = get_thinkpad_model_data(&thinkpad_id);
10319 if (ret) {
0978e012 10320 pr_err("unable to get DMI data: %d\n", ret);
bf20e740
HMH
10321 thinkpad_acpi_module_exit();
10322 return ret;
10323 }
5fba344c 10324 ret = probe_for_thinkpad();
d5a2f2f1
HMH
10325 if (ret) {
10326 thinkpad_acpi_module_exit();
5fba344c 10327 return ret;
d5a2f2f1 10328 }
1da177e4 10329
54ae1501 10330 /* Driver initialization */
d5a2f2f1 10331
7a43f788
HMH
10332 thinkpad_acpi_init_banner();
10333 tpacpi_check_outdated_fw();
10334
e0c7dfe7
HMH
10335 TPACPI_ACPIHANDLE_INIT(ecrd);
10336 TPACPI_ACPIHANDLE_INIT(ecwr);
1da177e4 10337
e0e3c061
HMH
10338 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
10339 if (!tpacpi_wq) {
10340 thinkpad_acpi_module_exit();
10341 return -ENOMEM;
10342 }
10343
e0c7dfe7 10344 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
1da177e4 10345 if (!proc_dir) {
0978e012 10346 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
1def7115 10347 thinkpad_acpi_module_exit();
1da177e4
LT
10348 return -ENODEV;
10349 }
78f81cc4 10350
54ae1501
HMH
10351 ret = platform_driver_register(&tpacpi_pdriver);
10352 if (ret) {
0978e012 10353 pr_err("unable to register main platform driver\n");
54ae1501
HMH
10354 thinkpad_acpi_module_exit();
10355 return ret;
10356 }
ac36393d
HMH
10357 tp_features.platform_drv_registered = 1;
10358
7fd40029
HMH
10359 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
10360 if (ret) {
0978e012 10361 pr_err("unable to register hwmon platform driver\n");
7fd40029
HMH
10362 thinkpad_acpi_module_exit();
10363 return ret;
10364 }
10365 tp_features.sensors_pdrv_registered = 1;
10366
176750d6 10367 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
2369cc94
HMH
10368 if (!ret) {
10369 tp_features.platform_drv_attrs_registered = 1;
35ff8b9f
HMH
10370 ret = tpacpi_create_driver_attributes(
10371 &tpacpi_hwmon_pdriver.driver);
2369cc94 10372 }
176750d6 10373 if (ret) {
0978e012 10374 pr_err("unable to create sysfs driver attributes\n");
176750d6
HMH
10375 thinkpad_acpi_module_exit();
10376 return ret;
10377 }
2369cc94 10378 tp_features.sensors_pdrv_attrs_registered = 1;
176750d6 10379
54ae1501
HMH
10380
10381 /* Device initialization */
e0c7dfe7 10382 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
54ae1501
HMH
10383 NULL, 0);
10384 if (IS_ERR(tpacpi_pdev)) {
10385 ret = PTR_ERR(tpacpi_pdev);
10386 tpacpi_pdev = NULL;
0978e012 10387 pr_err("unable to register platform device\n");
54ae1501
HMH
10388 thinkpad_acpi_module_exit();
10389 return ret;
10390 }
7fd40029 10391 tpacpi_sensors_pdev = platform_device_register_simple(
35ff8b9f
HMH
10392 TPACPI_HWMON_DRVR_NAME,
10393 -1, NULL, 0);
7fd40029
HMH
10394 if (IS_ERR(tpacpi_sensors_pdev)) {
10395 ret = PTR_ERR(tpacpi_sensors_pdev);
10396 tpacpi_sensors_pdev = NULL;
0978e012 10397 pr_err("unable to register hwmon platform device\n");
7fd40029
HMH
10398 thinkpad_acpi_module_exit();
10399 return ret;
10400 }
7fd40029 10401 tp_features.sensors_pdev_attrs_registered = 1;
6b99e356
SF
10402 tpacpi_hwmon = hwmon_device_register_with_groups(
10403 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
10404
54ae1501
HMH
10405 if (IS_ERR(tpacpi_hwmon)) {
10406 ret = PTR_ERR(tpacpi_hwmon);
10407 tpacpi_hwmon = NULL;
0978e012 10408 pr_err("unable to register hwmon device\n");
54ae1501
HMH
10409 thinkpad_acpi_module_exit();
10410 return ret;
10411 }
8523ed6f 10412 mutex_init(&tpacpi_inputdev_send_mutex);
7f5d1cd6
HMH
10413 tpacpi_inputdev = input_allocate_device();
10414 if (!tpacpi_inputdev) {
7f5d1cd6
HMH
10415 thinkpad_acpi_module_exit();
10416 return -ENOMEM;
10417 } else {
10418 /* Prepare input device, but don't register */
10419 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
e0c7dfe7 10420 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7f5d1cd6 10421 tpacpi_inputdev->id.bustype = BUS_HOST;
e28393c0 10422 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
7f5d1cd6
HMH
10423 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
10424 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
d112ef95 10425 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
7f5d1cd6 10426 }
77775838
HMH
10427
10428 /* Init subdriver dependencies */
10429 tpacpi_detect_brightness_capabilities();
10430
10431 /* Init subdrivers */
a5763f22
HMH
10432 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10433 ret = ibm_init(&ibms_init[i]);
10434 if (ret >= 0 && *ibms_init[i].param)
10435 ret = ibms_init[i].data->write(ibms_init[i].param);
1da177e4 10436 if (ret < 0) {
1def7115 10437 thinkpad_acpi_module_exit();
1da177e4
LT
10438 return ret;
10439 }
10440 }
b589ea4c
HMH
10441
10442 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
10443
7f5d1cd6
HMH
10444 ret = input_register_device(tpacpi_inputdev);
10445 if (ret < 0) {
0978e012 10446 pr_err("unable to register input device\n");
7f5d1cd6
HMH
10447 thinkpad_acpi_module_exit();
10448 return ret;
10449 } else {
10450 tp_features.input_device_registered = 1;
10451 }
1da177e4
LT
10452
10453 return 0;
10454}
10455
95e57ab2
HMH
10456MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
10457
922fe097
HMH
10458/*
10459 * This will autoload the driver in almost every ThinkPad
10460 * in widespread use.
10461 *
10462 * Only _VERY_ old models, like the 240, 240x and 570 lack
10463 * the HKEY event interface.
10464 */
10465MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
10466
f68080f8
HMH
10467/*
10468 * DMI matching for module autoloading
10469 *
10470 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
10471 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
10472 *
10473 * Only models listed in thinkwiki will be supported, so add yours
10474 * if it is not there yet.
10475 */
10476#define IBM_BIOS_MODULE_ALIAS(__type) \
b36a50f9 10477 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
f68080f8 10478
f68080f8
HMH
10479/* Ancient thinkpad BIOSes have to be identified by
10480 * BIOS type or model number, and there are far less
10481 * BIOS types than model numbers... */
922fe097 10482IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
f68080f8 10483
f68f53a2
HMH
10484MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
10485MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
e0c7dfe7
HMH
10486MODULE_DESCRIPTION(TPACPI_DESC);
10487MODULE_VERSION(TPACPI_VERSION);
f68080f8
HMH
10488MODULE_LICENSE("GPL");
10489
1def7115
HMH
10490module_init(thinkpad_acpi_module_init);
10491module_exit(thinkpad_acpi_module_exit);