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