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