]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/platform/x86/thinkpad_acpi.c
Merge branch 'fix/asoc' into for-linus
[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
d1e14dca
HMH
3096typedef u16 tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3097
a5763f22 3098static int __init hotkey_init(struct ibm_init_struct *iibm)
56b6aeb0 3099{
0f089147
HMH
3100 /* Requirements for changing the default keymaps:
3101 *
3102 * 1. Many of the keys are mapped to KEY_RESERVED for very
3103 * good reasons. Do not change them unless you have deep
3104 * knowledge on the IBM and Lenovo ThinkPad firmware for
3105 * the various ThinkPad models. The driver behaves
3106 * differently for KEY_RESERVED: such keys have their
3107 * hot key mask *unset* in mask_recommended, and also
3108 * in the initial hot key mask programmed into the
3109 * firmware at driver load time, which means the firm-
3110 * ware may react very differently if you change them to
3111 * something else;
3112 *
3113 * 2. You must be subscribed to the linux-thinkpad and
3114 * ibm-acpi-devel mailing lists, and you should read the
3115 * list archives since 2007 if you want to change the
3116 * keymaps. This requirement exists so that you will
3117 * know the past history of problems with the thinkpad-
3118 * acpi driver keymaps, and also that you will be
3119 * listening to any bug reports;
3120 *
3121 * 3. Do not send thinkpad-acpi specific patches directly to
3122 * for merging, *ever*. Send them to the linux-acpi
3123 * mailinglist for comments. Merging is to be done only
3124 * through acpi-test and the ACPI maintainer.
3125 *
3126 * If the above is too much to ask, don't change the keymap.
3127 * Ask the thinkpad-acpi maintainer to do it, instead.
3128 */
d1e14dca
HMH
3129
3130 enum keymap_index {
3131 TPACPI_KEYMAP_IBM_GENERIC = 0,
3132 TPACPI_KEYMAP_LENOVO_GENERIC,
3133 };
3134
3135 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3136 /* Generic keymap for IBM ThinkPads */
3137 [TPACPI_KEYMAP_IBM_GENERIC] = {
edf0e0e5 3138 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
d1e14dca 3139 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
edf0e0e5
HMH
3140 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3141 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
0f089147
HMH
3142
3143 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
edf0e0e5
HMH
3144 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3145 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3146 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
0f089147 3147
0d922e3b 3148 /* brightness: firmware always reacts to them */
e927c08d 3149 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
e927c08d 3150 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
0f089147
HMH
3151
3152 /* Thinklight: firmware always react to it */
edf0e0e5 3153 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
0f089147 3154
edf0e0e5
HMH
3155 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3156 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
0f089147
HMH
3157
3158 /* Volume: firmware always react to it and reprograms
3159 * the built-in *extra* mixer. Never map it to control
3160 * another mixer by default. */
e927c08d
HMH
3161 KEY_RESERVED, /* 0x14: VOLUME UP */
3162 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3163 KEY_RESERVED, /* 0x16: MUTE */
0f089147 3164
edf0e0e5 3165 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
0f089147 3166
edf0e0e5
HMH
3167 /* (assignments unknown, please report if found) */
3168 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3169 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
d1e14dca
HMH
3170 },
3171
3172 /* Generic keymap for Lenovo ThinkPads */
3173 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
edf0e0e5
HMH
3174 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3175 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2b754262 3176 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
edf0e0e5 3177 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
0f089147
HMH
3178
3179 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
edf0e0e5
HMH
3180 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3181 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3182 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
0f089147 3183
de4c8cc7
HMH
3184 /* These should be enabled --only-- when ACPI video
3185 * is disabled (i.e. in "vendor" mode), and are handled
3186 * in a special way by the init code */
3187 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3188 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
0f089147 3189
edf0e0e5 3190 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
0f089147 3191
edf0e0e5
HMH
3192 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3193 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
0f089147
HMH
3194
3195 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3196 * react to it and reprograms the built-in *extra* mixer.
3197 * Never map it to control another mixer by default.
3198 *
3199 * T60?, T61, R60?, R61: firmware and EC tries to send
3200 * these over the regular keyboard, so these are no-ops,
3201 * but there are still weird bugs re. MUTE, so do not
3202 * change unless you get test reports from all Lenovo
3203 * models. May cause the BIOS to interfere with the
3204 * HDA mixer.
3205 */
e927c08d
HMH
3206 KEY_RESERVED, /* 0x14: VOLUME UP */
3207 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3208 KEY_RESERVED, /* 0x16: MUTE */
0f089147 3209
edf0e0e5 3210 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
0f089147 3211
edf0e0e5
HMH
3212 /* (assignments unknown, please report if found) */
3213 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3214 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
d1e14dca 3215 },
edf0e0e5
HMH
3216 };
3217
d1e14dca
HMH
3218 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3219 /* Generic maps (fallback) */
3220 {
3221 .vendor = PCI_VENDOR_ID_IBM,
3222 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3223 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3224 },
3225 {
3226 .vendor = PCI_VENDOR_ID_LENOVO,
3227 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3228 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3229 },
3230 };
3231
3232#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3233#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_t[0])
edf0e0e5 3234
6a38abbf 3235 int res, i;
74941a69 3236 int status;
1b6521dc 3237 int hkeyv;
d89a727a
HMH
3238 bool radiosw_state = false;
3239 bool tabletsw_state = false;
b86c4722 3240
0d922e3b 3241 unsigned long quirks;
d1e14dca 3242 unsigned long keymap_id;
0d922e3b 3243
56e2c200
HMH
3244 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3245 "initializing hotkey subdriver\n");
fe08bc4b 3246
6a38abbf 3247 BUG_ON(!tpacpi_inputdev);
01e88f25
HMH
3248 BUG_ON(tpacpi_inputdev->open != NULL ||
3249 tpacpi_inputdev->close != NULL);
6a38abbf 3250
e0c7dfe7 3251 TPACPI_ACPIHANDLE_INIT(hkey);
40ca9fdf 3252 mutex_init(&hotkey_mutex);
5fba344c 3253
01e88f25
HMH
3254#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3255 mutex_init(&hotkey_thread_mutex);
3256 mutex_init(&hotkey_thread_data_mutex);
3257#endif
3258
56b6aeb0 3259 /* hotkey not supported on 570 */
d8fd94d9 3260 tp_features.hotkey = hkey_handle != NULL;
56b6aeb0 3261
56e2c200
HMH
3262 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3263 "hotkeys are %s\n",
d8fd94d9 3264 str_supported(tp_features.hotkey));
fe08bc4b 3265
9c0a76e1
HMH
3266 if (!tp_features.hotkey)
3267 return 1;
a0416420 3268
0d922e3b
HMH
3269 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3270 ARRAY_SIZE(tpacpi_hotkey_qtable));
3271
d64c81c4
HMH
3272 tpacpi_disable_brightness_delay();
3273
0d922e3b
HMH
3274 /* MUST have enough space for all attributes to be added to
3275 * hotkey_dev_attributes */
3276 hotkey_dev_attributes = create_attr_set(
3277 ARRAY_SIZE(hotkey_attributes) + 2,
3278 NULL);
9c0a76e1
HMH
3279 if (!hotkey_dev_attributes)
3280 return -ENOMEM;
3281 res = add_many_to_attr_set(hotkey_dev_attributes,
3282 hotkey_attributes,
3283 ARRAY_SIZE(hotkey_attributes));
3284 if (res)
3285 goto err_exit;
3286
0d922e3b 3287 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
9c0a76e1
HMH
3288 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3289 for HKEY interface version 0x100 */
3290 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3291 if ((hkeyv >> 8) != 1) {
3292 printk(TPACPI_ERR "unknown version of the "
3293 "HKEY interface: 0x%x\n", hkeyv);
3294 printk(TPACPI_ERR "please report this to %s\n",
3295 TPACPI_MAIL);
3296 } else {
3297 /*
3298 * MHKV 0x100 in A31, R40, R40e,
3299 * T4x, X31, and later
3300 */
56e2c200
HMH
3301 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3302 "firmware HKEY interface version: 0x%x\n",
3303 hkeyv);
0d922e3b
HMH
3304
3305 /* Paranoia check AND init hotkey_all_mask */
3306 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3307 "MHKA", "qd")) {
3308 printk(TPACPI_ERR
3309 "missing MHKA handler, "
3310 "please report this to %s\n",
3311 TPACPI_MAIL);
3312 /* Fallback: pre-init for FN+F3,F4,F12 */
3313 hotkey_all_mask = 0x080cU;
3314 } else {
3315 tp_features.hotkey_mask = 1;
3316 }
1b6521dc 3317 }
9c0a76e1 3318 }
56b6aeb0 3319
56e2c200
HMH
3320 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3321 "hotkey masks are %s\n",
9c0a76e1 3322 str_supported(tp_features.hotkey_mask));
fe08bc4b 3323
0d922e3b
HMH
3324 /* Init hotkey_all_mask if not initialized yet */
3325 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3326 (quirks & TPACPI_HK_Q_INIMASK))
3327 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
9b010de5 3328
0d922e3b 3329 /* Init hotkey_acpi_mask and hotkey_orig_mask */
9c0a76e1 3330 if (tp_features.hotkey_mask) {
0d922e3b
HMH
3331 /* hotkey_source_mask *must* be zero for
3332 * the first hotkey_mask_get to return hotkey_orig_mask */
9c0a76e1
HMH
3333 res = hotkey_mask_get();
3334 if (res)
3335 goto err_exit;
3336
0d922e3b
HMH
3337 hotkey_orig_mask = hotkey_acpi_mask;
3338 } else {
3339 hotkey_orig_mask = hotkey_all_mask;
3340 hotkey_acpi_mask = hotkey_all_mask;
9c0a76e1 3341 }
74941a69 3342
a73f3091
HMH
3343#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3344 if (dbg_wlswemul) {
3345 tp_features.hotkey_wlsw = 1;
d89a727a 3346 radiosw_state = !!tpacpi_wlsw_emulstate;
a73f3091
HMH
3347 printk(TPACPI_INFO
3348 "radio switch emulation enabled\n");
3349 } else
3350#endif
9c0a76e1
HMH
3351 /* Not all thinkpads have a hardware radio switch */
3352 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3353 tp_features.hotkey_wlsw = 1;
d89a727a 3354 radiosw_state = !!status;
9c0a76e1
HMH
3355 printk(TPACPI_INFO
3356 "radio switch found; radios are %s\n",
3357 enabled(status, 0));
3a872080
HMH
3358 }
3359 if (tp_features.hotkey_wlsw)
9c0a76e1
HMH
3360 res = add_to_attr_set(hotkey_dev_attributes,
3361 &dev_attr_hotkey_radio_sw.attr);
74941a69 3362
9c0a76e1
HMH
3363 /* For X41t, X60t, X61t Tablets... */
3364 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3365 tp_features.hotkey_tablet = 1;
d89a727a 3366 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
9c0a76e1
HMH
3367 printk(TPACPI_INFO
3368 "possible tablet mode switch found; "
3369 "ThinkPad in %s mode\n",
d89a727a 3370 (tabletsw_state) ? "tablet" : "laptop");
9c0a76e1
HMH
3371 res = add_to_attr_set(hotkey_dev_attributes,
3372 &dev_attr_hotkey_tablet_mode.attr);
3373 }
6c231bd5 3374
9c0a76e1
HMH
3375 if (!res)
3376 res = register_attr_set_with_sysfs(
3377 hotkey_dev_attributes,
3378 &tpacpi_pdev->dev.kobj);
3379 if (res)
3380 goto err_exit;
6a38abbf 3381
9c0a76e1 3382 /* Set up key map */
9c0a76e1
HMH
3383 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3384 GFP_KERNEL);
3385 if (!hotkey_keycode_map) {
3386 printk(TPACPI_ERR
3387 "failed to allocate memory for key map\n");
3388 res = -ENOMEM;
3389 goto err_exit;
3390 }
edf0e0e5 3391
d1e14dca
HMH
3392 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3393 ARRAY_SIZE(tpacpi_keymap_qtable));
3394 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3395 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3396 "using keymap number %lu\n", keymap_id);
3397
3398 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3399 TPACPI_HOTKEY_MAP_SIZE);
edf0e0e5 3400
792979c8 3401 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
9c0a76e1
HMH
3402 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3403 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3404 tpacpi_inputdev->keycode = hotkey_keycode_map;
3405 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3406 if (hotkey_keycode_map[i] != KEY_RESERVED) {
792979c8
HMH
3407 input_set_capability(tpacpi_inputdev, EV_KEY,
3408 hotkey_keycode_map[i]);
9c0a76e1
HMH
3409 } else {
3410 if (i < sizeof(hotkey_reserved_mask)*8)
3411 hotkey_reserved_mask |= 1 << i;
6a38abbf 3412 }
9c0a76e1 3413 }
6a38abbf 3414
9c0a76e1 3415 if (tp_features.hotkey_wlsw) {
792979c8 3416 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
d89a727a
HMH
3417 input_report_switch(tpacpi_inputdev,
3418 SW_RFKILL_ALL, radiosw_state);
9c0a76e1
HMH
3419 }
3420 if (tp_features.hotkey_tablet) {
792979c8 3421 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
d89a727a
HMH
3422 input_report_switch(tpacpi_inputdev,
3423 SW_TABLET_MODE, tabletsw_state);
9c0a76e1 3424 }
5c29d58f 3425
9c0a76e1 3426 /* Do not issue duplicate brightness change events to
77775838
HMH
3427 * userspace. tpacpi_detect_brightness_capabilities() must have
3428 * been called before this point */
8bf3d4c5 3429 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
9c0a76e1
HMH
3430 printk(TPACPI_INFO
3431 "This ThinkPad has standard ACPI backlight "
3432 "brightness control, supported by the ACPI "
3433 "video driver\n");
3434 printk(TPACPI_NOTICE
3435 "Disabling thinkpad-acpi brightness events "
3436 "by default...\n");
3437
de4c8cc7
HMH
3438 /* Disable brightness up/down on Lenovo thinkpads when
3439 * ACPI is handling them, otherwise it is plain impossible
3440 * for userspace to do something even remotely sane */
9c0a76e1
HMH
3441 hotkey_reserved_mask |=
3442 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3443 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
de4c8cc7
HMH
3444 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3445 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
9c0a76e1 3446 }
ff80f137 3447
230d8cf2 3448#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
0d922e3b
HMH
3449 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3450 & ~hotkey_all_mask
3451 & ~hotkey_reserved_mask;
230d8cf2
HMH
3452
3453 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3454 "hotkey source mask 0x%08x, polling freq %u\n",
3455 hotkey_source_mask, hotkey_poll_freq);
3456#endif
3457
56e2c200
HMH
3458 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3459 "enabling firmware HKEY event interface...\n");
2586d566 3460 res = hotkey_status_set(true);
9c0a76e1
HMH
3461 if (res) {
3462 hotkey_exit();
3463 return res;
3464 }
0d922e3b
HMH
3465 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3466 | hotkey_driver_mask)
3467 & ~hotkey_source_mask);
9c0a76e1
HMH
3468 if (res < 0 && res != -ENXIO) {
3469 hotkey_exit();
3470 return res;
3471 }
0d922e3b
HMH
3472 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3473 & ~hotkey_reserved_mask;
3474 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3475 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3476 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
01e88f25 3477
56e2c200
HMH
3478 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3479 "legacy ibm/hotkey event reporting over procfs %s\n",
9c0a76e1
HMH
3480 (hotkey_report_mode < 2) ?
3481 "enabled" : "disabled");
01e88f25 3482
9c0a76e1
HMH
3483 tpacpi_inputdev->open = &hotkey_inputdev_open;
3484 tpacpi_inputdev->close = &hotkey_inputdev_close;
56b6aeb0 3485
db25f16d 3486 hotkey_poll_setup_safe(true);
56b6aeb0 3487
9c0a76e1 3488 return 0;
01e88f25 3489
9c0a76e1
HMH
3490err_exit:
3491 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3492 hotkey_dev_attributes = NULL;
a0416420 3493
9c0a76e1 3494 return (res < 0)? res : 1;
56b6aeb0
HMH
3495}
3496
3827e7a3
HMH
3497static bool hotkey_notify_hotkey(const u32 hkey,
3498 bool *send_acpi_ev,
3499 bool *ignore_acpi_ev)
3500{
3501 /* 0x1000-0x1FFF: key presses */
3502 unsigned int scancode = hkey & 0xfff;
3503 *send_acpi_ev = true;
3504 *ignore_acpi_ev = false;
3505
34a656d2
HMH
3506 /* HKEY event 0x1001 is scancode 0x00 */
3507 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
3827e7a3
HMH
3508 scancode--;
3509 if (!(hotkey_source_mask & (1 << scancode))) {
0d922e3b 3510 tpacpi_input_send_key_masked(scancode);
3827e7a3
HMH
3511 *send_acpi_ev = false;
3512 } else {
3513 *ignore_acpi_ev = true;
3514 }
3515 return true;
3516 }
3517 return false;
3518}
3519
3520static bool hotkey_notify_wakeup(const u32 hkey,
3521 bool *send_acpi_ev,
3522 bool *ignore_acpi_ev)
3523{
3524 /* 0x2000-0x2FFF: Wakeup reason */
3525 *send_acpi_ev = true;
3526 *ignore_acpi_ev = false;
3527
3528 switch (hkey) {
67bcae6e
HMH
3529 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3530 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3827e7a3
HMH
3531 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3532 *ignore_acpi_ev = true;
3533 break;
3534
67bcae6e
HMH
3535 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3536 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3827e7a3
HMH
3537 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3538 *ignore_acpi_ev = true;
3539 break;
3540
67bcae6e
HMH
3541 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3542 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
106b4e66
HMH
3543 printk(TPACPI_ALERT
3544 "EMERGENCY WAKEUP: battery almost empty\n");
3545 /* how to auto-heal: */
3546 /* 2313: woke up from S3, go to S4/S5 */
3547 /* 2413: woke up from S4, go to S5 */
3548 break;
3549
3827e7a3
HMH
3550 default:
3551 return false;
3552 }
3553
3554 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3555 printk(TPACPI_INFO
3556 "woke up due to a hot-unplug "
3557 "request...\n");
3558 hotkey_wakeup_reason_notify_change();
3559 }
3560 return true;
3561}
3562
3563static bool hotkey_notify_usrevent(const u32 hkey,
3564 bool *send_acpi_ev,
3565 bool *ignore_acpi_ev)
3566{
3567 /* 0x5000-0x5FFF: human interface helpers */
3568 *send_acpi_ev = true;
3569 *ignore_acpi_ev = false;
3570
3571 switch (hkey) {
67bcae6e
HMH
3572 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3573 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3827e7a3
HMH
3574 return true;
3575
67bcae6e
HMH
3576 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3577 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3827e7a3
HMH
3578 tpacpi_input_send_tabletsw();
3579 hotkey_tablet_mode_notify_change();
3580 *send_acpi_ev = false;
3581 return true;
3582
67bcae6e
HMH
3583 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3584 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3585 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
176dd985 3586 /* do not propagate these events */
3827e7a3
HMH
3587 *ignore_acpi_ev = true;
3588 return true;
3589
3590 default:
3591 return false;
3592 }
3593}
3594
9ebd9e83
HMH
3595static void thermal_dump_all_sensors(void);
3596
106b4e66
HMH
3597static bool hotkey_notify_thermal(const u32 hkey,
3598 bool *send_acpi_ev,
3599 bool *ignore_acpi_ev)
3600{
9ebd9e83
HMH
3601 bool known = true;
3602
106b4e66
HMH
3603 /* 0x6000-0x6FFF: thermal alarms */
3604 *send_acpi_ev = true;
3605 *ignore_acpi_ev = false;
3606
3607 switch (hkey) {
9ebd9e83
HMH
3608 case TP_HKEY_EV_THM_TABLE_CHANGED:
3609 printk(TPACPI_INFO
3610 "EC reports that Thermal Table has changed\n");
3611 /* recommended action: do nothing, we don't have
3612 * Lenovo ATM information */
3613 return true;
67bcae6e 3614 case TP_HKEY_EV_ALARM_BAT_HOT:
106b4e66
HMH
3615 printk(TPACPI_CRIT
3616 "THERMAL ALARM: battery is too hot!\n");
3617 /* recommended action: warn user through gui */
9ebd9e83 3618 break;
67bcae6e 3619 case TP_HKEY_EV_ALARM_BAT_XHOT:
106b4e66
HMH
3620 printk(TPACPI_ALERT
3621 "THERMAL EMERGENCY: battery is extremely hot!\n");
3622 /* recommended action: immediate sleep/hibernate */
9ebd9e83 3623 break;
67bcae6e 3624 case TP_HKEY_EV_ALARM_SENSOR_HOT:
106b4e66
HMH
3625 printk(TPACPI_CRIT
3626 "THERMAL ALARM: "
3627 "a sensor reports something is too hot!\n");
3628 /* recommended action: warn user through gui, that */
3629 /* some internal component is too hot */
9ebd9e83 3630 break;
67bcae6e 3631 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
106b4e66
HMH
3632 printk(TPACPI_ALERT
3633 "THERMAL EMERGENCY: "
3634 "a sensor reports something is extremely hot!\n");
3635 /* recommended action: immediate sleep/hibernate */
9ebd9e83 3636 break;
106b4e66
HMH
3637 default:
3638 printk(TPACPI_ALERT
3639 "THERMAL ALERT: unknown thermal alarm received\n");
9ebd9e83 3640 known = false;
106b4e66 3641 }
9ebd9e83
HMH
3642
3643 thermal_dump_all_sensors();
3644
3645 return known;
106b4e66
HMH
3646}
3647
56b6aeb0
HMH
3648static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3649{
6a38abbf 3650 u32 hkey;
3827e7a3
HMH
3651 bool send_acpi_ev;
3652 bool ignore_acpi_ev;
3653 bool known_ev;
3eea123d
HMH
3654
3655 if (event != 0x80) {
35ff8b9f
HMH
3656 printk(TPACPI_ERR
3657 "unknown HKEY notification event %d\n", event);
3eea123d 3658 /* forward it to userspace, maybe it knows how to handle it */
35ff8b9f
HMH
3659 acpi_bus_generate_netlink_event(
3660 ibm->acpi->device->pnp.device_class,
e0b36fc5 3661 dev_name(&ibm->acpi->device->dev),
35ff8b9f 3662 event, 0);
3eea123d
HMH
3663 return;
3664 }
3665
3666 while (1) {
3667 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
e0c7dfe7 3668 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3eea123d
HMH
3669 return;
3670 }
3671
3672 if (hkey == 0) {
3673 /* queue empty */
3674 return;
3675 }
3676
3827e7a3
HMH
3677 send_acpi_ev = true;
3678 ignore_acpi_ev = false;
56b6aeb0 3679
ff80f137
HMH
3680 switch (hkey >> 12) {
3681 case 1:
3682 /* 0x1000-0x1FFF: key presses */
3827e7a3
HMH
3683 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3684 &ignore_acpi_ev);
ff80f137 3685 break;
a713b4d7 3686 case 2:
3827e7a3
HMH
3687 /* 0x2000-0x2FFF: Wakeup reason */
3688 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3689 &ignore_acpi_ev);
a713b4d7
HMH
3690 break;
3691 case 3:
3827e7a3 3692 /* 0x3000-0x3FFF: bay-related wakeups */
bf8b29c8
HMH
3693 switch (hkey) {
3694 case TP_HKEY_EV_BAYEJ_ACK:
a713b4d7
HMH
3695 hotkey_autosleep_ack = 1;
3696 printk(TPACPI_INFO
3697 "bay ejected\n");
50ebec09 3698 hotkey_wakeup_hotunplug_complete_notify_change();
3827e7a3 3699 known_ev = true;
bf8b29c8
HMH
3700 break;
3701 case TP_HKEY_EV_OPTDRV_EJ:
3702 /* FIXME: kick libata if SATA link offline */
3703 known_ev = true;
3704 break;
3705 default:
3827e7a3 3706 known_ev = false;
a713b4d7
HMH
3707 }
3708 break;
3709 case 4:
3827e7a3 3710 /* 0x4000-0x4FFF: dock-related wakeups */
67bcae6e 3711 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
a713b4d7
HMH
3712 hotkey_autosleep_ack = 1;
3713 printk(TPACPI_INFO
3714 "undocked\n");
50ebec09 3715 hotkey_wakeup_hotunplug_complete_notify_change();
3827e7a3 3716 known_ev = true;
a713b4d7 3717 } else {
3827e7a3 3718 known_ev = false;
a713b4d7
HMH
3719 }
3720 break;
ff80f137 3721 case 5:
d1edb2b5 3722 /* 0x5000-0x5FFF: human interface helpers */
3827e7a3
HMH
3723 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3724 &ignore_acpi_ev);
ff80f137 3725 break;
106b4e66
HMH
3726 case 6:
3727 /* 0x6000-0x6FFF: thermal alarms */
3728 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3729 &ignore_acpi_ev);
3730 break;
ff80f137
HMH
3731 case 7:
3732 /* 0x7000-0x7FFF: misc */
67bcae6e
HMH
3733 if (tp_features.hotkey_wlsw &&
3734 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
733e27c1 3735 tpacpi_send_radiosw_update();
3b64b51d 3736 send_acpi_ev = 0;
3827e7a3 3737 known_ev = true;
6a38abbf 3738 break;
6a38abbf 3739 }
ff80f137
HMH
3740 /* fallthrough to default */
3741 default:
3827e7a3 3742 known_ev = false;
3b64b51d 3743 }
3827e7a3 3744 if (!known_ev) {
35ff8b9f
HMH
3745 printk(TPACPI_NOTICE
3746 "unhandled HKEY event 0x%04x\n", hkey);
cb429358
HMH
3747 printk(TPACPI_NOTICE
3748 "please report the conditions when this "
3749 "event happened to %s\n", TPACPI_MAIL);
6a38abbf 3750 }
ff80f137 3751
3eea123d 3752 /* Legacy events */
35ff8b9f
HMH
3753 if (!ignore_acpi_ev &&
3754 (send_acpi_ev || hotkey_report_mode < 2)) {
3755 acpi_bus_generate_proc_event(ibm->acpi->device,
3756 event, hkey);
3e5ce914 3757 }
ff80f137 3758
3eea123d 3759 /* netlink events */
3e5ce914 3760 if (!ignore_acpi_ev && send_acpi_ev) {
35ff8b9f
HMH
3761 acpi_bus_generate_netlink_event(
3762 ibm->acpi->device->pnp.device_class,
e0b36fc5 3763 dev_name(&ibm->acpi->device->dev),
35ff8b9f 3764 event, hkey);
3eea123d 3765 }
56b6aeb0
HMH
3766 }
3767}
3768
a713b4d7
HMH
3769static void hotkey_suspend(pm_message_t state)
3770{
3771 /* Do these on suspend, we get the events on early resume! */
3772 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3773 hotkey_autosleep_ack = 0;
3774}
3775
5c29d58f
HMH
3776static void hotkey_resume(void)
3777{
d64c81c4
HMH
3778 tpacpi_disable_brightness_delay();
3779
0d922e3b
HMH
3780 if (hotkey_status_set(true) < 0 ||
3781 hotkey_mask_set(hotkey_acpi_mask) < 0)
35ff8b9f 3782 printk(TPACPI_ERR
0d922e3b
HMH
3783 "error while attempting to reset the event "
3784 "firmware interface\n");
3785
733e27c1 3786 tpacpi_send_radiosw_update();
6c231bd5 3787 hotkey_tablet_mode_notify_change();
50ebec09
HMH
3788 hotkey_wakeup_reason_notify_change();
3789 hotkey_wakeup_hotunplug_complete_notify_change();
db25f16d 3790 hotkey_poll_setup_safe(false);
5c29d58f
HMH
3791}
3792
a0416420 3793/* procfs -------------------------------------------------------------- */
887965e6 3794static int hotkey_read(struct seq_file *m)
1da177e4 3795{
ae92bd17 3796 int res, status;
1da177e4 3797
d8fd94d9 3798 if (!tp_features.hotkey) {
887965e6
AD
3799 seq_printf(m, "status:\t\tnot supported\n");
3800 return 0;
78f81cc4
BD
3801 }
3802
7646ea88 3803 if (mutex_lock_killable(&hotkey_mutex))
fc589a3c 3804 return -ERESTARTSYS;
b2c985e7
HMH
3805 res = hotkey_status_get(&status);
3806 if (!res)
3807 res = hotkey_mask_get();
40ca9fdf 3808 mutex_unlock(&hotkey_mutex);
b86c4722
HMH
3809 if (res)
3810 return res;
1da177e4 3811
887965e6 3812 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
0d922e3b 3813 if (hotkey_all_mask) {
887965e6
AD
3814 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3815 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
1da177e4 3816 } else {
887965e6
AD
3817 seq_printf(m, "mask:\t\tnot supported\n");
3818 seq_printf(m, "commands:\tenable, disable, reset\n");
1da177e4
LT
3819 }
3820
887965e6 3821 return 0;
1da177e4
LT
3822}
3823
406e988b 3824static void hotkey_enabledisable_warn(bool enable)
2586d566
HMH
3825{
3826 tpacpi_log_usertask("procfs hotkey enable/disable");
406e988b
HMH
3827 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3828 TPACPI_WARN
3829 "hotkey enable/disable functionality has been "
3830 "removed from the driver. Hotkeys are always "
3831 "enabled\n"))
3832 printk(TPACPI_ERR
3833 "Please remove the hotkey=enable module "
3834 "parameter, it is deprecated. Hotkeys are always "
3835 "enabled\n");
2586d566
HMH
3836}
3837
78f81cc4 3838static int hotkey_write(char *buf)
1da177e4 3839{
2586d566 3840 int res;
ae92bd17 3841 u32 mask;
1da177e4 3842 char *cmd;
1da177e4 3843
d8fd94d9 3844 if (!tp_features.hotkey)
1da177e4
LT
3845 return -ENODEV;
3846
7646ea88 3847 if (mutex_lock_killable(&hotkey_mutex))
fc589a3c 3848 return -ERESTARTSYS;
40ca9fdf 3849
0d922e3b 3850 mask = hotkey_user_mask;
78f81cc4 3851
40ca9fdf 3852 res = 0;
1da177e4
LT
3853 while ((cmd = next_cmd(&buf))) {
3854 if (strlencmp(cmd, "enable") == 0) {
406e988b 3855 hotkey_enabledisable_warn(1);
1da177e4 3856 } else if (strlencmp(cmd, "disable") == 0) {
406e988b 3857 hotkey_enabledisable_warn(0);
2586d566 3858 res = -EPERM;
1da177e4 3859 } else if (strlencmp(cmd, "reset") == 0) {
20c9aa46
HMH
3860 mask = (hotkey_all_mask | hotkey_source_mask)
3861 & ~hotkey_reserved_mask;
1da177e4
LT
3862 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3863 /* mask set */
3864 } else if (sscanf(cmd, "%x", &mask) == 1) {
3865 /* mask set */
40ca9fdf
HMH
3866 } else {
3867 res = -EINVAL;
3868 goto errexit;
3869 }
1da177e4 3870 }
56e2c200 3871
0d922e3b 3872 if (!res) {
56e2c200
HMH
3873 tpacpi_disclose_usertask("procfs hotkey",
3874 "set mask to 0x%08x\n", mask);
0d922e3b
HMH
3875 res = hotkey_user_mask_set(mask);
3876 }
1da177e4 3877
40ca9fdf
HMH
3878errexit:
3879 mutex_unlock(&hotkey_mutex);
3880 return res;
78f81cc4 3881}
1da177e4 3882
1ba90e3a 3883static const struct acpi_device_id ibm_htk_device_ids[] = {
e0c7dfe7 3884 {TPACPI_ACPI_HKEY_HID, 0},
1ba90e3a
TR
3885 {"", 0},
3886};
3887
8d376cd6 3888static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1ba90e3a 3889 .hid = ibm_htk_device_ids,
8d376cd6
HMH
3890 .notify = hotkey_notify,
3891 .handle = &hkey_handle,
3892 .type = ACPI_DEVICE_NOTIFY,
3893};
3894
a5763f22
HMH
3895static struct ibm_struct hotkey_driver_data = {
3896 .name = "hotkey",
a5763f22
HMH
3897 .read = hotkey_read,
3898 .write = hotkey_write,
3899 .exit = hotkey_exit,
5c29d58f 3900 .resume = hotkey_resume,
a713b4d7 3901 .suspend = hotkey_suspend,
8d376cd6 3902 .acpi = &ibm_hotkey_acpidriver,
a5763f22
HMH
3903};
3904
56b6aeb0
HMH
3905/*************************************************************************
3906 * Bluetooth subdriver
3907 */
1da177e4 3908
f74a27d4
HMH
3909enum {
3910 /* ACPI GBDC/SBDC bits */
3911 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3912 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
153f8220 3913 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
08fedfc9 3914 0 = disable, 1 = enable */
153f8220
HMH
3915};
3916
3917enum {
3918 /* ACPI \BLTH commands */
3919 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3920 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3921 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3922 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3923 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
f74a27d4
HMH
3924};
3925
bee4cd9b
HMH
3926#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3927
19d337df 3928static int bluetooth_get_status(void)
07431ec8
HMH
3929{
3930 int status;
3931
a73f3091
HMH
3932#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3933 if (dbg_bluetoothemul)
3934 return (tpacpi_bluetooth_emulstate) ?
19d337df 3935 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
a73f3091
HMH
3936#endif
3937
07431ec8
HMH
3938 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3939 return -EIO;
3940
0e74dc26 3941 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
19d337df 3942 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
07431ec8
HMH
3943}
3944
19d337df 3945static int bluetooth_set_status(enum tpacpi_rfkill_state state)
0e74dc26
HMH
3946{
3947 int status;
3948
bee4cd9b 3949 vdbg_printk(TPACPI_DBG_RFKILL,
19d337df
JB
3950 "will attempt to %s bluetooth\n",
3951 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
bee4cd9b 3952
a73f3091
HMH
3953#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3954 if (dbg_bluetoothemul) {
19d337df 3955 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
a73f3091
HMH
3956 return 0;
3957 }
3958#endif
3959
19d337df 3960 if (state == TPACPI_RFK_RADIO_ON)
08fedfc9
HMH
3961 status = TP_ACPI_BLUETOOTH_RADIOSSW
3962 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3963 else
3964 status = 0;
19d337df 3965
07431ec8
HMH
3966 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3967 return -EIO;
3968
3969 return 0;
3970}
f74a27d4 3971
d3a6ade4
HMH
3972/* sysfs bluetooth enable ---------------------------------------------- */
3973static ssize_t bluetooth_enable_show(struct device *dev,
3974 struct device_attribute *attr,
3975 char *buf)
3976{
19d337df
JB
3977 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3978 attr, buf);
d3a6ade4
HMH
3979}
3980
3981static ssize_t bluetooth_enable_store(struct device *dev,
3982 struct device_attribute *attr,
3983 const char *buf, size_t count)
3984{
19d337df
JB
3985 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3986 attr, buf, count);
d3a6ade4
HMH
3987}
3988
3989static struct device_attribute dev_attr_bluetooth_enable =
cc4c24e1 3990 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
d3a6ade4
HMH
3991 bluetooth_enable_show, bluetooth_enable_store);
3992
3993/* --------------------------------------------------------------------- */
3994
3995static struct attribute *bluetooth_attributes[] = {
3996 &dev_attr_bluetooth_enable.attr,
3997 NULL
3998};
3999
4000static const struct attribute_group bluetooth_attr_group = {
d3a6ade4
HMH
4001 .attrs = bluetooth_attributes,
4002};
4003
19d337df
JB
4004static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4005 .get_status = bluetooth_get_status,
4006 .set_status = bluetooth_set_status,
4007};
0e74dc26 4008
90d9d3c7
HMH
4009static void bluetooth_shutdown(void)
4010{
4011 /* Order firmware to save current state to NVRAM */
4012 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4013 TP_ACPI_BLTH_SAVE_STATE))
4014 printk(TPACPI_NOTICE
4015 "failed to save bluetooth state to NVRAM\n");
bee4cd9b
HMH
4016 else
4017 vdbg_printk(TPACPI_DBG_RFKILL,
4018 "bluestooth state saved to NVRAM\n");
90d9d3c7
HMH
4019}
4020
07431ec8
HMH
4021static void bluetooth_exit(void)
4022{
4023 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4024 &bluetooth_attr_group);
19d337df
JB
4025
4026 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4027
4028 bluetooth_shutdown();
07431ec8
HMH
4029}
4030
a5763f22 4031static int __init bluetooth_init(struct ibm_init_struct *iibm)
1da177e4 4032{
d3a6ade4 4033 int res;
d6fdd1e9
HMH
4034 int status = 0;
4035
bee4cd9b
HMH
4036 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4037 "initializing bluetooth subdriver\n");
fe08bc4b 4038
e0c7dfe7 4039 TPACPI_ACPIHANDLE_INIT(hkey);
5fba344c 4040
78f81cc4
BD
4041 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4042 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
d8fd94d9 4043 tp_features.bluetooth = hkey_handle &&
d6fdd1e9
HMH
4044 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4045
bee4cd9b
HMH
4046 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4047 "bluetooth is %s, status 0x%02x\n",
d6fdd1e9
HMH
4048 str_supported(tp_features.bluetooth),
4049 status);
4050
a73f3091
HMH
4051#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4052 if (dbg_bluetoothemul) {
4053 tp_features.bluetooth = 1;
4054 printk(TPACPI_INFO
4055 "bluetooth switch emulation enabled\n");
4056 } else
4057#endif
3a872080
HMH
4058 if (tp_features.bluetooth &&
4059 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4060 /* no bluetooth hardware present in system */
4061 tp_features.bluetooth = 0;
bee4cd9b 4062 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3a872080
HMH
4063 "bluetooth hardware not installed\n");
4064 }
4065
0e74dc26
HMH
4066 if (!tp_features.bluetooth)
4067 return 1;
4068
0e74dc26 4069 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
19d337df 4070 &bluetooth_tprfk_ops,
0e74dc26 4071 RFKILL_TYPE_BLUETOOTH,
bee4cd9b 4072 TPACPI_RFK_BLUETOOTH_SW_NAME,
19d337df
JB
4073 true);
4074 if (res)
4075 return res;
4076
4077 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4078 &bluetooth_attr_group);
0e74dc26 4079 if (res) {
19d337df 4080 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
0e74dc26 4081 return res;
d6fdd1e9 4082 }
fe08bc4b 4083
0e74dc26 4084 return 0;
1da177e4
LT
4085}
4086
d3a6ade4 4087/* procfs -------------------------------------------------------------- */
887965e6 4088static int bluetooth_read(struct seq_file *m)
1da177e4 4089{
887965e6 4090 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
1da177e4
LT
4091}
4092
78f81cc4 4093static int bluetooth_write(char *buf)
1da177e4 4094{
19d337df 4095 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
1da177e4
LT
4096}
4097
a5763f22
HMH
4098static struct ibm_struct bluetooth_driver_data = {
4099 .name = "bluetooth",
4100 .read = bluetooth_read,
4101 .write = bluetooth_write,
d3a6ade4 4102 .exit = bluetooth_exit,
90d9d3c7 4103 .shutdown = bluetooth_shutdown,
a5763f22
HMH
4104};
4105
56b6aeb0
HMH
4106/*************************************************************************
4107 * Wan subdriver
4108 */
4109
f74a27d4
HMH
4110enum {
4111 /* ACPI GWAN/SWAN bits */
4112 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4113 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
153f8220 4114 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
08fedfc9 4115 0 = disable, 1 = enable */
f74a27d4
HMH
4116};
4117
bee4cd9b
HMH
4118#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4119
19d337df 4120static int wan_get_status(void)
07431ec8
HMH
4121{
4122 int status;
4123
a73f3091
HMH
4124#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4125 if (dbg_wwanemul)
4126 return (tpacpi_wwan_emulstate) ?
19d337df 4127 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
a73f3091
HMH
4128#endif
4129
07431ec8
HMH
4130 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4131 return -EIO;
4132
0e74dc26 4133 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
19d337df 4134 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
0e74dc26
HMH
4135}
4136
19d337df 4137static int wan_set_status(enum tpacpi_rfkill_state state)
07431ec8
HMH
4138{
4139 int status;
4140
bee4cd9b 4141 vdbg_printk(TPACPI_DBG_RFKILL,
19d337df
JB
4142 "will attempt to %s wwan\n",
4143 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
bee4cd9b 4144
a73f3091
HMH
4145#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4146 if (dbg_wwanemul) {
19d337df 4147 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
a73f3091
HMH
4148 return 0;
4149 }
4150#endif
4151
19d337df 4152 if (state == TPACPI_RFK_RADIO_ON)
08fedfc9
HMH
4153 status = TP_ACPI_WANCARD_RADIOSSW
4154 | TP_ACPI_WANCARD_RESUMECTRL;
4155 else
4156 status = 0;
19d337df 4157
07431ec8
HMH
4158 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4159 return -EIO;
4160
4161 return 0;
4162}
f74a27d4 4163
d3a6ade4
HMH
4164/* sysfs wan enable ---------------------------------------------------- */
4165static ssize_t wan_enable_show(struct device *dev,
4166 struct device_attribute *attr,
4167 char *buf)
4168{
19d337df
JB
4169 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4170 attr, buf);
d3a6ade4
HMH
4171}
4172
4173static ssize_t wan_enable_store(struct device *dev,
4174 struct device_attribute *attr,
4175 const char *buf, size_t count)
4176{
19d337df
JB
4177 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4178 attr, buf, count);
d3a6ade4
HMH
4179}
4180
4181static struct device_attribute dev_attr_wan_enable =
cc4c24e1 4182 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
d3a6ade4
HMH
4183 wan_enable_show, wan_enable_store);
4184
4185/* --------------------------------------------------------------------- */
4186
4187static struct attribute *wan_attributes[] = {
4188 &dev_attr_wan_enable.attr,
4189 NULL
4190};
4191
4192static const struct attribute_group wan_attr_group = {
d3a6ade4
HMH
4193 .attrs = wan_attributes,
4194};
4195
19d337df
JB
4196static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4197 .get_status = wan_get_status,
4198 .set_status = wan_set_status,
4199};
0e74dc26 4200
90d9d3c7
HMH
4201static void wan_shutdown(void)
4202{
4203 /* Order firmware to save current state to NVRAM */
4204 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4205 TP_ACPI_WGSV_SAVE_STATE))
4206 printk(TPACPI_NOTICE
4207 "failed to save WWAN state to NVRAM\n");
bee4cd9b
HMH
4208 else
4209 vdbg_printk(TPACPI_DBG_RFKILL,
4210 "WWAN state saved to NVRAM\n");
90d9d3c7
HMH
4211}
4212
07431ec8
HMH
4213static void wan_exit(void)
4214{
4215 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4216 &wan_attr_group);
19d337df
JB
4217
4218 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4219
4220 wan_shutdown();
07431ec8
HMH
4221}
4222
a5763f22 4223static int __init wan_init(struct ibm_init_struct *iibm)
42adb53c 4224{
d3a6ade4 4225 int res;
d6fdd1e9
HMH
4226 int status = 0;
4227
bee4cd9b
HMH
4228 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4229 "initializing wan subdriver\n");
fe08bc4b 4230
e0c7dfe7 4231 TPACPI_ACPIHANDLE_INIT(hkey);
5fba344c 4232
d8fd94d9 4233 tp_features.wan = hkey_handle &&
d6fdd1e9
HMH
4234 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4235
bee4cd9b
HMH
4236 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4237 "wan is %s, status 0x%02x\n",
d6fdd1e9
HMH
4238 str_supported(tp_features.wan),
4239 status);
4240
a73f3091
HMH
4241#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4242 if (dbg_wwanemul) {
4243 tp_features.wan = 1;
4244 printk(TPACPI_INFO
4245 "wwan switch emulation enabled\n");
4246 } else
4247#endif
3a872080
HMH
4248 if (tp_features.wan &&
4249 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4250 /* no wan hardware present in system */
4251 tp_features.wan = 0;
bee4cd9b 4252 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3a872080
HMH
4253 "wan hardware not installed\n");
4254 }
4255
0e74dc26
HMH
4256 if (!tp_features.wan)
4257 return 1;
4258
0e74dc26 4259 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
19d337df 4260 &wan_tprfk_ops,
0e74dc26 4261 RFKILL_TYPE_WWAN,
bee4cd9b 4262 TPACPI_RFK_WWAN_SW_NAME,
19d337df
JB
4263 true);
4264 if (res)
4265 return res;
4266
4267 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4268 &wan_attr_group);
4269
0e74dc26 4270 if (res) {
19d337df 4271 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
0e74dc26 4272 return res;
d6fdd1e9 4273 }
fe08bc4b 4274
0e74dc26 4275 return 0;
42adb53c
JF
4276}
4277
d3a6ade4 4278/* procfs -------------------------------------------------------------- */
887965e6 4279static int wan_read(struct seq_file *m)
42adb53c 4280{
887965e6 4281 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
42adb53c
JF
4282}
4283
4284static int wan_write(char *buf)
4285{
19d337df 4286 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
42adb53c
JF
4287}
4288
a5763f22
HMH
4289static struct ibm_struct wan_driver_data = {
4290 .name = "wan",
4291 .read = wan_read,
4292 .write = wan_write,
d3a6ade4 4293 .exit = wan_exit,
90d9d3c7 4294 .shutdown = wan_shutdown,
a5763f22
HMH
4295};
4296
0045c0aa
HMH
4297/*************************************************************************
4298 * UWB subdriver
4299 */
4300
4301enum {
4302 /* ACPI GUWB/SUWB bits */
4303 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4304 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4305};
4306
bee4cd9b
HMH
4307#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4308
19d337df 4309static int uwb_get_status(void)
0045c0aa
HMH
4310{
4311 int status;
4312
0045c0aa
HMH
4313#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4314 if (dbg_uwbemul)
4315 return (tpacpi_uwb_emulstate) ?
19d337df 4316 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
0045c0aa
HMH
4317#endif
4318
4319 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4320 return -EIO;
4321
4322 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
19d337df 4323 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
0045c0aa
HMH
4324}
4325
19d337df 4326static int uwb_set_status(enum tpacpi_rfkill_state state)
0045c0aa
HMH
4327{
4328 int status;
4329
bee4cd9b 4330 vdbg_printk(TPACPI_DBG_RFKILL,
19d337df
JB
4331 "will attempt to %s UWB\n",
4332 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
bee4cd9b 4333
0045c0aa
HMH
4334#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4335 if (dbg_uwbemul) {
19d337df 4336 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
0045c0aa
HMH
4337 return 0;
4338 }
4339#endif
4340
19d337df
JB
4341 if (state == TPACPI_RFK_RADIO_ON)
4342 status = TP_ACPI_UWB_RADIOSSW;
4343 else
4344 status = 0;
4345
0045c0aa
HMH
4346 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4347 return -EIO;
4348
0045c0aa
HMH
4349 return 0;
4350}
4351
4352/* --------------------------------------------------------------------- */
4353
19d337df
JB
4354static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4355 .get_status = uwb_get_status,
4356 .set_status = uwb_set_status,
4357};
0045c0aa
HMH
4358
4359static void uwb_exit(void)
4360{
19d337df 4361 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
0045c0aa
HMH
4362}
4363
4364static int __init uwb_init(struct ibm_init_struct *iibm)
4365{
4366 int res;
4367 int status = 0;
4368
bee4cd9b
HMH
4369 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4370 "initializing uwb subdriver\n");
0045c0aa
HMH
4371
4372 TPACPI_ACPIHANDLE_INIT(hkey);
4373
4374 tp_features.uwb = hkey_handle &&
4375 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4376
bee4cd9b
HMH
4377 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4378 "uwb is %s, status 0x%02x\n",
0045c0aa
HMH
4379 str_supported(tp_features.uwb),
4380 status);
4381
4382#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4383 if (dbg_uwbemul) {
4384 tp_features.uwb = 1;
4385 printk(TPACPI_INFO
4386 "uwb switch emulation enabled\n");
4387 } else
4388#endif
4389 if (tp_features.uwb &&
4390 !(status & TP_ACPI_UWB_HWPRESENT)) {
4391 /* no uwb hardware present in system */
4392 tp_features.uwb = 0;
4393 dbg_printk(TPACPI_DBG_INIT,
4394 "uwb hardware not installed\n");
4395 }
4396
4397 if (!tp_features.uwb)
4398 return 1;
4399
4400 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
19d337df 4401 &uwb_tprfk_ops,
0045c0aa 4402 RFKILL_TYPE_UWB,
bee4cd9b 4403 TPACPI_RFK_UWB_SW_NAME,
19d337df 4404 false);
0045c0aa
HMH
4405 return res;
4406}
4407
4408static struct ibm_struct uwb_driver_data = {
4409 .name = "uwb",
4410 .exit = uwb_exit,
4411 .flags.experimental = 1,
4412};
4413
56b6aeb0
HMH
4414/*************************************************************************
4415 * Video subdriver
4416 */
4417
d7c1d17d
HMH
4418#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4419
f74a27d4
HMH
4420enum video_access_mode {
4421 TPACPI_VIDEO_NONE = 0,
4422 TPACPI_VIDEO_570, /* 570 */
4423 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4424 TPACPI_VIDEO_NEW, /* all others */
4425};
4426
4427enum { /* video status flags, based on VIDEO_570 */
4428 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4429 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4430 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4431};
4432
4433enum { /* TPACPI_VIDEO_570 constants */
4434 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4435 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4436 * video_status_flags */
4437 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4438 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4439};
4440
9a8e1738
HMH
4441static enum video_access_mode video_supported;
4442static int video_orig_autosw;
78f81cc4 4443
f74a27d4
HMH
4444static int video_autosw_get(void);
4445static int video_autosw_set(int enable);
4446
e0c7dfe7 4447TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
56b6aeb0 4448
a5763f22 4449static int __init video_init(struct ibm_init_struct *iibm)
1da177e4 4450{
78f81cc4
BD
4451 int ivga;
4452
fe08bc4b
HMH
4453 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4454
e0c7dfe7 4455 TPACPI_ACPIHANDLE_INIT(vid);
e28393c0
HMH
4456 if (tpacpi_is_ibm())
4457 TPACPI_ACPIHANDLE_INIT(vid2);
5fba344c 4458
78f81cc4
BD
4459 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4460 /* G41, assume IVGA doesn't change */
4461 vid_handle = vid2_handle;
4462
4463 if (!vid_handle)
4464 /* video switching not supported on R30, R31 */
efa27145 4465 video_supported = TPACPI_VIDEO_NONE;
e28393c0
HMH
4466 else if (tpacpi_is_ibm() &&
4467 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
78f81cc4 4468 /* 570 */
efa27145 4469 video_supported = TPACPI_VIDEO_570;
e28393c0
HMH
4470 else if (tpacpi_is_ibm() &&
4471 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
78f81cc4 4472 /* 600e/x, 770e, 770x */
efa27145 4473 video_supported = TPACPI_VIDEO_770;
78f81cc4
BD
4474 else
4475 /* all others */
efa27145 4476 video_supported = TPACPI_VIDEO_NEW;
1da177e4 4477
fe08bc4b
HMH
4478 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4479 str_supported(video_supported != TPACPI_VIDEO_NONE),
4480 video_supported);
4481
5fba344c 4482 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1da177e4
LT
4483}
4484
56b6aeb0
HMH
4485static void video_exit(void)
4486{
83f34724
HMH
4487 dbg_printk(TPACPI_DBG_EXIT,
4488 "restoring original video autoswitch mode\n");
4489 if (video_autosw_set(video_orig_autosw))
e0c7dfe7 4490 printk(TPACPI_ERR "error while trying to restore original "
83f34724 4491 "video autoswitch mode\n");
56b6aeb0
HMH
4492}
4493
83f34724 4494static int video_outputsw_get(void)
1da177e4
LT
4495{
4496 int status = 0;
4497 int i;
4498
83f34724
HMH
4499 switch (video_supported) {
4500 case TPACPI_VIDEO_570:
4501 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4502 TP_ACPI_VIDEO_570_PHSCMD))
4503 return -EIO;
4504 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4505 break;
4506 case TPACPI_VIDEO_770:
4507 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4508 return -EIO;
4509 if (i)
4510 status |= TP_ACPI_VIDEO_S_LCD;
4511 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4512 return -EIO;
4513 if (i)
4514 status |= TP_ACPI_VIDEO_S_CRT;
4515 break;
4516 case TPACPI_VIDEO_NEW:
4517 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4518 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4519 return -EIO;
4520 if (i)
4521 status |= TP_ACPI_VIDEO_S_CRT;
4522
4523 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4524 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4525 return -EIO;
4526 if (i)
4527 status |= TP_ACPI_VIDEO_S_LCD;
4528 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4529 return -EIO;
4530 if (i)
4531 status |= TP_ACPI_VIDEO_S_DVI;
4532 break;
4533 default:
4534 return -ENOSYS;
78f81cc4
BD
4535 }
4536
4537 return status;
4538}
1da177e4 4539
83f34724 4540static int video_outputsw_set(int status)
78f81cc4 4541{
83f34724
HMH
4542 int autosw;
4543 int res = 0;
4544
4545 switch (video_supported) {
4546 case TPACPI_VIDEO_570:
4547 res = acpi_evalf(NULL, NULL,
4548 "\\_SB.PHS2", "vdd",
4549 TP_ACPI_VIDEO_570_PHS2CMD,
4550 status | TP_ACPI_VIDEO_570_PHS2SET);
4551 break;
4552 case TPACPI_VIDEO_770:
4553 autosw = video_autosw_get();
4554 if (autosw < 0)
4555 return autosw;
1da177e4 4556
83f34724
HMH
4557 res = video_autosw_set(1);
4558 if (res)
4559 return res;
4560 res = acpi_evalf(vid_handle, NULL,
4561 "ASWT", "vdd", status * 0x100, 0);
4562 if (!autosw && video_autosw_set(autosw)) {
35ff8b9f
HMH
4563 printk(TPACPI_ERR
4564 "video auto-switch left enabled due to error\n");
83f34724
HMH
4565 return -EIO;
4566 }
4567 break;
4568 case TPACPI_VIDEO_NEW:
4569 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
35ff8b9f 4570 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
83f34724
HMH
4571 break;
4572 default:
4573 return -ENOSYS;
4574 }
1da177e4 4575
83f34724 4576 return (res)? 0 : -EIO;
1da177e4
LT
4577}
4578
83f34724 4579static int video_autosw_get(void)
78f81cc4 4580{
83f34724 4581 int autosw = 0;
78f81cc4 4582
83f34724
HMH
4583 switch (video_supported) {
4584 case TPACPI_VIDEO_570:
4585 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4586 return -EIO;
4587 break;
4588 case TPACPI_VIDEO_770:
4589 case TPACPI_VIDEO_NEW:
4590 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4591 return -EIO;
4592 break;
4593 default:
4594 return -ENOSYS;
4595 }
78f81cc4 4596
83f34724 4597 return autosw & 1;
78f81cc4
BD
4598}
4599
83f34724 4600static int video_autosw_set(int enable)
78f81cc4 4601{
83f34724
HMH
4602 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4603 return -EIO;
4604 return 0;
78f81cc4
BD
4605}
4606
83f34724 4607static int video_outputsw_cycle(void)
78f81cc4 4608{
83f34724
HMH
4609 int autosw = video_autosw_get();
4610 int res;
78f81cc4 4611
83f34724
HMH
4612 if (autosw < 0)
4613 return autosw;
78f81cc4 4614
83f34724
HMH
4615 switch (video_supported) {
4616 case TPACPI_VIDEO_570:
4617 res = video_autosw_set(1);
4618 if (res)
4619 return res;
4620 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4621 break;
4622 case TPACPI_VIDEO_770:
4623 case TPACPI_VIDEO_NEW:
4624 res = video_autosw_set(1);
4625 if (res)
4626 return res;
4627 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4628 break;
4629 default:
4630 return -ENOSYS;
4631 }
4632 if (!autosw && video_autosw_set(autosw)) {
35ff8b9f
HMH
4633 printk(TPACPI_ERR
4634 "video auto-switch left enabled due to error\n");
83f34724 4635 return -EIO;
78f81cc4
BD
4636 }
4637
83f34724
HMH
4638 return (res)? 0 : -EIO;
4639}
4640
4641static int video_expand_toggle(void)
4642{
4643 switch (video_supported) {
4644 case TPACPI_VIDEO_570:
4645 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4646 0 : -EIO;
4647 case TPACPI_VIDEO_770:
4648 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4649 0 : -EIO;
4650 case TPACPI_VIDEO_NEW:
4651 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4652 0 : -EIO;
4653 default:
4654 return -ENOSYS;
4655 }
4656 /* not reached */
78f81cc4
BD
4657}
4658
887965e6 4659static int video_read(struct seq_file *m)
56b6aeb0 4660{
83f34724 4661 int status, autosw;
56b6aeb0 4662
83f34724 4663 if (video_supported == TPACPI_VIDEO_NONE) {
887965e6
AD
4664 seq_printf(m, "status:\t\tnot supported\n");
4665 return 0;
56b6aeb0
HMH
4666 }
4667
b525c06c
HMH
4668 /* Even reads can crash X.org, so... */
4669 if (!capable(CAP_SYS_ADMIN))
4670 return -EPERM;
4671
83f34724
HMH
4672 status = video_outputsw_get();
4673 if (status < 0)
4674 return status;
4675
4676 autosw = video_autosw_get();
4677 if (autosw < 0)
4678 return autosw;
4679
887965e6
AD
4680 seq_printf(m, "status:\t\tsupported\n");
4681 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4682 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
efa27145 4683 if (video_supported == TPACPI_VIDEO_NEW)
887965e6
AD
4684 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4685 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4686 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4687 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
efa27145 4688 if (video_supported == TPACPI_VIDEO_NEW)
887965e6
AD
4689 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4690 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4691 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
56b6aeb0 4692
887965e6 4693 return 0;
56b6aeb0
HMH
4694}
4695
78f81cc4 4696static int video_write(char *buf)
1da177e4
LT
4697{
4698 char *cmd;
4699 int enable, disable, status;
83f34724 4700 int res;
1da177e4 4701
83f34724 4702 if (video_supported == TPACPI_VIDEO_NONE)
78f81cc4
BD
4703 return -ENODEV;
4704
b525c06c
HMH
4705 /* Even reads can crash X.org, let alone writes... */
4706 if (!capable(CAP_SYS_ADMIN))
4707 return -EPERM;
4708
83f34724
HMH
4709 enable = 0;
4710 disable = 0;
1da177e4
LT
4711
4712 while ((cmd = next_cmd(&buf))) {
4713 if (strlencmp(cmd, "lcd_enable") == 0) {
83f34724 4714 enable |= TP_ACPI_VIDEO_S_LCD;
1da177e4 4715 } else if (strlencmp(cmd, "lcd_disable") == 0) {
83f34724 4716 disable |= TP_ACPI_VIDEO_S_LCD;
1da177e4 4717 } else if (strlencmp(cmd, "crt_enable") == 0) {
83f34724 4718 enable |= TP_ACPI_VIDEO_S_CRT;
1da177e4 4719 } else if (strlencmp(cmd, "crt_disable") == 0) {
83f34724 4720 disable |= TP_ACPI_VIDEO_S_CRT;
efa27145 4721 } else if (video_supported == TPACPI_VIDEO_NEW &&
78f81cc4 4722 strlencmp(cmd, "dvi_enable") == 0) {
83f34724 4723 enable |= TP_ACPI_VIDEO_S_DVI;
efa27145 4724 } else if (video_supported == TPACPI_VIDEO_NEW &&
78f81cc4 4725 strlencmp(cmd, "dvi_disable") == 0) {
83f34724 4726 disable |= TP_ACPI_VIDEO_S_DVI;
1da177e4 4727 } else if (strlencmp(cmd, "auto_enable") == 0) {
83f34724
HMH
4728 res = video_autosw_set(1);
4729 if (res)
4730 return res;
1da177e4 4731 } else if (strlencmp(cmd, "auto_disable") == 0) {
83f34724
HMH
4732 res = video_autosw_set(0);
4733 if (res)
4734 return res;
1da177e4 4735 } else if (strlencmp(cmd, "video_switch") == 0) {
83f34724
HMH
4736 res = video_outputsw_cycle();
4737 if (res)
4738 return res;
1da177e4 4739 } else if (strlencmp(cmd, "expand_toggle") == 0) {
83f34724
HMH
4740 res = video_expand_toggle();
4741 if (res)
4742 return res;
1da177e4
LT
4743 } else
4744 return -EINVAL;
4745 }
4746
4747 if (enable || disable) {
83f34724
HMH
4748 status = video_outputsw_get();
4749 if (status < 0)
4750 return status;
4751 res = video_outputsw_set((status & ~disable) | enable);
4752 if (res)
4753 return res;
1da177e4
LT
4754 }
4755
4756 return 0;
4757}
4758
a5763f22
HMH
4759static struct ibm_struct video_driver_data = {
4760 .name = "video",
4761 .read = video_read,
4762 .write = video_write,
4763 .exit = video_exit,
4764};
4765
d7c1d17d
HMH
4766#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4767
56b6aeb0
HMH
4768/*************************************************************************
4769 * Light (thinklight) subdriver
4770 */
1da177e4 4771
e0c7dfe7
HMH
4772TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4773TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
56b6aeb0 4774
4fa6811b
HMH
4775static int light_get_status(void)
4776{
4777 int status = 0;
4778
4779 if (tp_features.light_status) {
4780 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4781 return -EIO;
4782 return (!!status);
4783 }
4784
4785 return -ENXIO;
4786}
4787
4788static int light_set_status(int status)
4789{
4790 int rc;
4791
4792 if (tp_features.light) {
4793 if (cmos_handle) {
4794 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4795 (status)?
4796 TP_CMOS_THINKLIGHT_ON :
4797 TP_CMOS_THINKLIGHT_OFF);
4798 } else {
4799 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4800 (status)? 1 : 0);
4801 }
4802 return (rc)? 0 : -EIO;
4803 }
4804
4805 return -ENXIO;
4806}
4807
e306501d
HMH
4808static void light_set_status_worker(struct work_struct *work)
4809{
4810 struct tpacpi_led_classdev *data =
4811 container_of(work, struct tpacpi_led_classdev, work);
4812
4813 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
75bd3bf2 4814 light_set_status((data->new_state != TPACPI_LED_OFF));
e306501d
HMH
4815}
4816
4817static void light_sysfs_set(struct led_classdev *led_cdev,
4818 enum led_brightness brightness)
4819{
4820 struct tpacpi_led_classdev *data =
4821 container_of(led_cdev,
4822 struct tpacpi_led_classdev,
4823 led_classdev);
75bd3bf2
HMH
4824 data->new_state = (brightness != LED_OFF) ?
4825 TPACPI_LED_ON : TPACPI_LED_OFF;
e0e3c061 4826 queue_work(tpacpi_wq, &data->work);
e306501d
HMH
4827}
4828
4829static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4830{
4831 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4832}
4833
4834static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4835 .led_classdev = {
4836 .name = "tpacpi::thinklight",
4837 .brightness_set = &light_sysfs_set,
4838 .brightness_get = &light_sysfs_get,
4839 }
4840};
4841
a5763f22 4842static int __init light_init(struct ibm_init_struct *iibm)
1da177e4 4843{
9c0a76e1 4844 int rc;
e306501d 4845
fe08bc4b
HMH
4846 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4847
e28393c0
HMH
4848 if (tpacpi_is_ibm()) {
4849 TPACPI_ACPIHANDLE_INIT(ledb);
4850 TPACPI_ACPIHANDLE_INIT(lght);
4851 }
e0c7dfe7 4852 TPACPI_ACPIHANDLE_INIT(cmos);
e306501d 4853 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
5fba344c 4854
78f81cc4 4855 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
d8fd94d9 4856 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
78f81cc4 4857
d8fd94d9 4858 if (tp_features.light)
78f81cc4
BD
4859 /* light status not supported on
4860 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
d8fd94d9
HMH
4861 tp_features.light_status =
4862 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1da177e4 4863
9c0a76e1
HMH
4864 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4865 str_supported(tp_features.light),
4866 str_supported(tp_features.light_status));
fe08bc4b 4867
9c0a76e1
HMH
4868 if (!tp_features.light)
4869 return 1;
4870
4871 rc = led_classdev_register(&tpacpi_pdev->dev,
4872 &tpacpi_led_thinklight.led_classdev);
e306501d
HMH
4873
4874 if (rc < 0) {
4875 tp_features.light = 0;
4876 tp_features.light_status = 0;
9c0a76e1
HMH
4877 } else {
4878 rc = 0;
e306501d 4879 }
9c0a76e1 4880
e306501d
HMH
4881 return rc;
4882}
4883
4884static void light_exit(void)
4885{
4886 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4887 if (work_pending(&tpacpi_led_thinklight.work))
e0e3c061 4888 flush_workqueue(tpacpi_wq);
1da177e4
LT
4889}
4890
887965e6 4891static int light_read(struct seq_file *m)
1da177e4 4892{
4fa6811b 4893 int status;
1da177e4 4894
d8fd94d9 4895 if (!tp_features.light) {
887965e6 4896 seq_printf(m, "status:\t\tnot supported\n");
d8fd94d9 4897 } else if (!tp_features.light_status) {
887965e6
AD
4898 seq_printf(m, "status:\t\tunknown\n");
4899 seq_printf(m, "commands:\ton, off\n");
78f81cc4 4900 } else {
4fa6811b
HMH
4901 status = light_get_status();
4902 if (status < 0)
4903 return status;
887965e6
AD
4904 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4905 seq_printf(m, "commands:\ton, off\n");
78f81cc4 4906 }
1da177e4 4907
887965e6 4908 return 0;
1da177e4
LT
4909}
4910
78f81cc4 4911static int light_write(char *buf)
1da177e4 4912{
1da177e4 4913 char *cmd;
4fa6811b 4914 int newstatus = 0;
78f81cc4 4915
d8fd94d9 4916 if (!tp_features.light)
78f81cc4
BD
4917 return -ENODEV;
4918
1da177e4
LT
4919 while ((cmd = next_cmd(&buf))) {
4920 if (strlencmp(cmd, "on") == 0) {
4fa6811b 4921 newstatus = 1;
1da177e4 4922 } else if (strlencmp(cmd, "off") == 0) {
4fa6811b 4923 newstatus = 0;
1da177e4
LT
4924 } else
4925 return -EINVAL;
1da177e4
LT
4926 }
4927
4fa6811b 4928 return light_set_status(newstatus);
1da177e4
LT
4929}
4930
a5763f22
HMH
4931static struct ibm_struct light_driver_data = {
4932 .name = "light",
4933 .read = light_read,
4934 .write = light_write,
e306501d 4935 .exit = light_exit,
a5763f22
HMH
4936};
4937
56b6aeb0
HMH
4938/*************************************************************************
4939 * CMOS subdriver
4940 */
4941
b616004c
HMH
4942/* sysfs cmos_command -------------------------------------------------- */
4943static ssize_t cmos_command_store(struct device *dev,
4944 struct device_attribute *attr,
4945 const char *buf, size_t count)
4946{
4947 unsigned long cmos_cmd;
4948 int res;
4949
4950 if (parse_strtoul(buf, 21, &cmos_cmd))
4951 return -EINVAL;
4952
4953 res = issue_thinkpad_cmos_command(cmos_cmd);
4954 return (res)? res : count;
4955}
4956
4957static struct device_attribute dev_attr_cmos_command =
4958 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4959
4960/* --------------------------------------------------------------------- */
4961
a5763f22 4962static int __init cmos_init(struct ibm_init_struct *iibm)
5fba344c 4963{
b616004c
HMH
4964 int res;
4965
fe08bc4b
HMH
4966 vdbg_printk(TPACPI_DBG_INIT,
4967 "initializing cmos commands subdriver\n");
4968
e0c7dfe7 4969 TPACPI_ACPIHANDLE_INIT(cmos);
5fba344c 4970
fe08bc4b
HMH
4971 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4972 str_supported(cmos_handle != NULL));
b616004c
HMH
4973
4974 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4975 if (res)
4976 return res;
4977
5fba344c
HMH
4978 return (cmos_handle)? 0 : 1;
4979}
4980
b616004c
HMH
4981static void cmos_exit(void)
4982{
4983 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4984}
4985
887965e6 4986static int cmos_read(struct seq_file *m)
1da177e4 4987{
78f81cc4
BD
4988 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4989 R30, R31, T20-22, X20-21 */
1da177e4 4990 if (!cmos_handle)
887965e6 4991 seq_printf(m, "status:\t\tnot supported\n");
1da177e4 4992 else {
887965e6
AD
4993 seq_printf(m, "status:\t\tsupported\n");
4994 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
1da177e4
LT
4995 }
4996
887965e6 4997 return 0;
1da177e4
LT
4998}
4999
78f81cc4 5000static int cmos_write(char *buf)
1da177e4
LT
5001{
5002 char *cmd;
c9bea99c 5003 int cmos_cmd, res;
1da177e4
LT
5004
5005 while ((cmd = next_cmd(&buf))) {
78f81cc4
BD
5006 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5007 cmos_cmd >= 0 && cmos_cmd <= 21) {
1da177e4
LT
5008 /* cmos_cmd set */
5009 } else
5010 return -EINVAL;
5011
c9bea99c
HMH
5012 res = issue_thinkpad_cmos_command(cmos_cmd);
5013 if (res)
5014 return res;
1da177e4
LT
5015 }
5016
5017 return 0;
78f81cc4
BD
5018}
5019
a5763f22
HMH
5020static struct ibm_struct cmos_driver_data = {
5021 .name = "cmos",
5022 .read = cmos_read,
5023 .write = cmos_write,
b616004c 5024 .exit = cmos_exit,
a5763f22 5025};
56b6aeb0
HMH
5026
5027/*************************************************************************
5028 * LED subdriver
5029 */
5030
f74a27d4
HMH
5031enum led_access_mode {
5032 TPACPI_LED_NONE = 0,
5033 TPACPI_LED_570, /* 570 */
5034 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5035 TPACPI_LED_NEW, /* all others */
5036};
5037
5038enum { /* For TPACPI_LED_OLD */
5039 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5040 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5041 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5042};
5043
9a8e1738 5044static enum led_access_mode led_supported;
78f81cc4 5045
2cbb5c8f 5046static acpi_handle led_handle;
56b6aeb0 5047
f21179a4 5048#define TPACPI_LED_NUMLEDS 16
af116101
HMH
5049static struct tpacpi_led_classdev *tpacpi_leds;
5050static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
e3aa51fe 5051static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
af116101
HMH
5052 /* there's a limit of 19 chars + NULL before 2.6.26 */
5053 "tpacpi::power",
5054 "tpacpi:orange:batt",
5055 "tpacpi:green:batt",
5056 "tpacpi::dock_active",
5057 "tpacpi::bay_active",
5058 "tpacpi::dock_batt",
5059 "tpacpi::unknown_led",
5060 "tpacpi::standby",
f21179a4
HMH
5061 "tpacpi::dock_status1",
5062 "tpacpi::dock_status2",
5063 "tpacpi::unknown_led2",
5064 "tpacpi::unknown_led3",
5065 "tpacpi::thinkvantage",
af116101 5066};
f21179a4 5067#define TPACPI_SAFE_LEDS 0x1081U
a4d5effc
HMH
5068
5069static inline bool tpacpi_is_led_restricted(const unsigned int led)
5070{
5071#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5072 return false;
5073#else
f21179a4 5074 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
a4d5effc
HMH
5075#endif
5076}
af116101 5077
24e45bbe 5078static int led_get_status(const unsigned int led)
4fa6811b
HMH
5079{
5080 int status;
af116101 5081 enum led_status_t led_s;
4fa6811b
HMH
5082
5083 switch (led_supported) {
5084 case TPACPI_LED_570:
5085 if (!acpi_evalf(ec_handle,
5086 &status, "GLED", "dd", 1 << led))
5087 return -EIO;
af116101 5088 led_s = (status == 0)?
4fa6811b
HMH
5089 TPACPI_LED_OFF :
5090 ((status == 1)?
5091 TPACPI_LED_ON :
5092 TPACPI_LED_BLINK);
af116101
HMH
5093 tpacpi_led_state_cache[led] = led_s;
5094 return led_s;
4fa6811b
HMH
5095 default:
5096 return -ENXIO;
5097 }
5098
5099 /* not reached */
5100}
5101
24e45bbe
HMH
5102static int led_set_status(const unsigned int led,
5103 const enum led_status_t ledstatus)
4fa6811b
HMH
5104{
5105 /* off, on, blink. Index is led_status_t */
24e45bbe
HMH
5106 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5107 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4fa6811b
HMH
5108
5109 int rc = 0;
5110
5111 switch (led_supported) {
5112 case TPACPI_LED_570:
24e45bbe 5113 /* 570 */
a4d5effc 5114 if (unlikely(led > 7))
24e45bbe 5115 return -EINVAL;
a4d5effc
HMH
5116 if (unlikely(tpacpi_is_led_restricted(led)))
5117 return -EPERM;
24e45bbe
HMH
5118 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5119 (1 << led), led_sled_arg1[ledstatus]))
5120 rc = -EIO;
5121 break;
4fa6811b 5122 case TPACPI_LED_OLD:
24e45bbe 5123 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
a4d5effc 5124 if (unlikely(led > 7))
24e45bbe 5125 return -EINVAL;
a4d5effc
HMH
5126 if (unlikely(tpacpi_is_led_restricted(led)))
5127 return -EPERM;
24e45bbe
HMH
5128 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5129 if (rc >= 0)
5130 rc = ec_write(TPACPI_LED_EC_HLBL,
5131 (ledstatus == TPACPI_LED_BLINK) << led);
5132 if (rc >= 0)
5133 rc = ec_write(TPACPI_LED_EC_HLCL,
5134 (ledstatus != TPACPI_LED_OFF) << led);
5135 break;
4fa6811b 5136 case TPACPI_LED_NEW:
24e45bbe 5137 /* all others */
a4d5effc
HMH
5138 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5139 return -EINVAL;
5140 if (unlikely(tpacpi_is_led_restricted(led)))
5141 return -EPERM;
24e45bbe
HMH
5142 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5143 led, led_led_arg1[ledstatus]))
5144 rc = -EIO;
5145 break;
4fa6811b
HMH
5146 default:
5147 rc = -ENXIO;
5148 }
5149
af116101
HMH
5150 if (!rc)
5151 tpacpi_led_state_cache[led] = ledstatus;
5152
5153 return rc;
5154}
5155
af116101
HMH
5156static void led_set_status_worker(struct work_struct *work)
5157{
5158 struct tpacpi_led_classdev *data =
5159 container_of(work, struct tpacpi_led_classdev, work);
5160
5161 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
75bd3bf2 5162 led_set_status(data->led, data->new_state);
af116101
HMH
5163}
5164
5165static void led_sysfs_set(struct led_classdev *led_cdev,
5166 enum led_brightness brightness)
5167{
5168 struct tpacpi_led_classdev *data = container_of(led_cdev,
5169 struct tpacpi_led_classdev, led_classdev);
5170
75bd3bf2
HMH
5171 if (brightness == LED_OFF)
5172 data->new_state = TPACPI_LED_OFF;
5173 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5174 data->new_state = TPACPI_LED_ON;
5175 else
5176 data->new_state = TPACPI_LED_BLINK;
5177
e0e3c061 5178 queue_work(tpacpi_wq, &data->work);
af116101
HMH
5179}
5180
5181static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5182 unsigned long *delay_on, unsigned long *delay_off)
5183{
5184 struct tpacpi_led_classdev *data = container_of(led_cdev,
5185 struct tpacpi_led_classdev, led_classdev);
5186
5187 /* Can we choose the flash rate? */
5188 if (*delay_on == 0 && *delay_off == 0) {
5189 /* yes. set them to the hardware blink rate (1 Hz) */
5190 *delay_on = 500; /* ms */
5191 *delay_off = 500; /* ms */
5192 } else if ((*delay_on != 500) || (*delay_off != 500))
5193 return -EINVAL;
5194
75bd3bf2 5195 data->new_state = TPACPI_LED_BLINK;
e0e3c061 5196 queue_work(tpacpi_wq, &data->work);
af116101
HMH
5197
5198 return 0;
5199}
5200
5201static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5202{
5203 int rc;
5204
5205 struct tpacpi_led_classdev *data = container_of(led_cdev,
5206 struct tpacpi_led_classdev, led_classdev);
5207
5208 rc = led_get_status(data->led);
5209
5210 if (rc == TPACPI_LED_OFF || rc < 0)
5211 rc = LED_OFF; /* no error handling in led class :( */
5212 else
5213 rc = LED_FULL;
5214
4fa6811b
HMH
5215 return rc;
5216}
5217
af116101
HMH
5218static void led_exit(void)
5219{
5220 unsigned int i;
5221
5222 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5223 if (tpacpi_leds[i].led_classdev.name)
5224 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5225 }
5226
5227 kfree(tpacpi_leds);
af116101
HMH
5228}
5229
a4d5effc
HMH
5230static int __init tpacpi_init_led(unsigned int led)
5231{
5232 int rc;
5233
5234 tpacpi_leds[led].led = led;
5235
f21179a4
HMH
5236 /* LEDs with no name don't get registered */
5237 if (!tpacpi_led_names[led])
5238 return 0;
5239
a4d5effc
HMH
5240 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5241 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5242 if (led_supported == TPACPI_LED_570)
5243 tpacpi_leds[led].led_classdev.brightness_get =
5244 &led_sysfs_get;
5245
5246 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5247
5248 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5249
5250 rc = led_classdev_register(&tpacpi_pdev->dev,
5251 &tpacpi_leds[led].led_classdev);
5252 if (rc < 0)
5253 tpacpi_leds[led].led_classdev.name = NULL;
5254
5255 return rc;
5256}
5257
f21179a4
HMH
5258static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5259 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5260 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5261 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5262
5263 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5264 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5265 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5266 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5267 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5268 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5269 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5270 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5271
5272 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5273 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5274 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5275 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5276 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5277
5278 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5279 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5280 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5281 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5282
5283 /* (1) - may have excess leds enabled on MSB */
5284
5285 /* Defaults (order matters, keep last, don't reorder!) */
5286 { /* Lenovo */
5287 .vendor = PCI_VENDOR_ID_LENOVO,
5288 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5289 .quirks = 0x1fffU,
5290 },
5291 { /* IBM ThinkPads with no EC version string */
5292 .vendor = PCI_VENDOR_ID_IBM,
5293 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5294 .quirks = 0x00ffU,
5295 },
5296 { /* IBM ThinkPads with EC version string */
5297 .vendor = PCI_VENDOR_ID_IBM,
5298 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5299 .quirks = 0x00bfU,
5300 },
5301};
5302
5303#undef TPACPI_LEDQ_IBM
5304#undef TPACPI_LEDQ_LNV
5305
2cbb5c8f
HMH
5306static enum led_access_mode __init led_init_detect_mode(void)
5307{
5308 acpi_status status;
5309
5310 if (tpacpi_is_ibm()) {
5311 /* 570 */
5312 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5313 if (ACPI_SUCCESS(status))
5314 return TPACPI_LED_570;
5315
5316 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5317 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5318 if (ACPI_SUCCESS(status))
5319 return TPACPI_LED_OLD;
5320 }
5321
5322 /* most others */
5323 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5324 if (ACPI_SUCCESS(status))
5325 return TPACPI_LED_NEW;
5326
5327 /* R30, R31, and unknown firmwares */
5328 led_handle = NULL;
5329 return TPACPI_LED_NONE;
5330}
5331
a5763f22 5332static int __init led_init(struct ibm_init_struct *iibm)
78f81cc4 5333{
af116101
HMH
5334 unsigned int i;
5335 int rc;
f21179a4 5336 unsigned long useful_leds;
af116101 5337
fe08bc4b
HMH
5338 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5339
2cbb5c8f 5340 led_supported = led_init_detect_mode();
78f81cc4 5341
fe08bc4b
HMH
5342 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5343 str_supported(led_supported), led_supported);
5344
f21179a4
HMH
5345 if (led_supported == TPACPI_LED_NONE)
5346 return 1;
5347
af116101
HMH
5348 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5349 GFP_KERNEL);
5350 if (!tpacpi_leds) {
5351 printk(TPACPI_ERR "Out of memory for LED data\n");
5352 return -ENOMEM;
5353 }
5354
f21179a4
HMH
5355 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5356 ARRAY_SIZE(led_useful_qtable));
5357
af116101 5358 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
f21179a4
HMH
5359 if (!tpacpi_is_led_restricted(i) &&
5360 test_bit(i, &useful_leds)) {
a4d5effc
HMH
5361 rc = tpacpi_init_led(i);
5362 if (rc < 0) {
5363 led_exit();
5364 return rc;
5365 }
af116101
HMH
5366 }
5367 }
5368
a4d5effc 5369#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
f21179a4
HMH
5370 printk(TPACPI_NOTICE
5371 "warning: userspace override of important "
5372 "firmware LEDs is enabled\n");
a4d5effc 5373#endif
f21179a4 5374 return 0;
78f81cc4
BD
5375}
5376
4fa6811b
HMH
5377#define str_led_status(s) \
5378 ((s) == TPACPI_LED_OFF ? "off" : \
5379 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
78f81cc4 5380
887965e6 5381static int led_read(struct seq_file *m)
1da177e4 5382{
78f81cc4 5383 if (!led_supported) {
887965e6
AD
5384 seq_printf(m, "status:\t\tnot supported\n");
5385 return 0;
78f81cc4 5386 }
887965e6 5387 seq_printf(m, "status:\t\tsupported\n");
78f81cc4 5388
efa27145 5389 if (led_supported == TPACPI_LED_570) {
78f81cc4
BD
5390 /* 570 */
5391 int i, status;
5392 for (i = 0; i < 8; i++) {
4fa6811b
HMH
5393 status = led_get_status(i);
5394 if (status < 0)
78f81cc4 5395 return -EIO;
887965e6 5396 seq_printf(m, "%d:\t\t%s\n",
4fa6811b 5397 i, str_led_status(status));
78f81cc4
BD
5398 }
5399 }
5400
887965e6 5401 seq_printf(m, "commands:\t"
f21179a4 5402 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
1da177e4 5403
887965e6 5404 return 0;
1da177e4
LT
5405}
5406
78f81cc4 5407static int led_write(char *buf)
1da177e4
LT
5408{
5409 char *cmd;
4fa6811b
HMH
5410 int led, rc;
5411 enum led_status_t s;
78f81cc4
BD
5412
5413 if (!led_supported)
5414 return -ENODEV;
1da177e4
LT
5415
5416 while ((cmd = next_cmd(&buf))) {
f21179a4 5417 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
1da177e4
LT
5418 return -EINVAL;
5419
78f81cc4 5420 if (strstr(cmd, "off")) {
4fa6811b 5421 s = TPACPI_LED_OFF;
1da177e4 5422 } else if (strstr(cmd, "on")) {
4fa6811b 5423 s = TPACPI_LED_ON;
78f81cc4 5424 } else if (strstr(cmd, "blink")) {
4fa6811b 5425 s = TPACPI_LED_BLINK;
78f81cc4 5426 } else {
4fa6811b 5427 return -EINVAL;
78f81cc4 5428 }
4fa6811b
HMH
5429
5430 rc = led_set_status(led, s);
5431 if (rc < 0)
5432 return rc;
78f81cc4
BD
5433 }
5434
5435 return 0;
5436}
5437
a5763f22
HMH
5438static struct ibm_struct led_driver_data = {
5439 .name = "led",
5440 .read = led_read,
5441 .write = led_write,
af116101 5442 .exit = led_exit,
a5763f22
HMH
5443};
5444
56b6aeb0
HMH
5445/*************************************************************************
5446 * Beep subdriver
5447 */
5448
e0c7dfe7 5449TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
56b6aeb0 5450
60201732
HMH
5451#define TPACPI_BEEP_Q1 0x0001
5452
5453static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5454 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5455 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5456};
5457
a5763f22 5458static int __init beep_init(struct ibm_init_struct *iibm)
5fba344c 5459{
60201732
HMH
5460 unsigned long quirks;
5461
fe08bc4b
HMH
5462 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5463
e0c7dfe7 5464 TPACPI_ACPIHANDLE_INIT(beep);
5fba344c 5465
fe08bc4b
HMH
5466 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5467 str_supported(beep_handle != NULL));
5468
60201732
HMH
5469 quirks = tpacpi_check_quirks(beep_quirk_table,
5470 ARRAY_SIZE(beep_quirk_table));
5471
5472 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5473
5fba344c
HMH
5474 return (beep_handle)? 0 : 1;
5475}
5476
887965e6 5477static int beep_read(struct seq_file *m)
78f81cc4 5478{
78f81cc4 5479 if (!beep_handle)
887965e6 5480 seq_printf(m, "status:\t\tnot supported\n");
78f81cc4 5481 else {
887965e6
AD
5482 seq_printf(m, "status:\t\tsupported\n");
5483 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
78f81cc4
BD
5484 }
5485
887965e6 5486 return 0;
78f81cc4
BD
5487}
5488
5489static int beep_write(char *buf)
5490{
5491 char *cmd;
5492 int beep_cmd;
5493
5494 if (!beep_handle)
5495 return -ENODEV;
5496
5497 while ((cmd = next_cmd(&buf))) {
5498 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5499 beep_cmd >= 0 && beep_cmd <= 17) {
5500 /* beep_cmd set */
5501 } else
5502 return -EINVAL;
60201732
HMH
5503 if (tp_features.beep_needs_two_args) {
5504 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5505 beep_cmd, 0))
5506 return -EIO;
5507 } else {
5508 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5509 beep_cmd))
5510 return -EIO;
5511 }
78f81cc4
BD
5512 }
5513
5514 return 0;
5515}
5516
a5763f22
HMH
5517static struct ibm_struct beep_driver_data = {
5518 .name = "beep",
5519 .read = beep_read,
5520 .write = beep_write,
5521};
5522
56b6aeb0
HMH
5523/*************************************************************************
5524 * Thermal subdriver
5525 */
78f81cc4 5526
f74a27d4
HMH
5527enum thermal_access_mode {
5528 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5529 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5530 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5531 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5532 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5533};
5534
5535enum { /* TPACPI_THERMAL_TPEC_* */
5536 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5537 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5538 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
9ebd9e83
HMH
5539
5540 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
f74a27d4
HMH
5541};
5542
9ebd9e83 5543
f74a27d4
HMH
5544#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5545struct ibm_thermal_sensors_struct {
5546 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5547};
5548
a26f878a 5549static enum thermal_access_mode thermal_read_mode;
78f81cc4 5550
b21a15f6
HMH
5551/* idx is zero-based */
5552static int thermal_get_sensor(int idx, s32 *value)
5553{
5554 int t;
5555 s8 tmp;
5556 char tmpi[5];
5557
5558 t = TP_EC_THERMAL_TMP0;
5559
5560 switch (thermal_read_mode) {
5561#if TPACPI_MAX_THERMAL_SENSORS >= 16
5562 case TPACPI_THERMAL_TPEC_16:
5563 if (idx >= 8 && idx <= 15) {
5564 t = TP_EC_THERMAL_TMP8;
5565 idx -= 8;
5566 }
5567 /* fallthrough */
5568#endif
5569 case TPACPI_THERMAL_TPEC_8:
5570 if (idx <= 7) {
5571 if (!acpi_ec_read(t + idx, &tmp))
5572 return -EIO;
5573 *value = tmp * 1000;
5574 return 0;
5575 }
5576 break;
5577
5578 case TPACPI_THERMAL_ACPI_UPDT:
5579 if (idx <= 7) {
5580 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5581 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5582 return -EIO;
5583 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5584 return -EIO;
5585 *value = (t - 2732) * 100;
5586 return 0;
5587 }
5588 break;
5589
5590 case TPACPI_THERMAL_ACPI_TMP07:
5591 if (idx <= 7) {
5592 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5593 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5594 return -EIO;
5595 if (t > 127 || t < -127)
5596 t = TP_EC_THERMAL_TMP_NA;
5597 *value = t * 1000;
5598 return 0;
5599 }
5600 break;
5601
5602 case TPACPI_THERMAL_NONE:
5603 default:
5604 return -ENOSYS;
5605 }
5606
5607 return -EINVAL;
5608}
5609
5610static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5611{
5612 int res, i;
5613 int n;
5614
5615 n = 8;
5616 i = 0;
5617
5618 if (!s)
5619 return -EINVAL;
5620
5621 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5622 n = 16;
5623
35ff8b9f 5624 for (i = 0 ; i < n; i++) {
b21a15f6
HMH
5625 res = thermal_get_sensor(i, &s->temp[i]);
5626 if (res)
5627 return res;
5628 }
5629
5630 return n;
5631}
f74a27d4 5632
9ebd9e83
HMH
5633static void thermal_dump_all_sensors(void)
5634{
5635 int n, i;
5636 struct ibm_thermal_sensors_struct t;
5637
5638 n = thermal_get_sensors(&t);
5639 if (n <= 0)
5640 return;
5641
5642 printk(TPACPI_NOTICE
5643 "temperatures (Celsius):");
5644
5645 for (i = 0; i < n; i++) {
5646 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5647 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5648 else
5649 printk(KERN_CONT " N/A");
5650 }
5651
5652 printk(KERN_CONT "\n");
5653}
5654
2c37aa4e
HMH
5655/* sysfs temp##_input -------------------------------------------------- */
5656
5657static ssize_t thermal_temp_input_show(struct device *dev,
5658 struct device_attribute *attr,
5659 char *buf)
5660{
5661 struct sensor_device_attribute *sensor_attr =
5662 to_sensor_dev_attr(attr);
5663 int idx = sensor_attr->index;
5664 s32 value;
5665 int res;
5666
5667 res = thermal_get_sensor(idx, &value);
5668 if (res)
5669 return res;
9ebd9e83 5670 if (value == TPACPI_THERMAL_SENSOR_NA)
2c37aa4e
HMH
5671 return -ENXIO;
5672
5673 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5674}
5675
5676#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
35ff8b9f
HMH
5677 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5678 thermal_temp_input_show, NULL, _idxB)
2c37aa4e
HMH
5679
5680static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5681 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5682 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5683 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5684 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5685 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5686 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5687 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5688 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5689 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5690 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5691 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5692 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5693 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5694 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5695 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5696 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5697};
5698
5699#define THERMAL_ATTRS(X) \
5700 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5701
5702static struct attribute *thermal_temp_input_attr[] = {
5703 THERMAL_ATTRS(8),
5704 THERMAL_ATTRS(9),
5705 THERMAL_ATTRS(10),
5706 THERMAL_ATTRS(11),
5707 THERMAL_ATTRS(12),
5708 THERMAL_ATTRS(13),
5709 THERMAL_ATTRS(14),
5710 THERMAL_ATTRS(15),
5711 THERMAL_ATTRS(0),
5712 THERMAL_ATTRS(1),
5713 THERMAL_ATTRS(2),
5714 THERMAL_ATTRS(3),
5715 THERMAL_ATTRS(4),
5716 THERMAL_ATTRS(5),
5717 THERMAL_ATTRS(6),
5718 THERMAL_ATTRS(7),
5719 NULL
5720};
5721
5722static const struct attribute_group thermal_temp_input16_group = {
5723 .attrs = thermal_temp_input_attr
5724};
5725
5726static const struct attribute_group thermal_temp_input8_group = {
5727 .attrs = &thermal_temp_input_attr[8]
5728};
5729
5730#undef THERMAL_SENSOR_ATTR_TEMP
5731#undef THERMAL_ATTRS
5732
5733/* --------------------------------------------------------------------- */
5734
a5763f22 5735static int __init thermal_init(struct ibm_init_struct *iibm)
78f81cc4 5736{
60eb0b35
HMH
5737 u8 t, ta1, ta2;
5738 int i;
5fba344c 5739 int acpi_tmp7;
2c37aa4e 5740 int res;
5fba344c 5741
fe08bc4b
HMH
5742 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5743
5fba344c 5744 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
60eb0b35 5745
3d6f99ca 5746 if (thinkpad_id.ec_model) {
60eb0b35
HMH
5747 /*
5748 * Direct EC access mode: sensors at registers
5749 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5750 * non-implemented, thermal sensors return 0x80 when
5751 * not available
5752 */
78f81cc4 5753
60eb0b35
HMH
5754 ta1 = ta2 = 0;
5755 for (i = 0; i < 8; i++) {
04cc862c 5756 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
60eb0b35
HMH
5757 ta1 |= t;
5758 } else {
5759 ta1 = 0;
5760 break;
5761 }
04cc862c 5762 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
60eb0b35
HMH
5763 ta2 |= t;
5764 } else {
5765 ta1 = 0;
5766 break;
5767 }
5768 }
5769 if (ta1 == 0) {
5770 /* This is sheer paranoia, but we handle it anyway */
5771 if (acpi_tmp7) {
e0c7dfe7 5772 printk(TPACPI_ERR
60eb0b35 5773 "ThinkPad ACPI EC access misbehaving, "
35ff8b9f
HMH
5774 "falling back to ACPI TMPx access "
5775 "mode\n");
efa27145 5776 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
60eb0b35 5777 } else {
e0c7dfe7 5778 printk(TPACPI_ERR
60eb0b35
HMH
5779 "ThinkPad ACPI EC access misbehaving, "
5780 "disabling thermal sensors access\n");
efa27145 5781 thermal_read_mode = TPACPI_THERMAL_NONE;
60eb0b35
HMH
5782 }
5783 } else {
5784 thermal_read_mode =
5785 (ta2 != 0) ?
efa27145 5786 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
60eb0b35
HMH
5787 }
5788 } else if (acpi_tmp7) {
e28393c0
HMH
5789 if (tpacpi_is_ibm() &&
5790 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
a26f878a 5791 /* 600e/x, 770e, 770x */
efa27145 5792 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
a26f878a 5793 } else {
e28393c0 5794 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
efa27145 5795 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
a26f878a
HMH
5796 }
5797 } else {
5798 /* temperatures not supported on 570, G4x, R30, R31, R32 */
efa27145 5799 thermal_read_mode = TPACPI_THERMAL_NONE;
a26f878a 5800 }
78f81cc4 5801
fe08bc4b
HMH
5802 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5803 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5804 thermal_read_mode);
5805
35ff8b9f 5806 switch (thermal_read_mode) {
2c37aa4e 5807 case TPACPI_THERMAL_TPEC_16:
7fd40029 5808 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
2c37aa4e
HMH
5809 &thermal_temp_input16_group);
5810 if (res)
5811 return res;
5812 break;
5813 case TPACPI_THERMAL_TPEC_8:
5814 case TPACPI_THERMAL_ACPI_TMP07:
5815 case TPACPI_THERMAL_ACPI_UPDT:
7fd40029 5816 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
2c37aa4e
HMH
5817 &thermal_temp_input8_group);
5818 if (res)
5819 return res;
5820 break;
5821 case TPACPI_THERMAL_NONE:
5822 default:
5823 return 1;
5824 }
5825
5826 return 0;
5827}
5828
5829static void thermal_exit(void)
5830{
35ff8b9f 5831 switch (thermal_read_mode) {
2c37aa4e 5832 case TPACPI_THERMAL_TPEC_16:
7fd40029 5833 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
2c37aa4e
HMH
5834 &thermal_temp_input16_group);
5835 break;
5836 case TPACPI_THERMAL_TPEC_8:
5837 case TPACPI_THERMAL_ACPI_TMP07:
5838 case TPACPI_THERMAL_ACPI_UPDT:
7fd40029 5839 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
f04d5e01 5840 &thermal_temp_input8_group);
2c37aa4e
HMH
5841 break;
5842 case TPACPI_THERMAL_NONE:
5843 default:
5844 break;
5845 }
78f81cc4
BD
5846}
5847
887965e6 5848static int thermal_read(struct seq_file *m)
a26f878a 5849{
a26f878a
HMH
5850 int n, i;
5851 struct ibm_thermal_sensors_struct t;
5852
5853 n = thermal_get_sensors(&t);
5854 if (unlikely(n < 0))
5855 return n;
5856
887965e6 5857 seq_printf(m, "temperatures:\t");
a26f878a
HMH
5858
5859 if (n > 0) {
5860 for (i = 0; i < (n - 1); i++)
887965e6
AD
5861 seq_printf(m, "%d ", t.temp[i] / 1000);
5862 seq_printf(m, "%d\n", t.temp[i] / 1000);
a26f878a 5863 } else
887965e6 5864 seq_printf(m, "not supported\n");
78f81cc4 5865
887965e6 5866 return 0;
78f81cc4
BD
5867}
5868
a5763f22
HMH
5869static struct ibm_struct thermal_driver_data = {
5870 .name = "thermal",
5871 .read = thermal_read,
2c37aa4e 5872 .exit = thermal_exit,
a5763f22
HMH
5873};
5874
56b6aeb0
HMH
5875/*************************************************************************
5876 * Backlight/brightness subdriver
5877 */
5878
f74a27d4
HMH
5879#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5880
0e501834
HMH
5881/*
5882 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5883 * CMOS NVRAM byte 0x5E, bits 0-3.
5884 *
5885 * EC HBRV (0x31) has the following layout
5886 * Bit 7: unknown function
5887 * Bit 6: unknown function
5888 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5889 * Bit 4: must be set to zero to avoid problems
5890 * Bit 3-0: backlight brightness level
5891 *
5892 * brightness_get_raw returns status data in the HBRV layout
d7880f10
HMH
5893 *
5894 * WARNING: The X61 has been verified to use HBRV for something else, so
5895 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5896 * testing on the very early *60 Lenovo models...
0e501834
HMH
5897 */
5898
e11aecf1
HMH
5899enum {
5900 TP_EC_BACKLIGHT = 0x31,
5901
5902 /* TP_EC_BACKLIGHT bitmasks */
5903 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5904 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5905 TP_EC_BACKLIGHT_MAPSW = 0x20,
5906};
5907
0e501834
HMH
5908enum tpacpi_brightness_access_mode {
5909 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5910 TPACPI_BRGHT_MODE_EC, /* EC control */
5911 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5912 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5913 TPACPI_BRGHT_MODE_MAX
5914};
5915
94954cc6 5916static struct backlight_device *ibm_backlight_device;
0e501834
HMH
5917
5918static enum tpacpi_brightness_access_mode brightness_mode =
5919 TPACPI_BRGHT_MODE_MAX;
5920
f74a27d4
HMH
5921static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5922
b21a15f6 5923static struct mutex brightness_mutex;
56b6aeb0 5924
0e501834
HMH
5925/* NVRAM brightness access,
5926 * call with brightness_mutex held! */
5927static unsigned int tpacpi_brightness_nvram_get(void)
b21a15f6 5928{
0e501834 5929 u8 lnvram;
b21a15f6 5930
0e501834
HMH
5931 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5932 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5933 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
77775838 5934 lnvram &= bright_maxlvl;
0e501834
HMH
5935
5936 return lnvram;
5937}
5938
5939static void tpacpi_brightness_checkpoint_nvram(void)
5940{
5941 u8 lec = 0;
5942 u8 b_nvram;
5943
5944 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5945 return;
5946
5947 vdbg_printk(TPACPI_DBG_BRGHT,
5948 "trying to checkpoint backlight level to NVRAM...\n");
5949
5950 if (mutex_lock_killable(&brightness_mutex) < 0)
5951 return;
5952
5953 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5954 goto unlock;
5955 lec &= TP_EC_BACKLIGHT_LVLMSK;
5956 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5957
5958 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5959 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5960 /* NVRAM needs update */
5961 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5962 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5963 b_nvram |= lec;
5964 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5965 dbg_printk(TPACPI_DBG_BRGHT,
5966 "updated NVRAM backlight level to %u (0x%02x)\n",
5967 (unsigned int) lec, (unsigned int) b_nvram);
5968 } else
5969 vdbg_printk(TPACPI_DBG_BRGHT,
5970 "NVRAM backlight level already is %u (0x%02x)\n",
5971 (unsigned int) lec, (unsigned int) b_nvram);
5972
5973unlock:
5974 mutex_unlock(&brightness_mutex);
5975}
5976
5977
5978/* call with brightness_mutex held! */
5979static int tpacpi_brightness_get_raw(int *status)
5980{
5981 u8 lec = 0;
5982
5983 switch (brightness_mode) {
5984 case TPACPI_BRGHT_MODE_UCMS_STEP:
5985 *status = tpacpi_brightness_nvram_get();
5986 return 0;
5987 case TPACPI_BRGHT_MODE_EC:
5988 case TPACPI_BRGHT_MODE_ECNVRAM:
5989 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
2d5e94d7 5990 return -EIO;
0e501834
HMH
5991 *status = lec;
5992 return 0;
5993 default:
5994 return -ENXIO;
b21a15f6 5995 }
0e501834
HMH
5996}
5997
5998/* call with brightness_mutex held! */
5999/* do NOT call with illegal backlight level value */
6000static int tpacpi_brightness_set_ec(unsigned int value)
6001{
6002 u8 lec = 0;
6003
6004 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6005 return -EIO;
6006
6007 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6008 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6009 (value & TP_EC_BACKLIGHT_LVLMSK))))
6010 return -EIO;
6011
6012 return 0;
6013}
6014
6015/* call with brightness_mutex held! */
6016static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6017{
6018 int cmos_cmd, inc;
6019 unsigned int current_value, i;
6020
6021 current_value = tpacpi_brightness_nvram_get();
6022
6023 if (value == current_value)
6024 return 0;
6025
6026 cmos_cmd = (value > current_value) ?
6027 TP_CMOS_BRIGHTNESS_UP :
6028 TP_CMOS_BRIGHTNESS_DOWN;
6029 inc = (value > current_value) ? 1 : -1;
6030
6031 for (i = current_value; i != value; i += inc)
6032 if (issue_thinkpad_cmos_command(cmos_cmd))
6033 return -EIO;
b21a15f6 6034
e11aecf1 6035 return 0;
b21a15f6
HMH
6036}
6037
6038/* May return EINTR which can always be mapped to ERESTARTSYS */
0e501834 6039static int brightness_set(unsigned int value)
b21a15f6 6040{
0e501834 6041 int res;
b21a15f6 6042
77775838 6043 if (value > bright_maxlvl || value < 0)
b21a15f6
HMH
6044 return -EINVAL;
6045
0e501834
HMH
6046 vdbg_printk(TPACPI_DBG_BRGHT,
6047 "set backlight level to %d\n", value);
6048
7646ea88 6049 res = mutex_lock_killable(&brightness_mutex);
b21a15f6
HMH
6050 if (res < 0)
6051 return res;
6052
0e501834
HMH
6053 switch (brightness_mode) {
6054 case TPACPI_BRGHT_MODE_EC:
6055 case TPACPI_BRGHT_MODE_ECNVRAM:
6056 res = tpacpi_brightness_set_ec(value);
6057 break;
6058 case TPACPI_BRGHT_MODE_UCMS_STEP:
6059 res = tpacpi_brightness_set_ucmsstep(value);
6060 break;
6061 default:
6062 res = -ENXIO;
b21a15f6
HMH
6063 }
6064
b21a15f6
HMH
6065 mutex_unlock(&brightness_mutex);
6066 return res;
6067}
6068
6069/* sysfs backlight class ----------------------------------------------- */
6070
6071static int brightness_update_status(struct backlight_device *bd)
6072{
0e501834 6073 unsigned int level =
b21a15f6
HMH
6074 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6075 bd->props.power == FB_BLANK_UNBLANK) ?
0e501834
HMH
6076 bd->props.brightness : 0;
6077
6078 dbg_printk(TPACPI_DBG_BRGHT,
6079 "backlight: attempt to set level to %d\n",
6080 level);
6081
6082 /* it is the backlight class's job (caller) to handle
6083 * EINTR and other errors properly */
6084 return brightness_set(level);
b21a15f6
HMH
6085}
6086
e11aecf1 6087static int brightness_get(struct backlight_device *bd)
a3f104c0 6088{
e11aecf1 6089 int status, res;
a3f104c0 6090
0e501834 6091 res = mutex_lock_killable(&brightness_mutex);
e11aecf1 6092 if (res < 0)
0e501834
HMH
6093 return 0;
6094
6095 res = tpacpi_brightness_get_raw(&status);
6096
6097 mutex_unlock(&brightness_mutex);
6098
6099 if (res < 0)
6100 return 0;
e11e211a 6101
e11aecf1 6102 return status & TP_EC_BACKLIGHT_LVLMSK;
e11e211a
HMH
6103}
6104
347a2686
HMH
6105static void tpacpi_brightness_notify_change(void)
6106{
6107 backlight_force_update(ibm_backlight_device,
6108 BACKLIGHT_UPDATE_HOTKEY);
6109}
6110
b21a15f6 6111static struct backlight_ops ibm_backlight_data = {
35ff8b9f
HMH
6112 .get_brightness = brightness_get,
6113 .update_status = brightness_update_status,
56b6aeb0 6114};
e11e211a 6115
b21a15f6 6116/* --------------------------------------------------------------------- */
e11e211a 6117
122f2672
HMH
6118/*
6119 * Call _BCL method of video device. On some ThinkPads this will
6120 * switch the firmware to the ACPI brightness control mode.
6121 */
6122
77775838
HMH
6123static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6124{
6125 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6126 union acpi_object *obj;
6127 int rc;
6128
122f2672 6129 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
77775838
HMH
6130 obj = (union acpi_object *)buffer.pointer;
6131 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6132 printk(TPACPI_ERR "Unknown _BCL data, "
6133 "please report this to %s\n", TPACPI_MAIL);
6134 rc = 0;
6135 } else {
6136 rc = obj->package.count;
6137 }
6138 } else {
6139 return 0;
6140 }
6141
6142 kfree(buffer.pointer);
6143 return rc;
6144}
6145
77775838
HMH
6146
6147/*
6148 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6149 */
6150static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6151{
122f2672 6152 acpi_handle video_device;
77775838 6153 int bcl_levels = 0;
77775838 6154
122f2672
HMH
6155 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6156 if (video_device)
6157 bcl_levels = tpacpi_query_bcl_levels(video_device);
77775838 6158
122f2672 6159 tp_features.bright_acpimode = (bcl_levels > 0);
77775838 6160
122f2672 6161 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
77775838
HMH
6162}
6163
59fe4fe3
HMH
6164/*
6165 * These are only useful for models that have only one possibility
6166 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6167 * these quirks.
6168 */
6169#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6170#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6171#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6172
6173static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6174 /* Models with ATI GPUs known to require ECNVRAM mode */
6175 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6176
6da25bf5 6177 /* Models with ATI GPUs that can use ECNVRAM */
7d1894d8 6178 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
59fe4fe3 6179 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
7d1894d8 6180 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
59fe4fe3
HMH
6181 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6182
6da25bf5 6183 /* Models with Intel Extreme Graphics 2 */
7d1894d8 6184 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
a9f8eacc
HMH
6185 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6186 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
59fe4fe3
HMH
6187
6188 /* Models with Intel GMA900 */
6189 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6190 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6191 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6192};
6193
77775838
HMH
6194/*
6195 * Returns < 0 for error, otherwise sets tp_features.bright_*
6196 * and bright_maxlvl.
6197 */
6198static void __init tpacpi_detect_brightness_capabilities(void)
6199{
6200 unsigned int b;
6201
6202 vdbg_printk(TPACPI_DBG_INIT,
6203 "detecting firmware brightness interface capabilities\n");
6204
6205 /* we could run a quirks check here (same table used by
6206 * brightness_init) if needed */
6207
6208 /*
6209 * We always attempt to detect acpi support, so as to switch
6210 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6211 * going to publish a backlight interface
6212 */
6213 b = tpacpi_check_std_acpi_brightness_support();
6214 switch (b) {
6215 case 16:
6216 bright_maxlvl = 15;
6217 printk(TPACPI_INFO
6218 "detected a 16-level brightness capable ThinkPad\n");
6219 break;
6220 case 8:
6221 case 0:
6222 bright_maxlvl = 7;
6223 printk(TPACPI_INFO
6224 "detected a 8-level brightness capable ThinkPad\n");
6225 break;
6226 default:
6227 printk(TPACPI_ERR
6228 "Unsupported brightness interface, "
6229 "please contact %s\n", TPACPI_MAIL);
6230 tp_features.bright_unkfw = 1;
6231 bright_maxlvl = b - 1;
6232 }
6233}
6234
a5763f22 6235static int __init brightness_init(struct ibm_init_struct *iibm)
56b6aeb0 6236{
a19a6ee6 6237 struct backlight_properties props;
56b6aeb0 6238 int b;
59fe4fe3 6239 unsigned long quirks;
56b6aeb0 6240
fe08bc4b
HMH
6241 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6242
f432255e
HMH
6243 mutex_init(&brightness_mutex);
6244
59fe4fe3
HMH
6245 quirks = tpacpi_check_quirks(brightness_quirk_table,
6246 ARRAY_SIZE(brightness_quirk_table));
6247
77775838
HMH
6248 /* tpacpi_detect_brightness_capabilities() must have run already */
6249
6250 /* if it is unknown, we don't handle it: it wouldn't be safe */
6251 if (tp_features.bright_unkfw)
6252 return 1;
2dba1b5d 6253
b5972796 6254 if (!brightness_enable) {
0e501834 6255 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
b5972796
HMH
6256 "brightness support disabled by "
6257 "module parameter\n");
6258 return 1;
6259 }
6260
217f0963
HMH
6261 if (acpi_video_backlight_support()) {
6262 if (brightness_enable > 1) {
6263 printk(TPACPI_INFO
6264 "Standard ACPI backlight interface "
6265 "available, not loading native one.\n");
6266 return 1;
6267 } else if (brightness_enable == 1) {
6268 printk(TPACPI_WARN
6269 "Cannot enable backlight brightness support, "
6270 "ACPI is already handling it. Refer to the "
6271 "acpi_backlight kernel parameter\n");
6272 return 1;
6273 }
6274 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6275 printk(TPACPI_NOTICE
6276 "Standard ACPI backlight interface not "
6277 "available, thinkpad_acpi native "
6278 "brightness control enabled\n");
6279 }
6280
0e501834
HMH
6281 /*
6282 * Check for module parameter bogosity, note that we
6283 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6284 * able to detect "unspecified"
6285 */
6286 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6287 return -EINVAL;
24d3b774 6288
0e501834
HMH
6289 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6290 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6291 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
59fe4fe3
HMH
6292 if (quirks & TPACPI_BRGHT_Q_EC)
6293 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6294 else
0e501834 6295 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
24d3b774 6296
0e501834 6297 dbg_printk(TPACPI_DBG_BRGHT,
59fe4fe3 6298 "driver auto-selected brightness_mode=%d\n",
0e501834
HMH
6299 brightness_mode);
6300 }
24d3b774 6301
d7880f10 6302 /* Safety */
e28393c0 6303 if (!tpacpi_is_ibm() &&
d7880f10
HMH
6304 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6305 brightness_mode == TPACPI_BRGHT_MODE_EC))
6306 return -EINVAL;
6307
0e501834 6308 if (tpacpi_brightness_get_raw(&b) < 0)
24d3b774 6309 return 1;
56b6aeb0 6310
a19a6ee6 6311 memset(&props, 0, sizeof(struct backlight_properties));
77775838
HMH
6312 props.max_brightness = bright_maxlvl;
6313 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
a19a6ee6
MG
6314 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6315 NULL, NULL,
6316 &ibm_backlight_data,
6317 &props);
56b6aeb0 6318 if (IS_ERR(ibm_backlight_device)) {
435c47e2
HMH
6319 int rc = PTR_ERR(ibm_backlight_device);
6320 ibm_backlight_device = NULL;
e0c7dfe7 6321 printk(TPACPI_ERR "Could not register backlight device\n");
435c47e2 6322 return rc;
56b6aeb0 6323 }
0e501834
HMH
6324 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6325 "brightness is supported\n");
56b6aeb0 6326
59fe4fe3
HMH
6327 if (quirks & TPACPI_BRGHT_Q_ASK) {
6328 printk(TPACPI_NOTICE
6329 "brightness: will use unverified default: "
6330 "brightness_mode=%d\n", brightness_mode);
6331 printk(TPACPI_NOTICE
6332 "brightness: please report to %s whether it works well "
6333 "or not on your ThinkPad\n", TPACPI_MAIL);
6334 }
6335
7d9745cf
HMH
6336 /* Added by mistake in early 2007. Probably useless, but it could
6337 * be working around some unknown firmware problem where the value
6338 * read at startup doesn't match the real hardware state... so leave
6339 * it in place just in case */
56b6aeb0
HMH
6340 backlight_update_status(ibm_backlight_device);
6341
347a2686
HMH
6342 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6343 "brightness: registering brightness hotkeys "
6344 "as change notification\n");
6345 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6346 | TP_ACPI_HKEY_BRGHTUP_MASK
6347 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
56b6aeb0
HMH
6348 return 0;
6349}
6350
0e501834
HMH
6351static void brightness_suspend(pm_message_t state)
6352{
6353 tpacpi_brightness_checkpoint_nvram();
6354}
6355
6356static void brightness_shutdown(void)
6357{
6358 tpacpi_brightness_checkpoint_nvram();
6359}
6360
56b6aeb0
HMH
6361static void brightness_exit(void)
6362{
6363 if (ibm_backlight_device) {
0e501834 6364 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
fe08bc4b 6365 "calling backlight_device_unregister()\n");
56b6aeb0 6366 backlight_device_unregister(ibm_backlight_device);
56b6aeb0 6367 }
0e501834
HMH
6368
6369 tpacpi_brightness_checkpoint_nvram();
56b6aeb0
HMH
6370}
6371
887965e6 6372static int brightness_read(struct seq_file *m)
56b6aeb0 6373{
56b6aeb0
HMH
6374 int level;
6375
35ff8b9f
HMH
6376 level = brightness_get(NULL);
6377 if (level < 0) {
887965e6 6378 seq_printf(m, "level:\t\tunreadable\n");
56b6aeb0 6379 } else {
887965e6
AD
6380 seq_printf(m, "level:\t\t%d\n", level);
6381 seq_printf(m, "commands:\tup, down\n");
77775838
HMH
6382 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6383 bright_maxlvl);
56b6aeb0
HMH
6384 }
6385
887965e6 6386 return 0;
56b6aeb0
HMH
6387}
6388
8acb0250
HM
6389static int brightness_write(char *buf)
6390{
6391 int level;
4273af8d 6392 int rc;
1da177e4 6393 char *cmd;
1da177e4 6394
4273af8d
HMH
6395 level = brightness_get(NULL);
6396 if (level < 0)
6397 return level;
78f81cc4 6398
4273af8d 6399 while ((cmd = next_cmd(&buf))) {
78f81cc4 6400 if (strlencmp(cmd, "up") == 0) {
77775838 6401 if (level < bright_maxlvl)
4273af8d 6402 level++;
78f81cc4 6403 } else if (strlencmp(cmd, "down") == 0) {
4273af8d
HMH
6404 if (level > 0)
6405 level--;
6406 } else if (sscanf(cmd, "level %d", &level) == 1 &&
77775838 6407 level >= 0 && level <= bright_maxlvl) {
4273af8d 6408 /* new level set */
78f81cc4
BD
6409 } else
6410 return -EINVAL;
78f81cc4
BD
6411 }
6412
0e501834
HMH
6413 tpacpi_disclose_usertask("procfs brightness",
6414 "set level to %d\n", level);
6415
4273af8d
HMH
6416 /*
6417 * Now we know what the final level should be, so we try to set it.
6418 * Doing it this way makes the syscall restartable in case of EINTR
6419 */
6420 rc = brightness_set(level);
347a2686
HMH
6421 if (!rc && ibm_backlight_device)
6422 backlight_force_update(ibm_backlight_device,
6423 BACKLIGHT_UPDATE_SYSFS);
80a8d122 6424 return (rc == -EINTR)? -ERESTARTSYS : rc;
78f81cc4
BD
6425}
6426
a5763f22
HMH
6427static struct ibm_struct brightness_driver_data = {
6428 .name = "brightness",
6429 .read = brightness_read,
6430 .write = brightness_write,
6431 .exit = brightness_exit,
0e501834
HMH
6432 .suspend = brightness_suspend,
6433 .shutdown = brightness_shutdown,
a5763f22
HMH
6434};
6435
56b6aeb0
HMH
6436/*************************************************************************
6437 * Volume subdriver
6438 */
fb87a811 6439
329e4e18
HMH
6440/*
6441 * IBM ThinkPads have a simple volume controller with MUTE gating.
6442 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6443 *
6444 * Since the *61 series (and probably also the later *60 series), Lenovo
6445 * ThinkPads only implement the MUTE gate.
6446 *
6447 * EC register 0x30
6448 * Bit 6: MUTE (1 mutes sound)
6449 * Bit 3-0: Volume
6450 * Other bits should be zero as far as we know.
6451 *
6452 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6453 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6454 * such as bit 7 which is used to detect repeated presses of MUTE,
6455 * and we leave them unchanged.
6456 */
6457
ff850c33
HMH
6458#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6459
0d204c34
HMH
6460#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6461#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6462#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6463
ead510ce 6464static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
0d204c34
HMH
6465static char *alsa_id = "ThinkPadEC";
6466static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6467
6468struct tpacpi_alsa_data {
6469 struct snd_card *card;
6470 struct snd_ctl_elem_id *ctl_mute_id;
6471 struct snd_ctl_elem_id *ctl_vol_id;
6472};
6473
6474static struct snd_card *alsa_card;
6475
329e4e18
HMH
6476enum {
6477 TP_EC_AUDIO = 0x30,
6478
6479 /* TP_EC_AUDIO bits */
6480 TP_EC_AUDIO_MUTESW = 6,
6481
6482 /* TP_EC_AUDIO bitmasks */
6483 TP_EC_AUDIO_LVL_MSK = 0x0F,
6484 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6485
6486 /* Maximum volume */
6487 TP_EC_VOLUME_MAX = 14,
6488};
6489
6490enum tpacpi_volume_access_mode {
6491 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6492 TPACPI_VOL_MODE_EC, /* Pure EC control */
6493 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6494 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6495 TPACPI_VOL_MODE_MAX
6496};
6497
a112ceee
HMH
6498enum tpacpi_volume_capabilities {
6499 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6500 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6501 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6502 TPACPI_VOL_CAP_MAX
6503};
6504
329e4e18
HMH
6505static enum tpacpi_volume_access_mode volume_mode =
6506 TPACPI_VOL_MODE_MAX;
6507
a112ceee 6508static enum tpacpi_volume_capabilities volume_capabilities;
c7ac6291 6509static int volume_control_allowed;
f74a27d4 6510
329e4e18
HMH
6511/*
6512 * Used to syncronize writers to TP_EC_AUDIO and
6513 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6514 */
6515static struct mutex volume_mutex;
6516
6517static void tpacpi_volume_checkpoint_nvram(void)
78f81cc4 6518{
329e4e18
HMH
6519 u8 lec = 0;
6520 u8 b_nvram;
a112ceee 6521 u8 ec_mask;
329e4e18
HMH
6522
6523 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6524 return;
c7ac6291
HMH
6525 if (!volume_control_allowed)
6526 return;
329e4e18
HMH
6527
6528 vdbg_printk(TPACPI_DBG_MIXER,
6529 "trying to checkpoint mixer state to NVRAM...\n");
78f81cc4 6530
a112ceee
HMH
6531 if (tp_features.mixer_no_level_control)
6532 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6533 else
6534 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6535
329e4e18
HMH
6536 if (mutex_lock_killable(&volume_mutex) < 0)
6537 return;
6538
6539 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6540 goto unlock;
6541 lec &= ec_mask;
6542 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6543
6544 if (lec != (b_nvram & ec_mask)) {
6545 /* NVRAM needs update */
6546 b_nvram &= ~ec_mask;
6547 b_nvram |= lec;
6548 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6549 dbg_printk(TPACPI_DBG_MIXER,
6550 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6551 (unsigned int) lec, (unsigned int) b_nvram);
78f81cc4 6552 } else {
329e4e18
HMH
6553 vdbg_printk(TPACPI_DBG_MIXER,
6554 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6555 (unsigned int) lec, (unsigned int) b_nvram);
78f81cc4
BD
6556 }
6557
329e4e18
HMH
6558unlock:
6559 mutex_unlock(&volume_mutex);
78f81cc4
BD
6560}
6561
329e4e18 6562static int volume_get_status_ec(u8 *status)
78f81cc4 6563{
329e4e18 6564 u8 s;
78f81cc4 6565
329e4e18
HMH
6566 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6567 return -EIO;
78f81cc4 6568
329e4e18 6569 *status = s;
1da177e4 6570
329e4e18 6571 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
35ff8b9f 6572
329e4e18
HMH
6573 return 0;
6574}
78f81cc4 6575
329e4e18
HMH
6576static int volume_get_status(u8 *status)
6577{
6578 return volume_get_status_ec(status);
6579}
78f81cc4 6580
329e4e18
HMH
6581static int volume_set_status_ec(const u8 status)
6582{
6583 if (!acpi_ec_write(TP_EC_AUDIO, status))
6584 return -EIO;
78f81cc4 6585
329e4e18
HMH
6586 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6587
6588 return 0;
6589}
6590
6591static int volume_set_status(const u8 status)
6592{
6593 return volume_set_status_ec(status);
6594}
6595
88cc8377
HMH
6596/* returns < 0 on error, 0 on no change, 1 on change */
6597static int __volume_set_mute_ec(const bool mute)
329e4e18
HMH
6598{
6599 int rc;
6600 u8 s, n;
6601
6602 if (mutex_lock_killable(&volume_mutex) < 0)
6603 return -EINTR;
6604
6605 rc = volume_get_status_ec(&s);
6606 if (rc)
6607 goto unlock;
6608
6609 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6610 s & ~TP_EC_AUDIO_MUTESW_MSK;
6611
88cc8377 6612 if (n != s) {
329e4e18 6613 rc = volume_set_status_ec(n);
88cc8377
HMH
6614 if (!rc)
6615 rc = 1;
6616 }
329e4e18
HMH
6617
6618unlock:
6619 mutex_unlock(&volume_mutex);
6620 return rc;
6621}
6622
88cc8377
HMH
6623static int volume_alsa_set_mute(const bool mute)
6624{
6625 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6626 (mute) ? "" : "un");
6627 return __volume_set_mute_ec(mute);
6628}
6629
329e4e18
HMH
6630static int volume_set_mute(const bool mute)
6631{
88cc8377
HMH
6632 int rc;
6633
329e4e18
HMH
6634 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6635 (mute) ? "" : "un");
88cc8377
HMH
6636
6637 rc = __volume_set_mute_ec(mute);
6638 return (rc < 0) ? rc : 0;
329e4e18
HMH
6639}
6640
88cc8377
HMH
6641/* returns < 0 on error, 0 on no change, 1 on change */
6642static int __volume_set_volume_ec(const u8 vol)
329e4e18
HMH
6643{
6644 int rc;
6645 u8 s, n;
6646
6647 if (vol > TP_EC_VOLUME_MAX)
6648 return -EINVAL;
6649
6650 if (mutex_lock_killable(&volume_mutex) < 0)
6651 return -EINTR;
6652
6653 rc = volume_get_status_ec(&s);
6654 if (rc)
6655 goto unlock;
6656
6657 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6658
88cc8377 6659 if (n != s) {
329e4e18 6660 rc = volume_set_status_ec(n);
88cc8377
HMH
6661 if (!rc)
6662 rc = 1;
6663 }
329e4e18
HMH
6664
6665unlock:
6666 mutex_unlock(&volume_mutex);
6667 return rc;
6668}
6669
88cc8377 6670static int volume_alsa_set_volume(const u8 vol)
329e4e18
HMH
6671{
6672 dbg_printk(TPACPI_DBG_MIXER,
88cc8377
HMH
6673 "ALSA: trying to set volume level to %hu\n", vol);
6674 return __volume_set_volume_ec(vol);
329e4e18
HMH
6675}
6676
0d204c34
HMH
6677static void volume_alsa_notify_change(void)
6678{
6679 struct tpacpi_alsa_data *d;
6680
6681 if (alsa_card && alsa_card->private_data) {
6682 d = alsa_card->private_data;
6683 if (d->ctl_mute_id)
6684 snd_ctl_notify(alsa_card,
6685 SNDRV_CTL_EVENT_MASK_VALUE,
6686 d->ctl_mute_id);
6687 if (d->ctl_vol_id)
6688 snd_ctl_notify(alsa_card,
6689 SNDRV_CTL_EVENT_MASK_VALUE,
6690 d->ctl_vol_id);
6691 }
6692}
6693
6694static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6695 struct snd_ctl_elem_info *uinfo)
6696{
6697 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6698 uinfo->count = 1;
6699 uinfo->value.integer.min = 0;
6700 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6701 return 0;
6702}
6703
6704static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6705 struct snd_ctl_elem_value *ucontrol)
6706{
6707 u8 s;
6708 int rc;
6709
6710 rc = volume_get_status(&s);
6711 if (rc < 0)
6712 return rc;
6713
6714 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6715 return 0;
6716}
6717
6718static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6719 struct snd_ctl_elem_value *ucontrol)
6720{
38e11cde
HMH
6721 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6722 ucontrol->value.integer.value[0]);
88cc8377 6723 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
0d204c34
HMH
6724}
6725
6726#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6727
6728static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6729 struct snd_ctl_elem_value *ucontrol)
6730{
6731 u8 s;
6732 int rc;
6733
6734 rc = volume_get_status(&s);
6735 if (rc < 0)
6736 return rc;
6737
6738 ucontrol->value.integer.value[0] =
6739 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6740 return 0;
6741}
6742
6743static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6744 struct snd_ctl_elem_value *ucontrol)
6745{
38e11cde
HMH
6746 tpacpi_disclose_usertask("ALSA", "%smute\n",
6747 ucontrol->value.integer.value[0] ?
6748 "un" : "");
88cc8377 6749 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
0d204c34
HMH
6750}
6751
6752static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6753 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6754 .name = "Console Playback Volume",
6755 .index = 0,
6756 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6757 .info = volume_alsa_vol_info,
6758 .get = volume_alsa_vol_get,
6759};
6760
6761static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6762 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6763 .name = "Console Playback Switch",
6764 .index = 0,
6765 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6766 .info = volume_alsa_mute_info,
6767 .get = volume_alsa_mute_get,
6768};
6769
329e4e18
HMH
6770static void volume_suspend(pm_message_t state)
6771{
6772 tpacpi_volume_checkpoint_nvram();
6773}
6774
0d204c34
HMH
6775static void volume_resume(void)
6776{
6777 volume_alsa_notify_change();
6778}
6779
329e4e18
HMH
6780static void volume_shutdown(void)
6781{
6782 tpacpi_volume_checkpoint_nvram();
6783}
6784
6785static void volume_exit(void)
6786{
0d204c34
HMH
6787 if (alsa_card) {
6788 snd_card_free(alsa_card);
6789 alsa_card = NULL;
6790 }
6791
329e4e18
HMH
6792 tpacpi_volume_checkpoint_nvram();
6793}
6794
0d204c34
HMH
6795static int __init volume_create_alsa_mixer(void)
6796{
6797 struct snd_card *card;
6798 struct tpacpi_alsa_data *data;
6799 struct snd_kcontrol *ctl_vol;
6800 struct snd_kcontrol *ctl_mute;
6801 int rc;
6802
6803 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6804 sizeof(struct tpacpi_alsa_data), &card);
74c75c18
HMH
6805 if (rc < 0 || !card) {
6806 printk(TPACPI_ERR
6807 "Failed to create ALSA card structures: %d\n", rc);
6808 return 1;
6809 }
0d204c34
HMH
6810
6811 BUG_ON(!card->private_data);
6812 data = card->private_data;
6813 data->card = card;
6814
6815 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6816 sizeof(card->driver));
6817 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6818 sizeof(card->shortname));
6819 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6820 (thinkpad_id.ec_version_str) ?
6821 thinkpad_id.ec_version_str : "(unknown)");
6822 snprintf(card->longname, sizeof(card->longname),
6823 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6824 (thinkpad_id.ec_version_str) ?
6825 thinkpad_id.ec_version_str : "unknown");
6826
6827 if (volume_control_allowed) {
6828 volume_alsa_control_vol.put = volume_alsa_vol_put;
6829 volume_alsa_control_vol.access =
6830 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6831
6832 volume_alsa_control_mute.put = volume_alsa_mute_put;
6833 volume_alsa_control_mute.access =
6834 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6835 }
6836
6837 if (!tp_features.mixer_no_level_control) {
6838 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6839 rc = snd_ctl_add(card, ctl_vol);
6840 if (rc < 0) {
6841 printk(TPACPI_ERR
74c75c18
HMH
6842 "Failed to create ALSA volume control: %d\n",
6843 rc);
6844 goto err_exit;
78f81cc4 6845 }
0d204c34
HMH
6846 data->ctl_vol_id = &ctl_vol->id;
6847 }
78f81cc4 6848
0d204c34
HMH
6849 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6850 rc = snd_ctl_add(card, ctl_mute);
6851 if (rc < 0) {
74c75c18
HMH
6852 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6853 rc);
6854 goto err_exit;
0d204c34
HMH
6855 }
6856 data->ctl_mute_id = &ctl_mute->id;
35ff8b9f 6857
0d204c34
HMH
6858 snd_card_set_dev(card, &tpacpi_pdev->dev);
6859 rc = snd_card_register(card);
0d204c34 6860 if (rc < 0) {
74c75c18
HMH
6861 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6862 goto err_exit;
0d204c34
HMH
6863 }
6864
6865 alsa_card = card;
74c75c18
HMH
6866 return 0;
6867
6868err_exit:
6869 snd_card_free(card);
6870 return 1;
0d204c34
HMH
6871}
6872
a112ceee
HMH
6873#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6874#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6875
6876static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6877 /* Whitelist volume level on all IBM by default */
6878 { .vendor = PCI_VENDOR_ID_IBM,
6879 .bios = TPACPI_MATCH_ANY,
6880 .ec = TPACPI_MATCH_ANY,
6881 .quirks = TPACPI_VOL_Q_LEVEL },
6882
6883 /* Lenovo models with volume control (needs confirmation) */
6884 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6885 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6886 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6887 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6888 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6889 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6890 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6891
6892 /* Whitelist mute-only on all Lenovo by default */
6893 { .vendor = PCI_VENDOR_ID_LENOVO,
6894 .bios = TPACPI_MATCH_ANY,
6895 .ec = TPACPI_MATCH_ANY,
6896 .quirks = TPACPI_VOL_Q_MUTEONLY }
6897};
6898
329e4e18
HMH
6899static int __init volume_init(struct ibm_init_struct *iibm)
6900{
a112ceee 6901 unsigned long quirks;
0d204c34 6902 int rc;
a112ceee 6903
329e4e18
HMH
6904 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6905
6906 mutex_init(&volume_mutex);
6907
6908 /*
6909 * Check for module parameter bogosity, note that we
6910 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6911 * able to detect "unspecified"
6912 */
6913 if (volume_mode > TPACPI_VOL_MODE_MAX)
6914 return -EINVAL;
6915
6916 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6917 printk(TPACPI_ERR
6918 "UCMS step volume mode not implemented, "
6919 "please contact %s\n", TPACPI_MAIL);
6920 return 1;
6921 }
6922
a112ceee
HMH
6923 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6924 return -EINVAL;
6925
0d204c34
HMH
6926 /*
6927 * The ALSA mixer is our primary interface.
6928 * When disabled, don't install the subdriver at all
6929 */
6930 if (!alsa_enable) {
6931 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6932 "ALSA mixer disabled by parameter, "
6933 "not loading volume subdriver...\n");
6934 return 1;
6935 }
6936
a112ceee
HMH
6937 quirks = tpacpi_check_quirks(volume_quirk_table,
6938 ARRAY_SIZE(volume_quirk_table));
6939
6940 switch (volume_capabilities) {
6941 case TPACPI_VOL_CAP_AUTO:
6942 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6943 tp_features.mixer_no_level_control = 1;
6944 else if (quirks & TPACPI_VOL_Q_LEVEL)
6945 tp_features.mixer_no_level_control = 0;
6946 else
6947 return 1; /* no mixer */
6948 break;
6949 case TPACPI_VOL_CAP_VOLMUTE:
6950 tp_features.mixer_no_level_control = 0;
6951 break;
6952 case TPACPI_VOL_CAP_MUTEONLY:
6953 tp_features.mixer_no_level_control = 1;
6954 break;
6955 default:
6956 return 1;
6957 }
6958
6959 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6960 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6961 "using user-supplied volume_capabilities=%d\n",
6962 volume_capabilities);
6963
329e4e18
HMH
6964 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6965 volume_mode == TPACPI_VOL_MODE_MAX) {
6966 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6967
6968 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6969 "driver auto-selected volume_mode=%d\n",
6970 volume_mode);
6971 } else {
6972 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6973 "using user-supplied volume_mode=%d\n",
6974 volume_mode);
6975 }
6976
6977 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
a112ceee
HMH
6978 "mute is supported, volume control is %s\n",
6979 str_supported(!tp_features.mixer_no_level_control));
329e4e18 6980
0d204c34
HMH
6981 rc = volume_create_alsa_mixer();
6982 if (rc) {
6983 printk(TPACPI_ERR
6984 "Could not create the ALSA mixer interface\n");
6985 return rc;
6986 }
6987
c7ac6291
HMH
6988 printk(TPACPI_INFO
6989 "Console audio control enabled, mode: %s\n",
6990 (volume_control_allowed) ?
6991 "override (read/write)" :
6992 "monitor (read only)");
6993
0d204c34
HMH
6994 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6995 "registering volume hotkeys as change notification\n");
6996 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6997 | TP_ACPI_HKEY_VOLUP_MASK
6998 | TP_ACPI_HKEY_VOLDWN_MASK
6999 | TP_ACPI_HKEY_MUTE_MASK);
7000
329e4e18
HMH
7001 return 0;
7002}
f74a27d4 7003
887965e6 7004static int volume_read(struct seq_file *m)
78f81cc4 7005{
329e4e18 7006 u8 status;
78f81cc4 7007
329e4e18 7008 if (volume_get_status(&status) < 0) {
887965e6 7009 seq_printf(m, "level:\t\tunreadable\n");
78f81cc4 7010 } else {
a112ceee 7011 if (tp_features.mixer_no_level_control)
887965e6 7012 seq_printf(m, "level:\t\tunsupported\n");
a112ceee 7013 else
887965e6 7014 seq_printf(m, "level:\t\t%d\n",
a112ceee
HMH
7015 status & TP_EC_AUDIO_LVL_MSK);
7016
887965e6 7017 seq_printf(m, "mute:\t\t%s\n",
329e4e18 7018 onoff(status, TP_EC_AUDIO_MUTESW));
a112ceee 7019
c7ac6291 7020 if (volume_control_allowed) {
887965e6 7021 seq_printf(m, "commands:\tunmute, mute\n");
c7ac6291 7022 if (!tp_features.mixer_no_level_control) {
887965e6 7023 seq_printf(m,
c7ac6291 7024 "commands:\tup, down\n");
887965e6 7025 seq_printf(m,
c7ac6291
HMH
7026 "commands:\tlevel <level>"
7027 " (<level> is 0-%d)\n",
7028 TP_EC_VOLUME_MAX);
7029 }
78f81cc4
BD
7030 }
7031 }
7032
7033 return 0;
7034}
7035
78f81cc4
BD
7036static int volume_write(char *buf)
7037{
329e4e18
HMH
7038 u8 s;
7039 u8 new_level, new_mute;
7040 int l;
78f81cc4 7041 char *cmd;
329e4e18 7042 int rc;
78f81cc4 7043
c7ac6291
HMH
7044 /*
7045 * We do allow volume control at driver startup, so that the
7046 * user can set initial state through the volume=... parameter hack.
7047 */
7048 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7049 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7050 tp_warned.volume_ctrl_forbidden = 1;
7051 printk(TPACPI_NOTICE
7052 "Console audio control in monitor mode, "
7053 "changes are not allowed.\n");
7054 printk(TPACPI_NOTICE
7055 "Use the volume_control=1 module parameter "
7056 "to enable volume control\n");
7057 }
7058 return -EPERM;
7059 }
7060
329e4e18
HMH
7061 rc = volume_get_status(&s);
7062 if (rc < 0)
7063 return rc;
78f81cc4 7064
329e4e18
HMH
7065 new_level = s & TP_EC_AUDIO_LVL_MSK;
7066 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7067
7068 while ((cmd = next_cmd(&buf))) {
a112ceee
HMH
7069 if (!tp_features.mixer_no_level_control) {
7070 if (strlencmp(cmd, "up") == 0) {
7071 if (new_mute)
7072 new_mute = 0;
7073 else if (new_level < TP_EC_VOLUME_MAX)
7074 new_level++;
7075 continue;
7076 } else if (strlencmp(cmd, "down") == 0) {
7077 if (new_mute)
7078 new_mute = 0;
7079 else if (new_level > 0)
7080 new_level--;
7081 continue;
7082 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7083 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7084 new_level = l;
7085 continue;
7086 }
7087 }
7088 if (strlencmp(cmd, "mute") == 0)
329e4e18 7089 new_mute = TP_EC_AUDIO_MUTESW_MSK;
a112ceee
HMH
7090 else if (strlencmp(cmd, "unmute") == 0)
7091 new_mute = 0;
7092 else
1da177e4 7093 return -EINVAL;
329e4e18 7094 }
1da177e4 7095
a112ceee
HMH
7096 if (tp_features.mixer_no_level_control) {
7097 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7098 new_mute ? "" : "un");
7099 rc = volume_set_mute(!!new_mute);
7100 } else {
7101 tpacpi_disclose_usertask("procfs volume",
7102 "%smute and set level to %d\n",
7103 new_mute ? "" : "un", new_level);
7104 rc = volume_set_status(new_mute | new_level);
7105 }
0d204c34 7106 volume_alsa_notify_change();
78f81cc4 7107
329e4e18 7108 return (rc == -EINTR) ? -ERESTARTSYS : rc;
78f81cc4
BD
7109}
7110
a5763f22
HMH
7111static struct ibm_struct volume_driver_data = {
7112 .name = "volume",
7113 .read = volume_read,
7114 .write = volume_write,
329e4e18
HMH
7115 .exit = volume_exit,
7116 .suspend = volume_suspend,
0d204c34 7117 .resume = volume_resume,
329e4e18 7118 .shutdown = volume_shutdown,
a5763f22 7119};
56b6aeb0 7120
ff850c33
HMH
7121#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7122
7123#define alsa_card NULL
7124
7125static void inline volume_alsa_notify_change(void)
7126{
7127}
7128
7129static int __init volume_init(struct ibm_init_struct *iibm)
7130{
7131 printk(TPACPI_INFO
7132 "volume: disabled as there is no ALSA support in this kernel\n");
7133
7134 return 1;
7135}
7136
7137static struct ibm_struct volume_driver_data = {
7138 .name = "volume",
7139};
7140
7141#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7142
56b6aeb0
HMH
7143/*************************************************************************
7144 * Fan subdriver
7145 */
7146
7147/*
7148 * FAN ACCESS MODES
7149 *
efa27145 7150 * TPACPI_FAN_RD_ACPI_GFAN:
56b6aeb0
HMH
7151 * ACPI GFAN method: returns fan level
7152 *
efa27145 7153 * see TPACPI_FAN_WR_ACPI_SFAN
f51d1a39 7154 * EC 0x2f (HFSP) not available if GFAN exists
56b6aeb0 7155 *
efa27145 7156 * TPACPI_FAN_WR_ACPI_SFAN:
56b6aeb0
HMH
7157 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7158 *
f51d1a39
HMH
7159 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7160 * it for writing.
56b6aeb0 7161 *
efa27145 7162 * TPACPI_FAN_WR_TPEC:
f51d1a39
HMH
7163 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7164 * Supported on almost all ThinkPads
56b6aeb0
HMH
7165 *
7166 * Fan speed changes of any sort (including those caused by the
7167 * disengaged mode) are usually done slowly by the firmware as the
9ddc5b6f 7168 * maximum amount of fan duty cycle change per second seems to be
56b6aeb0
HMH
7169 * limited.
7170 *
7171 * Reading is not available if GFAN exists.
7172 * Writing is not available if SFAN exists.
7173 *
7174 * Bits
7175 * 7 automatic mode engaged;
7176 * (default operation mode of the ThinkPad)
7177 * fan level is ignored in this mode.
f51d1a39 7178 * 6 full speed mode (takes precedence over bit 7);
56b6aeb0 7179 * not available on all thinkpads. May disable
f51d1a39
HMH
7180 * the tachometer while the fan controller ramps up
7181 * the speed (which can take up to a few *minutes*).
7182 * Speeds up fan to 100% duty-cycle, which is far above
7183 * the standard RPM levels. It is not impossible that
7184 * it could cause hardware damage.
56b6aeb0
HMH
7185 * 5-3 unused in some models. Extra bits for fan level
7186 * in others, but still useless as all values above
7187 * 7 map to the same speed as level 7 in these models.
7188 * 2-0 fan level (0..7 usually)
7189 * 0x00 = stop
7190 * 0x07 = max (set when temperatures critical)
7191 * Some ThinkPads may have other levels, see
efa27145 7192 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
56b6aeb0
HMH
7193 *
7194 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7195 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7196 * does so, its initial value is meaningless (0x07).
7197 *
7198 * For firmware bugs, refer to:
7199 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7200 *
7201 * ----
7202 *
7203 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7204 * Main fan tachometer reading (in RPM)
7205 *
7206 * This register is present on all ThinkPads with a new-style EC, and
7207 * it is known not to be present on the A21m/e, and T22, as there is
7208 * something else in offset 0x84 according to the ACPI DSDT. Other
7209 * ThinkPads from this same time period (and earlier) probably lack the
7210 * tachometer as well.
7211 *
877d0310 7212 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
56b6aeb0
HMH
7213 * was never fixed by IBM to report the EC firmware version string
7214 * probably support the tachometer (like the early X models), so
7215 * detecting it is quite hard. We need more data to know for sure.
7216 *
7217 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7218 * might result.
7219 *
f51d1a39
HMH
7220 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7221 * mode.
56b6aeb0
HMH
7222 *
7223 * For firmware bugs, refer to:
7224 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7225 *
d7377247
HMH
7226 * ----
7227 *
7228 * ThinkPad EC register 0x31 bit 0 (only on select models)
7229 *
7230 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7231 * show the speed of the main fan. When bit 0 of EC register 0x31
7232 * is one, the tachometer registers show the speed of the auxiliary
7233 * fan.
7234 *
7235 * Fan control seems to affect both fans, regardless of the state
7236 * of this bit.
7237 *
7238 * So far, only the firmware for the X60/X61 non-tablet versions
7239 * seem to support this (firmware TP-7M).
7240 *
efa27145 7241 * TPACPI_FAN_WR_ACPI_FANS:
56b6aeb0
HMH
7242 * ThinkPad X31, X40, X41. Not available in the X60.
7243 *
7244 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7245 * high speed. ACPI DSDT seems to map these three speeds to levels
7246 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7247 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7248 *
7249 * The speeds are stored on handles
7250 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7251 *
af901ca1 7252 * There are three default speed sets, accessible as handles:
56b6aeb0
HMH
7253 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7254 *
7255 * ACPI DSDT switches which set is in use depending on various
7256 * factors.
7257 *
efa27145 7258 * TPACPI_FAN_WR_TPEC is also available and should be used to
56b6aeb0
HMH
7259 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7260 * but the ACPI tables just mention level 7.
7261 */
7262
f74a27d4
HMH
7263enum { /* Fan control constants */
7264 fan_status_offset = 0x2f, /* EC register 0x2f */
7265 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7266 * 0x84 must be read before 0x85 */
d7377247
HMH
7267 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7268 bit 0 selects which fan is active */
f74a27d4
HMH
7269
7270 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7271 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7272
7273 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7274};
7275
7276enum fan_status_access_mode {
7277 TPACPI_FAN_NONE = 0, /* No fan status or control */
7278 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7279 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7280};
7281
7282enum fan_control_access_mode {
7283 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7284 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7285 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7286 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7287};
7288
7289enum fan_control_commands {
7290 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7291 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7292 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7293 * and also watchdog cmd */
7294};
7295
7296static int fan_control_allowed;
7297
69ba91cb
HMH
7298static enum fan_status_access_mode fan_status_access_mode;
7299static enum fan_control_access_mode fan_control_access_mode;
7300static enum fan_control_commands fan_control_commands;
78f81cc4 7301
778b4d74 7302static u8 fan_control_initial_status;
fe98a52c 7303static u8 fan_control_desired_level;
0081b162 7304static u8 fan_control_resume_level;
f74a27d4
HMH
7305static int fan_watchdog_maxinterval;
7306
7307static struct mutex fan_mutex;
778b4d74 7308
25c68a33 7309static void fan_watchdog_fire(struct work_struct *ignored);
25c68a33 7310static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
16663a87 7311
e0c7dfe7
HMH
7312TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7313TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
56b6aeb0
HMH
7314 "\\FSPD", /* 600e/x, 770e, 770x */
7315 ); /* all others */
e0c7dfe7 7316TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
56b6aeb0
HMH
7317 "JFNS", /* 770x-JL */
7318 ); /* all others */
7319
1c2ece75
HMH
7320/*
7321 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7322 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7323 * be in auto mode (0x80).
7324 *
7325 * This is corrected by any write to HFSP either by the driver, or
7326 * by the firmware.
7327 *
7328 * We assume 0x07 really means auto mode while this quirk is active,
7329 * as this is far more likely than the ThinkPad being in level 7,
7330 * which is only used by the firmware during thermal emergencies.
7d95a3d5
HMH
7331 *
7332 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7333 * TP-70 (T43, R52), which are known to be buggy.
1c2ece75
HMH
7334 */
7335
7d95a3d5 7336static void fan_quirk1_setup(void)
1c2ece75 7337{
1c2ece75 7338 if (fan_control_initial_status == 0x07) {
7d95a3d5
HMH
7339 printk(TPACPI_NOTICE
7340 "fan_init: initial fan status is unknown, "
7341 "assuming it is in auto mode\n");
7342 tp_features.fan_ctrl_status_undef = 1;
1c2ece75
HMH
7343 }
7344}
7345
7346static void fan_quirk1_handle(u8 *fan_status)
7347{
7348 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7349 if (*fan_status != fan_control_initial_status) {
7350 /* something changed the HFSP regisnter since
7351 * driver init time, so it is not undefined
7352 * anymore */
7353 tp_features.fan_ctrl_status_undef = 0;
7354 } else {
7355 /* Return most likely status. In fact, it
7356 * might be the only possible status */
7357 *fan_status = TP_EC_FAN_AUTO;
7358 }
7359 }
7360}
7361
d7377247
HMH
7362/* Select main fan on X60/X61, NOOP on others */
7363static bool fan_select_fan1(void)
7364{
7365 if (tp_features.second_fan) {
7366 u8 val;
7367
7368 if (ec_read(fan_select_offset, &val) < 0)
7369 return false;
7370 val &= 0xFEU;
7371 if (ec_write(fan_select_offset, val) < 0)
7372 return false;
7373 }
7374 return true;
7375}
7376
7377/* Select secondary fan on X60/X61 */
7378static bool fan_select_fan2(void)
7379{
7380 u8 val;
7381
7382 if (!tp_features.second_fan)
7383 return false;
7384
7385 if (ec_read(fan_select_offset, &val) < 0)
7386 return false;
7387 val |= 0x01U;
7388 if (ec_write(fan_select_offset, val) < 0)
7389 return false;
7390
7391 return true;
7392}
7393
fe98a52c 7394/*
b21a15f6 7395 * Call with fan_mutex held
fe98a52c 7396 */
b21a15f6 7397static void fan_update_desired_level(u8 status)
fe98a52c 7398{
b21a15f6
HMH
7399 if ((status &
7400 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7401 if (status > 7)
7402 fan_control_desired_level = 7;
7403 else
7404 fan_control_desired_level = status;
7405 }
7406}
fe98a52c 7407
b21a15f6
HMH
7408static int fan_get_status(u8 *status)
7409{
7410 u8 s;
fe98a52c 7411
b21a15f6
HMH
7412 /* TODO:
7413 * Add TPACPI_FAN_RD_ACPI_FANS ? */
fe98a52c 7414
b21a15f6
HMH
7415 switch (fan_status_access_mode) {
7416 case TPACPI_FAN_RD_ACPI_GFAN:
7417 /* 570, 600e/x, 770e, 770x */
fe98a52c 7418
b21a15f6
HMH
7419 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7420 return -EIO;
fe98a52c 7421
b21a15f6
HMH
7422 if (likely(status))
7423 *status = s & 0x07;
fe98a52c 7424
b21a15f6
HMH
7425 break;
7426
7427 case TPACPI_FAN_RD_TPEC:
7428 /* all except 570, 600e/x, 770e, 770x */
7429 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7430 return -EIO;
7431
1c2ece75 7432 if (likely(status)) {
b21a15f6 7433 *status = s;
1c2ece75
HMH
7434 fan_quirk1_handle(status);
7435 }
fe98a52c 7436
fe98a52c 7437 break;
b21a15f6 7438
fe98a52c 7439 default:
b21a15f6 7440 return -ENXIO;
fe98a52c
HMH
7441 }
7442
b21a15f6
HMH
7443 return 0;
7444}
fe98a52c 7445
b21a15f6
HMH
7446static int fan_get_status_safe(u8 *status)
7447{
7448 int rc;
7449 u8 s;
fe98a52c 7450
7646ea88 7451 if (mutex_lock_killable(&fan_mutex))
b21a15f6
HMH
7452 return -ERESTARTSYS;
7453 rc = fan_get_status(&s);
7454 if (!rc)
7455 fan_update_desired_level(s);
7456 mutex_unlock(&fan_mutex);
fe98a52c 7457
b21a15f6
HMH
7458 if (status)
7459 *status = s;
fe98a52c 7460
b21a15f6
HMH
7461 return rc;
7462}
7463
7464static int fan_get_speed(unsigned int *speed)
fe98a52c 7465{
b21a15f6 7466 u8 hi, lo;
fe98a52c 7467
b21a15f6
HMH
7468 switch (fan_status_access_mode) {
7469 case TPACPI_FAN_RD_TPEC:
7470 /* all except 570, 600e/x, 770e, 770x */
d7377247
HMH
7471 if (unlikely(!fan_select_fan1()))
7472 return -EIO;
b21a15f6
HMH
7473 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7474 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7475 return -EIO;
fe98a52c 7476
b21a15f6
HMH
7477 if (likely(speed))
7478 *speed = (hi << 8) | lo;
fe98a52c 7479
b21a15f6 7480 break;
fe98a52c 7481
b21a15f6
HMH
7482 default:
7483 return -ENXIO;
7484 }
fe98a52c 7485
b21a15f6 7486 return 0;
fe98a52c
HMH
7487}
7488
d7377247
HMH
7489static int fan2_get_speed(unsigned int *speed)
7490{
7491 u8 hi, lo;
7492 bool rc;
7493
7494 switch (fan_status_access_mode) {
7495 case TPACPI_FAN_RD_TPEC:
7496 /* all except 570, 600e/x, 770e, 770x */
7497 if (unlikely(!fan_select_fan2()))
7498 return -EIO;
7499 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7500 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7501 fan_select_fan1(); /* play it safe */
7502 if (rc)
7503 return -EIO;
7504
7505 if (likely(speed))
7506 *speed = (hi << 8) | lo;
7507
7508 break;
7509
7510 default:
7511 return -ENXIO;
7512 }
7513
7514 return 0;
7515}
7516
b21a15f6 7517static int fan_set_level(int level)
fe98a52c 7518{
b21a15f6
HMH
7519 if (!fan_control_allowed)
7520 return -EPERM;
fe98a52c 7521
b21a15f6
HMH
7522 switch (fan_control_access_mode) {
7523 case TPACPI_FAN_WR_ACPI_SFAN:
7524 if (level >= 0 && level <= 7) {
7525 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7526 return -EIO;
7527 } else
7528 return -EINVAL;
7529 break;
fe98a52c 7530
b21a15f6
HMH
7531 case TPACPI_FAN_WR_ACPI_FANS:
7532 case TPACPI_FAN_WR_TPEC:
0081b162
HMH
7533 if (!(level & TP_EC_FAN_AUTO) &&
7534 !(level & TP_EC_FAN_FULLSPEED) &&
b21a15f6
HMH
7535 ((level < 0) || (level > 7)))
7536 return -EINVAL;
fe98a52c 7537
b21a15f6
HMH
7538 /* safety net should the EC not support AUTO
7539 * or FULLSPEED mode bits and just ignore them */
7540 if (level & TP_EC_FAN_FULLSPEED)
7541 level |= 7; /* safety min speed 7 */
547266e4 7542 else if (level & TP_EC_FAN_AUTO)
b21a15f6 7543 level |= 4; /* safety min speed 4 */
fe98a52c 7544
b21a15f6
HMH
7545 if (!acpi_ec_write(fan_status_offset, level))
7546 return -EIO;
7547 else
7548 tp_features.fan_ctrl_status_undef = 0;
7549 break;
fe98a52c 7550
b21a15f6
HMH
7551 default:
7552 return -ENXIO;
7553 }
74a60c0f
HMH
7554
7555 vdbg_printk(TPACPI_DBG_FAN,
7556 "fan control: set fan control register to 0x%02x\n", level);
b21a15f6 7557 return 0;
fe98a52c
HMH
7558}
7559
b21a15f6 7560static int fan_set_level_safe(int level)
fe98a52c 7561{
b21a15f6 7562 int rc;
fe98a52c 7563
b21a15f6
HMH
7564 if (!fan_control_allowed)
7565 return -EPERM;
fe98a52c 7566
7646ea88 7567 if (mutex_lock_killable(&fan_mutex))
b21a15f6 7568 return -ERESTARTSYS;
fe98a52c 7569
b21a15f6
HMH
7570 if (level == TPACPI_FAN_LAST_LEVEL)
7571 level = fan_control_desired_level;
fe98a52c 7572
b21a15f6
HMH
7573 rc = fan_set_level(level);
7574 if (!rc)
7575 fan_update_desired_level(level);
7576
7577 mutex_unlock(&fan_mutex);
7578 return rc;
fe98a52c
HMH
7579}
7580
b21a15f6 7581static int fan_set_enable(void)
fe98a52c 7582{
b21a15f6
HMH
7583 u8 s;
7584 int rc;
fe98a52c 7585
ecf2a80a
HMH
7586 if (!fan_control_allowed)
7587 return -EPERM;
7588
7646ea88 7589 if (mutex_lock_killable(&fan_mutex))
b21a15f6 7590 return -ERESTARTSYS;
fe98a52c 7591
b21a15f6
HMH
7592 switch (fan_control_access_mode) {
7593 case TPACPI_FAN_WR_ACPI_FANS:
7594 case TPACPI_FAN_WR_TPEC:
7595 rc = fan_get_status(&s);
7596 if (rc < 0)
7597 break;
fe98a52c 7598
b21a15f6
HMH
7599 /* Don't go out of emergency fan mode */
7600 if (s != 7) {
7601 s &= 0x07;
7602 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7603 }
fe98a52c 7604
b21a15f6
HMH
7605 if (!acpi_ec_write(fan_status_offset, s))
7606 rc = -EIO;
7607 else {
7608 tp_features.fan_ctrl_status_undef = 0;
7609 rc = 0;
7610 }
7611 break;
fe98a52c 7612
b21a15f6
HMH
7613 case TPACPI_FAN_WR_ACPI_SFAN:
7614 rc = fan_get_status(&s);
7615 if (rc < 0)
7616 break;
fe98a52c 7617
b21a15f6 7618 s &= 0x07;
fe98a52c 7619
b21a15f6
HMH
7620 /* Set fan to at least level 4 */
7621 s |= 4;
fe08bc4b 7622
b21a15f6 7623 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
35ff8b9f 7624 rc = -EIO;
b21a15f6
HMH
7625 else
7626 rc = 0;
7627 break;
78f81cc4 7628
b21a15f6
HMH
7629 default:
7630 rc = -ENXIO;
7631 }
5fba344c 7632
b21a15f6 7633 mutex_unlock(&fan_mutex);
74a60c0f
HMH
7634
7635 if (!rc)
7636 vdbg_printk(TPACPI_DBG_FAN,
7637 "fan control: set fan control register to 0x%02x\n",
7638 s);
b21a15f6 7639 return rc;
69ba91cb 7640}
78f81cc4 7641
b21a15f6 7642static int fan_set_disable(void)
78f81cc4 7643{
b21a15f6 7644 int rc;
c52f0aa5 7645
b21a15f6
HMH
7646 if (!fan_control_allowed)
7647 return -EPERM;
78f81cc4 7648
7646ea88 7649 if (mutex_lock_killable(&fan_mutex))
b21a15f6 7650 return -ERESTARTSYS;
3ef8a609 7651
b21a15f6
HMH
7652 rc = 0;
7653 switch (fan_control_access_mode) {
7654 case TPACPI_FAN_WR_ACPI_FANS:
7655 case TPACPI_FAN_WR_TPEC:
7656 if (!acpi_ec_write(fan_status_offset, 0x00))
7657 rc = -EIO;
7658 else {
7659 fan_control_desired_level = 0;
7660 tp_features.fan_ctrl_status_undef = 0;
7661 }
3ef8a609
HMH
7662 break;
7663
b21a15f6
HMH
7664 case TPACPI_FAN_WR_ACPI_SFAN:
7665 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7666 rc = -EIO;
7667 else
7668 fan_control_desired_level = 0;
c52f0aa5
HMH
7669 break;
7670
7671 default:
b21a15f6 7672 rc = -ENXIO;
78f81cc4
BD
7673 }
7674
74a60c0f
HMH
7675 if (!rc)
7676 vdbg_printk(TPACPI_DBG_FAN,
7677 "fan control: set fan control register to 0\n");
fe98a52c 7678
fe98a52c 7679 mutex_unlock(&fan_mutex);
fe98a52c
HMH
7680 return rc;
7681}
7682
b21a15f6 7683static int fan_set_speed(int speed)
c52f0aa5 7684{
b21a15f6 7685 int rc;
c52f0aa5 7686
b21a15f6
HMH
7687 if (!fan_control_allowed)
7688 return -EPERM;
3ef8a609 7689
7646ea88 7690 if (mutex_lock_killable(&fan_mutex))
b21a15f6 7691 return -ERESTARTSYS;
c52f0aa5 7692
b21a15f6
HMH
7693 rc = 0;
7694 switch (fan_control_access_mode) {
7695 case TPACPI_FAN_WR_ACPI_FANS:
7696 if (speed >= 0 && speed <= 65535) {
7697 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7698 speed, speed, speed))
7699 rc = -EIO;
7700 } else
7701 rc = -EINVAL;
c52f0aa5
HMH
7702 break;
7703
7704 default:
b21a15f6 7705 rc = -ENXIO;
c52f0aa5
HMH
7706 }
7707
b21a15f6
HMH
7708 mutex_unlock(&fan_mutex);
7709 return rc;
16663a87
HMH
7710}
7711
7712static void fan_watchdog_reset(void)
7713{
94954cc6 7714 static int fan_watchdog_active;
16663a87 7715
4985cd0a
HMH
7716 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7717 return;
7718
16663a87
HMH
7719 if (fan_watchdog_active)
7720 cancel_delayed_work(&fan_watchdog_task);
7721
8fef502e
HMH
7722 if (fan_watchdog_maxinterval > 0 &&
7723 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
16663a87 7724 fan_watchdog_active = 1;
e0e3c061 7725 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
16663a87
HMH
7726 msecs_to_jiffies(fan_watchdog_maxinterval
7727 * 1000))) {
35ff8b9f 7728 printk(TPACPI_ERR
e0e3c061 7729 "failed to queue the fan watchdog, "
16663a87
HMH
7730 "watchdog will not trigger\n");
7731 }
7732 } else
7733 fan_watchdog_active = 0;
7734}
7735
b21a15f6 7736static void fan_watchdog_fire(struct work_struct *ignored)
78f81cc4 7737{
b21a15f6 7738 int rc;
18ad7996 7739
b21a15f6
HMH
7740 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7741 return;
a12095c2 7742
e0c7dfe7 7743 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
b21a15f6
HMH
7744 rc = fan_set_enable();
7745 if (rc < 0) {
e0c7dfe7 7746 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
b21a15f6
HMH
7747 "will try again later...\n", -rc);
7748 /* reschedule for later */
7749 fan_watchdog_reset();
7750 }
7751}
eaa7571b 7752
b21a15f6
HMH
7753/*
7754 * SYSFS fan layout: hwmon compatible (device)
7755 *
7756 * pwm*_enable:
7757 * 0: "disengaged" mode
7758 * 1: manual mode
7759 * 2: native EC "auto" mode (recommended, hardware default)
7760 *
7761 * pwm*: set speed in manual mode, ignored otherwise.
7762 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7763 * interpolation.
7764 *
7765 * fan*_input: tachometer reading, RPM
7766 *
7767 *
7768 * SYSFS fan layout: extensions
7769 *
7770 * fan_watchdog (driver):
7771 * fan watchdog interval in seconds, 0 disables (default), max 120
7772 */
7773
7774/* sysfs fan pwm1_enable ----------------------------------------------- */
7775static ssize_t fan_pwm1_enable_show(struct device *dev,
7776 struct device_attribute *attr,
7777 char *buf)
7778{
7779 int res, mode;
7780 u8 status;
7781
7782 res = fan_get_status_safe(&status);
7783 if (res)
7784 return res;
7785
b21a15f6
HMH
7786 if (status & TP_EC_FAN_FULLSPEED) {
7787 mode = 0;
7788 } else if (status & TP_EC_FAN_AUTO) {
7789 mode = 2;
7790 } else
7791 mode = 1;
7792
7793 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7794}
a12095c2 7795
b21a15f6
HMH
7796static ssize_t fan_pwm1_enable_store(struct device *dev,
7797 struct device_attribute *attr,
7798 const char *buf, size_t count)
7799{
7800 unsigned long t;
7801 int res, level;
7802
7803 if (parse_strtoul(buf, 2, &t))
7804 return -EINVAL;
7805
74a60c0f
HMH
7806 tpacpi_disclose_usertask("hwmon pwm1_enable",
7807 "set fan mode to %lu\n", t);
7808
b21a15f6
HMH
7809 switch (t) {
7810 case 0:
7811 level = TP_EC_FAN_FULLSPEED;
7812 break;
7813 case 1:
7814 level = TPACPI_FAN_LAST_LEVEL;
7815 break;
7816 case 2:
7817 level = TP_EC_FAN_AUTO;
7818 break;
7819 case 3:
7820 /* reserved for software-controlled auto mode */
7821 return -ENOSYS;
18ad7996 7822 default:
b21a15f6 7823 return -EINVAL;
18ad7996 7824 }
b21a15f6
HMH
7825
7826 res = fan_set_level_safe(level);
7827 if (res == -ENXIO)
7828 return -EINVAL;
7829 else if (res < 0)
7830 return res;
7831
7832 fan_watchdog_reset();
7833
7834 return count;
18ad7996
HMH
7835}
7836
b21a15f6
HMH
7837static struct device_attribute dev_attr_fan_pwm1_enable =
7838 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7839 fan_pwm1_enable_show, fan_pwm1_enable_store);
7840
7841/* sysfs fan pwm1 ------------------------------------------------------ */
7842static ssize_t fan_pwm1_show(struct device *dev,
7843 struct device_attribute *attr,
7844 char *buf)
fe98a52c 7845{
b21a15f6
HMH
7846 int res;
7847 u8 status;
fe98a52c 7848
b21a15f6
HMH
7849 res = fan_get_status_safe(&status);
7850 if (res)
7851 return res;
ecf2a80a 7852
b21a15f6
HMH
7853 if ((status &
7854 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7855 status = fan_control_desired_level;
fe98a52c 7856
b21a15f6
HMH
7857 if (status > 7)
7858 status = 7;
fe98a52c 7859
b21a15f6 7860 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
fe98a52c
HMH
7861}
7862
b21a15f6
HMH
7863static ssize_t fan_pwm1_store(struct device *dev,
7864 struct device_attribute *attr,
7865 const char *buf, size_t count)
18ad7996 7866{
b21a15f6 7867 unsigned long s;
1c6a334e 7868 int rc;
b21a15f6 7869 u8 status, newlevel;
1c6a334e 7870
b21a15f6
HMH
7871 if (parse_strtoul(buf, 255, &s))
7872 return -EINVAL;
7873
74a60c0f
HMH
7874 tpacpi_disclose_usertask("hwmon pwm1",
7875 "set fan speed to %lu\n", s);
7876
b21a15f6
HMH
7877 /* scale down from 0-255 to 0-7 */
7878 newlevel = (s >> 5) & 0x07;
ecf2a80a 7879
7646ea88 7880 if (mutex_lock_killable(&fan_mutex))
fc589a3c 7881 return -ERESTARTSYS;
40ca9fdf 7882
b21a15f6
HMH
7883 rc = fan_get_status(&status);
7884 if (!rc && (status &
7885 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7886 rc = fan_set_level(newlevel);
7887 if (rc == -ENXIO)
7888 rc = -EINVAL;
7889 else if (!rc) {
7890 fan_update_desired_level(newlevel);
7891 fan_watchdog_reset();
eaa7571b 7892 }
b21a15f6 7893 }
1c6a334e 7894
b21a15f6
HMH
7895 mutex_unlock(&fan_mutex);
7896 return (rc)? rc : count;
7897}
1c6a334e 7898
b21a15f6
HMH
7899static struct device_attribute dev_attr_fan_pwm1 =
7900 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7901 fan_pwm1_show, fan_pwm1_store);
1c6a334e 7902
b21a15f6
HMH
7903/* sysfs fan fan1_input ------------------------------------------------ */
7904static ssize_t fan_fan1_input_show(struct device *dev,
7905 struct device_attribute *attr,
7906 char *buf)
7907{
7908 int res;
7909 unsigned int speed;
1c6a334e 7910
b21a15f6
HMH
7911 res = fan_get_speed(&speed);
7912 if (res < 0)
7913 return res;
1c6a334e 7914
b21a15f6
HMH
7915 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7916}
18ad7996 7917
b21a15f6
HMH
7918static struct device_attribute dev_attr_fan_fan1_input =
7919 __ATTR(fan1_input, S_IRUGO,
7920 fan_fan1_input_show, NULL);
40ca9fdf 7921
d7377247
HMH
7922/* sysfs fan fan2_input ------------------------------------------------ */
7923static ssize_t fan_fan2_input_show(struct device *dev,
7924 struct device_attribute *attr,
7925 char *buf)
7926{
7927 int res;
7928 unsigned int speed;
7929
7930 res = fan2_get_speed(&speed);
7931 if (res < 0)
7932 return res;
7933
7934 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7935}
7936
7937static struct device_attribute dev_attr_fan_fan2_input =
7938 __ATTR(fan2_input, S_IRUGO,
7939 fan_fan2_input_show, NULL);
7940
b21a15f6
HMH
7941/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7942static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7943 char *buf)
7944{
7945 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
18ad7996
HMH
7946}
7947
b21a15f6
HMH
7948static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7949 const char *buf, size_t count)
18ad7996 7950{
b21a15f6
HMH
7951 unsigned long t;
7952
7953 if (parse_strtoul(buf, 120, &t))
7954 return -EINVAL;
40ca9fdf 7955
ecf2a80a
HMH
7956 if (!fan_control_allowed)
7957 return -EPERM;
7958
b21a15f6
HMH
7959 fan_watchdog_maxinterval = t;
7960 fan_watchdog_reset();
40ca9fdf 7961
74a60c0f
HMH
7962 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7963
b21a15f6
HMH
7964 return count;
7965}
7966
7967static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7968 fan_fan_watchdog_show, fan_fan_watchdog_store);
7969
7970/* --------------------------------------------------------------------- */
7971static struct attribute *fan_attributes[] = {
7972 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7973 &dev_attr_fan_fan1_input.attr,
d7377247 7974 NULL, /* for fan2_input */
b21a15f6
HMH
7975 NULL
7976};
7977
7978static const struct attribute_group fan_attr_group = {
7979 .attrs = fan_attributes,
7980};
7981
d7377247
HMH
7982#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7983#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7d95a3d5
HMH
7984
7985#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7986 { .vendor = PCI_VENDOR_ID_IBM, \
7987 .bios = TPACPI_MATCH_ANY, \
7988 .ec = TPID(__id1, __id2), \
7989 .quirks = __quirks }
7990
d7377247
HMH
7991#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7992 { .vendor = PCI_VENDOR_ID_LENOVO, \
7993 .bios = TPACPI_MATCH_ANY, \
7994 .ec = TPID(__id1, __id2), \
7995 .quirks = __quirks }
7996
7d95a3d5
HMH
7997static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7998 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7999 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8000 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8001 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
d7377247 8002 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7d95a3d5
HMH
8003};
8004
d7377247 8005#undef TPACPI_FAN_QL
7d95a3d5
HMH
8006#undef TPACPI_FAN_QI
8007
b21a15f6
HMH
8008static int __init fan_init(struct ibm_init_struct *iibm)
8009{
8010 int rc;
7d95a3d5 8011 unsigned long quirks;
b21a15f6 8012
74a60c0f
HMH
8013 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8014 "initializing fan subdriver\n");
b21a15f6
HMH
8015
8016 mutex_init(&fan_mutex);
8017 fan_status_access_mode = TPACPI_FAN_NONE;
8018 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8019 fan_control_commands = 0;
8020 fan_watchdog_maxinterval = 0;
8021 tp_features.fan_ctrl_status_undef = 0;
d7377247 8022 tp_features.second_fan = 0;
b21a15f6
HMH
8023 fan_control_desired_level = 7;
8024
e28393c0
HMH
8025 if (tpacpi_is_ibm()) {
8026 TPACPI_ACPIHANDLE_INIT(fans);
8027 TPACPI_ACPIHANDLE_INIT(gfan);
8028 TPACPI_ACPIHANDLE_INIT(sfan);
8029 }
b21a15f6 8030
7d95a3d5
HMH
8031 quirks = tpacpi_check_quirks(fan_quirk_table,
8032 ARRAY_SIZE(fan_quirk_table));
8033
b21a15f6
HMH
8034 if (gfan_handle) {
8035 /* 570, 600e/x, 770e, 770x */
8036 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8037 } else {
8038 /* all other ThinkPads: note that even old-style
8039 * ThinkPad ECs supports the fan control register */
8040 if (likely(acpi_ec_read(fan_status_offset,
8041 &fan_control_initial_status))) {
8042 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7d95a3d5
HMH
8043 if (quirks & TPACPI_FAN_Q1)
8044 fan_quirk1_setup();
d7377247
HMH
8045 if (quirks & TPACPI_FAN_2FAN) {
8046 tp_features.second_fan = 1;
8047 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8048 "secondary fan support enabled\n");
8049 }
b21a15f6 8050 } else {
e0c7dfe7 8051 printk(TPACPI_ERR
b21a15f6
HMH
8052 "ThinkPad ACPI EC access misbehaving, "
8053 "fan status and control unavailable\n");
8054 return 1;
8055 }
8056 }
8057
8058 if (sfan_handle) {
8059 /* 570, 770x-JL */
8060 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8061 fan_control_commands |=
8062 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8063 } else {
8064 if (!gfan_handle) {
8065 /* gfan without sfan means no fan control */
8066 /* all other models implement TP EC 0x2f control */
8067
8068 if (fans_handle) {
8069 /* X31, X40, X41 */
8070 fan_control_access_mode =
8071 TPACPI_FAN_WR_ACPI_FANS;
8072 fan_control_commands |=
8073 TPACPI_FAN_CMD_SPEED |
8074 TPACPI_FAN_CMD_LEVEL |
8075 TPACPI_FAN_CMD_ENABLE;
8076 } else {
8077 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8078 fan_control_commands |=
8079 TPACPI_FAN_CMD_LEVEL |
8080 TPACPI_FAN_CMD_ENABLE;
8081 }
fe98a52c 8082 }
b21a15f6 8083 }
18ad7996 8084
74a60c0f
HMH
8085 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8086 "fan is %s, modes %d, %d\n",
b21a15f6
HMH
8087 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8088 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8089 fan_status_access_mode, fan_control_access_mode);
1c6a334e 8090
b21a15f6
HMH
8091 /* fan control master switch */
8092 if (!fan_control_allowed) {
8093 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8094 fan_control_commands = 0;
74a60c0f 8095 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
b21a15f6 8096 "fan control features disabled by parameter\n");
18ad7996 8097 }
40ca9fdf 8098
b21a15f6
HMH
8099 /* update fan_control_desired_level */
8100 if (fan_status_access_mode != TPACPI_FAN_NONE)
8101 fan_get_status_safe(NULL);
fe98a52c 8102
b21a15f6
HMH
8103 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8104 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
d7377247
HMH
8105 if (tp_features.second_fan) {
8106 /* attach second fan tachometer */
8107 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8108 &dev_attr_fan_fan2_input.attr;
8109 }
b21a15f6
HMH
8110 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8111 &fan_attr_group);
b21a15f6
HMH
8112 if (rc < 0)
8113 return rc;
9c0a76e1
HMH
8114
8115 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8116 &driver_attr_fan_watchdog);
8117 if (rc < 0) {
8118 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8119 &fan_attr_group);
8120 return rc;
8121 }
b21a15f6
HMH
8122 return 0;
8123 } else
8124 return 1;
56b6aeb0
HMH
8125}
8126
b21a15f6 8127static void fan_exit(void)
56b6aeb0 8128{
74a60c0f 8129 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
35ff8b9f 8130 "cancelling any pending fan watchdog tasks\n");
56b6aeb0 8131
b21a15f6
HMH
8132 /* FIXME: can we really do this unconditionally? */
8133 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
35ff8b9f
HMH
8134 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8135 &driver_attr_fan_watchdog);
40ca9fdf 8136
b21a15f6 8137 cancel_delayed_work(&fan_watchdog_task);
e0e3c061 8138 flush_workqueue(tpacpi_wq);
56b6aeb0
HMH
8139}
8140
75700e53
HMH
8141static void fan_suspend(pm_message_t state)
8142{
0081b162
HMH
8143 int rc;
8144
75700e53
HMH
8145 if (!fan_control_allowed)
8146 return;
8147
8148 /* Store fan status in cache */
0081b162
HMH
8149 fan_control_resume_level = 0;
8150 rc = fan_get_status_safe(&fan_control_resume_level);
8151 if (rc < 0)
8152 printk(TPACPI_NOTICE
8153 "failed to read fan level for later "
8154 "restore during resume: %d\n", rc);
8155
8156 /* if it is undefined, don't attempt to restore it.
8157 * KEEP THIS LAST */
75700e53 8158 if (tp_features.fan_ctrl_status_undef)
0081b162 8159 fan_control_resume_level = 0;
75700e53
HMH
8160}
8161
8162static void fan_resume(void)
8163{
75700e53
HMH
8164 u8 current_level = 7;
8165 bool do_set = false;
0081b162 8166 int rc;
75700e53
HMH
8167
8168 /* DSDT *always* updates status on resume */
8169 tp_features.fan_ctrl_status_undef = 0;
8170
75700e53 8171 if (!fan_control_allowed ||
0081b162 8172 !fan_control_resume_level ||
75700e53
HMH
8173 (fan_get_status_safe(&current_level) < 0))
8174 return;
8175
8176 switch (fan_control_access_mode) {
8177 case TPACPI_FAN_WR_ACPI_SFAN:
0081b162
HMH
8178 /* never decrease fan level */
8179 do_set = (fan_control_resume_level > current_level);
75700e53
HMH
8180 break;
8181 case TPACPI_FAN_WR_ACPI_FANS:
8182 case TPACPI_FAN_WR_TPEC:
0081b162
HMH
8183 /* never decrease fan level, scale is:
8184 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8185 *
8186 * We expect the firmware to set either 7 or AUTO, but we
8187 * handle FULLSPEED out of paranoia.
8188 *
8189 * So, we can safely only restore FULLSPEED or 7, anything
8190 * else could slow the fan. Restoring AUTO is useless, at
8191 * best that's exactly what the DSDT already set (it is the
8192 * slower it uses).
8193 *
8194 * Always keep in mind that the DSDT *will* have set the
8195 * fans to what the vendor supposes is the best level. We
8196 * muck with it only to speed the fan up.
8197 */
8198 if (fan_control_resume_level != 7 &&
8199 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8200 return;
8201 else
8202 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8203 (current_level != fan_control_resume_level);
75700e53
HMH
8204 break;
8205 default:
8206 return;
8207 }
8208 if (do_set) {
8209 printk(TPACPI_NOTICE
8210 "restoring fan level to 0x%02x\n",
0081b162
HMH
8211 fan_control_resume_level);
8212 rc = fan_set_level_safe(fan_control_resume_level);
8213 if (rc < 0)
8214 printk(TPACPI_NOTICE
8215 "failed to restore fan level: %d\n", rc);
75700e53
HMH
8216 }
8217}
8218
887965e6 8219static int fan_read(struct seq_file *m)
56b6aeb0 8220{
56b6aeb0
HMH
8221 int rc;
8222 u8 status;
8223 unsigned int speed = 0;
8224
8225 switch (fan_status_access_mode) {
efa27145 8226 case TPACPI_FAN_RD_ACPI_GFAN:
56b6aeb0 8227 /* 570, 600e/x, 770e, 770x */
35ff8b9f
HMH
8228 rc = fan_get_status_safe(&status);
8229 if (rc < 0)
56b6aeb0
HMH
8230 return rc;
8231
887965e6 8232 seq_printf(m, "status:\t\t%s\n"
56b6aeb0
HMH
8233 "level:\t\t%d\n",
8234 (status != 0) ? "enabled" : "disabled", status);
8235 break;
8236
efa27145 8237 case TPACPI_FAN_RD_TPEC:
56b6aeb0 8238 /* all except 570, 600e/x, 770e, 770x */
35ff8b9f
HMH
8239 rc = fan_get_status_safe(&status);
8240 if (rc < 0)
56b6aeb0
HMH
8241 return rc;
8242
887965e6 8243 seq_printf(m, "status:\t\t%s\n",
56b6aeb0
HMH
8244 (status != 0) ? "enabled" : "disabled");
8245
35ff8b9f
HMH
8246 rc = fan_get_speed(&speed);
8247 if (rc < 0)
56b6aeb0
HMH
8248 return rc;
8249
887965e6 8250 seq_printf(m, "speed:\t\t%d\n", speed);
56b6aeb0 8251
efa27145 8252 if (status & TP_EC_FAN_FULLSPEED)
56b6aeb0 8253 /* Disengaged mode takes precedence */
887965e6 8254 seq_printf(m, "level:\t\tdisengaged\n");
efa27145 8255 else if (status & TP_EC_FAN_AUTO)
887965e6 8256 seq_printf(m, "level:\t\tauto\n");
56b6aeb0 8257 else
887965e6 8258 seq_printf(m, "level:\t\t%d\n", status);
56b6aeb0
HMH
8259 break;
8260
efa27145 8261 case TPACPI_FAN_NONE:
56b6aeb0 8262 default:
887965e6 8263 seq_printf(m, "status:\t\tnot supported\n");
56b6aeb0
HMH
8264 }
8265
efa27145 8266 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
887965e6 8267 seq_printf(m, "commands:\tlevel <level>");
56b6aeb0
HMH
8268
8269 switch (fan_control_access_mode) {
efa27145 8270 case TPACPI_FAN_WR_ACPI_SFAN:
887965e6 8271 seq_printf(m, " (<level> is 0-7)\n");
56b6aeb0
HMH
8272 break;
8273
8274 default:
887965e6 8275 seq_printf(m, " (<level> is 0-7, "
fe98a52c 8276 "auto, disengaged, full-speed)\n");
56b6aeb0
HMH
8277 break;
8278 }
8279 }
18ad7996 8280
efa27145 8281 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
887965e6 8282 seq_printf(m, "commands:\tenable, disable\n"
35ff8b9f
HMH
8283 "commands:\twatchdog <timeout> (<timeout> "
8284 "is 0 (off), 1-120 (seconds))\n");
1da177e4 8285
efa27145 8286 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
887965e6 8287 seq_printf(m, "commands:\tspeed <speed>"
56b6aeb0
HMH
8288 " (<speed> is 0-65535)\n");
8289
887965e6 8290 return 0;
78f81cc4
BD
8291}
8292
18ad7996
HMH
8293static int fan_write_cmd_level(const char *cmd, int *rc)
8294{
8295 int level;
8296
a12095c2 8297 if (strlencmp(cmd, "level auto") == 0)
efa27145 8298 level = TP_EC_FAN_AUTO;
fe98a52c 8299 else if ((strlencmp(cmd, "level disengaged") == 0) |
35ff8b9f 8300 (strlencmp(cmd, "level full-speed") == 0))
efa27145 8301 level = TP_EC_FAN_FULLSPEED;
a12095c2 8302 else if (sscanf(cmd, "level %d", &level) != 1)
18ad7996
HMH
8303 return 0;
8304
35ff8b9f
HMH
8305 *rc = fan_set_level_safe(level);
8306 if (*rc == -ENXIO)
e0c7dfe7 8307 printk(TPACPI_ERR "level command accepted for unsupported "
18ad7996 8308 "access mode %d", fan_control_access_mode);
74a60c0f
HMH
8309 else if (!*rc)
8310 tpacpi_disclose_usertask("procfs fan",
8311 "set level to %d\n", level);
18ad7996
HMH
8312
8313 return 1;
8314}
8315
8316static int fan_write_cmd_enable(const char *cmd, int *rc)
8317{
8318 if (strlencmp(cmd, "enable") != 0)
8319 return 0;
8320
35ff8b9f
HMH
8321 *rc = fan_set_enable();
8322 if (*rc == -ENXIO)
e0c7dfe7 8323 printk(TPACPI_ERR "enable command accepted for unsupported "
18ad7996 8324 "access mode %d", fan_control_access_mode);
74a60c0f
HMH
8325 else if (!*rc)
8326 tpacpi_disclose_usertask("procfs fan", "enable\n");
18ad7996
HMH
8327
8328 return 1;
8329}
8330
8331static int fan_write_cmd_disable(const char *cmd, int *rc)
8332{
8333 if (strlencmp(cmd, "disable") != 0)
8334 return 0;
8335
35ff8b9f
HMH
8336 *rc = fan_set_disable();
8337 if (*rc == -ENXIO)
e0c7dfe7 8338 printk(TPACPI_ERR "disable command accepted for unsupported "
18ad7996 8339 "access mode %d", fan_control_access_mode);
74a60c0f
HMH
8340 else if (!*rc)
8341 tpacpi_disclose_usertask("procfs fan", "disable\n");
18ad7996
HMH
8342
8343 return 1;
8344}
8345
8346static int fan_write_cmd_speed(const char *cmd, int *rc)
8347{
8348 int speed;
8349
a8b7a662
HMH
8350 /* TODO:
8351 * Support speed <low> <medium> <high> ? */
8352
18ad7996
HMH
8353 if (sscanf(cmd, "speed %d", &speed) != 1)
8354 return 0;
8355
35ff8b9f
HMH
8356 *rc = fan_set_speed(speed);
8357 if (*rc == -ENXIO)
e0c7dfe7 8358 printk(TPACPI_ERR "speed command accepted for unsupported "
18ad7996 8359 "access mode %d", fan_control_access_mode);
74a60c0f
HMH
8360 else if (!*rc)
8361 tpacpi_disclose_usertask("procfs fan",
8362 "set speed to %d\n", speed);
18ad7996
HMH
8363
8364 return 1;
8365}
8366
16663a87
HMH
8367static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8368{
8369 int interval;
8370
8371 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8372 return 0;
8373
8374 if (interval < 0 || interval > 120)
8375 *rc = -EINVAL;
74a60c0f 8376 else {
16663a87 8377 fan_watchdog_maxinterval = interval;
74a60c0f
HMH
8378 tpacpi_disclose_usertask("procfs fan",
8379 "set watchdog timer to %d\n",
8380 interval);
8381 }
16663a87
HMH
8382
8383 return 1;
8384}
8385
18ad7996
HMH
8386static int fan_write(char *buf)
8387{
8388 char *cmd;
8389 int rc = 0;
8390
8391 while (!rc && (cmd = next_cmd(&buf))) {
efa27145 8392 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
18ad7996 8393 fan_write_cmd_level(cmd, &rc)) &&
efa27145 8394 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
18ad7996 8395 (fan_write_cmd_enable(cmd, &rc) ||
16663a87
HMH
8396 fan_write_cmd_disable(cmd, &rc) ||
8397 fan_write_cmd_watchdog(cmd, &rc))) &&
efa27145 8398 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
18ad7996
HMH
8399 fan_write_cmd_speed(cmd, &rc))
8400 )
8401 rc = -EINVAL;
16663a87
HMH
8402 else if (!rc)
8403 fan_watchdog_reset();
18ad7996
HMH
8404 }
8405
8406 return rc;
8407}
8408
a5763f22
HMH
8409static struct ibm_struct fan_driver_data = {
8410 .name = "fan",
8411 .read = fan_read,
8412 .write = fan_write,
8413 .exit = fan_exit,
75700e53
HMH
8414 .suspend = fan_suspend,
8415 .resume = fan_resume,
a5763f22
HMH
8416};
8417
56b6aeb0
HMH
8418/****************************************************************************
8419 ****************************************************************************
8420 *
8421 * Infrastructure
8422 *
8423 ****************************************************************************
8424 ****************************************************************************/
8425
8b468c0c
HMH
8426/*
8427 * HKEY event callout for other subdrivers go here
8428 * (yes, it is ugly, but it is quick, safe, and gets the job done
8429 */
8430static void tpacpi_driver_event(const unsigned int hkey_event)
8431{
347a2686
HMH
8432 if (ibm_backlight_device) {
8433 switch (hkey_event) {
8434 case TP_HKEY_EV_BRGHT_UP:
8435 case TP_HKEY_EV_BRGHT_DOWN:
8436 tpacpi_brightness_notify_change();
8437 }
8438 }
0d204c34
HMH
8439 if (alsa_card) {
8440 switch (hkey_event) {
8441 case TP_HKEY_EV_VOL_UP:
8442 case TP_HKEY_EV_VOL_DOWN:
8443 case TP_HKEY_EV_VOL_MUTE:
8444 volume_alsa_notify_change();
8445 }
8446 }
8b468c0c
HMH
8447}
8448
8b468c0c
HMH
8449static void hotkey_driver_event(const unsigned int scancode)
8450{
67bcae6e 8451 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8b468c0c
HMH
8452}
8453
7fd40029
HMH
8454/* sysfs name ---------------------------------------------------------- */
8455static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8456 struct device_attribute *attr,
8457 char *buf)
8458{
e0c7dfe7 8459 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7fd40029
HMH
8460}
8461
8462static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8463 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8464
8465/* --------------------------------------------------------------------- */
8466
56b6aeb0 8467/* /proc support */
94954cc6 8468static struct proc_dir_entry *proc_dir;
16663a87 8469
56b6aeb0
HMH
8470/*
8471 * Module and infrastructure proble, init and exit handling
8472 */
1da177e4 8473
b21a15f6
HMH
8474static int force_load;
8475
fe08bc4b 8476#ifdef CONFIG_THINKPAD_ACPI_DEBUG
a5763f22 8477static const char * __init str_supported(int is_supported)
fe08bc4b 8478{
a5763f22 8479 static char text_unsupported[] __initdata = "not supported";
fe08bc4b 8480
a5763f22 8481 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
fe08bc4b
HMH
8482}
8483#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8484
b21a15f6
HMH
8485static void ibm_exit(struct ibm_struct *ibm)
8486{
8487 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8488
8489 list_del_init(&ibm->all_drivers);
8490
8491 if (ibm->flags.acpi_notify_installed) {
8492 dbg_printk(TPACPI_DBG_EXIT,
8493 "%s: acpi_remove_notify_handler\n", ibm->name);
8494 BUG_ON(!ibm->acpi);
8495 acpi_remove_notify_handler(*ibm->acpi->handle,
8496 ibm->acpi->type,
8497 dispatch_acpi_notify);
8498 ibm->flags.acpi_notify_installed = 0;
8499 ibm->flags.acpi_notify_installed = 0;
8500 }
8501
8502 if (ibm->flags.proc_created) {
8503 dbg_printk(TPACPI_DBG_EXIT,
8504 "%s: remove_proc_entry\n", ibm->name);
8505 remove_proc_entry(ibm->name, proc_dir);
8506 ibm->flags.proc_created = 0;
8507 }
8508
8509 if (ibm->flags.acpi_driver_registered) {
8510 dbg_printk(TPACPI_DBG_EXIT,
8511 "%s: acpi_bus_unregister_driver\n", ibm->name);
8512 BUG_ON(!ibm->acpi);
8513 acpi_bus_unregister_driver(ibm->acpi->driver);
8514 kfree(ibm->acpi->driver);
8515 ibm->acpi->driver = NULL;
8516 ibm->flags.acpi_driver_registered = 0;
8517 }
8518
8519 if (ibm->flags.init_called && ibm->exit) {
8520 ibm->exit();
8521 ibm->flags.init_called = 0;
8522 }
8523
8524 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8525}
f74a27d4 8526
a5763f22 8527static int __init ibm_init(struct ibm_init_struct *iibm)
1da177e4
LT
8528{
8529 int ret;
a5763f22 8530 struct ibm_struct *ibm = iibm->data;
1da177e4
LT
8531 struct proc_dir_entry *entry;
8532
a5763f22
HMH
8533 BUG_ON(ibm == NULL);
8534
8535 INIT_LIST_HEAD(&ibm->all_drivers);
8536
92641177 8537 if (ibm->flags.experimental && !experimental)
1da177e4
LT
8538 return 0;
8539
fe08bc4b
HMH
8540 dbg_printk(TPACPI_DBG_INIT,
8541 "probing for %s\n", ibm->name);
8542
a5763f22
HMH
8543 if (iibm->init) {
8544 ret = iibm->init(iibm);
5fba344c
HMH
8545 if (ret > 0)
8546 return 0; /* probe failed */
8547 if (ret)
1da177e4 8548 return ret;
a5763f22 8549
92641177 8550 ibm->flags.init_called = 1;
1da177e4
LT
8551 }
8552
8d376cd6
HMH
8553 if (ibm->acpi) {
8554 if (ibm->acpi->hid) {
8555 ret = register_tpacpi_subdriver(ibm);
8556 if (ret)
8557 goto err_out;
8558 }
5fba344c 8559
8d376cd6
HMH
8560 if (ibm->acpi->notify) {
8561 ret = setup_acpi_notify(ibm);
8562 if (ret == -ENODEV) {
e0c7dfe7 8563 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8d376cd6
HMH
8564 ibm->name);
8565 ret = 0;
8566 goto err_out;
8567 }
8568 if (ret < 0)
8569 goto err_out;
5fba344c 8570 }
5fba344c
HMH
8571 }
8572
fe08bc4b
HMH
8573 dbg_printk(TPACPI_DBG_INIT,
8574 "%s installed\n", ibm->name);
8575
78f81cc4 8576 if (ibm->read) {
b525c06c 8577 mode_t mode = iibm->base_procfs_mode;
887965e6 8578
b525c06c
HMH
8579 if (!mode)
8580 mode = S_IRUGO;
887965e6
AD
8581 if (ibm->write)
8582 mode |= S_IWUSR;
8583 entry = proc_create_data(ibm->name, mode, proc_dir,
8584 &dispatch_proc_fops, ibm);
78f81cc4 8585 if (!entry) {
e0c7dfe7 8586 printk(TPACPI_ERR "unable to create proc entry %s\n",
78f81cc4 8587 ibm->name);
5fba344c
HMH
8588 ret = -ENODEV;
8589 goto err_out;
78f81cc4 8590 }
92641177 8591 ibm->flags.proc_created = 1;
78f81cc4 8592 }
1da177e4 8593
a5763f22
HMH
8594 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8595
1da177e4 8596 return 0;
5fba344c
HMH
8597
8598err_out:
fe08bc4b
HMH
8599 dbg_printk(TPACPI_DBG_INIT,
8600 "%s: at error exit path with result %d\n",
8601 ibm->name, ret);
8602
5fba344c
HMH
8603 ibm_exit(ibm);
8604 return (ret < 0)? ret : 0;
1da177e4
LT
8605}
8606
56b6aeb0
HMH
8607/* Probing */
8608
050df107
HMH
8609static bool __pure __init tpacpi_is_fw_digit(const char c)
8610{
8611 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8612}
8613
8614/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8615static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8616 const char t)
8617{
8618 return s && strlen(s) >= 8 &&
8619 tpacpi_is_fw_digit(s[0]) &&
8620 tpacpi_is_fw_digit(s[1]) &&
8621 s[2] == t && s[3] == 'T' &&
8622 tpacpi_is_fw_digit(s[4]) &&
8623 tpacpi_is_fw_digit(s[5]) &&
8624 s[6] == 'W' && s[7] == 'W';
8625}
8626
bf20e740
HMH
8627/* returns 0 - probe ok, or < 0 - probe error.
8628 * Probe ok doesn't mean thinkpad found.
8629 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8630static int __must_check __init get_thinkpad_model_data(
8631 struct thinkpad_id_data *tp)
1da177e4 8632{
1855256c 8633 const struct dmi_device *dev = NULL;
56b6aeb0 8634 char ec_fw_string[18];
bf20e740 8635 char const *s;
1da177e4 8636
d5a2f2f1 8637 if (!tp)
bf20e740 8638 return -EINVAL;
d5a2f2f1
HMH
8639
8640 memset(tp, 0, sizeof(*tp));
8641
8642 if (dmi_name_in_vendors("IBM"))
8643 tp->vendor = PCI_VENDOR_ID_IBM;
8644 else if (dmi_name_in_vendors("LENOVO"))
8645 tp->vendor = PCI_VENDOR_ID_LENOVO;
8646 else
bf20e740 8647 return 0;
d5a2f2f1 8648
bf20e740
HMH
8649 s = dmi_get_system_info(DMI_BIOS_VERSION);
8650 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8651 if (s && !tp->bios_version_str)
8652 return -ENOMEM;
050df107
HMH
8653
8654 /* Really ancient ThinkPad 240X will fail this, which is fine */
8655 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
bf20e740 8656 return 0;
050df107 8657
d5a2f2f1
HMH
8658 tp->bios_model = tp->bios_version_str[0]
8659 | (tp->bios_version_str[1] << 8);
050df107
HMH
8660 tp->bios_release = (tp->bios_version_str[4] << 8)
8661 | tp->bios_version_str[5];
d5a2f2f1 8662
56b6aeb0
HMH
8663 /*
8664 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8665 * X32 or newer, all Z series; Some models must have an
8666 * up-to-date BIOS or they will not be detected.
8667 *
8668 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8669 */
8670 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8671 if (sscanf(dev->name,
8672 "IBM ThinkPad Embedded Controller -[%17c",
8673 ec_fw_string) == 1) {
8674 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8675 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
d5a2f2f1
HMH
8676
8677 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
bf20e740
HMH
8678 if (!tp->ec_version_str)
8679 return -ENOMEM;
050df107
HMH
8680
8681 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8682 tp->ec_model = ec_fw_string[0]
8683 | (ec_fw_string[1] << 8);
8684 tp->ec_release = (ec_fw_string[4] << 8)
8685 | ec_fw_string[5];
8686 } else {
8687 printk(TPACPI_NOTICE
8688 "ThinkPad firmware release %s "
8689 "doesn't match the known patterns\n",
8690 ec_fw_string);
8691 printk(TPACPI_NOTICE
8692 "please report this to %s\n",
8693 TPACPI_MAIL);
8694 }
d5a2f2f1 8695 break;
78f81cc4 8696 }
1da177e4 8697 }
d5a2f2f1 8698
bf20e740
HMH
8699 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8700 if (s && !strnicmp(s, "ThinkPad", 8)) {
8701 tp->model_str = kstrdup(s, GFP_KERNEL);
8702 if (!tp->model_str)
8703 return -ENOMEM;
d5a2f2f1 8704 }
8c74adbc 8705
bf20e740
HMH
8706 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8707 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8708 if (s && !tp->nummodel_str)
8709 return -ENOMEM;
8710
8711 return 0;
1da177e4
LT
8712}
8713
5fba344c
HMH
8714static int __init probe_for_thinkpad(void)
8715{
8716 int is_thinkpad;
8717
8718 if (acpi_disabled)
8719 return -ENODEV;
8720
e28393c0
HMH
8721 /* It would be dangerous to run the driver in this case */
8722 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8723 return -ENODEV;
8724
5fba344c
HMH
8725 /*
8726 * Non-ancient models have better DMI tagging, but very old models
e675abaf 8727 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
5fba344c 8728 */
e675abaf
HMH
8729 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8730 (thinkpad_id.ec_model != 0) ||
8731 tpacpi_is_fw_known();
5fba344c 8732
437e470c
HMH
8733 /* The EC handler is required */
8734 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
5fba344c
HMH
8735 if (!ec_handle) {
8736 if (is_thinkpad)
e0c7dfe7 8737 printk(TPACPI_ERR
5fba344c
HMH
8738 "Not yet supported ThinkPad detected!\n");
8739 return -ENODEV;
8740 }
8741
0dcef77c
HMH
8742 if (!is_thinkpad && !force_load)
8743 return -ENODEV;
8744
5fba344c
HMH
8745 return 0;
8746}
8747
7a43f788
HMH
8748static void __init thinkpad_acpi_init_banner(void)
8749{
8750 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8751 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8752
8753 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8754 (thinkpad_id.bios_version_str) ?
8755 thinkpad_id.bios_version_str : "unknown",
8756 (thinkpad_id.ec_version_str) ?
8757 thinkpad_id.ec_version_str : "unknown");
8758
8759 BUG_ON(!thinkpad_id.vendor);
8760
8761 if (thinkpad_id.model_str)
8762 printk(TPACPI_INFO "%s %s, model %s\n",
8763 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8764 "IBM" : ((thinkpad_id.vendor ==
8765 PCI_VENDOR_ID_LENOVO) ?
8766 "Lenovo" : "Unknown vendor"),
8767 thinkpad_id.model_str,
8768 (thinkpad_id.nummodel_str) ?
8769 thinkpad_id.nummodel_str : "unknown");
8770}
5fba344c 8771
56b6aeb0 8772/* Module init, exit, parameters */
1da177e4 8773
a5763f22
HMH
8774static struct ibm_init_struct ibms_init[] __initdata = {
8775 {
a5763f22
HMH
8776 .data = &thinkpad_acpi_driver_data,
8777 },
8778 {
8779 .init = hotkey_init,
8780 .data = &hotkey_driver_data,
8781 },
8782 {
8783 .init = bluetooth_init,
8784 .data = &bluetooth_driver_data,
8785 },
8786 {
8787 .init = wan_init,
8788 .data = &wan_driver_data,
8789 },
0045c0aa
HMH
8790 {
8791 .init = uwb_init,
8792 .data = &uwb_driver_data,
8793 },
d7c1d17d 8794#ifdef CONFIG_THINKPAD_ACPI_VIDEO
a5763f22
HMH
8795 {
8796 .init = video_init,
b525c06c 8797 .base_procfs_mode = S_IRUSR,
a5763f22
HMH
8798 .data = &video_driver_data,
8799 },
d7c1d17d 8800#endif
a5763f22
HMH
8801 {
8802 .init = light_init,
8803 .data = &light_driver_data,
8804 },
a5763f22
HMH
8805 {
8806 .init = cmos_init,
8807 .data = &cmos_driver_data,
8808 },
8809 {
8810 .init = led_init,
8811 .data = &led_driver_data,
8812 },
8813 {
8814 .init = beep_init,
8815 .data = &beep_driver_data,
8816 },
8817 {
8818 .init = thermal_init,
8819 .data = &thermal_driver_data,
8820 },
a5763f22
HMH
8821 {
8822 .init = brightness_init,
8823 .data = &brightness_driver_data,
8824 },
8825 {
329e4e18 8826 .init = volume_init,
a5763f22
HMH
8827 .data = &volume_driver_data,
8828 },
8829 {
8830 .init = fan_init,
8831 .data = &fan_driver_data,
8832 },
8833};
8834
3945ac36 8835static int __init set_ibm_param(const char *val, struct kernel_param *kp)
1da177e4
LT
8836{
8837 unsigned int i;
a5763f22 8838 struct ibm_struct *ibm;
1da177e4 8839
59f91ff1
HMH
8840 if (!kp || !kp->name || !val)
8841 return -EINVAL;
8842
a5763f22
HMH
8843 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8844 ibm = ibms_init[i].data;
59f91ff1
HMH
8845 WARN_ON(ibm == NULL);
8846
8847 if (!ibm || !ibm->name)
8848 continue;
a5763f22
HMH
8849
8850 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8851 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
78f81cc4 8852 return -ENOSPC;
a5763f22
HMH
8853 strcpy(ibms_init[i].param, val);
8854 strcat(ibms_init[i].param, ",");
78f81cc4
BD
8855 return 0;
8856 }
a5763f22 8857 }
1da177e4 8858
1da177e4
LT
8859 return -EINVAL;
8860}
8861
b09c7225 8862module_param(experimental, int, 0444);
f68080f8 8863MODULE_PARM_DESC(experimental,
35ff8b9f 8864 "Enables experimental features when non-zero");
56b6aeb0 8865
132ce091 8866module_param_named(debug, dbg_level, uint, 0);
f68080f8 8867MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
132ce091 8868
b09c7225 8869module_param(force_load, bool, 0444);
f68080f8 8870MODULE_PARM_DESC(force_load,
35ff8b9f
HMH
8871 "Attempts to load the driver even on a "
8872 "mis-identified ThinkPad when true");
0dcef77c 8873
b09c7225 8874module_param_named(fan_control, fan_control_allowed, bool, 0444);
f68080f8 8875MODULE_PARM_DESC(fan_control,
35ff8b9f 8876 "Enables setting fan parameters features when true");
ecf2a80a 8877
b09c7225 8878module_param_named(brightness_mode, brightness_mode, uint, 0444);
f68080f8 8879MODULE_PARM_DESC(brightness_mode,
35ff8b9f 8880 "Selects brightness control strategy: "
0e501834 8881 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
24d3b774 8882
b09c7225 8883module_param(brightness_enable, uint, 0444);
f68080f8 8884MODULE_PARM_DESC(brightness_enable,
35ff8b9f 8885 "Enables backlight control when 1, disables when 0");
87cc537a 8886
b09c7225 8887module_param(hotkey_report_mode, uint, 0444);
f68080f8 8888MODULE_PARM_DESC(hotkey_report_mode,
35ff8b9f
HMH
8889 "used for backwards compatibility with userspace, "
8890 "see documentation");
ff80f137 8891
ff850c33 8892#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
329e4e18
HMH
8893module_param_named(volume_mode, volume_mode, uint, 0444);
8894MODULE_PARM_DESC(volume_mode,
8895 "Selects volume control strategy: "
8896 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8897
a112ceee
HMH
8898module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8899MODULE_PARM_DESC(volume_capabilities,
8900 "Selects the mixer capabilites: "
8901 "0=auto, 1=volume and mute, 2=mute only");
8902
c7ac6291
HMH
8903module_param_named(volume_control, volume_control_allowed, bool, 0444);
8904MODULE_PARM_DESC(volume_control,
8905 "Enables software override for the console audio "
8906 "control when true");
8907
0d204c34
HMH
8908/* ALSA module API parameters */
8909module_param_named(index, alsa_index, int, 0444);
8910MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8911module_param_named(id, alsa_id, charp, 0444);
8912MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8913module_param_named(enable, alsa_enable, bool, 0444);
8914MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
ff850c33 8915#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
0d204c34 8916
e0c7dfe7 8917#define TPACPI_PARAM(feature) \
f68080f8 8918 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
cbb14842 8919 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
35ff8b9f 8920 "at module load, see documentation")
1da177e4 8921
e0c7dfe7
HMH
8922TPACPI_PARAM(hotkey);
8923TPACPI_PARAM(bluetooth);
8924TPACPI_PARAM(video);
8925TPACPI_PARAM(light);
e0c7dfe7
HMH
8926TPACPI_PARAM(cmos);
8927TPACPI_PARAM(led);
8928TPACPI_PARAM(beep);
e0c7dfe7
HMH
8929TPACPI_PARAM(brightness);
8930TPACPI_PARAM(volume);
8931TPACPI_PARAM(fan);
78f81cc4 8932
a73f3091 8933#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
b09c7225 8934module_param(dbg_wlswemul, uint, 0444);
a73f3091
HMH
8935MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8936module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8937MODULE_PARM_DESC(wlsw_state,
8938 "Initial state of the emulated WLSW switch");
8939
b09c7225 8940module_param(dbg_bluetoothemul, uint, 0444);
a73f3091
HMH
8941MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8942module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8943MODULE_PARM_DESC(bluetooth_state,
8944 "Initial state of the emulated bluetooth switch");
8945
b09c7225 8946module_param(dbg_wwanemul, uint, 0444);
a73f3091
HMH
8947MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8948module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8949MODULE_PARM_DESC(wwan_state,
8950 "Initial state of the emulated WWAN switch");
0045c0aa 8951
b09c7225 8952module_param(dbg_uwbemul, uint, 0444);
0045c0aa
HMH
8953MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8954module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8955MODULE_PARM_DESC(uwb_state,
8956 "Initial state of the emulated UWB switch");
a73f3091
HMH
8957#endif
8958
b21a15f6
HMH
8959static void thinkpad_acpi_module_exit(void)
8960{
8961 struct ibm_struct *ibm, *itmp;
8962
8963 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8964
8965 list_for_each_entry_safe_reverse(ibm, itmp,
8966 &tpacpi_all_drivers,
8967 all_drivers) {
8968 ibm_exit(ibm);
8969 }
8970
8971 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8972
8973 if (tpacpi_inputdev) {
8974 if (tp_features.input_device_registered)
8975 input_unregister_device(tpacpi_inputdev);
8976 else
8977 input_free_device(tpacpi_inputdev);
8978 }
8979
8980 if (tpacpi_hwmon)
8981 hwmon_device_unregister(tpacpi_hwmon);
8982
8983 if (tp_features.sensors_pdev_attrs_registered)
8984 device_remove_file(&tpacpi_sensors_pdev->dev,
8985 &dev_attr_thinkpad_acpi_pdev_name);
8986 if (tpacpi_sensors_pdev)
8987 platform_device_unregister(tpacpi_sensors_pdev);
8988 if (tpacpi_pdev)
8989 platform_device_unregister(tpacpi_pdev);
8990
8991 if (tp_features.sensors_pdrv_attrs_registered)
8992 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8993 if (tp_features.platform_drv_attrs_registered)
8994 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8995
8996 if (tp_features.sensors_pdrv_registered)
8997 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8998
8999 if (tp_features.platform_drv_registered)
9000 platform_driver_unregister(&tpacpi_pdriver);
9001
9002 if (proc_dir)
e0c7dfe7 9003 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
b21a15f6 9004
e0e3c061
HMH
9005 if (tpacpi_wq)
9006 destroy_workqueue(tpacpi_wq);
9007
b21a15f6
HMH
9008 kfree(thinkpad_id.bios_version_str);
9009 kfree(thinkpad_id.ec_version_str);
9010 kfree(thinkpad_id.model_str);
9011}
9012
f74a27d4 9013
1def7115 9014static int __init thinkpad_acpi_module_init(void)
1da177e4
LT
9015{
9016 int ret, i;
9017
8fef502e
HMH
9018 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9019
ff80f137
HMH
9020 /* Parameter checking */
9021 if (hotkey_report_mode > 2)
9022 return -EINVAL;
9023
54ae1501 9024 /* Driver-level probe */
d5a2f2f1 9025
bf20e740
HMH
9026 ret = get_thinkpad_model_data(&thinkpad_id);
9027 if (ret) {
9028 printk(TPACPI_ERR
9029 "unable to get DMI data: %d\n", ret);
9030 thinkpad_acpi_module_exit();
9031 return ret;
9032 }
5fba344c 9033 ret = probe_for_thinkpad();
d5a2f2f1
HMH
9034 if (ret) {
9035 thinkpad_acpi_module_exit();
5fba344c 9036 return ret;
d5a2f2f1 9037 }
1da177e4 9038
54ae1501 9039 /* Driver initialization */
d5a2f2f1 9040
7a43f788
HMH
9041 thinkpad_acpi_init_banner();
9042 tpacpi_check_outdated_fw();
9043
e0c7dfe7
HMH
9044 TPACPI_ACPIHANDLE_INIT(ecrd);
9045 TPACPI_ACPIHANDLE_INIT(ecwr);
1da177e4 9046
e0e3c061
HMH
9047 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9048 if (!tpacpi_wq) {
9049 thinkpad_acpi_module_exit();
9050 return -ENOMEM;
9051 }
9052
e0c7dfe7 9053 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
1da177e4 9054 if (!proc_dir) {
35ff8b9f
HMH
9055 printk(TPACPI_ERR
9056 "unable to create proc dir " TPACPI_PROC_DIR);
1def7115 9057 thinkpad_acpi_module_exit();
1da177e4
LT
9058 return -ENODEV;
9059 }
78f81cc4 9060
54ae1501
HMH
9061 ret = platform_driver_register(&tpacpi_pdriver);
9062 if (ret) {
35ff8b9f
HMH
9063 printk(TPACPI_ERR
9064 "unable to register main platform driver\n");
54ae1501
HMH
9065 thinkpad_acpi_module_exit();
9066 return ret;
9067 }
ac36393d
HMH
9068 tp_features.platform_drv_registered = 1;
9069
7fd40029
HMH
9070 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9071 if (ret) {
35ff8b9f
HMH
9072 printk(TPACPI_ERR
9073 "unable to register hwmon platform driver\n");
7fd40029
HMH
9074 thinkpad_acpi_module_exit();
9075 return ret;
9076 }
9077 tp_features.sensors_pdrv_registered = 1;
9078
176750d6 9079 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
2369cc94
HMH
9080 if (!ret) {
9081 tp_features.platform_drv_attrs_registered = 1;
35ff8b9f
HMH
9082 ret = tpacpi_create_driver_attributes(
9083 &tpacpi_hwmon_pdriver.driver);
2369cc94 9084 }
176750d6 9085 if (ret) {
35ff8b9f
HMH
9086 printk(TPACPI_ERR
9087 "unable to create sysfs driver attributes\n");
176750d6
HMH
9088 thinkpad_acpi_module_exit();
9089 return ret;
9090 }
2369cc94 9091 tp_features.sensors_pdrv_attrs_registered = 1;
176750d6 9092
54ae1501
HMH
9093
9094 /* Device initialization */
e0c7dfe7 9095 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
54ae1501
HMH
9096 NULL, 0);
9097 if (IS_ERR(tpacpi_pdev)) {
9098 ret = PTR_ERR(tpacpi_pdev);
9099 tpacpi_pdev = NULL;
e0c7dfe7 9100 printk(TPACPI_ERR "unable to register platform device\n");
54ae1501
HMH
9101 thinkpad_acpi_module_exit();
9102 return ret;
9103 }
7fd40029 9104 tpacpi_sensors_pdev = platform_device_register_simple(
35ff8b9f
HMH
9105 TPACPI_HWMON_DRVR_NAME,
9106 -1, NULL, 0);
7fd40029
HMH
9107 if (IS_ERR(tpacpi_sensors_pdev)) {
9108 ret = PTR_ERR(tpacpi_sensors_pdev);
9109 tpacpi_sensors_pdev = NULL;
35ff8b9f
HMH
9110 printk(TPACPI_ERR
9111 "unable to register hwmon platform device\n");
7fd40029
HMH
9112 thinkpad_acpi_module_exit();
9113 return ret;
9114 }
9115 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9116 &dev_attr_thinkpad_acpi_pdev_name);
9117 if (ret) {
e0c7dfe7 9118 printk(TPACPI_ERR
35ff8b9f 9119 "unable to create sysfs hwmon device attributes\n");
7fd40029
HMH
9120 thinkpad_acpi_module_exit();
9121 return ret;
9122 }
9123 tp_features.sensors_pdev_attrs_registered = 1;
9124 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
54ae1501
HMH
9125 if (IS_ERR(tpacpi_hwmon)) {
9126 ret = PTR_ERR(tpacpi_hwmon);
9127 tpacpi_hwmon = NULL;
e0c7dfe7 9128 printk(TPACPI_ERR "unable to register hwmon device\n");
54ae1501
HMH
9129 thinkpad_acpi_module_exit();
9130 return ret;
9131 }
8523ed6f 9132 mutex_init(&tpacpi_inputdev_send_mutex);
7f5d1cd6
HMH
9133 tpacpi_inputdev = input_allocate_device();
9134 if (!tpacpi_inputdev) {
e0c7dfe7 9135 printk(TPACPI_ERR "unable to allocate input device\n");
7f5d1cd6
HMH
9136 thinkpad_acpi_module_exit();
9137 return -ENOMEM;
9138 } else {
9139 /* Prepare input device, but don't register */
9140 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
e0c7dfe7 9141 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7f5d1cd6 9142 tpacpi_inputdev->id.bustype = BUS_HOST;
e28393c0 9143 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
7f5d1cd6
HMH
9144 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9145 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
d112ef95 9146 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
7f5d1cd6 9147 }
77775838
HMH
9148
9149 /* Init subdriver dependencies */
9150 tpacpi_detect_brightness_capabilities();
9151
9152 /* Init subdrivers */
a5763f22
HMH
9153 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9154 ret = ibm_init(&ibms_init[i]);
9155 if (ret >= 0 && *ibms_init[i].param)
9156 ret = ibms_init[i].data->write(ibms_init[i].param);
1da177e4 9157 if (ret < 0) {
1def7115 9158 thinkpad_acpi_module_exit();
1da177e4
LT
9159 return ret;
9160 }
9161 }
b589ea4c
HMH
9162
9163 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9164
7f5d1cd6
HMH
9165 ret = input_register_device(tpacpi_inputdev);
9166 if (ret < 0) {
e0c7dfe7 9167 printk(TPACPI_ERR "unable to register input device\n");
7f5d1cd6
HMH
9168 thinkpad_acpi_module_exit();
9169 return ret;
9170 } else {
9171 tp_features.input_device_registered = 1;
9172 }
1da177e4
LT
9173
9174 return 0;
9175}
9176
95e57ab2
HMH
9177MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9178
922fe097
HMH
9179/*
9180 * This will autoload the driver in almost every ThinkPad
9181 * in widespread use.
9182 *
9183 * Only _VERY_ old models, like the 240, 240x and 570 lack
9184 * the HKEY event interface.
9185 */
9186MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9187
f68080f8
HMH
9188/*
9189 * DMI matching for module autoloading
9190 *
9191 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9192 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9193 *
9194 * Only models listed in thinkwiki will be supported, so add yours
9195 * if it is not there yet.
9196 */
9197#define IBM_BIOS_MODULE_ALIAS(__type) \
b36a50f9 9198 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
f68080f8 9199
f68080f8
HMH
9200/* Ancient thinkpad BIOSes have to be identified by
9201 * BIOS type or model number, and there are far less
9202 * BIOS types than model numbers... */
922fe097 9203IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
f68080f8 9204
f68f53a2
HMH
9205MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9206MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
e0c7dfe7
HMH
9207MODULE_DESCRIPTION(TPACPI_DESC);
9208MODULE_VERSION(TPACPI_VERSION);
f68080f8
HMH
9209MODULE_LICENSE("GPL");
9210
1def7115
HMH
9211module_init(thinkpad_acpi_module_init);
9212module_exit(thinkpad_acpi_module_exit);