blob: 877a7802e830c4f96c943125d12aa69ebc5103a7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuh5d2eb142009-12-15 21:51:13 -020024#define TPACPI_VERSION "0.24"
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020025#define TPACPI_SYSFS_VERSION 0x020700
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
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.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#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>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000057#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020058#include <linux/kthread.h>
59#include <linux/freezer.h>
60#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020062
63#include <linux/nvram.h>
64#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020065#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020066#include <linux/sysfs.h>
67#include <linux/backlight.h>
68#include <linux/fb.h>
69#include <linux/platform_device.h>
70#include <linux/hwmon.h>
71#include <linux/hwmon-sysfs.h>
72#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030073#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030074#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020075#include <asm/uaccess.h>
76
77#include <linux/dmi.h>
78#include <linux/jiffies.h>
79#include <linux/workqueue.h>
80
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020081#include <sound/core.h>
82#include <sound/control.h>
83#include <sound/initval.h>
84
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020085#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020086
87#include <linux/pci_ids.h>
88
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020089
90/* ThinkPad CMOS commands */
91#define TP_CMOS_VOLUME_DOWN 0
92#define TP_CMOS_VOLUME_UP 1
93#define TP_CMOS_VOLUME_MUTE 2
94#define TP_CMOS_BRIGHTNESS_UP 4
95#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030096#define TP_CMOS_THINKLIGHT_ON 12
97#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020098
99/* NVRAM Addresses */
100enum tp_nvram_addr {
101 TP_NVRAM_ADDR_HK2 = 0x57,
102 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
103 TP_NVRAM_ADDR_VIDEO = 0x59,
104 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
105 TP_NVRAM_ADDR_MIXER = 0x60,
106};
107
108/* NVRAM bit masks */
109enum {
110 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
111 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
112 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
113 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
114 TP_NVRAM_MASK_THINKLIGHT = 0x10,
115 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
116 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
117 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
118 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
119 TP_NVRAM_MASK_MUTE = 0x40,
120 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
121 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
122 TP_NVRAM_POS_LEVEL_VOLUME = 0,
123};
124
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300125/* Misc NVRAM-related */
126enum {
127 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
128};
129
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200130/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200131#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300132#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200133
134/* Input IDs */
135#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
136#define TPACPI_HKEY_INPUT_VERSION 0x4101
137
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200138/* ACPI \WGSV commands */
139enum {
140 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
141 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
142 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
143 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
144};
145
146/* TP_ACPI_WGSV_GET_STATE bits */
147enum {
148 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
149 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
150 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
151 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
152 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
153 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
154 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
155 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
156 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
157 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
158};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200159
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300160/* HKEY events */
161enum tpacpi_hkey_event_t {
162 /* Hotkey-related */
163 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
164 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
165 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
166 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
167 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
168 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
169
170 /* Reasons for waking up from S3/S4 */
171 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
172 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
173 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
174 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
175 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
176 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
177
178 /* Auto-sleep after eject request */
179 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
180 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
181
182 /* Misc bay events */
183 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
184
185 /* User-interface events */
186 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
187 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
188 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
189 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
190 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
191 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
192 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
193
194 /* Thermal events */
195 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
196 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
197 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
198 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
199 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
200
201 /* Misc */
202 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
203};
204
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200205/****************************************************************************
206 * Main driver
207 */
208
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200209#define TPACPI_NAME "thinkpad"
210#define TPACPI_DESC "ThinkPad ACPI Extras"
211#define TPACPI_FILE TPACPI_NAME "_acpi"
212#define TPACPI_URL "http://ibm-acpi.sf.net/"
213#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200214
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200215#define TPACPI_PROC_DIR "ibm"
216#define TPACPI_ACPI_EVENT_PREFIX "ibm"
217#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300218#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200219#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200220
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300221#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300222#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300223
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200224#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200225
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000226/* printk headers */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200227#define TPACPI_LOG TPACPI_FILE ": "
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000228#define TPACPI_EMERG KERN_EMERG TPACPI_LOG
229#define TPACPI_ALERT KERN_ALERT TPACPI_LOG
230#define TPACPI_CRIT KERN_CRIT TPACPI_LOG
231#define TPACPI_ERR KERN_ERR TPACPI_LOG
232#define TPACPI_WARN KERN_WARNING TPACPI_LOG
233#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
234#define TPACPI_INFO KERN_INFO TPACPI_LOG
235#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200236
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000237/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200238#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000239#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200240#define TPACPI_DBG_INIT 0x0001
241#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000242#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000243#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000244#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000245#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200246#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200247
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200248#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
249#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
250#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200251
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200252
253/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200254 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200255 */
256
257struct ibm_struct;
258
259struct tp_acpi_drv_struct {
260 const struct acpi_device_id *hid;
261 struct acpi_driver *driver;
262
263 void (*notify) (struct ibm_struct *, u32);
264 acpi_handle *handle;
265 u32 type;
266 struct acpi_device *device;
267};
268
269struct ibm_struct {
270 char *name;
271
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200272 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200273 int (*write) (char *);
274 void (*exit) (void);
275 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200276 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200277 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200278
279 struct list_head all_drivers;
280
281 struct tp_acpi_drv_struct *acpi;
282
283 struct {
284 u8 acpi_driver_registered:1;
285 u8 acpi_notify_installed:1;
286 u8 proc_created:1;
287 u8 init_called:1;
288 u8 experimental:1;
289 } flags;
290};
291
292struct ibm_init_struct {
293 char param[32];
294
295 int (*init) (struct ibm_init_struct *);
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -0300296 mode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200297 struct ibm_struct *data;
298};
299
300static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200301 u32 bluetooth:1;
302 u32 hotkey:1;
303 u32 hotkey_mask:1;
304 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200305 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200306 u32 light:1;
307 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300308 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300309 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200310 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200311 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200312 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300313 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300314 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200315 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200316 u32 input_device_registered:1;
317 u32 platform_drv_registered:1;
318 u32 platform_drv_attrs_registered:1;
319 u32 sensors_pdrv_registered:1;
320 u32 sensors_pdrv_attrs_registered:1;
321 u32 sensors_pdev_attrs_registered:1;
322 u32 hotkey_poll_active:1;
323} tp_features;
324
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300325static struct {
326 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200327 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300328} tp_warned;
329
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200330struct thinkpad_id_data {
331 unsigned int vendor; /* ThinkPad vendor:
332 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
333
334 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
335 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
336
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300337 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200338 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300339 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
340 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200341
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300342 char *model_str; /* ThinkPad T43 */
343 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200344};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200345static struct thinkpad_id_data thinkpad_id;
346
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300347static enum {
348 TPACPI_LIFE_INIT = 0,
349 TPACPI_LIFE_RUNNING,
350 TPACPI_LIFE_EXITING,
351} tpacpi_lifecycle;
352
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200353static int experimental;
354static u32 dbg_level;
355
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300356static struct workqueue_struct *tpacpi_wq;
357
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000358enum led_status_t {
359 TPACPI_LED_OFF = 0,
360 TPACPI_LED_ON,
361 TPACPI_LED_BLINK,
362};
363
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300364/* Special LED class that can defer work */
365struct tpacpi_led_classdev {
366 struct led_classdev led_classdev;
367 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000368 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300369 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300370};
371
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300372/* brightness level capabilities */
373static unsigned int bright_maxlvl; /* 0 = unknown */
374
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200375#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
376static int dbg_wlswemul;
377static int tpacpi_wlsw_emulstate;
378static int dbg_bluetoothemul;
379static int tpacpi_bluetooth_emulstate;
380static int dbg_wwanemul;
381static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200382static int dbg_uwbemul;
383static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200384#endif
385
386
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000387/*************************************************************************
388 * Debugging helpers
389 */
390
391#define dbg_printk(a_dbg_level, format, arg...) \
392 do { if (dbg_level & (a_dbg_level)) \
393 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
394 } while (0)
395
396#ifdef CONFIG_THINKPAD_ACPI_DEBUG
397#define vdbg_printk dbg_printk
398static const char *str_supported(int is_supported);
399#else
400#define vdbg_printk(a_dbg_level, format, arg...) \
401 do { } while (0)
402#endif
403
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000404static void tpacpi_log_usertask(const char * const what)
405{
406 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
407 what, task_tgid_vnr(current));
408}
409
410#define tpacpi_disclose_usertask(what, format, arg...) \
411 do { \
412 if (unlikely( \
413 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
414 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
415 printk(TPACPI_DEBUG "%s: PID %d: " format, \
416 what, task_tgid_vnr(current), ## arg); \
417 } \
418 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000419
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300420/*
421 * Quirk handling helpers
422 *
423 * ThinkPad IDs and versions seen in the field so far
424 * are two-characters from the set [0-9A-Z], i.e. base 36.
425 *
426 * We use values well outside that range as specials.
427 */
428
429#define TPACPI_MATCH_ANY 0xffffU
430#define TPACPI_MATCH_UNKNOWN 0U
431
432/* TPID('1', 'Y') == 0x5931 */
433#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
434
435#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
436 { .vendor = PCI_VENDOR_ID_IBM, \
437 .bios = TPID(__id1, __id2), \
438 .ec = TPACPI_MATCH_ANY, \
439 .quirks = (__quirk) }
440
441#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
442 { .vendor = PCI_VENDOR_ID_LENOVO, \
443 .bios = TPID(__id1, __id2), \
444 .ec = TPACPI_MATCH_ANY, \
445 .quirks = (__quirk) }
446
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200447#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_LENOVO, \
449 .bios = TPACPI_MATCH_ANY, \
450 .ec = TPID(__id1, __id2), \
451 .quirks = (__quirk) }
452
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300453struct tpacpi_quirk {
454 unsigned int vendor;
455 u16 bios;
456 u16 ec;
457 unsigned long quirks;
458};
459
460/**
461 * tpacpi_check_quirks() - search BIOS/EC version on a list
462 * @qlist: array of &struct tpacpi_quirk
463 * @qlist_size: number of elements in @qlist
464 *
465 * Iterates over a quirks list until one is found that matches the
466 * ThinkPad's vendor, BIOS and EC model.
467 *
468 * Returns 0 if nothing matches, otherwise returns the quirks field of
469 * the matching &struct tpacpi_quirk entry.
470 *
471 * The match criteria is: vendor, ec and bios much match.
472 */
473static unsigned long __init tpacpi_check_quirks(
474 const struct tpacpi_quirk *qlist,
475 unsigned int qlist_size)
476{
477 while (qlist_size) {
478 if ((qlist->vendor == thinkpad_id.vendor ||
479 qlist->vendor == TPACPI_MATCH_ANY) &&
480 (qlist->bios == thinkpad_id.bios_model ||
481 qlist->bios == TPACPI_MATCH_ANY) &&
482 (qlist->ec == thinkpad_id.ec_model ||
483 qlist->ec == TPACPI_MATCH_ANY))
484 return qlist->quirks;
485
486 qlist_size--;
487 qlist++;
488 }
489 return 0;
490}
491
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300492static inline bool __pure __init tpacpi_is_lenovo(void)
493{
494 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
495}
496
497static inline bool __pure __init tpacpi_is_ibm(void)
498{
499 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
500}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300501
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300502/****************************************************************************
503 ****************************************************************************
504 *
505 * ACPI Helpers and device model
506 *
507 ****************************************************************************
508 ****************************************************************************/
509
510/*************************************************************************
511 * ACPI basic handles
512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300514static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300515static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200517#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 static acpi_handle object##_handle; \
519 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400520 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 static char *object##_paths[] = { paths }
522
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200523TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
524TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200526TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
527 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400528 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
529 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300530 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400531
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200532TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400533 "^HKEY", /* R30, R31 */
534 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300535 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400536
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200537TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
538 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
539 "\\_SB.PCI0.VID0", /* 770e */
540 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
Henrique de Moraes Holschuha3189302010-05-16 19:45:26 -0300541 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200542 "\\_SB.PCI0.AGP.VID", /* all others */
543 ); /* R30, R31 */
544
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400545
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300546/*************************************************************************
547 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200548 */
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550static int acpi_evalf(acpi_handle handle,
551 void *res, char *method, char *fmt, ...)
552{
553 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400554 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200555 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400556 struct acpi_buffer result, *resultp;
557 union acpi_object out_obj;
558 acpi_status status;
559 va_list ap;
560 char res_type;
561 int success;
562 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200565 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return 0;
567 }
568
569 if (*fmt == 'q') {
570 quiet = 1;
571 fmt++;
572 } else
573 quiet = 0;
574
575 res_type = *(fmt++);
576
577 params.count = 0;
578 params.pointer = &in_objs[0];
579
580 va_start(ap, fmt);
581 while (*fmt) {
582 char c = *(fmt++);
583 switch (c) {
584 case 'd': /* int */
585 in_objs[params.count].integer.value = va_arg(ap, int);
586 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
587 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400588 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200590 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 "with invalid format character '%c'\n", c);
592 return 0;
593 }
594 }
595 va_end(ap);
596
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400597 if (res_type != 'v') {
598 result.length = sizeof(out_obj);
599 result.pointer = &out_obj;
600 resultp = &result;
601 } else
602 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400604 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400607 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (res)
609 *(int *)res = out_obj.integer.value;
610 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
611 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400612 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 success = status == AE_OK;
614 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400615 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200617 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 "with invalid format character '%c'\n", res_type);
619 return 0;
620 }
621
622 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200623 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 method, fmt0, status);
625
626 return success;
627}
628
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200629static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300630{
631 int v;
632
633 if (ecrd_handle) {
634 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
635 return 0;
636 *p = v;
637 } else {
638 if (ec_read(i, p) < 0)
639 return 0;
640 }
641
642 return 1;
643}
644
645static int acpi_ec_write(int i, u8 v)
646{
647 if (ecwr_handle) {
648 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
649 return 0;
650 } else {
651 if (ec_write(i, v) < 0)
652 return 0;
653 }
654
655 return 1;
656}
657
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300658static int issue_thinkpad_cmos_command(int cmos_cmd)
659{
660 if (!cmos_handle)
661 return -ENXIO;
662
663 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
664 return -EIO;
665
666 return 0;
667}
668
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300669/*************************************************************************
670 * ACPI device model
671 */
672
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200673#define TPACPI_ACPIHANDLE_INIT(object) \
674 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200675 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
676
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300677static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300678 acpi_handle *handle, acpi_handle parent,
679 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300680{
681 int i;
682 acpi_status status;
683
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300684 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
685 name);
686
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300687 for (i = 0; i < num_paths; i++) {
688 status = acpi_get_handle(parent, paths[i], handle);
689 if (ACPI_SUCCESS(status)) {
690 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300691 dbg_printk(TPACPI_DBG_INIT,
692 "Found ACPI handle %s for %s\n",
693 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300694 return;
695 }
696 }
697
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300698 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
699 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300700 *handle = NULL;
701}
702
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300703static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
704 u32 level, void *context, void **return_value)
705{
706 *(acpi_handle *)return_value = handle;
707
708 return AE_CTRL_TERMINATE;
709}
710
711static void __init tpacpi_acpi_handle_locate(const char *name,
712 const char *hid,
713 acpi_handle *handle)
714{
715 acpi_status status;
716 acpi_handle device_found;
717
718 BUG_ON(!name || !hid || !handle);
719 vdbg_printk(TPACPI_DBG_INIT,
720 "trying to locate ACPI handle for %s, using HID %s\n",
721 name, hid);
722
723 memset(&device_found, 0, sizeof(device_found));
724 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
725 (void *)name, &device_found);
726
727 *handle = NULL;
728
729 if (ACPI_SUCCESS(status)) {
730 *handle = device_found;
731 dbg_printk(TPACPI_DBG_INIT,
732 "Found ACPI handle for %s\n", name);
733 } else {
734 vdbg_printk(TPACPI_DBG_INIT,
735 "Could not locate an ACPI handle for %s: %s\n",
736 name, acpi_format_exception(status));
737 }
738}
739
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300740static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300741{
742 struct ibm_struct *ibm = data;
743
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300744 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
745 return;
746
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300747 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300748 return;
749
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300750 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300751}
752
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300753static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300754{
755 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300756 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300757
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300758 BUG_ON(!ibm->acpi);
759
760 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300761 return 0;
762
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300763 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300764 "setting up ACPI notify for %s\n", ibm->name);
765
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300766 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
767 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200768 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300769 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300770 return -ENODEV;
771 }
772
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700773 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300774 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200775 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300776 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300777
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300778 status = acpi_install_notify_handler(*ibm->acpi->handle,
779 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300780 if (ACPI_FAILURE(status)) {
781 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200782 printk(TPACPI_NOTICE
783 "another device driver is already "
784 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300785 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200786 printk(TPACPI_ERR
787 "acpi_install_notify_handler(%s) failed: %d\n",
788 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300789 }
790 return -ENODEV;
791 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300792 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300793 return 0;
794}
795
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300796static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300797{
798 return 0;
799}
800
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300801static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300802{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300803 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300804
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300805 dbg_printk(TPACPI_DBG_INIT,
806 "registering %s as an ACPI driver\n", ibm->name);
807
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300808 BUG_ON(!ibm->acpi);
809
810 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
811 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300812 printk(TPACPI_ERR
813 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300814 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300815 }
816
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200817 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300818 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200819
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300820 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300821
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300822 rc = acpi_bus_register_driver(ibm->acpi->driver);
823 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200824 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200825 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300826 kfree(ibm->acpi->driver);
827 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300828 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300829 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300830
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300831 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300832}
833
834
835/****************************************************************************
836 ****************************************************************************
837 *
838 * Procfs Helpers
839 *
840 ****************************************************************************
841 ****************************************************************************/
842
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200843static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300844{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200845 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300846
847 if (!ibm || !ibm->read)
848 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200849 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300850}
851
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200852static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300853{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200854 return single_open(file, dispatch_proc_show, PDE(inode)->data);
855}
856
857static ssize_t dispatch_proc_write(struct file *file,
858 const char __user *userbuf,
859 size_t count, loff_t *pos)
860{
861 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300862 char *kernbuf;
863 int ret;
864
865 if (!ibm || !ibm->write)
866 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300867 if (count > PAGE_SIZE - 2)
868 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300869
870 kernbuf = kmalloc(count + 2, GFP_KERNEL);
871 if (!kernbuf)
872 return -ENOMEM;
873
874 if (copy_from_user(kernbuf, userbuf, count)) {
875 kfree(kernbuf);
876 return -EFAULT;
877 }
878
879 kernbuf[count] = 0;
880 strcat(kernbuf, ",");
881 ret = ibm->write(kernbuf);
882 if (ret == 0)
883 ret = count;
884
885 kfree(kernbuf);
886
887 return ret;
888}
889
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200890static const struct file_operations dispatch_proc_fops = {
891 .owner = THIS_MODULE,
892 .open = dispatch_proc_open,
893 .read = seq_read,
894 .llseek = seq_lseek,
895 .release = single_release,
896 .write = dispatch_proc_write,
897};
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899static char *next_cmd(char **cmds)
900{
901 char *start = *cmds;
902 char *end;
903
904 while ((end = strchr(start, ',')) && end == start)
905 start = end + 1;
906
907 if (!end)
908 return NULL;
909
910 *end = 0;
911 *cmds = end + 1;
912 return start;
913}
914
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300915
916/****************************************************************************
917 ****************************************************************************
918 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300919 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300920 *
921 ****************************************************************************
922 ****************************************************************************/
923
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300924static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300925static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700926static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300927static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300928static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200929static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300930
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200931static int tpacpi_suspend_handler(struct platform_device *pdev,
932 pm_message_t state)
933{
934 struct ibm_struct *ibm, *itmp;
935
936 list_for_each_entry_safe(ibm, itmp,
937 &tpacpi_all_drivers,
938 all_drivers) {
939 if (ibm->suspend)
940 (ibm->suspend)(state);
941 }
942
943 return 0;
944}
945
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300946static int tpacpi_resume_handler(struct platform_device *pdev)
947{
948 struct ibm_struct *ibm, *itmp;
949
950 list_for_each_entry_safe(ibm, itmp,
951 &tpacpi_all_drivers,
952 all_drivers) {
953 if (ibm->resume)
954 (ibm->resume)();
955 }
956
957 return 0;
958}
959
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200960static void tpacpi_shutdown_handler(struct platform_device *pdev)
961{
962 struct ibm_struct *ibm, *itmp;
963
964 list_for_each_entry_safe(ibm, itmp,
965 &tpacpi_all_drivers,
966 all_drivers) {
967 if (ibm->shutdown)
968 (ibm->shutdown)();
969 }
970}
971
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300972static struct platform_driver tpacpi_pdriver = {
973 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200974 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300975 .owner = THIS_MODULE,
976 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200977 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300978 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200979 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300980};
981
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300982static struct platform_driver tpacpi_hwmon_pdriver = {
983 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200984 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300985 .owner = THIS_MODULE,
986 },
987};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300988
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300989/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300990 * sysfs support helpers
991 */
992
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200993struct attribute_set {
994 unsigned int members, max_members;
995 struct attribute_group group;
996};
997
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300998struct attribute_set_obj {
999 struct attribute_set s;
1000 struct attribute *a;
1001} __attribute__((packed));
1002
1003static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001004 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001005{
1006 struct attribute_set_obj *sobj;
1007
1008 if (max_members == 0)
1009 return NULL;
1010
1011 /* Allocates space for implicit NULL at the end too */
1012 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1013 max_members * sizeof(struct attribute *),
1014 GFP_KERNEL);
1015 if (!sobj)
1016 return NULL;
1017 sobj->s.max_members = max_members;
1018 sobj->s.group.attrs = &sobj->a;
1019 sobj->s.group.name = name;
1020
1021 return &sobj->s;
1022}
1023
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001024#define destroy_attr_set(_set) \
1025 kfree(_set);
1026
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001027/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001028static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001029{
1030 if (!s || !attr)
1031 return -EINVAL;
1032
1033 if (s->members >= s->max_members)
1034 return -ENOMEM;
1035
1036 s->group.attrs[s->members] = attr;
1037 s->members++;
1038
1039 return 0;
1040}
1041
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001042static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001043 struct attribute **attr,
1044 unsigned int count)
1045{
1046 int i, res;
1047
1048 for (i = 0; i < count; i++) {
1049 res = add_to_attr_set(s, attr[i]);
1050 if (res)
1051 return res;
1052 }
1053
1054 return 0;
1055}
1056
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001057static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001058{
1059 sysfs_remove_group(kobj, &s->group);
1060 destroy_attr_set(s);
1061}
1062
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001063#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1064 sysfs_create_group(_kobj, &_attr_set->group)
1065
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001066static int parse_strtoul(const char *buf,
1067 unsigned long max, unsigned long *value)
1068{
1069 char *endp;
1070
André Goddard Rosae7d28602009-12-14 18:01:06 -08001071 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1072 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001073 if (*endp || *value > max)
1074 return -EINVAL;
1075
1076 return 0;
1077}
1078
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001079static void tpacpi_disable_brightness_delay(void)
1080{
1081 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1082 printk(TPACPI_NOTICE
1083 "ACPI backlight control delay disabled\n");
1084}
1085
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001086static void printk_deprecated_attribute(const char * const what,
1087 const char * const details)
1088{
1089 tpacpi_log_usertask("deprecated sysfs attribute");
1090 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1091 "will be removed. %s\n",
1092 what, details);
1093}
1094
Johannes Berg19d337d2009-06-02 13:01:37 +02001095/*************************************************************************
1096 * rfkill and radio control support helpers
1097 */
1098
1099/*
1100 * ThinkPad-ACPI firmware handling model:
1101 *
1102 * WLSW (master wireless switch) is event-driven, and is common to all
1103 * firmware-controlled radios. It cannot be controlled, just monitored,
1104 * as expected. It overrides all radio state in firmware
1105 *
1106 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1107 * (TODO: verify how WLSW interacts with the returned radio state).
1108 *
1109 * The only time there are shadow radio state changes, is when
1110 * masked-off hotkeys are used.
1111 */
1112
1113/*
1114 * Internal driver API for radio state:
1115 *
1116 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1117 * bool: true means radio blocked (off)
1118 */
1119enum tpacpi_rfkill_state {
1120 TPACPI_RFK_RADIO_OFF = 0,
1121 TPACPI_RFK_RADIO_ON
1122};
1123
1124/* rfkill switches */
1125enum tpacpi_rfk_id {
1126 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1127 TPACPI_RFK_WWAN_SW_ID,
1128 TPACPI_RFK_UWB_SW_ID,
1129 TPACPI_RFK_SW_MAX
1130};
1131
1132static const char *tpacpi_rfkill_names[] = {
1133 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1134 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1135 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1136 [TPACPI_RFK_SW_MAX] = NULL
1137};
1138
1139/* ThinkPad-ACPI rfkill subdriver */
1140struct tpacpi_rfk {
1141 struct rfkill *rfkill;
1142 enum tpacpi_rfk_id id;
1143 const struct tpacpi_rfk_ops *ops;
1144};
1145
1146struct tpacpi_rfk_ops {
1147 /* firmware interface */
1148 int (*get_status)(void);
1149 int (*set_status)(const enum tpacpi_rfkill_state);
1150};
1151
1152static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1153
1154/* Query FW and update rfkill sw state for a given rfkill switch */
1155static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1156{
1157 int status;
1158
1159 if (!tp_rfk)
1160 return -ENODEV;
1161
1162 status = (tp_rfk->ops->get_status)();
1163 if (status < 0)
1164 return status;
1165
1166 rfkill_set_sw_state(tp_rfk->rfkill,
1167 (status == TPACPI_RFK_RADIO_OFF));
1168
1169 return status;
1170}
1171
1172/* Query FW and update rfkill sw state for all rfkill switches */
1173static void tpacpi_rfk_update_swstate_all(void)
1174{
1175 unsigned int i;
1176
1177 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1178 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1179}
1180
1181/*
1182 * Sync the HW-blocking state of all rfkill switches,
1183 * do notice it causes the rfkill core to schedule uevents
1184 */
1185static void tpacpi_rfk_update_hwblock_state(bool blocked)
1186{
1187 unsigned int i;
1188 struct tpacpi_rfk *tp_rfk;
1189
1190 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1191 tp_rfk = tpacpi_rfkill_switches[i];
1192 if (tp_rfk) {
1193 if (rfkill_set_hw_state(tp_rfk->rfkill,
1194 blocked)) {
1195 /* ignore -- we track sw block */
1196 }
1197 }
1198 }
1199}
1200
1201/* Call to get the WLSW state from the firmware */
1202static int hotkey_get_wlsw(void);
1203
1204/* Call to query WLSW state and update all rfkill switches */
1205static bool tpacpi_rfk_check_hwblock_state(void)
1206{
1207 int res = hotkey_get_wlsw();
1208 int hw_blocked;
1209
1210 /* When unknown or unsupported, we have to assume it is unblocked */
1211 if (res < 0)
1212 return false;
1213
1214 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1215 tpacpi_rfk_update_hwblock_state(hw_blocked);
1216
1217 return hw_blocked;
1218}
1219
1220static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1221{
1222 struct tpacpi_rfk *tp_rfk = data;
1223 int res;
1224
1225 dbg_printk(TPACPI_DBG_RFKILL,
1226 "request to change radio state to %s\n",
1227 blocked ? "blocked" : "unblocked");
1228
1229 /* try to set radio state */
1230 res = (tp_rfk->ops->set_status)(blocked ?
1231 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1232
1233 /* and update the rfkill core with whatever the FW really did */
1234 tpacpi_rfk_update_swstate(tp_rfk);
1235
1236 return (res < 0) ? res : 0;
1237}
1238
1239static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1240 .set_block = tpacpi_rfk_hook_set_block,
1241};
1242
1243static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1244 const struct tpacpi_rfk_ops *tp_rfkops,
1245 const enum rfkill_type rfktype,
1246 const char *name,
1247 const bool set_default)
1248{
1249 struct tpacpi_rfk *atp_rfk;
1250 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001251 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001252 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001253 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001254
1255 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1256
Johannes Berg19d337d2009-06-02 13:01:37 +02001257 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1258 if (atp_rfk)
1259 atp_rfk->rfkill = rfkill_alloc(name,
1260 &tpacpi_pdev->dev,
1261 rfktype,
1262 &tpacpi_rfk_rfkill_ops,
1263 atp_rfk);
1264 if (!atp_rfk || !atp_rfk->rfkill) {
1265 printk(TPACPI_ERR
1266 "failed to allocate memory for rfkill class\n");
1267 kfree(atp_rfk);
1268 return -ENOMEM;
1269 }
1270
1271 atp_rfk->id = id;
1272 atp_rfk->ops = tp_rfkops;
1273
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001274 sw_status = (tp_rfkops->get_status)();
1275 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001276 printk(TPACPI_ERR
1277 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001278 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001279 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001280 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001281 if (set_default) {
1282 /* try to keep the initial state, since we ask the
1283 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001284 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001285 }
1286 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001287 hw_state = tpacpi_rfk_check_hwblock_state();
1288 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001289
1290 res = rfkill_register(atp_rfk->rfkill);
1291 if (res < 0) {
1292 printk(TPACPI_ERR
1293 "failed to register %s rfkill switch: %d\n",
1294 name, res);
1295 rfkill_destroy(atp_rfk->rfkill);
1296 kfree(atp_rfk);
1297 return res;
1298 }
1299
1300 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001301
1302 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1303 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001304 return 0;
1305}
1306
1307static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1308{
1309 struct tpacpi_rfk *tp_rfk;
1310
1311 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1312
1313 tp_rfk = tpacpi_rfkill_switches[id];
1314 if (tp_rfk) {
1315 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001316 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001317 tpacpi_rfkill_switches[id] = NULL;
1318 kfree(tp_rfk);
1319 }
1320}
1321
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001322static void printk_deprecated_rfkill_attribute(const char * const what)
1323{
1324 printk_deprecated_attribute(what,
1325 "Please switch to generic rfkill before year 2010");
1326}
1327
Johannes Berg19d337d2009-06-02 13:01:37 +02001328/* sysfs <radio> enable ------------------------------------------------ */
1329static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1330 struct device_attribute *attr,
1331 char *buf)
1332{
1333 int status;
1334
1335 printk_deprecated_rfkill_attribute(attr->attr.name);
1336
1337 /* This is in the ABI... */
1338 if (tpacpi_rfk_check_hwblock_state()) {
1339 status = TPACPI_RFK_RADIO_OFF;
1340 } else {
1341 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1342 if (status < 0)
1343 return status;
1344 }
1345
1346 return snprintf(buf, PAGE_SIZE, "%d\n",
1347 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1348}
1349
1350static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1351 struct device_attribute *attr,
1352 const char *buf, size_t count)
1353{
1354 unsigned long t;
1355 int res;
1356
1357 printk_deprecated_rfkill_attribute(attr->attr.name);
1358
1359 if (parse_strtoul(buf, 1, &t))
1360 return -EINVAL;
1361
1362 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1363
1364 /* This is in the ABI... */
1365 if (tpacpi_rfk_check_hwblock_state() && !!t)
1366 return -EPERM;
1367
1368 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1369 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1370 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1371
1372 return (res < 0) ? res : count;
1373}
1374
1375/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001376static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001377{
Johannes Berg19d337d2009-06-02 13:01:37 +02001378 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001379 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001380 else {
1381 int status;
1382
1383 /* This is in the ABI... */
1384 if (tpacpi_rfk_check_hwblock_state()) {
1385 status = TPACPI_RFK_RADIO_OFF;
1386 } else {
1387 status = tpacpi_rfk_update_swstate(
1388 tpacpi_rfkill_switches[id]);
1389 if (status < 0)
1390 return status;
1391 }
1392
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001393 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001394 (status == TPACPI_RFK_RADIO_ON) ?
1395 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001396 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001397 }
1398
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001399 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001400}
1401
1402static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1403{
1404 char *cmd;
1405 int status = -1;
1406 int res = 0;
1407
1408 if (id >= TPACPI_RFK_SW_MAX)
1409 return -ENODEV;
1410
1411 while ((cmd = next_cmd(&buf))) {
1412 if (strlencmp(cmd, "enable") == 0)
1413 status = TPACPI_RFK_RADIO_ON;
1414 else if (strlencmp(cmd, "disable") == 0)
1415 status = TPACPI_RFK_RADIO_OFF;
1416 else
1417 return -EINVAL;
1418 }
1419
1420 if (status != -1) {
1421 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1422 (status == TPACPI_RFK_RADIO_ON) ?
1423 "enable" : "disable",
1424 tpacpi_rfkill_names[id]);
1425 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1426 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1427 }
1428
1429 return res;
1430}
1431
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001432/*************************************************************************
1433 * thinkpad-acpi driver attributes
1434 */
1435
1436/* interface_version --------------------------------------------------- */
1437static ssize_t tpacpi_driver_interface_version_show(
1438 struct device_driver *drv,
1439 char *buf)
1440{
1441 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1442}
1443
1444static DRIVER_ATTR(interface_version, S_IRUGO,
1445 tpacpi_driver_interface_version_show, NULL);
1446
1447/* debug_level --------------------------------------------------------- */
1448static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1449 char *buf)
1450{
1451 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1452}
1453
1454static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1455 const char *buf, size_t count)
1456{
1457 unsigned long t;
1458
1459 if (parse_strtoul(buf, 0xffff, &t))
1460 return -EINVAL;
1461
1462 dbg_level = t;
1463
1464 return count;
1465}
1466
1467static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1468 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1469
1470/* version ------------------------------------------------------------- */
1471static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1472 char *buf)
1473{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001474 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1475 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001476}
1477
1478static DRIVER_ATTR(version, S_IRUGO,
1479 tpacpi_driver_version_show, NULL);
1480
1481/* --------------------------------------------------------------------- */
1482
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001483#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1484
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001485/* wlsw_emulstate ------------------------------------------------------ */
1486static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1487 char *buf)
1488{
1489 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1490}
1491
1492static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1493 const char *buf, size_t count)
1494{
1495 unsigned long t;
1496
1497 if (parse_strtoul(buf, 1, &t))
1498 return -EINVAL;
1499
Johannes Berg19d337d2009-06-02 13:01:37 +02001500 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001501 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001502 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1503 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001504
1505 return count;
1506}
1507
1508static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1509 tpacpi_driver_wlsw_emulstate_show,
1510 tpacpi_driver_wlsw_emulstate_store);
1511
1512/* bluetooth_emulstate ------------------------------------------------- */
1513static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1514 struct device_driver *drv,
1515 char *buf)
1516{
1517 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1518}
1519
1520static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1521 struct device_driver *drv,
1522 const char *buf, size_t count)
1523{
1524 unsigned long t;
1525
1526 if (parse_strtoul(buf, 1, &t))
1527 return -EINVAL;
1528
1529 tpacpi_bluetooth_emulstate = !!t;
1530
1531 return count;
1532}
1533
1534static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1535 tpacpi_driver_bluetooth_emulstate_show,
1536 tpacpi_driver_bluetooth_emulstate_store);
1537
1538/* wwan_emulstate ------------------------------------------------- */
1539static ssize_t tpacpi_driver_wwan_emulstate_show(
1540 struct device_driver *drv,
1541 char *buf)
1542{
1543 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1544}
1545
1546static ssize_t tpacpi_driver_wwan_emulstate_store(
1547 struct device_driver *drv,
1548 const char *buf, size_t count)
1549{
1550 unsigned long t;
1551
1552 if (parse_strtoul(buf, 1, &t))
1553 return -EINVAL;
1554
1555 tpacpi_wwan_emulstate = !!t;
1556
1557 return count;
1558}
1559
1560static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1561 tpacpi_driver_wwan_emulstate_show,
1562 tpacpi_driver_wwan_emulstate_store);
1563
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001564/* uwb_emulstate ------------------------------------------------- */
1565static ssize_t tpacpi_driver_uwb_emulstate_show(
1566 struct device_driver *drv,
1567 char *buf)
1568{
1569 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1570}
1571
1572static ssize_t tpacpi_driver_uwb_emulstate_store(
1573 struct device_driver *drv,
1574 const char *buf, size_t count)
1575{
1576 unsigned long t;
1577
1578 if (parse_strtoul(buf, 1, &t))
1579 return -EINVAL;
1580
1581 tpacpi_uwb_emulstate = !!t;
1582
1583 return count;
1584}
1585
1586static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1587 tpacpi_driver_uwb_emulstate_show,
1588 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001589#endif
1590
1591/* --------------------------------------------------------------------- */
1592
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001593static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001594 &driver_attr_debug_level, &driver_attr_version,
1595 &driver_attr_interface_version,
1596};
1597
1598static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1599{
1600 int i, res;
1601
1602 i = 0;
1603 res = 0;
1604 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1605 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1606 i++;
1607 }
1608
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001609#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1610 if (!res && dbg_wlswemul)
1611 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1612 if (!res && dbg_bluetoothemul)
1613 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1614 if (!res && dbg_wwanemul)
1615 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001616 if (!res && dbg_uwbemul)
1617 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001618#endif
1619
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001620 return res;
1621}
1622
1623static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1624{
1625 int i;
1626
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001627 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001628 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001629
1630#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1631 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1632 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1633 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001634 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001635#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001636}
1637
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001638/*************************************************************************
1639 * Firmware Data
1640 */
1641
1642/*
1643 * Table of recommended minimum BIOS versions
1644 *
1645 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001646 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001647 * bugs and bad ACPI behaviour on older versions
1648 *
1649 * 2. BIOS or EC fw with known bugs that trigger on Linux
1650 *
1651 * 3. BIOS with known reduced functionality in older versions
1652 *
1653 * We recommend the latest BIOS and EC version.
1654 * We only support the latest BIOS and EC fw version as a rule.
1655 *
1656 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1657 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001658 *
1659 * WARNING: we use this table also to detect that the machine is
1660 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001661 */
1662
1663#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1664 { .vendor = (__v), \
1665 .bios = TPID(__id1, __id2), \
1666 .ec = TPACPI_MATCH_ANY, \
1667 .quirks = TPACPI_MATCH_ANY << 16 \
1668 | (__bv1) << 8 | (__bv2) }
1669
1670#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001671 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001672 { .vendor = (__v), \
1673 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001674 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001675 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1676 | (__bv1) << 8 | (__bv2) }
1677
1678#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1679 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1680
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001681/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001682#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1683 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001684 __bv1, __bv2, TPID(__id1, __id2), \
1685 __ev1, __ev2), \
1686 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1687 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1688 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001689
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001690/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001691#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1692 __eid1, __eid2, __ev1, __ev2) \
1693 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001694 __bv1, __bv2, TPID(__eid1, __eid2), \
1695 __ev1, __ev2), \
1696 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1697 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1698 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001699
1700#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1701 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1702
1703#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1704 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001705 __bv1, __bv2, TPID(__id1, __id2), \
1706 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001707
1708#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1709 __eid1, __eid2, __ev1, __ev2) \
1710 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001711 __bv1, __bv2, TPID(__eid1, __eid2), \
1712 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001713
1714static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1715 /* Numeric models ------------------ */
1716 /* FW MODEL BIOS VERS */
1717 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1718 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1719 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1720 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1721 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1722 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1723 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1724 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1725 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1726
1727 /* A-series ------------------------- */
1728 /* FW MODEL BIOS VERS EC VERS */
1729 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1730 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1731 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1732 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1733 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1734 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1735 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1736 TPV_QI0('1', '3', '2', '0'), /* A22m */
1737 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1738 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1739 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1740
1741 /* G-series ------------------------- */
1742 /* FW MODEL BIOS VERS */
1743 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1744 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1745
1746 /* R-series, T-series --------------- */
1747 /* FW MODEL BIOS VERS EC VERS */
1748 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1749 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1750 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1751 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1752 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1753 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1754 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1755 T40/p, T41/p, T42/p (1) */
1756 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1757 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1758 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1759 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1760
1761 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1762 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1763 TPV_QI0('1', '6', '3', '2'), /* T22 */
1764 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1765 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1766 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1767
1768 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1769 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001770 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001771
1772 /* BIOS FW BIOS VERS EC FW EC VERS */
1773 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1774 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1775
1776 /* X-series ------------------------- */
1777 /* FW MODEL BIOS VERS EC VERS */
1778 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1779 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1780 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1781 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1782 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1783 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1784 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1785
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001786 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1787 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001788
1789 /* (0) - older versions lack DMI EC fw string and functionality */
1790 /* (1) - older versions known to lack functionality */
1791};
1792
1793#undef TPV_QL1
1794#undef TPV_QL0
1795#undef TPV_QI2
1796#undef TPV_QI1
1797#undef TPV_QI0
1798#undef TPV_Q_X
1799#undef TPV_Q
1800
1801static void __init tpacpi_check_outdated_fw(void)
1802{
1803 unsigned long fwvers;
1804 u16 ec_version, bios_version;
1805
1806 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1807 ARRAY_SIZE(tpacpi_bios_version_qtable));
1808
1809 if (!fwvers)
1810 return;
1811
1812 bios_version = fwvers & 0xffffU;
1813 ec_version = (fwvers >> 16) & 0xffffU;
1814
1815 /* note that unknown versions are set to 0x0000 and we use that */
1816 if ((bios_version > thinkpad_id.bios_release) ||
1817 (ec_version > thinkpad_id.ec_release &&
1818 ec_version != TPACPI_MATCH_ANY)) {
1819 /*
1820 * The changelogs would let us track down the exact
1821 * reason, but it is just too much of a pain to track
1822 * it. We only list BIOSes that are either really
1823 * broken, or really stable to begin with, so it is
1824 * best if the user upgrades the firmware anyway.
1825 */
1826 printk(TPACPI_WARN
1827 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1828 printk(TPACPI_WARN
1829 "WARNING: This firmware may be missing critical bug "
1830 "fixes and/or important features\n");
1831 }
1832}
1833
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001834static bool __init tpacpi_is_fw_known(void)
1835{
1836 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1837 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1838}
1839
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001840/****************************************************************************
1841 ****************************************************************************
1842 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001843 * Subdrivers
1844 *
1845 ****************************************************************************
1846 ****************************************************************************/
1847
1848/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001849 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001850 */
1851
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001852static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001854 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1855 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
1858
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001859static struct ibm_struct thinkpad_acpi_driver_data = {
1860 .name = "driver",
1861 .read = thinkpad_acpi_driver_read,
1862};
1863
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001864/*************************************************************************
1865 * Hotkey subdriver
1866 */
1867
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001868/*
1869 * ThinkPad firmware event model
1870 *
1871 * The ThinkPad firmware has two main event interfaces: normal ACPI
1872 * notifications (which follow the ACPI standard), and a private event
1873 * interface.
1874 *
1875 * The private event interface also issues events for the hotkeys. As
1876 * the driver gained features, the event handling code ended up being
1877 * built around the hotkey subdriver. This will need to be refactored
1878 * to a more formal event API eventually.
1879 *
1880 * Some "hotkeys" are actually supposed to be used as event reports,
1881 * such as "brightness has changed", "volume has changed", depending on
1882 * the ThinkPad model and how the firmware is operating.
1883 *
1884 * Unlike other classes, hotkey-class events have mask/unmask control on
1885 * non-ancient firmware. However, how it behaves changes a lot with the
1886 * firmware model and version.
1887 */
1888
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001889enum { /* hot key scan codes (derived from ACPI DSDT) */
1890 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1891 TP_ACPI_HOTKEYSCAN_FNF2,
1892 TP_ACPI_HOTKEYSCAN_FNF3,
1893 TP_ACPI_HOTKEYSCAN_FNF4,
1894 TP_ACPI_HOTKEYSCAN_FNF5,
1895 TP_ACPI_HOTKEYSCAN_FNF6,
1896 TP_ACPI_HOTKEYSCAN_FNF7,
1897 TP_ACPI_HOTKEYSCAN_FNF8,
1898 TP_ACPI_HOTKEYSCAN_FNF9,
1899 TP_ACPI_HOTKEYSCAN_FNF10,
1900 TP_ACPI_HOTKEYSCAN_FNF11,
1901 TP_ACPI_HOTKEYSCAN_FNF12,
1902 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1903 TP_ACPI_HOTKEYSCAN_FNINSERT,
1904 TP_ACPI_HOTKEYSCAN_FNDELETE,
1905 TP_ACPI_HOTKEYSCAN_FNHOME,
1906 TP_ACPI_HOTKEYSCAN_FNEND,
1907 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1908 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1909 TP_ACPI_HOTKEYSCAN_FNSPACE,
1910 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1911 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1912 TP_ACPI_HOTKEYSCAN_MUTE,
1913 TP_ACPI_HOTKEYSCAN_THINKPAD,
1914};
1915
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001916enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001917 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1918 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1919};
1920
1921enum { /* Positions of some of the keys in hotkey masks */
1922 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1923 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1924 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1925 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1926 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1927 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1928 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1929 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1930 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1931 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1932 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1933};
1934
1935enum { /* NVRAM to ACPI HKEY group map */
1936 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1937 TP_ACPI_HKEY_ZOOM_MASK |
1938 TP_ACPI_HKEY_DISPSWTCH_MASK |
1939 TP_ACPI_HKEY_HIBERNATE_MASK,
1940 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1941 TP_ACPI_HKEY_BRGHTDWN_MASK,
1942 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1943 TP_ACPI_HKEY_VOLDWN_MASK |
1944 TP_ACPI_HKEY_MUTE_MASK,
1945};
1946
1947#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1948struct tp_nvram_state {
1949 u16 thinkpad_toggle:1;
1950 u16 zoom_toggle:1;
1951 u16 display_toggle:1;
1952 u16 thinklight_toggle:1;
1953 u16 hibernate_toggle:1;
1954 u16 displayexp_toggle:1;
1955 u16 display_state:1;
1956 u16 brightness_toggle:1;
1957 u16 volume_toggle:1;
1958 u16 mute:1;
1959
1960 u8 brightness_level;
1961 u8 volume_level;
1962};
1963
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001964/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001965static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001966
1967/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001968static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001969
1970/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001971 * Acquire mutex to write poller control variables as an
1972 * atomic block.
1973 *
1974 * Increment hotkey_config_change when changing them if you
1975 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001976 *
1977 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1978 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001979static struct mutex hotkey_thread_data_mutex;
1980static unsigned int hotkey_config_change;
1981
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001982/*
1983 * hotkey poller control variables
1984 *
1985 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001986 *
1987 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1988 * should be used only when the changes need to be taken as
1989 * a block, OR when one needs to force the kthread to forget
1990 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001991 */
1992static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1993static unsigned int hotkey_poll_freq = 10; /* Hz */
1994
1995#define HOTKEY_CONFIG_CRITICAL_START \
1996 do { \
1997 mutex_lock(&hotkey_thread_data_mutex); \
1998 hotkey_config_change++; \
1999 } while (0);
2000#define HOTKEY_CONFIG_CRITICAL_END \
2001 mutex_unlock(&hotkey_thread_data_mutex);
2002
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002003#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2004
2005#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002006#define HOTKEY_CONFIG_CRITICAL_START
2007#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002008
2009#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2010
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002011static struct mutex hotkey_mutex;
2012
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002013static enum { /* Reasons for waking up */
2014 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2015 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2016 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2017} hotkey_wakeup_reason;
2018
2019static int hotkey_autosleep_ack;
2020
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002021static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2022static u32 hotkey_all_mask; /* all events supported in fw */
2023static u32 hotkey_reserved_mask; /* events better left disabled */
2024static u32 hotkey_driver_mask; /* events needed by the driver */
2025static u32 hotkey_user_mask; /* events visible to userspace */
2026static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002027
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02002028static unsigned int hotkey_report_mode;
2029
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002030static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002031
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002032static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002033
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002034static void tpacpi_driver_event(const unsigned int hkey_event);
2035static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002036static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002037
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002038/* HKEY.MHKG() return bits */
2039#define TP_HOTKEY_TABLET_MASK (1 << 3)
2040
Johannes Berg19d337d2009-06-02 13:01:37 +02002041static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002042{
Johannes Berg19d337d2009-06-02 13:01:37 +02002043 int status;
2044
2045 if (!tp_features.hotkey_wlsw)
2046 return -ENODEV;
2047
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002048#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002049 if (dbg_wlswemul)
2050 return (tpacpi_wlsw_emulstate) ?
2051 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002052#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002053
2054 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002055 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002056
2057 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002058}
2059
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002060static int hotkey_get_tablet_mode(int *status)
2061{
2062 int s;
2063
2064 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2065 return -EIO;
2066
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002067 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2068 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002069}
2070
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002071/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002072 * Reads current event mask from firmware, and updates
2073 * hotkey_acpi_mask accordingly. Also resets any bits
2074 * from hotkey_user_mask that are unavailable to be
2075 * delivered (shadow requirement of the userspace ABI).
2076 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002077 * Call with hotkey_mutex held
2078 */
2079static int hotkey_mask_get(void)
2080{
2081 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002082 u32 m = 0;
2083
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002084 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002085 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002086
2087 hotkey_acpi_mask = m;
2088 } else {
2089 /* no mask support doesn't mean no event support... */
2090 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002091 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002092
2093 /* sync userspace-visible mask */
2094 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002095
2096 return 0;
2097}
2098
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002099void static hotkey_mask_warn_incomplete_mask(void)
2100{
2101 /* log only what the user can fix... */
2102 const u32 wantedmask = hotkey_driver_mask &
2103 ~(hotkey_acpi_mask | hotkey_source_mask) &
2104 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2105
2106 if (wantedmask)
2107 printk(TPACPI_NOTICE
2108 "required events 0x%08x not enabled!\n",
2109 wantedmask);
2110}
2111
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002112/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002113 * Set the firmware mask when supported
2114 *
2115 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2116 *
2117 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2118 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002119 * Call with hotkey_mutex held
2120 */
2121static int hotkey_mask_set(u32 mask)
2122{
2123 int i;
2124 int rc = 0;
2125
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002126 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002127
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002128 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002129 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002130 if (!acpi_evalf(hkey_handle,
2131 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002132 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002133 rc = -EIO;
2134 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002135 }
2136 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002137 }
2138
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002139 /*
2140 * We *must* make an inconditional call to hotkey_mask_get to
2141 * refresh hotkey_acpi_mask and update hotkey_user_mask
2142 *
2143 * Take the opportunity to also log when we cannot _enable_
2144 * a given event.
2145 */
2146 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2147 printk(TPACPI_NOTICE
2148 "asked for hotkey mask 0x%08x, but "
2149 "firmware forced it to 0x%08x\n",
2150 fwmask, hotkey_acpi_mask);
2151 }
2152
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002153 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2154 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002155
2156 return rc;
2157}
2158
2159/*
2160 * Sets hotkey_user_mask and tries to set the firmware mask
2161 *
2162 * Call with hotkey_mutex held
2163 */
2164static int hotkey_user_mask_set(const u32 mask)
2165{
2166 int rc;
2167
2168 /* Give people a chance to notice they are doing something that
2169 * is bound to go boom on their users sooner or later */
2170 if (!tp_warned.hotkey_mask_ff &&
2171 (mask == 0xffff || mask == 0xffffff ||
2172 mask == 0xffffffff)) {
2173 tp_warned.hotkey_mask_ff = 1;
2174 printk(TPACPI_NOTICE
2175 "setting the hotkey mask to 0x%08x is likely "
2176 "not the best way to go about it\n", mask);
2177 printk(TPACPI_NOTICE
2178 "please consider using the driver defaults, "
2179 "and refer to up-to-date thinkpad-acpi "
2180 "documentation\n");
2181 }
2182
2183 /* Try to enable what the user asked for, plus whatever we need.
2184 * this syncs everything but won't enable bits in hotkey_user_mask */
2185 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2186
2187 /* Enable the available bits in hotkey_user_mask */
2188 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2189
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002190 return rc;
2191}
2192
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002193/*
2194 * Sets the driver hotkey mask.
2195 *
2196 * Can be called even if the hotkey subdriver is inactive
2197 */
2198static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2199{
2200 int rc;
2201
2202 /* Do the right thing if hotkey_init has not been called yet */
2203 if (!tp_features.hotkey) {
2204 hotkey_driver_mask = mask;
2205 return 0;
2206 }
2207
2208 mutex_lock(&hotkey_mutex);
2209
2210 HOTKEY_CONFIG_CRITICAL_START
2211 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002212#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002213 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002214#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002215 HOTKEY_CONFIG_CRITICAL_END
2216
2217 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2218 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002219 hotkey_poll_setup(true);
2220
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002221 mutex_unlock(&hotkey_mutex);
2222
2223 return rc;
2224}
2225
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002226static int hotkey_status_get(int *status)
2227{
2228 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2229 return -EIO;
2230
2231 return 0;
2232}
2233
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002234static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002235{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002236 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002237 return -EIO;
2238
2239 return 0;
2240}
2241
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002242static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002243{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002244 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002245
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002246 if (tp_features.hotkey_tablet &&
2247 !hotkey_get_tablet_mode(&state)) {
2248 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002249
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002250 input_report_switch(tpacpi_inputdev,
2251 SW_TABLET_MODE, !!state);
2252 input_sync(tpacpi_inputdev);
2253
2254 mutex_unlock(&tpacpi_inputdev_send_mutex);
2255 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002256}
2257
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002258/* Do NOT call without validating scancode first */
2259static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002260{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002261 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002262
2263 if (keycode != KEY_RESERVED) {
2264 mutex_lock(&tpacpi_inputdev_send_mutex);
2265
2266 input_report_key(tpacpi_inputdev, keycode, 1);
2267 if (keycode == KEY_UNKNOWN)
2268 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2269 scancode);
2270 input_sync(tpacpi_inputdev);
2271
2272 input_report_key(tpacpi_inputdev, keycode, 0);
2273 if (keycode == KEY_UNKNOWN)
2274 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2275 scancode);
2276 input_sync(tpacpi_inputdev);
2277
2278 mutex_unlock(&tpacpi_inputdev_send_mutex);
2279 }
2280}
2281
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002282/* Do NOT call without validating scancode first */
2283static void tpacpi_input_send_key_masked(const unsigned int scancode)
2284{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002285 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002286 if (hotkey_user_mask & (1 << scancode))
2287 tpacpi_input_send_key(scancode);
2288}
2289
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002290#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2291static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2292
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002293/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002294static void tpacpi_hotkey_send_key(unsigned int scancode)
2295{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002296 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002297 if (hotkey_report_mode < 2) {
2298 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03002299 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002300 }
2301}
2302
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002303static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002304{
2305 u8 d;
2306
2307 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2308 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2309 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2310 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2311 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2312 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2313 }
2314 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2315 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2316 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2317 }
2318 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2319 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2320 n->displayexp_toggle =
2321 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2322 }
2323 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2324 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2325 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2326 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2327 n->brightness_toggle =
2328 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2329 }
2330 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2331 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2332 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2333 >> TP_NVRAM_POS_LEVEL_VOLUME;
2334 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2335 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2336 }
2337}
2338
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002339static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2340 struct tp_nvram_state *newn,
2341 const u32 event_mask)
2342{
2343
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002344#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002345 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002346 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002347 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002348 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002349 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002350
2351#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002352 do { \
2353 if (event_mask & (1 << __scancode)) \
2354 tpacpi_hotkey_send_key(__scancode); \
2355 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002356
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002357 void issue_volchange(const unsigned int oldvol,
2358 const unsigned int newvol)
2359 {
2360 unsigned int i = oldvol;
2361
2362 while (i > newvol) {
2363 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2364 i--;
2365 }
2366 while (i < newvol) {
2367 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2368 i++;
2369 }
2370 }
2371
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002372 void issue_brightnesschange(const unsigned int oldbrt,
2373 const unsigned int newbrt)
2374 {
2375 unsigned int i = oldbrt;
2376
2377 while (i > newbrt) {
2378 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2379 i--;
2380 }
2381 while (i < newbrt) {
2382 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2383 i++;
2384 }
2385 }
2386
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002387 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2388 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2389 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2390 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2391
2392 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2393
2394 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2395
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002396 /*
2397 * Handle volume
2398 *
2399 * This code is supposed to duplicate the IBM firmware behaviour:
2400 * - Pressing MUTE issues mute hotkey message, even when already mute
2401 * - Pressing Volume up/down issues volume up/down hotkey messages,
2402 * even when already at maximum or minumum volume
2403 * - The act of unmuting issues volume up/down notification,
2404 * depending which key was used to unmute
2405 *
2406 * We are constrained to what the NVRAM can tell us, which is not much
2407 * and certainly not enough if more than one volume hotkey was pressed
2408 * since the last poll cycle.
2409 *
2410 * Just to make our life interesting, some newer Lenovo ThinkPads have
2411 * bugs in the BIOS and may fail to update volume_toggle properly.
2412 */
2413 if (newn->mute) {
2414 /* muted */
2415 if (!oldn->mute ||
2416 oldn->volume_toggle != newn->volume_toggle ||
2417 oldn->volume_level != newn->volume_level) {
2418 /* recently muted, or repeated mute keypress, or
2419 * multiple presses ending in mute */
2420 issue_volchange(oldn->volume_level, newn->volume_level);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2422 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002423 } else {
2424 /* unmute */
2425 if (oldn->mute) {
2426 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002428 }
2429 if (oldn->volume_level != newn->volume_level) {
2430 issue_volchange(oldn->volume_level, newn->volume_level);
2431 } else if (oldn->volume_toggle != newn->volume_toggle) {
2432 /* repeated vol up/down keypress at end of scale ? */
2433 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002434 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002435 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002437 }
2438 }
2439
2440 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002441 if (oldn->brightness_level != newn->brightness_level) {
2442 issue_brightnesschange(oldn->brightness_level,
2443 newn->brightness_level);
2444 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2445 /* repeated key presses that didn't change state */
2446 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002447 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002448 else if (newn->brightness_level >= bright_maxlvl
2449 && !tp_features.bright_unkfw)
2450 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002451 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002452
2453#undef TPACPI_COMPARE_KEY
2454#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002455}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002456
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002457/*
2458 * Polling driver
2459 *
2460 * We track all events in hotkey_source_mask all the time, since
2461 * most of them are edge-based. We only issue those requested by
2462 * hotkey_user_mask or hotkey_driver_mask, though.
2463 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002464static int hotkey_kthread(void *data)
2465{
2466 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002467 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002468 unsigned int si, so;
2469 unsigned long t;
2470 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002471 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002472
2473 mutex_lock(&hotkey_thread_mutex);
2474
2475 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2476 goto exit;
2477
2478 set_freezable();
2479
2480 so = 0;
2481 si = 1;
2482 t = 0;
2483
2484 /* Initial state for compares */
2485 mutex_lock(&hotkey_thread_data_mutex);
2486 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002487 poll_mask = hotkey_source_mask;
2488 event_mask = hotkey_source_mask &
2489 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002490 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002491 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002492 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002493
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002494 while (!kthread_should_stop()) {
2495 if (t == 0) {
2496 if (likely(poll_freq))
2497 t = 1000/poll_freq;
2498 else
2499 t = 100; /* should never happen... */
2500 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002501 t = msleep_interruptible(t);
2502 if (unlikely(kthread_should_stop()))
2503 break;
2504 must_reset = try_to_freeze();
2505 if (t > 0 && !must_reset)
2506 continue;
2507
2508 mutex_lock(&hotkey_thread_data_mutex);
2509 if (must_reset || hotkey_config_change != change_detector) {
2510 /* forget old state on thaw or config change */
2511 si = so;
2512 t = 0;
2513 change_detector = hotkey_config_change;
2514 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002515 poll_mask = hotkey_source_mask;
2516 event_mask = hotkey_source_mask &
2517 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002518 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002519 mutex_unlock(&hotkey_thread_data_mutex);
2520
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002521 if (likely(poll_mask)) {
2522 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002523 if (likely(si != so)) {
2524 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002525 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002526 }
2527 }
2528
2529 so = si;
2530 si ^= 1;
2531 }
2532
2533exit:
2534 mutex_unlock(&hotkey_thread_mutex);
2535 return 0;
2536}
2537
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002538/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002539static void hotkey_poll_stop_sync(void)
2540{
2541 if (tpacpi_hotkey_task) {
2542 if (frozen(tpacpi_hotkey_task) ||
2543 freezing(tpacpi_hotkey_task))
2544 thaw_process(tpacpi_hotkey_task);
2545
2546 kthread_stop(tpacpi_hotkey_task);
2547 tpacpi_hotkey_task = NULL;
2548 mutex_lock(&hotkey_thread_mutex);
2549 /* at this point, the thread did exit */
2550 mutex_unlock(&hotkey_thread_mutex);
2551 }
2552}
2553
2554/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002555static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002556{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002557 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2558 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002559
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002560 if (hotkey_poll_freq > 0 &&
2561 (poll_driver_mask ||
2562 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002563 if (!tpacpi_hotkey_task) {
2564 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002565 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002566 if (IS_ERR(tpacpi_hotkey_task)) {
2567 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002568 printk(TPACPI_ERR
2569 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002570 "for hotkey polling\n");
2571 }
2572 }
2573 } else {
2574 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002575 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002576 hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002577 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002578 "hot keys 0x%08x and/or events 0x%08x "
2579 "require polling, which is currently "
2580 "disabled\n",
2581 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002582 }
2583 }
2584}
2585
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002586static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002587{
2588 mutex_lock(&hotkey_mutex);
2589 hotkey_poll_setup(may_warn);
2590 mutex_unlock(&hotkey_mutex);
2591}
2592
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002593/* call with hotkey_mutex held */
2594static void hotkey_poll_set_freq(unsigned int freq)
2595{
2596 if (!freq)
2597 hotkey_poll_stop_sync();
2598
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002599 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002600}
2601
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002602#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2603
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002604static void hotkey_poll_setup(const bool __unused)
2605{
2606}
2607
2608static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002609{
2610}
2611
2612#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2613
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002614static int hotkey_inputdev_open(struct input_dev *dev)
2615{
2616 switch (tpacpi_lifecycle) {
2617 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002618 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002619 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002620 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002621 case TPACPI_LIFE_EXITING:
2622 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002623 }
2624
2625 /* Should only happen if tpacpi_lifecycle is corrupt */
2626 BUG();
2627 return -EBUSY;
2628}
2629
2630static void hotkey_inputdev_close(struct input_dev *dev)
2631{
2632 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002633 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002634 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002635 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002636}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002637
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002638/* sysfs hotkey enable ------------------------------------------------- */
2639static ssize_t hotkey_enable_show(struct device *dev,
2640 struct device_attribute *attr,
2641 char *buf)
2642{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002643 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002644
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002645 printk_deprecated_attribute("hotkey_enable",
2646 "Hotkey reporting is always enabled");
2647
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002648 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002649 if (res)
2650 return res;
2651
2652 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2653}
2654
2655static ssize_t hotkey_enable_store(struct device *dev,
2656 struct device_attribute *attr,
2657 const char *buf, size_t count)
2658{
2659 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002660
2661 printk_deprecated_attribute("hotkey_enable",
2662 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002663
2664 if (parse_strtoul(buf, 1, &t))
2665 return -EINVAL;
2666
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002667 if (t == 0)
2668 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002669
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002670 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002671}
2672
2673static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002674 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002675 hotkey_enable_show, hotkey_enable_store);
2676
2677/* sysfs hotkey mask --------------------------------------------------- */
2678static ssize_t hotkey_mask_show(struct device *dev,
2679 struct device_attribute *attr,
2680 char *buf)
2681{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002682 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002683}
2684
2685static ssize_t hotkey_mask_store(struct device *dev,
2686 struct device_attribute *attr,
2687 const char *buf, size_t count)
2688{
2689 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002690 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002691
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002692 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002693 return -EINVAL;
2694
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002695 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002696 return -ERESTARTSYS;
2697
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002698 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002699
2700#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002701 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002702#endif
2703
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002704 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002705
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002706 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2707
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002708 return (res) ? res : count;
2709}
2710
2711static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002712 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002713 hotkey_mask_show, hotkey_mask_store);
2714
2715/* sysfs hotkey bios_enabled ------------------------------------------- */
2716static ssize_t hotkey_bios_enabled_show(struct device *dev,
2717 struct device_attribute *attr,
2718 char *buf)
2719{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002720 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002721}
2722
2723static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002724 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002725
2726/* sysfs hotkey bios_mask ---------------------------------------------- */
2727static ssize_t hotkey_bios_mask_show(struct device *dev,
2728 struct device_attribute *attr,
2729 char *buf)
2730{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002731 printk_deprecated_attribute("hotkey_bios_mask",
2732 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002733 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002734}
2735
2736static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002737 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002738
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002739/* sysfs hotkey all_mask ----------------------------------------------- */
2740static ssize_t hotkey_all_mask_show(struct device *dev,
2741 struct device_attribute *attr,
2742 char *buf)
2743{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002744 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2745 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002746}
2747
2748static struct device_attribute dev_attr_hotkey_all_mask =
2749 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2750
2751/* sysfs hotkey recommended_mask --------------------------------------- */
2752static ssize_t hotkey_recommended_mask_show(struct device *dev,
2753 struct device_attribute *attr,
2754 char *buf)
2755{
2756 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002757 (hotkey_all_mask | hotkey_source_mask)
2758 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002759}
2760
2761static struct device_attribute dev_attr_hotkey_recommended_mask =
2762 __ATTR(hotkey_recommended_mask, S_IRUGO,
2763 hotkey_recommended_mask_show, NULL);
2764
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002765#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2766
2767/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2768static ssize_t hotkey_source_mask_show(struct device *dev,
2769 struct device_attribute *attr,
2770 char *buf)
2771{
2772 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2773}
2774
2775static ssize_t hotkey_source_mask_store(struct device *dev,
2776 struct device_attribute *attr,
2777 const char *buf, size_t count)
2778{
2779 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002780 u32 r_ev;
2781 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002782
2783 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2784 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2785 return -EINVAL;
2786
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002787 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002788 return -ERESTARTSYS;
2789
2790 HOTKEY_CONFIG_CRITICAL_START
2791 hotkey_source_mask = t;
2792 HOTKEY_CONFIG_CRITICAL_END
2793
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002794 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2795 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002796 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002797
2798 /* check if events needed by the driver got disabled */
2799 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2800 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002801
2802 mutex_unlock(&hotkey_mutex);
2803
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002804 if (rc < 0)
2805 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2806 "firmware event mask!\n");
2807
2808 if (r_ev)
2809 printk(TPACPI_NOTICE "hotkey_source_mask: "
2810 "some important events were disabled: "
2811 "0x%04x\n", r_ev);
2812
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002813 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2814
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002815 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002816}
2817
2818static struct device_attribute dev_attr_hotkey_source_mask =
2819 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2820 hotkey_source_mask_show, hotkey_source_mask_store);
2821
2822/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2823static ssize_t hotkey_poll_freq_show(struct device *dev,
2824 struct device_attribute *attr,
2825 char *buf)
2826{
2827 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2828}
2829
2830static ssize_t hotkey_poll_freq_store(struct device *dev,
2831 struct device_attribute *attr,
2832 const char *buf, size_t count)
2833{
2834 unsigned long t;
2835
2836 if (parse_strtoul(buf, 25, &t))
2837 return -EINVAL;
2838
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002839 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002840 return -ERESTARTSYS;
2841
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002842 hotkey_poll_set_freq(t);
2843 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002844
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002845 mutex_unlock(&hotkey_mutex);
2846
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002847 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2848
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002849 return count;
2850}
2851
2852static struct device_attribute dev_attr_hotkey_poll_freq =
2853 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2854 hotkey_poll_freq_show, hotkey_poll_freq_store);
2855
2856#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2857
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002858/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002859static ssize_t hotkey_radio_sw_show(struct device *dev,
2860 struct device_attribute *attr,
2861 char *buf)
2862{
Johannes Berg19d337d2009-06-02 13:01:37 +02002863 int res;
2864 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002865 if (res < 0)
2866 return res;
2867
Johannes Berg19d337d2009-06-02 13:01:37 +02002868 /* Opportunistic update */
2869 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2870
2871 return snprintf(buf, PAGE_SIZE, "%d\n",
2872 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002873}
2874
2875static struct device_attribute dev_attr_hotkey_radio_sw =
2876 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2877
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002878static void hotkey_radio_sw_notify_change(void)
2879{
2880 if (tp_features.hotkey_wlsw)
2881 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2882 "hotkey_radio_sw");
2883}
2884
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002885/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2886static ssize_t hotkey_tablet_mode_show(struct device *dev,
2887 struct device_attribute *attr,
2888 char *buf)
2889{
2890 int res, s;
2891 res = hotkey_get_tablet_mode(&s);
2892 if (res < 0)
2893 return res;
2894
2895 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2896}
2897
2898static struct device_attribute dev_attr_hotkey_tablet_mode =
2899 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2900
2901static void hotkey_tablet_mode_notify_change(void)
2902{
2903 if (tp_features.hotkey_tablet)
2904 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2905 "hotkey_tablet_mode");
2906}
2907
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002908/* sysfs hotkey report_mode -------------------------------------------- */
2909static ssize_t hotkey_report_mode_show(struct device *dev,
2910 struct device_attribute *attr,
2911 char *buf)
2912{
2913 return snprintf(buf, PAGE_SIZE, "%d\n",
2914 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2915}
2916
2917static struct device_attribute dev_attr_hotkey_report_mode =
2918 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2919
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002920/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002921static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2922 struct device_attribute *attr,
2923 char *buf)
2924{
2925 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2926}
2927
2928static struct device_attribute dev_attr_hotkey_wakeup_reason =
2929 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2930
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002931static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002932{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002933 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2934 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002935}
2936
2937/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002938static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2939 struct device_attribute *attr,
2940 char *buf)
2941{
2942 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2943}
2944
2945static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2946 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2947 hotkey_wakeup_hotunplug_complete_show, NULL);
2948
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002949static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002950{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002951 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2952 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002953}
2954
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002955/* --------------------------------------------------------------------- */
2956
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002957static struct attribute *hotkey_attributes[] __initdata = {
2958 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002959 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002960 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002961 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002962 &dev_attr_hotkey_wakeup_reason.attr,
2963 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002964 &dev_attr_hotkey_mask.attr,
2965 &dev_attr_hotkey_all_mask.attr,
2966 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002967#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002968 &dev_attr_hotkey_source_mask.attr,
2969 &dev_attr_hotkey_poll_freq.attr,
2970#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002971};
2972
Johannes Berg19d337d2009-06-02 13:01:37 +02002973/*
2974 * Sync both the hw and sw blocking state of all switches
2975 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002976static void tpacpi_send_radiosw_update(void)
2977{
2978 int wlsw;
2979
Johannes Berg19d337d2009-06-02 13:01:37 +02002980 /*
2981 * We must sync all rfkill controllers *before* issuing any
2982 * rfkill input events, or we will race the rfkill core input
2983 * handler.
2984 *
2985 * tpacpi_inputdev_send_mutex works as a syncronization point
2986 * for the above.
2987 *
2988 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2989 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002990
Johannes Berg19d337d2009-06-02 13:01:37 +02002991 wlsw = hotkey_get_wlsw();
2992
2993 /* Sync hw blocking state first if it is hw-blocked */
2994 if (wlsw == TPACPI_RFK_RADIO_OFF)
2995 tpacpi_rfk_update_hwblock_state(true);
2996
2997 /* Sync sw blocking state */
2998 tpacpi_rfk_update_swstate_all();
2999
3000 /* Sync hw blocking state last if it is hw-unblocked */
3001 if (wlsw == TPACPI_RFK_RADIO_ON)
3002 tpacpi_rfk_update_hwblock_state(false);
3003
3004 /* Issue rfkill input event for WLSW switch */
3005 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003006 mutex_lock(&tpacpi_inputdev_send_mutex);
3007
3008 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003009 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003010 input_sync(tpacpi_inputdev);
3011
3012 mutex_unlock(&tpacpi_inputdev_send_mutex);
3013 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003014
3015 /*
3016 * this can be unconditional, as we will poll state again
3017 * if userspace uses the notify to read data
3018 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003019 hotkey_radio_sw_notify_change();
3020}
3021
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003022static void hotkey_exit(void)
3023{
3024#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003025 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003026 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003027 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003028#endif
3029
3030 if (hotkey_dev_attributes)
3031 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3032
3033 kfree(hotkey_keycode_map);
3034
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003035 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003036 "restoring original HKEY status and mask\n");
3037 /* yes, there is a bitwise or below, we want the
3038 * functions to be called even if one of them fail */
3039 if (((tp_features.hotkey_mask &&
3040 hotkey_mask_set(hotkey_orig_mask)) |
3041 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003042 printk(TPACPI_ERR
3043 "failed to restore hot key mask "
3044 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003045}
3046
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003047static void __init hotkey_unmap(const unsigned int scancode)
3048{
3049 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3050 clear_bit(hotkey_keycode_map[scancode],
3051 tpacpi_inputdev->keybit);
3052 hotkey_keycode_map[scancode] = KEY_RESERVED;
3053 }
3054}
3055
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003056/*
3057 * HKEY quirks:
3058 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3059 */
3060
3061#define TPACPI_HK_Q_INIMASK 0x0001
3062
3063static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3064 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3065 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3066 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3067 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3068 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3069 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3070 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3071 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3072 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3073 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3074 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3075 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3076 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3077 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3078 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3079 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3080 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3081 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3082 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3083};
3084
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003085static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003086{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003087 /* Requirements for changing the default keymaps:
3088 *
3089 * 1. Many of the keys are mapped to KEY_RESERVED for very
3090 * good reasons. Do not change them unless you have deep
3091 * knowledge on the IBM and Lenovo ThinkPad firmware for
3092 * the various ThinkPad models. The driver behaves
3093 * differently for KEY_RESERVED: such keys have their
3094 * hot key mask *unset* in mask_recommended, and also
3095 * in the initial hot key mask programmed into the
3096 * firmware at driver load time, which means the firm-
3097 * ware may react very differently if you change them to
3098 * something else;
3099 *
3100 * 2. You must be subscribed to the linux-thinkpad and
3101 * ibm-acpi-devel mailing lists, and you should read the
3102 * list archives since 2007 if you want to change the
3103 * keymaps. This requirement exists so that you will
3104 * know the past history of problems with the thinkpad-
3105 * acpi driver keymaps, and also that you will be
3106 * listening to any bug reports;
3107 *
3108 * 3. Do not send thinkpad-acpi specific patches directly to
3109 * for merging, *ever*. Send them to the linux-acpi
3110 * mailinglist for comments. Merging is to be done only
3111 * through acpi-test and the ACPI maintainer.
3112 *
3113 * If the above is too much to ask, don't change the keymap.
3114 * Ask the thinkpad-acpi maintainer to do it, instead.
3115 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003116 static u16 ibm_keycode_map[] __initdata = {
3117 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3118 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3119 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3120 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003121
3122 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003123 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3124 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3125 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003126
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003127 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003128 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003129 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003130
3131 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003132 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003133
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003134 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3135 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003136
3137 /* Volume: firmware always react to it and reprograms
3138 * the built-in *extra* mixer. Never map it to control
3139 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003140 KEY_RESERVED, /* 0x14: VOLUME UP */
3141 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3142 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003143
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003144 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003145
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003146 /* (assignments unknown, please report if found) */
3147 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3148 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3149 };
3150 static u16 lenovo_keycode_map[] __initdata = {
3151 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3152 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3153 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3154 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003155
3156 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003157 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3158 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3159 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003160
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003161 /* These should be enabled --only-- when ACPI video
3162 * is disabled (i.e. in "vendor" mode), and are handled
3163 * in a special way by the init code */
3164 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3165 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003166
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003167 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003168
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003169 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3170 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003171
3172 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3173 * react to it and reprograms the built-in *extra* mixer.
3174 * Never map it to control another mixer by default.
3175 *
3176 * T60?, T61, R60?, R61: firmware and EC tries to send
3177 * these over the regular keyboard, so these are no-ops,
3178 * but there are still weird bugs re. MUTE, so do not
3179 * change unless you get test reports from all Lenovo
3180 * models. May cause the BIOS to interfere with the
3181 * HDA mixer.
3182 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003183 KEY_RESERVED, /* 0x14: VOLUME UP */
3184 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3185 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003186
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003187 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003188
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003189 /* (assignments unknown, please report if found) */
3190 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3191 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3192 };
3193
3194#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3195#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3196#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3197
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003198 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003199 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003200 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003201 bool radiosw_state = false;
3202 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003203
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003204 unsigned long quirks;
3205
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003206 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3207 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003208
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003209 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003210 BUG_ON(tpacpi_inputdev->open != NULL ||
3211 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003212
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003213 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003214 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003215
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003216#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3217 mutex_init(&hotkey_thread_mutex);
3218 mutex_init(&hotkey_thread_data_mutex);
3219#endif
3220
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003221 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003222 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003223
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003224 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3225 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003226 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003227
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003228 if (!tp_features.hotkey)
3229 return 1;
3230
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003231 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3232 ARRAY_SIZE(tpacpi_hotkey_qtable));
3233
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003234 tpacpi_disable_brightness_delay();
3235
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003236 /* MUST have enough space for all attributes to be added to
3237 * hotkey_dev_attributes */
3238 hotkey_dev_attributes = create_attr_set(
3239 ARRAY_SIZE(hotkey_attributes) + 2,
3240 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003241 if (!hotkey_dev_attributes)
3242 return -ENOMEM;
3243 res = add_many_to_attr_set(hotkey_dev_attributes,
3244 hotkey_attributes,
3245 ARRAY_SIZE(hotkey_attributes));
3246 if (res)
3247 goto err_exit;
3248
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003249 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003250 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3251 for HKEY interface version 0x100 */
3252 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3253 if ((hkeyv >> 8) != 1) {
3254 printk(TPACPI_ERR "unknown version of the "
3255 "HKEY interface: 0x%x\n", hkeyv);
3256 printk(TPACPI_ERR "please report this to %s\n",
3257 TPACPI_MAIL);
3258 } else {
3259 /*
3260 * MHKV 0x100 in A31, R40, R40e,
3261 * T4x, X31, and later
3262 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003263 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3264 "firmware HKEY interface version: 0x%x\n",
3265 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003266
3267 /* Paranoia check AND init hotkey_all_mask */
3268 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3269 "MHKA", "qd")) {
3270 printk(TPACPI_ERR
3271 "missing MHKA handler, "
3272 "please report this to %s\n",
3273 TPACPI_MAIL);
3274 /* Fallback: pre-init for FN+F3,F4,F12 */
3275 hotkey_all_mask = 0x080cU;
3276 } else {
3277 tp_features.hotkey_mask = 1;
3278 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003279 }
3280 }
3281
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003282 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3283 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003284 str_supported(tp_features.hotkey_mask));
3285
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003286 /* Init hotkey_all_mask if not initialized yet */
3287 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3288 (quirks & TPACPI_HK_Q_INIMASK))
3289 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003290
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003291 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003292 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003293 /* hotkey_source_mask *must* be zero for
3294 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003295 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003296 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003297 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003298
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003299 hotkey_orig_mask = hotkey_acpi_mask;
3300 } else {
3301 hotkey_orig_mask = hotkey_all_mask;
3302 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003303 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003304
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003305#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3306 if (dbg_wlswemul) {
3307 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003308 radiosw_state = !!tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003309 printk(TPACPI_INFO
3310 "radio switch emulation enabled\n");
3311 } else
3312#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003313 /* Not all thinkpads have a hardware radio switch */
3314 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3315 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003316 radiosw_state = !!status;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003317 printk(TPACPI_INFO
3318 "radio switch found; radios are %s\n",
3319 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003320 }
3321 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003322 res = add_to_attr_set(hotkey_dev_attributes,
3323 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003324
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003325 /* For X41t, X60t, X61t Tablets... */
3326 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3327 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003328 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003329 printk(TPACPI_INFO
3330 "possible tablet mode switch found; "
3331 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003332 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003333 res = add_to_attr_set(hotkey_dev_attributes,
3334 &dev_attr_hotkey_tablet_mode.attr);
3335 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003336
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003337 if (!res)
3338 res = register_attr_set_with_sysfs(
3339 hotkey_dev_attributes,
3340 &tpacpi_pdev->dev.kobj);
3341 if (res)
3342 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003343
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003344 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003345
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003346 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3347 GFP_KERNEL);
3348 if (!hotkey_keycode_map) {
3349 printk(TPACPI_ERR
3350 "failed to allocate memory for key map\n");
3351 res = -ENOMEM;
3352 goto err_exit;
3353 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003354
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03003355 if (tpacpi_is_lenovo()) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003356 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003357 "using Lenovo default hot key map\n");
3358 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3359 TPACPI_HOTKEY_MAP_SIZE);
3360 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003361 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003362 "using IBM default hot key map\n");
3363 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3364 TPACPI_HOTKEY_MAP_SIZE);
3365 }
3366
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003367 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003368 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3369 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3370 tpacpi_inputdev->keycode = hotkey_keycode_map;
3371 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3372 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003373 input_set_capability(tpacpi_inputdev, EV_KEY,
3374 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003375 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003376 if (i < sizeof(hotkey_reserved_mask)*8)
3377 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003378 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003379 }
3380
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003381 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003382 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003383 input_report_switch(tpacpi_inputdev,
3384 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003385 }
3386 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003387 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003388 input_report_switch(tpacpi_inputdev,
3389 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003390 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003391
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003392 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003393 * userspace. tpacpi_detect_brightness_capabilities() must have
3394 * been called before this point */
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003395 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003396 printk(TPACPI_INFO
3397 "This ThinkPad has standard ACPI backlight "
3398 "brightness control, supported by the ACPI "
3399 "video driver\n");
3400 printk(TPACPI_NOTICE
3401 "Disabling thinkpad-acpi brightness events "
3402 "by default...\n");
3403
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003404 /* Disable brightness up/down on Lenovo thinkpads when
3405 * ACPI is handling them, otherwise it is plain impossible
3406 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003407 hotkey_reserved_mask |=
3408 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3409 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003410 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3411 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003412 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003413
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003414#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003415 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3416 & ~hotkey_all_mask
3417 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003418
3419 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3420 "hotkey source mask 0x%08x, polling freq %u\n",
3421 hotkey_source_mask, hotkey_poll_freq);
3422#endif
3423
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003424 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3425 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003426 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003427 if (res) {
3428 hotkey_exit();
3429 return res;
3430 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003431 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3432 | hotkey_driver_mask)
3433 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003434 if (res < 0 && res != -ENXIO) {
3435 hotkey_exit();
3436 return res;
3437 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003438 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3439 & ~hotkey_reserved_mask;
3440 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3441 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3442 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003443
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003444 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3445 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003446 (hotkey_report_mode < 2) ?
3447 "enabled" : "disabled");
3448
3449 tpacpi_inputdev->open = &hotkey_inputdev_open;
3450 tpacpi_inputdev->close = &hotkey_inputdev_close;
3451
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003452 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003453
3454 return 0;
3455
3456err_exit:
3457 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3458 hotkey_dev_attributes = NULL;
3459
3460 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003461}
3462
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003463static bool hotkey_notify_hotkey(const u32 hkey,
3464 bool *send_acpi_ev,
3465 bool *ignore_acpi_ev)
3466{
3467 /* 0x1000-0x1FFF: key presses */
3468 unsigned int scancode = hkey & 0xfff;
3469 *send_acpi_ev = true;
3470 *ignore_acpi_ev = false;
3471
3472 if (scancode > 0 && scancode < 0x21) {
3473 scancode--;
3474 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003475 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003476 *send_acpi_ev = false;
3477 } else {
3478 *ignore_acpi_ev = true;
3479 }
3480 return true;
3481 }
3482 return false;
3483}
3484
3485static bool hotkey_notify_wakeup(const u32 hkey,
3486 bool *send_acpi_ev,
3487 bool *ignore_acpi_ev)
3488{
3489 /* 0x2000-0x2FFF: Wakeup reason */
3490 *send_acpi_ev = true;
3491 *ignore_acpi_ev = false;
3492
3493 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003494 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3495 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003496 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3497 *ignore_acpi_ev = true;
3498 break;
3499
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003500 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3501 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003502 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3503 *ignore_acpi_ev = true;
3504 break;
3505
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003506 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3507 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003508 printk(TPACPI_ALERT
3509 "EMERGENCY WAKEUP: battery almost empty\n");
3510 /* how to auto-heal: */
3511 /* 2313: woke up from S3, go to S4/S5 */
3512 /* 2413: woke up from S4, go to S5 */
3513 break;
3514
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003515 default:
3516 return false;
3517 }
3518
3519 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3520 printk(TPACPI_INFO
3521 "woke up due to a hot-unplug "
3522 "request...\n");
3523 hotkey_wakeup_reason_notify_change();
3524 }
3525 return true;
3526}
3527
3528static bool hotkey_notify_usrevent(const u32 hkey,
3529 bool *send_acpi_ev,
3530 bool *ignore_acpi_ev)
3531{
3532 /* 0x5000-0x5FFF: human interface helpers */
3533 *send_acpi_ev = true;
3534 *ignore_acpi_ev = false;
3535
3536 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003537 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3538 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003539 return true;
3540
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003541 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3542 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003543 tpacpi_input_send_tabletsw();
3544 hotkey_tablet_mode_notify_change();
3545 *send_acpi_ev = false;
3546 return true;
3547
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003548 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3549 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3550 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003551 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003552 *ignore_acpi_ev = true;
3553 return true;
3554
3555 default:
3556 return false;
3557 }
3558}
3559
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003560static void thermal_dump_all_sensors(void);
3561
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003562static bool hotkey_notify_thermal(const u32 hkey,
3563 bool *send_acpi_ev,
3564 bool *ignore_acpi_ev)
3565{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003566 bool known = true;
3567
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003568 /* 0x6000-0x6FFF: thermal alarms */
3569 *send_acpi_ev = true;
3570 *ignore_acpi_ev = false;
3571
3572 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003573 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003574 printk(TPACPI_INFO
3575 "EC reports that Thermal Table has changed\n");
3576 /* recommended action: do nothing, we don't have
3577 * Lenovo ATM information */
3578 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003579 case TP_HKEY_EV_ALARM_BAT_HOT:
3580 printk(TPACPI_CRIT
3581 "THERMAL ALARM: battery is too hot!\n");
3582 /* recommended action: warn user through gui */
3583 break;
3584 case TP_HKEY_EV_ALARM_BAT_XHOT:
3585 printk(TPACPI_ALERT
3586 "THERMAL EMERGENCY: battery is extremely hot!\n");
3587 /* recommended action: immediate sleep/hibernate */
3588 break;
3589 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3590 printk(TPACPI_CRIT
3591 "THERMAL ALARM: "
3592 "a sensor reports something is too hot!\n");
3593 /* recommended action: warn user through gui, that */
3594 /* some internal component is too hot */
3595 break;
3596 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3597 printk(TPACPI_ALERT
3598 "THERMAL EMERGENCY: "
3599 "a sensor reports something is extremely hot!\n");
3600 /* recommended action: immediate sleep/hibernate */
3601 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003602 default:
3603 printk(TPACPI_ALERT
3604 "THERMAL ALERT: unknown thermal alarm received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003605 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003606 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003607
3608 thermal_dump_all_sensors();
3609
3610 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003611}
3612
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003613static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3614{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003615 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003616 bool send_acpi_ev;
3617 bool ignore_acpi_ev;
3618 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003619
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003620 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003621 printk(TPACPI_ERR
3622 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003623 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003624 acpi_bus_generate_netlink_event(
3625 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003626 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003627 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003628 return;
3629 }
3630
3631 while (1) {
3632 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003633 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003634 return;
3635 }
3636
3637 if (hkey == 0) {
3638 /* queue empty */
3639 return;
3640 }
3641
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003642 send_acpi_ev = true;
3643 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003644
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003645 switch (hkey >> 12) {
3646 case 1:
3647 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003648 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3649 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003650 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003651 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003652 /* 0x2000-0x2FFF: Wakeup reason */
3653 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3654 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003655 break;
3656 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003657 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003658 switch (hkey) {
3659 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003660 hotkey_autosleep_ack = 1;
3661 printk(TPACPI_INFO
3662 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003663 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003664 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003665 break;
3666 case TP_HKEY_EV_OPTDRV_EJ:
3667 /* FIXME: kick libata if SATA link offline */
3668 known_ev = true;
3669 break;
3670 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003671 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003672 }
3673 break;
3674 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003675 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003676 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003677 hotkey_autosleep_ack = 1;
3678 printk(TPACPI_INFO
3679 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003680 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003681 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003682 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003683 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003684 }
3685 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003686 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003687 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003688 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3689 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003690 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003691 case 6:
3692 /* 0x6000-0x6FFF: thermal alarms */
3693 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3694 &ignore_acpi_ev);
3695 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003696 case 7:
3697 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003698 if (tp_features.hotkey_wlsw &&
3699 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003700 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003701 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003702 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003703 break;
3704 }
3705 /* fallthrough to default */
3706 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003707 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003708 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003709 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003710 printk(TPACPI_NOTICE
3711 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003712 printk(TPACPI_NOTICE
3713 "please report the conditions when this "
3714 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003715 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003716
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003717 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003718 if (!ignore_acpi_ev &&
3719 (send_acpi_ev || hotkey_report_mode < 2)) {
3720 acpi_bus_generate_proc_event(ibm->acpi->device,
3721 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003722 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003723
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003724 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003725 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003726 acpi_bus_generate_netlink_event(
3727 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003728 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003729 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003730 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003731 }
3732}
3733
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003734static void hotkey_suspend(pm_message_t state)
3735{
3736 /* Do these on suspend, we get the events on early resume! */
3737 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3738 hotkey_autosleep_ack = 0;
3739}
3740
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003741static void hotkey_resume(void)
3742{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003743 tpacpi_disable_brightness_delay();
3744
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003745 if (hotkey_status_set(true) < 0 ||
3746 hotkey_mask_set(hotkey_acpi_mask) < 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003747 printk(TPACPI_ERR
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003748 "error while attempting to reset the event "
3749 "firmware interface\n");
3750
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003751 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003752 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003753 hotkey_wakeup_reason_notify_change();
3754 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003755 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003756}
3757
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003758/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003759static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003761 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003763 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003764 seq_printf(m, "status:\t\tnot supported\n");
3765 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003766 }
3767
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003768 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003769 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003770 res = hotkey_status_get(&status);
3771 if (!res)
3772 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003773 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003774 if (res)
3775 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003777 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003778 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003779 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3780 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003782 seq_printf(m, "mask:\t\tnot supported\n");
3783 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 }
3785
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787}
3788
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003789static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003790{
3791 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003792 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3793 TPACPI_WARN
3794 "hotkey enable/disable functionality has been "
3795 "removed from the driver. Hotkeys are always "
3796 "enabled\n"))
3797 printk(TPACPI_ERR
3798 "Please remove the hotkey=enable module "
3799 "parameter, it is deprecated. Hotkeys are always "
3800 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003801}
3802
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003803static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003805 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003806 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003809 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 return -ENODEV;
3811
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003812 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003813 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003814
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003815 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003816
3817 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 while ((cmd = next_cmd(&buf))) {
3819 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003820 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003822 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003823 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003825 mask = (hotkey_all_mask | hotkey_source_mask)
3826 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3828 /* mask set */
3829 } else if (sscanf(cmd, "%x", &mask) == 1) {
3830 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003831 } else {
3832 res = -EINVAL;
3833 goto errexit;
3834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003836
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003837 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003838 tpacpi_disclose_usertask("procfs hotkey",
3839 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003840 res = hotkey_user_mask_set(mask);
3841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003843errexit:
3844 mutex_unlock(&hotkey_mutex);
3845 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003846}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003848static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003849 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003850 {"", 0},
3851};
3852
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003853static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003854 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003855 .notify = hotkey_notify,
3856 .handle = &hkey_handle,
3857 .type = ACPI_DEVICE_NOTIFY,
3858};
3859
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003860static struct ibm_struct hotkey_driver_data = {
3861 .name = "hotkey",
3862 .read = hotkey_read,
3863 .write = hotkey_write,
3864 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003865 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003866 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003867 .acpi = &ibm_hotkey_acpidriver,
3868};
3869
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003870/*************************************************************************
3871 * Bluetooth subdriver
3872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003874enum {
3875 /* ACPI GBDC/SBDC bits */
3876 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3877 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003878 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003879 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003880};
3881
3882enum {
3883 /* ACPI \BLTH commands */
3884 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3885 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3886 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3887 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3888 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003889};
3890
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003891#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3892
Johannes Berg19d337d2009-06-02 13:01:37 +02003893static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003894{
3895 int status;
3896
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003897#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3898 if (dbg_bluetoothemul)
3899 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003900 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003901#endif
3902
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003903 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3904 return -EIO;
3905
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003906 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003907 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003908}
3909
Johannes Berg19d337d2009-06-02 13:01:37 +02003910static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003911{
3912 int status;
3913
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003914 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003915 "will attempt to %s bluetooth\n",
3916 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003917
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003918#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3919 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003920 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003921 return 0;
3922 }
3923#endif
3924
Johannes Berg19d337d2009-06-02 13:01:37 +02003925 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003926 status = TP_ACPI_BLUETOOTH_RADIOSSW
3927 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3928 else
3929 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003930
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003931 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3932 return -EIO;
3933
3934 return 0;
3935}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003936
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003937/* sysfs bluetooth enable ---------------------------------------------- */
3938static ssize_t bluetooth_enable_show(struct device *dev,
3939 struct device_attribute *attr,
3940 char *buf)
3941{
Johannes Berg19d337d2009-06-02 13:01:37 +02003942 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3943 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003944}
3945
3946static ssize_t bluetooth_enable_store(struct device *dev,
3947 struct device_attribute *attr,
3948 const char *buf, size_t count)
3949{
Johannes Berg19d337d2009-06-02 13:01:37 +02003950 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3951 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003952}
3953
3954static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003955 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003956 bluetooth_enable_show, bluetooth_enable_store);
3957
3958/* --------------------------------------------------------------------- */
3959
3960static struct attribute *bluetooth_attributes[] = {
3961 &dev_attr_bluetooth_enable.attr,
3962 NULL
3963};
3964
3965static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003966 .attrs = bluetooth_attributes,
3967};
3968
Johannes Berg19d337d2009-06-02 13:01:37 +02003969static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3970 .get_status = bluetooth_get_status,
3971 .set_status = bluetooth_set_status,
3972};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003973
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003974static void bluetooth_shutdown(void)
3975{
3976 /* Order firmware to save current state to NVRAM */
3977 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3978 TP_ACPI_BLTH_SAVE_STATE))
3979 printk(TPACPI_NOTICE
3980 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003981 else
3982 vdbg_printk(TPACPI_DBG_RFKILL,
3983 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003984}
3985
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003986static void bluetooth_exit(void)
3987{
3988 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3989 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003990
3991 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3992
3993 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003994}
3995
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003996static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003998 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003999 int status = 0;
4000
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004001 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4002 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004003
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004004 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004005
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004006 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4007 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004008 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004009 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004011 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4012 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004013 str_supported(tp_features.bluetooth),
4014 status);
4015
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004016#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4017 if (dbg_bluetoothemul) {
4018 tp_features.bluetooth = 1;
4019 printk(TPACPI_INFO
4020 "bluetooth switch emulation enabled\n");
4021 } else
4022#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004023 if (tp_features.bluetooth &&
4024 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4025 /* no bluetooth hardware present in system */
4026 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004027 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004028 "bluetooth hardware not installed\n");
4029 }
4030
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004031 if (!tp_features.bluetooth)
4032 return 1;
4033
Johannes Berg19d337d2009-06-02 13:01:37 +02004034 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4035 &bluetooth_tprfk_ops,
4036 RFKILL_TYPE_BLUETOOTH,
4037 TPACPI_RFK_BLUETOOTH_SW_NAME,
4038 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004039 if (res)
4040 return res;
4041
Johannes Berg19d337d2009-06-02 13:01:37 +02004042 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4043 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004044 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004045 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004046 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004047 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004048
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050}
4051
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004052/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004053static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004055 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056}
4057
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004058static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
Johannes Berg19d337d2009-06-02 13:01:37 +02004060 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061}
4062
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004063static struct ibm_struct bluetooth_driver_data = {
4064 .name = "bluetooth",
4065 .read = bluetooth_read,
4066 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004067 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004068 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004069};
4070
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004071/*************************************************************************
4072 * Wan subdriver
4073 */
4074
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004075enum {
4076 /* ACPI GWAN/SWAN bits */
4077 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4078 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004079 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004080 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004081};
4082
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004083#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4084
Johannes Berg19d337d2009-06-02 13:01:37 +02004085static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004086{
4087 int status;
4088
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004089#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4090 if (dbg_wwanemul)
4091 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004092 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004093#endif
4094
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004095 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4096 return -EIO;
4097
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004098 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004099 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004100}
4101
Johannes Berg19d337d2009-06-02 13:01:37 +02004102static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004103{
4104 int status;
4105
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004106 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004107 "will attempt to %s wwan\n",
4108 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004109
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004110#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4111 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004112 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004113 return 0;
4114 }
4115#endif
4116
Johannes Berg19d337d2009-06-02 13:01:37 +02004117 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004118 status = TP_ACPI_WANCARD_RADIOSSW
4119 | TP_ACPI_WANCARD_RESUMECTRL;
4120 else
4121 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004122
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004123 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4124 return -EIO;
4125
4126 return 0;
4127}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004128
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004129/* sysfs wan enable ---------------------------------------------------- */
4130static ssize_t wan_enable_show(struct device *dev,
4131 struct device_attribute *attr,
4132 char *buf)
4133{
Johannes Berg19d337d2009-06-02 13:01:37 +02004134 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4135 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004136}
4137
4138static ssize_t wan_enable_store(struct device *dev,
4139 struct device_attribute *attr,
4140 const char *buf, size_t count)
4141{
Johannes Berg19d337d2009-06-02 13:01:37 +02004142 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4143 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004144}
4145
4146static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004147 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004148 wan_enable_show, wan_enable_store);
4149
4150/* --------------------------------------------------------------------- */
4151
4152static struct attribute *wan_attributes[] = {
4153 &dev_attr_wan_enable.attr,
4154 NULL
4155};
4156
4157static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004158 .attrs = wan_attributes,
4159};
4160
Johannes Berg19d337d2009-06-02 13:01:37 +02004161static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4162 .get_status = wan_get_status,
4163 .set_status = wan_set_status,
4164};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004165
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004166static void wan_shutdown(void)
4167{
4168 /* Order firmware to save current state to NVRAM */
4169 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4170 TP_ACPI_WGSV_SAVE_STATE))
4171 printk(TPACPI_NOTICE
4172 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004173 else
4174 vdbg_printk(TPACPI_DBG_RFKILL,
4175 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004176}
4177
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004178static void wan_exit(void)
4179{
4180 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4181 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004182
4183 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4184
4185 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004186}
4187
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004188static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004189{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004190 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004191 int status = 0;
4192
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004193 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4194 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004195
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004196 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004197
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004198 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004199 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004200
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004201 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4202 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004203 str_supported(tp_features.wan),
4204 status);
4205
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004206#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4207 if (dbg_wwanemul) {
4208 tp_features.wan = 1;
4209 printk(TPACPI_INFO
4210 "wwan switch emulation enabled\n");
4211 } else
4212#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004213 if (tp_features.wan &&
4214 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4215 /* no wan hardware present in system */
4216 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004217 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004218 "wan hardware not installed\n");
4219 }
4220
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004221 if (!tp_features.wan)
4222 return 1;
4223
Johannes Berg19d337d2009-06-02 13:01:37 +02004224 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4225 &wan_tprfk_ops,
4226 RFKILL_TYPE_WWAN,
4227 TPACPI_RFK_WWAN_SW_NAME,
4228 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004229 if (res)
4230 return res;
4231
Johannes Berg19d337d2009-06-02 13:01:37 +02004232 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4233 &wan_attr_group);
4234
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004235 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004236 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004237 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004238 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004239
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004240 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004241}
4242
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004243/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004244static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004245{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004246 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004247}
4248
4249static int wan_write(char *buf)
4250{
Johannes Berg19d337d2009-06-02 13:01:37 +02004251 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004252}
4253
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004254static struct ibm_struct wan_driver_data = {
4255 .name = "wan",
4256 .read = wan_read,
4257 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004258 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004259 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004260};
4261
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004262/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004263 * UWB subdriver
4264 */
4265
4266enum {
4267 /* ACPI GUWB/SUWB bits */
4268 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4269 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4270};
4271
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004272#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4273
Johannes Berg19d337d2009-06-02 13:01:37 +02004274static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004275{
4276 int status;
4277
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004278#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4279 if (dbg_uwbemul)
4280 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004281 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004282#endif
4283
4284 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4285 return -EIO;
4286
4287 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004288 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004289}
4290
Johannes Berg19d337d2009-06-02 13:01:37 +02004291static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004292{
4293 int status;
4294
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004295 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004296 "will attempt to %s UWB\n",
4297 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004298
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004299#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4300 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004301 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004302 return 0;
4303 }
4304#endif
4305
Johannes Berg19d337d2009-06-02 13:01:37 +02004306 if (state == TPACPI_RFK_RADIO_ON)
4307 status = TP_ACPI_UWB_RADIOSSW;
4308 else
4309 status = 0;
4310
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004311 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4312 return -EIO;
4313
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004314 return 0;
4315}
4316
4317/* --------------------------------------------------------------------- */
4318
Johannes Berg19d337d2009-06-02 13:01:37 +02004319static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4320 .get_status = uwb_get_status,
4321 .set_status = uwb_set_status,
4322};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004323
4324static void uwb_exit(void)
4325{
Johannes Berg19d337d2009-06-02 13:01:37 +02004326 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004327}
4328
4329static int __init uwb_init(struct ibm_init_struct *iibm)
4330{
4331 int res;
4332 int status = 0;
4333
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004334 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4335 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004336
4337 TPACPI_ACPIHANDLE_INIT(hkey);
4338
4339 tp_features.uwb = hkey_handle &&
4340 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4341
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004342 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4343 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004344 str_supported(tp_features.uwb),
4345 status);
4346
4347#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4348 if (dbg_uwbemul) {
4349 tp_features.uwb = 1;
4350 printk(TPACPI_INFO
4351 "uwb switch emulation enabled\n");
4352 } else
4353#endif
4354 if (tp_features.uwb &&
4355 !(status & TP_ACPI_UWB_HWPRESENT)) {
4356 /* no uwb hardware present in system */
4357 tp_features.uwb = 0;
4358 dbg_printk(TPACPI_DBG_INIT,
4359 "uwb hardware not installed\n");
4360 }
4361
4362 if (!tp_features.uwb)
4363 return 1;
4364
4365 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004366 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004367 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004368 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004369 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004370 return res;
4371}
4372
4373static struct ibm_struct uwb_driver_data = {
4374 .name = "uwb",
4375 .exit = uwb_exit,
4376 .flags.experimental = 1,
4377};
4378
4379/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004380 * Video subdriver
4381 */
4382
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004383#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4384
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004385enum video_access_mode {
4386 TPACPI_VIDEO_NONE = 0,
4387 TPACPI_VIDEO_570, /* 570 */
4388 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4389 TPACPI_VIDEO_NEW, /* all others */
4390};
4391
4392enum { /* video status flags, based on VIDEO_570 */
4393 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4394 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4395 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4396};
4397
4398enum { /* TPACPI_VIDEO_570 constants */
4399 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4400 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4401 * video_status_flags */
4402 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4403 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4404};
4405
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004406static enum video_access_mode video_supported;
4407static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004408
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004409static int video_autosw_get(void);
4410static int video_autosw_set(int enable);
4411
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004412TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004413
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004414static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004416 int ivga;
4417
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004418 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4419
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004420 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004421 if (tpacpi_is_ibm())
4422 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004423
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004424 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4425 /* G41, assume IVGA doesn't change */
4426 vid_handle = vid2_handle;
4427
4428 if (!vid_handle)
4429 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004430 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004431 else if (tpacpi_is_ibm() &&
4432 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004433 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004434 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004435 else if (tpacpi_is_ibm() &&
4436 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004437 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004438 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004439 else
4440 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004441 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004443 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4444 str_supported(video_supported != TPACPI_VIDEO_NONE),
4445 video_supported);
4446
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004447 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448}
4449
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004450static void video_exit(void)
4451{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004452 dbg_printk(TPACPI_DBG_EXIT,
4453 "restoring original video autoswitch mode\n");
4454 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004455 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004456 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004457}
4458
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004459static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460{
4461 int status = 0;
4462 int i;
4463
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004464 switch (video_supported) {
4465 case TPACPI_VIDEO_570:
4466 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4467 TP_ACPI_VIDEO_570_PHSCMD))
4468 return -EIO;
4469 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4470 break;
4471 case TPACPI_VIDEO_770:
4472 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4473 return -EIO;
4474 if (i)
4475 status |= TP_ACPI_VIDEO_S_LCD;
4476 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4477 return -EIO;
4478 if (i)
4479 status |= TP_ACPI_VIDEO_S_CRT;
4480 break;
4481 case TPACPI_VIDEO_NEW:
4482 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4483 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4484 return -EIO;
4485 if (i)
4486 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004488 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4489 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4490 return -EIO;
4491 if (i)
4492 status |= TP_ACPI_VIDEO_S_LCD;
4493 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4494 return -EIO;
4495 if (i)
4496 status |= TP_ACPI_VIDEO_S_DVI;
4497 break;
4498 default:
4499 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
4502 return status;
4503}
4504
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004505static int video_outputsw_set(int status)
4506{
4507 int autosw;
4508 int res = 0;
4509
4510 switch (video_supported) {
4511 case TPACPI_VIDEO_570:
4512 res = acpi_evalf(NULL, NULL,
4513 "\\_SB.PHS2", "vdd",
4514 TP_ACPI_VIDEO_570_PHS2CMD,
4515 status | TP_ACPI_VIDEO_570_PHS2SET);
4516 break;
4517 case TPACPI_VIDEO_770:
4518 autosw = video_autosw_get();
4519 if (autosw < 0)
4520 return autosw;
4521
4522 res = video_autosw_set(1);
4523 if (res)
4524 return res;
4525 res = acpi_evalf(vid_handle, NULL,
4526 "ASWT", "vdd", status * 0x100, 0);
4527 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004528 printk(TPACPI_ERR
4529 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004530 return -EIO;
4531 }
4532 break;
4533 case TPACPI_VIDEO_NEW:
4534 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004535 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004536 break;
4537 default:
4538 return -ENOSYS;
4539 }
4540
4541 return (res)? 0 : -EIO;
4542}
4543
4544static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004546 int autosw = 0;
4547
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004548 switch (video_supported) {
4549 case TPACPI_VIDEO_570:
4550 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4551 return -EIO;
4552 break;
4553 case TPACPI_VIDEO_770:
4554 case TPACPI_VIDEO_NEW:
4555 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4556 return -EIO;
4557 break;
4558 default:
4559 return -ENOSYS;
4560 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004561
4562 return autosw & 1;
4563}
4564
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004565static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004566{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004567 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004568 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004569 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004570}
4571
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004572static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004573{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004574 int autosw = video_autosw_get();
4575 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004576
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004577 if (autosw < 0)
4578 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004579
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004580 switch (video_supported) {
4581 case TPACPI_VIDEO_570:
4582 res = video_autosw_set(1);
4583 if (res)
4584 return res;
4585 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4586 break;
4587 case TPACPI_VIDEO_770:
4588 case TPACPI_VIDEO_NEW:
4589 res = video_autosw_set(1);
4590 if (res)
4591 return res;
4592 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4593 break;
4594 default:
4595 return -ENOSYS;
4596 }
4597 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004598 printk(TPACPI_ERR
4599 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004600 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004601 }
4602
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004603 return (res)? 0 : -EIO;
4604}
4605
4606static int video_expand_toggle(void)
4607{
4608 switch (video_supported) {
4609 case TPACPI_VIDEO_570:
4610 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4611 0 : -EIO;
4612 case TPACPI_VIDEO_770:
4613 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4614 0 : -EIO;
4615 case TPACPI_VIDEO_NEW:
4616 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4617 0 : -EIO;
4618 default:
4619 return -ENOSYS;
4620 }
4621 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004622}
4623
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004624static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004625{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004626 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004627
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004628 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004629 seq_printf(m, "status:\t\tnot supported\n");
4630 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004631 }
4632
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004633 /* Even reads can crash X.org, so... */
4634 if (!capable(CAP_SYS_ADMIN))
4635 return -EPERM;
4636
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004637 status = video_outputsw_get();
4638 if (status < 0)
4639 return status;
4640
4641 autosw = video_autosw_get();
4642 if (autosw < 0)
4643 return autosw;
4644
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004645 seq_printf(m, "status:\t\tsupported\n");
4646 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4647 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004648 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004649 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4650 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4651 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4652 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004653 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004654 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4655 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4656 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004657
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004658 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004659}
4660
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004661static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662{
4663 char *cmd;
4664 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004665 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004667 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004668 return -ENODEV;
4669
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004670 /* Even reads can crash X.org, let alone writes... */
4671 if (!capable(CAP_SYS_ADMIN))
4672 return -EPERM;
4673
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004674 enable = 0;
4675 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
4677 while ((cmd = next_cmd(&buf))) {
4678 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004679 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004681 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004683 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004685 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004686 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004687 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004688 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004689 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004690 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004691 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004693 res = video_autosw_set(1);
4694 if (res)
4695 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004697 res = video_autosw_set(0);
4698 if (res)
4699 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004701 res = video_outputsw_cycle();
4702 if (res)
4703 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004705 res = video_expand_toggle();
4706 if (res)
4707 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 } else
4709 return -EINVAL;
4710 }
4711
4712 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004713 status = video_outputsw_get();
4714 if (status < 0)
4715 return status;
4716 res = video_outputsw_set((status & ~disable) | enable);
4717 if (res)
4718 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 }
4720
4721 return 0;
4722}
4723
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004724static struct ibm_struct video_driver_data = {
4725 .name = "video",
4726 .read = video_read,
4727 .write = video_write,
4728 .exit = video_exit,
4729};
4730
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004731#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4732
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004733/*************************************************************************
4734 * Light (thinklight) subdriver
4735 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004737TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4738TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004739
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004740static int light_get_status(void)
4741{
4742 int status = 0;
4743
4744 if (tp_features.light_status) {
4745 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4746 return -EIO;
4747 return (!!status);
4748 }
4749
4750 return -ENXIO;
4751}
4752
4753static int light_set_status(int status)
4754{
4755 int rc;
4756
4757 if (tp_features.light) {
4758 if (cmos_handle) {
4759 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4760 (status)?
4761 TP_CMOS_THINKLIGHT_ON :
4762 TP_CMOS_THINKLIGHT_OFF);
4763 } else {
4764 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4765 (status)? 1 : 0);
4766 }
4767 return (rc)? 0 : -EIO;
4768 }
4769
4770 return -ENXIO;
4771}
4772
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004773static void light_set_status_worker(struct work_struct *work)
4774{
4775 struct tpacpi_led_classdev *data =
4776 container_of(work, struct tpacpi_led_classdev, work);
4777
4778 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004779 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004780}
4781
4782static void light_sysfs_set(struct led_classdev *led_cdev,
4783 enum led_brightness brightness)
4784{
4785 struct tpacpi_led_classdev *data =
4786 container_of(led_cdev,
4787 struct tpacpi_led_classdev,
4788 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004789 data->new_state = (brightness != LED_OFF) ?
4790 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004791 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004792}
4793
4794static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4795{
4796 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4797}
4798
4799static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4800 .led_classdev = {
4801 .name = "tpacpi::thinklight",
4802 .brightness_set = &light_sysfs_set,
4803 .brightness_get = &light_sysfs_get,
4804 }
4805};
4806
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004807static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004809 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004810
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004811 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4812
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004813 if (tpacpi_is_ibm()) {
4814 TPACPI_ACPIHANDLE_INIT(ledb);
4815 TPACPI_ACPIHANDLE_INIT(lght);
4816 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004817 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004818 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004819
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004820 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004821 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004822
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004823 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004824 /* light status not supported on
4825 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004826 tp_features.light_status =
4827 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004829 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4830 str_supported(tp_features.light),
4831 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004832
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004833 if (!tp_features.light)
4834 return 1;
4835
4836 rc = led_classdev_register(&tpacpi_pdev->dev,
4837 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004838
4839 if (rc < 0) {
4840 tp_features.light = 0;
4841 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004842 } else {
4843 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004844 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004845
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004846 return rc;
4847}
4848
4849static void light_exit(void)
4850{
4851 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4852 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004853 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854}
4855
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004856static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004858 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004860 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004861 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004862 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004863 seq_printf(m, "status:\t\tunknown\n");
4864 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004865 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004866 status = light_get_status();
4867 if (status < 0)
4868 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004869 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4870 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004873 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874}
4875
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004876static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004879 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004880
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004881 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004882 return -ENODEV;
4883
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 while ((cmd = next_cmd(&buf))) {
4885 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004886 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004888 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 } else
4890 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 }
4892
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004893 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894}
4895
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004896static struct ibm_struct light_driver_data = {
4897 .name = "light",
4898 .read = light_read,
4899 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004900 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004901};
4902
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004903/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004904 * CMOS subdriver
4905 */
4906
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004907/* sysfs cmos_command -------------------------------------------------- */
4908static ssize_t cmos_command_store(struct device *dev,
4909 struct device_attribute *attr,
4910 const char *buf, size_t count)
4911{
4912 unsigned long cmos_cmd;
4913 int res;
4914
4915 if (parse_strtoul(buf, 21, &cmos_cmd))
4916 return -EINVAL;
4917
4918 res = issue_thinkpad_cmos_command(cmos_cmd);
4919 return (res)? res : count;
4920}
4921
4922static struct device_attribute dev_attr_cmos_command =
4923 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4924
4925/* --------------------------------------------------------------------- */
4926
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004927static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004928{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004929 int res;
4930
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004931 vdbg_printk(TPACPI_DBG_INIT,
4932 "initializing cmos commands subdriver\n");
4933
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004934 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004935
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004936 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4937 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004938
4939 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4940 if (res)
4941 return res;
4942
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004943 return (cmos_handle)? 0 : 1;
4944}
4945
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004946static void cmos_exit(void)
4947{
4948 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4949}
4950
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004951static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004953 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4954 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004956 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004958 seq_printf(m, "status:\t\tsupported\n");
4959 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 }
4961
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004962 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963}
4964
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004965static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966{
4967 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004968 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
4970 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004971 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4972 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 /* cmos_cmd set */
4974 } else
4975 return -EINVAL;
4976
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004977 res = issue_thinkpad_cmos_command(cmos_cmd);
4978 if (res)
4979 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 }
4981
4982 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004983}
4984
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004985static struct ibm_struct cmos_driver_data = {
4986 .name = "cmos",
4987 .read = cmos_read,
4988 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004989 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004990};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004991
4992/*************************************************************************
4993 * LED subdriver
4994 */
4995
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004996enum led_access_mode {
4997 TPACPI_LED_NONE = 0,
4998 TPACPI_LED_570, /* 570 */
4999 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5000 TPACPI_LED_NEW, /* all others */
5001};
5002
5003enum { /* For TPACPI_LED_OLD */
5004 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5005 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5006 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5007};
5008
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005009static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005010
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005011TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005012 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
5013 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005014 "LED", /* all others */
5015 ); /* R30, R31 */
5016
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005017#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005018static struct tpacpi_led_classdev *tpacpi_leds;
5019static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005020static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005021 /* there's a limit of 19 chars + NULL before 2.6.26 */
5022 "tpacpi::power",
5023 "tpacpi:orange:batt",
5024 "tpacpi:green:batt",
5025 "tpacpi::dock_active",
5026 "tpacpi::bay_active",
5027 "tpacpi::dock_batt",
5028 "tpacpi::unknown_led",
5029 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005030 "tpacpi::dock_status1",
5031 "tpacpi::dock_status2",
5032 "tpacpi::unknown_led2",
5033 "tpacpi::unknown_led3",
5034 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005035};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005036#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005037
5038static inline bool tpacpi_is_led_restricted(const unsigned int led)
5039{
5040#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5041 return false;
5042#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005043 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005044#endif
5045}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005046
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005047static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005048{
5049 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005050 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005051
5052 switch (led_supported) {
5053 case TPACPI_LED_570:
5054 if (!acpi_evalf(ec_handle,
5055 &status, "GLED", "dd", 1 << led))
5056 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005057 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005058 TPACPI_LED_OFF :
5059 ((status == 1)?
5060 TPACPI_LED_ON :
5061 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005062 tpacpi_led_state_cache[led] = led_s;
5063 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005064 default:
5065 return -ENXIO;
5066 }
5067
5068 /* not reached */
5069}
5070
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005071static int led_set_status(const unsigned int led,
5072 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005073{
5074 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005075 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5076 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005077
5078 int rc = 0;
5079
5080 switch (led_supported) {
5081 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005082 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005083 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005084 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005085 if (unlikely(tpacpi_is_led_restricted(led)))
5086 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005087 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5088 (1 << led), led_sled_arg1[ledstatus]))
5089 rc = -EIO;
5090 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005091 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005092 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005093 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005094 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005095 if (unlikely(tpacpi_is_led_restricted(led)))
5096 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005097 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5098 if (rc >= 0)
5099 rc = ec_write(TPACPI_LED_EC_HLBL,
5100 (ledstatus == TPACPI_LED_BLINK) << led);
5101 if (rc >= 0)
5102 rc = ec_write(TPACPI_LED_EC_HLCL,
5103 (ledstatus != TPACPI_LED_OFF) << led);
5104 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005105 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005106 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005107 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5108 return -EINVAL;
5109 if (unlikely(tpacpi_is_led_restricted(led)))
5110 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005111 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5112 led, led_led_arg1[ledstatus]))
5113 rc = -EIO;
5114 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005115 default:
5116 rc = -ENXIO;
5117 }
5118
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005119 if (!rc)
5120 tpacpi_led_state_cache[led] = ledstatus;
5121
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005122 return rc;
5123}
5124
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005125static void led_set_status_worker(struct work_struct *work)
5126{
5127 struct tpacpi_led_classdev *data =
5128 container_of(work, struct tpacpi_led_classdev, work);
5129
5130 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005131 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005132}
5133
5134static void led_sysfs_set(struct led_classdev *led_cdev,
5135 enum led_brightness brightness)
5136{
5137 struct tpacpi_led_classdev *data = container_of(led_cdev,
5138 struct tpacpi_led_classdev, led_classdev);
5139
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005140 if (brightness == LED_OFF)
5141 data->new_state = TPACPI_LED_OFF;
5142 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5143 data->new_state = TPACPI_LED_ON;
5144 else
5145 data->new_state = TPACPI_LED_BLINK;
5146
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005147 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005148}
5149
5150static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5151 unsigned long *delay_on, unsigned long *delay_off)
5152{
5153 struct tpacpi_led_classdev *data = container_of(led_cdev,
5154 struct tpacpi_led_classdev, led_classdev);
5155
5156 /* Can we choose the flash rate? */
5157 if (*delay_on == 0 && *delay_off == 0) {
5158 /* yes. set them to the hardware blink rate (1 Hz) */
5159 *delay_on = 500; /* ms */
5160 *delay_off = 500; /* ms */
5161 } else if ((*delay_on != 500) || (*delay_off != 500))
5162 return -EINVAL;
5163
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005164 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005165 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005166
5167 return 0;
5168}
5169
5170static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5171{
5172 int rc;
5173
5174 struct tpacpi_led_classdev *data = container_of(led_cdev,
5175 struct tpacpi_led_classdev, led_classdev);
5176
5177 rc = led_get_status(data->led);
5178
5179 if (rc == TPACPI_LED_OFF || rc < 0)
5180 rc = LED_OFF; /* no error handling in led class :( */
5181 else
5182 rc = LED_FULL;
5183
5184 return rc;
5185}
5186
5187static void led_exit(void)
5188{
5189 unsigned int i;
5190
5191 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5192 if (tpacpi_leds[i].led_classdev.name)
5193 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5194 }
5195
5196 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005197}
5198
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005199static int __init tpacpi_init_led(unsigned int led)
5200{
5201 int rc;
5202
5203 tpacpi_leds[led].led = led;
5204
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005205 /* LEDs with no name don't get registered */
5206 if (!tpacpi_led_names[led])
5207 return 0;
5208
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005209 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5210 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5211 if (led_supported == TPACPI_LED_570)
5212 tpacpi_leds[led].led_classdev.brightness_get =
5213 &led_sysfs_get;
5214
5215 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5216
5217 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5218
5219 rc = led_classdev_register(&tpacpi_pdev->dev,
5220 &tpacpi_leds[led].led_classdev);
5221 if (rc < 0)
5222 tpacpi_leds[led].led_classdev.name = NULL;
5223
5224 return rc;
5225}
5226
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005227static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5228 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5229 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5230 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5231
5232 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5233 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5234 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5235 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5236 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5237 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5238 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5239 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5240
5241 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5242 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5243 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5244 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5245 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5246
5247 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5248 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5249 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5250 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5251
5252 /* (1) - may have excess leds enabled on MSB */
5253
5254 /* Defaults (order matters, keep last, don't reorder!) */
5255 { /* Lenovo */
5256 .vendor = PCI_VENDOR_ID_LENOVO,
5257 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5258 .quirks = 0x1fffU,
5259 },
5260 { /* IBM ThinkPads with no EC version string */
5261 .vendor = PCI_VENDOR_ID_IBM,
5262 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5263 .quirks = 0x00ffU,
5264 },
5265 { /* IBM ThinkPads with EC version string */
5266 .vendor = PCI_VENDOR_ID_IBM,
5267 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5268 .quirks = 0x00bfU,
5269 },
5270};
5271
5272#undef TPACPI_LEDQ_IBM
5273#undef TPACPI_LEDQ_LNV
5274
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005275static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005276{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005277 unsigned int i;
5278 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005279 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005280
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005281 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5282
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005283 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005284
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005285 if (!led_handle)
5286 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005287 led_supported = TPACPI_LED_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005288 else if (tpacpi_is_ibm() && strlencmp(led_path, "SLED") == 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005289 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005290 led_supported = TPACPI_LED_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005291 else if (tpacpi_is_ibm() && strlencmp(led_path, "SYSL") == 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005292 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005293 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005294 else
5295 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005296 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005297
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005298 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5299 str_supported(led_supported), led_supported);
5300
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005301 if (led_supported == TPACPI_LED_NONE)
5302 return 1;
5303
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005304 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5305 GFP_KERNEL);
5306 if (!tpacpi_leds) {
5307 printk(TPACPI_ERR "Out of memory for LED data\n");
5308 return -ENOMEM;
5309 }
5310
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005311 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5312 ARRAY_SIZE(led_useful_qtable));
5313
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005314 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005315 if (!tpacpi_is_led_restricted(i) &&
5316 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005317 rc = tpacpi_init_led(i);
5318 if (rc < 0) {
5319 led_exit();
5320 return rc;
5321 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005322 }
5323 }
5324
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005325#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005326 printk(TPACPI_NOTICE
5327 "warning: userspace override of important "
5328 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005329#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005330 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005331}
5332
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005333#define str_led_status(s) \
5334 ((s) == TPACPI_LED_OFF ? "off" : \
5335 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005336
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005337static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005339 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005340 seq_printf(m, "status:\t\tnot supported\n");
5341 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005342 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005343 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005344
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005345 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005346 /* 570 */
5347 int i, status;
5348 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005349 status = led_get_status(i);
5350 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005351 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005352 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005353 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005354 }
5355 }
5356
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005357 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005358 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361}
5362
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005363static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364{
5365 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005366 int led, rc;
5367 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005368
5369 if (!led_supported)
5370 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371
5372 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005373 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 return -EINVAL;
5375
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005376 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005377 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005379 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005380 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005381 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005382 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005383 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005384 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005385
5386 rc = led_set_status(led, s);
5387 if (rc < 0)
5388 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 }
5390
5391 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005392}
5393
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005394static struct ibm_struct led_driver_data = {
5395 .name = "led",
5396 .read = led_read,
5397 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005398 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005399};
5400
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005401/*************************************************************************
5402 * Beep subdriver
5403 */
5404
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005405TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005406
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005407#define TPACPI_BEEP_Q1 0x0001
5408
5409static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5410 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5411 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5412};
5413
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005414static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005415{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005416 unsigned long quirks;
5417
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005418 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5419
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005420 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005421
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005422 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5423 str_supported(beep_handle != NULL));
5424
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005425 quirks = tpacpi_check_quirks(beep_quirk_table,
5426 ARRAY_SIZE(beep_quirk_table));
5427
5428 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5429
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005430 return (beep_handle)? 0 : 1;
5431}
5432
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005433static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005435 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005436 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005437 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005438 seq_printf(m, "status:\t\tsupported\n");
5439 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443}
5444
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005445static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446{
5447 char *cmd;
5448 int beep_cmd;
5449
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005450 if (!beep_handle)
5451 return -ENODEV;
5452
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005454 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5455 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 /* beep_cmd set */
5457 } else
5458 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005459 if (tp_features.beep_needs_two_args) {
5460 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5461 beep_cmd, 0))
5462 return -EIO;
5463 } else {
5464 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5465 beep_cmd))
5466 return -EIO;
5467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 }
5469
5470 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005471}
5472
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005473static struct ibm_struct beep_driver_data = {
5474 .name = "beep",
5475 .read = beep_read,
5476 .write = beep_write,
5477};
5478
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005479/*************************************************************************
5480 * Thermal subdriver
5481 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005482
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005483enum thermal_access_mode {
5484 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5485 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5486 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5487 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5488 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5489};
5490
5491enum { /* TPACPI_THERMAL_TPEC_* */
5492 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5493 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5494 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005495
5496 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005497};
5498
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005499
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005500#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5501struct ibm_thermal_sensors_struct {
5502 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5503};
5504
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005505static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005506
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005507/* idx is zero-based */
5508static int thermal_get_sensor(int idx, s32 *value)
5509{
5510 int t;
5511 s8 tmp;
5512 char tmpi[5];
5513
5514 t = TP_EC_THERMAL_TMP0;
5515
5516 switch (thermal_read_mode) {
5517#if TPACPI_MAX_THERMAL_SENSORS >= 16
5518 case TPACPI_THERMAL_TPEC_16:
5519 if (idx >= 8 && idx <= 15) {
5520 t = TP_EC_THERMAL_TMP8;
5521 idx -= 8;
5522 }
5523 /* fallthrough */
5524#endif
5525 case TPACPI_THERMAL_TPEC_8:
5526 if (idx <= 7) {
5527 if (!acpi_ec_read(t + idx, &tmp))
5528 return -EIO;
5529 *value = tmp * 1000;
5530 return 0;
5531 }
5532 break;
5533
5534 case TPACPI_THERMAL_ACPI_UPDT:
5535 if (idx <= 7) {
5536 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5537 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5538 return -EIO;
5539 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5540 return -EIO;
5541 *value = (t - 2732) * 100;
5542 return 0;
5543 }
5544 break;
5545
5546 case TPACPI_THERMAL_ACPI_TMP07:
5547 if (idx <= 7) {
5548 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5549 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5550 return -EIO;
5551 if (t > 127 || t < -127)
5552 t = TP_EC_THERMAL_TMP_NA;
5553 *value = t * 1000;
5554 return 0;
5555 }
5556 break;
5557
5558 case TPACPI_THERMAL_NONE:
5559 default:
5560 return -ENOSYS;
5561 }
5562
5563 return -EINVAL;
5564}
5565
5566static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5567{
5568 int res, i;
5569 int n;
5570
5571 n = 8;
5572 i = 0;
5573
5574 if (!s)
5575 return -EINVAL;
5576
5577 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5578 n = 16;
5579
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005580 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005581 res = thermal_get_sensor(i, &s->temp[i]);
5582 if (res)
5583 return res;
5584 }
5585
5586 return n;
5587}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005588
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005589static void thermal_dump_all_sensors(void)
5590{
5591 int n, i;
5592 struct ibm_thermal_sensors_struct t;
5593
5594 n = thermal_get_sensors(&t);
5595 if (n <= 0)
5596 return;
5597
5598 printk(TPACPI_NOTICE
5599 "temperatures (Celsius):");
5600
5601 for (i = 0; i < n; i++) {
5602 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5603 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5604 else
5605 printk(KERN_CONT " N/A");
5606 }
5607
5608 printk(KERN_CONT "\n");
5609}
5610
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005611/* sysfs temp##_input -------------------------------------------------- */
5612
5613static ssize_t thermal_temp_input_show(struct device *dev,
5614 struct device_attribute *attr,
5615 char *buf)
5616{
5617 struct sensor_device_attribute *sensor_attr =
5618 to_sensor_dev_attr(attr);
5619 int idx = sensor_attr->index;
5620 s32 value;
5621 int res;
5622
5623 res = thermal_get_sensor(idx, &value);
5624 if (res)
5625 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005626 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005627 return -ENXIO;
5628
5629 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5630}
5631
5632#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005633 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5634 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005635
5636static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5637 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5638 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5639 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5640 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5641 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5642 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5643 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5644 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5645 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5646 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5647 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5648 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5649 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5650 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5651 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5652 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5653};
5654
5655#define THERMAL_ATTRS(X) \
5656 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5657
5658static struct attribute *thermal_temp_input_attr[] = {
5659 THERMAL_ATTRS(8),
5660 THERMAL_ATTRS(9),
5661 THERMAL_ATTRS(10),
5662 THERMAL_ATTRS(11),
5663 THERMAL_ATTRS(12),
5664 THERMAL_ATTRS(13),
5665 THERMAL_ATTRS(14),
5666 THERMAL_ATTRS(15),
5667 THERMAL_ATTRS(0),
5668 THERMAL_ATTRS(1),
5669 THERMAL_ATTRS(2),
5670 THERMAL_ATTRS(3),
5671 THERMAL_ATTRS(4),
5672 THERMAL_ATTRS(5),
5673 THERMAL_ATTRS(6),
5674 THERMAL_ATTRS(7),
5675 NULL
5676};
5677
5678static const struct attribute_group thermal_temp_input16_group = {
5679 .attrs = thermal_temp_input_attr
5680};
5681
5682static const struct attribute_group thermal_temp_input8_group = {
5683 .attrs = &thermal_temp_input_attr[8]
5684};
5685
5686#undef THERMAL_SENSOR_ATTR_TEMP
5687#undef THERMAL_ATTRS
5688
5689/* --------------------------------------------------------------------- */
5690
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005691static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005692{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005693 u8 t, ta1, ta2;
5694 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005695 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005696 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005697
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005698 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5699
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005700 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005701
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005702 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005703 /*
5704 * Direct EC access mode: sensors at registers
5705 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5706 * non-implemented, thermal sensors return 0x80 when
5707 * not available
5708 */
5709
5710 ta1 = ta2 = 0;
5711 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005712 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005713 ta1 |= t;
5714 } else {
5715 ta1 = 0;
5716 break;
5717 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005718 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005719 ta2 |= t;
5720 } else {
5721 ta1 = 0;
5722 break;
5723 }
5724 }
5725 if (ta1 == 0) {
5726 /* This is sheer paranoia, but we handle it anyway */
5727 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005728 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005729 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005730 "falling back to ACPI TMPx access "
5731 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005732 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005733 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005734 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005735 "ThinkPad ACPI EC access misbehaving, "
5736 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005737 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005738 }
5739 } else {
5740 thermal_read_mode =
5741 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005742 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005743 }
5744 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005745 if (tpacpi_is_ibm() &&
5746 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005747 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005748 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005749 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005750 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005751 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005752 }
5753 } else {
5754 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005755 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005756 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005757
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005758 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5759 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5760 thermal_read_mode);
5761
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005762 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005763 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005764 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005765 &thermal_temp_input16_group);
5766 if (res)
5767 return res;
5768 break;
5769 case TPACPI_THERMAL_TPEC_8:
5770 case TPACPI_THERMAL_ACPI_TMP07:
5771 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005772 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005773 &thermal_temp_input8_group);
5774 if (res)
5775 return res;
5776 break;
5777 case TPACPI_THERMAL_NONE:
5778 default:
5779 return 1;
5780 }
5781
5782 return 0;
5783}
5784
5785static void thermal_exit(void)
5786{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005787 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005788 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005789 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005790 &thermal_temp_input16_group);
5791 break;
5792 case TPACPI_THERMAL_TPEC_8:
5793 case TPACPI_THERMAL_ACPI_TMP07:
5794 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005795 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08005796 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005797 break;
5798 case TPACPI_THERMAL_NONE:
5799 default:
5800 break;
5801 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005802}
5803
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005804static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005805{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005806 int n, i;
5807 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005808
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005809 n = thermal_get_sensors(&t);
5810 if (unlikely(n < 0))
5811 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005812
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005813 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005814
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005815 if (n > 0) {
5816 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005817 seq_printf(m, "%d ", t.temp[i] / 1000);
5818 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005819 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005820 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005821
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005822 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005823}
5824
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005825static struct ibm_struct thermal_driver_data = {
5826 .name = "thermal",
5827 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005828 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005829};
5830
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005831/*************************************************************************
5832 * EC Dump subdriver
5833 */
5834
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005835static u8 ecdump_regs[256];
5836
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005837static int ecdump_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005838{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005839 int i, j;
5840 u8 v;
5841
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005842 seq_printf(m, "EC "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005843 " +00 +01 +02 +03 +04 +05 +06 +07"
5844 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5845 for (i = 0; i < 256; i += 16) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005846 seq_printf(m, "EC 0x%02x:", i);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005847 for (j = 0; j < 16; j++) {
5848 if (!acpi_ec_read(i + j, &v))
5849 break;
5850 if (v != ecdump_regs[i + j])
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005851 seq_printf(m, " *%02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005852 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005853 seq_printf(m, " %02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005854 ecdump_regs[i + j] = v;
5855 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005856 seq_putc(m, '\n');
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005857 if (j != 16)
5858 break;
5859 }
5860
5861 /* These are way too dangerous to advertise openly... */
5862#if 0
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005863 seq_printf(m, "commands:\t0x<offset> 0x<value>"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005864 " (<offset> is 00-ff, <value> is 00-ff)\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005865 seq_printf(m, "commands:\t0x<offset> <value> "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005866 " (<offset> is 00-ff, <value> is 0-255)\n");
5867#endif
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005868 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005869}
5870
5871static int ecdump_write(char *buf)
5872{
5873 char *cmd;
5874 int i, v;
5875
5876 while ((cmd = next_cmd(&buf))) {
5877 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5878 /* i and v set */
5879 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5880 /* i and v set */
5881 } else
5882 return -EINVAL;
5883 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5884 if (!acpi_ec_write(i, v))
5885 return -EIO;
5886 } else
5887 return -EINVAL;
5888 }
5889
5890 return 0;
5891}
5892
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005893static struct ibm_struct ecdump_driver_data = {
5894 .name = "ecdump",
5895 .read = ecdump_read,
5896 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005897 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005898};
5899
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005900/*************************************************************************
5901 * Backlight/brightness subdriver
5902 */
Holger Macht8acb0252006-10-20 14:30:28 -07005903
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005904#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5905
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005906/*
5907 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5908 * CMOS NVRAM byte 0x5E, bits 0-3.
5909 *
5910 * EC HBRV (0x31) has the following layout
5911 * Bit 7: unknown function
5912 * Bit 6: unknown function
5913 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5914 * Bit 4: must be set to zero to avoid problems
5915 * Bit 3-0: backlight brightness level
5916 *
5917 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005918 *
5919 * WARNING: The X61 has been verified to use HBRV for something else, so
5920 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5921 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005922 */
5923
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005924enum {
5925 TP_EC_BACKLIGHT = 0x31,
5926
5927 /* TP_EC_BACKLIGHT bitmasks */
5928 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5929 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5930 TP_EC_BACKLIGHT_MAPSW = 0x20,
5931};
5932
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005933enum tpacpi_brightness_access_mode {
5934 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5935 TPACPI_BRGHT_MODE_EC, /* EC control */
5936 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5937 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5938 TPACPI_BRGHT_MODE_MAX
5939};
5940
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005941static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005942
5943static enum tpacpi_brightness_access_mode brightness_mode =
5944 TPACPI_BRGHT_MODE_MAX;
5945
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005946static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5947
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005948static struct mutex brightness_mutex;
5949
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005950/* NVRAM brightness access,
5951 * call with brightness_mutex held! */
5952static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005953{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005954 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005955
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005956 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5957 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5958 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03005959 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005960
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005961 return lnvram;
5962}
5963
5964static void tpacpi_brightness_checkpoint_nvram(void)
5965{
5966 u8 lec = 0;
5967 u8 b_nvram;
5968
5969 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5970 return;
5971
5972 vdbg_printk(TPACPI_DBG_BRGHT,
5973 "trying to checkpoint backlight level to NVRAM...\n");
5974
5975 if (mutex_lock_killable(&brightness_mutex) < 0)
5976 return;
5977
5978 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5979 goto unlock;
5980 lec &= TP_EC_BACKLIGHT_LVLMSK;
5981 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5982
5983 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5984 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5985 /* NVRAM needs update */
5986 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5987 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5988 b_nvram |= lec;
5989 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5990 dbg_printk(TPACPI_DBG_BRGHT,
5991 "updated NVRAM backlight level to %u (0x%02x)\n",
5992 (unsigned int) lec, (unsigned int) b_nvram);
5993 } else
5994 vdbg_printk(TPACPI_DBG_BRGHT,
5995 "NVRAM backlight level already is %u (0x%02x)\n",
5996 (unsigned int) lec, (unsigned int) b_nvram);
5997
5998unlock:
5999 mutex_unlock(&brightness_mutex);
6000}
6001
6002
6003/* call with brightness_mutex held! */
6004static int tpacpi_brightness_get_raw(int *status)
6005{
6006 u8 lec = 0;
6007
6008 switch (brightness_mode) {
6009 case TPACPI_BRGHT_MODE_UCMS_STEP:
6010 *status = tpacpi_brightness_nvram_get();
6011 return 0;
6012 case TPACPI_BRGHT_MODE_EC:
6013 case TPACPI_BRGHT_MODE_ECNVRAM:
6014 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006015 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006016 *status = lec;
6017 return 0;
6018 default:
6019 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006020 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006021}
6022
6023/* call with brightness_mutex held! */
6024/* do NOT call with illegal backlight level value */
6025static int tpacpi_brightness_set_ec(unsigned int value)
6026{
6027 u8 lec = 0;
6028
6029 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6030 return -EIO;
6031
6032 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6033 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6034 (value & TP_EC_BACKLIGHT_LVLMSK))))
6035 return -EIO;
6036
6037 return 0;
6038}
6039
6040/* call with brightness_mutex held! */
6041static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6042{
6043 int cmos_cmd, inc;
6044 unsigned int current_value, i;
6045
6046 current_value = tpacpi_brightness_nvram_get();
6047
6048 if (value == current_value)
6049 return 0;
6050
6051 cmos_cmd = (value > current_value) ?
6052 TP_CMOS_BRIGHTNESS_UP :
6053 TP_CMOS_BRIGHTNESS_DOWN;
6054 inc = (value > current_value) ? 1 : -1;
6055
6056 for (i = current_value; i != value; i += inc)
6057 if (issue_thinkpad_cmos_command(cmos_cmd))
6058 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006059
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006060 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006061}
6062
6063/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006064static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006065{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006066 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006067
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006068 if (value > bright_maxlvl || value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006069 return -EINVAL;
6070
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006071 vdbg_printk(TPACPI_DBG_BRGHT,
6072 "set backlight level to %d\n", value);
6073
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006074 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006075 if (res < 0)
6076 return res;
6077
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006078 switch (brightness_mode) {
6079 case TPACPI_BRGHT_MODE_EC:
6080 case TPACPI_BRGHT_MODE_ECNVRAM:
6081 res = tpacpi_brightness_set_ec(value);
6082 break;
6083 case TPACPI_BRGHT_MODE_UCMS_STEP:
6084 res = tpacpi_brightness_set_ucmsstep(value);
6085 break;
6086 default:
6087 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006088 }
6089
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006090 mutex_unlock(&brightness_mutex);
6091 return res;
6092}
6093
6094/* sysfs backlight class ----------------------------------------------- */
6095
6096static int brightness_update_status(struct backlight_device *bd)
6097{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006098 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006099 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6100 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006101 bd->props.brightness : 0;
6102
6103 dbg_printk(TPACPI_DBG_BRGHT,
6104 "backlight: attempt to set level to %d\n",
6105 level);
6106
6107 /* it is the backlight class's job (caller) to handle
6108 * EINTR and other errors properly */
6109 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006110}
Holger Macht8acb0252006-10-20 14:30:28 -07006111
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006112static int brightness_get(struct backlight_device *bd)
6113{
6114 int status, res;
6115
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006116 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006117 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006118 return 0;
6119
6120 res = tpacpi_brightness_get_raw(&status);
6121
6122 mutex_unlock(&brightness_mutex);
6123
6124 if (res < 0)
6125 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006126
6127 return status & TP_EC_BACKLIGHT_LVLMSK;
6128}
6129
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006130static void tpacpi_brightness_notify_change(void)
6131{
6132 backlight_force_update(ibm_backlight_device,
6133 BACKLIGHT_UPDATE_HOTKEY);
6134}
6135
Richard Purdie599a52d2007-02-10 23:07:48 +00006136static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006137 .get_brightness = brightness_get,
6138 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006139};
6140
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006141/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006142
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006143static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6144{
6145 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6146 union acpi_object *obj;
6147 int rc;
6148
6149 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
6150 obj = (union acpi_object *)buffer.pointer;
6151 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6152 printk(TPACPI_ERR "Unknown _BCL data, "
6153 "please report this to %s\n", TPACPI_MAIL);
6154 rc = 0;
6155 } else {
6156 rc = obj->package.count;
6157 }
6158 } else {
6159 return 0;
6160 }
6161
6162 kfree(buffer.pointer);
6163 return rc;
6164}
6165
6166static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
6167 u32 lvl, void *context, void **rv)
6168{
6169 char name[ACPI_PATH_SEGMENT_LENGTH];
6170 struct acpi_buffer buffer = { sizeof(name), &name };
6171
6172 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
6173 !strncmp("_BCL", name, sizeof(name) - 1)) {
6174 BUG_ON(!rv || !*rv);
6175 **(int **)rv = tpacpi_query_bcl_levels(handle);
6176 return AE_CTRL_TERMINATE;
6177 } else {
6178 return AE_OK;
6179 }
6180}
6181
6182/*
6183 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6184 */
6185static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6186{
6187 int status;
6188 int bcl_levels = 0;
6189 void *bcl_ptr = &bcl_levels;
6190
6191 if (!vid_handle)
6192 TPACPI_ACPIHANDLE_INIT(vid);
6193
6194 if (!vid_handle)
6195 return 0;
6196
6197 /*
6198 * Search for a _BCL method, and execute it. This is safe on all
6199 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
6200 * BIOS in ACPI backlight control mode. We do NOT have to care
6201 * about calling the _BCL method in an enabled video device, any
6202 * will do for our purposes.
6203 */
6204
6205 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
6206 tpacpi_acpi_walk_find_bcl, NULL, NULL,
6207 &bcl_ptr);
6208
6209 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
6210 tp_features.bright_acpimode = 1;
6211 return bcl_levels - 2;
6212 }
6213
6214 return 0;
6215}
6216
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006217/*
6218 * These are only useful for models that have only one possibility
6219 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6220 * these quirks.
6221 */
6222#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6223#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6224#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6225
6226static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6227 /* Models with ATI GPUs known to require ECNVRAM mode */
6228 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6229
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006230 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006231 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006232 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006233 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006234 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6235
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006236 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006237 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006238 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6239 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006240
6241 /* Models with Intel GMA900 */
6242 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6243 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6244 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6245};
6246
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006247/*
6248 * Returns < 0 for error, otherwise sets tp_features.bright_*
6249 * and bright_maxlvl.
6250 */
6251static void __init tpacpi_detect_brightness_capabilities(void)
6252{
6253 unsigned int b;
6254
6255 vdbg_printk(TPACPI_DBG_INIT,
6256 "detecting firmware brightness interface capabilities\n");
6257
6258 /* we could run a quirks check here (same table used by
6259 * brightness_init) if needed */
6260
6261 /*
6262 * We always attempt to detect acpi support, so as to switch
6263 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6264 * going to publish a backlight interface
6265 */
6266 b = tpacpi_check_std_acpi_brightness_support();
6267 switch (b) {
6268 case 16:
6269 bright_maxlvl = 15;
6270 printk(TPACPI_INFO
6271 "detected a 16-level brightness capable ThinkPad\n");
6272 break;
6273 case 8:
6274 case 0:
6275 bright_maxlvl = 7;
6276 printk(TPACPI_INFO
6277 "detected a 8-level brightness capable ThinkPad\n");
6278 break;
6279 default:
6280 printk(TPACPI_ERR
6281 "Unsupported brightness interface, "
6282 "please contact %s\n", TPACPI_MAIL);
6283 tp_features.bright_unkfw = 1;
6284 bright_maxlvl = b - 1;
6285 }
6286}
6287
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006288static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006289{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006290 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006291 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006292 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006293
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006294 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6295
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006296 mutex_init(&brightness_mutex);
6297
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006298 quirks = tpacpi_check_quirks(brightness_quirk_table,
6299 ARRAY_SIZE(brightness_quirk_table));
6300
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006301 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006302
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006303 /* if it is unknown, we don't handle it: it wouldn't be safe */
6304 if (tp_features.bright_unkfw)
6305 return 1;
6306
6307 if (tp_features.bright_acpimode) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006308 if (acpi_video_backlight_support()) {
6309 if (brightness_enable > 1) {
6310 printk(TPACPI_NOTICE
6311 "Standard ACPI backlight interface "
6312 "available, not loading native one.\n");
6313 return 1;
6314 } else if (brightness_enable == 1) {
6315 printk(TPACPI_NOTICE
6316 "Backlight control force enabled, even if standard "
6317 "ACPI backlight interface is available\n");
6318 }
6319 } else {
6320 if (brightness_enable > 1) {
6321 printk(TPACPI_NOTICE
6322 "Standard ACPI backlight interface not "
6323 "available, thinkpad_acpi native "
6324 "brightness control enabled\n");
6325 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02006326 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006327 }
6328
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006329 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006330 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006331 "brightness support disabled by "
6332 "module parameter\n");
6333 return 1;
6334 }
6335
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006336 /*
6337 * Check for module parameter bogosity, note that we
6338 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6339 * able to detect "unspecified"
6340 */
6341 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006342 return -EINVAL;
6343
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006344 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6345 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6346 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006347 if (quirks & TPACPI_BRGHT_Q_EC)
6348 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6349 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006350 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6351
6352 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006353 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006354 brightness_mode);
6355 }
6356
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006357 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006358 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006359 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6360 brightness_mode == TPACPI_BRGHT_MODE_EC))
6361 return -EINVAL;
6362
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006363 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006364 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006365
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006366 memset(&props, 0, sizeof(struct backlight_properties));
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006367 props.max_brightness = bright_maxlvl;
6368 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006369 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6370 NULL, NULL,
6371 &ibm_backlight_data,
6372 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006373 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006374 int rc = PTR_ERR(ibm_backlight_device);
6375 ibm_backlight_device = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006376 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006377 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006378 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006379 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6380 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006381
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006382 if (quirks & TPACPI_BRGHT_Q_ASK) {
6383 printk(TPACPI_NOTICE
6384 "brightness: will use unverified default: "
6385 "brightness_mode=%d\n", brightness_mode);
6386 printk(TPACPI_NOTICE
6387 "brightness: please report to %s whether it works well "
6388 "or not on your ThinkPad\n", TPACPI_MAIL);
6389 }
6390
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006391 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006392
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006393 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6394 "brightness: registering brightness hotkeys "
6395 "as change notification\n");
6396 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6397 | TP_ACPI_HKEY_BRGHTUP_MASK
6398 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006399 return 0;
6400}
6401
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006402static void brightness_suspend(pm_message_t state)
6403{
6404 tpacpi_brightness_checkpoint_nvram();
6405}
6406
6407static void brightness_shutdown(void)
6408{
6409 tpacpi_brightness_checkpoint_nvram();
6410}
6411
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006412static void brightness_exit(void)
6413{
6414 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006415 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006416 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006417 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006418 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006419
6420 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006421}
6422
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006423static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006424{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006425 int level;
6426
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006427 level = brightness_get(NULL);
6428 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006429 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006430 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006431 seq_printf(m, "level:\t\t%d\n", level);
6432 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006433 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6434 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006435 }
6436
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006437 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006438}
6439
6440static int brightness_write(char *buf)
6441{
6442 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006443 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006444 char *cmd;
6445
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006446 level = brightness_get(NULL);
6447 if (level < 0)
6448 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006449
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006450 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006451 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006452 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006453 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006454 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006455 if (level > 0)
6456 level--;
6457 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006458 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006459 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006460 } else
6461 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006462 }
6463
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006464 tpacpi_disclose_usertask("procfs brightness",
6465 "set level to %d\n", level);
6466
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006467 /*
6468 * Now we know what the final level should be, so we try to set it.
6469 * Doing it this way makes the syscall restartable in case of EINTR
6470 */
6471 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006472 if (!rc && ibm_backlight_device)
6473 backlight_force_update(ibm_backlight_device,
6474 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006475 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006476}
6477
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006478static struct ibm_struct brightness_driver_data = {
6479 .name = "brightness",
6480 .read = brightness_read,
6481 .write = brightness_write,
6482 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006483 .suspend = brightness_suspend,
6484 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006485};
6486
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006487/*************************************************************************
6488 * Volume subdriver
6489 */
6490
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006491/*
6492 * IBM ThinkPads have a simple volume controller with MUTE gating.
6493 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6494 *
6495 * Since the *61 series (and probably also the later *60 series), Lenovo
6496 * ThinkPads only implement the MUTE gate.
6497 *
6498 * EC register 0x30
6499 * Bit 6: MUTE (1 mutes sound)
6500 * Bit 3-0: Volume
6501 * Other bits should be zero as far as we know.
6502 *
6503 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6504 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6505 * such as bit 7 which is used to detect repeated presses of MUTE,
6506 * and we leave them unchanged.
6507 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006508
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006509#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6510
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006511#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6512#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6513#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6514
Henrique de Moraes Holschuhead510c2009-12-26 22:52:13 -02006515static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006516static char *alsa_id = "ThinkPadEC";
6517static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6518
6519struct tpacpi_alsa_data {
6520 struct snd_card *card;
6521 struct snd_ctl_elem_id *ctl_mute_id;
6522 struct snd_ctl_elem_id *ctl_vol_id;
6523};
6524
6525static struct snd_card *alsa_card;
6526
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006527enum {
6528 TP_EC_AUDIO = 0x30,
6529
6530 /* TP_EC_AUDIO bits */
6531 TP_EC_AUDIO_MUTESW = 6,
6532
6533 /* TP_EC_AUDIO bitmasks */
6534 TP_EC_AUDIO_LVL_MSK = 0x0F,
6535 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6536
6537 /* Maximum volume */
6538 TP_EC_VOLUME_MAX = 14,
6539};
6540
6541enum tpacpi_volume_access_mode {
6542 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6543 TPACPI_VOL_MODE_EC, /* Pure EC control */
6544 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6545 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6546 TPACPI_VOL_MODE_MAX
6547};
6548
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006549enum tpacpi_volume_capabilities {
6550 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6551 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6552 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6553 TPACPI_VOL_CAP_MAX
6554};
6555
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006556static enum tpacpi_volume_access_mode volume_mode =
6557 TPACPI_VOL_MODE_MAX;
6558
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006559static enum tpacpi_volume_capabilities volume_capabilities;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006560static int volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006561
6562/*
6563 * Used to syncronize writers to TP_EC_AUDIO and
6564 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6565 */
6566static struct mutex volume_mutex;
6567
6568static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006569{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006570 u8 lec = 0;
6571 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006572 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006573
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006574 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6575 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006576 if (!volume_control_allowed)
6577 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006578
6579 vdbg_printk(TPACPI_DBG_MIXER,
6580 "trying to checkpoint mixer state to NVRAM...\n");
6581
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006582 if (tp_features.mixer_no_level_control)
6583 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6584 else
6585 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6586
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006587 if (mutex_lock_killable(&volume_mutex) < 0)
6588 return;
6589
6590 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6591 goto unlock;
6592 lec &= ec_mask;
6593 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6594
6595 if (lec != (b_nvram & ec_mask)) {
6596 /* NVRAM needs update */
6597 b_nvram &= ~ec_mask;
6598 b_nvram |= lec;
6599 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6600 dbg_printk(TPACPI_DBG_MIXER,
6601 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6602 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006603 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006604 vdbg_printk(TPACPI_DBG_MIXER,
6605 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6606 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006607 }
6608
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006609unlock:
6610 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006611}
6612
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006613static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006614{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006615 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006616
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006617 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6618 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006619
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006620 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006621
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006622 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006623
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006624 return 0;
6625}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006626
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006627static int volume_get_status(u8 *status)
6628{
6629 return volume_get_status_ec(status);
6630}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006631
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006632static int volume_set_status_ec(const u8 status)
6633{
6634 if (!acpi_ec_write(TP_EC_AUDIO, status))
6635 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006636
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006637 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6638
6639 return 0;
6640}
6641
6642static int volume_set_status(const u8 status)
6643{
6644 return volume_set_status_ec(status);
6645}
6646
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006647/* returns < 0 on error, 0 on no change, 1 on change */
6648static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006649{
6650 int rc;
6651 u8 s, n;
6652
6653 if (mutex_lock_killable(&volume_mutex) < 0)
6654 return -EINTR;
6655
6656 rc = volume_get_status_ec(&s);
6657 if (rc)
6658 goto unlock;
6659
6660 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6661 s & ~TP_EC_AUDIO_MUTESW_MSK;
6662
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006663 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006664 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006665 if (!rc)
6666 rc = 1;
6667 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006668
6669unlock:
6670 mutex_unlock(&volume_mutex);
6671 return rc;
6672}
6673
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006674static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006675{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006676 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006677 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006678 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006679}
6680
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006681static int volume_set_mute(const bool mute)
6682{
6683 int rc;
6684
6685 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6686 (mute) ? "" : "un");
6687
6688 rc = __volume_set_mute_ec(mute);
6689 return (rc < 0) ? rc : 0;
6690}
6691
6692/* returns < 0 on error, 0 on no change, 1 on change */
6693static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006694{
6695 int rc;
6696 u8 s, n;
6697
6698 if (vol > TP_EC_VOLUME_MAX)
6699 return -EINVAL;
6700
6701 if (mutex_lock_killable(&volume_mutex) < 0)
6702 return -EINTR;
6703
6704 rc = volume_get_status_ec(&s);
6705 if (rc)
6706 goto unlock;
6707
6708 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6709
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006710 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006711 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006712 if (!rc)
6713 rc = 1;
6714 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006715
6716unlock:
6717 mutex_unlock(&volume_mutex);
6718 return rc;
6719}
6720
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006721static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006722{
6723 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006724 "ALSA: trying to set volume level to %hu\n", vol);
6725 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006726}
6727
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006728static void volume_alsa_notify_change(void)
6729{
6730 struct tpacpi_alsa_data *d;
6731
6732 if (alsa_card && alsa_card->private_data) {
6733 d = alsa_card->private_data;
6734 if (d->ctl_mute_id)
6735 snd_ctl_notify(alsa_card,
6736 SNDRV_CTL_EVENT_MASK_VALUE,
6737 d->ctl_mute_id);
6738 if (d->ctl_vol_id)
6739 snd_ctl_notify(alsa_card,
6740 SNDRV_CTL_EVENT_MASK_VALUE,
6741 d->ctl_vol_id);
6742 }
6743}
6744
6745static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6746 struct snd_ctl_elem_info *uinfo)
6747{
6748 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6749 uinfo->count = 1;
6750 uinfo->value.integer.min = 0;
6751 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6752 return 0;
6753}
6754
6755static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6756 struct snd_ctl_elem_value *ucontrol)
6757{
6758 u8 s;
6759 int rc;
6760
6761 rc = volume_get_status(&s);
6762 if (rc < 0)
6763 return rc;
6764
6765 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6766 return 0;
6767}
6768
6769static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6770 struct snd_ctl_elem_value *ucontrol)
6771{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006772 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6773 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006774 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006775}
6776
6777#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6778
6779static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6780 struct snd_ctl_elem_value *ucontrol)
6781{
6782 u8 s;
6783 int rc;
6784
6785 rc = volume_get_status(&s);
6786 if (rc < 0)
6787 return rc;
6788
6789 ucontrol->value.integer.value[0] =
6790 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6791 return 0;
6792}
6793
6794static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6795 struct snd_ctl_elem_value *ucontrol)
6796{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006797 tpacpi_disclose_usertask("ALSA", "%smute\n",
6798 ucontrol->value.integer.value[0] ?
6799 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006800 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006801}
6802
6803static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6804 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6805 .name = "Console Playback Volume",
6806 .index = 0,
6807 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6808 .info = volume_alsa_vol_info,
6809 .get = volume_alsa_vol_get,
6810};
6811
6812static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6813 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6814 .name = "Console Playback Switch",
6815 .index = 0,
6816 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6817 .info = volume_alsa_mute_info,
6818 .get = volume_alsa_mute_get,
6819};
6820
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006821static void volume_suspend(pm_message_t state)
6822{
6823 tpacpi_volume_checkpoint_nvram();
6824}
6825
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006826static void volume_resume(void)
6827{
6828 volume_alsa_notify_change();
6829}
6830
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006831static void volume_shutdown(void)
6832{
6833 tpacpi_volume_checkpoint_nvram();
6834}
6835
6836static void volume_exit(void)
6837{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006838 if (alsa_card) {
6839 snd_card_free(alsa_card);
6840 alsa_card = NULL;
6841 }
6842
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006843 tpacpi_volume_checkpoint_nvram();
6844}
6845
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006846static int __init volume_create_alsa_mixer(void)
6847{
6848 struct snd_card *card;
6849 struct tpacpi_alsa_data *data;
6850 struct snd_kcontrol *ctl_vol;
6851 struct snd_kcontrol *ctl_mute;
6852 int rc;
6853
6854 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6855 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006856 if (rc < 0 || !card) {
6857 printk(TPACPI_ERR
6858 "Failed to create ALSA card structures: %d\n", rc);
6859 return 1;
6860 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006861
6862 BUG_ON(!card->private_data);
6863 data = card->private_data;
6864 data->card = card;
6865
6866 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6867 sizeof(card->driver));
6868 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6869 sizeof(card->shortname));
6870 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6871 (thinkpad_id.ec_version_str) ?
6872 thinkpad_id.ec_version_str : "(unknown)");
6873 snprintf(card->longname, sizeof(card->longname),
6874 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6875 (thinkpad_id.ec_version_str) ?
6876 thinkpad_id.ec_version_str : "unknown");
6877
6878 if (volume_control_allowed) {
6879 volume_alsa_control_vol.put = volume_alsa_vol_put;
6880 volume_alsa_control_vol.access =
6881 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6882
6883 volume_alsa_control_mute.put = volume_alsa_mute_put;
6884 volume_alsa_control_mute.access =
6885 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6886 }
6887
6888 if (!tp_features.mixer_no_level_control) {
6889 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6890 rc = snd_ctl_add(card, ctl_vol);
6891 if (rc < 0) {
6892 printk(TPACPI_ERR
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006893 "Failed to create ALSA volume control: %d\n",
6894 rc);
6895 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006896 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006897 data->ctl_vol_id = &ctl_vol->id;
6898 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006899
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006900 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6901 rc = snd_ctl_add(card, ctl_mute);
6902 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006903 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6904 rc);
6905 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006906 }
6907 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006908
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006909 snd_card_set_dev(card, &tpacpi_pdev->dev);
6910 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006911 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006912 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6913 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006914 }
6915
6916 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006917 return 0;
6918
6919err_exit:
6920 snd_card_free(card);
6921 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006922}
6923
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006924#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6925#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6926
6927static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6928 /* Whitelist volume level on all IBM by default */
6929 { .vendor = PCI_VENDOR_ID_IBM,
6930 .bios = TPACPI_MATCH_ANY,
6931 .ec = TPACPI_MATCH_ANY,
6932 .quirks = TPACPI_VOL_Q_LEVEL },
6933
6934 /* Lenovo models with volume control (needs confirmation) */
6935 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6936 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6937 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6938 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6939 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6940 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6941 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6942
6943 /* Whitelist mute-only on all Lenovo by default */
6944 { .vendor = PCI_VENDOR_ID_LENOVO,
6945 .bios = TPACPI_MATCH_ANY,
6946 .ec = TPACPI_MATCH_ANY,
6947 .quirks = TPACPI_VOL_Q_MUTEONLY }
6948};
6949
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006950static int __init volume_init(struct ibm_init_struct *iibm)
6951{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006952 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006953 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006954
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006955 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6956
6957 mutex_init(&volume_mutex);
6958
6959 /*
6960 * Check for module parameter bogosity, note that we
6961 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6962 * able to detect "unspecified"
6963 */
6964 if (volume_mode > TPACPI_VOL_MODE_MAX)
6965 return -EINVAL;
6966
6967 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6968 printk(TPACPI_ERR
6969 "UCMS step volume mode not implemented, "
6970 "please contact %s\n", TPACPI_MAIL);
6971 return 1;
6972 }
6973
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006974 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6975 return -EINVAL;
6976
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006977 /*
6978 * The ALSA mixer is our primary interface.
6979 * When disabled, don't install the subdriver at all
6980 */
6981 if (!alsa_enable) {
6982 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6983 "ALSA mixer disabled by parameter, "
6984 "not loading volume subdriver...\n");
6985 return 1;
6986 }
6987
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006988 quirks = tpacpi_check_quirks(volume_quirk_table,
6989 ARRAY_SIZE(volume_quirk_table));
6990
6991 switch (volume_capabilities) {
6992 case TPACPI_VOL_CAP_AUTO:
6993 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6994 tp_features.mixer_no_level_control = 1;
6995 else if (quirks & TPACPI_VOL_Q_LEVEL)
6996 tp_features.mixer_no_level_control = 0;
6997 else
6998 return 1; /* no mixer */
6999 break;
7000 case TPACPI_VOL_CAP_VOLMUTE:
7001 tp_features.mixer_no_level_control = 0;
7002 break;
7003 case TPACPI_VOL_CAP_MUTEONLY:
7004 tp_features.mixer_no_level_control = 1;
7005 break;
7006 default:
7007 return 1;
7008 }
7009
7010 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7011 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7012 "using user-supplied volume_capabilities=%d\n",
7013 volume_capabilities);
7014
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007015 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7016 volume_mode == TPACPI_VOL_MODE_MAX) {
7017 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7018
7019 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7020 "driver auto-selected volume_mode=%d\n",
7021 volume_mode);
7022 } else {
7023 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7024 "using user-supplied volume_mode=%d\n",
7025 volume_mode);
7026 }
7027
7028 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007029 "mute is supported, volume control is %s\n",
7030 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007031
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007032 rc = volume_create_alsa_mixer();
7033 if (rc) {
7034 printk(TPACPI_ERR
7035 "Could not create the ALSA mixer interface\n");
7036 return rc;
7037 }
7038
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007039 printk(TPACPI_INFO
7040 "Console audio control enabled, mode: %s\n",
7041 (volume_control_allowed) ?
7042 "override (read/write)" :
7043 "monitor (read only)");
7044
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007045 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7046 "registering volume hotkeys as change notification\n");
7047 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7048 | TP_ACPI_HKEY_VOLUP_MASK
7049 | TP_ACPI_HKEY_VOLDWN_MASK
7050 | TP_ACPI_HKEY_MUTE_MASK);
7051
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007052 return 0;
7053}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007054
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007055static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007056{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007057 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007058
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007059 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007060 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007061 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007062 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007063 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007064 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007065 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007066 status & TP_EC_AUDIO_LVL_MSK);
7067
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007068 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007069 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007070
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007071 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007072 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007073 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007074 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007075 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007076 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007077 "commands:\tlevel <level>"
7078 " (<level> is 0-%d)\n",
7079 TP_EC_VOLUME_MAX);
7080 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007081 }
7082 }
7083
7084 return 0;
7085}
7086
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007087static int volume_write(char *buf)
7088{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007089 u8 s;
7090 u8 new_level, new_mute;
7091 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007092 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007093 int rc;
7094
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007095 /*
7096 * We do allow volume control at driver startup, so that the
7097 * user can set initial state through the volume=... parameter hack.
7098 */
7099 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7100 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7101 tp_warned.volume_ctrl_forbidden = 1;
7102 printk(TPACPI_NOTICE
7103 "Console audio control in monitor mode, "
7104 "changes are not allowed.\n");
7105 printk(TPACPI_NOTICE
7106 "Use the volume_control=1 module parameter "
7107 "to enable volume control\n");
7108 }
7109 return -EPERM;
7110 }
7111
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007112 rc = volume_get_status(&s);
7113 if (rc < 0)
7114 return rc;
7115
7116 new_level = s & TP_EC_AUDIO_LVL_MSK;
7117 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007118
7119 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007120 if (!tp_features.mixer_no_level_control) {
7121 if (strlencmp(cmd, "up") == 0) {
7122 if (new_mute)
7123 new_mute = 0;
7124 else if (new_level < TP_EC_VOLUME_MAX)
7125 new_level++;
7126 continue;
7127 } else if (strlencmp(cmd, "down") == 0) {
7128 if (new_mute)
7129 new_mute = 0;
7130 else if (new_level > 0)
7131 new_level--;
7132 continue;
7133 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7134 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7135 new_level = l;
7136 continue;
7137 }
7138 }
7139 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007140 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007141 else if (strlencmp(cmd, "unmute") == 0)
7142 new_mute = 0;
7143 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007144 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007145 }
7146
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007147 if (tp_features.mixer_no_level_control) {
7148 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7149 new_mute ? "" : "un");
7150 rc = volume_set_mute(!!new_mute);
7151 } else {
7152 tpacpi_disclose_usertask("procfs volume",
7153 "%smute and set level to %d\n",
7154 new_mute ? "" : "un", new_level);
7155 rc = volume_set_status(new_mute | new_level);
7156 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007157 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007158
7159 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007160}
7161
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007162static struct ibm_struct volume_driver_data = {
7163 .name = "volume",
7164 .read = volume_read,
7165 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007166 .exit = volume_exit,
7167 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007168 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007169 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007170};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007171
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007172#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7173
7174#define alsa_card NULL
7175
7176static void inline volume_alsa_notify_change(void)
7177{
7178}
7179
7180static int __init volume_init(struct ibm_init_struct *iibm)
7181{
7182 printk(TPACPI_INFO
7183 "volume: disabled as there is no ALSA support in this kernel\n");
7184
7185 return 1;
7186}
7187
7188static struct ibm_struct volume_driver_data = {
7189 .name = "volume",
7190};
7191
7192#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7193
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007194/*************************************************************************
7195 * Fan subdriver
7196 */
7197
7198/*
7199 * FAN ACCESS MODES
7200 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007201 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007202 * ACPI GFAN method: returns fan level
7203 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007204 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007205 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007206 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007207 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007208 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7209 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007210 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7211 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007212 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007213 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007214 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7215 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007216 *
7217 * Fan speed changes of any sort (including those caused by the
7218 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007219 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007220 * limited.
7221 *
7222 * Reading is not available if GFAN exists.
7223 * Writing is not available if SFAN exists.
7224 *
7225 * Bits
7226 * 7 automatic mode engaged;
7227 * (default operation mode of the ThinkPad)
7228 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007229 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007230 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007231 * the tachometer while the fan controller ramps up
7232 * the speed (which can take up to a few *minutes*).
7233 * Speeds up fan to 100% duty-cycle, which is far above
7234 * the standard RPM levels. It is not impossible that
7235 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007236 * 5-3 unused in some models. Extra bits for fan level
7237 * in others, but still useless as all values above
7238 * 7 map to the same speed as level 7 in these models.
7239 * 2-0 fan level (0..7 usually)
7240 * 0x00 = stop
7241 * 0x07 = max (set when temperatures critical)
7242 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007243 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007244 *
7245 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7246 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7247 * does so, its initial value is meaningless (0x07).
7248 *
7249 * For firmware bugs, refer to:
7250 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7251 *
7252 * ----
7253 *
7254 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7255 * Main fan tachometer reading (in RPM)
7256 *
7257 * This register is present on all ThinkPads with a new-style EC, and
7258 * it is known not to be present on the A21m/e, and T22, as there is
7259 * something else in offset 0x84 according to the ACPI DSDT. Other
7260 * ThinkPads from this same time period (and earlier) probably lack the
7261 * tachometer as well.
7262 *
Nick Andrew877d0312009-01-26 11:06:57 +01007263 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007264 * was never fixed by IBM to report the EC firmware version string
7265 * probably support the tachometer (like the early X models), so
7266 * detecting it is quite hard. We need more data to know for sure.
7267 *
7268 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7269 * might result.
7270 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007271 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7272 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007273 *
7274 * For firmware bugs, refer to:
7275 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7276 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007277 * ----
7278 *
7279 * ThinkPad EC register 0x31 bit 0 (only on select models)
7280 *
7281 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7282 * show the speed of the main fan. When bit 0 of EC register 0x31
7283 * is one, the tachometer registers show the speed of the auxiliary
7284 * fan.
7285 *
7286 * Fan control seems to affect both fans, regardless of the state
7287 * of this bit.
7288 *
7289 * So far, only the firmware for the X60/X61 non-tablet versions
7290 * seem to support this (firmware TP-7M).
7291 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007292 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007293 * ThinkPad X31, X40, X41. Not available in the X60.
7294 *
7295 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7296 * high speed. ACPI DSDT seems to map these three speeds to levels
7297 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7298 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7299 *
7300 * The speeds are stored on handles
7301 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7302 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007303 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007304 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7305 *
7306 * ACPI DSDT switches which set is in use depending on various
7307 * factors.
7308 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007309 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007310 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7311 * but the ACPI tables just mention level 7.
7312 */
7313
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007314enum { /* Fan control constants */
7315 fan_status_offset = 0x2f, /* EC register 0x2f */
7316 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7317 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007318 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7319 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007320
7321 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7322 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7323
7324 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7325};
7326
7327enum fan_status_access_mode {
7328 TPACPI_FAN_NONE = 0, /* No fan status or control */
7329 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7330 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7331};
7332
7333enum fan_control_access_mode {
7334 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7335 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7336 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7337 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7338};
7339
7340enum fan_control_commands {
7341 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7342 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7343 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7344 * and also watchdog cmd */
7345};
7346
7347static int fan_control_allowed;
7348
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007349static enum fan_status_access_mode fan_status_access_mode;
7350static enum fan_control_access_mode fan_control_access_mode;
7351static enum fan_control_commands fan_control_commands;
7352
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007353static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007354static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007355static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007356static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007357
7358static struct mutex fan_mutex;
7359
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007360static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007361static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007362
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007363TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7364TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007365 "\\FSPD", /* 600e/x, 770e, 770x */
7366 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007367TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007368 "JFNS", /* 770x-JL */
7369 ); /* all others */
7370
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007371/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007372 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7373 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7374 * be in auto mode (0x80).
7375 *
7376 * This is corrected by any write to HFSP either by the driver, or
7377 * by the firmware.
7378 *
7379 * We assume 0x07 really means auto mode while this quirk is active,
7380 * as this is far more likely than the ThinkPad being in level 7,
7381 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007382 *
7383 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7384 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007385 */
7386
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007387static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007388{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007389 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007390 printk(TPACPI_NOTICE
7391 "fan_init: initial fan status is unknown, "
7392 "assuming it is in auto mode\n");
7393 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007394 }
7395}
7396
7397static void fan_quirk1_handle(u8 *fan_status)
7398{
7399 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7400 if (*fan_status != fan_control_initial_status) {
7401 /* something changed the HFSP regisnter since
7402 * driver init time, so it is not undefined
7403 * anymore */
7404 tp_features.fan_ctrl_status_undef = 0;
7405 } else {
7406 /* Return most likely status. In fact, it
7407 * might be the only possible status */
7408 *fan_status = TP_EC_FAN_AUTO;
7409 }
7410 }
7411}
7412
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007413/* Select main fan on X60/X61, NOOP on others */
7414static bool fan_select_fan1(void)
7415{
7416 if (tp_features.second_fan) {
7417 u8 val;
7418
7419 if (ec_read(fan_select_offset, &val) < 0)
7420 return false;
7421 val &= 0xFEU;
7422 if (ec_write(fan_select_offset, val) < 0)
7423 return false;
7424 }
7425 return true;
7426}
7427
7428/* Select secondary fan on X60/X61 */
7429static bool fan_select_fan2(void)
7430{
7431 u8 val;
7432
7433 if (!tp_features.second_fan)
7434 return false;
7435
7436 if (ec_read(fan_select_offset, &val) < 0)
7437 return false;
7438 val |= 0x01U;
7439 if (ec_write(fan_select_offset, val) < 0)
7440 return false;
7441
7442 return true;
7443}
7444
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007445/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007446 * Call with fan_mutex held
7447 */
7448static void fan_update_desired_level(u8 status)
7449{
7450 if ((status &
7451 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7452 if (status > 7)
7453 fan_control_desired_level = 7;
7454 else
7455 fan_control_desired_level = status;
7456 }
7457}
7458
7459static int fan_get_status(u8 *status)
7460{
7461 u8 s;
7462
7463 /* TODO:
7464 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7465
7466 switch (fan_status_access_mode) {
7467 case TPACPI_FAN_RD_ACPI_GFAN:
7468 /* 570, 600e/x, 770e, 770x */
7469
7470 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7471 return -EIO;
7472
7473 if (likely(status))
7474 *status = s & 0x07;
7475
7476 break;
7477
7478 case TPACPI_FAN_RD_TPEC:
7479 /* all except 570, 600e/x, 770e, 770x */
7480 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7481 return -EIO;
7482
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007483 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007484 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007485 fan_quirk1_handle(status);
7486 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007487
7488 break;
7489
7490 default:
7491 return -ENXIO;
7492 }
7493
7494 return 0;
7495}
7496
7497static int fan_get_status_safe(u8 *status)
7498{
7499 int rc;
7500 u8 s;
7501
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007502 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007503 return -ERESTARTSYS;
7504 rc = fan_get_status(&s);
7505 if (!rc)
7506 fan_update_desired_level(s);
7507 mutex_unlock(&fan_mutex);
7508
7509 if (status)
7510 *status = s;
7511
7512 return rc;
7513}
7514
7515static int fan_get_speed(unsigned int *speed)
7516{
7517 u8 hi, lo;
7518
7519 switch (fan_status_access_mode) {
7520 case TPACPI_FAN_RD_TPEC:
7521 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007522 if (unlikely(!fan_select_fan1()))
7523 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007524 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7525 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7526 return -EIO;
7527
7528 if (likely(speed))
7529 *speed = (hi << 8) | lo;
7530
7531 break;
7532
7533 default:
7534 return -ENXIO;
7535 }
7536
7537 return 0;
7538}
7539
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007540static int fan2_get_speed(unsigned int *speed)
7541{
7542 u8 hi, lo;
7543 bool rc;
7544
7545 switch (fan_status_access_mode) {
7546 case TPACPI_FAN_RD_TPEC:
7547 /* all except 570, 600e/x, 770e, 770x */
7548 if (unlikely(!fan_select_fan2()))
7549 return -EIO;
7550 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7551 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7552 fan_select_fan1(); /* play it safe */
7553 if (rc)
7554 return -EIO;
7555
7556 if (likely(speed))
7557 *speed = (hi << 8) | lo;
7558
7559 break;
7560
7561 default:
7562 return -ENXIO;
7563 }
7564
7565 return 0;
7566}
7567
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007568static int fan_set_level(int level)
7569{
7570 if (!fan_control_allowed)
7571 return -EPERM;
7572
7573 switch (fan_control_access_mode) {
7574 case TPACPI_FAN_WR_ACPI_SFAN:
7575 if (level >= 0 && level <= 7) {
7576 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7577 return -EIO;
7578 } else
7579 return -EINVAL;
7580 break;
7581
7582 case TPACPI_FAN_WR_ACPI_FANS:
7583 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007584 if (!(level & TP_EC_FAN_AUTO) &&
7585 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007586 ((level < 0) || (level > 7)))
7587 return -EINVAL;
7588
7589 /* safety net should the EC not support AUTO
7590 * or FULLSPEED mode bits and just ignore them */
7591 if (level & TP_EC_FAN_FULLSPEED)
7592 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007593 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007594 level |= 4; /* safety min speed 4 */
7595
7596 if (!acpi_ec_write(fan_status_offset, level))
7597 return -EIO;
7598 else
7599 tp_features.fan_ctrl_status_undef = 0;
7600 break;
7601
7602 default:
7603 return -ENXIO;
7604 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007605
7606 vdbg_printk(TPACPI_DBG_FAN,
7607 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007608 return 0;
7609}
7610
7611static int fan_set_level_safe(int level)
7612{
7613 int rc;
7614
7615 if (!fan_control_allowed)
7616 return -EPERM;
7617
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007618 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007619 return -ERESTARTSYS;
7620
7621 if (level == TPACPI_FAN_LAST_LEVEL)
7622 level = fan_control_desired_level;
7623
7624 rc = fan_set_level(level);
7625 if (!rc)
7626 fan_update_desired_level(level);
7627
7628 mutex_unlock(&fan_mutex);
7629 return rc;
7630}
7631
7632static int fan_set_enable(void)
7633{
7634 u8 s;
7635 int rc;
7636
7637 if (!fan_control_allowed)
7638 return -EPERM;
7639
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007640 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007641 return -ERESTARTSYS;
7642
7643 switch (fan_control_access_mode) {
7644 case TPACPI_FAN_WR_ACPI_FANS:
7645 case TPACPI_FAN_WR_TPEC:
7646 rc = fan_get_status(&s);
7647 if (rc < 0)
7648 break;
7649
7650 /* Don't go out of emergency fan mode */
7651 if (s != 7) {
7652 s &= 0x07;
7653 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7654 }
7655
7656 if (!acpi_ec_write(fan_status_offset, s))
7657 rc = -EIO;
7658 else {
7659 tp_features.fan_ctrl_status_undef = 0;
7660 rc = 0;
7661 }
7662 break;
7663
7664 case TPACPI_FAN_WR_ACPI_SFAN:
7665 rc = fan_get_status(&s);
7666 if (rc < 0)
7667 break;
7668
7669 s &= 0x07;
7670
7671 /* Set fan to at least level 4 */
7672 s |= 4;
7673
7674 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007675 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007676 else
7677 rc = 0;
7678 break;
7679
7680 default:
7681 rc = -ENXIO;
7682 }
7683
7684 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007685
7686 if (!rc)
7687 vdbg_printk(TPACPI_DBG_FAN,
7688 "fan control: set fan control register to 0x%02x\n",
7689 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007690 return rc;
7691}
7692
7693static int fan_set_disable(void)
7694{
7695 int rc;
7696
7697 if (!fan_control_allowed)
7698 return -EPERM;
7699
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007700 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007701 return -ERESTARTSYS;
7702
7703 rc = 0;
7704 switch (fan_control_access_mode) {
7705 case TPACPI_FAN_WR_ACPI_FANS:
7706 case TPACPI_FAN_WR_TPEC:
7707 if (!acpi_ec_write(fan_status_offset, 0x00))
7708 rc = -EIO;
7709 else {
7710 fan_control_desired_level = 0;
7711 tp_features.fan_ctrl_status_undef = 0;
7712 }
7713 break;
7714
7715 case TPACPI_FAN_WR_ACPI_SFAN:
7716 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7717 rc = -EIO;
7718 else
7719 fan_control_desired_level = 0;
7720 break;
7721
7722 default:
7723 rc = -ENXIO;
7724 }
7725
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007726 if (!rc)
7727 vdbg_printk(TPACPI_DBG_FAN,
7728 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007729
7730 mutex_unlock(&fan_mutex);
7731 return rc;
7732}
7733
7734static int fan_set_speed(int speed)
7735{
7736 int rc;
7737
7738 if (!fan_control_allowed)
7739 return -EPERM;
7740
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007741 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007742 return -ERESTARTSYS;
7743
7744 rc = 0;
7745 switch (fan_control_access_mode) {
7746 case TPACPI_FAN_WR_ACPI_FANS:
7747 if (speed >= 0 && speed <= 65535) {
7748 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7749 speed, speed, speed))
7750 rc = -EIO;
7751 } else
7752 rc = -EINVAL;
7753 break;
7754
7755 default:
7756 rc = -ENXIO;
7757 }
7758
7759 mutex_unlock(&fan_mutex);
7760 return rc;
7761}
7762
7763static void fan_watchdog_reset(void)
7764{
7765 static int fan_watchdog_active;
7766
7767 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7768 return;
7769
7770 if (fan_watchdog_active)
7771 cancel_delayed_work(&fan_watchdog_task);
7772
7773 if (fan_watchdog_maxinterval > 0 &&
7774 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7775 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007776 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007777 msecs_to_jiffies(fan_watchdog_maxinterval
7778 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007779 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007780 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007781 "watchdog will not trigger\n");
7782 }
7783 } else
7784 fan_watchdog_active = 0;
7785}
7786
7787static void fan_watchdog_fire(struct work_struct *ignored)
7788{
7789 int rc;
7790
7791 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7792 return;
7793
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007794 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007795 rc = fan_set_enable();
7796 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007797 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007798 "will try again later...\n", -rc);
7799 /* reschedule for later */
7800 fan_watchdog_reset();
7801 }
7802}
7803
7804/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007805 * SYSFS fan layout: hwmon compatible (device)
7806 *
7807 * pwm*_enable:
7808 * 0: "disengaged" mode
7809 * 1: manual mode
7810 * 2: native EC "auto" mode (recommended, hardware default)
7811 *
7812 * pwm*: set speed in manual mode, ignored otherwise.
7813 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7814 * interpolation.
7815 *
7816 * fan*_input: tachometer reading, RPM
7817 *
7818 *
7819 * SYSFS fan layout: extensions
7820 *
7821 * fan_watchdog (driver):
7822 * fan watchdog interval in seconds, 0 disables (default), max 120
7823 */
7824
7825/* sysfs fan pwm1_enable ----------------------------------------------- */
7826static ssize_t fan_pwm1_enable_show(struct device *dev,
7827 struct device_attribute *attr,
7828 char *buf)
7829{
7830 int res, mode;
7831 u8 status;
7832
7833 res = fan_get_status_safe(&status);
7834 if (res)
7835 return res;
7836
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007837 if (status & TP_EC_FAN_FULLSPEED) {
7838 mode = 0;
7839 } else if (status & TP_EC_FAN_AUTO) {
7840 mode = 2;
7841 } else
7842 mode = 1;
7843
7844 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7845}
7846
7847static ssize_t fan_pwm1_enable_store(struct device *dev,
7848 struct device_attribute *attr,
7849 const char *buf, size_t count)
7850{
7851 unsigned long t;
7852 int res, level;
7853
7854 if (parse_strtoul(buf, 2, &t))
7855 return -EINVAL;
7856
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007857 tpacpi_disclose_usertask("hwmon pwm1_enable",
7858 "set fan mode to %lu\n", t);
7859
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007860 switch (t) {
7861 case 0:
7862 level = TP_EC_FAN_FULLSPEED;
7863 break;
7864 case 1:
7865 level = TPACPI_FAN_LAST_LEVEL;
7866 break;
7867 case 2:
7868 level = TP_EC_FAN_AUTO;
7869 break;
7870 case 3:
7871 /* reserved for software-controlled auto mode */
7872 return -ENOSYS;
7873 default:
7874 return -EINVAL;
7875 }
7876
7877 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007878 if (res == -ENXIO)
7879 return -EINVAL;
7880 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007881 return res;
7882
7883 fan_watchdog_reset();
7884
7885 return count;
7886}
7887
7888static struct device_attribute dev_attr_fan_pwm1_enable =
7889 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7890 fan_pwm1_enable_show, fan_pwm1_enable_store);
7891
7892/* sysfs fan pwm1 ------------------------------------------------------ */
7893static ssize_t fan_pwm1_show(struct device *dev,
7894 struct device_attribute *attr,
7895 char *buf)
7896{
7897 int res;
7898 u8 status;
7899
7900 res = fan_get_status_safe(&status);
7901 if (res)
7902 return res;
7903
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007904 if ((status &
7905 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7906 status = fan_control_desired_level;
7907
7908 if (status > 7)
7909 status = 7;
7910
7911 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7912}
7913
7914static ssize_t fan_pwm1_store(struct device *dev,
7915 struct device_attribute *attr,
7916 const char *buf, size_t count)
7917{
7918 unsigned long s;
7919 int rc;
7920 u8 status, newlevel;
7921
7922 if (parse_strtoul(buf, 255, &s))
7923 return -EINVAL;
7924
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007925 tpacpi_disclose_usertask("hwmon pwm1",
7926 "set fan speed to %lu\n", s);
7927
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007928 /* scale down from 0-255 to 0-7 */
7929 newlevel = (s >> 5) & 0x07;
7930
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007931 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007932 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007933
7934 rc = fan_get_status(&status);
7935 if (!rc && (status &
7936 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7937 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007938 if (rc == -ENXIO)
7939 rc = -EINVAL;
7940 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007941 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007942 fan_watchdog_reset();
7943 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007944 }
7945
7946 mutex_unlock(&fan_mutex);
7947 return (rc)? rc : count;
7948}
7949
7950static struct device_attribute dev_attr_fan_pwm1 =
7951 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7952 fan_pwm1_show, fan_pwm1_store);
7953
7954/* sysfs fan fan1_input ------------------------------------------------ */
7955static ssize_t fan_fan1_input_show(struct device *dev,
7956 struct device_attribute *attr,
7957 char *buf)
7958{
7959 int res;
7960 unsigned int speed;
7961
7962 res = fan_get_speed(&speed);
7963 if (res < 0)
7964 return res;
7965
7966 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7967}
7968
7969static struct device_attribute dev_attr_fan_fan1_input =
7970 __ATTR(fan1_input, S_IRUGO,
7971 fan_fan1_input_show, NULL);
7972
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007973/* sysfs fan fan2_input ------------------------------------------------ */
7974static ssize_t fan_fan2_input_show(struct device *dev,
7975 struct device_attribute *attr,
7976 char *buf)
7977{
7978 int res;
7979 unsigned int speed;
7980
7981 res = fan2_get_speed(&speed);
7982 if (res < 0)
7983 return res;
7984
7985 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7986}
7987
7988static struct device_attribute dev_attr_fan_fan2_input =
7989 __ATTR(fan2_input, S_IRUGO,
7990 fan_fan2_input_show, NULL);
7991
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007992/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007993static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7994 char *buf)
7995{
7996 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7997}
7998
7999static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8000 const char *buf, size_t count)
8001{
8002 unsigned long t;
8003
8004 if (parse_strtoul(buf, 120, &t))
8005 return -EINVAL;
8006
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008007 if (!fan_control_allowed)
8008 return -EPERM;
8009
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008010 fan_watchdog_maxinterval = t;
8011 fan_watchdog_reset();
8012
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008013 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8014
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008015 return count;
8016}
8017
8018static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8019 fan_fan_watchdog_show, fan_fan_watchdog_store);
8020
8021/* --------------------------------------------------------------------- */
8022static struct attribute *fan_attributes[] = {
8023 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8024 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008025 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008026 NULL
8027};
8028
8029static const struct attribute_group fan_attr_group = {
8030 .attrs = fan_attributes,
8031};
8032
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008033#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8034#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008035
8036#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8037 { .vendor = PCI_VENDOR_ID_IBM, \
8038 .bios = TPACPI_MATCH_ANY, \
8039 .ec = TPID(__id1, __id2), \
8040 .quirks = __quirks }
8041
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008042#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8043 { .vendor = PCI_VENDOR_ID_LENOVO, \
8044 .bios = TPACPI_MATCH_ANY, \
8045 .ec = TPID(__id1, __id2), \
8046 .quirks = __quirks }
8047
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008048static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8049 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8050 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8051 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8052 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008053 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008054};
8055
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008056#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008057#undef TPACPI_FAN_QI
8058
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008059static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008060{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008061 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008062 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008063
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008064 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8065 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008066
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008067 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008068 fan_status_access_mode = TPACPI_FAN_NONE;
8069 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008070 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008071 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008072 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008073 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008074 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008075
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008076 if (tpacpi_is_ibm()) {
8077 TPACPI_ACPIHANDLE_INIT(fans);
8078 TPACPI_ACPIHANDLE_INIT(gfan);
8079 TPACPI_ACPIHANDLE_INIT(sfan);
8080 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008081
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008082 quirks = tpacpi_check_quirks(fan_quirk_table,
8083 ARRAY_SIZE(fan_quirk_table));
8084
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008085 if (gfan_handle) {
8086 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008087 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008088 } else {
8089 /* all other ThinkPads: note that even old-style
8090 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008091 if (likely(acpi_ec_read(fan_status_offset,
8092 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008093 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008094 if (quirks & TPACPI_FAN_Q1)
8095 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008096 if (quirks & TPACPI_FAN_2FAN) {
8097 tp_features.second_fan = 1;
8098 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8099 "secondary fan support enabled\n");
8100 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008101 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008102 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008103 "ThinkPad ACPI EC access misbehaving, "
8104 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008105 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008106 }
8107 }
8108
8109 if (sfan_handle) {
8110 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008111 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008112 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008113 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008114 } else {
8115 if (!gfan_handle) {
8116 /* gfan without sfan means no fan control */
8117 /* all other models implement TP EC 0x2f control */
8118
8119 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008120 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008121 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008122 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008123 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008124 TPACPI_FAN_CMD_SPEED |
8125 TPACPI_FAN_CMD_LEVEL |
8126 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008127 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008128 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008129 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008130 TPACPI_FAN_CMD_LEVEL |
8131 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008132 }
8133 }
8134 }
8135
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008136 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8137 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008138 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8139 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8140 fan_status_access_mode, fan_control_access_mode);
8141
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008142 /* fan control master switch */
8143 if (!fan_control_allowed) {
8144 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8145 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008146 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008147 "fan control features disabled by parameter\n");
8148 }
8149
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008150 /* update fan_control_desired_level */
8151 if (fan_status_access_mode != TPACPI_FAN_NONE)
8152 fan_get_status_safe(NULL);
8153
8154 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8155 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008156 if (tp_features.second_fan) {
8157 /* attach second fan tachometer */
8158 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8159 &dev_attr_fan_fan2_input.attr;
8160 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008161 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008162 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008163 if (rc < 0)
8164 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008165
8166 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8167 &driver_attr_fan_watchdog);
8168 if (rc < 0) {
8169 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8170 &fan_attr_group);
8171 return rc;
8172 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008173 return 0;
8174 } else
8175 return 1;
8176}
8177
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008178static void fan_exit(void)
8179{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008180 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008181 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008182
8183 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008184 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008185 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8186 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008187
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008188 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008189 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008190}
8191
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008192static void fan_suspend(pm_message_t state)
8193{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008194 int rc;
8195
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008196 if (!fan_control_allowed)
8197 return;
8198
8199 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008200 fan_control_resume_level = 0;
8201 rc = fan_get_status_safe(&fan_control_resume_level);
8202 if (rc < 0)
8203 printk(TPACPI_NOTICE
8204 "failed to read fan level for later "
8205 "restore during resume: %d\n", rc);
8206
8207 /* if it is undefined, don't attempt to restore it.
8208 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008209 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008210 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008211}
8212
8213static void fan_resume(void)
8214{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008215 u8 current_level = 7;
8216 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008217 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008218
8219 /* DSDT *always* updates status on resume */
8220 tp_features.fan_ctrl_status_undef = 0;
8221
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008222 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008223 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008224 (fan_get_status_safe(&current_level) < 0))
8225 return;
8226
8227 switch (fan_control_access_mode) {
8228 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008229 /* never decrease fan level */
8230 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008231 break;
8232 case TPACPI_FAN_WR_ACPI_FANS:
8233 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008234 /* never decrease fan level, scale is:
8235 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8236 *
8237 * We expect the firmware to set either 7 or AUTO, but we
8238 * handle FULLSPEED out of paranoia.
8239 *
8240 * So, we can safely only restore FULLSPEED or 7, anything
8241 * else could slow the fan. Restoring AUTO is useless, at
8242 * best that's exactly what the DSDT already set (it is the
8243 * slower it uses).
8244 *
8245 * Always keep in mind that the DSDT *will* have set the
8246 * fans to what the vendor supposes is the best level. We
8247 * muck with it only to speed the fan up.
8248 */
8249 if (fan_control_resume_level != 7 &&
8250 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8251 return;
8252 else
8253 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8254 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008255 break;
8256 default:
8257 return;
8258 }
8259 if (do_set) {
8260 printk(TPACPI_NOTICE
8261 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008262 fan_control_resume_level);
8263 rc = fan_set_level_safe(fan_control_resume_level);
8264 if (rc < 0)
8265 printk(TPACPI_NOTICE
8266 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008267 }
8268}
8269
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008270static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008271{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008272 int rc;
8273 u8 status;
8274 unsigned int speed = 0;
8275
8276 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008277 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008278 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008279 rc = fan_get_status_safe(&status);
8280 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008281 return rc;
8282
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008283 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008284 "level:\t\t%d\n",
8285 (status != 0) ? "enabled" : "disabled", status);
8286 break;
8287
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008288 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008289 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008290 rc = fan_get_status_safe(&status);
8291 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008292 return rc;
8293
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008294 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008295 (status != 0) ? "enabled" : "disabled");
8296
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008297 rc = fan_get_speed(&speed);
8298 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008299 return rc;
8300
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008301 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008302
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008303 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008304 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008305 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008306 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008307 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008308 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008309 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008310 break;
8311
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008312 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008313 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008314 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008315 }
8316
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008317 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008318 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008319
8320 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008321 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008322 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008323 break;
8324
8325 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008326 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008327 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008328 break;
8329 }
8330 }
8331
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008332 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008333 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008334 "commands:\twatchdog <timeout> (<timeout> "
8335 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008336
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008337 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008338 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008339 " (<speed> is 0-65535)\n");
8340
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008341 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008342}
8343
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008344static int fan_write_cmd_level(const char *cmd, int *rc)
8345{
8346 int level;
8347
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008348 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008349 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008350 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008351 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008352 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008353 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008354 return 0;
8355
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008356 *rc = fan_set_level_safe(level);
8357 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008358 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008359 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008360 else if (!*rc)
8361 tpacpi_disclose_usertask("procfs fan",
8362 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008363
8364 return 1;
8365}
8366
8367static int fan_write_cmd_enable(const char *cmd, int *rc)
8368{
8369 if (strlencmp(cmd, "enable") != 0)
8370 return 0;
8371
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008372 *rc = fan_set_enable();
8373 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008374 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008375 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008376 else if (!*rc)
8377 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008378
8379 return 1;
8380}
8381
8382static int fan_write_cmd_disable(const char *cmd, int *rc)
8383{
8384 if (strlencmp(cmd, "disable") != 0)
8385 return 0;
8386
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008387 *rc = fan_set_disable();
8388 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008389 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008390 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008391 else if (!*rc)
8392 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008393
8394 return 1;
8395}
8396
8397static int fan_write_cmd_speed(const char *cmd, int *rc)
8398{
8399 int speed;
8400
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008401 /* TODO:
8402 * Support speed <low> <medium> <high> ? */
8403
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008404 if (sscanf(cmd, "speed %d", &speed) != 1)
8405 return 0;
8406
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008407 *rc = fan_set_speed(speed);
8408 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008409 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008410 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008411 else if (!*rc)
8412 tpacpi_disclose_usertask("procfs fan",
8413 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008414
8415 return 1;
8416}
8417
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008418static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8419{
8420 int interval;
8421
8422 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8423 return 0;
8424
8425 if (interval < 0 || interval > 120)
8426 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008427 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008428 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008429 tpacpi_disclose_usertask("procfs fan",
8430 "set watchdog timer to %d\n",
8431 interval);
8432 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008433
8434 return 1;
8435}
8436
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008437static int fan_write(char *buf)
8438{
8439 char *cmd;
8440 int rc = 0;
8441
8442 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008443 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008444 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008445 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008446 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008447 fan_write_cmd_disable(cmd, &rc) ||
8448 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008449 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008450 fan_write_cmd_speed(cmd, &rc))
8451 )
8452 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008453 else if (!rc)
8454 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008455 }
8456
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008457 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008458}
8459
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008460static struct ibm_struct fan_driver_data = {
8461 .name = "fan",
8462 .read = fan_read,
8463 .write = fan_write,
8464 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008465 .suspend = fan_suspend,
8466 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008467};
8468
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008469/****************************************************************************
8470 ****************************************************************************
8471 *
8472 * Infrastructure
8473 *
8474 ****************************************************************************
8475 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008477/*
8478 * HKEY event callout for other subdrivers go here
8479 * (yes, it is ugly, but it is quick, safe, and gets the job done
8480 */
8481static void tpacpi_driver_event(const unsigned int hkey_event)
8482{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008483 if (ibm_backlight_device) {
8484 switch (hkey_event) {
8485 case TP_HKEY_EV_BRGHT_UP:
8486 case TP_HKEY_EV_BRGHT_DOWN:
8487 tpacpi_brightness_notify_change();
8488 }
8489 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008490 if (alsa_card) {
8491 switch (hkey_event) {
8492 case TP_HKEY_EV_VOL_UP:
8493 case TP_HKEY_EV_VOL_DOWN:
8494 case TP_HKEY_EV_VOL_MUTE:
8495 volume_alsa_notify_change();
8496 }
8497 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008498}
8499
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008500static void hotkey_driver_event(const unsigned int scancode)
8501{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008502 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008503}
8504
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008505/* sysfs name ---------------------------------------------------------- */
8506static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8507 struct device_attribute *attr,
8508 char *buf)
8509{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008510 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008511}
8512
8513static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8514 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8515
8516/* --------------------------------------------------------------------- */
8517
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008518/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03008519static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008520
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008521/*
8522 * Module and infrastructure proble, init and exit handling
8523 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008524
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008525static int force_load;
8526
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008527#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008528static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008529{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008530 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008531
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008532 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008533}
8534#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8535
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008536static void ibm_exit(struct ibm_struct *ibm)
8537{
8538 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8539
8540 list_del_init(&ibm->all_drivers);
8541
8542 if (ibm->flags.acpi_notify_installed) {
8543 dbg_printk(TPACPI_DBG_EXIT,
8544 "%s: acpi_remove_notify_handler\n", ibm->name);
8545 BUG_ON(!ibm->acpi);
8546 acpi_remove_notify_handler(*ibm->acpi->handle,
8547 ibm->acpi->type,
8548 dispatch_acpi_notify);
8549 ibm->flags.acpi_notify_installed = 0;
8550 ibm->flags.acpi_notify_installed = 0;
8551 }
8552
8553 if (ibm->flags.proc_created) {
8554 dbg_printk(TPACPI_DBG_EXIT,
8555 "%s: remove_proc_entry\n", ibm->name);
8556 remove_proc_entry(ibm->name, proc_dir);
8557 ibm->flags.proc_created = 0;
8558 }
8559
8560 if (ibm->flags.acpi_driver_registered) {
8561 dbg_printk(TPACPI_DBG_EXIT,
8562 "%s: acpi_bus_unregister_driver\n", ibm->name);
8563 BUG_ON(!ibm->acpi);
8564 acpi_bus_unregister_driver(ibm->acpi->driver);
8565 kfree(ibm->acpi->driver);
8566 ibm->acpi->driver = NULL;
8567 ibm->flags.acpi_driver_registered = 0;
8568 }
8569
8570 if (ibm->flags.init_called && ibm->exit) {
8571 ibm->exit();
8572 ibm->flags.init_called = 0;
8573 }
8574
8575 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8576}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008577
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008578static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008579{
8580 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008581 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582 struct proc_dir_entry *entry;
8583
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008584 BUG_ON(ibm == NULL);
8585
8586 INIT_LIST_HEAD(&ibm->all_drivers);
8587
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008588 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008589 return 0;
8590
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008591 dbg_printk(TPACPI_DBG_INIT,
8592 "probing for %s\n", ibm->name);
8593
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008594 if (iibm->init) {
8595 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008596 if (ret > 0)
8597 return 0; /* probe failed */
8598 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008599 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008600
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008601 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008602 }
8603
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008604 if (ibm->acpi) {
8605 if (ibm->acpi->hid) {
8606 ret = register_tpacpi_subdriver(ibm);
8607 if (ret)
8608 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008609 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008610
8611 if (ibm->acpi->notify) {
8612 ret = setup_acpi_notify(ibm);
8613 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008614 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008615 ibm->name);
8616 ret = 0;
8617 goto err_out;
8618 }
8619 if (ret < 0)
8620 goto err_out;
8621 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008622 }
8623
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008624 dbg_printk(TPACPI_DBG_INIT,
8625 "%s installed\n", ibm->name);
8626
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008627 if (ibm->read) {
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008628 mode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008629
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008630 if (!mode)
8631 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008632 if (ibm->write)
8633 mode |= S_IWUSR;
8634 entry = proc_create_data(ibm->name, mode, proc_dir,
8635 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008636 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008637 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008638 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008639 ret = -ENODEV;
8640 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008641 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008642 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008645 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8646
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008648
8649err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008650 dbg_printk(TPACPI_DBG_INIT,
8651 "%s: at error exit path with result %d\n",
8652 ibm->name, ret);
8653
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008654 ibm_exit(ibm);
8655 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008656}
8657
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008658/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008659
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008660static bool __pure __init tpacpi_is_fw_digit(const char c)
8661{
8662 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8663}
8664
8665/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8666static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8667 const char t)
8668{
8669 return s && strlen(s) >= 8 &&
8670 tpacpi_is_fw_digit(s[0]) &&
8671 tpacpi_is_fw_digit(s[1]) &&
8672 s[2] == t && s[3] == 'T' &&
8673 tpacpi_is_fw_digit(s[4]) &&
8674 tpacpi_is_fw_digit(s[5]) &&
8675 s[6] == 'W' && s[7] == 'W';
8676}
8677
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008678/* returns 0 - probe ok, or < 0 - probe error.
8679 * Probe ok doesn't mean thinkpad found.
8680 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8681static int __must_check __init get_thinkpad_model_data(
8682 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008683{
Jeff Garzik18552562007-10-03 15:15:40 -04008684 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008685 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008686 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008687
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008688 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008689 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008690
8691 memset(tp, 0, sizeof(*tp));
8692
8693 if (dmi_name_in_vendors("IBM"))
8694 tp->vendor = PCI_VENDOR_ID_IBM;
8695 else if (dmi_name_in_vendors("LENOVO"))
8696 tp->vendor = PCI_VENDOR_ID_LENOVO;
8697 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008698 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008699
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008700 s = dmi_get_system_info(DMI_BIOS_VERSION);
8701 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8702 if (s && !tp->bios_version_str)
8703 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008704
8705 /* Really ancient ThinkPad 240X will fail this, which is fine */
8706 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008707 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008708
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008709 tp->bios_model = tp->bios_version_str[0]
8710 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008711 tp->bios_release = (tp->bios_version_str[4] << 8)
8712 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008713
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008714 /*
8715 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8716 * X32 or newer, all Z series; Some models must have an
8717 * up-to-date BIOS or they will not be detected.
8718 *
8719 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8720 */
8721 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008722 if (sscanf(dev->name,
8723 "IBM ThinkPad Embedded Controller -[%17c",
8724 ec_fw_string) == 1) {
8725 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8726 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008727
8728 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008729 if (!tp->ec_version_str)
8730 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008731
8732 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8733 tp->ec_model = ec_fw_string[0]
8734 | (ec_fw_string[1] << 8);
8735 tp->ec_release = (ec_fw_string[4] << 8)
8736 | ec_fw_string[5];
8737 } else {
8738 printk(TPACPI_NOTICE
8739 "ThinkPad firmware release %s "
8740 "doesn't match the known patterns\n",
8741 ec_fw_string);
8742 printk(TPACPI_NOTICE
8743 "please report this to %s\n",
8744 TPACPI_MAIL);
8745 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008746 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008747 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008748 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008749
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008750 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8751 if (s && !strnicmp(s, "ThinkPad", 8)) {
8752 tp->model_str = kstrdup(s, GFP_KERNEL);
8753 if (!tp->model_str)
8754 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008755 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008756
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008757 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8758 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8759 if (s && !tp->nummodel_str)
8760 return -ENOMEM;
8761
8762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008763}
8764
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008765static int __init probe_for_thinkpad(void)
8766{
8767 int is_thinkpad;
8768
8769 if (acpi_disabled)
8770 return -ENODEV;
8771
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008772 /* It would be dangerous to run the driver in this case */
8773 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8774 return -ENODEV;
8775
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008776 /*
8777 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008778 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008779 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008780 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8781 (thinkpad_id.ec_model != 0) ||
8782 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008783
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03008784 /* The EC handler is required */
8785 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008786 if (!ec_handle) {
8787 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008788 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008789 "Not yet supported ThinkPad detected!\n");
8790 return -ENODEV;
8791 }
8792
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008793 if (!is_thinkpad && !force_load)
8794 return -ENODEV;
8795
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008796 return 0;
8797}
8798
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008799static void __init thinkpad_acpi_init_banner(void)
8800{
8801 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8802 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8803
8804 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8805 (thinkpad_id.bios_version_str) ?
8806 thinkpad_id.bios_version_str : "unknown",
8807 (thinkpad_id.ec_version_str) ?
8808 thinkpad_id.ec_version_str : "unknown");
8809
8810 BUG_ON(!thinkpad_id.vendor);
8811
8812 if (thinkpad_id.model_str)
8813 printk(TPACPI_INFO "%s %s, model %s\n",
8814 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8815 "IBM" : ((thinkpad_id.vendor ==
8816 PCI_VENDOR_ID_LENOVO) ?
8817 "Lenovo" : "Unknown vendor"),
8818 thinkpad_id.model_str,
8819 (thinkpad_id.nummodel_str) ?
8820 thinkpad_id.nummodel_str : "unknown");
8821}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008822
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008823/* Module init, exit, parameters */
8824
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008825static struct ibm_init_struct ibms_init[] __initdata = {
8826 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008827 .data = &thinkpad_acpi_driver_data,
8828 },
8829 {
8830 .init = hotkey_init,
8831 .data = &hotkey_driver_data,
8832 },
8833 {
8834 .init = bluetooth_init,
8835 .data = &bluetooth_driver_data,
8836 },
8837 {
8838 .init = wan_init,
8839 .data = &wan_driver_data,
8840 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008841 {
8842 .init = uwb_init,
8843 .data = &uwb_driver_data,
8844 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008845#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008846 {
8847 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008848 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008849 .data = &video_driver_data,
8850 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008851#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008852 {
8853 .init = light_init,
8854 .data = &light_driver_data,
8855 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008856 {
8857 .init = cmos_init,
8858 .data = &cmos_driver_data,
8859 },
8860 {
8861 .init = led_init,
8862 .data = &led_driver_data,
8863 },
8864 {
8865 .init = beep_init,
8866 .data = &beep_driver_data,
8867 },
8868 {
8869 .init = thermal_init,
8870 .data = &thermal_driver_data,
8871 },
8872 {
8873 .data = &ecdump_driver_data,
8874 },
8875 {
8876 .init = brightness_init,
8877 .data = &brightness_driver_data,
8878 },
8879 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008880 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008881 .data = &volume_driver_data,
8882 },
8883 {
8884 .init = fan_init,
8885 .data = &fan_driver_data,
8886 },
8887};
8888
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008889static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8890{
8891 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008892 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008893
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008894 if (!kp || !kp->name || !val)
8895 return -EINVAL;
8896
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008897 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8898 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008899 WARN_ON(ibm == NULL);
8900
8901 if (!ibm || !ibm->name)
8902 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008903
8904 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8905 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008906 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008907 strcpy(ibms_init[i].param, val);
8908 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008909 return 0;
8910 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008911 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008912
8913 return -EINVAL;
8914}
8915
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008916module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008917MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008918 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008919
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008920module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008921MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008922
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008923module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008924MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008925 "Attempts to load the driver even on a "
8926 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008927
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008928module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008929MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008930 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008931
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008932module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008933MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008934 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008935 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008936
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008937module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008938MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008939 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008940
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008941module_param(hotkey_report_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008942MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008943 "used for backwards compatibility with userspace, "
8944 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008945
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008946#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008947module_param_named(volume_mode, volume_mode, uint, 0444);
8948MODULE_PARM_DESC(volume_mode,
8949 "Selects volume control strategy: "
8950 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8951
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008952module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8953MODULE_PARM_DESC(volume_capabilities,
8954 "Selects the mixer capabilites: "
8955 "0=auto, 1=volume and mute, 2=mute only");
8956
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008957module_param_named(volume_control, volume_control_allowed, bool, 0444);
8958MODULE_PARM_DESC(volume_control,
8959 "Enables software override for the console audio "
8960 "control when true");
8961
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008962/* ALSA module API parameters */
8963module_param_named(index, alsa_index, int, 0444);
8964MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8965module_param_named(id, alsa_id, charp, 0444);
8966MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8967module_param_named(enable, alsa_enable, bool, 0444);
8968MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008969#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008970
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008971#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008972 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008973 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008974 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008975
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008976TPACPI_PARAM(hotkey);
8977TPACPI_PARAM(bluetooth);
8978TPACPI_PARAM(video);
8979TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008980TPACPI_PARAM(cmos);
8981TPACPI_PARAM(led);
8982TPACPI_PARAM(beep);
8983TPACPI_PARAM(ecdump);
8984TPACPI_PARAM(brightness);
8985TPACPI_PARAM(volume);
8986TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008987
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008988#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008989module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008990MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8991module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8992MODULE_PARM_DESC(wlsw_state,
8993 "Initial state of the emulated WLSW switch");
8994
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008995module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008996MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8997module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8998MODULE_PARM_DESC(bluetooth_state,
8999 "Initial state of the emulated bluetooth switch");
9000
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009001module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009002MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9003module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9004MODULE_PARM_DESC(wwan_state,
9005 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009006
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009007module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009008MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9009module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9010MODULE_PARM_DESC(uwb_state,
9011 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009012#endif
9013
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009014static void thinkpad_acpi_module_exit(void)
9015{
9016 struct ibm_struct *ibm, *itmp;
9017
9018 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9019
9020 list_for_each_entry_safe_reverse(ibm, itmp,
9021 &tpacpi_all_drivers,
9022 all_drivers) {
9023 ibm_exit(ibm);
9024 }
9025
9026 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9027
9028 if (tpacpi_inputdev) {
9029 if (tp_features.input_device_registered)
9030 input_unregister_device(tpacpi_inputdev);
9031 else
9032 input_free_device(tpacpi_inputdev);
9033 }
9034
9035 if (tpacpi_hwmon)
9036 hwmon_device_unregister(tpacpi_hwmon);
9037
9038 if (tp_features.sensors_pdev_attrs_registered)
9039 device_remove_file(&tpacpi_sensors_pdev->dev,
9040 &dev_attr_thinkpad_acpi_pdev_name);
9041 if (tpacpi_sensors_pdev)
9042 platform_device_unregister(tpacpi_sensors_pdev);
9043 if (tpacpi_pdev)
9044 platform_device_unregister(tpacpi_pdev);
9045
9046 if (tp_features.sensors_pdrv_attrs_registered)
9047 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9048 if (tp_features.platform_drv_attrs_registered)
9049 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9050
9051 if (tp_features.sensors_pdrv_registered)
9052 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9053
9054 if (tp_features.platform_drv_registered)
9055 platform_driver_unregister(&tpacpi_pdriver);
9056
9057 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009058 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009059
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009060 if (tpacpi_wq)
9061 destroy_workqueue(tpacpi_wq);
9062
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009063 kfree(thinkpad_id.bios_version_str);
9064 kfree(thinkpad_id.ec_version_str);
9065 kfree(thinkpad_id.model_str);
9066}
9067
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009068
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009069static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009070{
9071 int ret, i;
9072
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009073 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9074
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03009075 /* Parameter checking */
9076 if (hotkey_report_mode > 2)
9077 return -EINVAL;
9078
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009079 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009080
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009081 ret = get_thinkpad_model_data(&thinkpad_id);
9082 if (ret) {
9083 printk(TPACPI_ERR
9084 "unable to get DMI data: %d\n", ret);
9085 thinkpad_acpi_module_exit();
9086 return ret;
9087 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009088 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009089 if (ret) {
9090 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009091 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009093
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009094 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009095
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009096 thinkpad_acpi_init_banner();
9097 tpacpi_check_outdated_fw();
9098
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009099 TPACPI_ACPIHANDLE_INIT(ecrd);
9100 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009101
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009102 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9103 if (!tpacpi_wq) {
9104 thinkpad_acpi_module_exit();
9105 return -ENOMEM;
9106 }
9107
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009108 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009109 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009110 printk(TPACPI_ERR
9111 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009112 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009113 return -ENODEV;
9114 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009115
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009116 ret = platform_driver_register(&tpacpi_pdriver);
9117 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009118 printk(TPACPI_ERR
9119 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009120 thinkpad_acpi_module_exit();
9121 return ret;
9122 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009123 tp_features.platform_drv_registered = 1;
9124
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009125 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9126 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009127 printk(TPACPI_ERR
9128 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009129 thinkpad_acpi_module_exit();
9130 return ret;
9131 }
9132 tp_features.sensors_pdrv_registered = 1;
9133
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009134 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009135 if (!ret) {
9136 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009137 ret = tpacpi_create_driver_attributes(
9138 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009139 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009140 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009141 printk(TPACPI_ERR
9142 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009143 thinkpad_acpi_module_exit();
9144 return ret;
9145 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009146 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009147
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009148
9149 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009150 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009151 NULL, 0);
9152 if (IS_ERR(tpacpi_pdev)) {
9153 ret = PTR_ERR(tpacpi_pdev);
9154 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009155 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009156 thinkpad_acpi_module_exit();
9157 return ret;
9158 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009159 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009160 TPACPI_HWMON_DRVR_NAME,
9161 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009162 if (IS_ERR(tpacpi_sensors_pdev)) {
9163 ret = PTR_ERR(tpacpi_sensors_pdev);
9164 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009165 printk(TPACPI_ERR
9166 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009167 thinkpad_acpi_module_exit();
9168 return ret;
9169 }
9170 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9171 &dev_attr_thinkpad_acpi_pdev_name);
9172 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009173 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009174 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009175 thinkpad_acpi_module_exit();
9176 return ret;
9177 }
9178 tp_features.sensors_pdev_attrs_registered = 1;
9179 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009180 if (IS_ERR(tpacpi_hwmon)) {
9181 ret = PTR_ERR(tpacpi_hwmon);
9182 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009183 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009184 thinkpad_acpi_module_exit();
9185 return ret;
9186 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009187 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009188 tpacpi_inputdev = input_allocate_device();
9189 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009190 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009191 thinkpad_acpi_module_exit();
9192 return -ENOMEM;
9193 } else {
9194 /* Prepare input device, but don't register */
9195 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009196 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009197 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009198 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009199 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9200 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009201 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009202 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009203
9204 /* Init subdriver dependencies */
9205 tpacpi_detect_brightness_capabilities();
9206
9207 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009208 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9209 ret = ibm_init(&ibms_init[i]);
9210 if (ret >= 0 && *ibms_init[i].param)
9211 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009212 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009213 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009214 return ret;
9215 }
9216 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009217
9218 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9219
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009220 ret = input_register_device(tpacpi_inputdev);
9221 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009222 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009223 thinkpad_acpi_module_exit();
9224 return ret;
9225 } else {
9226 tp_features.input_device_registered = 1;
9227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009228
9229 return 0;
9230}
9231
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009232MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9233
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009234/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009235 * This will autoload the driver in almost every ThinkPad
9236 * in widespread use.
9237 *
9238 * Only _VERY_ old models, like the 240, 240x and 570 lack
9239 * the HKEY event interface.
9240 */
9241MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9242
9243/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009244 * DMI matching for module autoloading
9245 *
9246 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9247 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9248 *
9249 * Only models listed in thinkwiki will be supported, so add yours
9250 * if it is not there yet.
9251 */
9252#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009253 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009254
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009255/* Ancient thinkpad BIOSes have to be identified by
9256 * BIOS type or model number, and there are far less
9257 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009258IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009259
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009260MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9261MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009262MODULE_DESCRIPTION(TPACPI_DESC);
9263MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009264MODULE_LICENSE("GPL");
9265
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009266module_init(thinkpad_acpi_module_init);
9267module_exit(thinkpad_acpi_module_exit);