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