blob: 29ad273464439b37f379c5a80ce77e16edbd4f52 [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 Holschuh94954cc2007-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 */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300608 success = (status == AE_OK &&
609 out_obj.type == ACPI_TYPE_INTEGER);
610 if (success && res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 *(int *)res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400613 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 success = status == AE_OK;
615 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400616 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200618 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 "with invalid format character '%c'\n", res_type);
620 return 0;
621 }
622
623 if (!success && !quiet)
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300624 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %s\n",
625 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 return success;
628}
629
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200630static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300631{
632 int v;
633
634 if (ecrd_handle) {
635 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
636 return 0;
637 *p = v;
638 } else {
639 if (ec_read(i, p) < 0)
640 return 0;
641 }
642
643 return 1;
644}
645
646static int acpi_ec_write(int i, u8 v)
647{
648 if (ecwr_handle) {
649 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
650 return 0;
651 } else {
652 if (ec_write(i, v) < 0)
653 return 0;
654 }
655
656 return 1;
657}
658
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300659static int issue_thinkpad_cmos_command(int cmos_cmd)
660{
661 if (!cmos_handle)
662 return -ENXIO;
663
664 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
665 return -EIO;
666
667 return 0;
668}
669
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300670/*************************************************************************
671 * ACPI device model
672 */
673
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200674#define TPACPI_ACPIHANDLE_INIT(object) \
675 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200676 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
677
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300678static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300679 acpi_handle *handle, acpi_handle parent,
680 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300681{
682 int i;
683 acpi_status status;
684
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300685 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
686 name);
687
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300688 for (i = 0; i < num_paths; i++) {
689 status = acpi_get_handle(parent, paths[i], handle);
690 if (ACPI_SUCCESS(status)) {
691 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300692 dbg_printk(TPACPI_DBG_INIT,
693 "Found ACPI handle %s for %s\n",
694 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300695 return;
696 }
697 }
698
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300699 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
700 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300701 *handle = NULL;
702}
703
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300704static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
705 u32 level, void *context, void **return_value)
706{
707 *(acpi_handle *)return_value = handle;
708
709 return AE_CTRL_TERMINATE;
710}
711
712static void __init tpacpi_acpi_handle_locate(const char *name,
713 const char *hid,
714 acpi_handle *handle)
715{
716 acpi_status status;
717 acpi_handle device_found;
718
719 BUG_ON(!name || !hid || !handle);
720 vdbg_printk(TPACPI_DBG_INIT,
721 "trying to locate ACPI handle for %s, using HID %s\n",
722 name, hid);
723
724 memset(&device_found, 0, sizeof(device_found));
725 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
726 (void *)name, &device_found);
727
728 *handle = NULL;
729
730 if (ACPI_SUCCESS(status)) {
731 *handle = device_found;
732 dbg_printk(TPACPI_DBG_INIT,
733 "Found ACPI handle for %s\n", name);
734 } else {
735 vdbg_printk(TPACPI_DBG_INIT,
736 "Could not locate an ACPI handle for %s: %s\n",
737 name, acpi_format_exception(status));
738 }
739}
740
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300741static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300742{
743 struct ibm_struct *ibm = data;
744
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300745 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
746 return;
747
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300748 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300749 return;
750
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300751 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300752}
753
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300754static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300755{
756 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300757 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300758
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300759 BUG_ON(!ibm->acpi);
760
761 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300762 return 0;
763
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300764 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300765 "setting up ACPI notify for %s\n", ibm->name);
766
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300767 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
768 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200769 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300770 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300771 return -ENODEV;
772 }
773
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700774 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300775 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200776 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300777 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300778
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300779 status = acpi_install_notify_handler(*ibm->acpi->handle,
780 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300781 if (ACPI_FAILURE(status)) {
782 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200783 printk(TPACPI_NOTICE
784 "another device driver is already "
785 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300786 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200787 printk(TPACPI_ERR
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300788 "acpi_install_notify_handler(%s) failed: %s\n",
789 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300790 }
791 return -ENODEV;
792 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300793 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300794 return 0;
795}
796
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300797static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300798{
799 return 0;
800}
801
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300802static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300803{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300804 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300805
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300806 dbg_printk(TPACPI_DBG_INIT,
807 "registering %s as an ACPI driver\n", ibm->name);
808
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300809 BUG_ON(!ibm->acpi);
810
811 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
812 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300813 printk(TPACPI_ERR
814 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300815 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300816 }
817
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200818 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300819 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200820
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300821 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300822
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300823 rc = acpi_bus_register_driver(ibm->acpi->driver);
824 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200825 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200826 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300827 kfree(ibm->acpi->driver);
828 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300829 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300830 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300831
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300832 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300833}
834
835
836/****************************************************************************
837 ****************************************************************************
838 *
839 * Procfs Helpers
840 *
841 ****************************************************************************
842 ****************************************************************************/
843
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200844static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300845{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200846 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300847
848 if (!ibm || !ibm->read)
849 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200850 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300851}
852
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200853static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300854{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200855 return single_open(file, dispatch_proc_show, PDE(inode)->data);
856}
857
858static ssize_t dispatch_proc_write(struct file *file,
859 const char __user *userbuf,
860 size_t count, loff_t *pos)
861{
862 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300863 char *kernbuf;
864 int ret;
865
866 if (!ibm || !ibm->write)
867 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300868 if (count > PAGE_SIZE - 2)
869 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300870
871 kernbuf = kmalloc(count + 2, GFP_KERNEL);
872 if (!kernbuf)
873 return -ENOMEM;
874
875 if (copy_from_user(kernbuf, userbuf, count)) {
876 kfree(kernbuf);
877 return -EFAULT;
878 }
879
880 kernbuf[count] = 0;
881 strcat(kernbuf, ",");
882 ret = ibm->write(kernbuf);
883 if (ret == 0)
884 ret = count;
885
886 kfree(kernbuf);
887
888 return ret;
889}
890
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200891static const struct file_operations dispatch_proc_fops = {
892 .owner = THIS_MODULE,
893 .open = dispatch_proc_open,
894 .read = seq_read,
895 .llseek = seq_lseek,
896 .release = single_release,
897 .write = dispatch_proc_write,
898};
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900static char *next_cmd(char **cmds)
901{
902 char *start = *cmds;
903 char *end;
904
905 while ((end = strchr(start, ',')) && end == start)
906 start = end + 1;
907
908 if (!end)
909 return NULL;
910
911 *end = 0;
912 *cmds = end + 1;
913 return start;
914}
915
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300916
917/****************************************************************************
918 ****************************************************************************
919 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300920 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300921 *
922 ****************************************************************************
923 ****************************************************************************/
924
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300925static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300926static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700927static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300928static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300929static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200930static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300931
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200932static int tpacpi_suspend_handler(struct platform_device *pdev,
933 pm_message_t state)
934{
935 struct ibm_struct *ibm, *itmp;
936
937 list_for_each_entry_safe(ibm, itmp,
938 &tpacpi_all_drivers,
939 all_drivers) {
940 if (ibm->suspend)
941 (ibm->suspend)(state);
942 }
943
944 return 0;
945}
946
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300947static int tpacpi_resume_handler(struct platform_device *pdev)
948{
949 struct ibm_struct *ibm, *itmp;
950
951 list_for_each_entry_safe(ibm, itmp,
952 &tpacpi_all_drivers,
953 all_drivers) {
954 if (ibm->resume)
955 (ibm->resume)();
956 }
957
958 return 0;
959}
960
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200961static void tpacpi_shutdown_handler(struct platform_device *pdev)
962{
963 struct ibm_struct *ibm, *itmp;
964
965 list_for_each_entry_safe(ibm, itmp,
966 &tpacpi_all_drivers,
967 all_drivers) {
968 if (ibm->shutdown)
969 (ibm->shutdown)();
970 }
971}
972
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300973static struct platform_driver tpacpi_pdriver = {
974 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200975 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300976 .owner = THIS_MODULE,
977 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200978 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300979 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200980 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300981};
982
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300983static struct platform_driver tpacpi_hwmon_pdriver = {
984 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200985 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300986 .owner = THIS_MODULE,
987 },
988};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300989
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300990/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300991 * sysfs support helpers
992 */
993
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200994struct attribute_set {
995 unsigned int members, max_members;
996 struct attribute_group group;
997};
998
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300999struct attribute_set_obj {
1000 struct attribute_set s;
1001 struct attribute *a;
1002} __attribute__((packed));
1003
1004static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001005 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001006{
1007 struct attribute_set_obj *sobj;
1008
1009 if (max_members == 0)
1010 return NULL;
1011
1012 /* Allocates space for implicit NULL at the end too */
1013 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1014 max_members * sizeof(struct attribute *),
1015 GFP_KERNEL);
1016 if (!sobj)
1017 return NULL;
1018 sobj->s.max_members = max_members;
1019 sobj->s.group.attrs = &sobj->a;
1020 sobj->s.group.name = name;
1021
1022 return &sobj->s;
1023}
1024
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001025#define destroy_attr_set(_set) \
1026 kfree(_set);
1027
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001028/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001029static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001030{
1031 if (!s || !attr)
1032 return -EINVAL;
1033
1034 if (s->members >= s->max_members)
1035 return -ENOMEM;
1036
1037 s->group.attrs[s->members] = attr;
1038 s->members++;
1039
1040 return 0;
1041}
1042
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001043static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001044 struct attribute **attr,
1045 unsigned int count)
1046{
1047 int i, res;
1048
1049 for (i = 0; i < count; i++) {
1050 res = add_to_attr_set(s, attr[i]);
1051 if (res)
1052 return res;
1053 }
1054
1055 return 0;
1056}
1057
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001058static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001059{
1060 sysfs_remove_group(kobj, &s->group);
1061 destroy_attr_set(s);
1062}
1063
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001064#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1065 sysfs_create_group(_kobj, &_attr_set->group)
1066
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001067static int parse_strtoul(const char *buf,
1068 unsigned long max, unsigned long *value)
1069{
1070 char *endp;
1071
André Goddard Rosae7d28602009-12-14 18:01:06 -08001072 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1073 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001074 if (*endp || *value > max)
1075 return -EINVAL;
1076
1077 return 0;
1078}
1079
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001080static void tpacpi_disable_brightness_delay(void)
1081{
1082 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1083 printk(TPACPI_NOTICE
1084 "ACPI backlight control delay disabled\n");
1085}
1086
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001087static void printk_deprecated_attribute(const char * const what,
1088 const char * const details)
1089{
1090 tpacpi_log_usertask("deprecated sysfs attribute");
1091 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1092 "will be removed. %s\n",
1093 what, details);
1094}
1095
Johannes Berg19d337d2009-06-02 13:01:37 +02001096/*************************************************************************
1097 * rfkill and radio control support helpers
1098 */
1099
1100/*
1101 * ThinkPad-ACPI firmware handling model:
1102 *
1103 * WLSW (master wireless switch) is event-driven, and is common to all
1104 * firmware-controlled radios. It cannot be controlled, just monitored,
1105 * as expected. It overrides all radio state in firmware
1106 *
1107 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1108 * (TODO: verify how WLSW interacts with the returned radio state).
1109 *
1110 * The only time there are shadow radio state changes, is when
1111 * masked-off hotkeys are used.
1112 */
1113
1114/*
1115 * Internal driver API for radio state:
1116 *
1117 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1118 * bool: true means radio blocked (off)
1119 */
1120enum tpacpi_rfkill_state {
1121 TPACPI_RFK_RADIO_OFF = 0,
1122 TPACPI_RFK_RADIO_ON
1123};
1124
1125/* rfkill switches */
1126enum tpacpi_rfk_id {
1127 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1128 TPACPI_RFK_WWAN_SW_ID,
1129 TPACPI_RFK_UWB_SW_ID,
1130 TPACPI_RFK_SW_MAX
1131};
1132
1133static const char *tpacpi_rfkill_names[] = {
1134 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1135 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1136 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1137 [TPACPI_RFK_SW_MAX] = NULL
1138};
1139
1140/* ThinkPad-ACPI rfkill subdriver */
1141struct tpacpi_rfk {
1142 struct rfkill *rfkill;
1143 enum tpacpi_rfk_id id;
1144 const struct tpacpi_rfk_ops *ops;
1145};
1146
1147struct tpacpi_rfk_ops {
1148 /* firmware interface */
1149 int (*get_status)(void);
1150 int (*set_status)(const enum tpacpi_rfkill_state);
1151};
1152
1153static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1154
1155/* Query FW and update rfkill sw state for a given rfkill switch */
1156static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1157{
1158 int status;
1159
1160 if (!tp_rfk)
1161 return -ENODEV;
1162
1163 status = (tp_rfk->ops->get_status)();
1164 if (status < 0)
1165 return status;
1166
1167 rfkill_set_sw_state(tp_rfk->rfkill,
1168 (status == TPACPI_RFK_RADIO_OFF));
1169
1170 return status;
1171}
1172
1173/* Query FW and update rfkill sw state for all rfkill switches */
1174static void tpacpi_rfk_update_swstate_all(void)
1175{
1176 unsigned int i;
1177
1178 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1179 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1180}
1181
1182/*
1183 * Sync the HW-blocking state of all rfkill switches,
1184 * do notice it causes the rfkill core to schedule uevents
1185 */
1186static void tpacpi_rfk_update_hwblock_state(bool blocked)
1187{
1188 unsigned int i;
1189 struct tpacpi_rfk *tp_rfk;
1190
1191 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1192 tp_rfk = tpacpi_rfkill_switches[i];
1193 if (tp_rfk) {
1194 if (rfkill_set_hw_state(tp_rfk->rfkill,
1195 blocked)) {
1196 /* ignore -- we track sw block */
1197 }
1198 }
1199 }
1200}
1201
1202/* Call to get the WLSW state from the firmware */
1203static int hotkey_get_wlsw(void);
1204
1205/* Call to query WLSW state and update all rfkill switches */
1206static bool tpacpi_rfk_check_hwblock_state(void)
1207{
1208 int res = hotkey_get_wlsw();
1209 int hw_blocked;
1210
1211 /* When unknown or unsupported, we have to assume it is unblocked */
1212 if (res < 0)
1213 return false;
1214
1215 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1216 tpacpi_rfk_update_hwblock_state(hw_blocked);
1217
1218 return hw_blocked;
1219}
1220
1221static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1222{
1223 struct tpacpi_rfk *tp_rfk = data;
1224 int res;
1225
1226 dbg_printk(TPACPI_DBG_RFKILL,
1227 "request to change radio state to %s\n",
1228 blocked ? "blocked" : "unblocked");
1229
1230 /* try to set radio state */
1231 res = (tp_rfk->ops->set_status)(blocked ?
1232 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1233
1234 /* and update the rfkill core with whatever the FW really did */
1235 tpacpi_rfk_update_swstate(tp_rfk);
1236
1237 return (res < 0) ? res : 0;
1238}
1239
1240static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1241 .set_block = tpacpi_rfk_hook_set_block,
1242};
1243
1244static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1245 const struct tpacpi_rfk_ops *tp_rfkops,
1246 const enum rfkill_type rfktype,
1247 const char *name,
1248 const bool set_default)
1249{
1250 struct tpacpi_rfk *atp_rfk;
1251 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001252 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001253 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001254 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001255
1256 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1257
Johannes Berg19d337d2009-06-02 13:01:37 +02001258 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1259 if (atp_rfk)
1260 atp_rfk->rfkill = rfkill_alloc(name,
1261 &tpacpi_pdev->dev,
1262 rfktype,
1263 &tpacpi_rfk_rfkill_ops,
1264 atp_rfk);
1265 if (!atp_rfk || !atp_rfk->rfkill) {
1266 printk(TPACPI_ERR
1267 "failed to allocate memory for rfkill class\n");
1268 kfree(atp_rfk);
1269 return -ENOMEM;
1270 }
1271
1272 atp_rfk->id = id;
1273 atp_rfk->ops = tp_rfkops;
1274
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001275 sw_status = (tp_rfkops->get_status)();
1276 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001277 printk(TPACPI_ERR
1278 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001279 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001280 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001281 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001282 if (set_default) {
1283 /* try to keep the initial state, since we ask the
1284 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001285 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001286 }
1287 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001288 hw_state = tpacpi_rfk_check_hwblock_state();
1289 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001290
1291 res = rfkill_register(atp_rfk->rfkill);
1292 if (res < 0) {
1293 printk(TPACPI_ERR
1294 "failed to register %s rfkill switch: %d\n",
1295 name, res);
1296 rfkill_destroy(atp_rfk->rfkill);
1297 kfree(atp_rfk);
1298 return res;
1299 }
1300
1301 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001302
1303 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1304 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001305 return 0;
1306}
1307
1308static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1309{
1310 struct tpacpi_rfk *tp_rfk;
1311
1312 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1313
1314 tp_rfk = tpacpi_rfkill_switches[id];
1315 if (tp_rfk) {
1316 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001317 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001318 tpacpi_rfkill_switches[id] = NULL;
1319 kfree(tp_rfk);
1320 }
1321}
1322
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001323static void printk_deprecated_rfkill_attribute(const char * const what)
1324{
1325 printk_deprecated_attribute(what,
1326 "Please switch to generic rfkill before year 2010");
1327}
1328
Johannes Berg19d337d2009-06-02 13:01:37 +02001329/* sysfs <radio> enable ------------------------------------------------ */
1330static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1331 struct device_attribute *attr,
1332 char *buf)
1333{
1334 int status;
1335
1336 printk_deprecated_rfkill_attribute(attr->attr.name);
1337
1338 /* This is in the ABI... */
1339 if (tpacpi_rfk_check_hwblock_state()) {
1340 status = TPACPI_RFK_RADIO_OFF;
1341 } else {
1342 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1343 if (status < 0)
1344 return status;
1345 }
1346
1347 return snprintf(buf, PAGE_SIZE, "%d\n",
1348 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1349}
1350
1351static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1352 struct device_attribute *attr,
1353 const char *buf, size_t count)
1354{
1355 unsigned long t;
1356 int res;
1357
1358 printk_deprecated_rfkill_attribute(attr->attr.name);
1359
1360 if (parse_strtoul(buf, 1, &t))
1361 return -EINVAL;
1362
1363 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1364
1365 /* This is in the ABI... */
1366 if (tpacpi_rfk_check_hwblock_state() && !!t)
1367 return -EPERM;
1368
1369 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1370 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1371 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1372
1373 return (res < 0) ? res : count;
1374}
1375
1376/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001377static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001378{
Johannes Berg19d337d2009-06-02 13:01:37 +02001379 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001380 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001381 else {
1382 int status;
1383
1384 /* This is in the ABI... */
1385 if (tpacpi_rfk_check_hwblock_state()) {
1386 status = TPACPI_RFK_RADIO_OFF;
1387 } else {
1388 status = tpacpi_rfk_update_swstate(
1389 tpacpi_rfkill_switches[id]);
1390 if (status < 0)
1391 return status;
1392 }
1393
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001394 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001395 (status == TPACPI_RFK_RADIO_ON) ?
1396 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001397 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001398 }
1399
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001400 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001401}
1402
1403static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1404{
1405 char *cmd;
1406 int status = -1;
1407 int res = 0;
1408
1409 if (id >= TPACPI_RFK_SW_MAX)
1410 return -ENODEV;
1411
1412 while ((cmd = next_cmd(&buf))) {
1413 if (strlencmp(cmd, "enable") == 0)
1414 status = TPACPI_RFK_RADIO_ON;
1415 else if (strlencmp(cmd, "disable") == 0)
1416 status = TPACPI_RFK_RADIO_OFF;
1417 else
1418 return -EINVAL;
1419 }
1420
1421 if (status != -1) {
1422 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1423 (status == TPACPI_RFK_RADIO_ON) ?
1424 "enable" : "disable",
1425 tpacpi_rfkill_names[id]);
1426 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1427 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1428 }
1429
1430 return res;
1431}
1432
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001433/*************************************************************************
1434 * thinkpad-acpi driver attributes
1435 */
1436
1437/* interface_version --------------------------------------------------- */
1438static ssize_t tpacpi_driver_interface_version_show(
1439 struct device_driver *drv,
1440 char *buf)
1441{
1442 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1443}
1444
1445static DRIVER_ATTR(interface_version, S_IRUGO,
1446 tpacpi_driver_interface_version_show, NULL);
1447
1448/* debug_level --------------------------------------------------------- */
1449static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1450 char *buf)
1451{
1452 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1453}
1454
1455static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1456 const char *buf, size_t count)
1457{
1458 unsigned long t;
1459
1460 if (parse_strtoul(buf, 0xffff, &t))
1461 return -EINVAL;
1462
1463 dbg_level = t;
1464
1465 return count;
1466}
1467
1468static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1469 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1470
1471/* version ------------------------------------------------------------- */
1472static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1473 char *buf)
1474{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001475 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1476 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001477}
1478
1479static DRIVER_ATTR(version, S_IRUGO,
1480 tpacpi_driver_version_show, NULL);
1481
1482/* --------------------------------------------------------------------- */
1483
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001484#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1485
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001486/* wlsw_emulstate ------------------------------------------------------ */
1487static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1488 char *buf)
1489{
1490 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1491}
1492
1493static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1494 const char *buf, size_t count)
1495{
1496 unsigned long t;
1497
1498 if (parse_strtoul(buf, 1, &t))
1499 return -EINVAL;
1500
Johannes Berg19d337d2009-06-02 13:01:37 +02001501 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001502 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001503 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1504 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001505
1506 return count;
1507}
1508
1509static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1510 tpacpi_driver_wlsw_emulstate_show,
1511 tpacpi_driver_wlsw_emulstate_store);
1512
1513/* bluetooth_emulstate ------------------------------------------------- */
1514static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1515 struct device_driver *drv,
1516 char *buf)
1517{
1518 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1519}
1520
1521static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1522 struct device_driver *drv,
1523 const char *buf, size_t count)
1524{
1525 unsigned long t;
1526
1527 if (parse_strtoul(buf, 1, &t))
1528 return -EINVAL;
1529
1530 tpacpi_bluetooth_emulstate = !!t;
1531
1532 return count;
1533}
1534
1535static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1536 tpacpi_driver_bluetooth_emulstate_show,
1537 tpacpi_driver_bluetooth_emulstate_store);
1538
1539/* wwan_emulstate ------------------------------------------------- */
1540static ssize_t tpacpi_driver_wwan_emulstate_show(
1541 struct device_driver *drv,
1542 char *buf)
1543{
1544 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1545}
1546
1547static ssize_t tpacpi_driver_wwan_emulstate_store(
1548 struct device_driver *drv,
1549 const char *buf, size_t count)
1550{
1551 unsigned long t;
1552
1553 if (parse_strtoul(buf, 1, &t))
1554 return -EINVAL;
1555
1556 tpacpi_wwan_emulstate = !!t;
1557
1558 return count;
1559}
1560
1561static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1562 tpacpi_driver_wwan_emulstate_show,
1563 tpacpi_driver_wwan_emulstate_store);
1564
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001565/* uwb_emulstate ------------------------------------------------- */
1566static ssize_t tpacpi_driver_uwb_emulstate_show(
1567 struct device_driver *drv,
1568 char *buf)
1569{
1570 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1571}
1572
1573static ssize_t tpacpi_driver_uwb_emulstate_store(
1574 struct device_driver *drv,
1575 const char *buf, size_t count)
1576{
1577 unsigned long t;
1578
1579 if (parse_strtoul(buf, 1, &t))
1580 return -EINVAL;
1581
1582 tpacpi_uwb_emulstate = !!t;
1583
1584 return count;
1585}
1586
1587static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1588 tpacpi_driver_uwb_emulstate_show,
1589 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001590#endif
1591
1592/* --------------------------------------------------------------------- */
1593
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001594static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001595 &driver_attr_debug_level, &driver_attr_version,
1596 &driver_attr_interface_version,
1597};
1598
1599static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1600{
1601 int i, res;
1602
1603 i = 0;
1604 res = 0;
1605 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1606 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1607 i++;
1608 }
1609
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001610#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1611 if (!res && dbg_wlswemul)
1612 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1613 if (!res && dbg_bluetoothemul)
1614 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1615 if (!res && dbg_wwanemul)
1616 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001617 if (!res && dbg_uwbemul)
1618 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001619#endif
1620
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001621 return res;
1622}
1623
1624static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1625{
1626 int i;
1627
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001628 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001629 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001630
1631#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1632 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1633 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1634 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001635 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001636#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001637}
1638
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001639/*************************************************************************
1640 * Firmware Data
1641 */
1642
1643/*
1644 * Table of recommended minimum BIOS versions
1645 *
1646 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001647 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001648 * bugs and bad ACPI behaviour on older versions
1649 *
1650 * 2. BIOS or EC fw with known bugs that trigger on Linux
1651 *
1652 * 3. BIOS with known reduced functionality in older versions
1653 *
1654 * We recommend the latest BIOS and EC version.
1655 * We only support the latest BIOS and EC fw version as a rule.
1656 *
1657 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1658 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001659 *
1660 * WARNING: we use this table also to detect that the machine is
1661 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001662 */
1663
1664#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1665 { .vendor = (__v), \
1666 .bios = TPID(__id1, __id2), \
1667 .ec = TPACPI_MATCH_ANY, \
1668 .quirks = TPACPI_MATCH_ANY << 16 \
1669 | (__bv1) << 8 | (__bv2) }
1670
1671#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001672 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001673 { .vendor = (__v), \
1674 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001675 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001676 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1677 | (__bv1) << 8 | (__bv2) }
1678
1679#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1680 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1681
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001682/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001683#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1684 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001685 __bv1, __bv2, TPID(__id1, __id2), \
1686 __ev1, __ev2), \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1688 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1689 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001690
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001691/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001692#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1693 __eid1, __eid2, __ev1, __ev2) \
1694 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001695 __bv1, __bv2, TPID(__eid1, __eid2), \
1696 __ev1, __ev2), \
1697 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1698 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1699 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001700
1701#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1702 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1703
1704#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1705 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001706 __bv1, __bv2, TPID(__id1, __id2), \
1707 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001708
1709#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1710 __eid1, __eid2, __ev1, __ev2) \
1711 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001712 __bv1, __bv2, TPID(__eid1, __eid2), \
1713 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001714
1715static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1716 /* Numeric models ------------------ */
1717 /* FW MODEL BIOS VERS */
1718 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1719 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1720 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1721 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1722 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1723 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1724 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1725 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1726 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1727
1728 /* A-series ------------------------- */
1729 /* FW MODEL BIOS VERS EC VERS */
1730 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1731 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1732 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1733 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1734 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1735 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1736 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1737 TPV_QI0('1', '3', '2', '0'), /* A22m */
1738 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1739 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1740 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1741
1742 /* G-series ------------------------- */
1743 /* FW MODEL BIOS VERS */
1744 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1745 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1746
1747 /* R-series, T-series --------------- */
1748 /* FW MODEL BIOS VERS EC VERS */
1749 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1750 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1751 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1752 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1753 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1754 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1755 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1756 T40/p, T41/p, T42/p (1) */
1757 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1758 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1759 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1760 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1761
1762 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1763 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1764 TPV_QI0('1', '6', '3', '2'), /* T22 */
1765 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1766 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1767 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1768
1769 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1770 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001771 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001772
1773 /* BIOS FW BIOS VERS EC FW EC VERS */
1774 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1775 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1776
1777 /* X-series ------------------------- */
1778 /* FW MODEL BIOS VERS EC VERS */
1779 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1780 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1781 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1782 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1783 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1784 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1785 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1786
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001787 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1788 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001789
1790 /* (0) - older versions lack DMI EC fw string and functionality */
1791 /* (1) - older versions known to lack functionality */
1792};
1793
1794#undef TPV_QL1
1795#undef TPV_QL0
1796#undef TPV_QI2
1797#undef TPV_QI1
1798#undef TPV_QI0
1799#undef TPV_Q_X
1800#undef TPV_Q
1801
1802static void __init tpacpi_check_outdated_fw(void)
1803{
1804 unsigned long fwvers;
1805 u16 ec_version, bios_version;
1806
1807 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1808 ARRAY_SIZE(tpacpi_bios_version_qtable));
1809
1810 if (!fwvers)
1811 return;
1812
1813 bios_version = fwvers & 0xffffU;
1814 ec_version = (fwvers >> 16) & 0xffffU;
1815
1816 /* note that unknown versions are set to 0x0000 and we use that */
1817 if ((bios_version > thinkpad_id.bios_release) ||
1818 (ec_version > thinkpad_id.ec_release &&
1819 ec_version != TPACPI_MATCH_ANY)) {
1820 /*
1821 * The changelogs would let us track down the exact
1822 * reason, but it is just too much of a pain to track
1823 * it. We only list BIOSes that are either really
1824 * broken, or really stable to begin with, so it is
1825 * best if the user upgrades the firmware anyway.
1826 */
1827 printk(TPACPI_WARN
1828 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1829 printk(TPACPI_WARN
1830 "WARNING: This firmware may be missing critical bug "
1831 "fixes and/or important features\n");
1832 }
1833}
1834
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001835static bool __init tpacpi_is_fw_known(void)
1836{
1837 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1838 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1839}
1840
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001841/****************************************************************************
1842 ****************************************************************************
1843 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001844 * Subdrivers
1845 *
1846 ****************************************************************************
1847 ****************************************************************************/
1848
1849/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001850 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001851 */
1852
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001853static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001855 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1856 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001860static struct ibm_struct thinkpad_acpi_driver_data = {
1861 .name = "driver",
1862 .read = thinkpad_acpi_driver_read,
1863};
1864
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001865/*************************************************************************
1866 * Hotkey subdriver
1867 */
1868
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001869/*
1870 * ThinkPad firmware event model
1871 *
1872 * The ThinkPad firmware has two main event interfaces: normal ACPI
1873 * notifications (which follow the ACPI standard), and a private event
1874 * interface.
1875 *
1876 * The private event interface also issues events for the hotkeys. As
1877 * the driver gained features, the event handling code ended up being
1878 * built around the hotkey subdriver. This will need to be refactored
1879 * to a more formal event API eventually.
1880 *
1881 * Some "hotkeys" are actually supposed to be used as event reports,
1882 * such as "brightness has changed", "volume has changed", depending on
1883 * the ThinkPad model and how the firmware is operating.
1884 *
1885 * Unlike other classes, hotkey-class events have mask/unmask control on
1886 * non-ancient firmware. However, how it behaves changes a lot with the
1887 * firmware model and version.
1888 */
1889
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001890enum { /* hot key scan codes (derived from ACPI DSDT) */
1891 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1892 TP_ACPI_HOTKEYSCAN_FNF2,
1893 TP_ACPI_HOTKEYSCAN_FNF3,
1894 TP_ACPI_HOTKEYSCAN_FNF4,
1895 TP_ACPI_HOTKEYSCAN_FNF5,
1896 TP_ACPI_HOTKEYSCAN_FNF6,
1897 TP_ACPI_HOTKEYSCAN_FNF7,
1898 TP_ACPI_HOTKEYSCAN_FNF8,
1899 TP_ACPI_HOTKEYSCAN_FNF9,
1900 TP_ACPI_HOTKEYSCAN_FNF10,
1901 TP_ACPI_HOTKEYSCAN_FNF11,
1902 TP_ACPI_HOTKEYSCAN_FNF12,
1903 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1904 TP_ACPI_HOTKEYSCAN_FNINSERT,
1905 TP_ACPI_HOTKEYSCAN_FNDELETE,
1906 TP_ACPI_HOTKEYSCAN_FNHOME,
1907 TP_ACPI_HOTKEYSCAN_FNEND,
1908 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1909 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1910 TP_ACPI_HOTKEYSCAN_FNSPACE,
1911 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1912 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1913 TP_ACPI_HOTKEYSCAN_MUTE,
1914 TP_ACPI_HOTKEYSCAN_THINKPAD,
1915};
1916
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001917enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001918 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1919 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1920};
1921
1922enum { /* Positions of some of the keys in hotkey masks */
1923 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1924 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1925 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1926 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1927 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1928 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1929 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1930 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1931 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1932 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1933 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1934};
1935
1936enum { /* NVRAM to ACPI HKEY group map */
1937 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1938 TP_ACPI_HKEY_ZOOM_MASK |
1939 TP_ACPI_HKEY_DISPSWTCH_MASK |
1940 TP_ACPI_HKEY_HIBERNATE_MASK,
1941 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1942 TP_ACPI_HKEY_BRGHTDWN_MASK,
1943 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1944 TP_ACPI_HKEY_VOLDWN_MASK |
1945 TP_ACPI_HKEY_MUTE_MASK,
1946};
1947
1948#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1949struct tp_nvram_state {
1950 u16 thinkpad_toggle:1;
1951 u16 zoom_toggle:1;
1952 u16 display_toggle:1;
1953 u16 thinklight_toggle:1;
1954 u16 hibernate_toggle:1;
1955 u16 displayexp_toggle:1;
1956 u16 display_state:1;
1957 u16 brightness_toggle:1;
1958 u16 volume_toggle:1;
1959 u16 mute:1;
1960
1961 u8 brightness_level;
1962 u8 volume_level;
1963};
1964
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001965/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001966static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001967
1968/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001969static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001970
1971/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001972 * Acquire mutex to write poller control variables as an
1973 * atomic block.
1974 *
1975 * Increment hotkey_config_change when changing them if you
1976 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001977 *
1978 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1979 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001980static struct mutex hotkey_thread_data_mutex;
1981static unsigned int hotkey_config_change;
1982
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001983/*
1984 * hotkey poller control variables
1985 *
1986 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001987 *
1988 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1989 * should be used only when the changes need to be taken as
1990 * a block, OR when one needs to force the kthread to forget
1991 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001992 */
1993static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1994static unsigned int hotkey_poll_freq = 10; /* Hz */
1995
1996#define HOTKEY_CONFIG_CRITICAL_START \
1997 do { \
1998 mutex_lock(&hotkey_thread_data_mutex); \
1999 hotkey_config_change++; \
2000 } while (0);
2001#define HOTKEY_CONFIG_CRITICAL_END \
2002 mutex_unlock(&hotkey_thread_data_mutex);
2003
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002004#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2005
2006#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002007#define HOTKEY_CONFIG_CRITICAL_START
2008#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002009
2010#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2011
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002012static struct mutex hotkey_mutex;
2013
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002014static enum { /* Reasons for waking up */
2015 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2016 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2017 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2018} hotkey_wakeup_reason;
2019
2020static int hotkey_autosleep_ack;
2021
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002022static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2023static u32 hotkey_all_mask; /* all events supported in fw */
2024static u32 hotkey_reserved_mask; /* events better left disabled */
2025static u32 hotkey_driver_mask; /* events needed by the driver */
2026static u32 hotkey_user_mask; /* events visible to userspace */
2027static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002028
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02002029static unsigned int hotkey_report_mode;
2030
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002031static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002032
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03002033static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002034
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002035static void tpacpi_driver_event(const unsigned int hkey_event);
2036static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002037static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002038
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002039/* HKEY.MHKG() return bits */
2040#define TP_HOTKEY_TABLET_MASK (1 << 3)
2041
Johannes Berg19d337d2009-06-02 13:01:37 +02002042static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002043{
Johannes Berg19d337d2009-06-02 13:01:37 +02002044 int status;
2045
2046 if (!tp_features.hotkey_wlsw)
2047 return -ENODEV;
2048
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002049#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002050 if (dbg_wlswemul)
2051 return (tpacpi_wlsw_emulstate) ?
2052 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002053#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002054
2055 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002056 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002057
2058 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002059}
2060
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002061static int hotkey_get_tablet_mode(int *status)
2062{
2063 int s;
2064
2065 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2066 return -EIO;
2067
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002068 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2069 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002070}
2071
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002072/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002073 * Reads current event mask from firmware, and updates
2074 * hotkey_acpi_mask accordingly. Also resets any bits
2075 * from hotkey_user_mask that are unavailable to be
2076 * delivered (shadow requirement of the userspace ABI).
2077 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002078 * Call with hotkey_mutex held
2079 */
2080static int hotkey_mask_get(void)
2081{
2082 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002083 u32 m = 0;
2084
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002085 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002086 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002087
2088 hotkey_acpi_mask = m;
2089 } else {
2090 /* no mask support doesn't mean no event support... */
2091 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002092 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002093
2094 /* sync userspace-visible mask */
2095 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002096
2097 return 0;
2098}
2099
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002100void static hotkey_mask_warn_incomplete_mask(void)
2101{
2102 /* log only what the user can fix... */
2103 const u32 wantedmask = hotkey_driver_mask &
2104 ~(hotkey_acpi_mask | hotkey_source_mask) &
2105 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2106
2107 if (wantedmask)
2108 printk(TPACPI_NOTICE
2109 "required events 0x%08x not enabled!\n",
2110 wantedmask);
2111}
2112
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002113/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002114 * Set the firmware mask when supported
2115 *
2116 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2117 *
2118 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2119 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002120 * Call with hotkey_mutex held
2121 */
2122static int hotkey_mask_set(u32 mask)
2123{
2124 int i;
2125 int rc = 0;
2126
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002127 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002128
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002129 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002130 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002131 if (!acpi_evalf(hkey_handle,
2132 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002133 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002134 rc = -EIO;
2135 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002136 }
2137 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002138 }
2139
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002140 /*
2141 * We *must* make an inconditional call to hotkey_mask_get to
2142 * refresh hotkey_acpi_mask and update hotkey_user_mask
2143 *
2144 * Take the opportunity to also log when we cannot _enable_
2145 * a given event.
2146 */
2147 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2148 printk(TPACPI_NOTICE
2149 "asked for hotkey mask 0x%08x, but "
2150 "firmware forced it to 0x%08x\n",
2151 fwmask, hotkey_acpi_mask);
2152 }
2153
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002154 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2155 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002156
2157 return rc;
2158}
2159
2160/*
2161 * Sets hotkey_user_mask and tries to set the firmware mask
2162 *
2163 * Call with hotkey_mutex held
2164 */
2165static int hotkey_user_mask_set(const u32 mask)
2166{
2167 int rc;
2168
2169 /* Give people a chance to notice they are doing something that
2170 * is bound to go boom on their users sooner or later */
2171 if (!tp_warned.hotkey_mask_ff &&
2172 (mask == 0xffff || mask == 0xffffff ||
2173 mask == 0xffffffff)) {
2174 tp_warned.hotkey_mask_ff = 1;
2175 printk(TPACPI_NOTICE
2176 "setting the hotkey mask to 0x%08x is likely "
2177 "not the best way to go about it\n", mask);
2178 printk(TPACPI_NOTICE
2179 "please consider using the driver defaults, "
2180 "and refer to up-to-date thinkpad-acpi "
2181 "documentation\n");
2182 }
2183
2184 /* Try to enable what the user asked for, plus whatever we need.
2185 * this syncs everything but won't enable bits in hotkey_user_mask */
2186 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2187
2188 /* Enable the available bits in hotkey_user_mask */
2189 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2190
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002191 return rc;
2192}
2193
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002194/*
2195 * Sets the driver hotkey mask.
2196 *
2197 * Can be called even if the hotkey subdriver is inactive
2198 */
2199static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2200{
2201 int rc;
2202
2203 /* Do the right thing if hotkey_init has not been called yet */
2204 if (!tp_features.hotkey) {
2205 hotkey_driver_mask = mask;
2206 return 0;
2207 }
2208
2209 mutex_lock(&hotkey_mutex);
2210
2211 HOTKEY_CONFIG_CRITICAL_START
2212 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002213#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002214 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002215#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002216 HOTKEY_CONFIG_CRITICAL_END
2217
2218 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2219 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002220 hotkey_poll_setup(true);
2221
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002222 mutex_unlock(&hotkey_mutex);
2223
2224 return rc;
2225}
2226
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002227static int hotkey_status_get(int *status)
2228{
2229 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2230 return -EIO;
2231
2232 return 0;
2233}
2234
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002235static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002236{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002237 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002238 return -EIO;
2239
2240 return 0;
2241}
2242
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002243static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002244{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002245 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002246
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002247 if (tp_features.hotkey_tablet &&
2248 !hotkey_get_tablet_mode(&state)) {
2249 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002250
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002251 input_report_switch(tpacpi_inputdev,
2252 SW_TABLET_MODE, !!state);
2253 input_sync(tpacpi_inputdev);
2254
2255 mutex_unlock(&tpacpi_inputdev_send_mutex);
2256 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002257}
2258
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002259/* Do NOT call without validating scancode first */
2260static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002261{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002262 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002263
2264 if (keycode != KEY_RESERVED) {
2265 mutex_lock(&tpacpi_inputdev_send_mutex);
2266
2267 input_report_key(tpacpi_inputdev, keycode, 1);
2268 if (keycode == KEY_UNKNOWN)
2269 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2270 scancode);
2271 input_sync(tpacpi_inputdev);
2272
2273 input_report_key(tpacpi_inputdev, keycode, 0);
2274 if (keycode == KEY_UNKNOWN)
2275 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2276 scancode);
2277 input_sync(tpacpi_inputdev);
2278
2279 mutex_unlock(&tpacpi_inputdev_send_mutex);
2280 }
2281}
2282
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002283/* Do NOT call without validating scancode first */
2284static void tpacpi_input_send_key_masked(const unsigned int scancode)
2285{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002286 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002287 if (hotkey_user_mask & (1 << scancode))
2288 tpacpi_input_send_key(scancode);
2289}
2290
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002291#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2292static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2293
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002294/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002295static void tpacpi_hotkey_send_key(unsigned int scancode)
2296{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002297 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002298 if (hotkey_report_mode < 2) {
2299 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03002300 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002301 }
2302}
2303
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002304static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002305{
2306 u8 d;
2307
2308 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2309 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2310 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2311 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2312 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2313 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2314 }
2315 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2316 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2317 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2318 }
2319 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2320 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2321 n->displayexp_toggle =
2322 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2323 }
2324 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2325 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2326 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2327 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2328 n->brightness_toggle =
2329 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2330 }
2331 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2332 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2333 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2334 >> TP_NVRAM_POS_LEVEL_VOLUME;
2335 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2336 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2337 }
2338}
2339
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002340static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2341 struct tp_nvram_state *newn,
2342 const u32 event_mask)
2343{
2344
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002345#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002346 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002347 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002348 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002349 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002350 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002351
2352#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002353 do { \
2354 if (event_mask & (1 << __scancode)) \
2355 tpacpi_hotkey_send_key(__scancode); \
2356 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002357
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002358 void issue_volchange(const unsigned int oldvol,
2359 const unsigned int newvol)
2360 {
2361 unsigned int i = oldvol;
2362
2363 while (i > newvol) {
2364 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2365 i--;
2366 }
2367 while (i < newvol) {
2368 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2369 i++;
2370 }
2371 }
2372
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002373 void issue_brightnesschange(const unsigned int oldbrt,
2374 const unsigned int newbrt)
2375 {
2376 unsigned int i = oldbrt;
2377
2378 while (i > newbrt) {
2379 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2380 i--;
2381 }
2382 while (i < newbrt) {
2383 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2384 i++;
2385 }
2386 }
2387
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002388 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2389 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2390 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2391 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2392
2393 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2394
2395 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2396
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002397 /*
2398 * Handle volume
2399 *
2400 * This code is supposed to duplicate the IBM firmware behaviour:
2401 * - Pressing MUTE issues mute hotkey message, even when already mute
2402 * - Pressing Volume up/down issues volume up/down hotkey messages,
2403 * even when already at maximum or minumum volume
2404 * - The act of unmuting issues volume up/down notification,
2405 * depending which key was used to unmute
2406 *
2407 * We are constrained to what the NVRAM can tell us, which is not much
2408 * and certainly not enough if more than one volume hotkey was pressed
2409 * since the last poll cycle.
2410 *
2411 * Just to make our life interesting, some newer Lenovo ThinkPads have
2412 * bugs in the BIOS and may fail to update volume_toggle properly.
2413 */
2414 if (newn->mute) {
2415 /* muted */
2416 if (!oldn->mute ||
2417 oldn->volume_toggle != newn->volume_toggle ||
2418 oldn->volume_level != newn->volume_level) {
2419 /* recently muted, or repeated mute keypress, or
2420 * multiple presses ending in mute */
2421 issue_volchange(oldn->volume_level, newn->volume_level);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002422 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2423 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002424 } else {
2425 /* unmute */
2426 if (oldn->mute) {
2427 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002428 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002429 }
2430 if (oldn->volume_level != newn->volume_level) {
2431 issue_volchange(oldn->volume_level, newn->volume_level);
2432 } else if (oldn->volume_toggle != newn->volume_toggle) {
2433 /* repeated vol up/down keypress at end of scale ? */
2434 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002435 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002436 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2437 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002438 }
2439 }
2440
2441 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002442 if (oldn->brightness_level != newn->brightness_level) {
2443 issue_brightnesschange(oldn->brightness_level,
2444 newn->brightness_level);
2445 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2446 /* repeated key presses that didn't change state */
2447 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002448 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002449 else if (newn->brightness_level >= bright_maxlvl
2450 && !tp_features.bright_unkfw)
2451 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002452 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002453
2454#undef TPACPI_COMPARE_KEY
2455#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002456}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002457
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002458/*
2459 * Polling driver
2460 *
2461 * We track all events in hotkey_source_mask all the time, since
2462 * most of them are edge-based. We only issue those requested by
2463 * hotkey_user_mask or hotkey_driver_mask, though.
2464 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002465static int hotkey_kthread(void *data)
2466{
2467 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002468 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002469 unsigned int si, so;
2470 unsigned long t;
2471 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002472 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002473
2474 mutex_lock(&hotkey_thread_mutex);
2475
2476 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2477 goto exit;
2478
2479 set_freezable();
2480
2481 so = 0;
2482 si = 1;
2483 t = 0;
2484
2485 /* Initial state for compares */
2486 mutex_lock(&hotkey_thread_data_mutex);
2487 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002488 poll_mask = hotkey_source_mask;
2489 event_mask = hotkey_source_mask &
2490 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002491 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002492 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002493 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002494
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002495 while (!kthread_should_stop()) {
2496 if (t == 0) {
2497 if (likely(poll_freq))
2498 t = 1000/poll_freq;
2499 else
2500 t = 100; /* should never happen... */
2501 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002502 t = msleep_interruptible(t);
2503 if (unlikely(kthread_should_stop()))
2504 break;
2505 must_reset = try_to_freeze();
2506 if (t > 0 && !must_reset)
2507 continue;
2508
2509 mutex_lock(&hotkey_thread_data_mutex);
2510 if (must_reset || hotkey_config_change != change_detector) {
2511 /* forget old state on thaw or config change */
2512 si = so;
2513 t = 0;
2514 change_detector = hotkey_config_change;
2515 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002516 poll_mask = hotkey_source_mask;
2517 event_mask = hotkey_source_mask &
2518 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002519 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002520 mutex_unlock(&hotkey_thread_data_mutex);
2521
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002522 if (likely(poll_mask)) {
2523 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002524 if (likely(si != so)) {
2525 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002526 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002527 }
2528 }
2529
2530 so = si;
2531 si ^= 1;
2532 }
2533
2534exit:
2535 mutex_unlock(&hotkey_thread_mutex);
2536 return 0;
2537}
2538
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002539/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002540static void hotkey_poll_stop_sync(void)
2541{
2542 if (tpacpi_hotkey_task) {
2543 if (frozen(tpacpi_hotkey_task) ||
2544 freezing(tpacpi_hotkey_task))
2545 thaw_process(tpacpi_hotkey_task);
2546
2547 kthread_stop(tpacpi_hotkey_task);
2548 tpacpi_hotkey_task = NULL;
2549 mutex_lock(&hotkey_thread_mutex);
2550 /* at this point, the thread did exit */
2551 mutex_unlock(&hotkey_thread_mutex);
2552 }
2553}
2554
2555/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002556static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002557{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002558 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2559 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002560
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002561 if (hotkey_poll_freq > 0 &&
2562 (poll_driver_mask ||
2563 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002564 if (!tpacpi_hotkey_task) {
2565 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002566 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002567 if (IS_ERR(tpacpi_hotkey_task)) {
2568 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002569 printk(TPACPI_ERR
2570 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002571 "for hotkey polling\n");
2572 }
2573 }
2574 } else {
2575 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002576 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002577 hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002578 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002579 "hot keys 0x%08x and/or events 0x%08x "
2580 "require polling, which is currently "
2581 "disabled\n",
2582 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002583 }
2584 }
2585}
2586
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002587static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002588{
2589 mutex_lock(&hotkey_mutex);
2590 hotkey_poll_setup(may_warn);
2591 mutex_unlock(&hotkey_mutex);
2592}
2593
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002594/* call with hotkey_mutex held */
2595static void hotkey_poll_set_freq(unsigned int freq)
2596{
2597 if (!freq)
2598 hotkey_poll_stop_sync();
2599
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002600 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002601}
2602
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002603#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2604
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002605static void hotkey_poll_setup(const bool __unused)
2606{
2607}
2608
2609static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002610{
2611}
2612
2613#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2614
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002615static int hotkey_inputdev_open(struct input_dev *dev)
2616{
2617 switch (tpacpi_lifecycle) {
2618 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002619 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002620 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002621 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002622 case TPACPI_LIFE_EXITING:
2623 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002624 }
2625
2626 /* Should only happen if tpacpi_lifecycle is corrupt */
2627 BUG();
2628 return -EBUSY;
2629}
2630
2631static void hotkey_inputdev_close(struct input_dev *dev)
2632{
2633 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002634 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002635 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002636 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002637}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002638
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002639/* sysfs hotkey enable ------------------------------------------------- */
2640static ssize_t hotkey_enable_show(struct device *dev,
2641 struct device_attribute *attr,
2642 char *buf)
2643{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002644 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002645
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002646 printk_deprecated_attribute("hotkey_enable",
2647 "Hotkey reporting is always enabled");
2648
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002649 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002650 if (res)
2651 return res;
2652
2653 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2654}
2655
2656static ssize_t hotkey_enable_store(struct device *dev,
2657 struct device_attribute *attr,
2658 const char *buf, size_t count)
2659{
2660 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002661
2662 printk_deprecated_attribute("hotkey_enable",
2663 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002664
2665 if (parse_strtoul(buf, 1, &t))
2666 return -EINVAL;
2667
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002668 if (t == 0)
2669 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002670
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002671 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002672}
2673
2674static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002675 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002676 hotkey_enable_show, hotkey_enable_store);
2677
2678/* sysfs hotkey mask --------------------------------------------------- */
2679static ssize_t hotkey_mask_show(struct device *dev,
2680 struct device_attribute *attr,
2681 char *buf)
2682{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002683 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002684}
2685
2686static ssize_t hotkey_mask_store(struct device *dev,
2687 struct device_attribute *attr,
2688 const char *buf, size_t count)
2689{
2690 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002691 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002692
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002693 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002694 return -EINVAL;
2695
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002696 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002697 return -ERESTARTSYS;
2698
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002699 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002700
2701#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002702 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002703#endif
2704
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002705 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002706
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002707 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2708
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002709 return (res) ? res : count;
2710}
2711
2712static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002713 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002714 hotkey_mask_show, hotkey_mask_store);
2715
2716/* sysfs hotkey bios_enabled ------------------------------------------- */
2717static ssize_t hotkey_bios_enabled_show(struct device *dev,
2718 struct device_attribute *attr,
2719 char *buf)
2720{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002721 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002722}
2723
2724static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002725 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002726
2727/* sysfs hotkey bios_mask ---------------------------------------------- */
2728static ssize_t hotkey_bios_mask_show(struct device *dev,
2729 struct device_attribute *attr,
2730 char *buf)
2731{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002732 printk_deprecated_attribute("hotkey_bios_mask",
2733 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002734 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002735}
2736
2737static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002738 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002739
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002740/* sysfs hotkey all_mask ----------------------------------------------- */
2741static ssize_t hotkey_all_mask_show(struct device *dev,
2742 struct device_attribute *attr,
2743 char *buf)
2744{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002745 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2746 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002747}
2748
2749static struct device_attribute dev_attr_hotkey_all_mask =
2750 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2751
2752/* sysfs hotkey recommended_mask --------------------------------------- */
2753static ssize_t hotkey_recommended_mask_show(struct device *dev,
2754 struct device_attribute *attr,
2755 char *buf)
2756{
2757 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002758 (hotkey_all_mask | hotkey_source_mask)
2759 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002760}
2761
2762static struct device_attribute dev_attr_hotkey_recommended_mask =
2763 __ATTR(hotkey_recommended_mask, S_IRUGO,
2764 hotkey_recommended_mask_show, NULL);
2765
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002766#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2767
2768/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2769static ssize_t hotkey_source_mask_show(struct device *dev,
2770 struct device_attribute *attr,
2771 char *buf)
2772{
2773 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2774}
2775
2776static ssize_t hotkey_source_mask_store(struct device *dev,
2777 struct device_attribute *attr,
2778 const char *buf, size_t count)
2779{
2780 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002781 u32 r_ev;
2782 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002783
2784 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2785 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2786 return -EINVAL;
2787
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002788 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002789 return -ERESTARTSYS;
2790
2791 HOTKEY_CONFIG_CRITICAL_START
2792 hotkey_source_mask = t;
2793 HOTKEY_CONFIG_CRITICAL_END
2794
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002795 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2796 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002797 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002798
2799 /* check if events needed by the driver got disabled */
2800 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2801 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002802
2803 mutex_unlock(&hotkey_mutex);
2804
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002805 if (rc < 0)
2806 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2807 "firmware event mask!\n");
2808
2809 if (r_ev)
2810 printk(TPACPI_NOTICE "hotkey_source_mask: "
2811 "some important events were disabled: "
2812 "0x%04x\n", r_ev);
2813
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002814 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2815
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002816 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002817}
2818
2819static struct device_attribute dev_attr_hotkey_source_mask =
2820 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2821 hotkey_source_mask_show, hotkey_source_mask_store);
2822
2823/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2824static ssize_t hotkey_poll_freq_show(struct device *dev,
2825 struct device_attribute *attr,
2826 char *buf)
2827{
2828 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2829}
2830
2831static ssize_t hotkey_poll_freq_store(struct device *dev,
2832 struct device_attribute *attr,
2833 const char *buf, size_t count)
2834{
2835 unsigned long t;
2836
2837 if (parse_strtoul(buf, 25, &t))
2838 return -EINVAL;
2839
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002840 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002841 return -ERESTARTSYS;
2842
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002843 hotkey_poll_set_freq(t);
2844 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002845
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002846 mutex_unlock(&hotkey_mutex);
2847
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002848 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2849
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002850 return count;
2851}
2852
2853static struct device_attribute dev_attr_hotkey_poll_freq =
2854 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2855 hotkey_poll_freq_show, hotkey_poll_freq_store);
2856
2857#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2858
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002859/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002860static ssize_t hotkey_radio_sw_show(struct device *dev,
2861 struct device_attribute *attr,
2862 char *buf)
2863{
Johannes Berg19d337d2009-06-02 13:01:37 +02002864 int res;
2865 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002866 if (res < 0)
2867 return res;
2868
Johannes Berg19d337d2009-06-02 13:01:37 +02002869 /* Opportunistic update */
2870 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2871
2872 return snprintf(buf, PAGE_SIZE, "%d\n",
2873 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002874}
2875
2876static struct device_attribute dev_attr_hotkey_radio_sw =
2877 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2878
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002879static void hotkey_radio_sw_notify_change(void)
2880{
2881 if (tp_features.hotkey_wlsw)
2882 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2883 "hotkey_radio_sw");
2884}
2885
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002886/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2887static ssize_t hotkey_tablet_mode_show(struct device *dev,
2888 struct device_attribute *attr,
2889 char *buf)
2890{
2891 int res, s;
2892 res = hotkey_get_tablet_mode(&s);
2893 if (res < 0)
2894 return res;
2895
2896 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2897}
2898
2899static struct device_attribute dev_attr_hotkey_tablet_mode =
2900 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2901
2902static void hotkey_tablet_mode_notify_change(void)
2903{
2904 if (tp_features.hotkey_tablet)
2905 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2906 "hotkey_tablet_mode");
2907}
2908
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002909/* sysfs hotkey report_mode -------------------------------------------- */
2910static ssize_t hotkey_report_mode_show(struct device *dev,
2911 struct device_attribute *attr,
2912 char *buf)
2913{
2914 return snprintf(buf, PAGE_SIZE, "%d\n",
2915 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2916}
2917
2918static struct device_attribute dev_attr_hotkey_report_mode =
2919 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2920
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002921/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002922static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2923 struct device_attribute *attr,
2924 char *buf)
2925{
2926 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2927}
2928
2929static struct device_attribute dev_attr_hotkey_wakeup_reason =
2930 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2931
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002932static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002933{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002934 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2935 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002936}
2937
2938/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002939static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2940 struct device_attribute *attr,
2941 char *buf)
2942{
2943 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2944}
2945
2946static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2947 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2948 hotkey_wakeup_hotunplug_complete_show, NULL);
2949
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002950static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002951{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002952 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2953 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002954}
2955
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002956/* --------------------------------------------------------------------- */
2957
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002958static struct attribute *hotkey_attributes[] __initdata = {
2959 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002960 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002961 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002962 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002963 &dev_attr_hotkey_wakeup_reason.attr,
2964 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002965 &dev_attr_hotkey_mask.attr,
2966 &dev_attr_hotkey_all_mask.attr,
2967 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002968#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002969 &dev_attr_hotkey_source_mask.attr,
2970 &dev_attr_hotkey_poll_freq.attr,
2971#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002972};
2973
Johannes Berg19d337d2009-06-02 13:01:37 +02002974/*
2975 * Sync both the hw and sw blocking state of all switches
2976 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002977static void tpacpi_send_radiosw_update(void)
2978{
2979 int wlsw;
2980
Johannes Berg19d337d2009-06-02 13:01:37 +02002981 /*
2982 * We must sync all rfkill controllers *before* issuing any
2983 * rfkill input events, or we will race the rfkill core input
2984 * handler.
2985 *
2986 * tpacpi_inputdev_send_mutex works as a syncronization point
2987 * for the above.
2988 *
2989 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2990 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002991
Johannes Berg19d337d2009-06-02 13:01:37 +02002992 wlsw = hotkey_get_wlsw();
2993
2994 /* Sync hw blocking state first if it is hw-blocked */
2995 if (wlsw == TPACPI_RFK_RADIO_OFF)
2996 tpacpi_rfk_update_hwblock_state(true);
2997
2998 /* Sync sw blocking state */
2999 tpacpi_rfk_update_swstate_all();
3000
3001 /* Sync hw blocking state last if it is hw-unblocked */
3002 if (wlsw == TPACPI_RFK_RADIO_ON)
3003 tpacpi_rfk_update_hwblock_state(false);
3004
3005 /* Issue rfkill input event for WLSW switch */
3006 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003007 mutex_lock(&tpacpi_inputdev_send_mutex);
3008
3009 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003010 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003011 input_sync(tpacpi_inputdev);
3012
3013 mutex_unlock(&tpacpi_inputdev_send_mutex);
3014 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003015
3016 /*
3017 * this can be unconditional, as we will poll state again
3018 * if userspace uses the notify to read data
3019 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003020 hotkey_radio_sw_notify_change();
3021}
3022
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003023static void hotkey_exit(void)
3024{
3025#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003026 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003027 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003028 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003029#endif
3030
3031 if (hotkey_dev_attributes)
3032 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3033
3034 kfree(hotkey_keycode_map);
3035
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003036 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003037 "restoring original HKEY status and mask\n");
3038 /* yes, there is a bitwise or below, we want the
3039 * functions to be called even if one of them fail */
3040 if (((tp_features.hotkey_mask &&
3041 hotkey_mask_set(hotkey_orig_mask)) |
3042 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003043 printk(TPACPI_ERR
3044 "failed to restore hot key mask "
3045 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003046}
3047
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003048static void __init hotkey_unmap(const unsigned int scancode)
3049{
3050 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3051 clear_bit(hotkey_keycode_map[scancode],
3052 tpacpi_inputdev->keybit);
3053 hotkey_keycode_map[scancode] = KEY_RESERVED;
3054 }
3055}
3056
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003057/*
3058 * HKEY quirks:
3059 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3060 */
3061
3062#define TPACPI_HK_Q_INIMASK 0x0001
3063
3064static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3065 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3066 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3067 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3068 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3069 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3070 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3071 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3072 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3073 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3074 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3075 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3076 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3077 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3078 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3079 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3080 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3081 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3082 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3083 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3084};
3085
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003086static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003087{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003088 /* Requirements for changing the default keymaps:
3089 *
3090 * 1. Many of the keys are mapped to KEY_RESERVED for very
3091 * good reasons. Do not change them unless you have deep
3092 * knowledge on the IBM and Lenovo ThinkPad firmware for
3093 * the various ThinkPad models. The driver behaves
3094 * differently for KEY_RESERVED: such keys have their
3095 * hot key mask *unset* in mask_recommended, and also
3096 * in the initial hot key mask programmed into the
3097 * firmware at driver load time, which means the firm-
3098 * ware may react very differently if you change them to
3099 * something else;
3100 *
3101 * 2. You must be subscribed to the linux-thinkpad and
3102 * ibm-acpi-devel mailing lists, and you should read the
3103 * list archives since 2007 if you want to change the
3104 * keymaps. This requirement exists so that you will
3105 * know the past history of problems with the thinkpad-
3106 * acpi driver keymaps, and also that you will be
3107 * listening to any bug reports;
3108 *
3109 * 3. Do not send thinkpad-acpi specific patches directly to
3110 * for merging, *ever*. Send them to the linux-acpi
3111 * mailinglist for comments. Merging is to be done only
3112 * through acpi-test and the ACPI maintainer.
3113 *
3114 * If the above is too much to ask, don't change the keymap.
3115 * Ask the thinkpad-acpi maintainer to do it, instead.
3116 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003117 static u16 ibm_keycode_map[] __initdata = {
3118 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3119 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3120 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3121 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003122
3123 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003124 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3125 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3126 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003127
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003128 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003129 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003130 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003131
3132 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003133 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003134
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003135 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3136 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003137
3138 /* Volume: firmware always react to it and reprograms
3139 * the built-in *extra* mixer. Never map it to control
3140 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003141 KEY_RESERVED, /* 0x14: VOLUME UP */
3142 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3143 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003144
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003145 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003146
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003147 /* (assignments unknown, please report if found) */
3148 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3149 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3150 };
3151 static u16 lenovo_keycode_map[] __initdata = {
3152 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3153 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3154 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3155 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003156
3157 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003158 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3159 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3160 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003161
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003162 /* These should be enabled --only-- when ACPI video
3163 * is disabled (i.e. in "vendor" mode), and are handled
3164 * in a special way by the init code */
3165 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3166 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003167
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003168 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003169
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003170 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3171 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003172
3173 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3174 * react to it and reprograms the built-in *extra* mixer.
3175 * Never map it to control another mixer by default.
3176 *
3177 * T60?, T61, R60?, R61: firmware and EC tries to send
3178 * these over the regular keyboard, so these are no-ops,
3179 * but there are still weird bugs re. MUTE, so do not
3180 * change unless you get test reports from all Lenovo
3181 * models. May cause the BIOS to interfere with the
3182 * HDA mixer.
3183 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003184 KEY_RESERVED, /* 0x14: VOLUME UP */
3185 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3186 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003187
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003188 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003189
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003190 /* (assignments unknown, please report if found) */
3191 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3192 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3193 };
3194
3195#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3196#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3197#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3198
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003199 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003200 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003201 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003202 bool radiosw_state = false;
3203 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003204
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003205 unsigned long quirks;
3206
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003207 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3208 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003209
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003210 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003211 BUG_ON(tpacpi_inputdev->open != NULL ||
3212 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003213
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003214 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003215 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003216
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003217#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3218 mutex_init(&hotkey_thread_mutex);
3219 mutex_init(&hotkey_thread_data_mutex);
3220#endif
3221
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003222 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003223 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003224
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003225 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3226 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003227 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003228
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003229 if (!tp_features.hotkey)
3230 return 1;
3231
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003232 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3233 ARRAY_SIZE(tpacpi_hotkey_qtable));
3234
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003235 tpacpi_disable_brightness_delay();
3236
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003237 /* MUST have enough space for all attributes to be added to
3238 * hotkey_dev_attributes */
3239 hotkey_dev_attributes = create_attr_set(
3240 ARRAY_SIZE(hotkey_attributes) + 2,
3241 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003242 if (!hotkey_dev_attributes)
3243 return -ENOMEM;
3244 res = add_many_to_attr_set(hotkey_dev_attributes,
3245 hotkey_attributes,
3246 ARRAY_SIZE(hotkey_attributes));
3247 if (res)
3248 goto err_exit;
3249
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003250 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003251 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3252 for HKEY interface version 0x100 */
3253 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3254 if ((hkeyv >> 8) != 1) {
3255 printk(TPACPI_ERR "unknown version of the "
3256 "HKEY interface: 0x%x\n", hkeyv);
3257 printk(TPACPI_ERR "please report this to %s\n",
3258 TPACPI_MAIL);
3259 } else {
3260 /*
3261 * MHKV 0x100 in A31, R40, R40e,
3262 * T4x, X31, and later
3263 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003264 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3265 "firmware HKEY interface version: 0x%x\n",
3266 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003267
3268 /* Paranoia check AND init hotkey_all_mask */
3269 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3270 "MHKA", "qd")) {
3271 printk(TPACPI_ERR
3272 "missing MHKA handler, "
3273 "please report this to %s\n",
3274 TPACPI_MAIL);
3275 /* Fallback: pre-init for FN+F3,F4,F12 */
3276 hotkey_all_mask = 0x080cU;
3277 } else {
3278 tp_features.hotkey_mask = 1;
3279 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003280 }
3281 }
3282
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003283 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3284 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003285 str_supported(tp_features.hotkey_mask));
3286
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003287 /* Init hotkey_all_mask if not initialized yet */
3288 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3289 (quirks & TPACPI_HK_Q_INIMASK))
3290 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003291
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003292 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003293 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003294 /* hotkey_source_mask *must* be zero for
3295 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003296 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003297 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003298 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003299
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003300 hotkey_orig_mask = hotkey_acpi_mask;
3301 } else {
3302 hotkey_orig_mask = hotkey_all_mask;
3303 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003304 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003305
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003306#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3307 if (dbg_wlswemul) {
3308 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003309 radiosw_state = !!tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003310 printk(TPACPI_INFO
3311 "radio switch emulation enabled\n");
3312 } else
3313#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003314 /* Not all thinkpads have a hardware radio switch */
3315 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3316 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003317 radiosw_state = !!status;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003318 printk(TPACPI_INFO
3319 "radio switch found; radios are %s\n",
3320 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003321 }
3322 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003323 res = add_to_attr_set(hotkey_dev_attributes,
3324 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003325
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003326 /* For X41t, X60t, X61t Tablets... */
3327 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3328 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003329 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003330 printk(TPACPI_INFO
3331 "possible tablet mode switch found; "
3332 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003333 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003334 res = add_to_attr_set(hotkey_dev_attributes,
3335 &dev_attr_hotkey_tablet_mode.attr);
3336 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003337
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003338 if (!res)
3339 res = register_attr_set_with_sysfs(
3340 hotkey_dev_attributes,
3341 &tpacpi_pdev->dev.kobj);
3342 if (res)
3343 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003344
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003345 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003346
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003347 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3348 GFP_KERNEL);
3349 if (!hotkey_keycode_map) {
3350 printk(TPACPI_ERR
3351 "failed to allocate memory for key map\n");
3352 res = -ENOMEM;
3353 goto err_exit;
3354 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003355
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03003356 if (tpacpi_is_lenovo()) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003357 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003358 "using Lenovo default hot key map\n");
3359 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3360 TPACPI_HOTKEY_MAP_SIZE);
3361 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003362 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003363 "using IBM default hot key map\n");
3364 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3365 TPACPI_HOTKEY_MAP_SIZE);
3366 }
3367
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003368 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003369 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3370 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3371 tpacpi_inputdev->keycode = hotkey_keycode_map;
3372 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3373 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003374 input_set_capability(tpacpi_inputdev, EV_KEY,
3375 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003376 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003377 if (i < sizeof(hotkey_reserved_mask)*8)
3378 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003379 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003380 }
3381
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003382 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003383 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003384 input_report_switch(tpacpi_inputdev,
3385 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003386 }
3387 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003388 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003389 input_report_switch(tpacpi_inputdev,
3390 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003391 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003392
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003393 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003394 * userspace. tpacpi_detect_brightness_capabilities() must have
3395 * been called before this point */
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003396 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003397 printk(TPACPI_INFO
3398 "This ThinkPad has standard ACPI backlight "
3399 "brightness control, supported by the ACPI "
3400 "video driver\n");
3401 printk(TPACPI_NOTICE
3402 "Disabling thinkpad-acpi brightness events "
3403 "by default...\n");
3404
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003405 /* Disable brightness up/down on Lenovo thinkpads when
3406 * ACPI is handling them, otherwise it is plain impossible
3407 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003408 hotkey_reserved_mask |=
3409 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3410 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003411 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3412 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003413 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003414
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003415#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003416 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3417 & ~hotkey_all_mask
3418 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003419
3420 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3421 "hotkey source mask 0x%08x, polling freq %u\n",
3422 hotkey_source_mask, hotkey_poll_freq);
3423#endif
3424
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003425 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3426 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003427 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003428 if (res) {
3429 hotkey_exit();
3430 return res;
3431 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003432 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3433 | hotkey_driver_mask)
3434 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003435 if (res < 0 && res != -ENXIO) {
3436 hotkey_exit();
3437 return res;
3438 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003439 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3440 & ~hotkey_reserved_mask;
3441 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3442 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3443 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003444
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003445 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3446 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003447 (hotkey_report_mode < 2) ?
3448 "enabled" : "disabled");
3449
3450 tpacpi_inputdev->open = &hotkey_inputdev_open;
3451 tpacpi_inputdev->close = &hotkey_inputdev_close;
3452
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003453 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003454
3455 return 0;
3456
3457err_exit:
3458 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3459 hotkey_dev_attributes = NULL;
3460
3461 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003462}
3463
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003464static bool hotkey_notify_hotkey(const u32 hkey,
3465 bool *send_acpi_ev,
3466 bool *ignore_acpi_ev)
3467{
3468 /* 0x1000-0x1FFF: key presses */
3469 unsigned int scancode = hkey & 0xfff;
3470 *send_acpi_ev = true;
3471 *ignore_acpi_ev = false;
3472
3473 if (scancode > 0 && scancode < 0x21) {
3474 scancode--;
3475 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003476 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003477 *send_acpi_ev = false;
3478 } else {
3479 *ignore_acpi_ev = true;
3480 }
3481 return true;
3482 }
3483 return false;
3484}
3485
3486static bool hotkey_notify_wakeup(const u32 hkey,
3487 bool *send_acpi_ev,
3488 bool *ignore_acpi_ev)
3489{
3490 /* 0x2000-0x2FFF: Wakeup reason */
3491 *send_acpi_ev = true;
3492 *ignore_acpi_ev = false;
3493
3494 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003495 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3496 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003497 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3498 *ignore_acpi_ev = true;
3499 break;
3500
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003501 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3502 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003503 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3504 *ignore_acpi_ev = true;
3505 break;
3506
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003507 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3508 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003509 printk(TPACPI_ALERT
3510 "EMERGENCY WAKEUP: battery almost empty\n");
3511 /* how to auto-heal: */
3512 /* 2313: woke up from S3, go to S4/S5 */
3513 /* 2413: woke up from S4, go to S5 */
3514 break;
3515
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003516 default:
3517 return false;
3518 }
3519
3520 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3521 printk(TPACPI_INFO
3522 "woke up due to a hot-unplug "
3523 "request...\n");
3524 hotkey_wakeup_reason_notify_change();
3525 }
3526 return true;
3527}
3528
3529static bool hotkey_notify_usrevent(const u32 hkey,
3530 bool *send_acpi_ev,
3531 bool *ignore_acpi_ev)
3532{
3533 /* 0x5000-0x5FFF: human interface helpers */
3534 *send_acpi_ev = true;
3535 *ignore_acpi_ev = false;
3536
3537 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003538 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3539 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003540 return true;
3541
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003542 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3543 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003544 tpacpi_input_send_tabletsw();
3545 hotkey_tablet_mode_notify_change();
3546 *send_acpi_ev = false;
3547 return true;
3548
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003549 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3550 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3551 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003552 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003553 *ignore_acpi_ev = true;
3554 return true;
3555
3556 default:
3557 return false;
3558 }
3559}
3560
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003561static void thermal_dump_all_sensors(void);
3562
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003563static bool hotkey_notify_thermal(const u32 hkey,
3564 bool *send_acpi_ev,
3565 bool *ignore_acpi_ev)
3566{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003567 bool known = true;
3568
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003569 /* 0x6000-0x6FFF: thermal alarms */
3570 *send_acpi_ev = true;
3571 *ignore_acpi_ev = false;
3572
3573 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003574 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003575 printk(TPACPI_INFO
3576 "EC reports that Thermal Table has changed\n");
3577 /* recommended action: do nothing, we don't have
3578 * Lenovo ATM information */
3579 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003580 case TP_HKEY_EV_ALARM_BAT_HOT:
3581 printk(TPACPI_CRIT
3582 "THERMAL ALARM: battery is too hot!\n");
3583 /* recommended action: warn user through gui */
3584 break;
3585 case TP_HKEY_EV_ALARM_BAT_XHOT:
3586 printk(TPACPI_ALERT
3587 "THERMAL EMERGENCY: battery is extremely hot!\n");
3588 /* recommended action: immediate sleep/hibernate */
3589 break;
3590 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3591 printk(TPACPI_CRIT
3592 "THERMAL ALARM: "
3593 "a sensor reports something is too hot!\n");
3594 /* recommended action: warn user through gui, that */
3595 /* some internal component is too hot */
3596 break;
3597 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3598 printk(TPACPI_ALERT
3599 "THERMAL EMERGENCY: "
3600 "a sensor reports something is extremely hot!\n");
3601 /* recommended action: immediate sleep/hibernate */
3602 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003603 default:
3604 printk(TPACPI_ALERT
3605 "THERMAL ALERT: unknown thermal alarm received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003606 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003607 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003608
3609 thermal_dump_all_sensors();
3610
3611 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003612}
3613
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003614static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3615{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003616 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003617 bool send_acpi_ev;
3618 bool ignore_acpi_ev;
3619 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003620
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003621 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003622 printk(TPACPI_ERR
3623 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003624 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003625 acpi_bus_generate_netlink_event(
3626 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003627 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003628 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003629 return;
3630 }
3631
3632 while (1) {
3633 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003634 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003635 return;
3636 }
3637
3638 if (hkey == 0) {
3639 /* queue empty */
3640 return;
3641 }
3642
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003643 send_acpi_ev = true;
3644 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003645
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003646 switch (hkey >> 12) {
3647 case 1:
3648 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003649 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3650 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003651 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003652 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003653 /* 0x2000-0x2FFF: Wakeup reason */
3654 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3655 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003656 break;
3657 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003658 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003659 switch (hkey) {
3660 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003661 hotkey_autosleep_ack = 1;
3662 printk(TPACPI_INFO
3663 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003664 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003665 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003666 break;
3667 case TP_HKEY_EV_OPTDRV_EJ:
3668 /* FIXME: kick libata if SATA link offline */
3669 known_ev = true;
3670 break;
3671 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003672 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003673 }
3674 break;
3675 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003676 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003677 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003678 hotkey_autosleep_ack = 1;
3679 printk(TPACPI_INFO
3680 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003681 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003682 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003683 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003684 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003685 }
3686 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003687 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003688 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003689 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3690 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003691 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003692 case 6:
3693 /* 0x6000-0x6FFF: thermal alarms */
3694 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3695 &ignore_acpi_ev);
3696 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003697 case 7:
3698 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003699 if (tp_features.hotkey_wlsw &&
3700 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003701 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003702 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003703 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003704 break;
3705 }
3706 /* fallthrough to default */
3707 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003708 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003709 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003710 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003711 printk(TPACPI_NOTICE
3712 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003713 printk(TPACPI_NOTICE
3714 "please report the conditions when this "
3715 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003716 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003717
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003718 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003719 if (!ignore_acpi_ev &&
3720 (send_acpi_ev || hotkey_report_mode < 2)) {
3721 acpi_bus_generate_proc_event(ibm->acpi->device,
3722 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003723 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003724
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003725 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003726 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003727 acpi_bus_generate_netlink_event(
3728 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003729 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003730 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003731 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003732 }
3733}
3734
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003735static void hotkey_suspend(pm_message_t state)
3736{
3737 /* Do these on suspend, we get the events on early resume! */
3738 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3739 hotkey_autosleep_ack = 0;
3740}
3741
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003742static void hotkey_resume(void)
3743{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003744 tpacpi_disable_brightness_delay();
3745
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003746 if (hotkey_status_set(true) < 0 ||
3747 hotkey_mask_set(hotkey_acpi_mask) < 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003748 printk(TPACPI_ERR
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003749 "error while attempting to reset the event "
3750 "firmware interface\n");
3751
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003752 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003753 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003754 hotkey_wakeup_reason_notify_change();
3755 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003756 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003757}
3758
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003759/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003760static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003762 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003764 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003765 seq_printf(m, "status:\t\tnot supported\n");
3766 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003767 }
3768
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003769 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003770 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003771 res = hotkey_status_get(&status);
3772 if (!res)
3773 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003774 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003775 if (res)
3776 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003778 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003779 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003780 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3781 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003783 seq_printf(m, "mask:\t\tnot supported\n");
3784 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 }
3786
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788}
3789
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003790static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003791{
3792 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003793 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3794 TPACPI_WARN
3795 "hotkey enable/disable functionality has been "
3796 "removed from the driver. Hotkeys are always "
3797 "enabled\n"))
3798 printk(TPACPI_ERR
3799 "Please remove the hotkey=enable module "
3800 "parameter, it is deprecated. Hotkeys are always "
3801 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003802}
3803
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003804static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003806 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003807 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003810 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 return -ENODEV;
3812
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003813 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003814 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003815
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003816 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003817
3818 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 while ((cmd = next_cmd(&buf))) {
3820 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003821 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003823 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003824 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003826 mask = (hotkey_all_mask | hotkey_source_mask)
3827 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3829 /* mask set */
3830 } else if (sscanf(cmd, "%x", &mask) == 1) {
3831 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003832 } else {
3833 res = -EINVAL;
3834 goto errexit;
3835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003837
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003838 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003839 tpacpi_disclose_usertask("procfs hotkey",
3840 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003841 res = hotkey_user_mask_set(mask);
3842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003844errexit:
3845 mutex_unlock(&hotkey_mutex);
3846 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003847}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003849static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003850 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003851 {"", 0},
3852};
3853
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003854static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003855 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003856 .notify = hotkey_notify,
3857 .handle = &hkey_handle,
3858 .type = ACPI_DEVICE_NOTIFY,
3859};
3860
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003861static struct ibm_struct hotkey_driver_data = {
3862 .name = "hotkey",
3863 .read = hotkey_read,
3864 .write = hotkey_write,
3865 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003866 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003867 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003868 .acpi = &ibm_hotkey_acpidriver,
3869};
3870
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003871/*************************************************************************
3872 * Bluetooth subdriver
3873 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003875enum {
3876 /* ACPI GBDC/SBDC bits */
3877 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3878 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003879 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003880 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003881};
3882
3883enum {
3884 /* ACPI \BLTH commands */
3885 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3886 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3887 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3888 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3889 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003890};
3891
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003892#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3893
Johannes Berg19d337d2009-06-02 13:01:37 +02003894static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003895{
3896 int status;
3897
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003898#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3899 if (dbg_bluetoothemul)
3900 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003901 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003902#endif
3903
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003904 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3905 return -EIO;
3906
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003907 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003908 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003909}
3910
Johannes Berg19d337d2009-06-02 13:01:37 +02003911static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003912{
3913 int status;
3914
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003915 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003916 "will attempt to %s bluetooth\n",
3917 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003918
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003919#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3920 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003921 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003922 return 0;
3923 }
3924#endif
3925
Johannes Berg19d337d2009-06-02 13:01:37 +02003926 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003927 status = TP_ACPI_BLUETOOTH_RADIOSSW
3928 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3929 else
3930 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003931
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003932 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3933 return -EIO;
3934
3935 return 0;
3936}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003937
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003938/* sysfs bluetooth enable ---------------------------------------------- */
3939static ssize_t bluetooth_enable_show(struct device *dev,
3940 struct device_attribute *attr,
3941 char *buf)
3942{
Johannes Berg19d337d2009-06-02 13:01:37 +02003943 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3944 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003945}
3946
3947static ssize_t bluetooth_enable_store(struct device *dev,
3948 struct device_attribute *attr,
3949 const char *buf, size_t count)
3950{
Johannes Berg19d337d2009-06-02 13:01:37 +02003951 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3952 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003953}
3954
3955static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003956 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003957 bluetooth_enable_show, bluetooth_enable_store);
3958
3959/* --------------------------------------------------------------------- */
3960
3961static struct attribute *bluetooth_attributes[] = {
3962 &dev_attr_bluetooth_enable.attr,
3963 NULL
3964};
3965
3966static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003967 .attrs = bluetooth_attributes,
3968};
3969
Johannes Berg19d337d2009-06-02 13:01:37 +02003970static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3971 .get_status = bluetooth_get_status,
3972 .set_status = bluetooth_set_status,
3973};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003974
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003975static void bluetooth_shutdown(void)
3976{
3977 /* Order firmware to save current state to NVRAM */
3978 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3979 TP_ACPI_BLTH_SAVE_STATE))
3980 printk(TPACPI_NOTICE
3981 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003982 else
3983 vdbg_printk(TPACPI_DBG_RFKILL,
3984 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003985}
3986
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003987static void bluetooth_exit(void)
3988{
3989 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3990 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003991
3992 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3993
3994 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003995}
3996
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003997static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003999 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004000 int status = 0;
4001
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004002 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4003 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004004
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004005 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004006
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004007 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4008 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004009 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004010 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004012 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4013 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004014 str_supported(tp_features.bluetooth),
4015 status);
4016
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004017#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4018 if (dbg_bluetoothemul) {
4019 tp_features.bluetooth = 1;
4020 printk(TPACPI_INFO
4021 "bluetooth switch emulation enabled\n");
4022 } else
4023#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004024 if (tp_features.bluetooth &&
4025 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4026 /* no bluetooth hardware present in system */
4027 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004028 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004029 "bluetooth hardware not installed\n");
4030 }
4031
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004032 if (!tp_features.bluetooth)
4033 return 1;
4034
Johannes Berg19d337d2009-06-02 13:01:37 +02004035 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4036 &bluetooth_tprfk_ops,
4037 RFKILL_TYPE_BLUETOOTH,
4038 TPACPI_RFK_BLUETOOTH_SW_NAME,
4039 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004040 if (res)
4041 return res;
4042
Johannes Berg19d337d2009-06-02 13:01:37 +02004043 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4044 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004045 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004046 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004047 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004048 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004049
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004050 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051}
4052
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004053/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004054static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004056 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057}
4058
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004059static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060{
Johannes Berg19d337d2009-06-02 13:01:37 +02004061 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062}
4063
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004064static struct ibm_struct bluetooth_driver_data = {
4065 .name = "bluetooth",
4066 .read = bluetooth_read,
4067 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004068 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004069 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004070};
4071
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004072/*************************************************************************
4073 * Wan subdriver
4074 */
4075
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004076enum {
4077 /* ACPI GWAN/SWAN bits */
4078 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4079 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004080 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004081 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004082};
4083
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004084#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4085
Johannes Berg19d337d2009-06-02 13:01:37 +02004086static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004087{
4088 int status;
4089
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004090#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4091 if (dbg_wwanemul)
4092 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004093 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004094#endif
4095
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004096 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4097 return -EIO;
4098
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004099 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004100 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004101}
4102
Johannes Berg19d337d2009-06-02 13:01:37 +02004103static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004104{
4105 int status;
4106
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004107 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004108 "will attempt to %s wwan\n",
4109 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004110
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004111#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4112 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004113 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004114 return 0;
4115 }
4116#endif
4117
Johannes Berg19d337d2009-06-02 13:01:37 +02004118 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004119 status = TP_ACPI_WANCARD_RADIOSSW
4120 | TP_ACPI_WANCARD_RESUMECTRL;
4121 else
4122 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004123
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004124 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4125 return -EIO;
4126
4127 return 0;
4128}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004129
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004130/* sysfs wan enable ---------------------------------------------------- */
4131static ssize_t wan_enable_show(struct device *dev,
4132 struct device_attribute *attr,
4133 char *buf)
4134{
Johannes Berg19d337d2009-06-02 13:01:37 +02004135 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4136 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004137}
4138
4139static ssize_t wan_enable_store(struct device *dev,
4140 struct device_attribute *attr,
4141 const char *buf, size_t count)
4142{
Johannes Berg19d337d2009-06-02 13:01:37 +02004143 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4144 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004145}
4146
4147static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004148 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004149 wan_enable_show, wan_enable_store);
4150
4151/* --------------------------------------------------------------------- */
4152
4153static struct attribute *wan_attributes[] = {
4154 &dev_attr_wan_enable.attr,
4155 NULL
4156};
4157
4158static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004159 .attrs = wan_attributes,
4160};
4161
Johannes Berg19d337d2009-06-02 13:01:37 +02004162static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4163 .get_status = wan_get_status,
4164 .set_status = wan_set_status,
4165};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004166
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004167static void wan_shutdown(void)
4168{
4169 /* Order firmware to save current state to NVRAM */
4170 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4171 TP_ACPI_WGSV_SAVE_STATE))
4172 printk(TPACPI_NOTICE
4173 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004174 else
4175 vdbg_printk(TPACPI_DBG_RFKILL,
4176 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004177}
4178
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004179static void wan_exit(void)
4180{
4181 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4182 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004183
4184 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4185
4186 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004187}
4188
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004189static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004190{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004191 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004192 int status = 0;
4193
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004194 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4195 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004196
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004197 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004198
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004199 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004200 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004201
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004202 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4203 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004204 str_supported(tp_features.wan),
4205 status);
4206
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004207#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4208 if (dbg_wwanemul) {
4209 tp_features.wan = 1;
4210 printk(TPACPI_INFO
4211 "wwan switch emulation enabled\n");
4212 } else
4213#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004214 if (tp_features.wan &&
4215 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4216 /* no wan hardware present in system */
4217 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004218 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004219 "wan hardware not installed\n");
4220 }
4221
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004222 if (!tp_features.wan)
4223 return 1;
4224
Johannes Berg19d337d2009-06-02 13:01:37 +02004225 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4226 &wan_tprfk_ops,
4227 RFKILL_TYPE_WWAN,
4228 TPACPI_RFK_WWAN_SW_NAME,
4229 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004230 if (res)
4231 return res;
4232
Johannes Berg19d337d2009-06-02 13:01:37 +02004233 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4234 &wan_attr_group);
4235
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004236 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004237 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004238 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004239 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004240
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004241 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004242}
4243
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004244/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004245static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004246{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004247 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004248}
4249
4250static int wan_write(char *buf)
4251{
Johannes Berg19d337d2009-06-02 13:01:37 +02004252 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004253}
4254
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004255static struct ibm_struct wan_driver_data = {
4256 .name = "wan",
4257 .read = wan_read,
4258 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004259 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004260 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004261};
4262
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004263/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004264 * UWB subdriver
4265 */
4266
4267enum {
4268 /* ACPI GUWB/SUWB bits */
4269 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4270 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4271};
4272
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004273#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4274
Johannes Berg19d337d2009-06-02 13:01:37 +02004275static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004276{
4277 int status;
4278
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004279#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4280 if (dbg_uwbemul)
4281 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004282 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004283#endif
4284
4285 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4286 return -EIO;
4287
4288 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004289 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004290}
4291
Johannes Berg19d337d2009-06-02 13:01:37 +02004292static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004293{
4294 int status;
4295
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004296 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004297 "will attempt to %s UWB\n",
4298 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004299
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004300#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4301 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004302 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004303 return 0;
4304 }
4305#endif
4306
Johannes Berg19d337d2009-06-02 13:01:37 +02004307 if (state == TPACPI_RFK_RADIO_ON)
4308 status = TP_ACPI_UWB_RADIOSSW;
4309 else
4310 status = 0;
4311
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004312 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4313 return -EIO;
4314
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004315 return 0;
4316}
4317
4318/* --------------------------------------------------------------------- */
4319
Johannes Berg19d337d2009-06-02 13:01:37 +02004320static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4321 .get_status = uwb_get_status,
4322 .set_status = uwb_set_status,
4323};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004324
4325static void uwb_exit(void)
4326{
Johannes Berg19d337d2009-06-02 13:01:37 +02004327 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004328}
4329
4330static int __init uwb_init(struct ibm_init_struct *iibm)
4331{
4332 int res;
4333 int status = 0;
4334
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004335 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4336 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004337
4338 TPACPI_ACPIHANDLE_INIT(hkey);
4339
4340 tp_features.uwb = hkey_handle &&
4341 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4342
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004343 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4344 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004345 str_supported(tp_features.uwb),
4346 status);
4347
4348#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4349 if (dbg_uwbemul) {
4350 tp_features.uwb = 1;
4351 printk(TPACPI_INFO
4352 "uwb switch emulation enabled\n");
4353 } else
4354#endif
4355 if (tp_features.uwb &&
4356 !(status & TP_ACPI_UWB_HWPRESENT)) {
4357 /* no uwb hardware present in system */
4358 tp_features.uwb = 0;
4359 dbg_printk(TPACPI_DBG_INIT,
4360 "uwb hardware not installed\n");
4361 }
4362
4363 if (!tp_features.uwb)
4364 return 1;
4365
4366 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004367 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004368 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004369 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004370 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004371 return res;
4372}
4373
4374static struct ibm_struct uwb_driver_data = {
4375 .name = "uwb",
4376 .exit = uwb_exit,
4377 .flags.experimental = 1,
4378};
4379
4380/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004381 * Video subdriver
4382 */
4383
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004384#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4385
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004386enum video_access_mode {
4387 TPACPI_VIDEO_NONE = 0,
4388 TPACPI_VIDEO_570, /* 570 */
4389 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4390 TPACPI_VIDEO_NEW, /* all others */
4391};
4392
4393enum { /* video status flags, based on VIDEO_570 */
4394 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4395 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4396 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4397};
4398
4399enum { /* TPACPI_VIDEO_570 constants */
4400 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4401 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4402 * video_status_flags */
4403 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4404 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4405};
4406
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004407static enum video_access_mode video_supported;
4408static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004409
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004410static int video_autosw_get(void);
4411static int video_autosw_set(int enable);
4412
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004413TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004414
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004415static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004417 int ivga;
4418
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004419 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4420
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004421 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004422 if (tpacpi_is_ibm())
4423 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004424
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004425 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4426 /* G41, assume IVGA doesn't change */
4427 vid_handle = vid2_handle;
4428
4429 if (!vid_handle)
4430 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004431 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004432 else if (tpacpi_is_ibm() &&
4433 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004434 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004435 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004436 else if (tpacpi_is_ibm() &&
4437 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004438 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004439 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004440 else
4441 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004442 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004444 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4445 str_supported(video_supported != TPACPI_VIDEO_NONE),
4446 video_supported);
4447
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004448 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449}
4450
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004451static void video_exit(void)
4452{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004453 dbg_printk(TPACPI_DBG_EXIT,
4454 "restoring original video autoswitch mode\n");
4455 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004456 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004457 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004458}
4459
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004460static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461{
4462 int status = 0;
4463 int i;
4464
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004465 switch (video_supported) {
4466 case TPACPI_VIDEO_570:
4467 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4468 TP_ACPI_VIDEO_570_PHSCMD))
4469 return -EIO;
4470 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4471 break;
4472 case TPACPI_VIDEO_770:
4473 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4474 return -EIO;
4475 if (i)
4476 status |= TP_ACPI_VIDEO_S_LCD;
4477 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4478 return -EIO;
4479 if (i)
4480 status |= TP_ACPI_VIDEO_S_CRT;
4481 break;
4482 case TPACPI_VIDEO_NEW:
4483 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4484 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4485 return -EIO;
4486 if (i)
4487 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004489 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4490 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4491 return -EIO;
4492 if (i)
4493 status |= TP_ACPI_VIDEO_S_LCD;
4494 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4495 return -EIO;
4496 if (i)
4497 status |= TP_ACPI_VIDEO_S_DVI;
4498 break;
4499 default:
4500 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
4503 return status;
4504}
4505
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004506static int video_outputsw_set(int status)
4507{
4508 int autosw;
4509 int res = 0;
4510
4511 switch (video_supported) {
4512 case TPACPI_VIDEO_570:
4513 res = acpi_evalf(NULL, NULL,
4514 "\\_SB.PHS2", "vdd",
4515 TP_ACPI_VIDEO_570_PHS2CMD,
4516 status | TP_ACPI_VIDEO_570_PHS2SET);
4517 break;
4518 case TPACPI_VIDEO_770:
4519 autosw = video_autosw_get();
4520 if (autosw < 0)
4521 return autosw;
4522
4523 res = video_autosw_set(1);
4524 if (res)
4525 return res;
4526 res = acpi_evalf(vid_handle, NULL,
4527 "ASWT", "vdd", status * 0x100, 0);
4528 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004529 printk(TPACPI_ERR
4530 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004531 return -EIO;
4532 }
4533 break;
4534 case TPACPI_VIDEO_NEW:
4535 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004536 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004537 break;
4538 default:
4539 return -ENOSYS;
4540 }
4541
4542 return (res)? 0 : -EIO;
4543}
4544
4545static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004547 int autosw = 0;
4548
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004549 switch (video_supported) {
4550 case TPACPI_VIDEO_570:
4551 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4552 return -EIO;
4553 break;
4554 case TPACPI_VIDEO_770:
4555 case TPACPI_VIDEO_NEW:
4556 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4557 return -EIO;
4558 break;
4559 default:
4560 return -ENOSYS;
4561 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004562
4563 return autosw & 1;
4564}
4565
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004566static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004567{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004568 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004569 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004570 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004571}
4572
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004573static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004574{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004575 int autosw = video_autosw_get();
4576 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004577
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004578 if (autosw < 0)
4579 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004580
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004581 switch (video_supported) {
4582 case TPACPI_VIDEO_570:
4583 res = video_autosw_set(1);
4584 if (res)
4585 return res;
4586 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4587 break;
4588 case TPACPI_VIDEO_770:
4589 case TPACPI_VIDEO_NEW:
4590 res = video_autosw_set(1);
4591 if (res)
4592 return res;
4593 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4594 break;
4595 default:
4596 return -ENOSYS;
4597 }
4598 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004599 printk(TPACPI_ERR
4600 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004601 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004602 }
4603
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004604 return (res)? 0 : -EIO;
4605}
4606
4607static int video_expand_toggle(void)
4608{
4609 switch (video_supported) {
4610 case TPACPI_VIDEO_570:
4611 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4612 0 : -EIO;
4613 case TPACPI_VIDEO_770:
4614 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4615 0 : -EIO;
4616 case TPACPI_VIDEO_NEW:
4617 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4618 0 : -EIO;
4619 default:
4620 return -ENOSYS;
4621 }
4622 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004623}
4624
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004625static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004626{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004627 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004628
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004629 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004630 seq_printf(m, "status:\t\tnot supported\n");
4631 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004632 }
4633
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004634 /* Even reads can crash X.org, so... */
4635 if (!capable(CAP_SYS_ADMIN))
4636 return -EPERM;
4637
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004638 status = video_outputsw_get();
4639 if (status < 0)
4640 return status;
4641
4642 autosw = video_autosw_get();
4643 if (autosw < 0)
4644 return autosw;
4645
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004646 seq_printf(m, "status:\t\tsupported\n");
4647 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4648 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004649 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004650 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4651 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4652 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4653 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004654 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004655 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4656 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4657 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004658
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004659 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004660}
4661
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004662static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663{
4664 char *cmd;
4665 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004666 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004668 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004669 return -ENODEV;
4670
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004671 /* Even reads can crash X.org, let alone writes... */
4672 if (!capable(CAP_SYS_ADMIN))
4673 return -EPERM;
4674
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004675 enable = 0;
4676 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
4678 while ((cmd = next_cmd(&buf))) {
4679 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004680 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004682 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004684 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004686 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004687 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004688 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004689 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004690 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004691 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004692 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004694 res = video_autosw_set(1);
4695 if (res)
4696 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004698 res = video_autosw_set(0);
4699 if (res)
4700 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004702 res = video_outputsw_cycle();
4703 if (res)
4704 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004706 res = video_expand_toggle();
4707 if (res)
4708 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 } else
4710 return -EINVAL;
4711 }
4712
4713 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004714 status = video_outputsw_get();
4715 if (status < 0)
4716 return status;
4717 res = video_outputsw_set((status & ~disable) | enable);
4718 if (res)
4719 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 }
4721
4722 return 0;
4723}
4724
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004725static struct ibm_struct video_driver_data = {
4726 .name = "video",
4727 .read = video_read,
4728 .write = video_write,
4729 .exit = video_exit,
4730};
4731
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004732#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4733
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004734/*************************************************************************
4735 * Light (thinklight) subdriver
4736 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004738TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4739TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004740
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004741static int light_get_status(void)
4742{
4743 int status = 0;
4744
4745 if (tp_features.light_status) {
4746 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4747 return -EIO;
4748 return (!!status);
4749 }
4750
4751 return -ENXIO;
4752}
4753
4754static int light_set_status(int status)
4755{
4756 int rc;
4757
4758 if (tp_features.light) {
4759 if (cmos_handle) {
4760 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4761 (status)?
4762 TP_CMOS_THINKLIGHT_ON :
4763 TP_CMOS_THINKLIGHT_OFF);
4764 } else {
4765 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4766 (status)? 1 : 0);
4767 }
4768 return (rc)? 0 : -EIO;
4769 }
4770
4771 return -ENXIO;
4772}
4773
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004774static void light_set_status_worker(struct work_struct *work)
4775{
4776 struct tpacpi_led_classdev *data =
4777 container_of(work, struct tpacpi_led_classdev, work);
4778
4779 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004780 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004781}
4782
4783static void light_sysfs_set(struct led_classdev *led_cdev,
4784 enum led_brightness brightness)
4785{
4786 struct tpacpi_led_classdev *data =
4787 container_of(led_cdev,
4788 struct tpacpi_led_classdev,
4789 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004790 data->new_state = (brightness != LED_OFF) ?
4791 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004792 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004793}
4794
4795static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4796{
4797 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4798}
4799
4800static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4801 .led_classdev = {
4802 .name = "tpacpi::thinklight",
4803 .brightness_set = &light_sysfs_set,
4804 .brightness_get = &light_sysfs_get,
4805 }
4806};
4807
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004808static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004810 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004811
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004812 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4813
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004814 if (tpacpi_is_ibm()) {
4815 TPACPI_ACPIHANDLE_INIT(ledb);
4816 TPACPI_ACPIHANDLE_INIT(lght);
4817 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004818 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004819 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004820
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004821 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004822 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004823
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004824 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004825 /* light status not supported on
4826 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004827 tp_features.light_status =
4828 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004830 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4831 str_supported(tp_features.light),
4832 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004833
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004834 if (!tp_features.light)
4835 return 1;
4836
4837 rc = led_classdev_register(&tpacpi_pdev->dev,
4838 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004839
4840 if (rc < 0) {
4841 tp_features.light = 0;
4842 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004843 } else {
4844 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004845 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004846
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004847 return rc;
4848}
4849
4850static void light_exit(void)
4851{
4852 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4853 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004854 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855}
4856
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004857static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004859 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004861 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004862 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004863 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004864 seq_printf(m, "status:\t\tunknown\n");
4865 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004866 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004867 status = light_get_status();
4868 if (status < 0)
4869 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004870 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4871 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875}
4876
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004877static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004880 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004881
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004882 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004883 return -ENODEV;
4884
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 while ((cmd = next_cmd(&buf))) {
4886 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004887 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004889 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 } else
4891 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 }
4893
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004894 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895}
4896
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004897static struct ibm_struct light_driver_data = {
4898 .name = "light",
4899 .read = light_read,
4900 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004901 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004902};
4903
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004904/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004905 * CMOS subdriver
4906 */
4907
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004908/* sysfs cmos_command -------------------------------------------------- */
4909static ssize_t cmos_command_store(struct device *dev,
4910 struct device_attribute *attr,
4911 const char *buf, size_t count)
4912{
4913 unsigned long cmos_cmd;
4914 int res;
4915
4916 if (parse_strtoul(buf, 21, &cmos_cmd))
4917 return -EINVAL;
4918
4919 res = issue_thinkpad_cmos_command(cmos_cmd);
4920 return (res)? res : count;
4921}
4922
4923static struct device_attribute dev_attr_cmos_command =
4924 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4925
4926/* --------------------------------------------------------------------- */
4927
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004928static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004929{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004930 int res;
4931
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004932 vdbg_printk(TPACPI_DBG_INIT,
4933 "initializing cmos commands subdriver\n");
4934
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004935 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004936
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004937 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4938 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004939
4940 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4941 if (res)
4942 return res;
4943
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004944 return (cmos_handle)? 0 : 1;
4945}
4946
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004947static void cmos_exit(void)
4948{
4949 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4950}
4951
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004952static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004954 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4955 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004957 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004959 seq_printf(m, "status:\t\tsupported\n");
4960 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 }
4962
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004963 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964}
4965
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004966static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967{
4968 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004969 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
4971 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004972 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4973 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 /* cmos_cmd set */
4975 } else
4976 return -EINVAL;
4977
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004978 res = issue_thinkpad_cmos_command(cmos_cmd);
4979 if (res)
4980 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 }
4982
4983 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004984}
4985
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004986static struct ibm_struct cmos_driver_data = {
4987 .name = "cmos",
4988 .read = cmos_read,
4989 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004990 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004991};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004992
4993/*************************************************************************
4994 * LED subdriver
4995 */
4996
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004997enum led_access_mode {
4998 TPACPI_LED_NONE = 0,
4999 TPACPI_LED_570, /* 570 */
5000 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5001 TPACPI_LED_NEW, /* all others */
5002};
5003
5004enum { /* For TPACPI_LED_OLD */
5005 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5006 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5007 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5008};
5009
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005010static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005011
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005012static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005013
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005014#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005015static struct tpacpi_led_classdev *tpacpi_leds;
5016static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005017static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005018 /* there's a limit of 19 chars + NULL before 2.6.26 */
5019 "tpacpi::power",
5020 "tpacpi:orange:batt",
5021 "tpacpi:green:batt",
5022 "tpacpi::dock_active",
5023 "tpacpi::bay_active",
5024 "tpacpi::dock_batt",
5025 "tpacpi::unknown_led",
5026 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005027 "tpacpi::dock_status1",
5028 "tpacpi::dock_status2",
5029 "tpacpi::unknown_led2",
5030 "tpacpi::unknown_led3",
5031 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005032};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005033#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005034
5035static inline bool tpacpi_is_led_restricted(const unsigned int led)
5036{
5037#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5038 return false;
5039#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005040 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005041#endif
5042}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005043
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005044static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005045{
5046 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005047 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005048
5049 switch (led_supported) {
5050 case TPACPI_LED_570:
5051 if (!acpi_evalf(ec_handle,
5052 &status, "GLED", "dd", 1 << led))
5053 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005054 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005055 TPACPI_LED_OFF :
5056 ((status == 1)?
5057 TPACPI_LED_ON :
5058 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005059 tpacpi_led_state_cache[led] = led_s;
5060 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005061 default:
5062 return -ENXIO;
5063 }
5064
5065 /* not reached */
5066}
5067
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005068static int led_set_status(const unsigned int led,
5069 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005070{
5071 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005072 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5073 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005074
5075 int rc = 0;
5076
5077 switch (led_supported) {
5078 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005079 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005080 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005081 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005082 if (unlikely(tpacpi_is_led_restricted(led)))
5083 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005084 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5085 (1 << led), led_sled_arg1[ledstatus]))
5086 rc = -EIO;
5087 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005088 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005089 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005090 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005091 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005092 if (unlikely(tpacpi_is_led_restricted(led)))
5093 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005094 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5095 if (rc >= 0)
5096 rc = ec_write(TPACPI_LED_EC_HLBL,
5097 (ledstatus == TPACPI_LED_BLINK) << led);
5098 if (rc >= 0)
5099 rc = ec_write(TPACPI_LED_EC_HLCL,
5100 (ledstatus != TPACPI_LED_OFF) << led);
5101 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005102 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005103 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005104 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5105 return -EINVAL;
5106 if (unlikely(tpacpi_is_led_restricted(led)))
5107 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005108 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5109 led, led_led_arg1[ledstatus]))
5110 rc = -EIO;
5111 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005112 default:
5113 rc = -ENXIO;
5114 }
5115
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005116 if (!rc)
5117 tpacpi_led_state_cache[led] = ledstatus;
5118
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005119 return rc;
5120}
5121
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005122static void led_set_status_worker(struct work_struct *work)
5123{
5124 struct tpacpi_led_classdev *data =
5125 container_of(work, struct tpacpi_led_classdev, work);
5126
5127 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005128 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005129}
5130
5131static void led_sysfs_set(struct led_classdev *led_cdev,
5132 enum led_brightness brightness)
5133{
5134 struct tpacpi_led_classdev *data = container_of(led_cdev,
5135 struct tpacpi_led_classdev, led_classdev);
5136
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005137 if (brightness == LED_OFF)
5138 data->new_state = TPACPI_LED_OFF;
5139 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5140 data->new_state = TPACPI_LED_ON;
5141 else
5142 data->new_state = TPACPI_LED_BLINK;
5143
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005144 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005145}
5146
5147static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5148 unsigned long *delay_on, unsigned long *delay_off)
5149{
5150 struct tpacpi_led_classdev *data = container_of(led_cdev,
5151 struct tpacpi_led_classdev, led_classdev);
5152
5153 /* Can we choose the flash rate? */
5154 if (*delay_on == 0 && *delay_off == 0) {
5155 /* yes. set them to the hardware blink rate (1 Hz) */
5156 *delay_on = 500; /* ms */
5157 *delay_off = 500; /* ms */
5158 } else if ((*delay_on != 500) || (*delay_off != 500))
5159 return -EINVAL;
5160
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005161 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005162 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005163
5164 return 0;
5165}
5166
5167static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5168{
5169 int rc;
5170
5171 struct tpacpi_led_classdev *data = container_of(led_cdev,
5172 struct tpacpi_led_classdev, led_classdev);
5173
5174 rc = led_get_status(data->led);
5175
5176 if (rc == TPACPI_LED_OFF || rc < 0)
5177 rc = LED_OFF; /* no error handling in led class :( */
5178 else
5179 rc = LED_FULL;
5180
5181 return rc;
5182}
5183
5184static void led_exit(void)
5185{
5186 unsigned int i;
5187
5188 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5189 if (tpacpi_leds[i].led_classdev.name)
5190 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5191 }
5192
5193 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005194}
5195
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005196static int __init tpacpi_init_led(unsigned int led)
5197{
5198 int rc;
5199
5200 tpacpi_leds[led].led = led;
5201
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005202 /* LEDs with no name don't get registered */
5203 if (!tpacpi_led_names[led])
5204 return 0;
5205
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005206 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5207 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5208 if (led_supported == TPACPI_LED_570)
5209 tpacpi_leds[led].led_classdev.brightness_get =
5210 &led_sysfs_get;
5211
5212 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5213
5214 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5215
5216 rc = led_classdev_register(&tpacpi_pdev->dev,
5217 &tpacpi_leds[led].led_classdev);
5218 if (rc < 0)
5219 tpacpi_leds[led].led_classdev.name = NULL;
5220
5221 return rc;
5222}
5223
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005224static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5225 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5226 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5227 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5228
5229 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5230 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5231 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5232 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5233 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5234 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5235 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5236 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5237
5238 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5239 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5240 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5241 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5242 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5243
5244 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5245 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5246 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5247 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5248
5249 /* (1) - may have excess leds enabled on MSB */
5250
5251 /* Defaults (order matters, keep last, don't reorder!) */
5252 { /* Lenovo */
5253 .vendor = PCI_VENDOR_ID_LENOVO,
5254 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5255 .quirks = 0x1fffU,
5256 },
5257 { /* IBM ThinkPads with no EC version string */
5258 .vendor = PCI_VENDOR_ID_IBM,
5259 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5260 .quirks = 0x00ffU,
5261 },
5262 { /* IBM ThinkPads with EC version string */
5263 .vendor = PCI_VENDOR_ID_IBM,
5264 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5265 .quirks = 0x00bfU,
5266 },
5267};
5268
5269#undef TPACPI_LEDQ_IBM
5270#undef TPACPI_LEDQ_LNV
5271
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005272static enum led_access_mode __init led_init_detect_mode(void)
5273{
5274 acpi_status status;
5275
5276 if (tpacpi_is_ibm()) {
5277 /* 570 */
5278 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5279 if (ACPI_SUCCESS(status))
5280 return TPACPI_LED_570;
5281
5282 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5283 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5284 if (ACPI_SUCCESS(status))
5285 return TPACPI_LED_OLD;
5286 }
5287
5288 /* most others */
5289 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5290 if (ACPI_SUCCESS(status))
5291 return TPACPI_LED_NEW;
5292
5293 /* R30, R31, and unknown firmwares */
5294 led_handle = NULL;
5295 return TPACPI_LED_NONE;
5296}
5297
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005298static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005299{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005300 unsigned int i;
5301 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005302 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005303
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005304 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5305
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005306 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005307
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005308 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5309 str_supported(led_supported), led_supported);
5310
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005311 if (led_supported == TPACPI_LED_NONE)
5312 return 1;
5313
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005314 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5315 GFP_KERNEL);
5316 if (!tpacpi_leds) {
5317 printk(TPACPI_ERR "Out of memory for LED data\n");
5318 return -ENOMEM;
5319 }
5320
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005321 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5322 ARRAY_SIZE(led_useful_qtable));
5323
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005324 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005325 if (!tpacpi_is_led_restricted(i) &&
5326 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005327 rc = tpacpi_init_led(i);
5328 if (rc < 0) {
5329 led_exit();
5330 return rc;
5331 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005332 }
5333 }
5334
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005335#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005336 printk(TPACPI_NOTICE
5337 "warning: userspace override of important "
5338 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005339#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005340 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005341}
5342
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005343#define str_led_status(s) \
5344 ((s) == TPACPI_LED_OFF ? "off" : \
5345 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005346
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005347static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005349 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005350 seq_printf(m, "status:\t\tnot supported\n");
5351 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005352 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005353 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005354
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005355 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005356 /* 570 */
5357 int i, status;
5358 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005359 status = led_get_status(i);
5360 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005361 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005362 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005363 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005364 }
5365 }
5366
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005367 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005368 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371}
5372
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005373static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374{
5375 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005376 int led, rc;
5377 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005378
5379 if (!led_supported)
5380 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381
5382 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005383 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 return -EINVAL;
5385
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005386 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005387 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005389 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005390 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005391 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005392 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005393 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005394 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005395
5396 rc = led_set_status(led, s);
5397 if (rc < 0)
5398 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 }
5400
5401 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005402}
5403
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005404static struct ibm_struct led_driver_data = {
5405 .name = "led",
5406 .read = led_read,
5407 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005408 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005409};
5410
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005411/*************************************************************************
5412 * Beep subdriver
5413 */
5414
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005415TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005416
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005417#define TPACPI_BEEP_Q1 0x0001
5418
5419static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5420 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5421 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5422};
5423
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005424static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005425{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005426 unsigned long quirks;
5427
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005428 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5429
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005430 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005431
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005432 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5433 str_supported(beep_handle != NULL));
5434
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005435 quirks = tpacpi_check_quirks(beep_quirk_table,
5436 ARRAY_SIZE(beep_quirk_table));
5437
5438 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5439
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005440 return (beep_handle)? 0 : 1;
5441}
5442
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005443static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005445 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005446 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005447 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005448 seq_printf(m, "status:\t\tsupported\n");
5449 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453}
5454
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005455static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456{
5457 char *cmd;
5458 int beep_cmd;
5459
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005460 if (!beep_handle)
5461 return -ENODEV;
5462
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005464 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5465 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 /* beep_cmd set */
5467 } else
5468 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005469 if (tp_features.beep_needs_two_args) {
5470 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5471 beep_cmd, 0))
5472 return -EIO;
5473 } else {
5474 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5475 beep_cmd))
5476 return -EIO;
5477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 }
5479
5480 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005481}
5482
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005483static struct ibm_struct beep_driver_data = {
5484 .name = "beep",
5485 .read = beep_read,
5486 .write = beep_write,
5487};
5488
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005489/*************************************************************************
5490 * Thermal subdriver
5491 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005492
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005493enum thermal_access_mode {
5494 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5495 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5496 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5497 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5498 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5499};
5500
5501enum { /* TPACPI_THERMAL_TPEC_* */
5502 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5503 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5504 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005505
5506 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005507};
5508
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005509
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005510#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5511struct ibm_thermal_sensors_struct {
5512 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5513};
5514
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005515static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005516
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005517/* idx is zero-based */
5518static int thermal_get_sensor(int idx, s32 *value)
5519{
5520 int t;
5521 s8 tmp;
5522 char tmpi[5];
5523
5524 t = TP_EC_THERMAL_TMP0;
5525
5526 switch (thermal_read_mode) {
5527#if TPACPI_MAX_THERMAL_SENSORS >= 16
5528 case TPACPI_THERMAL_TPEC_16:
5529 if (idx >= 8 && idx <= 15) {
5530 t = TP_EC_THERMAL_TMP8;
5531 idx -= 8;
5532 }
5533 /* fallthrough */
5534#endif
5535 case TPACPI_THERMAL_TPEC_8:
5536 if (idx <= 7) {
5537 if (!acpi_ec_read(t + idx, &tmp))
5538 return -EIO;
5539 *value = tmp * 1000;
5540 return 0;
5541 }
5542 break;
5543
5544 case TPACPI_THERMAL_ACPI_UPDT:
5545 if (idx <= 7) {
5546 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5547 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5548 return -EIO;
5549 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5550 return -EIO;
5551 *value = (t - 2732) * 100;
5552 return 0;
5553 }
5554 break;
5555
5556 case TPACPI_THERMAL_ACPI_TMP07:
5557 if (idx <= 7) {
5558 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5559 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5560 return -EIO;
5561 if (t > 127 || t < -127)
5562 t = TP_EC_THERMAL_TMP_NA;
5563 *value = t * 1000;
5564 return 0;
5565 }
5566 break;
5567
5568 case TPACPI_THERMAL_NONE:
5569 default:
5570 return -ENOSYS;
5571 }
5572
5573 return -EINVAL;
5574}
5575
5576static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5577{
5578 int res, i;
5579 int n;
5580
5581 n = 8;
5582 i = 0;
5583
5584 if (!s)
5585 return -EINVAL;
5586
5587 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5588 n = 16;
5589
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005590 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005591 res = thermal_get_sensor(i, &s->temp[i]);
5592 if (res)
5593 return res;
5594 }
5595
5596 return n;
5597}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005598
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005599static void thermal_dump_all_sensors(void)
5600{
5601 int n, i;
5602 struct ibm_thermal_sensors_struct t;
5603
5604 n = thermal_get_sensors(&t);
5605 if (n <= 0)
5606 return;
5607
5608 printk(TPACPI_NOTICE
5609 "temperatures (Celsius):");
5610
5611 for (i = 0; i < n; i++) {
5612 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5613 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5614 else
5615 printk(KERN_CONT " N/A");
5616 }
5617
5618 printk(KERN_CONT "\n");
5619}
5620
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005621/* sysfs temp##_input -------------------------------------------------- */
5622
5623static ssize_t thermal_temp_input_show(struct device *dev,
5624 struct device_attribute *attr,
5625 char *buf)
5626{
5627 struct sensor_device_attribute *sensor_attr =
5628 to_sensor_dev_attr(attr);
5629 int idx = sensor_attr->index;
5630 s32 value;
5631 int res;
5632
5633 res = thermal_get_sensor(idx, &value);
5634 if (res)
5635 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005636 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005637 return -ENXIO;
5638
5639 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5640}
5641
5642#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005643 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5644 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005645
5646static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5647 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5648 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5649 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5650 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5651 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5652 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5653 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5654 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5655 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5656 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5657 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5658 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5659 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5660 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5661 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5662 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5663};
5664
5665#define THERMAL_ATTRS(X) \
5666 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5667
5668static struct attribute *thermal_temp_input_attr[] = {
5669 THERMAL_ATTRS(8),
5670 THERMAL_ATTRS(9),
5671 THERMAL_ATTRS(10),
5672 THERMAL_ATTRS(11),
5673 THERMAL_ATTRS(12),
5674 THERMAL_ATTRS(13),
5675 THERMAL_ATTRS(14),
5676 THERMAL_ATTRS(15),
5677 THERMAL_ATTRS(0),
5678 THERMAL_ATTRS(1),
5679 THERMAL_ATTRS(2),
5680 THERMAL_ATTRS(3),
5681 THERMAL_ATTRS(4),
5682 THERMAL_ATTRS(5),
5683 THERMAL_ATTRS(6),
5684 THERMAL_ATTRS(7),
5685 NULL
5686};
5687
5688static const struct attribute_group thermal_temp_input16_group = {
5689 .attrs = thermal_temp_input_attr
5690};
5691
5692static const struct attribute_group thermal_temp_input8_group = {
5693 .attrs = &thermal_temp_input_attr[8]
5694};
5695
5696#undef THERMAL_SENSOR_ATTR_TEMP
5697#undef THERMAL_ATTRS
5698
5699/* --------------------------------------------------------------------- */
5700
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005701static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005702{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005703 u8 t, ta1, ta2;
5704 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005705 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005706 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005707
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005708 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5709
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005710 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005711
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005712 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005713 /*
5714 * Direct EC access mode: sensors at registers
5715 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5716 * non-implemented, thermal sensors return 0x80 when
5717 * not available
5718 */
5719
5720 ta1 = ta2 = 0;
5721 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005722 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005723 ta1 |= t;
5724 } else {
5725 ta1 = 0;
5726 break;
5727 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005728 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005729 ta2 |= t;
5730 } else {
5731 ta1 = 0;
5732 break;
5733 }
5734 }
5735 if (ta1 == 0) {
5736 /* This is sheer paranoia, but we handle it anyway */
5737 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005738 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005739 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005740 "falling back to ACPI TMPx access "
5741 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005742 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005743 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005744 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005745 "ThinkPad ACPI EC access misbehaving, "
5746 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005747 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005748 }
5749 } else {
5750 thermal_read_mode =
5751 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005752 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005753 }
5754 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005755 if (tpacpi_is_ibm() &&
5756 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005757 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005758 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005759 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005760 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005761 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005762 }
5763 } else {
5764 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005765 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005766 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005767
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005768 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5769 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5770 thermal_read_mode);
5771
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005772 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005773 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005774 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005775 &thermal_temp_input16_group);
5776 if (res)
5777 return res;
5778 break;
5779 case TPACPI_THERMAL_TPEC_8:
5780 case TPACPI_THERMAL_ACPI_TMP07:
5781 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005782 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005783 &thermal_temp_input8_group);
5784 if (res)
5785 return res;
5786 break;
5787 case TPACPI_THERMAL_NONE:
5788 default:
5789 return 1;
5790 }
5791
5792 return 0;
5793}
5794
5795static void thermal_exit(void)
5796{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005797 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005798 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005799 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005800 &thermal_temp_input16_group);
5801 break;
5802 case TPACPI_THERMAL_TPEC_8:
5803 case TPACPI_THERMAL_ACPI_TMP07:
5804 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005805 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08005806 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005807 break;
5808 case TPACPI_THERMAL_NONE:
5809 default:
5810 break;
5811 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005812}
5813
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005814static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005815{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005816 int n, i;
5817 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005818
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005819 n = thermal_get_sensors(&t);
5820 if (unlikely(n < 0))
5821 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005822
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005823 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005824
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005825 if (n > 0) {
5826 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005827 seq_printf(m, "%d ", t.temp[i] / 1000);
5828 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005829 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005830 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005831
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005832 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005833}
5834
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005835static struct ibm_struct thermal_driver_data = {
5836 .name = "thermal",
5837 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005838 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005839};
5840
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005841/*************************************************************************
5842 * EC Dump subdriver
5843 */
5844
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005845static u8 ecdump_regs[256];
5846
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005847static int ecdump_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005848{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005849 int i, j;
5850 u8 v;
5851
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005852 seq_printf(m, "EC "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005853 " +00 +01 +02 +03 +04 +05 +06 +07"
5854 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5855 for (i = 0; i < 256; i += 16) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005856 seq_printf(m, "EC 0x%02x:", i);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005857 for (j = 0; j < 16; j++) {
5858 if (!acpi_ec_read(i + j, &v))
5859 break;
5860 if (v != ecdump_regs[i + j])
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005861 seq_printf(m, " *%02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005862 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005863 seq_printf(m, " %02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005864 ecdump_regs[i + j] = v;
5865 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005866 seq_putc(m, '\n');
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005867 if (j != 16)
5868 break;
5869 }
5870
5871 /* These are way too dangerous to advertise openly... */
5872#if 0
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005873 seq_printf(m, "commands:\t0x<offset> 0x<value>"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005874 " (<offset> is 00-ff, <value> is 00-ff)\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005875 seq_printf(m, "commands:\t0x<offset> <value> "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005876 " (<offset> is 00-ff, <value> is 0-255)\n");
5877#endif
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005878 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005879}
5880
5881static int ecdump_write(char *buf)
5882{
5883 char *cmd;
5884 int i, v;
5885
5886 while ((cmd = next_cmd(&buf))) {
5887 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5888 /* i and v set */
5889 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5890 /* i and v set */
5891 } else
5892 return -EINVAL;
5893 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5894 if (!acpi_ec_write(i, v))
5895 return -EIO;
5896 } else
5897 return -EINVAL;
5898 }
5899
5900 return 0;
5901}
5902
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005903static struct ibm_struct ecdump_driver_data = {
5904 .name = "ecdump",
5905 .read = ecdump_read,
5906 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005907 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005908};
5909
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005910/*************************************************************************
5911 * Backlight/brightness subdriver
5912 */
Holger Macht8acb0252006-10-20 14:30:28 -07005913
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005914#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5915
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005916/*
5917 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5918 * CMOS NVRAM byte 0x5E, bits 0-3.
5919 *
5920 * EC HBRV (0x31) has the following layout
5921 * Bit 7: unknown function
5922 * Bit 6: unknown function
5923 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5924 * Bit 4: must be set to zero to avoid problems
5925 * Bit 3-0: backlight brightness level
5926 *
5927 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005928 *
5929 * WARNING: The X61 has been verified to use HBRV for something else, so
5930 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5931 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005932 */
5933
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005934enum {
5935 TP_EC_BACKLIGHT = 0x31,
5936
5937 /* TP_EC_BACKLIGHT bitmasks */
5938 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5939 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5940 TP_EC_BACKLIGHT_MAPSW = 0x20,
5941};
5942
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005943enum tpacpi_brightness_access_mode {
5944 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5945 TPACPI_BRGHT_MODE_EC, /* EC control */
5946 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5947 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5948 TPACPI_BRGHT_MODE_MAX
5949};
5950
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005951static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005952
5953static enum tpacpi_brightness_access_mode brightness_mode =
5954 TPACPI_BRGHT_MODE_MAX;
5955
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005956static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5957
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005958static struct mutex brightness_mutex;
5959
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005960/* NVRAM brightness access,
5961 * call with brightness_mutex held! */
5962static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005963{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005964 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005965
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005966 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5967 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5968 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03005969 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005970
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005971 return lnvram;
5972}
5973
5974static void tpacpi_brightness_checkpoint_nvram(void)
5975{
5976 u8 lec = 0;
5977 u8 b_nvram;
5978
5979 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5980 return;
5981
5982 vdbg_printk(TPACPI_DBG_BRGHT,
5983 "trying to checkpoint backlight level to NVRAM...\n");
5984
5985 if (mutex_lock_killable(&brightness_mutex) < 0)
5986 return;
5987
5988 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5989 goto unlock;
5990 lec &= TP_EC_BACKLIGHT_LVLMSK;
5991 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5992
5993 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5994 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5995 /* NVRAM needs update */
5996 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5997 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5998 b_nvram |= lec;
5999 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6000 dbg_printk(TPACPI_DBG_BRGHT,
6001 "updated NVRAM backlight level to %u (0x%02x)\n",
6002 (unsigned int) lec, (unsigned int) b_nvram);
6003 } else
6004 vdbg_printk(TPACPI_DBG_BRGHT,
6005 "NVRAM backlight level already is %u (0x%02x)\n",
6006 (unsigned int) lec, (unsigned int) b_nvram);
6007
6008unlock:
6009 mutex_unlock(&brightness_mutex);
6010}
6011
6012
6013/* call with brightness_mutex held! */
6014static int tpacpi_brightness_get_raw(int *status)
6015{
6016 u8 lec = 0;
6017
6018 switch (brightness_mode) {
6019 case TPACPI_BRGHT_MODE_UCMS_STEP:
6020 *status = tpacpi_brightness_nvram_get();
6021 return 0;
6022 case TPACPI_BRGHT_MODE_EC:
6023 case TPACPI_BRGHT_MODE_ECNVRAM:
6024 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006025 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006026 *status = lec;
6027 return 0;
6028 default:
6029 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006030 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006031}
6032
6033/* call with brightness_mutex held! */
6034/* do NOT call with illegal backlight level value */
6035static int tpacpi_brightness_set_ec(unsigned int value)
6036{
6037 u8 lec = 0;
6038
6039 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6040 return -EIO;
6041
6042 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6043 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6044 (value & TP_EC_BACKLIGHT_LVLMSK))))
6045 return -EIO;
6046
6047 return 0;
6048}
6049
6050/* call with brightness_mutex held! */
6051static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6052{
6053 int cmos_cmd, inc;
6054 unsigned int current_value, i;
6055
6056 current_value = tpacpi_brightness_nvram_get();
6057
6058 if (value == current_value)
6059 return 0;
6060
6061 cmos_cmd = (value > current_value) ?
6062 TP_CMOS_BRIGHTNESS_UP :
6063 TP_CMOS_BRIGHTNESS_DOWN;
6064 inc = (value > current_value) ? 1 : -1;
6065
6066 for (i = current_value; i != value; i += inc)
6067 if (issue_thinkpad_cmos_command(cmos_cmd))
6068 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006069
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006070 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006071}
6072
6073/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006074static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006075{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006076 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006077
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006078 if (value > bright_maxlvl || value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006079 return -EINVAL;
6080
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006081 vdbg_printk(TPACPI_DBG_BRGHT,
6082 "set backlight level to %d\n", value);
6083
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006084 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006085 if (res < 0)
6086 return res;
6087
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006088 switch (brightness_mode) {
6089 case TPACPI_BRGHT_MODE_EC:
6090 case TPACPI_BRGHT_MODE_ECNVRAM:
6091 res = tpacpi_brightness_set_ec(value);
6092 break;
6093 case TPACPI_BRGHT_MODE_UCMS_STEP:
6094 res = tpacpi_brightness_set_ucmsstep(value);
6095 break;
6096 default:
6097 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006098 }
6099
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006100 mutex_unlock(&brightness_mutex);
6101 return res;
6102}
6103
6104/* sysfs backlight class ----------------------------------------------- */
6105
6106static int brightness_update_status(struct backlight_device *bd)
6107{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006108 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006109 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6110 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006111 bd->props.brightness : 0;
6112
6113 dbg_printk(TPACPI_DBG_BRGHT,
6114 "backlight: attempt to set level to %d\n",
6115 level);
6116
6117 /* it is the backlight class's job (caller) to handle
6118 * EINTR and other errors properly */
6119 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006120}
Holger Macht8acb0252006-10-20 14:30:28 -07006121
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006122static int brightness_get(struct backlight_device *bd)
6123{
6124 int status, res;
6125
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006126 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006127 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006128 return 0;
6129
6130 res = tpacpi_brightness_get_raw(&status);
6131
6132 mutex_unlock(&brightness_mutex);
6133
6134 if (res < 0)
6135 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006136
6137 return status & TP_EC_BACKLIGHT_LVLMSK;
6138}
6139
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006140static void tpacpi_brightness_notify_change(void)
6141{
6142 backlight_force_update(ibm_backlight_device,
6143 BACKLIGHT_UPDATE_HOTKEY);
6144}
6145
Richard Purdie599a52d2007-02-10 23:07:48 +00006146static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006147 .get_brightness = brightness_get,
6148 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006149};
6150
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006151/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006152
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006153static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6154{
6155 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6156 union acpi_object *obj;
6157 int rc;
6158
6159 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
6160 obj = (union acpi_object *)buffer.pointer;
6161 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6162 printk(TPACPI_ERR "Unknown _BCL data, "
6163 "please report this to %s\n", TPACPI_MAIL);
6164 rc = 0;
6165 } else {
6166 rc = obj->package.count;
6167 }
6168 } else {
6169 return 0;
6170 }
6171
6172 kfree(buffer.pointer);
6173 return rc;
6174}
6175
6176static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
6177 u32 lvl, void *context, void **rv)
6178{
6179 char name[ACPI_PATH_SEGMENT_LENGTH];
6180 struct acpi_buffer buffer = { sizeof(name), &name };
6181
6182 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
6183 !strncmp("_BCL", name, sizeof(name) - 1)) {
6184 BUG_ON(!rv || !*rv);
6185 **(int **)rv = tpacpi_query_bcl_levels(handle);
6186 return AE_CTRL_TERMINATE;
6187 } else {
6188 return AE_OK;
6189 }
6190}
6191
6192/*
6193 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6194 */
6195static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6196{
6197 int status;
6198 int bcl_levels = 0;
6199 void *bcl_ptr = &bcl_levels;
6200
6201 if (!vid_handle)
6202 TPACPI_ACPIHANDLE_INIT(vid);
6203
6204 if (!vid_handle)
6205 return 0;
6206
6207 /*
6208 * Search for a _BCL method, and execute it. This is safe on all
6209 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
6210 * BIOS in ACPI backlight control mode. We do NOT have to care
6211 * about calling the _BCL method in an enabled video device, any
6212 * will do for our purposes.
6213 */
6214
6215 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
6216 tpacpi_acpi_walk_find_bcl, NULL, NULL,
6217 &bcl_ptr);
6218
6219 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
6220 tp_features.bright_acpimode = 1;
6221 return bcl_levels - 2;
6222 }
6223
6224 return 0;
6225}
6226
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006227/*
6228 * These are only useful for models that have only one possibility
6229 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6230 * these quirks.
6231 */
6232#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6233#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6234#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6235
6236static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6237 /* Models with ATI GPUs known to require ECNVRAM mode */
6238 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6239
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006240 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006241 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006242 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006243 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006244 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6245
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006246 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006247 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006248 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6249 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006250
6251 /* Models with Intel GMA900 */
6252 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6253 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6254 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6255};
6256
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006257/*
6258 * Returns < 0 for error, otherwise sets tp_features.bright_*
6259 * and bright_maxlvl.
6260 */
6261static void __init tpacpi_detect_brightness_capabilities(void)
6262{
6263 unsigned int b;
6264
6265 vdbg_printk(TPACPI_DBG_INIT,
6266 "detecting firmware brightness interface capabilities\n");
6267
6268 /* we could run a quirks check here (same table used by
6269 * brightness_init) if needed */
6270
6271 /*
6272 * We always attempt to detect acpi support, so as to switch
6273 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6274 * going to publish a backlight interface
6275 */
6276 b = tpacpi_check_std_acpi_brightness_support();
6277 switch (b) {
6278 case 16:
6279 bright_maxlvl = 15;
6280 printk(TPACPI_INFO
6281 "detected a 16-level brightness capable ThinkPad\n");
6282 break;
6283 case 8:
6284 case 0:
6285 bright_maxlvl = 7;
6286 printk(TPACPI_INFO
6287 "detected a 8-level brightness capable ThinkPad\n");
6288 break;
6289 default:
6290 printk(TPACPI_ERR
6291 "Unsupported brightness interface, "
6292 "please contact %s\n", TPACPI_MAIL);
6293 tp_features.bright_unkfw = 1;
6294 bright_maxlvl = b - 1;
6295 }
6296}
6297
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006298static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006299{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006300 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006301 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006302 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006303
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006304 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6305
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006306 mutex_init(&brightness_mutex);
6307
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006308 quirks = tpacpi_check_quirks(brightness_quirk_table,
6309 ARRAY_SIZE(brightness_quirk_table));
6310
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006311 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006312
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006313 /* if it is unknown, we don't handle it: it wouldn't be safe */
6314 if (tp_features.bright_unkfw)
6315 return 1;
6316
6317 if (tp_features.bright_acpimode) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006318 if (acpi_video_backlight_support()) {
6319 if (brightness_enable > 1) {
6320 printk(TPACPI_NOTICE
6321 "Standard ACPI backlight interface "
6322 "available, not loading native one.\n");
6323 return 1;
6324 } else if (brightness_enable == 1) {
6325 printk(TPACPI_NOTICE
6326 "Backlight control force enabled, even if standard "
6327 "ACPI backlight interface is available\n");
6328 }
6329 } else {
6330 if (brightness_enable > 1) {
6331 printk(TPACPI_NOTICE
6332 "Standard ACPI backlight interface not "
6333 "available, thinkpad_acpi native "
6334 "brightness control enabled\n");
6335 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02006336 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006337 }
6338
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006339 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006340 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006341 "brightness support disabled by "
6342 "module parameter\n");
6343 return 1;
6344 }
6345
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006346 /*
6347 * Check for module parameter bogosity, note that we
6348 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6349 * able to detect "unspecified"
6350 */
6351 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006352 return -EINVAL;
6353
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006354 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6355 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6356 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006357 if (quirks & TPACPI_BRGHT_Q_EC)
6358 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6359 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006360 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6361
6362 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006363 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006364 brightness_mode);
6365 }
6366
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006367 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006368 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006369 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6370 brightness_mode == TPACPI_BRGHT_MODE_EC))
6371 return -EINVAL;
6372
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006373 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006374 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006375
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006376 memset(&props, 0, sizeof(struct backlight_properties));
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006377 props.max_brightness = bright_maxlvl;
6378 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006379 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6380 NULL, NULL,
6381 &ibm_backlight_data,
6382 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006383 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006384 int rc = PTR_ERR(ibm_backlight_device);
6385 ibm_backlight_device = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006386 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006387 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006388 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006389 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6390 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006391
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006392 if (quirks & TPACPI_BRGHT_Q_ASK) {
6393 printk(TPACPI_NOTICE
6394 "brightness: will use unverified default: "
6395 "brightness_mode=%d\n", brightness_mode);
6396 printk(TPACPI_NOTICE
6397 "brightness: please report to %s whether it works well "
6398 "or not on your ThinkPad\n", TPACPI_MAIL);
6399 }
6400
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006401 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006402
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006403 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6404 "brightness: registering brightness hotkeys "
6405 "as change notification\n");
6406 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6407 | TP_ACPI_HKEY_BRGHTUP_MASK
6408 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006409 return 0;
6410}
6411
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006412static void brightness_suspend(pm_message_t state)
6413{
6414 tpacpi_brightness_checkpoint_nvram();
6415}
6416
6417static void brightness_shutdown(void)
6418{
6419 tpacpi_brightness_checkpoint_nvram();
6420}
6421
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006422static void brightness_exit(void)
6423{
6424 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006425 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006426 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006427 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006428 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006429
6430 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006431}
6432
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006433static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006434{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006435 int level;
6436
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006437 level = brightness_get(NULL);
6438 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006439 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006440 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006441 seq_printf(m, "level:\t\t%d\n", level);
6442 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006443 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6444 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006445 }
6446
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006447 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006448}
6449
6450static int brightness_write(char *buf)
6451{
6452 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006453 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006454 char *cmd;
6455
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006456 level = brightness_get(NULL);
6457 if (level < 0)
6458 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006459
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006460 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006461 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006462 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006463 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006464 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006465 if (level > 0)
6466 level--;
6467 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006468 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006469 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006470 } else
6471 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006472 }
6473
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006474 tpacpi_disclose_usertask("procfs brightness",
6475 "set level to %d\n", level);
6476
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006477 /*
6478 * Now we know what the final level should be, so we try to set it.
6479 * Doing it this way makes the syscall restartable in case of EINTR
6480 */
6481 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006482 if (!rc && ibm_backlight_device)
6483 backlight_force_update(ibm_backlight_device,
6484 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006485 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006486}
6487
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006488static struct ibm_struct brightness_driver_data = {
6489 .name = "brightness",
6490 .read = brightness_read,
6491 .write = brightness_write,
6492 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006493 .suspend = brightness_suspend,
6494 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006495};
6496
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006497/*************************************************************************
6498 * Volume subdriver
6499 */
6500
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006501/*
6502 * IBM ThinkPads have a simple volume controller with MUTE gating.
6503 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6504 *
6505 * Since the *61 series (and probably also the later *60 series), Lenovo
6506 * ThinkPads only implement the MUTE gate.
6507 *
6508 * EC register 0x30
6509 * Bit 6: MUTE (1 mutes sound)
6510 * Bit 3-0: Volume
6511 * Other bits should be zero as far as we know.
6512 *
6513 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6514 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6515 * such as bit 7 which is used to detect repeated presses of MUTE,
6516 * and we leave them unchanged.
6517 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006518
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006519#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6520
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006521#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6522#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6523#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6524
Henrique de Moraes Holschuhead510c2009-12-26 22:52:13 -02006525static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006526static char *alsa_id = "ThinkPadEC";
6527static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6528
6529struct tpacpi_alsa_data {
6530 struct snd_card *card;
6531 struct snd_ctl_elem_id *ctl_mute_id;
6532 struct snd_ctl_elem_id *ctl_vol_id;
6533};
6534
6535static struct snd_card *alsa_card;
6536
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006537enum {
6538 TP_EC_AUDIO = 0x30,
6539
6540 /* TP_EC_AUDIO bits */
6541 TP_EC_AUDIO_MUTESW = 6,
6542
6543 /* TP_EC_AUDIO bitmasks */
6544 TP_EC_AUDIO_LVL_MSK = 0x0F,
6545 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6546
6547 /* Maximum volume */
6548 TP_EC_VOLUME_MAX = 14,
6549};
6550
6551enum tpacpi_volume_access_mode {
6552 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6553 TPACPI_VOL_MODE_EC, /* Pure EC control */
6554 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6555 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6556 TPACPI_VOL_MODE_MAX
6557};
6558
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006559enum tpacpi_volume_capabilities {
6560 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6561 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6562 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6563 TPACPI_VOL_CAP_MAX
6564};
6565
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006566static enum tpacpi_volume_access_mode volume_mode =
6567 TPACPI_VOL_MODE_MAX;
6568
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006569static enum tpacpi_volume_capabilities volume_capabilities;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006570static int volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006571
6572/*
6573 * Used to syncronize writers to TP_EC_AUDIO and
6574 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6575 */
6576static struct mutex volume_mutex;
6577
6578static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006579{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006580 u8 lec = 0;
6581 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006582 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006583
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006584 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6585 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006586 if (!volume_control_allowed)
6587 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006588
6589 vdbg_printk(TPACPI_DBG_MIXER,
6590 "trying to checkpoint mixer state to NVRAM...\n");
6591
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006592 if (tp_features.mixer_no_level_control)
6593 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6594 else
6595 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6596
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006597 if (mutex_lock_killable(&volume_mutex) < 0)
6598 return;
6599
6600 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6601 goto unlock;
6602 lec &= ec_mask;
6603 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6604
6605 if (lec != (b_nvram & ec_mask)) {
6606 /* NVRAM needs update */
6607 b_nvram &= ~ec_mask;
6608 b_nvram |= lec;
6609 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6610 dbg_printk(TPACPI_DBG_MIXER,
6611 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6612 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006613 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006614 vdbg_printk(TPACPI_DBG_MIXER,
6615 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6616 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006617 }
6618
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006619unlock:
6620 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006621}
6622
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006623static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006624{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006625 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006626
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006627 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6628 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006629
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006630 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006631
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006632 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006633
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006634 return 0;
6635}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006636
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006637static int volume_get_status(u8 *status)
6638{
6639 return volume_get_status_ec(status);
6640}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006641
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006642static int volume_set_status_ec(const u8 status)
6643{
6644 if (!acpi_ec_write(TP_EC_AUDIO, status))
6645 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006646
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006647 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6648
6649 return 0;
6650}
6651
6652static int volume_set_status(const u8 status)
6653{
6654 return volume_set_status_ec(status);
6655}
6656
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006657/* returns < 0 on error, 0 on no change, 1 on change */
6658static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006659{
6660 int rc;
6661 u8 s, n;
6662
6663 if (mutex_lock_killable(&volume_mutex) < 0)
6664 return -EINTR;
6665
6666 rc = volume_get_status_ec(&s);
6667 if (rc)
6668 goto unlock;
6669
6670 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6671 s & ~TP_EC_AUDIO_MUTESW_MSK;
6672
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006673 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006674 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006675 if (!rc)
6676 rc = 1;
6677 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006678
6679unlock:
6680 mutex_unlock(&volume_mutex);
6681 return rc;
6682}
6683
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006684static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006685{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006686 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006687 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006688 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006689}
6690
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006691static int volume_set_mute(const bool mute)
6692{
6693 int rc;
6694
6695 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6696 (mute) ? "" : "un");
6697
6698 rc = __volume_set_mute_ec(mute);
6699 return (rc < 0) ? rc : 0;
6700}
6701
6702/* returns < 0 on error, 0 on no change, 1 on change */
6703static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006704{
6705 int rc;
6706 u8 s, n;
6707
6708 if (vol > TP_EC_VOLUME_MAX)
6709 return -EINVAL;
6710
6711 if (mutex_lock_killable(&volume_mutex) < 0)
6712 return -EINTR;
6713
6714 rc = volume_get_status_ec(&s);
6715 if (rc)
6716 goto unlock;
6717
6718 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6719
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006720 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006721 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006722 if (!rc)
6723 rc = 1;
6724 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006725
6726unlock:
6727 mutex_unlock(&volume_mutex);
6728 return rc;
6729}
6730
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006731static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006732{
6733 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006734 "ALSA: trying to set volume level to %hu\n", vol);
6735 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006736}
6737
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006738static void volume_alsa_notify_change(void)
6739{
6740 struct tpacpi_alsa_data *d;
6741
6742 if (alsa_card && alsa_card->private_data) {
6743 d = alsa_card->private_data;
6744 if (d->ctl_mute_id)
6745 snd_ctl_notify(alsa_card,
6746 SNDRV_CTL_EVENT_MASK_VALUE,
6747 d->ctl_mute_id);
6748 if (d->ctl_vol_id)
6749 snd_ctl_notify(alsa_card,
6750 SNDRV_CTL_EVENT_MASK_VALUE,
6751 d->ctl_vol_id);
6752 }
6753}
6754
6755static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6756 struct snd_ctl_elem_info *uinfo)
6757{
6758 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6759 uinfo->count = 1;
6760 uinfo->value.integer.min = 0;
6761 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6762 return 0;
6763}
6764
6765static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6766 struct snd_ctl_elem_value *ucontrol)
6767{
6768 u8 s;
6769 int rc;
6770
6771 rc = volume_get_status(&s);
6772 if (rc < 0)
6773 return rc;
6774
6775 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6776 return 0;
6777}
6778
6779static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6780 struct snd_ctl_elem_value *ucontrol)
6781{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006782 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6783 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006784 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006785}
6786
6787#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6788
6789static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6790 struct snd_ctl_elem_value *ucontrol)
6791{
6792 u8 s;
6793 int rc;
6794
6795 rc = volume_get_status(&s);
6796 if (rc < 0)
6797 return rc;
6798
6799 ucontrol->value.integer.value[0] =
6800 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6801 return 0;
6802}
6803
6804static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6805 struct snd_ctl_elem_value *ucontrol)
6806{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006807 tpacpi_disclose_usertask("ALSA", "%smute\n",
6808 ucontrol->value.integer.value[0] ?
6809 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006810 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006811}
6812
6813static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6814 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6815 .name = "Console Playback Volume",
6816 .index = 0,
6817 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6818 .info = volume_alsa_vol_info,
6819 .get = volume_alsa_vol_get,
6820};
6821
6822static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6823 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6824 .name = "Console Playback Switch",
6825 .index = 0,
6826 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6827 .info = volume_alsa_mute_info,
6828 .get = volume_alsa_mute_get,
6829};
6830
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006831static void volume_suspend(pm_message_t state)
6832{
6833 tpacpi_volume_checkpoint_nvram();
6834}
6835
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006836static void volume_resume(void)
6837{
6838 volume_alsa_notify_change();
6839}
6840
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006841static void volume_shutdown(void)
6842{
6843 tpacpi_volume_checkpoint_nvram();
6844}
6845
6846static void volume_exit(void)
6847{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006848 if (alsa_card) {
6849 snd_card_free(alsa_card);
6850 alsa_card = NULL;
6851 }
6852
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006853 tpacpi_volume_checkpoint_nvram();
6854}
6855
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006856static int __init volume_create_alsa_mixer(void)
6857{
6858 struct snd_card *card;
6859 struct tpacpi_alsa_data *data;
6860 struct snd_kcontrol *ctl_vol;
6861 struct snd_kcontrol *ctl_mute;
6862 int rc;
6863
6864 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6865 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006866 if (rc < 0 || !card) {
6867 printk(TPACPI_ERR
6868 "Failed to create ALSA card structures: %d\n", rc);
6869 return 1;
6870 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006871
6872 BUG_ON(!card->private_data);
6873 data = card->private_data;
6874 data->card = card;
6875
6876 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6877 sizeof(card->driver));
6878 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6879 sizeof(card->shortname));
6880 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6881 (thinkpad_id.ec_version_str) ?
6882 thinkpad_id.ec_version_str : "(unknown)");
6883 snprintf(card->longname, sizeof(card->longname),
6884 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6885 (thinkpad_id.ec_version_str) ?
6886 thinkpad_id.ec_version_str : "unknown");
6887
6888 if (volume_control_allowed) {
6889 volume_alsa_control_vol.put = volume_alsa_vol_put;
6890 volume_alsa_control_vol.access =
6891 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6892
6893 volume_alsa_control_mute.put = volume_alsa_mute_put;
6894 volume_alsa_control_mute.access =
6895 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6896 }
6897
6898 if (!tp_features.mixer_no_level_control) {
6899 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6900 rc = snd_ctl_add(card, ctl_vol);
6901 if (rc < 0) {
6902 printk(TPACPI_ERR
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006903 "Failed to create ALSA volume control: %d\n",
6904 rc);
6905 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006906 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006907 data->ctl_vol_id = &ctl_vol->id;
6908 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006909
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006910 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6911 rc = snd_ctl_add(card, ctl_mute);
6912 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006913 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6914 rc);
6915 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006916 }
6917 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006918
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006919 snd_card_set_dev(card, &tpacpi_pdev->dev);
6920 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006921 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006922 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6923 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006924 }
6925
6926 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006927 return 0;
6928
6929err_exit:
6930 snd_card_free(card);
6931 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006932}
6933
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006934#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6935#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6936
6937static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6938 /* Whitelist volume level on all IBM by default */
6939 { .vendor = PCI_VENDOR_ID_IBM,
6940 .bios = TPACPI_MATCH_ANY,
6941 .ec = TPACPI_MATCH_ANY,
6942 .quirks = TPACPI_VOL_Q_LEVEL },
6943
6944 /* Lenovo models with volume control (needs confirmation) */
6945 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6946 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6947 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6948 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6949 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6950 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6951 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6952
6953 /* Whitelist mute-only on all Lenovo by default */
6954 { .vendor = PCI_VENDOR_ID_LENOVO,
6955 .bios = TPACPI_MATCH_ANY,
6956 .ec = TPACPI_MATCH_ANY,
6957 .quirks = TPACPI_VOL_Q_MUTEONLY }
6958};
6959
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006960static int __init volume_init(struct ibm_init_struct *iibm)
6961{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006962 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006963 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006964
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006965 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6966
6967 mutex_init(&volume_mutex);
6968
6969 /*
6970 * Check for module parameter bogosity, note that we
6971 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6972 * able to detect "unspecified"
6973 */
6974 if (volume_mode > TPACPI_VOL_MODE_MAX)
6975 return -EINVAL;
6976
6977 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6978 printk(TPACPI_ERR
6979 "UCMS step volume mode not implemented, "
6980 "please contact %s\n", TPACPI_MAIL);
6981 return 1;
6982 }
6983
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006984 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6985 return -EINVAL;
6986
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006987 /*
6988 * The ALSA mixer is our primary interface.
6989 * When disabled, don't install the subdriver at all
6990 */
6991 if (!alsa_enable) {
6992 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6993 "ALSA mixer disabled by parameter, "
6994 "not loading volume subdriver...\n");
6995 return 1;
6996 }
6997
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006998 quirks = tpacpi_check_quirks(volume_quirk_table,
6999 ARRAY_SIZE(volume_quirk_table));
7000
7001 switch (volume_capabilities) {
7002 case TPACPI_VOL_CAP_AUTO:
7003 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7004 tp_features.mixer_no_level_control = 1;
7005 else if (quirks & TPACPI_VOL_Q_LEVEL)
7006 tp_features.mixer_no_level_control = 0;
7007 else
7008 return 1; /* no mixer */
7009 break;
7010 case TPACPI_VOL_CAP_VOLMUTE:
7011 tp_features.mixer_no_level_control = 0;
7012 break;
7013 case TPACPI_VOL_CAP_MUTEONLY:
7014 tp_features.mixer_no_level_control = 1;
7015 break;
7016 default:
7017 return 1;
7018 }
7019
7020 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7021 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7022 "using user-supplied volume_capabilities=%d\n",
7023 volume_capabilities);
7024
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007025 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7026 volume_mode == TPACPI_VOL_MODE_MAX) {
7027 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7028
7029 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7030 "driver auto-selected volume_mode=%d\n",
7031 volume_mode);
7032 } else {
7033 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7034 "using user-supplied volume_mode=%d\n",
7035 volume_mode);
7036 }
7037
7038 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007039 "mute is supported, volume control is %s\n",
7040 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007041
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007042 rc = volume_create_alsa_mixer();
7043 if (rc) {
7044 printk(TPACPI_ERR
7045 "Could not create the ALSA mixer interface\n");
7046 return rc;
7047 }
7048
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007049 printk(TPACPI_INFO
7050 "Console audio control enabled, mode: %s\n",
7051 (volume_control_allowed) ?
7052 "override (read/write)" :
7053 "monitor (read only)");
7054
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007055 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7056 "registering volume hotkeys as change notification\n");
7057 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7058 | TP_ACPI_HKEY_VOLUP_MASK
7059 | TP_ACPI_HKEY_VOLDWN_MASK
7060 | TP_ACPI_HKEY_MUTE_MASK);
7061
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007062 return 0;
7063}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007064
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007065static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007066{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007067 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007068
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007069 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007070 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007071 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007072 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007073 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007074 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007075 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007076 status & TP_EC_AUDIO_LVL_MSK);
7077
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007078 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007079 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007080
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007081 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007082 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007083 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007084 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007085 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007086 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007087 "commands:\tlevel <level>"
7088 " (<level> is 0-%d)\n",
7089 TP_EC_VOLUME_MAX);
7090 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007091 }
7092 }
7093
7094 return 0;
7095}
7096
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007097static int volume_write(char *buf)
7098{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007099 u8 s;
7100 u8 new_level, new_mute;
7101 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007102 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007103 int rc;
7104
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007105 /*
7106 * We do allow volume control at driver startup, so that the
7107 * user can set initial state through the volume=... parameter hack.
7108 */
7109 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7110 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7111 tp_warned.volume_ctrl_forbidden = 1;
7112 printk(TPACPI_NOTICE
7113 "Console audio control in monitor mode, "
7114 "changes are not allowed.\n");
7115 printk(TPACPI_NOTICE
7116 "Use the volume_control=1 module parameter "
7117 "to enable volume control\n");
7118 }
7119 return -EPERM;
7120 }
7121
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007122 rc = volume_get_status(&s);
7123 if (rc < 0)
7124 return rc;
7125
7126 new_level = s & TP_EC_AUDIO_LVL_MSK;
7127 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007128
7129 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007130 if (!tp_features.mixer_no_level_control) {
7131 if (strlencmp(cmd, "up") == 0) {
7132 if (new_mute)
7133 new_mute = 0;
7134 else if (new_level < TP_EC_VOLUME_MAX)
7135 new_level++;
7136 continue;
7137 } else if (strlencmp(cmd, "down") == 0) {
7138 if (new_mute)
7139 new_mute = 0;
7140 else if (new_level > 0)
7141 new_level--;
7142 continue;
7143 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7144 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7145 new_level = l;
7146 continue;
7147 }
7148 }
7149 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007150 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007151 else if (strlencmp(cmd, "unmute") == 0)
7152 new_mute = 0;
7153 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007154 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007155 }
7156
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007157 if (tp_features.mixer_no_level_control) {
7158 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7159 new_mute ? "" : "un");
7160 rc = volume_set_mute(!!new_mute);
7161 } else {
7162 tpacpi_disclose_usertask("procfs volume",
7163 "%smute and set level to %d\n",
7164 new_mute ? "" : "un", new_level);
7165 rc = volume_set_status(new_mute | new_level);
7166 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007167 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007168
7169 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007170}
7171
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007172static struct ibm_struct volume_driver_data = {
7173 .name = "volume",
7174 .read = volume_read,
7175 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007176 .exit = volume_exit,
7177 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007178 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007179 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007180};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007181
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007182#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7183
7184#define alsa_card NULL
7185
7186static void inline volume_alsa_notify_change(void)
7187{
7188}
7189
7190static int __init volume_init(struct ibm_init_struct *iibm)
7191{
7192 printk(TPACPI_INFO
7193 "volume: disabled as there is no ALSA support in this kernel\n");
7194
7195 return 1;
7196}
7197
7198static struct ibm_struct volume_driver_data = {
7199 .name = "volume",
7200};
7201
7202#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7203
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007204/*************************************************************************
7205 * Fan subdriver
7206 */
7207
7208/*
7209 * FAN ACCESS MODES
7210 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007211 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007212 * ACPI GFAN method: returns fan level
7213 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007214 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007215 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007216 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007217 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007218 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7219 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007220 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7221 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007222 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007223 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007224 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7225 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007226 *
7227 * Fan speed changes of any sort (including those caused by the
7228 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007229 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007230 * limited.
7231 *
7232 * Reading is not available if GFAN exists.
7233 * Writing is not available if SFAN exists.
7234 *
7235 * Bits
7236 * 7 automatic mode engaged;
7237 * (default operation mode of the ThinkPad)
7238 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007239 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007240 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007241 * the tachometer while the fan controller ramps up
7242 * the speed (which can take up to a few *minutes*).
7243 * Speeds up fan to 100% duty-cycle, which is far above
7244 * the standard RPM levels. It is not impossible that
7245 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007246 * 5-3 unused in some models. Extra bits for fan level
7247 * in others, but still useless as all values above
7248 * 7 map to the same speed as level 7 in these models.
7249 * 2-0 fan level (0..7 usually)
7250 * 0x00 = stop
7251 * 0x07 = max (set when temperatures critical)
7252 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007253 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007254 *
7255 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7256 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7257 * does so, its initial value is meaningless (0x07).
7258 *
7259 * For firmware bugs, refer to:
7260 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7261 *
7262 * ----
7263 *
7264 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7265 * Main fan tachometer reading (in RPM)
7266 *
7267 * This register is present on all ThinkPads with a new-style EC, and
7268 * it is known not to be present on the A21m/e, and T22, as there is
7269 * something else in offset 0x84 according to the ACPI DSDT. Other
7270 * ThinkPads from this same time period (and earlier) probably lack the
7271 * tachometer as well.
7272 *
Nick Andrew877d0312009-01-26 11:06:57 +01007273 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007274 * was never fixed by IBM to report the EC firmware version string
7275 * probably support the tachometer (like the early X models), so
7276 * detecting it is quite hard. We need more data to know for sure.
7277 *
7278 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7279 * might result.
7280 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007281 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7282 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007283 *
7284 * For firmware bugs, refer to:
7285 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7286 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007287 * ----
7288 *
7289 * ThinkPad EC register 0x31 bit 0 (only on select models)
7290 *
7291 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7292 * show the speed of the main fan. When bit 0 of EC register 0x31
7293 * is one, the tachometer registers show the speed of the auxiliary
7294 * fan.
7295 *
7296 * Fan control seems to affect both fans, regardless of the state
7297 * of this bit.
7298 *
7299 * So far, only the firmware for the X60/X61 non-tablet versions
7300 * seem to support this (firmware TP-7M).
7301 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007302 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007303 * ThinkPad X31, X40, X41. Not available in the X60.
7304 *
7305 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7306 * high speed. ACPI DSDT seems to map these three speeds to levels
7307 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7308 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7309 *
7310 * The speeds are stored on handles
7311 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7312 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007313 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007314 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7315 *
7316 * ACPI DSDT switches which set is in use depending on various
7317 * factors.
7318 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007319 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007320 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7321 * but the ACPI tables just mention level 7.
7322 */
7323
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007324enum { /* Fan control constants */
7325 fan_status_offset = 0x2f, /* EC register 0x2f */
7326 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7327 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007328 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7329 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007330
7331 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7332 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7333
7334 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7335};
7336
7337enum fan_status_access_mode {
7338 TPACPI_FAN_NONE = 0, /* No fan status or control */
7339 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7340 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7341};
7342
7343enum fan_control_access_mode {
7344 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7345 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7346 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7347 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7348};
7349
7350enum fan_control_commands {
7351 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7352 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7353 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7354 * and also watchdog cmd */
7355};
7356
7357static int fan_control_allowed;
7358
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007359static enum fan_status_access_mode fan_status_access_mode;
7360static enum fan_control_access_mode fan_control_access_mode;
7361static enum fan_control_commands fan_control_commands;
7362
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007363static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007364static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007365static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007366static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007367
7368static struct mutex fan_mutex;
7369
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007370static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007371static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007372
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007373TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7374TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007375 "\\FSPD", /* 600e/x, 770e, 770x */
7376 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007377TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007378 "JFNS", /* 770x-JL */
7379 ); /* all others */
7380
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007381/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007382 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7383 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7384 * be in auto mode (0x80).
7385 *
7386 * This is corrected by any write to HFSP either by the driver, or
7387 * by the firmware.
7388 *
7389 * We assume 0x07 really means auto mode while this quirk is active,
7390 * as this is far more likely than the ThinkPad being in level 7,
7391 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007392 *
7393 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7394 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007395 */
7396
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007397static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007398{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007399 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007400 printk(TPACPI_NOTICE
7401 "fan_init: initial fan status is unknown, "
7402 "assuming it is in auto mode\n");
7403 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007404 }
7405}
7406
7407static void fan_quirk1_handle(u8 *fan_status)
7408{
7409 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7410 if (*fan_status != fan_control_initial_status) {
7411 /* something changed the HFSP regisnter since
7412 * driver init time, so it is not undefined
7413 * anymore */
7414 tp_features.fan_ctrl_status_undef = 0;
7415 } else {
7416 /* Return most likely status. In fact, it
7417 * might be the only possible status */
7418 *fan_status = TP_EC_FAN_AUTO;
7419 }
7420 }
7421}
7422
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007423/* Select main fan on X60/X61, NOOP on others */
7424static bool fan_select_fan1(void)
7425{
7426 if (tp_features.second_fan) {
7427 u8 val;
7428
7429 if (ec_read(fan_select_offset, &val) < 0)
7430 return false;
7431 val &= 0xFEU;
7432 if (ec_write(fan_select_offset, val) < 0)
7433 return false;
7434 }
7435 return true;
7436}
7437
7438/* Select secondary fan on X60/X61 */
7439static bool fan_select_fan2(void)
7440{
7441 u8 val;
7442
7443 if (!tp_features.second_fan)
7444 return false;
7445
7446 if (ec_read(fan_select_offset, &val) < 0)
7447 return false;
7448 val |= 0x01U;
7449 if (ec_write(fan_select_offset, val) < 0)
7450 return false;
7451
7452 return true;
7453}
7454
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007455/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007456 * Call with fan_mutex held
7457 */
7458static void fan_update_desired_level(u8 status)
7459{
7460 if ((status &
7461 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7462 if (status > 7)
7463 fan_control_desired_level = 7;
7464 else
7465 fan_control_desired_level = status;
7466 }
7467}
7468
7469static int fan_get_status(u8 *status)
7470{
7471 u8 s;
7472
7473 /* TODO:
7474 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7475
7476 switch (fan_status_access_mode) {
7477 case TPACPI_FAN_RD_ACPI_GFAN:
7478 /* 570, 600e/x, 770e, 770x */
7479
7480 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7481 return -EIO;
7482
7483 if (likely(status))
7484 *status = s & 0x07;
7485
7486 break;
7487
7488 case TPACPI_FAN_RD_TPEC:
7489 /* all except 570, 600e/x, 770e, 770x */
7490 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7491 return -EIO;
7492
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007493 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007494 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007495 fan_quirk1_handle(status);
7496 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007497
7498 break;
7499
7500 default:
7501 return -ENXIO;
7502 }
7503
7504 return 0;
7505}
7506
7507static int fan_get_status_safe(u8 *status)
7508{
7509 int rc;
7510 u8 s;
7511
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007512 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007513 return -ERESTARTSYS;
7514 rc = fan_get_status(&s);
7515 if (!rc)
7516 fan_update_desired_level(s);
7517 mutex_unlock(&fan_mutex);
7518
7519 if (status)
7520 *status = s;
7521
7522 return rc;
7523}
7524
7525static int fan_get_speed(unsigned int *speed)
7526{
7527 u8 hi, lo;
7528
7529 switch (fan_status_access_mode) {
7530 case TPACPI_FAN_RD_TPEC:
7531 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007532 if (unlikely(!fan_select_fan1()))
7533 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007534 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7535 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7536 return -EIO;
7537
7538 if (likely(speed))
7539 *speed = (hi << 8) | lo;
7540
7541 break;
7542
7543 default:
7544 return -ENXIO;
7545 }
7546
7547 return 0;
7548}
7549
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007550static int fan2_get_speed(unsigned int *speed)
7551{
7552 u8 hi, lo;
7553 bool rc;
7554
7555 switch (fan_status_access_mode) {
7556 case TPACPI_FAN_RD_TPEC:
7557 /* all except 570, 600e/x, 770e, 770x */
7558 if (unlikely(!fan_select_fan2()))
7559 return -EIO;
7560 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7561 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7562 fan_select_fan1(); /* play it safe */
7563 if (rc)
7564 return -EIO;
7565
7566 if (likely(speed))
7567 *speed = (hi << 8) | lo;
7568
7569 break;
7570
7571 default:
7572 return -ENXIO;
7573 }
7574
7575 return 0;
7576}
7577
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007578static int fan_set_level(int level)
7579{
7580 if (!fan_control_allowed)
7581 return -EPERM;
7582
7583 switch (fan_control_access_mode) {
7584 case TPACPI_FAN_WR_ACPI_SFAN:
7585 if (level >= 0 && level <= 7) {
7586 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7587 return -EIO;
7588 } else
7589 return -EINVAL;
7590 break;
7591
7592 case TPACPI_FAN_WR_ACPI_FANS:
7593 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007594 if (!(level & TP_EC_FAN_AUTO) &&
7595 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007596 ((level < 0) || (level > 7)))
7597 return -EINVAL;
7598
7599 /* safety net should the EC not support AUTO
7600 * or FULLSPEED mode bits and just ignore them */
7601 if (level & TP_EC_FAN_FULLSPEED)
7602 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007603 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007604 level |= 4; /* safety min speed 4 */
7605
7606 if (!acpi_ec_write(fan_status_offset, level))
7607 return -EIO;
7608 else
7609 tp_features.fan_ctrl_status_undef = 0;
7610 break;
7611
7612 default:
7613 return -ENXIO;
7614 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007615
7616 vdbg_printk(TPACPI_DBG_FAN,
7617 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007618 return 0;
7619}
7620
7621static int fan_set_level_safe(int level)
7622{
7623 int rc;
7624
7625 if (!fan_control_allowed)
7626 return -EPERM;
7627
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007628 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007629 return -ERESTARTSYS;
7630
7631 if (level == TPACPI_FAN_LAST_LEVEL)
7632 level = fan_control_desired_level;
7633
7634 rc = fan_set_level(level);
7635 if (!rc)
7636 fan_update_desired_level(level);
7637
7638 mutex_unlock(&fan_mutex);
7639 return rc;
7640}
7641
7642static int fan_set_enable(void)
7643{
7644 u8 s;
7645 int rc;
7646
7647 if (!fan_control_allowed)
7648 return -EPERM;
7649
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007650 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007651 return -ERESTARTSYS;
7652
7653 switch (fan_control_access_mode) {
7654 case TPACPI_FAN_WR_ACPI_FANS:
7655 case TPACPI_FAN_WR_TPEC:
7656 rc = fan_get_status(&s);
7657 if (rc < 0)
7658 break;
7659
7660 /* Don't go out of emergency fan mode */
7661 if (s != 7) {
7662 s &= 0x07;
7663 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7664 }
7665
7666 if (!acpi_ec_write(fan_status_offset, s))
7667 rc = -EIO;
7668 else {
7669 tp_features.fan_ctrl_status_undef = 0;
7670 rc = 0;
7671 }
7672 break;
7673
7674 case TPACPI_FAN_WR_ACPI_SFAN:
7675 rc = fan_get_status(&s);
7676 if (rc < 0)
7677 break;
7678
7679 s &= 0x07;
7680
7681 /* Set fan to at least level 4 */
7682 s |= 4;
7683
7684 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007685 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007686 else
7687 rc = 0;
7688 break;
7689
7690 default:
7691 rc = -ENXIO;
7692 }
7693
7694 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007695
7696 if (!rc)
7697 vdbg_printk(TPACPI_DBG_FAN,
7698 "fan control: set fan control register to 0x%02x\n",
7699 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007700 return rc;
7701}
7702
7703static int fan_set_disable(void)
7704{
7705 int rc;
7706
7707 if (!fan_control_allowed)
7708 return -EPERM;
7709
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007710 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007711 return -ERESTARTSYS;
7712
7713 rc = 0;
7714 switch (fan_control_access_mode) {
7715 case TPACPI_FAN_WR_ACPI_FANS:
7716 case TPACPI_FAN_WR_TPEC:
7717 if (!acpi_ec_write(fan_status_offset, 0x00))
7718 rc = -EIO;
7719 else {
7720 fan_control_desired_level = 0;
7721 tp_features.fan_ctrl_status_undef = 0;
7722 }
7723 break;
7724
7725 case TPACPI_FAN_WR_ACPI_SFAN:
7726 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7727 rc = -EIO;
7728 else
7729 fan_control_desired_level = 0;
7730 break;
7731
7732 default:
7733 rc = -ENXIO;
7734 }
7735
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007736 if (!rc)
7737 vdbg_printk(TPACPI_DBG_FAN,
7738 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007739
7740 mutex_unlock(&fan_mutex);
7741 return rc;
7742}
7743
7744static int fan_set_speed(int speed)
7745{
7746 int rc;
7747
7748 if (!fan_control_allowed)
7749 return -EPERM;
7750
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007751 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007752 return -ERESTARTSYS;
7753
7754 rc = 0;
7755 switch (fan_control_access_mode) {
7756 case TPACPI_FAN_WR_ACPI_FANS:
7757 if (speed >= 0 && speed <= 65535) {
7758 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7759 speed, speed, speed))
7760 rc = -EIO;
7761 } else
7762 rc = -EINVAL;
7763 break;
7764
7765 default:
7766 rc = -ENXIO;
7767 }
7768
7769 mutex_unlock(&fan_mutex);
7770 return rc;
7771}
7772
7773static void fan_watchdog_reset(void)
7774{
7775 static int fan_watchdog_active;
7776
7777 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7778 return;
7779
7780 if (fan_watchdog_active)
7781 cancel_delayed_work(&fan_watchdog_task);
7782
7783 if (fan_watchdog_maxinterval > 0 &&
7784 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7785 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007786 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007787 msecs_to_jiffies(fan_watchdog_maxinterval
7788 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007789 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007790 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007791 "watchdog will not trigger\n");
7792 }
7793 } else
7794 fan_watchdog_active = 0;
7795}
7796
7797static void fan_watchdog_fire(struct work_struct *ignored)
7798{
7799 int rc;
7800
7801 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7802 return;
7803
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007804 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007805 rc = fan_set_enable();
7806 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007807 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007808 "will try again later...\n", -rc);
7809 /* reschedule for later */
7810 fan_watchdog_reset();
7811 }
7812}
7813
7814/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007815 * SYSFS fan layout: hwmon compatible (device)
7816 *
7817 * pwm*_enable:
7818 * 0: "disengaged" mode
7819 * 1: manual mode
7820 * 2: native EC "auto" mode (recommended, hardware default)
7821 *
7822 * pwm*: set speed in manual mode, ignored otherwise.
7823 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7824 * interpolation.
7825 *
7826 * fan*_input: tachometer reading, RPM
7827 *
7828 *
7829 * SYSFS fan layout: extensions
7830 *
7831 * fan_watchdog (driver):
7832 * fan watchdog interval in seconds, 0 disables (default), max 120
7833 */
7834
7835/* sysfs fan pwm1_enable ----------------------------------------------- */
7836static ssize_t fan_pwm1_enable_show(struct device *dev,
7837 struct device_attribute *attr,
7838 char *buf)
7839{
7840 int res, mode;
7841 u8 status;
7842
7843 res = fan_get_status_safe(&status);
7844 if (res)
7845 return res;
7846
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007847 if (status & TP_EC_FAN_FULLSPEED) {
7848 mode = 0;
7849 } else if (status & TP_EC_FAN_AUTO) {
7850 mode = 2;
7851 } else
7852 mode = 1;
7853
7854 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7855}
7856
7857static ssize_t fan_pwm1_enable_store(struct device *dev,
7858 struct device_attribute *attr,
7859 const char *buf, size_t count)
7860{
7861 unsigned long t;
7862 int res, level;
7863
7864 if (parse_strtoul(buf, 2, &t))
7865 return -EINVAL;
7866
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007867 tpacpi_disclose_usertask("hwmon pwm1_enable",
7868 "set fan mode to %lu\n", t);
7869
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007870 switch (t) {
7871 case 0:
7872 level = TP_EC_FAN_FULLSPEED;
7873 break;
7874 case 1:
7875 level = TPACPI_FAN_LAST_LEVEL;
7876 break;
7877 case 2:
7878 level = TP_EC_FAN_AUTO;
7879 break;
7880 case 3:
7881 /* reserved for software-controlled auto mode */
7882 return -ENOSYS;
7883 default:
7884 return -EINVAL;
7885 }
7886
7887 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007888 if (res == -ENXIO)
7889 return -EINVAL;
7890 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007891 return res;
7892
7893 fan_watchdog_reset();
7894
7895 return count;
7896}
7897
7898static struct device_attribute dev_attr_fan_pwm1_enable =
7899 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7900 fan_pwm1_enable_show, fan_pwm1_enable_store);
7901
7902/* sysfs fan pwm1 ------------------------------------------------------ */
7903static ssize_t fan_pwm1_show(struct device *dev,
7904 struct device_attribute *attr,
7905 char *buf)
7906{
7907 int res;
7908 u8 status;
7909
7910 res = fan_get_status_safe(&status);
7911 if (res)
7912 return res;
7913
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007914 if ((status &
7915 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7916 status = fan_control_desired_level;
7917
7918 if (status > 7)
7919 status = 7;
7920
7921 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7922}
7923
7924static ssize_t fan_pwm1_store(struct device *dev,
7925 struct device_attribute *attr,
7926 const char *buf, size_t count)
7927{
7928 unsigned long s;
7929 int rc;
7930 u8 status, newlevel;
7931
7932 if (parse_strtoul(buf, 255, &s))
7933 return -EINVAL;
7934
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007935 tpacpi_disclose_usertask("hwmon pwm1",
7936 "set fan speed to %lu\n", s);
7937
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007938 /* scale down from 0-255 to 0-7 */
7939 newlevel = (s >> 5) & 0x07;
7940
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007941 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007942 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007943
7944 rc = fan_get_status(&status);
7945 if (!rc && (status &
7946 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7947 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007948 if (rc == -ENXIO)
7949 rc = -EINVAL;
7950 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007951 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007952 fan_watchdog_reset();
7953 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007954 }
7955
7956 mutex_unlock(&fan_mutex);
7957 return (rc)? rc : count;
7958}
7959
7960static struct device_attribute dev_attr_fan_pwm1 =
7961 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7962 fan_pwm1_show, fan_pwm1_store);
7963
7964/* sysfs fan fan1_input ------------------------------------------------ */
7965static ssize_t fan_fan1_input_show(struct device *dev,
7966 struct device_attribute *attr,
7967 char *buf)
7968{
7969 int res;
7970 unsigned int speed;
7971
7972 res = fan_get_speed(&speed);
7973 if (res < 0)
7974 return res;
7975
7976 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7977}
7978
7979static struct device_attribute dev_attr_fan_fan1_input =
7980 __ATTR(fan1_input, S_IRUGO,
7981 fan_fan1_input_show, NULL);
7982
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007983/* sysfs fan fan2_input ------------------------------------------------ */
7984static ssize_t fan_fan2_input_show(struct device *dev,
7985 struct device_attribute *attr,
7986 char *buf)
7987{
7988 int res;
7989 unsigned int speed;
7990
7991 res = fan2_get_speed(&speed);
7992 if (res < 0)
7993 return res;
7994
7995 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7996}
7997
7998static struct device_attribute dev_attr_fan_fan2_input =
7999 __ATTR(fan2_input, S_IRUGO,
8000 fan_fan2_input_show, NULL);
8001
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008002/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008003static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
8004 char *buf)
8005{
8006 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8007}
8008
8009static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8010 const char *buf, size_t count)
8011{
8012 unsigned long t;
8013
8014 if (parse_strtoul(buf, 120, &t))
8015 return -EINVAL;
8016
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008017 if (!fan_control_allowed)
8018 return -EPERM;
8019
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008020 fan_watchdog_maxinterval = t;
8021 fan_watchdog_reset();
8022
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008023 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8024
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008025 return count;
8026}
8027
8028static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8029 fan_fan_watchdog_show, fan_fan_watchdog_store);
8030
8031/* --------------------------------------------------------------------- */
8032static struct attribute *fan_attributes[] = {
8033 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8034 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008035 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008036 NULL
8037};
8038
8039static const struct attribute_group fan_attr_group = {
8040 .attrs = fan_attributes,
8041};
8042
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008043#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8044#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008045
8046#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8047 { .vendor = PCI_VENDOR_ID_IBM, \
8048 .bios = TPACPI_MATCH_ANY, \
8049 .ec = TPID(__id1, __id2), \
8050 .quirks = __quirks }
8051
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008052#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8053 { .vendor = PCI_VENDOR_ID_LENOVO, \
8054 .bios = TPACPI_MATCH_ANY, \
8055 .ec = TPID(__id1, __id2), \
8056 .quirks = __quirks }
8057
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008058static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8059 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8060 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8061 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8062 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008063 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008064};
8065
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008066#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008067#undef TPACPI_FAN_QI
8068
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008069static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008070{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008071 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008072 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008073
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008074 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8075 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008076
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008077 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008078 fan_status_access_mode = TPACPI_FAN_NONE;
8079 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008080 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008081 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008082 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008083 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008084 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008085
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008086 if (tpacpi_is_ibm()) {
8087 TPACPI_ACPIHANDLE_INIT(fans);
8088 TPACPI_ACPIHANDLE_INIT(gfan);
8089 TPACPI_ACPIHANDLE_INIT(sfan);
8090 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008091
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008092 quirks = tpacpi_check_quirks(fan_quirk_table,
8093 ARRAY_SIZE(fan_quirk_table));
8094
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008095 if (gfan_handle) {
8096 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008097 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008098 } else {
8099 /* all other ThinkPads: note that even old-style
8100 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008101 if (likely(acpi_ec_read(fan_status_offset,
8102 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008103 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008104 if (quirks & TPACPI_FAN_Q1)
8105 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008106 if (quirks & TPACPI_FAN_2FAN) {
8107 tp_features.second_fan = 1;
8108 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8109 "secondary fan support enabled\n");
8110 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008111 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008112 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008113 "ThinkPad ACPI EC access misbehaving, "
8114 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008115 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008116 }
8117 }
8118
8119 if (sfan_handle) {
8120 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008121 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008122 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008123 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008124 } else {
8125 if (!gfan_handle) {
8126 /* gfan without sfan means no fan control */
8127 /* all other models implement TP EC 0x2f control */
8128
8129 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008130 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008131 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008132 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008133 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008134 TPACPI_FAN_CMD_SPEED |
8135 TPACPI_FAN_CMD_LEVEL |
8136 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008137 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008138 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008139 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008140 TPACPI_FAN_CMD_LEVEL |
8141 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008142 }
8143 }
8144 }
8145
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008146 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8147 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008148 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8149 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8150 fan_status_access_mode, fan_control_access_mode);
8151
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008152 /* fan control master switch */
8153 if (!fan_control_allowed) {
8154 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8155 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008156 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008157 "fan control features disabled by parameter\n");
8158 }
8159
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008160 /* update fan_control_desired_level */
8161 if (fan_status_access_mode != TPACPI_FAN_NONE)
8162 fan_get_status_safe(NULL);
8163
8164 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8165 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008166 if (tp_features.second_fan) {
8167 /* attach second fan tachometer */
8168 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8169 &dev_attr_fan_fan2_input.attr;
8170 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008171 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008172 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008173 if (rc < 0)
8174 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008175
8176 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8177 &driver_attr_fan_watchdog);
8178 if (rc < 0) {
8179 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8180 &fan_attr_group);
8181 return rc;
8182 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008183 return 0;
8184 } else
8185 return 1;
8186}
8187
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008188static void fan_exit(void)
8189{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008190 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008191 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008192
8193 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008194 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008195 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8196 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008197
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008198 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008199 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008200}
8201
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008202static void fan_suspend(pm_message_t state)
8203{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008204 int rc;
8205
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008206 if (!fan_control_allowed)
8207 return;
8208
8209 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008210 fan_control_resume_level = 0;
8211 rc = fan_get_status_safe(&fan_control_resume_level);
8212 if (rc < 0)
8213 printk(TPACPI_NOTICE
8214 "failed to read fan level for later "
8215 "restore during resume: %d\n", rc);
8216
8217 /* if it is undefined, don't attempt to restore it.
8218 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008219 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008220 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008221}
8222
8223static void fan_resume(void)
8224{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008225 u8 current_level = 7;
8226 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008227 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008228
8229 /* DSDT *always* updates status on resume */
8230 tp_features.fan_ctrl_status_undef = 0;
8231
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008232 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008233 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008234 (fan_get_status_safe(&current_level) < 0))
8235 return;
8236
8237 switch (fan_control_access_mode) {
8238 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008239 /* never decrease fan level */
8240 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008241 break;
8242 case TPACPI_FAN_WR_ACPI_FANS:
8243 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008244 /* never decrease fan level, scale is:
8245 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8246 *
8247 * We expect the firmware to set either 7 or AUTO, but we
8248 * handle FULLSPEED out of paranoia.
8249 *
8250 * So, we can safely only restore FULLSPEED or 7, anything
8251 * else could slow the fan. Restoring AUTO is useless, at
8252 * best that's exactly what the DSDT already set (it is the
8253 * slower it uses).
8254 *
8255 * Always keep in mind that the DSDT *will* have set the
8256 * fans to what the vendor supposes is the best level. We
8257 * muck with it only to speed the fan up.
8258 */
8259 if (fan_control_resume_level != 7 &&
8260 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8261 return;
8262 else
8263 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8264 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008265 break;
8266 default:
8267 return;
8268 }
8269 if (do_set) {
8270 printk(TPACPI_NOTICE
8271 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008272 fan_control_resume_level);
8273 rc = fan_set_level_safe(fan_control_resume_level);
8274 if (rc < 0)
8275 printk(TPACPI_NOTICE
8276 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008277 }
8278}
8279
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008280static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008281{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008282 int rc;
8283 u8 status;
8284 unsigned int speed = 0;
8285
8286 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008287 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008288 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008289 rc = fan_get_status_safe(&status);
8290 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008291 return rc;
8292
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008293 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008294 "level:\t\t%d\n",
8295 (status != 0) ? "enabled" : "disabled", status);
8296 break;
8297
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008298 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008299 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008300 rc = fan_get_status_safe(&status);
8301 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008302 return rc;
8303
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008304 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008305 (status != 0) ? "enabled" : "disabled");
8306
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008307 rc = fan_get_speed(&speed);
8308 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008309 return rc;
8310
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008311 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008312
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008313 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008314 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008315 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008316 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008317 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008318 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008319 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008320 break;
8321
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008322 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008323 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008324 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008325 }
8326
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008327 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008328 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008329
8330 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008331 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008332 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008333 break;
8334
8335 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008336 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008337 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008338 break;
8339 }
8340 }
8341
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008342 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008343 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008344 "commands:\twatchdog <timeout> (<timeout> "
8345 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008346
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008347 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008348 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008349 " (<speed> is 0-65535)\n");
8350
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008351 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008352}
8353
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008354static int fan_write_cmd_level(const char *cmd, int *rc)
8355{
8356 int level;
8357
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008358 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008359 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008360 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008361 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008362 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008363 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008364 return 0;
8365
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008366 *rc = fan_set_level_safe(level);
8367 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008368 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008369 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008370 else if (!*rc)
8371 tpacpi_disclose_usertask("procfs fan",
8372 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008373
8374 return 1;
8375}
8376
8377static int fan_write_cmd_enable(const char *cmd, int *rc)
8378{
8379 if (strlencmp(cmd, "enable") != 0)
8380 return 0;
8381
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008382 *rc = fan_set_enable();
8383 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008384 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008385 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008386 else if (!*rc)
8387 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008388
8389 return 1;
8390}
8391
8392static int fan_write_cmd_disable(const char *cmd, int *rc)
8393{
8394 if (strlencmp(cmd, "disable") != 0)
8395 return 0;
8396
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008397 *rc = fan_set_disable();
8398 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008399 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008400 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008401 else if (!*rc)
8402 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008403
8404 return 1;
8405}
8406
8407static int fan_write_cmd_speed(const char *cmd, int *rc)
8408{
8409 int speed;
8410
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008411 /* TODO:
8412 * Support speed <low> <medium> <high> ? */
8413
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008414 if (sscanf(cmd, "speed %d", &speed) != 1)
8415 return 0;
8416
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008417 *rc = fan_set_speed(speed);
8418 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008419 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008420 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008421 else if (!*rc)
8422 tpacpi_disclose_usertask("procfs fan",
8423 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008424
8425 return 1;
8426}
8427
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008428static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8429{
8430 int interval;
8431
8432 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8433 return 0;
8434
8435 if (interval < 0 || interval > 120)
8436 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008437 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008438 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008439 tpacpi_disclose_usertask("procfs fan",
8440 "set watchdog timer to %d\n",
8441 interval);
8442 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008443
8444 return 1;
8445}
8446
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008447static int fan_write(char *buf)
8448{
8449 char *cmd;
8450 int rc = 0;
8451
8452 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008453 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008454 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008455 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008456 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008457 fan_write_cmd_disable(cmd, &rc) ||
8458 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008459 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008460 fan_write_cmd_speed(cmd, &rc))
8461 )
8462 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008463 else if (!rc)
8464 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008465 }
8466
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008467 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008468}
8469
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008470static struct ibm_struct fan_driver_data = {
8471 .name = "fan",
8472 .read = fan_read,
8473 .write = fan_write,
8474 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008475 .suspend = fan_suspend,
8476 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008477};
8478
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008479/****************************************************************************
8480 ****************************************************************************
8481 *
8482 * Infrastructure
8483 *
8484 ****************************************************************************
8485 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008486
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008487/*
8488 * HKEY event callout for other subdrivers go here
8489 * (yes, it is ugly, but it is quick, safe, and gets the job done
8490 */
8491static void tpacpi_driver_event(const unsigned int hkey_event)
8492{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008493 if (ibm_backlight_device) {
8494 switch (hkey_event) {
8495 case TP_HKEY_EV_BRGHT_UP:
8496 case TP_HKEY_EV_BRGHT_DOWN:
8497 tpacpi_brightness_notify_change();
8498 }
8499 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008500 if (alsa_card) {
8501 switch (hkey_event) {
8502 case TP_HKEY_EV_VOL_UP:
8503 case TP_HKEY_EV_VOL_DOWN:
8504 case TP_HKEY_EV_VOL_MUTE:
8505 volume_alsa_notify_change();
8506 }
8507 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008508}
8509
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008510static void hotkey_driver_event(const unsigned int scancode)
8511{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008512 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008513}
8514
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008515/* sysfs name ---------------------------------------------------------- */
8516static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8517 struct device_attribute *attr,
8518 char *buf)
8519{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008520 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008521}
8522
8523static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8524 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8525
8526/* --------------------------------------------------------------------- */
8527
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008528/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03008529static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008530
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008531/*
8532 * Module and infrastructure proble, init and exit handling
8533 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008535static int force_load;
8536
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008537#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008538static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008539{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008540 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008541
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008542 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008543}
8544#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8545
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008546static void ibm_exit(struct ibm_struct *ibm)
8547{
8548 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8549
8550 list_del_init(&ibm->all_drivers);
8551
8552 if (ibm->flags.acpi_notify_installed) {
8553 dbg_printk(TPACPI_DBG_EXIT,
8554 "%s: acpi_remove_notify_handler\n", ibm->name);
8555 BUG_ON(!ibm->acpi);
8556 acpi_remove_notify_handler(*ibm->acpi->handle,
8557 ibm->acpi->type,
8558 dispatch_acpi_notify);
8559 ibm->flags.acpi_notify_installed = 0;
8560 ibm->flags.acpi_notify_installed = 0;
8561 }
8562
8563 if (ibm->flags.proc_created) {
8564 dbg_printk(TPACPI_DBG_EXIT,
8565 "%s: remove_proc_entry\n", ibm->name);
8566 remove_proc_entry(ibm->name, proc_dir);
8567 ibm->flags.proc_created = 0;
8568 }
8569
8570 if (ibm->flags.acpi_driver_registered) {
8571 dbg_printk(TPACPI_DBG_EXIT,
8572 "%s: acpi_bus_unregister_driver\n", ibm->name);
8573 BUG_ON(!ibm->acpi);
8574 acpi_bus_unregister_driver(ibm->acpi->driver);
8575 kfree(ibm->acpi->driver);
8576 ibm->acpi->driver = NULL;
8577 ibm->flags.acpi_driver_registered = 0;
8578 }
8579
8580 if (ibm->flags.init_called && ibm->exit) {
8581 ibm->exit();
8582 ibm->flags.init_called = 0;
8583 }
8584
8585 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8586}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008587
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008588static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008589{
8590 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008591 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592 struct proc_dir_entry *entry;
8593
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008594 BUG_ON(ibm == NULL);
8595
8596 INIT_LIST_HEAD(&ibm->all_drivers);
8597
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008598 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008599 return 0;
8600
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008601 dbg_printk(TPACPI_DBG_INIT,
8602 "probing for %s\n", ibm->name);
8603
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008604 if (iibm->init) {
8605 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008606 if (ret > 0)
8607 return 0; /* probe failed */
8608 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008609 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008610
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008611 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612 }
8613
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008614 if (ibm->acpi) {
8615 if (ibm->acpi->hid) {
8616 ret = register_tpacpi_subdriver(ibm);
8617 if (ret)
8618 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008619 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008620
8621 if (ibm->acpi->notify) {
8622 ret = setup_acpi_notify(ibm);
8623 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008624 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008625 ibm->name);
8626 ret = 0;
8627 goto err_out;
8628 }
8629 if (ret < 0)
8630 goto err_out;
8631 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008632 }
8633
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008634 dbg_printk(TPACPI_DBG_INIT,
8635 "%s installed\n", ibm->name);
8636
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008637 if (ibm->read) {
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008638 mode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008639
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008640 if (!mode)
8641 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008642 if (ibm->write)
8643 mode |= S_IWUSR;
8644 entry = proc_create_data(ibm->name, mode, proc_dir,
8645 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008646 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008647 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008648 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008649 ret = -ENODEV;
8650 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008651 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008652 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008654
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008655 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8656
Linus Torvalds1da177e2005-04-16 15:20:36 -07008657 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008658
8659err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008660 dbg_printk(TPACPI_DBG_INIT,
8661 "%s: at error exit path with result %d\n",
8662 ibm->name, ret);
8663
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008664 ibm_exit(ibm);
8665 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008666}
8667
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008668/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008669
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008670static bool __pure __init tpacpi_is_fw_digit(const char c)
8671{
8672 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8673}
8674
8675/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8676static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8677 const char t)
8678{
8679 return s && strlen(s) >= 8 &&
8680 tpacpi_is_fw_digit(s[0]) &&
8681 tpacpi_is_fw_digit(s[1]) &&
8682 s[2] == t && s[3] == 'T' &&
8683 tpacpi_is_fw_digit(s[4]) &&
8684 tpacpi_is_fw_digit(s[5]) &&
8685 s[6] == 'W' && s[7] == 'W';
8686}
8687
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008688/* returns 0 - probe ok, or < 0 - probe error.
8689 * Probe ok doesn't mean thinkpad found.
8690 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8691static int __must_check __init get_thinkpad_model_data(
8692 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008693{
Jeff Garzik18552562007-10-03 15:15:40 -04008694 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008695 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008696 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008697
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008698 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008699 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008700
8701 memset(tp, 0, sizeof(*tp));
8702
8703 if (dmi_name_in_vendors("IBM"))
8704 tp->vendor = PCI_VENDOR_ID_IBM;
8705 else if (dmi_name_in_vendors("LENOVO"))
8706 tp->vendor = PCI_VENDOR_ID_LENOVO;
8707 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008708 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008709
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008710 s = dmi_get_system_info(DMI_BIOS_VERSION);
8711 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8712 if (s && !tp->bios_version_str)
8713 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008714
8715 /* Really ancient ThinkPad 240X will fail this, which is fine */
8716 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008717 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008718
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008719 tp->bios_model = tp->bios_version_str[0]
8720 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008721 tp->bios_release = (tp->bios_version_str[4] << 8)
8722 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008723
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008724 /*
8725 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8726 * X32 or newer, all Z series; Some models must have an
8727 * up-to-date BIOS or they will not be detected.
8728 *
8729 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8730 */
8731 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008732 if (sscanf(dev->name,
8733 "IBM ThinkPad Embedded Controller -[%17c",
8734 ec_fw_string) == 1) {
8735 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8736 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008737
8738 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008739 if (!tp->ec_version_str)
8740 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008741
8742 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8743 tp->ec_model = ec_fw_string[0]
8744 | (ec_fw_string[1] << 8);
8745 tp->ec_release = (ec_fw_string[4] << 8)
8746 | ec_fw_string[5];
8747 } else {
8748 printk(TPACPI_NOTICE
8749 "ThinkPad firmware release %s "
8750 "doesn't match the known patterns\n",
8751 ec_fw_string);
8752 printk(TPACPI_NOTICE
8753 "please report this to %s\n",
8754 TPACPI_MAIL);
8755 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008756 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008757 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008758 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008759
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008760 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8761 if (s && !strnicmp(s, "ThinkPad", 8)) {
8762 tp->model_str = kstrdup(s, GFP_KERNEL);
8763 if (!tp->model_str)
8764 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008765 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008766
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008767 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8768 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8769 if (s && !tp->nummodel_str)
8770 return -ENOMEM;
8771
8772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773}
8774
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008775static int __init probe_for_thinkpad(void)
8776{
8777 int is_thinkpad;
8778
8779 if (acpi_disabled)
8780 return -ENODEV;
8781
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008782 /* It would be dangerous to run the driver in this case */
8783 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8784 return -ENODEV;
8785
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008786 /*
8787 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008788 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008789 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008790 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8791 (thinkpad_id.ec_model != 0) ||
8792 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008793
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03008794 /* The EC handler is required */
8795 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008796 if (!ec_handle) {
8797 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008798 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008799 "Not yet supported ThinkPad detected!\n");
8800 return -ENODEV;
8801 }
8802
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008803 if (!is_thinkpad && !force_load)
8804 return -ENODEV;
8805
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008806 return 0;
8807}
8808
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008809static void __init thinkpad_acpi_init_banner(void)
8810{
8811 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8812 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8813
8814 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8815 (thinkpad_id.bios_version_str) ?
8816 thinkpad_id.bios_version_str : "unknown",
8817 (thinkpad_id.ec_version_str) ?
8818 thinkpad_id.ec_version_str : "unknown");
8819
8820 BUG_ON(!thinkpad_id.vendor);
8821
8822 if (thinkpad_id.model_str)
8823 printk(TPACPI_INFO "%s %s, model %s\n",
8824 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8825 "IBM" : ((thinkpad_id.vendor ==
8826 PCI_VENDOR_ID_LENOVO) ?
8827 "Lenovo" : "Unknown vendor"),
8828 thinkpad_id.model_str,
8829 (thinkpad_id.nummodel_str) ?
8830 thinkpad_id.nummodel_str : "unknown");
8831}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008832
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008833/* Module init, exit, parameters */
8834
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008835static struct ibm_init_struct ibms_init[] __initdata = {
8836 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008837 .data = &thinkpad_acpi_driver_data,
8838 },
8839 {
8840 .init = hotkey_init,
8841 .data = &hotkey_driver_data,
8842 },
8843 {
8844 .init = bluetooth_init,
8845 .data = &bluetooth_driver_data,
8846 },
8847 {
8848 .init = wan_init,
8849 .data = &wan_driver_data,
8850 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008851 {
8852 .init = uwb_init,
8853 .data = &uwb_driver_data,
8854 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008855#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008856 {
8857 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008858 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008859 .data = &video_driver_data,
8860 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008861#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008862 {
8863 .init = light_init,
8864 .data = &light_driver_data,
8865 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008866 {
8867 .init = cmos_init,
8868 .data = &cmos_driver_data,
8869 },
8870 {
8871 .init = led_init,
8872 .data = &led_driver_data,
8873 },
8874 {
8875 .init = beep_init,
8876 .data = &beep_driver_data,
8877 },
8878 {
8879 .init = thermal_init,
8880 .data = &thermal_driver_data,
8881 },
8882 {
8883 .data = &ecdump_driver_data,
8884 },
8885 {
8886 .init = brightness_init,
8887 .data = &brightness_driver_data,
8888 },
8889 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008890 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008891 .data = &volume_driver_data,
8892 },
8893 {
8894 .init = fan_init,
8895 .data = &fan_driver_data,
8896 },
8897};
8898
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008899static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8900{
8901 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008902 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008903
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008904 if (!kp || !kp->name || !val)
8905 return -EINVAL;
8906
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008907 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8908 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008909 WARN_ON(ibm == NULL);
8910
8911 if (!ibm || !ibm->name)
8912 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008913
8914 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8915 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008916 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008917 strcpy(ibms_init[i].param, val);
8918 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008919 return 0;
8920 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008921 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008922
8923 return -EINVAL;
8924}
8925
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008926module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008927MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008928 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008929
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008930module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008931MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008932
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008933module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008934MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008935 "Attempts to load the driver even on a "
8936 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008937
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008938module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008939MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008940 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008941
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008942module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008943MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008944 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008945 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008946
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008947module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008948MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008949 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008950
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008951module_param(hotkey_report_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008952MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008953 "used for backwards compatibility with userspace, "
8954 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008955
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008956#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008957module_param_named(volume_mode, volume_mode, uint, 0444);
8958MODULE_PARM_DESC(volume_mode,
8959 "Selects volume control strategy: "
8960 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8961
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008962module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8963MODULE_PARM_DESC(volume_capabilities,
8964 "Selects the mixer capabilites: "
8965 "0=auto, 1=volume and mute, 2=mute only");
8966
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008967module_param_named(volume_control, volume_control_allowed, bool, 0444);
8968MODULE_PARM_DESC(volume_control,
8969 "Enables software override for the console audio "
8970 "control when true");
8971
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008972/* ALSA module API parameters */
8973module_param_named(index, alsa_index, int, 0444);
8974MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8975module_param_named(id, alsa_id, charp, 0444);
8976MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8977module_param_named(enable, alsa_enable, bool, 0444);
8978MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008979#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008980
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008981#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008982 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008983 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008984 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008985
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008986TPACPI_PARAM(hotkey);
8987TPACPI_PARAM(bluetooth);
8988TPACPI_PARAM(video);
8989TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008990TPACPI_PARAM(cmos);
8991TPACPI_PARAM(led);
8992TPACPI_PARAM(beep);
8993TPACPI_PARAM(ecdump);
8994TPACPI_PARAM(brightness);
8995TPACPI_PARAM(volume);
8996TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008997
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008998#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008999module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009000MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9001module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9002MODULE_PARM_DESC(wlsw_state,
9003 "Initial state of the emulated WLSW switch");
9004
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009005module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009006MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9007module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9008MODULE_PARM_DESC(bluetooth_state,
9009 "Initial state of the emulated bluetooth switch");
9010
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009011module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009012MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9013module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9014MODULE_PARM_DESC(wwan_state,
9015 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009016
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009017module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009018MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9019module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9020MODULE_PARM_DESC(uwb_state,
9021 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009022#endif
9023
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009024static void thinkpad_acpi_module_exit(void)
9025{
9026 struct ibm_struct *ibm, *itmp;
9027
9028 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9029
9030 list_for_each_entry_safe_reverse(ibm, itmp,
9031 &tpacpi_all_drivers,
9032 all_drivers) {
9033 ibm_exit(ibm);
9034 }
9035
9036 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9037
9038 if (tpacpi_inputdev) {
9039 if (tp_features.input_device_registered)
9040 input_unregister_device(tpacpi_inputdev);
9041 else
9042 input_free_device(tpacpi_inputdev);
9043 }
9044
9045 if (tpacpi_hwmon)
9046 hwmon_device_unregister(tpacpi_hwmon);
9047
9048 if (tp_features.sensors_pdev_attrs_registered)
9049 device_remove_file(&tpacpi_sensors_pdev->dev,
9050 &dev_attr_thinkpad_acpi_pdev_name);
9051 if (tpacpi_sensors_pdev)
9052 platform_device_unregister(tpacpi_sensors_pdev);
9053 if (tpacpi_pdev)
9054 platform_device_unregister(tpacpi_pdev);
9055
9056 if (tp_features.sensors_pdrv_attrs_registered)
9057 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9058 if (tp_features.platform_drv_attrs_registered)
9059 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9060
9061 if (tp_features.sensors_pdrv_registered)
9062 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9063
9064 if (tp_features.platform_drv_registered)
9065 platform_driver_unregister(&tpacpi_pdriver);
9066
9067 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009068 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009069
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009070 if (tpacpi_wq)
9071 destroy_workqueue(tpacpi_wq);
9072
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009073 kfree(thinkpad_id.bios_version_str);
9074 kfree(thinkpad_id.ec_version_str);
9075 kfree(thinkpad_id.model_str);
9076}
9077
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009078
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009079static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009080{
9081 int ret, i;
9082
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009083 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9084
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03009085 /* Parameter checking */
9086 if (hotkey_report_mode > 2)
9087 return -EINVAL;
9088
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009089 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009090
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009091 ret = get_thinkpad_model_data(&thinkpad_id);
9092 if (ret) {
9093 printk(TPACPI_ERR
9094 "unable to get DMI data: %d\n", ret);
9095 thinkpad_acpi_module_exit();
9096 return ret;
9097 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009098 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009099 if (ret) {
9100 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009101 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009103
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009104 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009105
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009106 thinkpad_acpi_init_banner();
9107 tpacpi_check_outdated_fw();
9108
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009109 TPACPI_ACPIHANDLE_INIT(ecrd);
9110 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009111
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009112 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9113 if (!tpacpi_wq) {
9114 thinkpad_acpi_module_exit();
9115 return -ENOMEM;
9116 }
9117
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009118 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009119 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009120 printk(TPACPI_ERR
9121 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009122 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009123 return -ENODEV;
9124 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009125
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009126 ret = platform_driver_register(&tpacpi_pdriver);
9127 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009128 printk(TPACPI_ERR
9129 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009130 thinkpad_acpi_module_exit();
9131 return ret;
9132 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009133 tp_features.platform_drv_registered = 1;
9134
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009135 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9136 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009137 printk(TPACPI_ERR
9138 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009139 thinkpad_acpi_module_exit();
9140 return ret;
9141 }
9142 tp_features.sensors_pdrv_registered = 1;
9143
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009144 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009145 if (!ret) {
9146 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009147 ret = tpacpi_create_driver_attributes(
9148 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009149 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009150 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009151 printk(TPACPI_ERR
9152 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009153 thinkpad_acpi_module_exit();
9154 return ret;
9155 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009156 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009157
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009158
9159 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009160 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009161 NULL, 0);
9162 if (IS_ERR(tpacpi_pdev)) {
9163 ret = PTR_ERR(tpacpi_pdev);
9164 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009165 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009166 thinkpad_acpi_module_exit();
9167 return ret;
9168 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009169 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009170 TPACPI_HWMON_DRVR_NAME,
9171 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009172 if (IS_ERR(tpacpi_sensors_pdev)) {
9173 ret = PTR_ERR(tpacpi_sensors_pdev);
9174 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009175 printk(TPACPI_ERR
9176 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009177 thinkpad_acpi_module_exit();
9178 return ret;
9179 }
9180 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9181 &dev_attr_thinkpad_acpi_pdev_name);
9182 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009183 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009184 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009185 thinkpad_acpi_module_exit();
9186 return ret;
9187 }
9188 tp_features.sensors_pdev_attrs_registered = 1;
9189 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009190 if (IS_ERR(tpacpi_hwmon)) {
9191 ret = PTR_ERR(tpacpi_hwmon);
9192 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009193 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009194 thinkpad_acpi_module_exit();
9195 return ret;
9196 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009197 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009198 tpacpi_inputdev = input_allocate_device();
9199 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009200 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009201 thinkpad_acpi_module_exit();
9202 return -ENOMEM;
9203 } else {
9204 /* Prepare input device, but don't register */
9205 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009206 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009207 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009208 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009209 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9210 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009211 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009212 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009213
9214 /* Init subdriver dependencies */
9215 tpacpi_detect_brightness_capabilities();
9216
9217 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009218 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9219 ret = ibm_init(&ibms_init[i]);
9220 if (ret >= 0 && *ibms_init[i].param)
9221 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009223 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009224 return ret;
9225 }
9226 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009227
9228 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9229
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009230 ret = input_register_device(tpacpi_inputdev);
9231 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009232 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009233 thinkpad_acpi_module_exit();
9234 return ret;
9235 } else {
9236 tp_features.input_device_registered = 1;
9237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009238
9239 return 0;
9240}
9241
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009242MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9243
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009244/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009245 * This will autoload the driver in almost every ThinkPad
9246 * in widespread use.
9247 *
9248 * Only _VERY_ old models, like the 240, 240x and 570 lack
9249 * the HKEY event interface.
9250 */
9251MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9252
9253/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009254 * DMI matching for module autoloading
9255 *
9256 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9257 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9258 *
9259 * Only models listed in thinkwiki will be supported, so add yours
9260 * if it is not there yet.
9261 */
9262#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009263 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009264
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009265/* Ancient thinkpad BIOSes have to be identified by
9266 * BIOS type or model number, and there are far less
9267 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009268IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009269
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009270MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9271MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009272MODULE_DESCRIPTION(TPACPI_DESC);
9273MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009274MODULE_LICENSE("GPL");
9275
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009276module_init(thinkpad_acpi_module_init);
9277module_exit(thinkpad_acpi_module_exit);