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