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