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