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