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