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