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