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