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