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