blob: cdcd2388af2b5fa849bf9a099b30b24b887a8ac6 [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
Joe Perches0978e012011-04-04 10:06:25 -070024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
Henrique de Moraes Holschuh5d2eb142009-12-15 21:51:13 -020026#define TPACPI_VERSION "0.24"
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020027#define TPACPI_SYSFS_VERSION 0x020700
Borislav Deianov78f81cc2005-08-17 00:00:00 -040028
29/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020031 * 2007-10-20 changelog trimmed down
32 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030033 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020035 *
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030039 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040040 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020042 *
43 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040044 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
51
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020052#include <linux/kernel.h>
53#include <linux/module.h>
54#include <linux/init.h>
55#include <linux/types.h>
56#include <linux/string.h>
57#include <linux/list.h>
58#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000059#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020060#include <linux/kthread.h>
61#include <linux/freezer.h>
62#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020064
65#include <linux/nvram.h>
66#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020067#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020068#include <linux/sysfs.h>
69#include <linux/backlight.h>
70#include <linux/fb.h>
71#include <linux/platform_device.h>
72#include <linux/hwmon.h>
73#include <linux/hwmon-sysfs.h>
74#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030075#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030076#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020077#include <asm/uaccess.h>
78
79#include <linux/dmi.h>
80#include <linux/jiffies.h>
81#include <linux/workqueue.h>
82
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020083#include <sound/core.h>
84#include <sound/control.h>
85#include <sound/initval.h>
86
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020087#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020088
89#include <linux/pci_ids.h>
90
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020091
92/* ThinkPad CMOS commands */
93#define TP_CMOS_VOLUME_DOWN 0
94#define TP_CMOS_VOLUME_UP 1
95#define TP_CMOS_VOLUME_MUTE 2
96#define TP_CMOS_BRIGHTNESS_UP 4
97#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030098#define TP_CMOS_THINKLIGHT_ON 12
99#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200100
101/* NVRAM Addresses */
102enum tp_nvram_addr {
103 TP_NVRAM_ADDR_HK2 = 0x57,
104 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
105 TP_NVRAM_ADDR_VIDEO = 0x59,
106 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
107 TP_NVRAM_ADDR_MIXER = 0x60,
108};
109
110/* NVRAM bit masks */
111enum {
112 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
113 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
114 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
115 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
116 TP_NVRAM_MASK_THINKLIGHT = 0x10,
117 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
118 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
119 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
120 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
121 TP_NVRAM_MASK_MUTE = 0x40,
122 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
123 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
124 TP_NVRAM_POS_LEVEL_VOLUME = 0,
125};
126
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300127/* Misc NVRAM-related */
128enum {
129 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
130};
131
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200132/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400133#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
134#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300135#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200136
137/* Input IDs */
138#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
139#define TPACPI_HKEY_INPUT_VERSION 0x4101
140
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200141/* ACPI \WGSV commands */
142enum {
143 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
144 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
145 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
146 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
147};
148
149/* TP_ACPI_WGSV_GET_STATE bits */
150enum {
151 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
152 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
153 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
154 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
155 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
156 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
157 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
158 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
159 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
160 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
161};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200162
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300163/* HKEY events */
164enum tpacpi_hkey_event_t {
165 /* Hotkey-related */
166 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
167 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
168 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
169 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
170 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
171 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
172
173 /* Reasons for waking up from S3/S4 */
174 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
175 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
176 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
177 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
178 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
179 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
180
181 /* Auto-sleep after eject request */
182 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
183 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
184
185 /* Misc bay events */
186 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
187
188 /* User-interface events */
189 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
190 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
191 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
192 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
193 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
194 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
195 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
196
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300197 /* Key-related user-interface events */
198 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
199 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
200
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300201 /* Thermal events */
202 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
203 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
204 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
205 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
206 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
207
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300208 TP_HKEY_EV_UNK_6040 = 0x6040, /* Related to AC change?
209 some sort of APM hint,
210 W520 */
211
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300212 /* Misc */
213 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
214};
215
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200216/****************************************************************************
217 * Main driver
218 */
219
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200220#define TPACPI_NAME "thinkpad"
221#define TPACPI_DESC "ThinkPad ACPI Extras"
222#define TPACPI_FILE TPACPI_NAME "_acpi"
223#define TPACPI_URL "http://ibm-acpi.sf.net/"
224#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200225
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200226#define TPACPI_PROC_DIR "ibm"
227#define TPACPI_ACPI_EVENT_PREFIX "ibm"
228#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300229#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200230#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200231
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300232#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300233#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300234
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200235#define TPACPI_MAX_ACPI_ARGS 3
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
Joe Perches0978e012011-04-04 10:06:25 -0700391#define dbg_printk(a_dbg_level, format, arg...) \
392do { \
393 if (dbg_level & (a_dbg_level)) \
394 printk(KERN_DEBUG pr_fmt("%s: " format), \
395 __func__, ##arg); \
396} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000397
398#ifdef CONFIG_THINKPAD_ACPI_DEBUG
399#define vdbg_printk dbg_printk
400static const char *str_supported(int is_supported);
401#else
Joe Perches0978e012011-04-04 10:06:25 -0700402static inline const char *str_supported(int is_supported) { return ""; }
403#define vdbg_printk(a_dbg_level, format, arg...) \
404 no_printk(format, ##arg)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000405#endif
406
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000407static void tpacpi_log_usertask(const char * const what)
408{
Joe Perches0978e012011-04-04 10:06:25 -0700409 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
410 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000411}
412
Joe Perches0978e012011-04-04 10:06:25 -0700413#define tpacpi_disclose_usertask(what, format, arg...) \
414do { \
415 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
416 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
417 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
418 what, task_tgid_vnr(current), ## arg); \
419 } \
420} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000421
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300422/*
423 * Quirk handling helpers
424 *
425 * ThinkPad IDs and versions seen in the field so far
426 * are two-characters from the set [0-9A-Z], i.e. base 36.
427 *
428 * We use values well outside that range as specials.
429 */
430
431#define TPACPI_MATCH_ANY 0xffffU
432#define TPACPI_MATCH_UNKNOWN 0U
433
434/* TPID('1', 'Y') == 0x5931 */
435#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
436
437#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
438 { .vendor = PCI_VENDOR_ID_IBM, \
439 .bios = TPID(__id1, __id2), \
440 .ec = TPACPI_MATCH_ANY, \
441 .quirks = (__quirk) }
442
443#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
444 { .vendor = PCI_VENDOR_ID_LENOVO, \
445 .bios = TPID(__id1, __id2), \
446 .ec = TPACPI_MATCH_ANY, \
447 .quirks = (__quirk) }
448
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200449#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
450 { .vendor = PCI_VENDOR_ID_LENOVO, \
451 .bios = TPACPI_MATCH_ANY, \
452 .ec = TPID(__id1, __id2), \
453 .quirks = (__quirk) }
454
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300455struct tpacpi_quirk {
456 unsigned int vendor;
457 u16 bios;
458 u16 ec;
459 unsigned long quirks;
460};
461
462/**
463 * tpacpi_check_quirks() - search BIOS/EC version on a list
464 * @qlist: array of &struct tpacpi_quirk
465 * @qlist_size: number of elements in @qlist
466 *
467 * Iterates over a quirks list until one is found that matches the
468 * ThinkPad's vendor, BIOS and EC model.
469 *
470 * Returns 0 if nothing matches, otherwise returns the quirks field of
471 * the matching &struct tpacpi_quirk entry.
472 *
473 * The match criteria is: vendor, ec and bios much match.
474 */
475static unsigned long __init tpacpi_check_quirks(
476 const struct tpacpi_quirk *qlist,
477 unsigned int qlist_size)
478{
479 while (qlist_size) {
480 if ((qlist->vendor == thinkpad_id.vendor ||
481 qlist->vendor == TPACPI_MATCH_ANY) &&
482 (qlist->bios == thinkpad_id.bios_model ||
483 qlist->bios == TPACPI_MATCH_ANY) &&
484 (qlist->ec == thinkpad_id.ec_model ||
485 qlist->ec == TPACPI_MATCH_ANY))
486 return qlist->quirks;
487
488 qlist_size--;
489 qlist++;
490 }
491 return 0;
492}
493
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300494static inline bool __pure __init tpacpi_is_lenovo(void)
495{
496 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
497}
498
499static inline bool __pure __init tpacpi_is_ibm(void)
500{
501 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
502}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300503
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300504/****************************************************************************
505 ****************************************************************************
506 *
507 * ACPI Helpers and device model
508 *
509 ****************************************************************************
510 ****************************************************************************/
511
512/*************************************************************************
513 * ACPI basic handles
514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300516static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300517static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200519#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 static acpi_handle object##_handle; \
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300521 static const acpi_handle *object##_parent __initdata = \
522 &parent##_handle; \
523 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200525TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
526TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200528TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
529 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400530 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
531 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300532 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400533
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200534TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400535 "^HKEY", /* R30, R31 */
536 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300537 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400538
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300539/*************************************************************************
540 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200541 */
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543static int acpi_evalf(acpi_handle handle,
544 void *res, char *method, char *fmt, ...)
545{
546 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400547 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200548 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400549 struct acpi_buffer result, *resultp;
550 union acpi_object out_obj;
551 acpi_status status;
552 va_list ap;
553 char res_type;
554 int success;
555 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700558 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return 0;
560 }
561
562 if (*fmt == 'q') {
563 quiet = 1;
564 fmt++;
565 } else
566 quiet = 0;
567
568 res_type = *(fmt++);
569
570 params.count = 0;
571 params.pointer = &in_objs[0];
572
573 va_start(ap, fmt);
574 while (*fmt) {
575 char c = *(fmt++);
576 switch (c) {
577 case 'd': /* int */
578 in_objs[params.count].integer.value = va_arg(ap, int);
579 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
580 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400581 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 default:
Joe Perches0978e012011-04-04 10:06:25 -0700583 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 "with invalid format character '%c'\n", c);
Jesper Juhl21365852010-12-24 19:56:28 +0100585 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return 0;
587 }
588 }
589 va_end(ap);
590
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400591 if (res_type != 'v') {
592 result.length = sizeof(out_obj);
593 result.pointer = &out_obj;
594 resultp = &result;
595 } else
596 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400598 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400601 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300602 success = (status == AE_OK &&
603 out_obj.type == ACPI_TYPE_INTEGER);
604 if (success && res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 *(int *)res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400607 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 success = status == AE_OK;
609 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400610 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 default:
Joe Perches0978e012011-04-04 10:06:25 -0700612 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 "with invalid format character '%c'\n", res_type);
614 return 0;
615 }
616
617 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700618 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300619 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 return success;
622}
623
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200624static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300625{
626 int v;
627
628 if (ecrd_handle) {
629 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
630 return 0;
631 *p = v;
632 } else {
633 if (ec_read(i, p) < 0)
634 return 0;
635 }
636
637 return 1;
638}
639
640static int acpi_ec_write(int i, u8 v)
641{
642 if (ecwr_handle) {
643 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
644 return 0;
645 } else {
646 if (ec_write(i, v) < 0)
647 return 0;
648 }
649
650 return 1;
651}
652
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300653static int issue_thinkpad_cmos_command(int cmos_cmd)
654{
655 if (!cmos_handle)
656 return -ENXIO;
657
658 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
659 return -EIO;
660
661 return 0;
662}
663
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300664/*************************************************************************
665 * ACPI device model
666 */
667
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200668#define TPACPI_ACPIHANDLE_INIT(object) \
669 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300670 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200671
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300672static void __init drv_acpi_handle_init(const char *name,
673 acpi_handle *handle, const acpi_handle parent,
674 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300675{
676 int i;
677 acpi_status status;
678
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300679 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
680 name);
681
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300682 for (i = 0; i < num_paths; i++) {
683 status = acpi_get_handle(parent, paths[i], handle);
684 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300685 dbg_printk(TPACPI_DBG_INIT,
686 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300687 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300688 return;
689 }
690 }
691
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300692 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
693 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300694 *handle = NULL;
695}
696
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300697static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
698 u32 level, void *context, void **return_value)
699{
700 *(acpi_handle *)return_value = handle;
701
702 return AE_CTRL_TERMINATE;
703}
704
705static void __init tpacpi_acpi_handle_locate(const char *name,
706 const char *hid,
707 acpi_handle *handle)
708{
709 acpi_status status;
710 acpi_handle device_found;
711
712 BUG_ON(!name || !hid || !handle);
713 vdbg_printk(TPACPI_DBG_INIT,
714 "trying to locate ACPI handle for %s, using HID %s\n",
715 name, hid);
716
717 memset(&device_found, 0, sizeof(device_found));
718 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
719 (void *)name, &device_found);
720
721 *handle = NULL;
722
723 if (ACPI_SUCCESS(status)) {
724 *handle = device_found;
725 dbg_printk(TPACPI_DBG_INIT,
726 "Found ACPI handle for %s\n", name);
727 } else {
728 vdbg_printk(TPACPI_DBG_INIT,
729 "Could not locate an ACPI handle for %s: %s\n",
730 name, acpi_format_exception(status));
731 }
732}
733
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300734static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300735{
736 struct ibm_struct *ibm = data;
737
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300738 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
739 return;
740
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300741 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300742 return;
743
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300744 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300745}
746
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300747static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300748{
749 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300750 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300751
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300752 BUG_ON(!ibm->acpi);
753
754 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300755 return 0;
756
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300757 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300758 "setting up ACPI notify for %s\n", ibm->name);
759
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300760 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
761 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700762 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300763 return -ENODEV;
764 }
765
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700766 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300767 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200768 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300769 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300770
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300771 status = acpi_install_notify_handler(*ibm->acpi->handle,
772 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300773 if (ACPI_FAILURE(status)) {
774 if (status == AE_ALREADY_EXISTS) {
Joe Perches0978e012011-04-04 10:06:25 -0700775 pr_notice("another device driver is already "
776 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300777 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700778 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300779 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300780 }
781 return -ENODEV;
782 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300783 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300784 return 0;
785}
786
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300787static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300788{
789 return 0;
790}
791
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300792static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300793{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300794 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300795
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300796 dbg_printk(TPACPI_DBG_INIT,
797 "registering %s as an ACPI driver\n", ibm->name);
798
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300799 BUG_ON(!ibm->acpi);
800
801 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
802 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700803 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300804 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300805 }
806
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200807 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300808 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200809
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300810 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300811
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300812 rc = acpi_bus_register_driver(ibm->acpi->driver);
813 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700814 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200815 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300816 kfree(ibm->acpi->driver);
817 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300818 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300819 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300820
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300821 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300822}
823
824
825/****************************************************************************
826 ****************************************************************************
827 *
828 * Procfs Helpers
829 *
830 ****************************************************************************
831 ****************************************************************************/
832
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200833static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300834{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200835 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300836
837 if (!ibm || !ibm->read)
838 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200839 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300840}
841
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200842static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300843{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200844 return single_open(file, dispatch_proc_show, PDE(inode)->data);
845}
846
847static ssize_t dispatch_proc_write(struct file *file,
848 const char __user *userbuf,
849 size_t count, loff_t *pos)
850{
851 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300852 char *kernbuf;
853 int ret;
854
855 if (!ibm || !ibm->write)
856 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300857 if (count > PAGE_SIZE - 2)
858 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300859
860 kernbuf = kmalloc(count + 2, GFP_KERNEL);
861 if (!kernbuf)
862 return -ENOMEM;
863
864 if (copy_from_user(kernbuf, userbuf, count)) {
865 kfree(kernbuf);
866 return -EFAULT;
867 }
868
869 kernbuf[count] = 0;
870 strcat(kernbuf, ",");
871 ret = ibm->write(kernbuf);
872 if (ret == 0)
873 ret = count;
874
875 kfree(kernbuf);
876
877 return ret;
878}
879
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200880static const struct file_operations dispatch_proc_fops = {
881 .owner = THIS_MODULE,
882 .open = dispatch_proc_open,
883 .read = seq_read,
884 .llseek = seq_lseek,
885 .release = single_release,
886 .write = dispatch_proc_write,
887};
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889static char *next_cmd(char **cmds)
890{
891 char *start = *cmds;
892 char *end;
893
894 while ((end = strchr(start, ',')) && end == start)
895 start = end + 1;
896
897 if (!end)
898 return NULL;
899
900 *end = 0;
901 *cmds = end + 1;
902 return start;
903}
904
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300905
906/****************************************************************************
907 ****************************************************************************
908 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300909 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300910 *
911 ****************************************************************************
912 ****************************************************************************/
913
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300914static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300915static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700916static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300917static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300918static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200919static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300920
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200921static int tpacpi_suspend_handler(struct platform_device *pdev,
922 pm_message_t state)
923{
924 struct ibm_struct *ibm, *itmp;
925
926 list_for_each_entry_safe(ibm, itmp,
927 &tpacpi_all_drivers,
928 all_drivers) {
929 if (ibm->suspend)
930 (ibm->suspend)(state);
931 }
932
933 return 0;
934}
935
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300936static int tpacpi_resume_handler(struct platform_device *pdev)
937{
938 struct ibm_struct *ibm, *itmp;
939
940 list_for_each_entry_safe(ibm, itmp,
941 &tpacpi_all_drivers,
942 all_drivers) {
943 if (ibm->resume)
944 (ibm->resume)();
945 }
946
947 return 0;
948}
949
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200950static void tpacpi_shutdown_handler(struct platform_device *pdev)
951{
952 struct ibm_struct *ibm, *itmp;
953
954 list_for_each_entry_safe(ibm, itmp,
955 &tpacpi_all_drivers,
956 all_drivers) {
957 if (ibm->shutdown)
958 (ibm->shutdown)();
959 }
960}
961
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300962static struct platform_driver tpacpi_pdriver = {
963 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200964 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300965 .owner = THIS_MODULE,
966 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200967 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300968 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200969 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300970};
971
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300972static struct platform_driver tpacpi_hwmon_pdriver = {
973 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200974 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300975 .owner = THIS_MODULE,
976 },
977};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300978
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300979/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300980 * sysfs support helpers
981 */
982
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200983struct attribute_set {
984 unsigned int members, max_members;
985 struct attribute_group group;
986};
987
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300988struct attribute_set_obj {
989 struct attribute_set s;
990 struct attribute *a;
991} __attribute__((packed));
992
993static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200994 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300995{
996 struct attribute_set_obj *sobj;
997
998 if (max_members == 0)
999 return NULL;
1000
1001 /* Allocates space for implicit NULL at the end too */
1002 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1003 max_members * sizeof(struct attribute *),
1004 GFP_KERNEL);
1005 if (!sobj)
1006 return NULL;
1007 sobj->s.max_members = max_members;
1008 sobj->s.group.attrs = &sobj->a;
1009 sobj->s.group.name = name;
1010
1011 return &sobj->s;
1012}
1013
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001014#define destroy_attr_set(_set) \
1015 kfree(_set);
1016
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001017/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001018static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001019{
1020 if (!s || !attr)
1021 return -EINVAL;
1022
1023 if (s->members >= s->max_members)
1024 return -ENOMEM;
1025
1026 s->group.attrs[s->members] = attr;
1027 s->members++;
1028
1029 return 0;
1030}
1031
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001032static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001033 struct attribute **attr,
1034 unsigned int count)
1035{
1036 int i, res;
1037
1038 for (i = 0; i < count; i++) {
1039 res = add_to_attr_set(s, attr[i]);
1040 if (res)
1041 return res;
1042 }
1043
1044 return 0;
1045}
1046
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001047static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001048{
1049 sysfs_remove_group(kobj, &s->group);
1050 destroy_attr_set(s);
1051}
1052
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001053#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1054 sysfs_create_group(_kobj, &_attr_set->group)
1055
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001056static int parse_strtoul(const char *buf,
1057 unsigned long max, unsigned long *value)
1058{
1059 char *endp;
1060
André Goddard Rosae7d28602009-12-14 18:01:06 -08001061 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1062 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001063 if (*endp || *value > max)
1064 return -EINVAL;
1065
1066 return 0;
1067}
1068
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001069static void tpacpi_disable_brightness_delay(void)
1070{
1071 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001072 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001073}
1074
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001075static void printk_deprecated_attribute(const char * const what,
1076 const char * const details)
1077{
1078 tpacpi_log_usertask("deprecated sysfs attribute");
Joe Perches0978e012011-04-04 10:06:25 -07001079 pr_warn("WARNING: sysfs attribute %s is deprecated and "
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001080 "will be removed. %s\n",
1081 what, details);
1082}
1083
Johannes Berg19d337d2009-06-02 13:01:37 +02001084/*************************************************************************
1085 * rfkill and radio control support helpers
1086 */
1087
1088/*
1089 * ThinkPad-ACPI firmware handling model:
1090 *
1091 * WLSW (master wireless switch) is event-driven, and is common to all
1092 * firmware-controlled radios. It cannot be controlled, just monitored,
1093 * as expected. It overrides all radio state in firmware
1094 *
1095 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1096 * (TODO: verify how WLSW interacts with the returned radio state).
1097 *
1098 * The only time there are shadow radio state changes, is when
1099 * masked-off hotkeys are used.
1100 */
1101
1102/*
1103 * Internal driver API for radio state:
1104 *
1105 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1106 * bool: true means radio blocked (off)
1107 */
1108enum tpacpi_rfkill_state {
1109 TPACPI_RFK_RADIO_OFF = 0,
1110 TPACPI_RFK_RADIO_ON
1111};
1112
1113/* rfkill switches */
1114enum tpacpi_rfk_id {
1115 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1116 TPACPI_RFK_WWAN_SW_ID,
1117 TPACPI_RFK_UWB_SW_ID,
1118 TPACPI_RFK_SW_MAX
1119};
1120
1121static const char *tpacpi_rfkill_names[] = {
1122 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1123 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1124 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1125 [TPACPI_RFK_SW_MAX] = NULL
1126};
1127
1128/* ThinkPad-ACPI rfkill subdriver */
1129struct tpacpi_rfk {
1130 struct rfkill *rfkill;
1131 enum tpacpi_rfk_id id;
1132 const struct tpacpi_rfk_ops *ops;
1133};
1134
1135struct tpacpi_rfk_ops {
1136 /* firmware interface */
1137 int (*get_status)(void);
1138 int (*set_status)(const enum tpacpi_rfkill_state);
1139};
1140
1141static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1142
1143/* Query FW and update rfkill sw state for a given rfkill switch */
1144static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1145{
1146 int status;
1147
1148 if (!tp_rfk)
1149 return -ENODEV;
1150
1151 status = (tp_rfk->ops->get_status)();
1152 if (status < 0)
1153 return status;
1154
1155 rfkill_set_sw_state(tp_rfk->rfkill,
1156 (status == TPACPI_RFK_RADIO_OFF));
1157
1158 return status;
1159}
1160
1161/* Query FW and update rfkill sw state for all rfkill switches */
1162static void tpacpi_rfk_update_swstate_all(void)
1163{
1164 unsigned int i;
1165
1166 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1167 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1168}
1169
1170/*
1171 * Sync the HW-blocking state of all rfkill switches,
1172 * do notice it causes the rfkill core to schedule uevents
1173 */
1174static void tpacpi_rfk_update_hwblock_state(bool blocked)
1175{
1176 unsigned int i;
1177 struct tpacpi_rfk *tp_rfk;
1178
1179 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1180 tp_rfk = tpacpi_rfkill_switches[i];
1181 if (tp_rfk) {
1182 if (rfkill_set_hw_state(tp_rfk->rfkill,
1183 blocked)) {
1184 /* ignore -- we track sw block */
1185 }
1186 }
1187 }
1188}
1189
1190/* Call to get the WLSW state from the firmware */
1191static int hotkey_get_wlsw(void);
1192
1193/* Call to query WLSW state and update all rfkill switches */
1194static bool tpacpi_rfk_check_hwblock_state(void)
1195{
1196 int res = hotkey_get_wlsw();
1197 int hw_blocked;
1198
1199 /* When unknown or unsupported, we have to assume it is unblocked */
1200 if (res < 0)
1201 return false;
1202
1203 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1204 tpacpi_rfk_update_hwblock_state(hw_blocked);
1205
1206 return hw_blocked;
1207}
1208
1209static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1210{
1211 struct tpacpi_rfk *tp_rfk = data;
1212 int res;
1213
1214 dbg_printk(TPACPI_DBG_RFKILL,
1215 "request to change radio state to %s\n",
1216 blocked ? "blocked" : "unblocked");
1217
1218 /* try to set radio state */
1219 res = (tp_rfk->ops->set_status)(blocked ?
1220 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1221
1222 /* and update the rfkill core with whatever the FW really did */
1223 tpacpi_rfk_update_swstate(tp_rfk);
1224
1225 return (res < 0) ? res : 0;
1226}
1227
1228static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1229 .set_block = tpacpi_rfk_hook_set_block,
1230};
1231
1232static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1233 const struct tpacpi_rfk_ops *tp_rfkops,
1234 const enum rfkill_type rfktype,
1235 const char *name,
1236 const bool set_default)
1237{
1238 struct tpacpi_rfk *atp_rfk;
1239 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001240 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001241 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001242 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001243
1244 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1245
Johannes Berg19d337d2009-06-02 13:01:37 +02001246 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1247 if (atp_rfk)
1248 atp_rfk->rfkill = rfkill_alloc(name,
1249 &tpacpi_pdev->dev,
1250 rfktype,
1251 &tpacpi_rfk_rfkill_ops,
1252 atp_rfk);
1253 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001254 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001255 kfree(atp_rfk);
1256 return -ENOMEM;
1257 }
1258
1259 atp_rfk->id = id;
1260 atp_rfk->ops = tp_rfkops;
1261
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001262 sw_status = (tp_rfkops->get_status)();
1263 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001264 pr_err("failed to read initial state for %s, error %d\n",
1265 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001266 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001267 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001268 if (set_default) {
1269 /* try to keep the initial state, since we ask the
1270 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001271 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001272 }
1273 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001274 hw_state = tpacpi_rfk_check_hwblock_state();
1275 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001276
1277 res = rfkill_register(atp_rfk->rfkill);
1278 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001279 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001280 rfkill_destroy(atp_rfk->rfkill);
1281 kfree(atp_rfk);
1282 return res;
1283 }
1284
1285 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001286
Joe Perches0978e012011-04-04 10:06:25 -07001287 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001288 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001289 return 0;
1290}
1291
1292static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1293{
1294 struct tpacpi_rfk *tp_rfk;
1295
1296 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1297
1298 tp_rfk = tpacpi_rfkill_switches[id];
1299 if (tp_rfk) {
1300 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001301 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001302 tpacpi_rfkill_switches[id] = NULL;
1303 kfree(tp_rfk);
1304 }
1305}
1306
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001307static void printk_deprecated_rfkill_attribute(const char * const what)
1308{
1309 printk_deprecated_attribute(what,
1310 "Please switch to generic rfkill before year 2010");
1311}
1312
Johannes Berg19d337d2009-06-02 13:01:37 +02001313/* sysfs <radio> enable ------------------------------------------------ */
1314static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1315 struct device_attribute *attr,
1316 char *buf)
1317{
1318 int status;
1319
1320 printk_deprecated_rfkill_attribute(attr->attr.name);
1321
1322 /* This is in the ABI... */
1323 if (tpacpi_rfk_check_hwblock_state()) {
1324 status = TPACPI_RFK_RADIO_OFF;
1325 } else {
1326 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1327 if (status < 0)
1328 return status;
1329 }
1330
1331 return snprintf(buf, PAGE_SIZE, "%d\n",
1332 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1333}
1334
1335static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1336 struct device_attribute *attr,
1337 const char *buf, size_t count)
1338{
1339 unsigned long t;
1340 int res;
1341
1342 printk_deprecated_rfkill_attribute(attr->attr.name);
1343
1344 if (parse_strtoul(buf, 1, &t))
1345 return -EINVAL;
1346
1347 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1348
1349 /* This is in the ABI... */
1350 if (tpacpi_rfk_check_hwblock_state() && !!t)
1351 return -EPERM;
1352
1353 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1354 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1355 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1356
1357 return (res < 0) ? res : count;
1358}
1359
1360/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001361static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001362{
Johannes Berg19d337d2009-06-02 13:01:37 +02001363 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001364 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001365 else {
1366 int status;
1367
1368 /* This is in the ABI... */
1369 if (tpacpi_rfk_check_hwblock_state()) {
1370 status = TPACPI_RFK_RADIO_OFF;
1371 } else {
1372 status = tpacpi_rfk_update_swstate(
1373 tpacpi_rfkill_switches[id]);
1374 if (status < 0)
1375 return status;
1376 }
1377
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001378 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001379 (status == TPACPI_RFK_RADIO_ON) ?
1380 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001381 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001382 }
1383
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001384 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001385}
1386
1387static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1388{
1389 char *cmd;
1390 int status = -1;
1391 int res = 0;
1392
1393 if (id >= TPACPI_RFK_SW_MAX)
1394 return -ENODEV;
1395
1396 while ((cmd = next_cmd(&buf))) {
1397 if (strlencmp(cmd, "enable") == 0)
1398 status = TPACPI_RFK_RADIO_ON;
1399 else if (strlencmp(cmd, "disable") == 0)
1400 status = TPACPI_RFK_RADIO_OFF;
1401 else
1402 return -EINVAL;
1403 }
1404
1405 if (status != -1) {
1406 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1407 (status == TPACPI_RFK_RADIO_ON) ?
1408 "enable" : "disable",
1409 tpacpi_rfkill_names[id]);
1410 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1411 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1412 }
1413
1414 return res;
1415}
1416
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001417/*************************************************************************
1418 * thinkpad-acpi driver attributes
1419 */
1420
1421/* interface_version --------------------------------------------------- */
1422static ssize_t tpacpi_driver_interface_version_show(
1423 struct device_driver *drv,
1424 char *buf)
1425{
1426 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1427}
1428
1429static DRIVER_ATTR(interface_version, S_IRUGO,
1430 tpacpi_driver_interface_version_show, NULL);
1431
1432/* debug_level --------------------------------------------------------- */
1433static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1434 char *buf)
1435{
1436 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1437}
1438
1439static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1440 const char *buf, size_t count)
1441{
1442 unsigned long t;
1443
1444 if (parse_strtoul(buf, 0xffff, &t))
1445 return -EINVAL;
1446
1447 dbg_level = t;
1448
1449 return count;
1450}
1451
1452static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1453 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1454
1455/* version ------------------------------------------------------------- */
1456static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1457 char *buf)
1458{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001459 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1460 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001461}
1462
1463static DRIVER_ATTR(version, S_IRUGO,
1464 tpacpi_driver_version_show, NULL);
1465
1466/* --------------------------------------------------------------------- */
1467
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001468#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1469
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001470/* wlsw_emulstate ------------------------------------------------------ */
1471static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1472 char *buf)
1473{
1474 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1475}
1476
1477static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1478 const char *buf, size_t count)
1479{
1480 unsigned long t;
1481
1482 if (parse_strtoul(buf, 1, &t))
1483 return -EINVAL;
1484
Johannes Berg19d337d2009-06-02 13:01:37 +02001485 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001486 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001487 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1488 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001489
1490 return count;
1491}
1492
1493static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1494 tpacpi_driver_wlsw_emulstate_show,
1495 tpacpi_driver_wlsw_emulstate_store);
1496
1497/* bluetooth_emulstate ------------------------------------------------- */
1498static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1499 struct device_driver *drv,
1500 char *buf)
1501{
1502 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1503}
1504
1505static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1506 struct device_driver *drv,
1507 const char *buf, size_t count)
1508{
1509 unsigned long t;
1510
1511 if (parse_strtoul(buf, 1, &t))
1512 return -EINVAL;
1513
1514 tpacpi_bluetooth_emulstate = !!t;
1515
1516 return count;
1517}
1518
1519static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1520 tpacpi_driver_bluetooth_emulstate_show,
1521 tpacpi_driver_bluetooth_emulstate_store);
1522
1523/* wwan_emulstate ------------------------------------------------- */
1524static ssize_t tpacpi_driver_wwan_emulstate_show(
1525 struct device_driver *drv,
1526 char *buf)
1527{
1528 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1529}
1530
1531static ssize_t tpacpi_driver_wwan_emulstate_store(
1532 struct device_driver *drv,
1533 const char *buf, size_t count)
1534{
1535 unsigned long t;
1536
1537 if (parse_strtoul(buf, 1, &t))
1538 return -EINVAL;
1539
1540 tpacpi_wwan_emulstate = !!t;
1541
1542 return count;
1543}
1544
1545static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1546 tpacpi_driver_wwan_emulstate_show,
1547 tpacpi_driver_wwan_emulstate_store);
1548
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001549/* uwb_emulstate ------------------------------------------------- */
1550static ssize_t tpacpi_driver_uwb_emulstate_show(
1551 struct device_driver *drv,
1552 char *buf)
1553{
1554 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1555}
1556
1557static ssize_t tpacpi_driver_uwb_emulstate_store(
1558 struct device_driver *drv,
1559 const char *buf, size_t count)
1560{
1561 unsigned long t;
1562
1563 if (parse_strtoul(buf, 1, &t))
1564 return -EINVAL;
1565
1566 tpacpi_uwb_emulstate = !!t;
1567
1568 return count;
1569}
1570
1571static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1572 tpacpi_driver_uwb_emulstate_show,
1573 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001574#endif
1575
1576/* --------------------------------------------------------------------- */
1577
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001578static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001579 &driver_attr_debug_level, &driver_attr_version,
1580 &driver_attr_interface_version,
1581};
1582
1583static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1584{
1585 int i, res;
1586
1587 i = 0;
1588 res = 0;
1589 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1590 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1591 i++;
1592 }
1593
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001594#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1595 if (!res && dbg_wlswemul)
1596 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1597 if (!res && dbg_bluetoothemul)
1598 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1599 if (!res && dbg_wwanemul)
1600 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001601 if (!res && dbg_uwbemul)
1602 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001603#endif
1604
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001605 return res;
1606}
1607
1608static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1609{
1610 int i;
1611
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001612 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001613 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001614
1615#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1616 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1617 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1618 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001619 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001620#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001621}
1622
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001623/*************************************************************************
1624 * Firmware Data
1625 */
1626
1627/*
1628 * Table of recommended minimum BIOS versions
1629 *
1630 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001631 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001632 * bugs and bad ACPI behaviour on older versions
1633 *
1634 * 2. BIOS or EC fw with known bugs that trigger on Linux
1635 *
1636 * 3. BIOS with known reduced functionality in older versions
1637 *
1638 * We recommend the latest BIOS and EC version.
1639 * We only support the latest BIOS and EC fw version as a rule.
1640 *
1641 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1642 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001643 *
1644 * WARNING: we use this table also to detect that the machine is
1645 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001646 */
1647
1648#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1649 { .vendor = (__v), \
1650 .bios = TPID(__id1, __id2), \
1651 .ec = TPACPI_MATCH_ANY, \
1652 .quirks = TPACPI_MATCH_ANY << 16 \
1653 | (__bv1) << 8 | (__bv2) }
1654
1655#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001656 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001657 { .vendor = (__v), \
1658 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001659 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001660 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1661 | (__bv1) << 8 | (__bv2) }
1662
1663#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1664 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1665
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001666/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001667#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1668 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001669 __bv1, __bv2, TPID(__id1, __id2), \
1670 __ev1, __ev2), \
1671 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1672 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1673 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001674
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001675/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001676#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1677 __eid1, __eid2, __ev1, __ev2) \
1678 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001679 __bv1, __bv2, TPID(__eid1, __eid2), \
1680 __ev1, __ev2), \
1681 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1682 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1683 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001684
1685#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1686 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1687
1688#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1689 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001690 __bv1, __bv2, TPID(__id1, __id2), \
1691 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001692
1693#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1694 __eid1, __eid2, __ev1, __ev2) \
1695 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001696 __bv1, __bv2, TPID(__eid1, __eid2), \
1697 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001698
1699static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1700 /* Numeric models ------------------ */
1701 /* FW MODEL BIOS VERS */
1702 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1703 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1704 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1705 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1706 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1707 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1708 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1709 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1710 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1711
1712 /* A-series ------------------------- */
1713 /* FW MODEL BIOS VERS EC VERS */
1714 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1715 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1716 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1717 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1718 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1719 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1720 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1721 TPV_QI0('1', '3', '2', '0'), /* A22m */
1722 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1723 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1724 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1725
1726 /* G-series ------------------------- */
1727 /* FW MODEL BIOS VERS */
1728 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1729 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1730
1731 /* R-series, T-series --------------- */
1732 /* FW MODEL BIOS VERS EC VERS */
1733 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1734 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1735 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1736 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1737 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1738 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1739 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1740 T40/p, T41/p, T42/p (1) */
1741 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1742 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1743 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1744 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1745
1746 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1747 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1748 TPV_QI0('1', '6', '3', '2'), /* T22 */
1749 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1750 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1751 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1752
1753 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1754 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001755 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001756
1757 /* BIOS FW BIOS VERS EC FW EC VERS */
1758 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1759 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1760
1761 /* X-series ------------------------- */
1762 /* FW MODEL BIOS VERS EC VERS */
1763 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1764 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1765 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1766 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1767 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1768 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1769 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1770
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001771 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1772 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001773
1774 /* (0) - older versions lack DMI EC fw string and functionality */
1775 /* (1) - older versions known to lack functionality */
1776};
1777
1778#undef TPV_QL1
1779#undef TPV_QL0
1780#undef TPV_QI2
1781#undef TPV_QI1
1782#undef TPV_QI0
1783#undef TPV_Q_X
1784#undef TPV_Q
1785
1786static void __init tpacpi_check_outdated_fw(void)
1787{
1788 unsigned long fwvers;
1789 u16 ec_version, bios_version;
1790
1791 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1792 ARRAY_SIZE(tpacpi_bios_version_qtable));
1793
1794 if (!fwvers)
1795 return;
1796
1797 bios_version = fwvers & 0xffffU;
1798 ec_version = (fwvers >> 16) & 0xffffU;
1799
1800 /* note that unknown versions are set to 0x0000 and we use that */
1801 if ((bios_version > thinkpad_id.bios_release) ||
1802 (ec_version > thinkpad_id.ec_release &&
1803 ec_version != TPACPI_MATCH_ANY)) {
1804 /*
1805 * The changelogs would let us track down the exact
1806 * reason, but it is just too much of a pain to track
1807 * it. We only list BIOSes that are either really
1808 * broken, or really stable to begin with, so it is
1809 * best if the user upgrades the firmware anyway.
1810 */
Joe Perches0978e012011-04-04 10:06:25 -07001811 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1812 pr_warn("WARNING: This firmware may be missing critical bug "
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001813 "fixes and/or important features\n");
1814 }
1815}
1816
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001817static bool __init tpacpi_is_fw_known(void)
1818{
1819 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1820 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1821}
1822
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001823/****************************************************************************
1824 ****************************************************************************
1825 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001826 * Subdrivers
1827 *
1828 ****************************************************************************
1829 ****************************************************************************/
1830
1831/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001832 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001833 */
1834
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001835static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001837 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1838 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001842static struct ibm_struct thinkpad_acpi_driver_data = {
1843 .name = "driver",
1844 .read = thinkpad_acpi_driver_read,
1845};
1846
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001847/*************************************************************************
1848 * Hotkey subdriver
1849 */
1850
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001851/*
1852 * ThinkPad firmware event model
1853 *
1854 * The ThinkPad firmware has two main event interfaces: normal ACPI
1855 * notifications (which follow the ACPI standard), and a private event
1856 * interface.
1857 *
1858 * The private event interface also issues events for the hotkeys. As
1859 * the driver gained features, the event handling code ended up being
1860 * built around the hotkey subdriver. This will need to be refactored
1861 * to a more formal event API eventually.
1862 *
1863 * Some "hotkeys" are actually supposed to be used as event reports,
1864 * such as "brightness has changed", "volume has changed", depending on
1865 * the ThinkPad model and how the firmware is operating.
1866 *
1867 * Unlike other classes, hotkey-class events have mask/unmask control on
1868 * non-ancient firmware. However, how it behaves changes a lot with the
1869 * firmware model and version.
1870 */
1871
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001872enum { /* hot key scan codes (derived from ACPI DSDT) */
1873 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1874 TP_ACPI_HOTKEYSCAN_FNF2,
1875 TP_ACPI_HOTKEYSCAN_FNF3,
1876 TP_ACPI_HOTKEYSCAN_FNF4,
1877 TP_ACPI_HOTKEYSCAN_FNF5,
1878 TP_ACPI_HOTKEYSCAN_FNF6,
1879 TP_ACPI_HOTKEYSCAN_FNF7,
1880 TP_ACPI_HOTKEYSCAN_FNF8,
1881 TP_ACPI_HOTKEYSCAN_FNF9,
1882 TP_ACPI_HOTKEYSCAN_FNF10,
1883 TP_ACPI_HOTKEYSCAN_FNF11,
1884 TP_ACPI_HOTKEYSCAN_FNF12,
1885 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1886 TP_ACPI_HOTKEYSCAN_FNINSERT,
1887 TP_ACPI_HOTKEYSCAN_FNDELETE,
1888 TP_ACPI_HOTKEYSCAN_FNHOME,
1889 TP_ACPI_HOTKEYSCAN_FNEND,
1890 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1891 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1892 TP_ACPI_HOTKEYSCAN_FNSPACE,
1893 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1894 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1895 TP_ACPI_HOTKEYSCAN_MUTE,
1896 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d2010-08-09 23:48:20 -03001897 TP_ACPI_HOTKEYSCAN_UNK1,
1898 TP_ACPI_HOTKEYSCAN_UNK2,
1899 TP_ACPI_HOTKEYSCAN_UNK3,
1900 TP_ACPI_HOTKEYSCAN_UNK4,
1901 TP_ACPI_HOTKEYSCAN_UNK5,
1902 TP_ACPI_HOTKEYSCAN_UNK6,
1903 TP_ACPI_HOTKEYSCAN_UNK7,
1904 TP_ACPI_HOTKEYSCAN_UNK8,
1905
1906 /* Hotkey keymap size */
1907 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001908};
1909
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001910enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001911 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1912 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1913};
1914
1915enum { /* Positions of some of the keys in hotkey masks */
1916 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1917 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1918 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1919 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1920 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1921 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1922 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1923 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1924 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1925 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1926 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1927};
1928
1929enum { /* NVRAM to ACPI HKEY group map */
1930 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1931 TP_ACPI_HKEY_ZOOM_MASK |
1932 TP_ACPI_HKEY_DISPSWTCH_MASK |
1933 TP_ACPI_HKEY_HIBERNATE_MASK,
1934 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1935 TP_ACPI_HKEY_BRGHTDWN_MASK,
1936 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1937 TP_ACPI_HKEY_VOLDWN_MASK |
1938 TP_ACPI_HKEY_MUTE_MASK,
1939};
1940
1941#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1942struct tp_nvram_state {
1943 u16 thinkpad_toggle:1;
1944 u16 zoom_toggle:1;
1945 u16 display_toggle:1;
1946 u16 thinklight_toggle:1;
1947 u16 hibernate_toggle:1;
1948 u16 displayexp_toggle:1;
1949 u16 display_state:1;
1950 u16 brightness_toggle:1;
1951 u16 volume_toggle:1;
1952 u16 mute:1;
1953
1954 u8 brightness_level;
1955 u8 volume_level;
1956};
1957
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001958/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001959static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001960
1961/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001962static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001963
1964/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001965 * Acquire mutex to write poller control variables as an
1966 * atomic block.
1967 *
1968 * Increment hotkey_config_change when changing them if you
1969 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001970 *
1971 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1972 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001973static struct mutex hotkey_thread_data_mutex;
1974static unsigned int hotkey_config_change;
1975
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001976/*
1977 * hotkey poller control variables
1978 *
1979 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001980 *
1981 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1982 * should be used only when the changes need to be taken as
1983 * a block, OR when one needs to force the kthread to forget
1984 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001985 */
1986static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1987static unsigned int hotkey_poll_freq = 10; /* Hz */
1988
1989#define HOTKEY_CONFIG_CRITICAL_START \
1990 do { \
1991 mutex_lock(&hotkey_thread_data_mutex); \
1992 hotkey_config_change++; \
1993 } while (0);
1994#define HOTKEY_CONFIG_CRITICAL_END \
1995 mutex_unlock(&hotkey_thread_data_mutex);
1996
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001997#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1998
1999#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002000#define HOTKEY_CONFIG_CRITICAL_START
2001#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002002
2003#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2004
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002005static struct mutex hotkey_mutex;
2006
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002007static enum { /* Reasons for waking up */
2008 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2009 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2010 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2011} hotkey_wakeup_reason;
2012
2013static int hotkey_autosleep_ack;
2014
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002015static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2016static u32 hotkey_all_mask; /* all events supported in fw */
2017static u32 hotkey_reserved_mask; /* events better left disabled */
2018static u32 hotkey_driver_mask; /* events needed by the driver */
2019static u32 hotkey_user_mask; /* events visible to userspace */
2020static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002021
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02002022static unsigned int hotkey_report_mode;
2023
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002024static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002025
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03002026static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002027
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002028static void tpacpi_driver_event(const unsigned int hkey_event);
2029static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002030static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002031
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002032/* HKEY.MHKG() return bits */
2033#define TP_HOTKEY_TABLET_MASK (1 << 3)
2034
Johannes Berg19d337d2009-06-02 13:01:37 +02002035static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002036{
Johannes Berg19d337d2009-06-02 13:01:37 +02002037 int status;
2038
2039 if (!tp_features.hotkey_wlsw)
2040 return -ENODEV;
2041
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002042#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002043 if (dbg_wlswemul)
2044 return (tpacpi_wlsw_emulstate) ?
2045 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002046#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002047
2048 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002049 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002050
2051 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002052}
2053
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002054static int hotkey_get_tablet_mode(int *status)
2055{
2056 int s;
2057
2058 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2059 return -EIO;
2060
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002061 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2062 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002063}
2064
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002065/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002066 * Reads current event mask from firmware, and updates
2067 * hotkey_acpi_mask accordingly. Also resets any bits
2068 * from hotkey_user_mask that are unavailable to be
2069 * delivered (shadow requirement of the userspace ABI).
2070 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002071 * Call with hotkey_mutex held
2072 */
2073static int hotkey_mask_get(void)
2074{
2075 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002076 u32 m = 0;
2077
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002078 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002079 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002080
2081 hotkey_acpi_mask = m;
2082 } else {
2083 /* no mask support doesn't mean no event support... */
2084 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002085 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002086
2087 /* sync userspace-visible mask */
2088 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002089
2090 return 0;
2091}
2092
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002093void static hotkey_mask_warn_incomplete_mask(void)
2094{
2095 /* log only what the user can fix... */
2096 const u32 wantedmask = hotkey_driver_mask &
2097 ~(hotkey_acpi_mask | hotkey_source_mask) &
2098 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2099
2100 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002101 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002102}
2103
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002104/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002105 * Set the firmware mask when supported
2106 *
2107 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2108 *
2109 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2110 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002111 * Call with hotkey_mutex held
2112 */
2113static int hotkey_mask_set(u32 mask)
2114{
2115 int i;
2116 int rc = 0;
2117
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002118 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002119
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002120 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002121 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002122 if (!acpi_evalf(hkey_handle,
2123 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002124 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002125 rc = -EIO;
2126 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002127 }
2128 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002129 }
2130
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002131 /*
2132 * We *must* make an inconditional call to hotkey_mask_get to
2133 * refresh hotkey_acpi_mask and update hotkey_user_mask
2134 *
2135 * Take the opportunity to also log when we cannot _enable_
2136 * a given event.
2137 */
2138 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Joe Perches0978e012011-04-04 10:06:25 -07002139 pr_notice("asked for hotkey mask 0x%08x, but "
2140 "firmware forced it to 0x%08x\n",
2141 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002142 }
2143
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002144 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2145 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002146
2147 return rc;
2148}
2149
2150/*
2151 * Sets hotkey_user_mask and tries to set the firmware mask
2152 *
2153 * Call with hotkey_mutex held
2154 */
2155static int hotkey_user_mask_set(const u32 mask)
2156{
2157 int rc;
2158
2159 /* Give people a chance to notice they are doing something that
2160 * is bound to go boom on their users sooner or later */
2161 if (!tp_warned.hotkey_mask_ff &&
2162 (mask == 0xffff || mask == 0xffffff ||
2163 mask == 0xffffffff)) {
2164 tp_warned.hotkey_mask_ff = 1;
Joe Perches0978e012011-04-04 10:06:25 -07002165 pr_notice("setting the hotkey mask to 0x%08x is likely "
2166 "not the best way to go about it\n", mask);
2167 pr_notice("please consider using the driver defaults, "
2168 "and refer to up-to-date thinkpad-acpi "
2169 "documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002170 }
2171
2172 /* Try to enable what the user asked for, plus whatever we need.
2173 * this syncs everything but won't enable bits in hotkey_user_mask */
2174 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2175
2176 /* Enable the available bits in hotkey_user_mask */
2177 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2178
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002179 return rc;
2180}
2181
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002182/*
2183 * Sets the driver hotkey mask.
2184 *
2185 * Can be called even if the hotkey subdriver is inactive
2186 */
2187static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2188{
2189 int rc;
2190
2191 /* Do the right thing if hotkey_init has not been called yet */
2192 if (!tp_features.hotkey) {
2193 hotkey_driver_mask = mask;
2194 return 0;
2195 }
2196
2197 mutex_lock(&hotkey_mutex);
2198
2199 HOTKEY_CONFIG_CRITICAL_START
2200 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002201#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002202 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002203#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002204 HOTKEY_CONFIG_CRITICAL_END
2205
2206 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2207 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002208 hotkey_poll_setup(true);
2209
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002210 mutex_unlock(&hotkey_mutex);
2211
2212 return rc;
2213}
2214
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002215static int hotkey_status_get(int *status)
2216{
2217 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2218 return -EIO;
2219
2220 return 0;
2221}
2222
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002223static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002224{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002225 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002226 return -EIO;
2227
2228 return 0;
2229}
2230
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002231static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002232{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002233 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002234
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002235 if (tp_features.hotkey_tablet &&
2236 !hotkey_get_tablet_mode(&state)) {
2237 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002238
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002239 input_report_switch(tpacpi_inputdev,
2240 SW_TABLET_MODE, !!state);
2241 input_sync(tpacpi_inputdev);
2242
2243 mutex_unlock(&tpacpi_inputdev_send_mutex);
2244 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002245}
2246
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002247/* Do NOT call without validating scancode first */
2248static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002249{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002250 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002251
2252 if (keycode != KEY_RESERVED) {
2253 mutex_lock(&tpacpi_inputdev_send_mutex);
2254
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002255 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002256 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002257 input_sync(tpacpi_inputdev);
2258
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002259 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002260 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002261 input_sync(tpacpi_inputdev);
2262
2263 mutex_unlock(&tpacpi_inputdev_send_mutex);
2264 }
2265}
2266
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002267/* Do NOT call without validating scancode first */
2268static void tpacpi_input_send_key_masked(const unsigned int scancode)
2269{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002270 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002271 if (hotkey_user_mask & (1 << scancode))
2272 tpacpi_input_send_key(scancode);
2273}
2274
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002275#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2276static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2277
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002278/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002279static void tpacpi_hotkey_send_key(unsigned int scancode)
2280{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002281 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002282 if (hotkey_report_mode < 2) {
2283 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03002284 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002285 }
2286}
2287
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002288static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002289{
2290 u8 d;
2291
2292 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2293 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2294 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2295 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2296 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2297 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2298 }
2299 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2300 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2301 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2302 }
2303 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2304 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2305 n->displayexp_toggle =
2306 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2307 }
2308 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2309 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2310 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2311 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2312 n->brightness_toggle =
2313 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2314 }
2315 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2316 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2317 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2318 >> TP_NVRAM_POS_LEVEL_VOLUME;
2319 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2320 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2321 }
2322}
2323
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002324static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2325 struct tp_nvram_state *newn,
2326 const u32 event_mask)
2327{
2328
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002329#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002330 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002331 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002332 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002333 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002334 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002335
2336#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002337 do { \
2338 if (event_mask & (1 << __scancode)) \
2339 tpacpi_hotkey_send_key(__scancode); \
2340 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002341
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002342 void issue_volchange(const unsigned int oldvol,
2343 const unsigned int newvol)
2344 {
2345 unsigned int i = oldvol;
2346
2347 while (i > newvol) {
2348 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2349 i--;
2350 }
2351 while (i < newvol) {
2352 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2353 i++;
2354 }
2355 }
2356
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002357 void issue_brightnesschange(const unsigned int oldbrt,
2358 const unsigned int newbrt)
2359 {
2360 unsigned int i = oldbrt;
2361
2362 while (i > newbrt) {
2363 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2364 i--;
2365 }
2366 while (i < newbrt) {
2367 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2368 i++;
2369 }
2370 }
2371
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002372 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2373 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2374 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2375 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2376
2377 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2378
2379 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2380
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002381 /*
2382 * Handle volume
2383 *
2384 * This code is supposed to duplicate the IBM firmware behaviour:
2385 * - Pressing MUTE issues mute hotkey message, even when already mute
2386 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002387 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002388 * - The act of unmuting issues volume up/down notification,
2389 * depending which key was used to unmute
2390 *
2391 * We are constrained to what the NVRAM can tell us, which is not much
2392 * and certainly not enough if more than one volume hotkey was pressed
2393 * since the last poll cycle.
2394 *
2395 * Just to make our life interesting, some newer Lenovo ThinkPads have
2396 * bugs in the BIOS and may fail to update volume_toggle properly.
2397 */
2398 if (newn->mute) {
2399 /* muted */
2400 if (!oldn->mute ||
2401 oldn->volume_toggle != newn->volume_toggle ||
2402 oldn->volume_level != newn->volume_level) {
2403 /* recently muted, or repeated mute keypress, or
2404 * multiple presses ending in mute */
2405 issue_volchange(oldn->volume_level, newn->volume_level);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002406 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2407 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002408 } else {
2409 /* unmute */
2410 if (oldn->mute) {
2411 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002412 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002413 }
2414 if (oldn->volume_level != newn->volume_level) {
2415 issue_volchange(oldn->volume_level, newn->volume_level);
2416 } else if (oldn->volume_toggle != newn->volume_toggle) {
2417 /* repeated vol up/down keypress at end of scale ? */
2418 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002419 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002420 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002422 }
2423 }
2424
2425 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002426 if (oldn->brightness_level != newn->brightness_level) {
2427 issue_brightnesschange(oldn->brightness_level,
2428 newn->brightness_level);
2429 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2430 /* repeated key presses that didn't change state */
2431 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002432 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002433 else if (newn->brightness_level >= bright_maxlvl
2434 && !tp_features.bright_unkfw)
2435 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002436 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002437
2438#undef TPACPI_COMPARE_KEY
2439#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002440}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002441
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002442/*
2443 * Polling driver
2444 *
2445 * We track all events in hotkey_source_mask all the time, since
2446 * most of them are edge-based. We only issue those requested by
2447 * hotkey_user_mask or hotkey_driver_mask, though.
2448 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002449static int hotkey_kthread(void *data)
2450{
2451 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002452 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002453 unsigned int si, so;
2454 unsigned long t;
2455 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002456 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002457
2458 mutex_lock(&hotkey_thread_mutex);
2459
2460 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2461 goto exit;
2462
2463 set_freezable();
2464
2465 so = 0;
2466 si = 1;
2467 t = 0;
2468
2469 /* Initial state for compares */
2470 mutex_lock(&hotkey_thread_data_mutex);
2471 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002472 poll_mask = hotkey_source_mask;
2473 event_mask = hotkey_source_mask &
2474 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002475 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002476 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002477 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002478
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002479 while (!kthread_should_stop()) {
2480 if (t == 0) {
2481 if (likely(poll_freq))
2482 t = 1000/poll_freq;
2483 else
2484 t = 100; /* should never happen... */
2485 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002486 t = msleep_interruptible(t);
2487 if (unlikely(kthread_should_stop()))
2488 break;
2489 must_reset = try_to_freeze();
2490 if (t > 0 && !must_reset)
2491 continue;
2492
2493 mutex_lock(&hotkey_thread_data_mutex);
2494 if (must_reset || hotkey_config_change != change_detector) {
2495 /* forget old state on thaw or config change */
2496 si = so;
2497 t = 0;
2498 change_detector = hotkey_config_change;
2499 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002500 poll_mask = hotkey_source_mask;
2501 event_mask = hotkey_source_mask &
2502 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002503 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002504 mutex_unlock(&hotkey_thread_data_mutex);
2505
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002506 if (likely(poll_mask)) {
2507 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002508 if (likely(si != so)) {
2509 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002510 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002511 }
2512 }
2513
2514 so = si;
2515 si ^= 1;
2516 }
2517
2518exit:
2519 mutex_unlock(&hotkey_thread_mutex);
2520 return 0;
2521}
2522
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002523/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002524static void hotkey_poll_stop_sync(void)
2525{
2526 if (tpacpi_hotkey_task) {
2527 if (frozen(tpacpi_hotkey_task) ||
2528 freezing(tpacpi_hotkey_task))
2529 thaw_process(tpacpi_hotkey_task);
2530
2531 kthread_stop(tpacpi_hotkey_task);
2532 tpacpi_hotkey_task = NULL;
2533 mutex_lock(&hotkey_thread_mutex);
2534 /* at this point, the thread did exit */
2535 mutex_unlock(&hotkey_thread_mutex);
2536 }
2537}
2538
2539/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002540static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002541{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002542 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2543 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002544
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002545 if (hotkey_poll_freq > 0 &&
2546 (poll_driver_mask ||
2547 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002548 if (!tpacpi_hotkey_task) {
2549 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002550 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002551 if (IS_ERR(tpacpi_hotkey_task)) {
2552 tpacpi_hotkey_task = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07002553 pr_err("could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002554 "for hotkey polling\n");
2555 }
2556 }
2557 } else {
2558 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002559 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002560 hotkey_poll_freq == 0) {
Joe Perches0978e012011-04-04 10:06:25 -07002561 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2562 "require polling, which is currently "
2563 "disabled\n",
2564 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002565 }
2566 }
2567}
2568
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002569static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002570{
2571 mutex_lock(&hotkey_mutex);
2572 hotkey_poll_setup(may_warn);
2573 mutex_unlock(&hotkey_mutex);
2574}
2575
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002576/* call with hotkey_mutex held */
2577static void hotkey_poll_set_freq(unsigned int freq)
2578{
2579 if (!freq)
2580 hotkey_poll_stop_sync();
2581
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002582 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002583}
2584
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002585#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2586
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002587static void hotkey_poll_setup(const bool __unused)
2588{
2589}
2590
2591static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002592{
2593}
2594
2595#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2596
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002597static int hotkey_inputdev_open(struct input_dev *dev)
2598{
2599 switch (tpacpi_lifecycle) {
2600 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002601 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002602 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002603 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002604 case TPACPI_LIFE_EXITING:
2605 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002606 }
2607
2608 /* Should only happen if tpacpi_lifecycle is corrupt */
2609 BUG();
2610 return -EBUSY;
2611}
2612
2613static void hotkey_inputdev_close(struct input_dev *dev)
2614{
2615 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002616 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002617 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002618 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002619}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002620
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002621/* sysfs hotkey enable ------------------------------------------------- */
2622static ssize_t hotkey_enable_show(struct device *dev,
2623 struct device_attribute *attr,
2624 char *buf)
2625{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002626 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002627
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002628 printk_deprecated_attribute("hotkey_enable",
2629 "Hotkey reporting is always enabled");
2630
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002631 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002632 if (res)
2633 return res;
2634
2635 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2636}
2637
2638static ssize_t hotkey_enable_store(struct device *dev,
2639 struct device_attribute *attr,
2640 const char *buf, size_t count)
2641{
2642 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002643
2644 printk_deprecated_attribute("hotkey_enable",
2645 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002646
2647 if (parse_strtoul(buf, 1, &t))
2648 return -EINVAL;
2649
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002650 if (t == 0)
2651 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002652
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002653 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002654}
2655
2656static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002657 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002658 hotkey_enable_show, hotkey_enable_store);
2659
2660/* sysfs hotkey mask --------------------------------------------------- */
2661static ssize_t hotkey_mask_show(struct device *dev,
2662 struct device_attribute *attr,
2663 char *buf)
2664{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002665 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002666}
2667
2668static ssize_t hotkey_mask_store(struct device *dev,
2669 struct device_attribute *attr,
2670 const char *buf, size_t count)
2671{
2672 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002673 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002674
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002675 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002676 return -EINVAL;
2677
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002678 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002679 return -ERESTARTSYS;
2680
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002681 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002682
2683#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002684 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002685#endif
2686
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002687 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002688
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002689 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2690
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002691 return (res) ? res : count;
2692}
2693
2694static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002695 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002696 hotkey_mask_show, hotkey_mask_store);
2697
2698/* sysfs hotkey bios_enabled ------------------------------------------- */
2699static ssize_t hotkey_bios_enabled_show(struct device *dev,
2700 struct device_attribute *attr,
2701 char *buf)
2702{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002703 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002704}
2705
2706static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002707 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002708
2709/* sysfs hotkey bios_mask ---------------------------------------------- */
2710static ssize_t hotkey_bios_mask_show(struct device *dev,
2711 struct device_attribute *attr,
2712 char *buf)
2713{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002714 printk_deprecated_attribute("hotkey_bios_mask",
2715 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002716 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002717}
2718
2719static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002720 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002721
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002722/* sysfs hotkey all_mask ----------------------------------------------- */
2723static ssize_t hotkey_all_mask_show(struct device *dev,
2724 struct device_attribute *attr,
2725 char *buf)
2726{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002727 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2728 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002729}
2730
2731static struct device_attribute dev_attr_hotkey_all_mask =
2732 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2733
2734/* sysfs hotkey recommended_mask --------------------------------------- */
2735static ssize_t hotkey_recommended_mask_show(struct device *dev,
2736 struct device_attribute *attr,
2737 char *buf)
2738{
2739 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002740 (hotkey_all_mask | hotkey_source_mask)
2741 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002742}
2743
2744static struct device_attribute dev_attr_hotkey_recommended_mask =
2745 __ATTR(hotkey_recommended_mask, S_IRUGO,
2746 hotkey_recommended_mask_show, NULL);
2747
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002748#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2749
2750/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2751static ssize_t hotkey_source_mask_show(struct device *dev,
2752 struct device_attribute *attr,
2753 char *buf)
2754{
2755 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2756}
2757
2758static ssize_t hotkey_source_mask_store(struct device *dev,
2759 struct device_attribute *attr,
2760 const char *buf, size_t count)
2761{
2762 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002763 u32 r_ev;
2764 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002765
2766 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2767 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2768 return -EINVAL;
2769
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002770 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002771 return -ERESTARTSYS;
2772
2773 HOTKEY_CONFIG_CRITICAL_START
2774 hotkey_source_mask = t;
2775 HOTKEY_CONFIG_CRITICAL_END
2776
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002777 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2778 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002779 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002780
2781 /* check if events needed by the driver got disabled */
2782 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2783 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002784
2785 mutex_unlock(&hotkey_mutex);
2786
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002787 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07002788 pr_err("hotkey_source_mask: "
2789 "failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002790
2791 if (r_ev)
Joe Perches0978e012011-04-04 10:06:25 -07002792 pr_notice("hotkey_source_mask: "
2793 "some important events were disabled: 0x%04x\n",
2794 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002795
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002796 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2797
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002798 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002799}
2800
2801static struct device_attribute dev_attr_hotkey_source_mask =
2802 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2803 hotkey_source_mask_show, hotkey_source_mask_store);
2804
2805/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2806static ssize_t hotkey_poll_freq_show(struct device *dev,
2807 struct device_attribute *attr,
2808 char *buf)
2809{
2810 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2811}
2812
2813static ssize_t hotkey_poll_freq_store(struct device *dev,
2814 struct device_attribute *attr,
2815 const char *buf, size_t count)
2816{
2817 unsigned long t;
2818
2819 if (parse_strtoul(buf, 25, &t))
2820 return -EINVAL;
2821
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002822 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002823 return -ERESTARTSYS;
2824
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002825 hotkey_poll_set_freq(t);
2826 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002827
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002828 mutex_unlock(&hotkey_mutex);
2829
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002830 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2831
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002832 return count;
2833}
2834
2835static struct device_attribute dev_attr_hotkey_poll_freq =
2836 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2837 hotkey_poll_freq_show, hotkey_poll_freq_store);
2838
2839#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2840
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002841/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002842static ssize_t hotkey_radio_sw_show(struct device *dev,
2843 struct device_attribute *attr,
2844 char *buf)
2845{
Johannes Berg19d337d2009-06-02 13:01:37 +02002846 int res;
2847 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002848 if (res < 0)
2849 return res;
2850
Johannes Berg19d337d2009-06-02 13:01:37 +02002851 /* Opportunistic update */
2852 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2853
2854 return snprintf(buf, PAGE_SIZE, "%d\n",
2855 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002856}
2857
2858static struct device_attribute dev_attr_hotkey_radio_sw =
2859 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2860
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002861static void hotkey_radio_sw_notify_change(void)
2862{
2863 if (tp_features.hotkey_wlsw)
2864 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2865 "hotkey_radio_sw");
2866}
2867
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002868/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2869static ssize_t hotkey_tablet_mode_show(struct device *dev,
2870 struct device_attribute *attr,
2871 char *buf)
2872{
2873 int res, s;
2874 res = hotkey_get_tablet_mode(&s);
2875 if (res < 0)
2876 return res;
2877
2878 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2879}
2880
2881static struct device_attribute dev_attr_hotkey_tablet_mode =
2882 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2883
2884static void hotkey_tablet_mode_notify_change(void)
2885{
2886 if (tp_features.hotkey_tablet)
2887 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2888 "hotkey_tablet_mode");
2889}
2890
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002891/* sysfs hotkey report_mode -------------------------------------------- */
2892static ssize_t hotkey_report_mode_show(struct device *dev,
2893 struct device_attribute *attr,
2894 char *buf)
2895{
2896 return snprintf(buf, PAGE_SIZE, "%d\n",
2897 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2898}
2899
2900static struct device_attribute dev_attr_hotkey_report_mode =
2901 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2902
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002903/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002904static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2905 struct device_attribute *attr,
2906 char *buf)
2907{
2908 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2909}
2910
2911static struct device_attribute dev_attr_hotkey_wakeup_reason =
2912 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2913
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002914static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002915{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002916 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2917 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002918}
2919
2920/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002921static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2922 struct device_attribute *attr,
2923 char *buf)
2924{
2925 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2926}
2927
2928static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2929 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2930 hotkey_wakeup_hotunplug_complete_show, NULL);
2931
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002932static void hotkey_wakeup_hotunplug_complete_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_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002936}
2937
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002938/* --------------------------------------------------------------------- */
2939
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002940static struct attribute *hotkey_attributes[] __initdata = {
2941 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002942 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002943 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002944 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002945 &dev_attr_hotkey_wakeup_reason.attr,
2946 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002947 &dev_attr_hotkey_mask.attr,
2948 &dev_attr_hotkey_all_mask.attr,
2949 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002950#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002951 &dev_attr_hotkey_source_mask.attr,
2952 &dev_attr_hotkey_poll_freq.attr,
2953#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002954};
2955
Johannes Berg19d337d2009-06-02 13:01:37 +02002956/*
2957 * Sync both the hw and sw blocking state of all switches
2958 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002959static void tpacpi_send_radiosw_update(void)
2960{
2961 int wlsw;
2962
Johannes Berg19d337d2009-06-02 13:01:37 +02002963 /*
2964 * We must sync all rfkill controllers *before* issuing any
2965 * rfkill input events, or we will race the rfkill core input
2966 * handler.
2967 *
Lucas De Marchic8440332011-03-17 17:18:22 -03002968 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02002969 * for the above.
2970 *
2971 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2972 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002973
Johannes Berg19d337d2009-06-02 13:01:37 +02002974 wlsw = hotkey_get_wlsw();
2975
2976 /* Sync hw blocking state first if it is hw-blocked */
2977 if (wlsw == TPACPI_RFK_RADIO_OFF)
2978 tpacpi_rfk_update_hwblock_state(true);
2979
2980 /* Sync sw blocking state */
2981 tpacpi_rfk_update_swstate_all();
2982
2983 /* Sync hw blocking state last if it is hw-unblocked */
2984 if (wlsw == TPACPI_RFK_RADIO_ON)
2985 tpacpi_rfk_update_hwblock_state(false);
2986
2987 /* Issue rfkill input event for WLSW switch */
2988 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002989 mutex_lock(&tpacpi_inputdev_send_mutex);
2990
2991 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002992 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002993 input_sync(tpacpi_inputdev);
2994
2995 mutex_unlock(&tpacpi_inputdev_send_mutex);
2996 }
Johannes Berg19d337d2009-06-02 13:01:37 +02002997
2998 /*
2999 * this can be unconditional, as we will poll state again
3000 * if userspace uses the notify to read data
3001 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003002 hotkey_radio_sw_notify_change();
3003}
3004
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003005static void hotkey_exit(void)
3006{
3007#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003008 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003009 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003010 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003011#endif
3012
3013 if (hotkey_dev_attributes)
3014 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3015
3016 kfree(hotkey_keycode_map);
3017
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003018 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003019 "restoring original HKEY status and mask\n");
3020 /* yes, there is a bitwise or below, we want the
3021 * functions to be called even if one of them fail */
3022 if (((tp_features.hotkey_mask &&
3023 hotkey_mask_set(hotkey_orig_mask)) |
3024 hotkey_status_set(false)) != 0)
Joe Perches0978e012011-04-04 10:06:25 -07003025 pr_err("failed to restore hot key mask "
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003026 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003027}
3028
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003029static void __init hotkey_unmap(const unsigned int scancode)
3030{
3031 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3032 clear_bit(hotkey_keycode_map[scancode],
3033 tpacpi_inputdev->keybit);
3034 hotkey_keycode_map[scancode] = KEY_RESERVED;
3035 }
3036}
3037
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003038/*
3039 * HKEY quirks:
3040 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3041 */
3042
3043#define TPACPI_HK_Q_INIMASK 0x0001
3044
3045static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3046 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3047 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3048 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3049 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3050 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3051 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3052 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3053 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3054 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3055 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3056 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3057 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3058 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3059 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3060 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3061 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3062 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3063 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3064 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3065};
3066
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003067typedef u16 tpacpi_keymap_entry_t;
3068typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003069
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003070static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003071{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003072 /* Requirements for changing the default keymaps:
3073 *
3074 * 1. Many of the keys are mapped to KEY_RESERVED for very
3075 * good reasons. Do not change them unless you have deep
3076 * knowledge on the IBM and Lenovo ThinkPad firmware for
3077 * the various ThinkPad models. The driver behaves
3078 * differently for KEY_RESERVED: such keys have their
3079 * hot key mask *unset* in mask_recommended, and also
3080 * in the initial hot key mask programmed into the
3081 * firmware at driver load time, which means the firm-
3082 * ware may react very differently if you change them to
3083 * something else;
3084 *
3085 * 2. You must be subscribed to the linux-thinkpad and
3086 * ibm-acpi-devel mailing lists, and you should read the
3087 * list archives since 2007 if you want to change the
3088 * keymaps. This requirement exists so that you will
3089 * know the past history of problems with the thinkpad-
3090 * acpi driver keymaps, and also that you will be
3091 * listening to any bug reports;
3092 *
3093 * 3. Do not send thinkpad-acpi specific patches directly to
3094 * for merging, *ever*. Send them to the linux-acpi
3095 * mailinglist for comments. Merging is to be done only
3096 * through acpi-test and the ACPI maintainer.
3097 *
3098 * If the above is too much to ask, don't change the keymap.
3099 * Ask the thinkpad-acpi maintainer to do it, instead.
3100 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003101
3102 enum keymap_index {
3103 TPACPI_KEYMAP_IBM_GENERIC = 0,
3104 TPACPI_KEYMAP_LENOVO_GENERIC,
3105 };
3106
3107 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3108 /* Generic keymap for IBM ThinkPads */
3109 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003110 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003111 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003112 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3113 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003114
3115 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003116 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3117 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3118 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003119
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003120 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003121 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003122 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003123
3124 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003125 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003126
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003127 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3128 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003129
3130 /* Volume: firmware always react to it and reprograms
3131 * the built-in *extra* mixer. Never map it to control
3132 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003133 KEY_RESERVED, /* 0x14: VOLUME UP */
3134 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3135 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003136
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003137 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003138
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003139 /* (assignments unknown, please report if found) */
3140 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3141 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003142 },
3143
3144 /* Generic keymap for Lenovo ThinkPads */
3145 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003146 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3147 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003148 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003149 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003150
3151 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003152 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3153 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3154 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003155
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003156 /* These should be enabled --only-- when ACPI video
3157 * is disabled (i.e. in "vendor" mode), and are handled
3158 * in a special way by the init code */
3159 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3160 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003161
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003162 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003163
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003164 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3165 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003166
3167 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3168 * react to it and reprograms the built-in *extra* mixer.
3169 * Never map it to control another mixer by default.
3170 *
3171 * T60?, T61, R60?, R61: firmware and EC tries to send
3172 * these over the regular keyboard, so these are no-ops,
3173 * but there are still weird bugs re. MUTE, so do not
3174 * change unless you get test reports from all Lenovo
3175 * models. May cause the BIOS to interfere with the
3176 * HDA mixer.
3177 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003178 KEY_RESERVED, /* 0x14: VOLUME UP */
3179 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3180 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003181
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003182 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003183
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003184 /* (assignments unknown, please report if found) */
3185 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3186 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003187 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003188 };
3189
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003190 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3191 /* Generic maps (fallback) */
3192 {
3193 .vendor = PCI_VENDOR_ID_IBM,
3194 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3195 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3196 },
3197 {
3198 .vendor = PCI_VENDOR_ID_LENOVO,
3199 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3200 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3201 },
3202 };
3203
3204#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003205#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003206
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003207 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003208 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003209 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003210 bool radiosw_state = false;
3211 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003212
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003213 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003214 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003215
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003216 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3217 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003218
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003219 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003220 BUG_ON(tpacpi_inputdev->open != NULL ||
3221 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003222
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003223 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003224 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003225
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003226#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3227 mutex_init(&hotkey_thread_mutex);
3228 mutex_init(&hotkey_thread_data_mutex);
3229#endif
3230
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003231 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003232 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003233
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003234 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3235 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003236 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003237
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003238 if (!tp_features.hotkey)
3239 return 1;
3240
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003241 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3242 ARRAY_SIZE(tpacpi_hotkey_qtable));
3243
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003244 tpacpi_disable_brightness_delay();
3245
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003246 /* MUST have enough space for all attributes to be added to
3247 * hotkey_dev_attributes */
3248 hotkey_dev_attributes = create_attr_set(
3249 ARRAY_SIZE(hotkey_attributes) + 2,
3250 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003251 if (!hotkey_dev_attributes)
3252 return -ENOMEM;
3253 res = add_many_to_attr_set(hotkey_dev_attributes,
3254 hotkey_attributes,
3255 ARRAY_SIZE(hotkey_attributes));
3256 if (res)
3257 goto err_exit;
3258
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003259 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003260 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3261 for HKEY interface version 0x100 */
3262 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3263 if ((hkeyv >> 8) != 1) {
Joe Perches0978e012011-04-04 10:06:25 -07003264 pr_err("unknown version of the HKEY interface: 0x%x\n",
3265 hkeyv);
3266 pr_err("please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003267 } else {
3268 /*
3269 * MHKV 0x100 in A31, R40, R40e,
3270 * T4x, X31, and later
3271 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003272 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3273 "firmware HKEY interface version: 0x%x\n",
3274 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003275
3276 /* Paranoia check AND init hotkey_all_mask */
3277 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3278 "MHKA", "qd")) {
Joe Perches0978e012011-04-04 10:06:25 -07003279 pr_err("missing MHKA handler, "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003280 "please report this to %s\n",
3281 TPACPI_MAIL);
3282 /* Fallback: pre-init for FN+F3,F4,F12 */
3283 hotkey_all_mask = 0x080cU;
3284 } else {
3285 tp_features.hotkey_mask = 1;
3286 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003287 }
3288 }
3289
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003290 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3291 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003292 str_supported(tp_features.hotkey_mask));
3293
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003294 /* Init hotkey_all_mask if not initialized yet */
3295 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3296 (quirks & TPACPI_HK_Q_INIMASK))
3297 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003298
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003299 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003300 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003301 /* hotkey_source_mask *must* be zero for
3302 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003303 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003304 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003305 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003306
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003307 hotkey_orig_mask = hotkey_acpi_mask;
3308 } else {
3309 hotkey_orig_mask = hotkey_all_mask;
3310 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003311 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003312
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003313#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3314 if (dbg_wlswemul) {
3315 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003316 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003317 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003318 } else
3319#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003320 /* Not all thinkpads have a hardware radio switch */
3321 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3322 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003323 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003324 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003325 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003326 }
3327 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003328 res = add_to_attr_set(hotkey_dev_attributes,
3329 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003330
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003331 /* For X41t, X60t, X61t Tablets... */
3332 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3333 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003334 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Joe Perches0978e012011-04-04 10:06:25 -07003335 pr_info("possible tablet mode switch found; "
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003336 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003337 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003338 res = add_to_attr_set(hotkey_dev_attributes,
3339 &dev_attr_hotkey_tablet_mode.attr);
3340 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003341
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003342 if (!res)
3343 res = register_attr_set_with_sysfs(
3344 hotkey_dev_attributes,
3345 &tpacpi_pdev->dev.kobj);
3346 if (res)
3347 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003348
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003349 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003350 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3351 GFP_KERNEL);
3352 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003353 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003354 res = -ENOMEM;
3355 goto err_exit;
3356 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003357
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003358 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3359 ARRAY_SIZE(tpacpi_keymap_qtable));
3360 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3361 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3362 "using keymap number %lu\n", keymap_id);
3363
3364 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3365 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003366
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003367 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003368 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3369 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3370 tpacpi_inputdev->keycode = hotkey_keycode_map;
3371 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3372 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003373 input_set_capability(tpacpi_inputdev, EV_KEY,
3374 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003375 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003376 if (i < sizeof(hotkey_reserved_mask)*8)
3377 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003378 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003379 }
3380
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003381 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003382 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003383 input_report_switch(tpacpi_inputdev,
3384 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003385 }
3386 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003387 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003388 input_report_switch(tpacpi_inputdev,
3389 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003390 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003391
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003392 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003393 * userspace. tpacpi_detect_brightness_capabilities() must have
3394 * been called before this point */
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003395 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Joe Perches0978e012011-04-04 10:06:25 -07003396 pr_info("This ThinkPad has standard ACPI backlight "
3397 "brightness control, supported by the ACPI "
3398 "video driver\n");
3399 pr_notice("Disabling thinkpad-acpi brightness events "
3400 "by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003401
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003402 /* Disable brightness up/down on Lenovo thinkpads when
3403 * ACPI is handling them, otherwise it is plain impossible
3404 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003405 hotkey_reserved_mask |=
3406 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3407 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003408 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3409 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003410 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003411
Henrique de Moraes Holschuh230d8cf2009-09-12 15:22:17 -03003412#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003413 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3414 & ~hotkey_all_mask
3415 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf2009-09-12 15:22:17 -03003416
3417 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3418 "hotkey source mask 0x%08x, polling freq %u\n",
3419 hotkey_source_mask, hotkey_poll_freq);
3420#endif
3421
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003422 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3423 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003424 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003425 if (res) {
3426 hotkey_exit();
3427 return res;
3428 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003429 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3430 | hotkey_driver_mask)
3431 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003432 if (res < 0 && res != -ENXIO) {
3433 hotkey_exit();
3434 return res;
3435 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003436 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3437 & ~hotkey_reserved_mask;
3438 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3439 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3440 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003441
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003442 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3443 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003444 (hotkey_report_mode < 2) ?
3445 "enabled" : "disabled");
3446
3447 tpacpi_inputdev->open = &hotkey_inputdev_open;
3448 tpacpi_inputdev->close = &hotkey_inputdev_close;
3449
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003450 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003451
3452 return 0;
3453
3454err_exit:
3455 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3456 hotkey_dev_attributes = NULL;
3457
3458 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003459}
3460
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003461static bool hotkey_notify_hotkey(const u32 hkey,
3462 bool *send_acpi_ev,
3463 bool *ignore_acpi_ev)
3464{
3465 /* 0x1000-0x1FFF: key presses */
3466 unsigned int scancode = hkey & 0xfff;
3467 *send_acpi_ev = true;
3468 *ignore_acpi_ev = false;
3469
Henrique de Moraes Holschuh34a656d2010-08-09 23:48:20 -03003470 /* HKEY event 0x1001 is scancode 0x00 */
3471 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003472 scancode--;
3473 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003474 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003475 *send_acpi_ev = false;
3476 } else {
3477 *ignore_acpi_ev = true;
3478 }
3479 return true;
3480 }
3481 return false;
3482}
3483
3484static bool hotkey_notify_wakeup(const u32 hkey,
3485 bool *send_acpi_ev,
3486 bool *ignore_acpi_ev)
3487{
3488 /* 0x2000-0x2FFF: Wakeup reason */
3489 *send_acpi_ev = true;
3490 *ignore_acpi_ev = false;
3491
3492 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003493 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3494 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003495 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3496 *ignore_acpi_ev = true;
3497 break;
3498
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003499 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3500 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003501 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3502 *ignore_acpi_ev = true;
3503 break;
3504
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003505 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3506 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003507 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003508 /* how to auto-heal: */
3509 /* 2313: woke up from S3, go to S4/S5 */
3510 /* 2413: woke up from S4, go to S5 */
3511 break;
3512
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003513 default:
3514 return false;
3515 }
3516
3517 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003518 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003519 hotkey_wakeup_reason_notify_change();
3520 }
3521 return true;
3522}
3523
3524static bool hotkey_notify_usrevent(const u32 hkey,
3525 bool *send_acpi_ev,
3526 bool *ignore_acpi_ev)
3527{
3528 /* 0x5000-0x5FFF: human interface helpers */
3529 *send_acpi_ev = true;
3530 *ignore_acpi_ev = false;
3531
3532 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003533 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3534 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003535 return true;
3536
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003537 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3538 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003539 tpacpi_input_send_tabletsw();
3540 hotkey_tablet_mode_notify_change();
3541 *send_acpi_ev = false;
3542 return true;
3543
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003544 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3545 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3546 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003547 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003548 *ignore_acpi_ev = true;
3549 return true;
3550
3551 default:
3552 return false;
3553 }
3554}
3555
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003556static void thermal_dump_all_sensors(void);
3557
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003558static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003559 bool *send_acpi_ev,
3560 bool *ignore_acpi_ev)
3561{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003562 bool known = true;
3563
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003564 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003565 *send_acpi_ev = true;
3566 *ignore_acpi_ev = false;
3567
3568 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003569 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003570 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003571 /* recommended action: do nothing, we don't have
3572 * Lenovo ATM information */
3573 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003574 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003575 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003576 /* recommended action: warn user through gui */
3577 break;
3578 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003579 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003580 /* recommended action: immediate sleep/hibernate */
3581 break;
3582 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003583 pr_crit("THERMAL ALARM: "
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003584 "a sensor reports something is too hot!\n");
3585 /* recommended action: warn user through gui, that */
3586 /* some internal component is too hot */
3587 break;
3588 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003589 pr_alert("THERMAL EMERGENCY: "
3590 "a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003591 /* recommended action: immediate sleep/hibernate */
3592 break;
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003593
3594 case TP_HKEY_EV_KEY_NUMLOCK:
3595 case TP_HKEY_EV_KEY_FN:
3596 /* key press events, we just ignore them as long as the EC
3597 * is still reporting them in the normal keyboard stream */
3598 *send_acpi_ev = false;
3599 *ignore_acpi_ev = true;
3600 return true;
3601
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003602 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003603 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003604 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003605 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003606
3607 thermal_dump_all_sensors();
3608
3609 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003610}
3611
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003612static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3613{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003614 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003615 bool send_acpi_ev;
3616 bool ignore_acpi_ev;
3617 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003618
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003619 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07003620 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003621 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003622 acpi_bus_generate_netlink_event(
3623 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003624 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003625 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003626 return;
3627 }
3628
3629 while (1) {
3630 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07003631 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003632 return;
3633 }
3634
3635 if (hkey == 0) {
3636 /* queue empty */
3637 return;
3638 }
3639
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003640 send_acpi_ev = true;
3641 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003642
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003643 switch (hkey >> 12) {
3644 case 1:
3645 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003646 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3647 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003648 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003649 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003650 /* 0x2000-0x2FFF: Wakeup reason */
3651 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3652 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003653 break;
3654 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003655 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003656 switch (hkey) {
3657 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003658 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07003659 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003660 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003661 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003662 break;
3663 case TP_HKEY_EV_OPTDRV_EJ:
3664 /* FIXME: kick libata if SATA link offline */
3665 known_ev = true;
3666 break;
3667 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003668 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003669 }
3670 break;
3671 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003672 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003673 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003674 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07003675 pr_info("undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003676 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003677 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003678 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003679 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003680 }
3681 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003682 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003683 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003684 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3685 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003686 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003687 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003688 /* 0x6000-0x6FFF: thermal alarms/notices and
3689 * keyboard events */
3690 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003691 &ignore_acpi_ev);
3692 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003693 case 7:
3694 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003695 if (tp_features.hotkey_wlsw &&
3696 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003697 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003698 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003699 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003700 break;
3701 }
3702 /* fallthrough to default */
3703 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003704 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003705 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003706 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07003707 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3708 pr_notice("please report the conditions when this "
3709 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003710 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003711
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003712 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003713 if (!ignore_acpi_ev &&
3714 (send_acpi_ev || hotkey_report_mode < 2)) {
3715 acpi_bus_generate_proc_event(ibm->acpi->device,
3716 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003717 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003718
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003719 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003720 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003721 acpi_bus_generate_netlink_event(
3722 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003723 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003724 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003725 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003726 }
3727}
3728
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003729static void hotkey_suspend(pm_message_t state)
3730{
3731 /* Do these on suspend, we get the events on early resume! */
3732 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3733 hotkey_autosleep_ack = 0;
3734}
3735
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003736static void hotkey_resume(void)
3737{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003738 tpacpi_disable_brightness_delay();
3739
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003740 if (hotkey_status_set(true) < 0 ||
3741 hotkey_mask_set(hotkey_acpi_mask) < 0)
Joe Perches0978e012011-04-04 10:06:25 -07003742 pr_err("error while attempting to reset the event "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003743 "firmware interface\n");
3744
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003745 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003746 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003747 hotkey_wakeup_reason_notify_change();
3748 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003749 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003750}
3751
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003752/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003753static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003755 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003757 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003758 seq_printf(m, "status:\t\tnot supported\n");
3759 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003760 }
3761
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003762 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003763 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003764 res = hotkey_status_get(&status);
3765 if (!res)
3766 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003767 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003768 if (res)
3769 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003771 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003772 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003773 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3774 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003776 seq_printf(m, "mask:\t\tnot supported\n");
3777 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 }
3779
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003780 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781}
3782
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003783static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003784{
3785 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003786 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Joe Perches0978e012011-04-04 10:06:25 -07003787 pr_fmt("hotkey enable/disable functionality has been "
3788 "removed from the driver. "
3789 "Hotkeys are always enabled.\n")))
3790 pr_err("Please remove the hotkey=enable module "
3791 "parameter, it is deprecated. "
3792 "Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003793}
3794
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003795static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003797 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003798 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003801 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 return -ENODEV;
3803
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003804 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003805 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003806
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003807 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003808
3809 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 while ((cmd = next_cmd(&buf))) {
3811 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003812 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003814 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003815 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003817 mask = (hotkey_all_mask | hotkey_source_mask)
3818 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3820 /* mask set */
3821 } else if (sscanf(cmd, "%x", &mask) == 1) {
3822 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003823 } else {
3824 res = -EINVAL;
3825 goto errexit;
3826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003828
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003829 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003830 tpacpi_disclose_usertask("procfs hotkey",
3831 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003832 res = hotkey_user_mask_set(mask);
3833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003835errexit:
3836 mutex_unlock(&hotkey_mutex);
3837 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003838}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003840static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04003841 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3842 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003843 {"", 0},
3844};
3845
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003846static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003847 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003848 .notify = hotkey_notify,
3849 .handle = &hkey_handle,
3850 .type = ACPI_DEVICE_NOTIFY,
3851};
3852
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003853static struct ibm_struct hotkey_driver_data = {
3854 .name = "hotkey",
3855 .read = hotkey_read,
3856 .write = hotkey_write,
3857 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003858 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003859 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003860 .acpi = &ibm_hotkey_acpidriver,
3861};
3862
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003863/*************************************************************************
3864 * Bluetooth subdriver
3865 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003867enum {
3868 /* ACPI GBDC/SBDC bits */
3869 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3870 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003871 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003872 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003873};
3874
3875enum {
3876 /* ACPI \BLTH commands */
3877 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3878 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3879 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3880 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3881 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003882};
3883
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003884#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3885
Johannes Berg19d337d2009-06-02 13:01:37 +02003886static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003887{
3888 int status;
3889
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003890#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3891 if (dbg_bluetoothemul)
3892 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003893 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003894#endif
3895
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003896 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3897 return -EIO;
3898
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003899 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003900 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003901}
3902
Johannes Berg19d337d2009-06-02 13:01:37 +02003903static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003904{
3905 int status;
3906
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003907 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003908 "will attempt to %s bluetooth\n",
3909 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003910
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003911#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3912 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003913 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003914 return 0;
3915 }
3916#endif
3917
Johannes Berg19d337d2009-06-02 13:01:37 +02003918 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003919 status = TP_ACPI_BLUETOOTH_RADIOSSW
3920 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3921 else
3922 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003923
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003924 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3925 return -EIO;
3926
3927 return 0;
3928}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003929
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003930/* sysfs bluetooth enable ---------------------------------------------- */
3931static ssize_t bluetooth_enable_show(struct device *dev,
3932 struct device_attribute *attr,
3933 char *buf)
3934{
Johannes Berg19d337d2009-06-02 13:01:37 +02003935 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3936 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003937}
3938
3939static ssize_t bluetooth_enable_store(struct device *dev,
3940 struct device_attribute *attr,
3941 const char *buf, size_t count)
3942{
Johannes Berg19d337d2009-06-02 13:01:37 +02003943 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3944 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003945}
3946
3947static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003948 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003949 bluetooth_enable_show, bluetooth_enable_store);
3950
3951/* --------------------------------------------------------------------- */
3952
3953static struct attribute *bluetooth_attributes[] = {
3954 &dev_attr_bluetooth_enable.attr,
3955 NULL
3956};
3957
3958static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003959 .attrs = bluetooth_attributes,
3960};
3961
Johannes Berg19d337d2009-06-02 13:01:37 +02003962static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3963 .get_status = bluetooth_get_status,
3964 .set_status = bluetooth_set_status,
3965};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003966
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003967static void bluetooth_shutdown(void)
3968{
3969 /* Order firmware to save current state to NVRAM */
3970 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3971 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07003972 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003973 else
3974 vdbg_printk(TPACPI_DBG_RFKILL,
3975 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003976}
3977
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003978static void bluetooth_exit(void)
3979{
3980 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3981 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003982
3983 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3984
3985 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003986}
3987
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003988static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003990 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003991 int status = 0;
3992
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003993 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3994 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003995
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003996 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003997
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003998 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3999 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004000 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004001 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004003 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4004 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004005 str_supported(tp_features.bluetooth),
4006 status);
4007
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004008#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4009 if (dbg_bluetoothemul) {
4010 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004011 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004012 } else
4013#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004014 if (tp_features.bluetooth &&
4015 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4016 /* no bluetooth hardware present in system */
4017 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004018 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004019 "bluetooth hardware not installed\n");
4020 }
4021
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004022 if (!tp_features.bluetooth)
4023 return 1;
4024
Johannes Berg19d337d2009-06-02 13:01:37 +02004025 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4026 &bluetooth_tprfk_ops,
4027 RFKILL_TYPE_BLUETOOTH,
4028 TPACPI_RFK_BLUETOOTH_SW_NAME,
4029 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004030 if (res)
4031 return res;
4032
Johannes Berg19d337d2009-06-02 13:01:37 +02004033 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4034 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004035 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004036 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004037 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004038 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004039
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041}
4042
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004043/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004044static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004046 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047}
4048
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004049static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050{
Johannes Berg19d337d2009-06-02 13:01:37 +02004051 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052}
4053
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004054static struct ibm_struct bluetooth_driver_data = {
4055 .name = "bluetooth",
4056 .read = bluetooth_read,
4057 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004058 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004059 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004060};
4061
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004062/*************************************************************************
4063 * Wan subdriver
4064 */
4065
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004066enum {
4067 /* ACPI GWAN/SWAN bits */
4068 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4069 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004070 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004071 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004072};
4073
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004074#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4075
Johannes Berg19d337d2009-06-02 13:01:37 +02004076static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004077{
4078 int status;
4079
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004080#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4081 if (dbg_wwanemul)
4082 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004083 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004084#endif
4085
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004086 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4087 return -EIO;
4088
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004089 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004090 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004091}
4092
Johannes Berg19d337d2009-06-02 13:01:37 +02004093static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004094{
4095 int status;
4096
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004097 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004098 "will attempt to %s wwan\n",
4099 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004100
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004101#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4102 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004103 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004104 return 0;
4105 }
4106#endif
4107
Johannes Berg19d337d2009-06-02 13:01:37 +02004108 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004109 status = TP_ACPI_WANCARD_RADIOSSW
4110 | TP_ACPI_WANCARD_RESUMECTRL;
4111 else
4112 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004113
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004114 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4115 return -EIO;
4116
4117 return 0;
4118}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004119
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004120/* sysfs wan enable ---------------------------------------------------- */
4121static ssize_t wan_enable_show(struct device *dev,
4122 struct device_attribute *attr,
4123 char *buf)
4124{
Johannes Berg19d337d2009-06-02 13:01:37 +02004125 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4126 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004127}
4128
4129static ssize_t wan_enable_store(struct device *dev,
4130 struct device_attribute *attr,
4131 const char *buf, size_t count)
4132{
Johannes Berg19d337d2009-06-02 13:01:37 +02004133 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4134 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004135}
4136
4137static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004138 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004139 wan_enable_show, wan_enable_store);
4140
4141/* --------------------------------------------------------------------- */
4142
4143static struct attribute *wan_attributes[] = {
4144 &dev_attr_wan_enable.attr,
4145 NULL
4146};
4147
4148static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004149 .attrs = wan_attributes,
4150};
4151
Johannes Berg19d337d2009-06-02 13:01:37 +02004152static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4153 .get_status = wan_get_status,
4154 .set_status = wan_set_status,
4155};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004156
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004157static void wan_shutdown(void)
4158{
4159 /* Order firmware to save current state to NVRAM */
4160 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4161 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004162 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004163 else
4164 vdbg_printk(TPACPI_DBG_RFKILL,
4165 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004166}
4167
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004168static void wan_exit(void)
4169{
4170 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4171 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004172
4173 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4174
4175 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004176}
4177
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004178static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004179{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004180 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004181 int status = 0;
4182
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004183 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4184 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004185
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004186 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004187
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004188 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004189 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004190
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004191 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4192 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004193 str_supported(tp_features.wan),
4194 status);
4195
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004196#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4197 if (dbg_wwanemul) {
4198 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004199 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004200 } else
4201#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004202 if (tp_features.wan &&
4203 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4204 /* no wan hardware present in system */
4205 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004206 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004207 "wan hardware not installed\n");
4208 }
4209
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004210 if (!tp_features.wan)
4211 return 1;
4212
Johannes Berg19d337d2009-06-02 13:01:37 +02004213 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4214 &wan_tprfk_ops,
4215 RFKILL_TYPE_WWAN,
4216 TPACPI_RFK_WWAN_SW_NAME,
4217 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004218 if (res)
4219 return res;
4220
Johannes Berg19d337d2009-06-02 13:01:37 +02004221 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4222 &wan_attr_group);
4223
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004224 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004225 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004226 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004227 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004228
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004229 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004230}
4231
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004232/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004233static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004234{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004235 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004236}
4237
4238static int wan_write(char *buf)
4239{
Johannes Berg19d337d2009-06-02 13:01:37 +02004240 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004241}
4242
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004243static struct ibm_struct wan_driver_data = {
4244 .name = "wan",
4245 .read = wan_read,
4246 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004247 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004248 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004249};
4250
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004251/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004252 * UWB subdriver
4253 */
4254
4255enum {
4256 /* ACPI GUWB/SUWB bits */
4257 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4258 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4259};
4260
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004261#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4262
Johannes Berg19d337d2009-06-02 13:01:37 +02004263static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004264{
4265 int status;
4266
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004267#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4268 if (dbg_uwbemul)
4269 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004270 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004271#endif
4272
4273 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4274 return -EIO;
4275
4276 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004277 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004278}
4279
Johannes Berg19d337d2009-06-02 13:01:37 +02004280static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004281{
4282 int status;
4283
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004284 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004285 "will attempt to %s UWB\n",
4286 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004287
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004288#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4289 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004290 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004291 return 0;
4292 }
4293#endif
4294
Johannes Berg19d337d2009-06-02 13:01:37 +02004295 if (state == TPACPI_RFK_RADIO_ON)
4296 status = TP_ACPI_UWB_RADIOSSW;
4297 else
4298 status = 0;
4299
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004300 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4301 return -EIO;
4302
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004303 return 0;
4304}
4305
4306/* --------------------------------------------------------------------- */
4307
Johannes Berg19d337d2009-06-02 13:01:37 +02004308static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4309 .get_status = uwb_get_status,
4310 .set_status = uwb_set_status,
4311};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004312
4313static void uwb_exit(void)
4314{
Johannes Berg19d337d2009-06-02 13:01:37 +02004315 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004316}
4317
4318static int __init uwb_init(struct ibm_init_struct *iibm)
4319{
4320 int res;
4321 int status = 0;
4322
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004323 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4324 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004325
4326 TPACPI_ACPIHANDLE_INIT(hkey);
4327
4328 tp_features.uwb = hkey_handle &&
4329 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4330
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004331 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4332 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004333 str_supported(tp_features.uwb),
4334 status);
4335
4336#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4337 if (dbg_uwbemul) {
4338 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004339 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004340 } else
4341#endif
4342 if (tp_features.uwb &&
4343 !(status & TP_ACPI_UWB_HWPRESENT)) {
4344 /* no uwb hardware present in system */
4345 tp_features.uwb = 0;
4346 dbg_printk(TPACPI_DBG_INIT,
4347 "uwb hardware not installed\n");
4348 }
4349
4350 if (!tp_features.uwb)
4351 return 1;
4352
4353 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004354 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004355 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004356 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004357 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004358 return res;
4359}
4360
4361static struct ibm_struct uwb_driver_data = {
4362 .name = "uwb",
4363 .exit = uwb_exit,
4364 .flags.experimental = 1,
4365};
4366
4367/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004368 * Video subdriver
4369 */
4370
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004371#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4372
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004373enum video_access_mode {
4374 TPACPI_VIDEO_NONE = 0,
4375 TPACPI_VIDEO_570, /* 570 */
4376 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4377 TPACPI_VIDEO_NEW, /* all others */
4378};
4379
4380enum { /* video status flags, based on VIDEO_570 */
4381 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4382 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4383 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4384};
4385
4386enum { /* TPACPI_VIDEO_570 constants */
4387 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4388 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4389 * video_status_flags */
4390 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4391 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4392};
4393
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004394static enum video_access_mode video_supported;
4395static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004396
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004397static int video_autosw_get(void);
4398static int video_autosw_set(int enable);
4399
Joe Perches112a6ee2011-04-04 10:06:24 -07004400TPACPI_HANDLE(vid, root,
4401 "\\_SB.PCI.AGP.VGA", /* 570 */
4402 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4403 "\\_SB.PCI0.VID0", /* 770e */
4404 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4405 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4406 "\\_SB.PCI0.AGP.VID", /* all others */
4407 ); /* R30, R31 */
4408
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004409TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004410
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004411static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004413 int ivga;
4414
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004415 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4416
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004417 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004418 if (tpacpi_is_ibm())
4419 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004420
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004421 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4422 /* G41, assume IVGA doesn't change */
4423 vid_handle = vid2_handle;
4424
4425 if (!vid_handle)
4426 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004427 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004428 else if (tpacpi_is_ibm() &&
4429 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004430 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004431 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004432 else if (tpacpi_is_ibm() &&
4433 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004434 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004435 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004436 else
4437 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004438 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004440 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4441 str_supported(video_supported != TPACPI_VIDEO_NONE),
4442 video_supported);
4443
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004444 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445}
4446
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004447static void video_exit(void)
4448{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004449 dbg_printk(TPACPI_DBG_EXIT,
4450 "restoring original video autoswitch mode\n");
4451 if (video_autosw_set(video_orig_autosw))
Joe Perches0978e012011-04-04 10:06:25 -07004452 pr_err("error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004453 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004454}
4455
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004456static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457{
4458 int status = 0;
4459 int i;
4460
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004461 switch (video_supported) {
4462 case TPACPI_VIDEO_570:
4463 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4464 TP_ACPI_VIDEO_570_PHSCMD))
4465 return -EIO;
4466 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4467 break;
4468 case TPACPI_VIDEO_770:
4469 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4470 return -EIO;
4471 if (i)
4472 status |= TP_ACPI_VIDEO_S_LCD;
4473 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4474 return -EIO;
4475 if (i)
4476 status |= TP_ACPI_VIDEO_S_CRT;
4477 break;
4478 case TPACPI_VIDEO_NEW:
4479 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4480 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4481 return -EIO;
4482 if (i)
4483 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004485 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4486 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4487 return -EIO;
4488 if (i)
4489 status |= TP_ACPI_VIDEO_S_LCD;
4490 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4491 return -EIO;
4492 if (i)
4493 status |= TP_ACPI_VIDEO_S_DVI;
4494 break;
4495 default:
4496 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
4499 return status;
4500}
4501
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004502static int video_outputsw_set(int status)
4503{
4504 int autosw;
4505 int res = 0;
4506
4507 switch (video_supported) {
4508 case TPACPI_VIDEO_570:
4509 res = acpi_evalf(NULL, NULL,
4510 "\\_SB.PHS2", "vdd",
4511 TP_ACPI_VIDEO_570_PHS2CMD,
4512 status | TP_ACPI_VIDEO_570_PHS2SET);
4513 break;
4514 case TPACPI_VIDEO_770:
4515 autosw = video_autosw_get();
4516 if (autosw < 0)
4517 return autosw;
4518
4519 res = video_autosw_set(1);
4520 if (res)
4521 return res;
4522 res = acpi_evalf(vid_handle, NULL,
4523 "ASWT", "vdd", status * 0x100, 0);
4524 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004525 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004526 return -EIO;
4527 }
4528 break;
4529 case TPACPI_VIDEO_NEW:
4530 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004531 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004532 break;
4533 default:
4534 return -ENOSYS;
4535 }
4536
4537 return (res)? 0 : -EIO;
4538}
4539
4540static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004542 int autosw = 0;
4543
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004544 switch (video_supported) {
4545 case TPACPI_VIDEO_570:
4546 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4547 return -EIO;
4548 break;
4549 case TPACPI_VIDEO_770:
4550 case TPACPI_VIDEO_NEW:
4551 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4552 return -EIO;
4553 break;
4554 default:
4555 return -ENOSYS;
4556 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004557
4558 return autosw & 1;
4559}
4560
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004561static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004562{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004563 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004564 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004565 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004566}
4567
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004568static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004569{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004570 int autosw = video_autosw_get();
4571 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004572
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004573 if (autosw < 0)
4574 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004575
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004576 switch (video_supported) {
4577 case TPACPI_VIDEO_570:
4578 res = video_autosw_set(1);
4579 if (res)
4580 return res;
4581 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4582 break;
4583 case TPACPI_VIDEO_770:
4584 case TPACPI_VIDEO_NEW:
4585 res = video_autosw_set(1);
4586 if (res)
4587 return res;
4588 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4589 break;
4590 default:
4591 return -ENOSYS;
4592 }
4593 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004594 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004595 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004596 }
4597
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004598 return (res)? 0 : -EIO;
4599}
4600
4601static int video_expand_toggle(void)
4602{
4603 switch (video_supported) {
4604 case TPACPI_VIDEO_570:
4605 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4606 0 : -EIO;
4607 case TPACPI_VIDEO_770:
4608 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4609 0 : -EIO;
4610 case TPACPI_VIDEO_NEW:
4611 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4612 0 : -EIO;
4613 default:
4614 return -ENOSYS;
4615 }
4616 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004617}
4618
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004619static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004620{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004621 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004622
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004623 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004624 seq_printf(m, "status:\t\tnot supported\n");
4625 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004626 }
4627
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004628 /* Even reads can crash X.org, so... */
4629 if (!capable(CAP_SYS_ADMIN))
4630 return -EPERM;
4631
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004632 status = video_outputsw_get();
4633 if (status < 0)
4634 return status;
4635
4636 autosw = video_autosw_get();
4637 if (autosw < 0)
4638 return autosw;
4639
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004640 seq_printf(m, "status:\t\tsupported\n");
4641 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4642 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004643 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004644 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4645 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4646 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4647 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004648 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004649 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4650 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4651 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004652
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004653 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004654}
4655
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004656static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657{
4658 char *cmd;
4659 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004660 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004662 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004663 return -ENODEV;
4664
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004665 /* Even reads can crash X.org, let alone writes... */
4666 if (!capable(CAP_SYS_ADMIN))
4667 return -EPERM;
4668
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004669 enable = 0;
4670 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
4672 while ((cmd = next_cmd(&buf))) {
4673 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004674 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004676 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004678 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004680 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004681 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004682 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004683 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004684 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004685 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004686 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004688 res = video_autosw_set(1);
4689 if (res)
4690 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004692 res = video_autosw_set(0);
4693 if (res)
4694 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004696 res = video_outputsw_cycle();
4697 if (res)
4698 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004700 res = video_expand_toggle();
4701 if (res)
4702 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 } else
4704 return -EINVAL;
4705 }
4706
4707 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004708 status = video_outputsw_get();
4709 if (status < 0)
4710 return status;
4711 res = video_outputsw_set((status & ~disable) | enable);
4712 if (res)
4713 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 }
4715
4716 return 0;
4717}
4718
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004719static struct ibm_struct video_driver_data = {
4720 .name = "video",
4721 .read = video_read,
4722 .write = video_write,
4723 .exit = video_exit,
4724};
4725
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004726#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4727
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004728/*************************************************************************
4729 * Light (thinklight) subdriver
4730 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004732TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4733TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004734
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004735static int light_get_status(void)
4736{
4737 int status = 0;
4738
4739 if (tp_features.light_status) {
4740 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4741 return -EIO;
4742 return (!!status);
4743 }
4744
4745 return -ENXIO;
4746}
4747
4748static int light_set_status(int status)
4749{
4750 int rc;
4751
4752 if (tp_features.light) {
4753 if (cmos_handle) {
4754 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4755 (status)?
4756 TP_CMOS_THINKLIGHT_ON :
4757 TP_CMOS_THINKLIGHT_OFF);
4758 } else {
4759 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4760 (status)? 1 : 0);
4761 }
4762 return (rc)? 0 : -EIO;
4763 }
4764
4765 return -ENXIO;
4766}
4767
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004768static void light_set_status_worker(struct work_struct *work)
4769{
4770 struct tpacpi_led_classdev *data =
4771 container_of(work, struct tpacpi_led_classdev, work);
4772
4773 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004774 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004775}
4776
4777static void light_sysfs_set(struct led_classdev *led_cdev,
4778 enum led_brightness brightness)
4779{
4780 struct tpacpi_led_classdev *data =
4781 container_of(led_cdev,
4782 struct tpacpi_led_classdev,
4783 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004784 data->new_state = (brightness != LED_OFF) ?
4785 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004786 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004787}
4788
4789static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4790{
4791 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4792}
4793
4794static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4795 .led_classdev = {
4796 .name = "tpacpi::thinklight",
4797 .brightness_set = &light_sysfs_set,
4798 .brightness_get = &light_sysfs_get,
4799 }
4800};
4801
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004802static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004804 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004805
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004806 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4807
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004808 if (tpacpi_is_ibm()) {
4809 TPACPI_ACPIHANDLE_INIT(ledb);
4810 TPACPI_ACPIHANDLE_INIT(lght);
4811 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004812 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004813 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004814
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004815 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004816 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004817
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004818 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004819 /* light status not supported on
4820 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004821 tp_features.light_status =
4822 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004824 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4825 str_supported(tp_features.light),
4826 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004827
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004828 if (!tp_features.light)
4829 return 1;
4830
4831 rc = led_classdev_register(&tpacpi_pdev->dev,
4832 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004833
4834 if (rc < 0) {
4835 tp_features.light = 0;
4836 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004837 } else {
4838 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004839 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004840
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004841 return rc;
4842}
4843
4844static void light_exit(void)
4845{
4846 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4847 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004848 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849}
4850
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004851static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004853 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004855 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004856 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004857 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004858 seq_printf(m, "status:\t\tunknown\n");
4859 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004860 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004861 status = light_get_status();
4862 if (status < 0)
4863 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004864 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4865 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004868 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869}
4870
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004871static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004874 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004875
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004876 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004877 return -ENODEV;
4878
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 while ((cmd = next_cmd(&buf))) {
4880 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004881 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004883 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 } else
4885 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 }
4887
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004888 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889}
4890
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004891static struct ibm_struct light_driver_data = {
4892 .name = "light",
4893 .read = light_read,
4894 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004895 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004896};
4897
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004898/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004899 * CMOS subdriver
4900 */
4901
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004902/* sysfs cmos_command -------------------------------------------------- */
4903static ssize_t cmos_command_store(struct device *dev,
4904 struct device_attribute *attr,
4905 const char *buf, size_t count)
4906{
4907 unsigned long cmos_cmd;
4908 int res;
4909
4910 if (parse_strtoul(buf, 21, &cmos_cmd))
4911 return -EINVAL;
4912
4913 res = issue_thinkpad_cmos_command(cmos_cmd);
4914 return (res)? res : count;
4915}
4916
4917static struct device_attribute dev_attr_cmos_command =
4918 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4919
4920/* --------------------------------------------------------------------- */
4921
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004922static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004923{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004924 int res;
4925
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004926 vdbg_printk(TPACPI_DBG_INIT,
4927 "initializing cmos commands subdriver\n");
4928
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004929 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004930
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004931 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4932 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004933
4934 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4935 if (res)
4936 return res;
4937
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004938 return (cmos_handle)? 0 : 1;
4939}
4940
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004941static void cmos_exit(void)
4942{
4943 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4944}
4945
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004946static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004948 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4949 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004951 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004953 seq_printf(m, "status:\t\tsupported\n");
4954 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 }
4956
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004957 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958}
4959
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004960static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961{
4962 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004963 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
4965 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004966 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4967 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 /* cmos_cmd set */
4969 } else
4970 return -EINVAL;
4971
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004972 res = issue_thinkpad_cmos_command(cmos_cmd);
4973 if (res)
4974 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 }
4976
4977 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004978}
4979
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004980static struct ibm_struct cmos_driver_data = {
4981 .name = "cmos",
4982 .read = cmos_read,
4983 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004984 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004985};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004986
4987/*************************************************************************
4988 * LED subdriver
4989 */
4990
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004991enum led_access_mode {
4992 TPACPI_LED_NONE = 0,
4993 TPACPI_LED_570, /* 570 */
4994 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4995 TPACPI_LED_NEW, /* all others */
4996};
4997
4998enum { /* For TPACPI_LED_OLD */
4999 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5000 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5001 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5002};
5003
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005004static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005005
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005006static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005007
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005008#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005009static struct tpacpi_led_classdev *tpacpi_leds;
5010static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005011static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005012 /* there's a limit of 19 chars + NULL before 2.6.26 */
5013 "tpacpi::power",
5014 "tpacpi:orange:batt",
5015 "tpacpi:green:batt",
5016 "tpacpi::dock_active",
5017 "tpacpi::bay_active",
5018 "tpacpi::dock_batt",
5019 "tpacpi::unknown_led",
5020 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005021 "tpacpi::dock_status1",
5022 "tpacpi::dock_status2",
5023 "tpacpi::unknown_led2",
5024 "tpacpi::unknown_led3",
5025 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005026};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005027#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005028
5029static inline bool tpacpi_is_led_restricted(const unsigned int led)
5030{
5031#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5032 return false;
5033#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005034 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005035#endif
5036}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005037
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005038static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005039{
5040 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005041 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005042
5043 switch (led_supported) {
5044 case TPACPI_LED_570:
5045 if (!acpi_evalf(ec_handle,
5046 &status, "GLED", "dd", 1 << led))
5047 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005048 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005049 TPACPI_LED_OFF :
5050 ((status == 1)?
5051 TPACPI_LED_ON :
5052 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005053 tpacpi_led_state_cache[led] = led_s;
5054 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005055 default:
5056 return -ENXIO;
5057 }
5058
5059 /* not reached */
5060}
5061
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005062static int led_set_status(const unsigned int led,
5063 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005064{
5065 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005066 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5067 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005068
5069 int rc = 0;
5070
5071 switch (led_supported) {
5072 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005073 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005074 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005075 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005076 if (unlikely(tpacpi_is_led_restricted(led)))
5077 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005078 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5079 (1 << led), led_sled_arg1[ledstatus]))
5080 rc = -EIO;
5081 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005082 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005083 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005084 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005085 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005086 if (unlikely(tpacpi_is_led_restricted(led)))
5087 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005088 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5089 if (rc >= 0)
5090 rc = ec_write(TPACPI_LED_EC_HLBL,
5091 (ledstatus == TPACPI_LED_BLINK) << led);
5092 if (rc >= 0)
5093 rc = ec_write(TPACPI_LED_EC_HLCL,
5094 (ledstatus != TPACPI_LED_OFF) << led);
5095 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005096 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005097 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005098 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5099 return -EINVAL;
5100 if (unlikely(tpacpi_is_led_restricted(led)))
5101 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005102 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5103 led, led_led_arg1[ledstatus]))
5104 rc = -EIO;
5105 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005106 default:
5107 rc = -ENXIO;
5108 }
5109
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005110 if (!rc)
5111 tpacpi_led_state_cache[led] = ledstatus;
5112
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005113 return rc;
5114}
5115
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005116static void led_set_status_worker(struct work_struct *work)
5117{
5118 struct tpacpi_led_classdev *data =
5119 container_of(work, struct tpacpi_led_classdev, work);
5120
5121 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005122 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005123}
5124
5125static void led_sysfs_set(struct led_classdev *led_cdev,
5126 enum led_brightness brightness)
5127{
5128 struct tpacpi_led_classdev *data = container_of(led_cdev,
5129 struct tpacpi_led_classdev, led_classdev);
5130
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005131 if (brightness == LED_OFF)
5132 data->new_state = TPACPI_LED_OFF;
5133 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5134 data->new_state = TPACPI_LED_ON;
5135 else
5136 data->new_state = TPACPI_LED_BLINK;
5137
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005138 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005139}
5140
5141static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5142 unsigned long *delay_on, unsigned long *delay_off)
5143{
5144 struct tpacpi_led_classdev *data = container_of(led_cdev,
5145 struct tpacpi_led_classdev, led_classdev);
5146
5147 /* Can we choose the flash rate? */
5148 if (*delay_on == 0 && *delay_off == 0) {
5149 /* yes. set them to the hardware blink rate (1 Hz) */
5150 *delay_on = 500; /* ms */
5151 *delay_off = 500; /* ms */
5152 } else if ((*delay_on != 500) || (*delay_off != 500))
5153 return -EINVAL;
5154
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005155 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005156 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005157
5158 return 0;
5159}
5160
5161static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5162{
5163 int rc;
5164
5165 struct tpacpi_led_classdev *data = container_of(led_cdev,
5166 struct tpacpi_led_classdev, led_classdev);
5167
5168 rc = led_get_status(data->led);
5169
5170 if (rc == TPACPI_LED_OFF || rc < 0)
5171 rc = LED_OFF; /* no error handling in led class :( */
5172 else
5173 rc = LED_FULL;
5174
5175 return rc;
5176}
5177
5178static void led_exit(void)
5179{
5180 unsigned int i;
5181
5182 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5183 if (tpacpi_leds[i].led_classdev.name)
5184 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5185 }
5186
5187 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005188}
5189
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005190static int __init tpacpi_init_led(unsigned int led)
5191{
5192 int rc;
5193
5194 tpacpi_leds[led].led = led;
5195
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005196 /* LEDs with no name don't get registered */
5197 if (!tpacpi_led_names[led])
5198 return 0;
5199
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005200 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5201 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5202 if (led_supported == TPACPI_LED_570)
5203 tpacpi_leds[led].led_classdev.brightness_get =
5204 &led_sysfs_get;
5205
5206 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5207
5208 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5209
5210 rc = led_classdev_register(&tpacpi_pdev->dev,
5211 &tpacpi_leds[led].led_classdev);
5212 if (rc < 0)
5213 tpacpi_leds[led].led_classdev.name = NULL;
5214
5215 return rc;
5216}
5217
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005218static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5219 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5220 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5221 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5222
5223 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5224 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5225 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5226 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5227 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5228 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5229 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5230 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5231
5232 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5233 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5234 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5235 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5236 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5237
5238 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5239 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5240 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5241 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5242
5243 /* (1) - may have excess leds enabled on MSB */
5244
5245 /* Defaults (order matters, keep last, don't reorder!) */
5246 { /* Lenovo */
5247 .vendor = PCI_VENDOR_ID_LENOVO,
5248 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5249 .quirks = 0x1fffU,
5250 },
5251 { /* IBM ThinkPads with no EC version string */
5252 .vendor = PCI_VENDOR_ID_IBM,
5253 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5254 .quirks = 0x00ffU,
5255 },
5256 { /* IBM ThinkPads with EC version string */
5257 .vendor = PCI_VENDOR_ID_IBM,
5258 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5259 .quirks = 0x00bfU,
5260 },
5261};
5262
5263#undef TPACPI_LEDQ_IBM
5264#undef TPACPI_LEDQ_LNV
5265
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005266static enum led_access_mode __init led_init_detect_mode(void)
5267{
5268 acpi_status status;
5269
5270 if (tpacpi_is_ibm()) {
5271 /* 570 */
5272 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5273 if (ACPI_SUCCESS(status))
5274 return TPACPI_LED_570;
5275
5276 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5277 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5278 if (ACPI_SUCCESS(status))
5279 return TPACPI_LED_OLD;
5280 }
5281
5282 /* most others */
5283 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5284 if (ACPI_SUCCESS(status))
5285 return TPACPI_LED_NEW;
5286
5287 /* R30, R31, and unknown firmwares */
5288 led_handle = NULL;
5289 return TPACPI_LED_NONE;
5290}
5291
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005292static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005293{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005294 unsigned int i;
5295 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005296 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005297
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005298 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5299
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005300 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005301
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005302 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5303 str_supported(led_supported), led_supported);
5304
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005305 if (led_supported == TPACPI_LED_NONE)
5306 return 1;
5307
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005308 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5309 GFP_KERNEL);
5310 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005311 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005312 return -ENOMEM;
5313 }
5314
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005315 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5316 ARRAY_SIZE(led_useful_qtable));
5317
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005318 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005319 if (!tpacpi_is_led_restricted(i) &&
5320 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005321 rc = tpacpi_init_led(i);
5322 if (rc < 0) {
5323 led_exit();
5324 return rc;
5325 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005326 }
5327 }
5328
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005329#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Joe Perches0978e012011-04-04 10:06:25 -07005330 pr_notice("warning: userspace override of important "
5331 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005332#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005333 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005334}
5335
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005336#define str_led_status(s) \
5337 ((s) == TPACPI_LED_OFF ? "off" : \
5338 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005339
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005340static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005342 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005343 seq_printf(m, "status:\t\tnot supported\n");
5344 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005345 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005346 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005347
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005348 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005349 /* 570 */
5350 int i, status;
5351 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005352 status = led_get_status(i);
5353 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005354 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005355 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005356 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005357 }
5358 }
5359
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005360 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005361 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364}
5365
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005366static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367{
5368 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005369 int led, rc;
5370 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005371
5372 if (!led_supported)
5373 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
5375 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005376 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 return -EINVAL;
5378
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005379 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005380 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005382 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005383 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005384 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005385 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005386 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005387 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005388
5389 rc = led_set_status(led, s);
5390 if (rc < 0)
5391 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 }
5393
5394 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005395}
5396
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005397static struct ibm_struct led_driver_data = {
5398 .name = "led",
5399 .read = led_read,
5400 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005401 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005402};
5403
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005404/*************************************************************************
5405 * Beep subdriver
5406 */
5407
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005408TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005409
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005410#define TPACPI_BEEP_Q1 0x0001
5411
5412static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5413 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5414 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5415};
5416
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005417static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005418{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005419 unsigned long quirks;
5420
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005421 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5422
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005423 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005424
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005425 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5426 str_supported(beep_handle != NULL));
5427
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005428 quirks = tpacpi_check_quirks(beep_quirk_table,
5429 ARRAY_SIZE(beep_quirk_table));
5430
5431 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5432
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005433 return (beep_handle)? 0 : 1;
5434}
5435
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005436static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005438 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005439 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005440 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005441 seq_printf(m, "status:\t\tsupported\n");
5442 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446}
5447
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005448static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449{
5450 char *cmd;
5451 int beep_cmd;
5452
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005453 if (!beep_handle)
5454 return -ENODEV;
5455
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005457 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5458 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 /* beep_cmd set */
5460 } else
5461 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005462 if (tp_features.beep_needs_two_args) {
5463 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5464 beep_cmd, 0))
5465 return -EIO;
5466 } else {
5467 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5468 beep_cmd))
5469 return -EIO;
5470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 }
5472
5473 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005474}
5475
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005476static struct ibm_struct beep_driver_data = {
5477 .name = "beep",
5478 .read = beep_read,
5479 .write = beep_write,
5480};
5481
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005482/*************************************************************************
5483 * Thermal subdriver
5484 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005485
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005486enum thermal_access_mode {
5487 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5488 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5489 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5490 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5491 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5492};
5493
5494enum { /* TPACPI_THERMAL_TPEC_* */
5495 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5496 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5497 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005498
5499 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005500};
5501
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005502
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005503#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5504struct ibm_thermal_sensors_struct {
5505 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5506};
5507
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005508static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005509
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005510/* idx is zero-based */
5511static int thermal_get_sensor(int idx, s32 *value)
5512{
5513 int t;
5514 s8 tmp;
5515 char tmpi[5];
5516
5517 t = TP_EC_THERMAL_TMP0;
5518
5519 switch (thermal_read_mode) {
5520#if TPACPI_MAX_THERMAL_SENSORS >= 16
5521 case TPACPI_THERMAL_TPEC_16:
5522 if (idx >= 8 && idx <= 15) {
5523 t = TP_EC_THERMAL_TMP8;
5524 idx -= 8;
5525 }
5526 /* fallthrough */
5527#endif
5528 case TPACPI_THERMAL_TPEC_8:
5529 if (idx <= 7) {
5530 if (!acpi_ec_read(t + idx, &tmp))
5531 return -EIO;
5532 *value = tmp * 1000;
5533 return 0;
5534 }
5535 break;
5536
5537 case TPACPI_THERMAL_ACPI_UPDT:
5538 if (idx <= 7) {
5539 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5540 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5541 return -EIO;
5542 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5543 return -EIO;
5544 *value = (t - 2732) * 100;
5545 return 0;
5546 }
5547 break;
5548
5549 case TPACPI_THERMAL_ACPI_TMP07:
5550 if (idx <= 7) {
5551 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5552 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5553 return -EIO;
5554 if (t > 127 || t < -127)
5555 t = TP_EC_THERMAL_TMP_NA;
5556 *value = t * 1000;
5557 return 0;
5558 }
5559 break;
5560
5561 case TPACPI_THERMAL_NONE:
5562 default:
5563 return -ENOSYS;
5564 }
5565
5566 return -EINVAL;
5567}
5568
5569static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5570{
5571 int res, i;
5572 int n;
5573
5574 n = 8;
5575 i = 0;
5576
5577 if (!s)
5578 return -EINVAL;
5579
5580 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5581 n = 16;
5582
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005583 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005584 res = thermal_get_sensor(i, &s->temp[i]);
5585 if (res)
5586 return res;
5587 }
5588
5589 return n;
5590}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005591
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005592static void thermal_dump_all_sensors(void)
5593{
5594 int n, i;
5595 struct ibm_thermal_sensors_struct t;
5596
5597 n = thermal_get_sensors(&t);
5598 if (n <= 0)
5599 return;
5600
Joe Perches0978e012011-04-04 10:06:25 -07005601 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005602
5603 for (i = 0; i < n; i++) {
5604 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07005605 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005606 else
Joe Perches0978e012011-04-04 10:06:25 -07005607 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005608 }
5609
Joe Perches0978e012011-04-04 10:06:25 -07005610 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005611}
5612
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005613/* sysfs temp##_input -------------------------------------------------- */
5614
5615static ssize_t thermal_temp_input_show(struct device *dev,
5616 struct device_attribute *attr,
5617 char *buf)
5618{
5619 struct sensor_device_attribute *sensor_attr =
5620 to_sensor_dev_attr(attr);
5621 int idx = sensor_attr->index;
5622 s32 value;
5623 int res;
5624
5625 res = thermal_get_sensor(idx, &value);
5626 if (res)
5627 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005628 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005629 return -ENXIO;
5630
5631 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5632}
5633
5634#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005635 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5636 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005637
5638static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5639 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5640 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5641 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5642 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5643 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5644 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5645 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5646 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5647 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5648 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5649 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5650 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5651 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5652 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5653 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5654 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5655};
5656
5657#define THERMAL_ATTRS(X) \
5658 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5659
5660static struct attribute *thermal_temp_input_attr[] = {
5661 THERMAL_ATTRS(8),
5662 THERMAL_ATTRS(9),
5663 THERMAL_ATTRS(10),
5664 THERMAL_ATTRS(11),
5665 THERMAL_ATTRS(12),
5666 THERMAL_ATTRS(13),
5667 THERMAL_ATTRS(14),
5668 THERMAL_ATTRS(15),
5669 THERMAL_ATTRS(0),
5670 THERMAL_ATTRS(1),
5671 THERMAL_ATTRS(2),
5672 THERMAL_ATTRS(3),
5673 THERMAL_ATTRS(4),
5674 THERMAL_ATTRS(5),
5675 THERMAL_ATTRS(6),
5676 THERMAL_ATTRS(7),
5677 NULL
5678};
5679
5680static const struct attribute_group thermal_temp_input16_group = {
5681 .attrs = thermal_temp_input_attr
5682};
5683
5684static const struct attribute_group thermal_temp_input8_group = {
5685 .attrs = &thermal_temp_input_attr[8]
5686};
5687
5688#undef THERMAL_SENSOR_ATTR_TEMP
5689#undef THERMAL_ATTRS
5690
5691/* --------------------------------------------------------------------- */
5692
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005693static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005694{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005695 u8 t, ta1, ta2;
5696 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005697 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005698 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005699
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005700 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5701
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005702 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005703
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005704 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005705 /*
5706 * Direct EC access mode: sensors at registers
5707 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5708 * non-implemented, thermal sensors return 0x80 when
5709 * not available
5710 */
5711
5712 ta1 = ta2 = 0;
5713 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005714 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005715 ta1 |= t;
5716 } else {
5717 ta1 = 0;
5718 break;
5719 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005720 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005721 ta2 |= t;
5722 } else {
5723 ta1 = 0;
5724 break;
5725 }
5726 }
5727 if (ta1 == 0) {
5728 /* This is sheer paranoia, but we handle it anyway */
5729 if (acpi_tmp7) {
Joe Perches0978e012011-04-04 10:06:25 -07005730 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005731 "falling back to ACPI TMPx access "
5732 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005733 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005734 } else {
Joe Perches0978e012011-04-04 10:06:25 -07005735 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005736 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005737 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005738 }
5739 } else {
5740 thermal_read_mode =
5741 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005742 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005743 }
5744 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005745 if (tpacpi_is_ibm() &&
5746 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005747 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005748 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005749 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005750 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005751 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005752 }
5753 } else {
5754 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005755 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005756 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005757
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005758 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5759 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5760 thermal_read_mode);
5761
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005762 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005763 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005764 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005765 &thermal_temp_input16_group);
5766 if (res)
5767 return res;
5768 break;
5769 case TPACPI_THERMAL_TPEC_8:
5770 case TPACPI_THERMAL_ACPI_TMP07:
5771 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005772 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005773 &thermal_temp_input8_group);
5774 if (res)
5775 return res;
5776 break;
5777 case TPACPI_THERMAL_NONE:
5778 default:
5779 return 1;
5780 }
5781
5782 return 0;
5783}
5784
5785static void thermal_exit(void)
5786{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005787 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005788 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005789 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005790 &thermal_temp_input16_group);
5791 break;
5792 case TPACPI_THERMAL_TPEC_8:
5793 case TPACPI_THERMAL_ACPI_TMP07:
5794 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005795 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08005796 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005797 break;
5798 case TPACPI_THERMAL_NONE:
5799 default:
5800 break;
5801 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005802}
5803
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005804static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005805{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005806 int n, i;
5807 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005808
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005809 n = thermal_get_sensors(&t);
5810 if (unlikely(n < 0))
5811 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005812
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005813 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005814
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005815 if (n > 0) {
5816 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005817 seq_printf(m, "%d ", t.temp[i] / 1000);
5818 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005819 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005820 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005821
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005822 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005823}
5824
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005825static struct ibm_struct thermal_driver_data = {
5826 .name = "thermal",
5827 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005828 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005829};
5830
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005831/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005832 * Backlight/brightness subdriver
5833 */
Holger Macht8acb0252006-10-20 14:30:28 -07005834
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005835#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5836
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005837/*
5838 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5839 * CMOS NVRAM byte 0x5E, bits 0-3.
5840 *
5841 * EC HBRV (0x31) has the following layout
5842 * Bit 7: unknown function
5843 * Bit 6: unknown function
5844 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5845 * Bit 4: must be set to zero to avoid problems
5846 * Bit 3-0: backlight brightness level
5847 *
5848 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005849 *
5850 * WARNING: The X61 has been verified to use HBRV for something else, so
5851 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5852 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005853 */
5854
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005855enum {
5856 TP_EC_BACKLIGHT = 0x31,
5857
5858 /* TP_EC_BACKLIGHT bitmasks */
5859 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5860 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5861 TP_EC_BACKLIGHT_MAPSW = 0x20,
5862};
5863
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005864enum tpacpi_brightness_access_mode {
5865 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5866 TPACPI_BRGHT_MODE_EC, /* EC control */
5867 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5868 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5869 TPACPI_BRGHT_MODE_MAX
5870};
5871
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005872static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005873
5874static enum tpacpi_brightness_access_mode brightness_mode =
5875 TPACPI_BRGHT_MODE_MAX;
5876
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005877static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5878
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005879static struct mutex brightness_mutex;
5880
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005881/* NVRAM brightness access,
5882 * call with brightness_mutex held! */
5883static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005884{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005885 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005886
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005887 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5888 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5889 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03005890 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005891
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005892 return lnvram;
5893}
5894
5895static void tpacpi_brightness_checkpoint_nvram(void)
5896{
5897 u8 lec = 0;
5898 u8 b_nvram;
5899
5900 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5901 return;
5902
5903 vdbg_printk(TPACPI_DBG_BRGHT,
5904 "trying to checkpoint backlight level to NVRAM...\n");
5905
5906 if (mutex_lock_killable(&brightness_mutex) < 0)
5907 return;
5908
5909 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5910 goto unlock;
5911 lec &= TP_EC_BACKLIGHT_LVLMSK;
5912 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5913
5914 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5915 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5916 /* NVRAM needs update */
5917 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5918 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5919 b_nvram |= lec;
5920 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5921 dbg_printk(TPACPI_DBG_BRGHT,
5922 "updated NVRAM backlight level to %u (0x%02x)\n",
5923 (unsigned int) lec, (unsigned int) b_nvram);
5924 } else
5925 vdbg_printk(TPACPI_DBG_BRGHT,
5926 "NVRAM backlight level already is %u (0x%02x)\n",
5927 (unsigned int) lec, (unsigned int) b_nvram);
5928
5929unlock:
5930 mutex_unlock(&brightness_mutex);
5931}
5932
5933
5934/* call with brightness_mutex held! */
5935static int tpacpi_brightness_get_raw(int *status)
5936{
5937 u8 lec = 0;
5938
5939 switch (brightness_mode) {
5940 case TPACPI_BRGHT_MODE_UCMS_STEP:
5941 *status = tpacpi_brightness_nvram_get();
5942 return 0;
5943 case TPACPI_BRGHT_MODE_EC:
5944 case TPACPI_BRGHT_MODE_ECNVRAM:
5945 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005946 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005947 *status = lec;
5948 return 0;
5949 default:
5950 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005951 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005952}
5953
5954/* call with brightness_mutex held! */
5955/* do NOT call with illegal backlight level value */
5956static int tpacpi_brightness_set_ec(unsigned int value)
5957{
5958 u8 lec = 0;
5959
5960 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5961 return -EIO;
5962
5963 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5964 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5965 (value & TP_EC_BACKLIGHT_LVLMSK))))
5966 return -EIO;
5967
5968 return 0;
5969}
5970
5971/* call with brightness_mutex held! */
5972static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5973{
5974 int cmos_cmd, inc;
5975 unsigned int current_value, i;
5976
5977 current_value = tpacpi_brightness_nvram_get();
5978
5979 if (value == current_value)
5980 return 0;
5981
5982 cmos_cmd = (value > current_value) ?
5983 TP_CMOS_BRIGHTNESS_UP :
5984 TP_CMOS_BRIGHTNESS_DOWN;
5985 inc = (value > current_value) ? 1 : -1;
5986
5987 for (i = current_value; i != value; i += inc)
5988 if (issue_thinkpad_cmos_command(cmos_cmd))
5989 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005990
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005991 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005992}
5993
5994/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005995static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005996{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005997 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005998
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03005999 if (value > bright_maxlvl || value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006000 return -EINVAL;
6001
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006002 vdbg_printk(TPACPI_DBG_BRGHT,
6003 "set backlight level to %d\n", value);
6004
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006005 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006006 if (res < 0)
6007 return res;
6008
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006009 switch (brightness_mode) {
6010 case TPACPI_BRGHT_MODE_EC:
6011 case TPACPI_BRGHT_MODE_ECNVRAM:
6012 res = tpacpi_brightness_set_ec(value);
6013 break;
6014 case TPACPI_BRGHT_MODE_UCMS_STEP:
6015 res = tpacpi_brightness_set_ucmsstep(value);
6016 break;
6017 default:
6018 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006019 }
6020
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006021 mutex_unlock(&brightness_mutex);
6022 return res;
6023}
6024
6025/* sysfs backlight class ----------------------------------------------- */
6026
6027static int brightness_update_status(struct backlight_device *bd)
6028{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006029 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006030 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6031 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006032 bd->props.brightness : 0;
6033
6034 dbg_printk(TPACPI_DBG_BRGHT,
6035 "backlight: attempt to set level to %d\n",
6036 level);
6037
6038 /* it is the backlight class's job (caller) to handle
6039 * EINTR and other errors properly */
6040 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006041}
Holger Macht8acb0252006-10-20 14:30:28 -07006042
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006043static int brightness_get(struct backlight_device *bd)
6044{
6045 int status, res;
6046
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006047 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006048 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006049 return 0;
6050
6051 res = tpacpi_brightness_get_raw(&status);
6052
6053 mutex_unlock(&brightness_mutex);
6054
6055 if (res < 0)
6056 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006057
6058 return status & TP_EC_BACKLIGHT_LVLMSK;
6059}
6060
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006061static void tpacpi_brightness_notify_change(void)
6062{
6063 backlight_force_update(ibm_backlight_device,
6064 BACKLIGHT_UPDATE_HOTKEY);
6065}
6066
Lionel Debrouxacc24722010-11-16 14:14:02 +01006067static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006068 .get_brightness = brightness_get,
6069 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006070};
6071
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006072/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006073
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006074/*
6075 * Call _BCL method of video device. On some ThinkPads this will
6076 * switch the firmware to the ACPI brightness control mode.
6077 */
6078
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006079static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6080{
6081 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6082 union acpi_object *obj;
6083 int rc;
6084
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006085 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006086 obj = (union acpi_object *)buffer.pointer;
6087 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006088 pr_err("Unknown _BCL data, please report this to %s\n",
6089 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006090 rc = 0;
6091 } else {
6092 rc = obj->package.count;
6093 }
6094 } else {
6095 return 0;
6096 }
6097
6098 kfree(buffer.pointer);
6099 return rc;
6100}
6101
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006102
6103/*
6104 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6105 */
6106static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6107{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006108 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006109 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006110
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006111 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6112 if (video_device)
6113 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006114
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006115 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006116
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006117 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006118}
6119
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006120/*
6121 * These are only useful for models that have only one possibility
6122 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6123 * these quirks.
6124 */
6125#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6126#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6127#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6128
6129static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6130 /* Models with ATI GPUs known to require ECNVRAM mode */
6131 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6132
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006133 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006134 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006135 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006136 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006137 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6138
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006139 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006140 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006141 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6142 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006143
6144 /* Models with Intel GMA900 */
6145 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6146 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6147 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6148};
6149
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006150/*
6151 * Returns < 0 for error, otherwise sets tp_features.bright_*
6152 * and bright_maxlvl.
6153 */
6154static void __init tpacpi_detect_brightness_capabilities(void)
6155{
6156 unsigned int b;
6157
6158 vdbg_printk(TPACPI_DBG_INIT,
6159 "detecting firmware brightness interface capabilities\n");
6160
6161 /* we could run a quirks check here (same table used by
6162 * brightness_init) if needed */
6163
6164 /*
6165 * We always attempt to detect acpi support, so as to switch
6166 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6167 * going to publish a backlight interface
6168 */
6169 b = tpacpi_check_std_acpi_brightness_support();
6170 switch (b) {
6171 case 16:
6172 bright_maxlvl = 15;
Joe Perches0978e012011-04-04 10:06:25 -07006173 pr_info("detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006174 break;
6175 case 8:
6176 case 0:
6177 bright_maxlvl = 7;
Joe Perches0978e012011-04-04 10:06:25 -07006178 pr_info("detected a 8-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006179 break;
6180 default:
Joe Perches0978e012011-04-04 10:06:25 -07006181 pr_err("Unsupported brightness interface, "
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006182 "please contact %s\n", TPACPI_MAIL);
6183 tp_features.bright_unkfw = 1;
6184 bright_maxlvl = b - 1;
6185 }
6186}
6187
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006188static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006189{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006190 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006191 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006192 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006193
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006194 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6195
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006196 mutex_init(&brightness_mutex);
6197
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006198 quirks = tpacpi_check_quirks(brightness_quirk_table,
6199 ARRAY_SIZE(brightness_quirk_table));
6200
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006201 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006202
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006203 /* if it is unknown, we don't handle it: it wouldn't be safe */
6204 if (tp_features.bright_unkfw)
6205 return 1;
6206
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006207 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006208 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006209 "brightness support disabled by "
6210 "module parameter\n");
6211 return 1;
6212 }
6213
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006214 if (acpi_video_backlight_support()) {
6215 if (brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006216 pr_info("Standard ACPI backlight interface "
6217 "available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006218 return 1;
6219 } else if (brightness_enable == 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006220 pr_warn("Cannot enable backlight brightness support, "
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006221 "ACPI is already handling it. Refer to the "
Joe Perches0978e012011-04-04 10:06:25 -07006222 "acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006223 return 1;
6224 }
6225 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006226 pr_notice("Standard ACPI backlight interface not "
6227 "available, thinkpad_acpi native "
6228 "brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006229 }
6230
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006231 /*
6232 * Check for module parameter bogosity, note that we
6233 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6234 * able to detect "unspecified"
6235 */
6236 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006237 return -EINVAL;
6238
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006239 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6240 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6241 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006242 if (quirks & TPACPI_BRGHT_Q_EC)
6243 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6244 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006245 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6246
6247 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006248 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006249 brightness_mode);
6250 }
6251
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006252 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006253 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006254 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6255 brightness_mode == TPACPI_BRGHT_MODE_EC))
6256 return -EINVAL;
6257
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006258 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006259 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006260
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006261 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006262 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006263 props.max_brightness = bright_maxlvl;
6264 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006265 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6266 NULL, NULL,
6267 &ibm_backlight_data,
6268 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006269 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006270 int rc = PTR_ERR(ibm_backlight_device);
6271 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006272 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006273 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006274 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006275 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6276 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006277
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006278 if (quirks & TPACPI_BRGHT_Q_ASK) {
Joe Perches0978e012011-04-04 10:06:25 -07006279 pr_notice("brightness: will use unverified default: "
6280 "brightness_mode=%d\n", brightness_mode);
6281 pr_notice("brightness: please report to %s whether it works well "
6282 "or not on your ThinkPad\n", TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006283 }
6284
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006285 /* Added by mistake in early 2007. Probably useless, but it could
6286 * be working around some unknown firmware problem where the value
6287 * read at startup doesn't match the real hardware state... so leave
6288 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006289 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006290
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006291 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6292 "brightness: registering brightness hotkeys "
6293 "as change notification\n");
6294 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6295 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006296 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006297 return 0;
6298}
6299
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006300static void brightness_suspend(pm_message_t state)
6301{
6302 tpacpi_brightness_checkpoint_nvram();
6303}
6304
6305static void brightness_shutdown(void)
6306{
6307 tpacpi_brightness_checkpoint_nvram();
6308}
6309
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006310static void brightness_exit(void)
6311{
6312 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006313 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006314 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006315 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006316 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006317
6318 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006319}
6320
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006321static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006322{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006323 int level;
6324
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006325 level = brightness_get(NULL);
6326 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006327 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006328 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006329 seq_printf(m, "level:\t\t%d\n", level);
6330 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006331 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6332 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006333 }
6334
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006335 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006336}
6337
6338static int brightness_write(char *buf)
6339{
6340 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006341 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006342 char *cmd;
6343
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006344 level = brightness_get(NULL);
6345 if (level < 0)
6346 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006347
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006348 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006349 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006350 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006351 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006352 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006353 if (level > 0)
6354 level--;
6355 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006356 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006357 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006358 } else
6359 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006360 }
6361
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006362 tpacpi_disclose_usertask("procfs brightness",
6363 "set level to %d\n", level);
6364
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006365 /*
6366 * Now we know what the final level should be, so we try to set it.
6367 * Doing it this way makes the syscall restartable in case of EINTR
6368 */
6369 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006370 if (!rc && ibm_backlight_device)
6371 backlight_force_update(ibm_backlight_device,
6372 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006373 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006374}
6375
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006376static struct ibm_struct brightness_driver_data = {
6377 .name = "brightness",
6378 .read = brightness_read,
6379 .write = brightness_write,
6380 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006381 .suspend = brightness_suspend,
6382 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006383};
6384
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006385/*************************************************************************
6386 * Volume subdriver
6387 */
6388
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006389/*
6390 * IBM ThinkPads have a simple volume controller with MUTE gating.
6391 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6392 *
6393 * Since the *61 series (and probably also the later *60 series), Lenovo
6394 * ThinkPads only implement the MUTE gate.
6395 *
6396 * EC register 0x30
6397 * Bit 6: MUTE (1 mutes sound)
6398 * Bit 3-0: Volume
6399 * Other bits should be zero as far as we know.
6400 *
6401 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6402 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6403 * such as bit 7 which is used to detect repeated presses of MUTE,
6404 * and we leave them unchanged.
6405 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006406
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006407#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6408
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006409#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6410#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6411#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6412
Henrique de Moraes Holschuhead510c2009-12-26 22:52:13 -02006413static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006414static char *alsa_id = "ThinkPadEC";
6415static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6416
6417struct tpacpi_alsa_data {
6418 struct snd_card *card;
6419 struct snd_ctl_elem_id *ctl_mute_id;
6420 struct snd_ctl_elem_id *ctl_vol_id;
6421};
6422
6423static struct snd_card *alsa_card;
6424
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006425enum {
6426 TP_EC_AUDIO = 0x30,
6427
6428 /* TP_EC_AUDIO bits */
6429 TP_EC_AUDIO_MUTESW = 6,
6430
6431 /* TP_EC_AUDIO bitmasks */
6432 TP_EC_AUDIO_LVL_MSK = 0x0F,
6433 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6434
6435 /* Maximum volume */
6436 TP_EC_VOLUME_MAX = 14,
6437};
6438
6439enum tpacpi_volume_access_mode {
6440 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6441 TPACPI_VOL_MODE_EC, /* Pure EC control */
6442 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6443 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6444 TPACPI_VOL_MODE_MAX
6445};
6446
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006447enum tpacpi_volume_capabilities {
6448 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6449 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6450 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6451 TPACPI_VOL_CAP_MAX
6452};
6453
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006454static enum tpacpi_volume_access_mode volume_mode =
6455 TPACPI_VOL_MODE_MAX;
6456
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006457static enum tpacpi_volume_capabilities volume_capabilities;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006458static int volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006459
6460/*
6461 * Used to syncronize writers to TP_EC_AUDIO and
6462 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6463 */
6464static struct mutex volume_mutex;
6465
6466static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006467{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006468 u8 lec = 0;
6469 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006470 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006471
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006472 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6473 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006474 if (!volume_control_allowed)
6475 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006476
6477 vdbg_printk(TPACPI_DBG_MIXER,
6478 "trying to checkpoint mixer state to NVRAM...\n");
6479
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006480 if (tp_features.mixer_no_level_control)
6481 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6482 else
6483 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6484
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006485 if (mutex_lock_killable(&volume_mutex) < 0)
6486 return;
6487
6488 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6489 goto unlock;
6490 lec &= ec_mask;
6491 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6492
6493 if (lec != (b_nvram & ec_mask)) {
6494 /* NVRAM needs update */
6495 b_nvram &= ~ec_mask;
6496 b_nvram |= lec;
6497 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6498 dbg_printk(TPACPI_DBG_MIXER,
6499 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6500 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006501 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006502 vdbg_printk(TPACPI_DBG_MIXER,
6503 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6504 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006505 }
6506
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006507unlock:
6508 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006509}
6510
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006511static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006512{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006513 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006514
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006515 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6516 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006517
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006518 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006519
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006520 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006521
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006522 return 0;
6523}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006524
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006525static int volume_get_status(u8 *status)
6526{
6527 return volume_get_status_ec(status);
6528}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006529
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006530static int volume_set_status_ec(const u8 status)
6531{
6532 if (!acpi_ec_write(TP_EC_AUDIO, status))
6533 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006534
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006535 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6536
6537 return 0;
6538}
6539
6540static int volume_set_status(const u8 status)
6541{
6542 return volume_set_status_ec(status);
6543}
6544
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006545/* returns < 0 on error, 0 on no change, 1 on change */
6546static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006547{
6548 int rc;
6549 u8 s, n;
6550
6551 if (mutex_lock_killable(&volume_mutex) < 0)
6552 return -EINTR;
6553
6554 rc = volume_get_status_ec(&s);
6555 if (rc)
6556 goto unlock;
6557
6558 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6559 s & ~TP_EC_AUDIO_MUTESW_MSK;
6560
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006561 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006562 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006563 if (!rc)
6564 rc = 1;
6565 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006566
6567unlock:
6568 mutex_unlock(&volume_mutex);
6569 return rc;
6570}
6571
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006572static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006573{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006574 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006575 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006576 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006577}
6578
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006579static int volume_set_mute(const bool mute)
6580{
6581 int rc;
6582
6583 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6584 (mute) ? "" : "un");
6585
6586 rc = __volume_set_mute_ec(mute);
6587 return (rc < 0) ? rc : 0;
6588}
6589
6590/* returns < 0 on error, 0 on no change, 1 on change */
6591static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006592{
6593 int rc;
6594 u8 s, n;
6595
6596 if (vol > TP_EC_VOLUME_MAX)
6597 return -EINVAL;
6598
6599 if (mutex_lock_killable(&volume_mutex) < 0)
6600 return -EINTR;
6601
6602 rc = volume_get_status_ec(&s);
6603 if (rc)
6604 goto unlock;
6605
6606 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6607
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006608 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006609 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006610 if (!rc)
6611 rc = 1;
6612 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006613
6614unlock:
6615 mutex_unlock(&volume_mutex);
6616 return rc;
6617}
6618
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006619static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006620{
6621 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006622 "ALSA: trying to set volume level to %hu\n", vol);
6623 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006624}
6625
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006626static void volume_alsa_notify_change(void)
6627{
6628 struct tpacpi_alsa_data *d;
6629
6630 if (alsa_card && alsa_card->private_data) {
6631 d = alsa_card->private_data;
6632 if (d->ctl_mute_id)
6633 snd_ctl_notify(alsa_card,
6634 SNDRV_CTL_EVENT_MASK_VALUE,
6635 d->ctl_mute_id);
6636 if (d->ctl_vol_id)
6637 snd_ctl_notify(alsa_card,
6638 SNDRV_CTL_EVENT_MASK_VALUE,
6639 d->ctl_vol_id);
6640 }
6641}
6642
6643static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6644 struct snd_ctl_elem_info *uinfo)
6645{
6646 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6647 uinfo->count = 1;
6648 uinfo->value.integer.min = 0;
6649 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6650 return 0;
6651}
6652
6653static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6654 struct snd_ctl_elem_value *ucontrol)
6655{
6656 u8 s;
6657 int rc;
6658
6659 rc = volume_get_status(&s);
6660 if (rc < 0)
6661 return rc;
6662
6663 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6664 return 0;
6665}
6666
6667static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6668 struct snd_ctl_elem_value *ucontrol)
6669{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006670 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6671 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006672 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006673}
6674
6675#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6676
6677static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6678 struct snd_ctl_elem_value *ucontrol)
6679{
6680 u8 s;
6681 int rc;
6682
6683 rc = volume_get_status(&s);
6684 if (rc < 0)
6685 return rc;
6686
6687 ucontrol->value.integer.value[0] =
6688 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6689 return 0;
6690}
6691
6692static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6693 struct snd_ctl_elem_value *ucontrol)
6694{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006695 tpacpi_disclose_usertask("ALSA", "%smute\n",
6696 ucontrol->value.integer.value[0] ?
6697 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006698 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006699}
6700
6701static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6702 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6703 .name = "Console Playback Volume",
6704 .index = 0,
6705 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6706 .info = volume_alsa_vol_info,
6707 .get = volume_alsa_vol_get,
6708};
6709
6710static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6711 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6712 .name = "Console Playback Switch",
6713 .index = 0,
6714 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6715 .info = volume_alsa_mute_info,
6716 .get = volume_alsa_mute_get,
6717};
6718
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006719static void volume_suspend(pm_message_t state)
6720{
6721 tpacpi_volume_checkpoint_nvram();
6722}
6723
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006724static void volume_resume(void)
6725{
6726 volume_alsa_notify_change();
6727}
6728
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006729static void volume_shutdown(void)
6730{
6731 tpacpi_volume_checkpoint_nvram();
6732}
6733
6734static void volume_exit(void)
6735{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006736 if (alsa_card) {
6737 snd_card_free(alsa_card);
6738 alsa_card = NULL;
6739 }
6740
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006741 tpacpi_volume_checkpoint_nvram();
6742}
6743
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006744static int __init volume_create_alsa_mixer(void)
6745{
6746 struct snd_card *card;
6747 struct tpacpi_alsa_data *data;
6748 struct snd_kcontrol *ctl_vol;
6749 struct snd_kcontrol *ctl_mute;
6750 int rc;
6751
6752 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6753 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006754 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07006755 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006756 return 1;
6757 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006758
6759 BUG_ON(!card->private_data);
6760 data = card->private_data;
6761 data->card = card;
6762
6763 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6764 sizeof(card->driver));
6765 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6766 sizeof(card->shortname));
6767 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6768 (thinkpad_id.ec_version_str) ?
6769 thinkpad_id.ec_version_str : "(unknown)");
6770 snprintf(card->longname, sizeof(card->longname),
6771 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6772 (thinkpad_id.ec_version_str) ?
6773 thinkpad_id.ec_version_str : "unknown");
6774
6775 if (volume_control_allowed) {
6776 volume_alsa_control_vol.put = volume_alsa_vol_put;
6777 volume_alsa_control_vol.access =
6778 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6779
6780 volume_alsa_control_mute.put = volume_alsa_mute_put;
6781 volume_alsa_control_mute.access =
6782 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6783 }
6784
6785 if (!tp_features.mixer_no_level_control) {
6786 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6787 rc = snd_ctl_add(card, ctl_vol);
6788 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006789 pr_err("Failed to create ALSA volume control: %d\n",
6790 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006791 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006792 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006793 data->ctl_vol_id = &ctl_vol->id;
6794 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006795
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006796 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6797 rc = snd_ctl_add(card, ctl_mute);
6798 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006799 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006800 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006801 }
6802 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006803
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006804 snd_card_set_dev(card, &tpacpi_pdev->dev);
6805 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006806 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006807 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006808 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006809 }
6810
6811 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006812 return 0;
6813
6814err_exit:
6815 snd_card_free(card);
6816 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006817}
6818
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006819#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6820#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6821
6822static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6823 /* Whitelist volume level on all IBM by default */
6824 { .vendor = PCI_VENDOR_ID_IBM,
6825 .bios = TPACPI_MATCH_ANY,
6826 .ec = TPACPI_MATCH_ANY,
6827 .quirks = TPACPI_VOL_Q_LEVEL },
6828
6829 /* Lenovo models with volume control (needs confirmation) */
6830 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6831 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6832 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6833 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6834 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6835 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6836 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6837
6838 /* Whitelist mute-only on all Lenovo by default */
6839 { .vendor = PCI_VENDOR_ID_LENOVO,
6840 .bios = TPACPI_MATCH_ANY,
6841 .ec = TPACPI_MATCH_ANY,
6842 .quirks = TPACPI_VOL_Q_MUTEONLY }
6843};
6844
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006845static int __init volume_init(struct ibm_init_struct *iibm)
6846{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006847 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006848 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006849
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006850 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6851
6852 mutex_init(&volume_mutex);
6853
6854 /*
6855 * Check for module parameter bogosity, note that we
6856 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6857 * able to detect "unspecified"
6858 */
6859 if (volume_mode > TPACPI_VOL_MODE_MAX)
6860 return -EINVAL;
6861
6862 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Joe Perches0978e012011-04-04 10:06:25 -07006863 pr_err("UCMS step volume mode not implemented, "
6864 "please contact %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006865 return 1;
6866 }
6867
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006868 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6869 return -EINVAL;
6870
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006871 /*
6872 * The ALSA mixer is our primary interface.
6873 * When disabled, don't install the subdriver at all
6874 */
6875 if (!alsa_enable) {
6876 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6877 "ALSA mixer disabled by parameter, "
6878 "not loading volume subdriver...\n");
6879 return 1;
6880 }
6881
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006882 quirks = tpacpi_check_quirks(volume_quirk_table,
6883 ARRAY_SIZE(volume_quirk_table));
6884
6885 switch (volume_capabilities) {
6886 case TPACPI_VOL_CAP_AUTO:
6887 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6888 tp_features.mixer_no_level_control = 1;
6889 else if (quirks & TPACPI_VOL_Q_LEVEL)
6890 tp_features.mixer_no_level_control = 0;
6891 else
6892 return 1; /* no mixer */
6893 break;
6894 case TPACPI_VOL_CAP_VOLMUTE:
6895 tp_features.mixer_no_level_control = 0;
6896 break;
6897 case TPACPI_VOL_CAP_MUTEONLY:
6898 tp_features.mixer_no_level_control = 1;
6899 break;
6900 default:
6901 return 1;
6902 }
6903
6904 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6905 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6906 "using user-supplied volume_capabilities=%d\n",
6907 volume_capabilities);
6908
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006909 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6910 volume_mode == TPACPI_VOL_MODE_MAX) {
6911 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6912
6913 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6914 "driver auto-selected volume_mode=%d\n",
6915 volume_mode);
6916 } else {
6917 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6918 "using user-supplied volume_mode=%d\n",
6919 volume_mode);
6920 }
6921
6922 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006923 "mute is supported, volume control is %s\n",
6924 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006925
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006926 rc = volume_create_alsa_mixer();
6927 if (rc) {
Joe Perches0978e012011-04-04 10:06:25 -07006928 pr_err("Could not create the ALSA mixer interface\n");
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006929 return rc;
6930 }
6931
Joe Perches0978e012011-04-04 10:06:25 -07006932 pr_info("Console audio control enabled, mode: %s\n",
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006933 (volume_control_allowed) ?
6934 "override (read/write)" :
6935 "monitor (read only)");
6936
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006937 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6938 "registering volume hotkeys as change notification\n");
6939 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6940 | TP_ACPI_HKEY_VOLUP_MASK
6941 | TP_ACPI_HKEY_VOLDWN_MASK
6942 | TP_ACPI_HKEY_MUTE_MASK);
6943
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006944 return 0;
6945}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006946
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006947static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006948{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006949 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006950
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006951 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006952 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006953 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006954 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006955 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006956 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006957 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006958 status & TP_EC_AUDIO_LVL_MSK);
6959
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006960 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006961 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006962
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006963 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006964 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006965 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006966 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006967 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006968 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006969 "commands:\tlevel <level>"
6970 " (<level> is 0-%d)\n",
6971 TP_EC_VOLUME_MAX);
6972 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006973 }
6974 }
6975
6976 return 0;
6977}
6978
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006979static int volume_write(char *buf)
6980{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006981 u8 s;
6982 u8 new_level, new_mute;
6983 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006984 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006985 int rc;
6986
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006987 /*
6988 * We do allow volume control at driver startup, so that the
6989 * user can set initial state through the volume=... parameter hack.
6990 */
6991 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6992 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6993 tp_warned.volume_ctrl_forbidden = 1;
Joe Perches0978e012011-04-04 10:06:25 -07006994 pr_notice("Console audio control in monitor mode, "
6995 "changes are not allowed\n");
6996 pr_notice("Use the volume_control=1 module parameter "
6997 "to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006998 }
6999 return -EPERM;
7000 }
7001
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007002 rc = volume_get_status(&s);
7003 if (rc < 0)
7004 return rc;
7005
7006 new_level = s & TP_EC_AUDIO_LVL_MSK;
7007 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007008
7009 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007010 if (!tp_features.mixer_no_level_control) {
7011 if (strlencmp(cmd, "up") == 0) {
7012 if (new_mute)
7013 new_mute = 0;
7014 else if (new_level < TP_EC_VOLUME_MAX)
7015 new_level++;
7016 continue;
7017 } else if (strlencmp(cmd, "down") == 0) {
7018 if (new_mute)
7019 new_mute = 0;
7020 else if (new_level > 0)
7021 new_level--;
7022 continue;
7023 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7024 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7025 new_level = l;
7026 continue;
7027 }
7028 }
7029 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007030 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007031 else if (strlencmp(cmd, "unmute") == 0)
7032 new_mute = 0;
7033 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007034 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007035 }
7036
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007037 if (tp_features.mixer_no_level_control) {
7038 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7039 new_mute ? "" : "un");
7040 rc = volume_set_mute(!!new_mute);
7041 } else {
7042 tpacpi_disclose_usertask("procfs volume",
7043 "%smute and set level to %d\n",
7044 new_mute ? "" : "un", new_level);
7045 rc = volume_set_status(new_mute | new_level);
7046 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007047 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007048
7049 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007050}
7051
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007052static struct ibm_struct volume_driver_data = {
7053 .name = "volume",
7054 .read = volume_read,
7055 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007056 .exit = volume_exit,
7057 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007058 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007059 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007060};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007061
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007062#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7063
7064#define alsa_card NULL
7065
7066static void inline volume_alsa_notify_change(void)
7067{
7068}
7069
7070static int __init volume_init(struct ibm_init_struct *iibm)
7071{
Joe Perches0978e012011-04-04 10:06:25 -07007072 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007073
7074 return 1;
7075}
7076
7077static struct ibm_struct volume_driver_data = {
7078 .name = "volume",
7079};
7080
7081#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7082
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007083/*************************************************************************
7084 * Fan subdriver
7085 */
7086
7087/*
7088 * FAN ACCESS MODES
7089 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007090 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007091 * ACPI GFAN method: returns fan level
7092 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007093 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007094 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007095 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007096 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007097 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7098 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007099 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7100 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007101 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007102 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007103 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7104 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007105 *
7106 * Fan speed changes of any sort (including those caused by the
7107 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007108 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007109 * limited.
7110 *
7111 * Reading is not available if GFAN exists.
7112 * Writing is not available if SFAN exists.
7113 *
7114 * Bits
7115 * 7 automatic mode engaged;
7116 * (default operation mode of the ThinkPad)
7117 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007118 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007119 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007120 * the tachometer while the fan controller ramps up
7121 * the speed (which can take up to a few *minutes*).
7122 * Speeds up fan to 100% duty-cycle, which is far above
7123 * the standard RPM levels. It is not impossible that
7124 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007125 * 5-3 unused in some models. Extra bits for fan level
7126 * in others, but still useless as all values above
7127 * 7 map to the same speed as level 7 in these models.
7128 * 2-0 fan level (0..7 usually)
7129 * 0x00 = stop
7130 * 0x07 = max (set when temperatures critical)
7131 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007132 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007133 *
7134 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007135 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007136 * does so, its initial value is meaningless (0x07).
7137 *
7138 * For firmware bugs, refer to:
7139 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7140 *
7141 * ----
7142 *
7143 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7144 * Main fan tachometer reading (in RPM)
7145 *
7146 * This register is present on all ThinkPads with a new-style EC, and
7147 * it is known not to be present on the A21m/e, and T22, as there is
7148 * something else in offset 0x84 according to the ACPI DSDT. Other
7149 * ThinkPads from this same time period (and earlier) probably lack the
7150 * tachometer as well.
7151 *
Nick Andrew877d0312009-01-26 11:06:57 +01007152 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007153 * was never fixed by IBM to report the EC firmware version string
7154 * probably support the tachometer (like the early X models), so
7155 * detecting it is quite hard. We need more data to know for sure.
7156 *
7157 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7158 * might result.
7159 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007160 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7161 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007162 *
7163 * For firmware bugs, refer to:
7164 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7165 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007166 * ----
7167 *
7168 * ThinkPad EC register 0x31 bit 0 (only on select models)
7169 *
7170 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7171 * show the speed of the main fan. When bit 0 of EC register 0x31
7172 * is one, the tachometer registers show the speed of the auxiliary
7173 * fan.
7174 *
7175 * Fan control seems to affect both fans, regardless of the state
7176 * of this bit.
7177 *
7178 * So far, only the firmware for the X60/X61 non-tablet versions
7179 * seem to support this (firmware TP-7M).
7180 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007181 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007182 * ThinkPad X31, X40, X41. Not available in the X60.
7183 *
7184 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7185 * high speed. ACPI DSDT seems to map these three speeds to levels
7186 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7187 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7188 *
7189 * The speeds are stored on handles
7190 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7191 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007192 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007193 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7194 *
7195 * ACPI DSDT switches which set is in use depending on various
7196 * factors.
7197 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007198 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007199 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7200 * but the ACPI tables just mention level 7.
7201 */
7202
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007203enum { /* Fan control constants */
7204 fan_status_offset = 0x2f, /* EC register 0x2f */
7205 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7206 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007207 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7208 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007209
7210 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7211 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7212
7213 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7214};
7215
7216enum fan_status_access_mode {
7217 TPACPI_FAN_NONE = 0, /* No fan status or control */
7218 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7219 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7220};
7221
7222enum fan_control_access_mode {
7223 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7224 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7225 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7226 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7227};
7228
7229enum fan_control_commands {
7230 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7231 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7232 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7233 * and also watchdog cmd */
7234};
7235
7236static int fan_control_allowed;
7237
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007238static enum fan_status_access_mode fan_status_access_mode;
7239static enum fan_control_access_mode fan_control_access_mode;
7240static enum fan_control_commands fan_control_commands;
7241
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007242static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007243static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007244static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007245static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007246
7247static struct mutex fan_mutex;
7248
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007249static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007250static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007251
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007252TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7253TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007254 "\\FSPD", /* 600e/x, 770e, 770x */
7255 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007256TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007257 "JFNS", /* 770x-JL */
7258 ); /* all others */
7259
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007260/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007261 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7262 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7263 * be in auto mode (0x80).
7264 *
7265 * This is corrected by any write to HFSP either by the driver, or
7266 * by the firmware.
7267 *
7268 * We assume 0x07 really means auto mode while this quirk is active,
7269 * as this is far more likely than the ThinkPad being in level 7,
7270 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007271 *
7272 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7273 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007274 */
7275
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007276static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007277{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007278 if (fan_control_initial_status == 0x07) {
Joe Perches0978e012011-04-04 10:06:25 -07007279 pr_notice("fan_init: initial fan status is unknown, "
7280 "assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007281 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007282 }
7283}
7284
7285static void fan_quirk1_handle(u8 *fan_status)
7286{
7287 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7288 if (*fan_status != fan_control_initial_status) {
7289 /* something changed the HFSP regisnter since
7290 * driver init time, so it is not undefined
7291 * anymore */
7292 tp_features.fan_ctrl_status_undef = 0;
7293 } else {
7294 /* Return most likely status. In fact, it
7295 * might be the only possible status */
7296 *fan_status = TP_EC_FAN_AUTO;
7297 }
7298 }
7299}
7300
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007301/* Select main fan on X60/X61, NOOP on others */
7302static bool fan_select_fan1(void)
7303{
7304 if (tp_features.second_fan) {
7305 u8 val;
7306
7307 if (ec_read(fan_select_offset, &val) < 0)
7308 return false;
7309 val &= 0xFEU;
7310 if (ec_write(fan_select_offset, val) < 0)
7311 return false;
7312 }
7313 return true;
7314}
7315
7316/* Select secondary fan on X60/X61 */
7317static bool fan_select_fan2(void)
7318{
7319 u8 val;
7320
7321 if (!tp_features.second_fan)
7322 return false;
7323
7324 if (ec_read(fan_select_offset, &val) < 0)
7325 return false;
7326 val |= 0x01U;
7327 if (ec_write(fan_select_offset, val) < 0)
7328 return false;
7329
7330 return true;
7331}
7332
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007333/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007334 * Call with fan_mutex held
7335 */
7336static void fan_update_desired_level(u8 status)
7337{
7338 if ((status &
7339 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7340 if (status > 7)
7341 fan_control_desired_level = 7;
7342 else
7343 fan_control_desired_level = status;
7344 }
7345}
7346
7347static int fan_get_status(u8 *status)
7348{
7349 u8 s;
7350
7351 /* TODO:
7352 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7353
7354 switch (fan_status_access_mode) {
7355 case TPACPI_FAN_RD_ACPI_GFAN:
7356 /* 570, 600e/x, 770e, 770x */
7357
7358 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7359 return -EIO;
7360
7361 if (likely(status))
7362 *status = s & 0x07;
7363
7364 break;
7365
7366 case TPACPI_FAN_RD_TPEC:
7367 /* all except 570, 600e/x, 770e, 770x */
7368 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7369 return -EIO;
7370
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007371 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007372 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007373 fan_quirk1_handle(status);
7374 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007375
7376 break;
7377
7378 default:
7379 return -ENXIO;
7380 }
7381
7382 return 0;
7383}
7384
7385static int fan_get_status_safe(u8 *status)
7386{
7387 int rc;
7388 u8 s;
7389
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007390 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007391 return -ERESTARTSYS;
7392 rc = fan_get_status(&s);
7393 if (!rc)
7394 fan_update_desired_level(s);
7395 mutex_unlock(&fan_mutex);
7396
7397 if (status)
7398 *status = s;
7399
7400 return rc;
7401}
7402
7403static int fan_get_speed(unsigned int *speed)
7404{
7405 u8 hi, lo;
7406
7407 switch (fan_status_access_mode) {
7408 case TPACPI_FAN_RD_TPEC:
7409 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007410 if (unlikely(!fan_select_fan1()))
7411 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007412 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7413 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7414 return -EIO;
7415
7416 if (likely(speed))
7417 *speed = (hi << 8) | lo;
7418
7419 break;
7420
7421 default:
7422 return -ENXIO;
7423 }
7424
7425 return 0;
7426}
7427
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007428static int fan2_get_speed(unsigned int *speed)
7429{
7430 u8 hi, lo;
7431 bool rc;
7432
7433 switch (fan_status_access_mode) {
7434 case TPACPI_FAN_RD_TPEC:
7435 /* all except 570, 600e/x, 770e, 770x */
7436 if (unlikely(!fan_select_fan2()))
7437 return -EIO;
7438 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7439 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7440 fan_select_fan1(); /* play it safe */
7441 if (rc)
7442 return -EIO;
7443
7444 if (likely(speed))
7445 *speed = (hi << 8) | lo;
7446
7447 break;
7448
7449 default:
7450 return -ENXIO;
7451 }
7452
7453 return 0;
7454}
7455
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007456static int fan_set_level(int level)
7457{
7458 if (!fan_control_allowed)
7459 return -EPERM;
7460
7461 switch (fan_control_access_mode) {
7462 case TPACPI_FAN_WR_ACPI_SFAN:
7463 if (level >= 0 && level <= 7) {
7464 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7465 return -EIO;
7466 } else
7467 return -EINVAL;
7468 break;
7469
7470 case TPACPI_FAN_WR_ACPI_FANS:
7471 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007472 if (!(level & TP_EC_FAN_AUTO) &&
7473 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007474 ((level < 0) || (level > 7)))
7475 return -EINVAL;
7476
7477 /* safety net should the EC not support AUTO
7478 * or FULLSPEED mode bits and just ignore them */
7479 if (level & TP_EC_FAN_FULLSPEED)
7480 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007481 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007482 level |= 4; /* safety min speed 4 */
7483
7484 if (!acpi_ec_write(fan_status_offset, level))
7485 return -EIO;
7486 else
7487 tp_features.fan_ctrl_status_undef = 0;
7488 break;
7489
7490 default:
7491 return -ENXIO;
7492 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007493
7494 vdbg_printk(TPACPI_DBG_FAN,
7495 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007496 return 0;
7497}
7498
7499static int fan_set_level_safe(int level)
7500{
7501 int rc;
7502
7503 if (!fan_control_allowed)
7504 return -EPERM;
7505
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007506 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007507 return -ERESTARTSYS;
7508
7509 if (level == TPACPI_FAN_LAST_LEVEL)
7510 level = fan_control_desired_level;
7511
7512 rc = fan_set_level(level);
7513 if (!rc)
7514 fan_update_desired_level(level);
7515
7516 mutex_unlock(&fan_mutex);
7517 return rc;
7518}
7519
7520static int fan_set_enable(void)
7521{
7522 u8 s;
7523 int rc;
7524
7525 if (!fan_control_allowed)
7526 return -EPERM;
7527
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007528 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007529 return -ERESTARTSYS;
7530
7531 switch (fan_control_access_mode) {
7532 case TPACPI_FAN_WR_ACPI_FANS:
7533 case TPACPI_FAN_WR_TPEC:
7534 rc = fan_get_status(&s);
7535 if (rc < 0)
7536 break;
7537
7538 /* Don't go out of emergency fan mode */
7539 if (s != 7) {
7540 s &= 0x07;
7541 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7542 }
7543
7544 if (!acpi_ec_write(fan_status_offset, s))
7545 rc = -EIO;
7546 else {
7547 tp_features.fan_ctrl_status_undef = 0;
7548 rc = 0;
7549 }
7550 break;
7551
7552 case TPACPI_FAN_WR_ACPI_SFAN:
7553 rc = fan_get_status(&s);
7554 if (rc < 0)
7555 break;
7556
7557 s &= 0x07;
7558
7559 /* Set fan to at least level 4 */
7560 s |= 4;
7561
7562 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007563 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007564 else
7565 rc = 0;
7566 break;
7567
7568 default:
7569 rc = -ENXIO;
7570 }
7571
7572 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007573
7574 if (!rc)
7575 vdbg_printk(TPACPI_DBG_FAN,
7576 "fan control: set fan control register to 0x%02x\n",
7577 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007578 return rc;
7579}
7580
7581static int fan_set_disable(void)
7582{
7583 int rc;
7584
7585 if (!fan_control_allowed)
7586 return -EPERM;
7587
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007588 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007589 return -ERESTARTSYS;
7590
7591 rc = 0;
7592 switch (fan_control_access_mode) {
7593 case TPACPI_FAN_WR_ACPI_FANS:
7594 case TPACPI_FAN_WR_TPEC:
7595 if (!acpi_ec_write(fan_status_offset, 0x00))
7596 rc = -EIO;
7597 else {
7598 fan_control_desired_level = 0;
7599 tp_features.fan_ctrl_status_undef = 0;
7600 }
7601 break;
7602
7603 case TPACPI_FAN_WR_ACPI_SFAN:
7604 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7605 rc = -EIO;
7606 else
7607 fan_control_desired_level = 0;
7608 break;
7609
7610 default:
7611 rc = -ENXIO;
7612 }
7613
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007614 if (!rc)
7615 vdbg_printk(TPACPI_DBG_FAN,
7616 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007617
7618 mutex_unlock(&fan_mutex);
7619 return rc;
7620}
7621
7622static int fan_set_speed(int speed)
7623{
7624 int rc;
7625
7626 if (!fan_control_allowed)
7627 return -EPERM;
7628
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007629 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007630 return -ERESTARTSYS;
7631
7632 rc = 0;
7633 switch (fan_control_access_mode) {
7634 case TPACPI_FAN_WR_ACPI_FANS:
7635 if (speed >= 0 && speed <= 65535) {
7636 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7637 speed, speed, speed))
7638 rc = -EIO;
7639 } else
7640 rc = -EINVAL;
7641 break;
7642
7643 default:
7644 rc = -ENXIO;
7645 }
7646
7647 mutex_unlock(&fan_mutex);
7648 return rc;
7649}
7650
7651static void fan_watchdog_reset(void)
7652{
7653 static int fan_watchdog_active;
7654
7655 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7656 return;
7657
7658 if (fan_watchdog_active)
7659 cancel_delayed_work(&fan_watchdog_task);
7660
7661 if (fan_watchdog_maxinterval > 0 &&
7662 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7663 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007664 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007665 msecs_to_jiffies(fan_watchdog_maxinterval
7666 * 1000))) {
Joe Perches0978e012011-04-04 10:06:25 -07007667 pr_err("failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007668 "watchdog will not trigger\n");
7669 }
7670 } else
7671 fan_watchdog_active = 0;
7672}
7673
7674static void fan_watchdog_fire(struct work_struct *ignored)
7675{
7676 int rc;
7677
7678 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7679 return;
7680
Joe Perches0978e012011-04-04 10:06:25 -07007681 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007682 rc = fan_set_enable();
7683 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007684 pr_err("fan watchdog: error %d while enabling fan, "
7685 "will try again later...\n", -rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007686 /* reschedule for later */
7687 fan_watchdog_reset();
7688 }
7689}
7690
7691/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007692 * SYSFS fan layout: hwmon compatible (device)
7693 *
7694 * pwm*_enable:
7695 * 0: "disengaged" mode
7696 * 1: manual mode
7697 * 2: native EC "auto" mode (recommended, hardware default)
7698 *
7699 * pwm*: set speed in manual mode, ignored otherwise.
7700 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7701 * interpolation.
7702 *
7703 * fan*_input: tachometer reading, RPM
7704 *
7705 *
7706 * SYSFS fan layout: extensions
7707 *
7708 * fan_watchdog (driver):
7709 * fan watchdog interval in seconds, 0 disables (default), max 120
7710 */
7711
7712/* sysfs fan pwm1_enable ----------------------------------------------- */
7713static ssize_t fan_pwm1_enable_show(struct device *dev,
7714 struct device_attribute *attr,
7715 char *buf)
7716{
7717 int res, mode;
7718 u8 status;
7719
7720 res = fan_get_status_safe(&status);
7721 if (res)
7722 return res;
7723
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007724 if (status & TP_EC_FAN_FULLSPEED) {
7725 mode = 0;
7726 } else if (status & TP_EC_FAN_AUTO) {
7727 mode = 2;
7728 } else
7729 mode = 1;
7730
7731 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7732}
7733
7734static ssize_t fan_pwm1_enable_store(struct device *dev,
7735 struct device_attribute *attr,
7736 const char *buf, size_t count)
7737{
7738 unsigned long t;
7739 int res, level;
7740
7741 if (parse_strtoul(buf, 2, &t))
7742 return -EINVAL;
7743
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007744 tpacpi_disclose_usertask("hwmon pwm1_enable",
7745 "set fan mode to %lu\n", t);
7746
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007747 switch (t) {
7748 case 0:
7749 level = TP_EC_FAN_FULLSPEED;
7750 break;
7751 case 1:
7752 level = TPACPI_FAN_LAST_LEVEL;
7753 break;
7754 case 2:
7755 level = TP_EC_FAN_AUTO;
7756 break;
7757 case 3:
7758 /* reserved for software-controlled auto mode */
7759 return -ENOSYS;
7760 default:
7761 return -EINVAL;
7762 }
7763
7764 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007765 if (res == -ENXIO)
7766 return -EINVAL;
7767 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007768 return res;
7769
7770 fan_watchdog_reset();
7771
7772 return count;
7773}
7774
7775static struct device_attribute dev_attr_fan_pwm1_enable =
7776 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7777 fan_pwm1_enable_show, fan_pwm1_enable_store);
7778
7779/* sysfs fan pwm1 ------------------------------------------------------ */
7780static ssize_t fan_pwm1_show(struct device *dev,
7781 struct device_attribute *attr,
7782 char *buf)
7783{
7784 int res;
7785 u8 status;
7786
7787 res = fan_get_status_safe(&status);
7788 if (res)
7789 return res;
7790
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007791 if ((status &
7792 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7793 status = fan_control_desired_level;
7794
7795 if (status > 7)
7796 status = 7;
7797
7798 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7799}
7800
7801static ssize_t fan_pwm1_store(struct device *dev,
7802 struct device_attribute *attr,
7803 const char *buf, size_t count)
7804{
7805 unsigned long s;
7806 int rc;
7807 u8 status, newlevel;
7808
7809 if (parse_strtoul(buf, 255, &s))
7810 return -EINVAL;
7811
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007812 tpacpi_disclose_usertask("hwmon pwm1",
7813 "set fan speed to %lu\n", s);
7814
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007815 /* scale down from 0-255 to 0-7 */
7816 newlevel = (s >> 5) & 0x07;
7817
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007818 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007819 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007820
7821 rc = fan_get_status(&status);
7822 if (!rc && (status &
7823 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7824 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007825 if (rc == -ENXIO)
7826 rc = -EINVAL;
7827 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007828 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007829 fan_watchdog_reset();
7830 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007831 }
7832
7833 mutex_unlock(&fan_mutex);
7834 return (rc)? rc : count;
7835}
7836
7837static struct device_attribute dev_attr_fan_pwm1 =
7838 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7839 fan_pwm1_show, fan_pwm1_store);
7840
7841/* sysfs fan fan1_input ------------------------------------------------ */
7842static ssize_t fan_fan1_input_show(struct device *dev,
7843 struct device_attribute *attr,
7844 char *buf)
7845{
7846 int res;
7847 unsigned int speed;
7848
7849 res = fan_get_speed(&speed);
7850 if (res < 0)
7851 return res;
7852
7853 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7854}
7855
7856static struct device_attribute dev_attr_fan_fan1_input =
7857 __ATTR(fan1_input, S_IRUGO,
7858 fan_fan1_input_show, NULL);
7859
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007860/* sysfs fan fan2_input ------------------------------------------------ */
7861static ssize_t fan_fan2_input_show(struct device *dev,
7862 struct device_attribute *attr,
7863 char *buf)
7864{
7865 int res;
7866 unsigned int speed;
7867
7868 res = fan2_get_speed(&speed);
7869 if (res < 0)
7870 return res;
7871
7872 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7873}
7874
7875static struct device_attribute dev_attr_fan_fan2_input =
7876 __ATTR(fan2_input, S_IRUGO,
7877 fan_fan2_input_show, NULL);
7878
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007879/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007880static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7881 char *buf)
7882{
7883 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7884}
7885
7886static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7887 const char *buf, size_t count)
7888{
7889 unsigned long t;
7890
7891 if (parse_strtoul(buf, 120, &t))
7892 return -EINVAL;
7893
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007894 if (!fan_control_allowed)
7895 return -EPERM;
7896
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007897 fan_watchdog_maxinterval = t;
7898 fan_watchdog_reset();
7899
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007900 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7901
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007902 return count;
7903}
7904
7905static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7906 fan_fan_watchdog_show, fan_fan_watchdog_store);
7907
7908/* --------------------------------------------------------------------- */
7909static struct attribute *fan_attributes[] = {
7910 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7911 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007912 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007913 NULL
7914};
7915
7916static const struct attribute_group fan_attr_group = {
7917 .attrs = fan_attributes,
7918};
7919
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007920#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7921#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007922
7923#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7924 { .vendor = PCI_VENDOR_ID_IBM, \
7925 .bios = TPACPI_MATCH_ANY, \
7926 .ec = TPID(__id1, __id2), \
7927 .quirks = __quirks }
7928
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007929#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7930 { .vendor = PCI_VENDOR_ID_LENOVO, \
7931 .bios = TPACPI_MATCH_ANY, \
7932 .ec = TPID(__id1, __id2), \
7933 .quirks = __quirks }
7934
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007935static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7936 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7937 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7938 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7939 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007940 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007941};
7942
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007943#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007944#undef TPACPI_FAN_QI
7945
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007946static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007947{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007948 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007949 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007950
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007951 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7952 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007953
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007954 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007955 fan_status_access_mode = TPACPI_FAN_NONE;
7956 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007957 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007958 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007959 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007960 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007961 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007962
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03007963 if (tpacpi_is_ibm()) {
7964 TPACPI_ACPIHANDLE_INIT(fans);
7965 TPACPI_ACPIHANDLE_INIT(gfan);
7966 TPACPI_ACPIHANDLE_INIT(sfan);
7967 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007968
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007969 quirks = tpacpi_check_quirks(fan_quirk_table,
7970 ARRAY_SIZE(fan_quirk_table));
7971
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007972 if (gfan_handle) {
7973 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007974 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007975 } else {
7976 /* all other ThinkPads: note that even old-style
7977 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007978 if (likely(acpi_ec_read(fan_status_offset,
7979 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007980 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007981 if (quirks & TPACPI_FAN_Q1)
7982 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007983 if (quirks & TPACPI_FAN_2FAN) {
7984 tp_features.second_fan = 1;
7985 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7986 "secondary fan support enabled\n");
7987 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007988 } else {
Joe Perches0978e012011-04-04 10:06:25 -07007989 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007990 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007991 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007992 }
7993 }
7994
7995 if (sfan_handle) {
7996 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007997 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007998 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007999 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008000 } else {
8001 if (!gfan_handle) {
8002 /* gfan without sfan means no fan control */
8003 /* all other models implement TP EC 0x2f control */
8004
8005 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008006 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008007 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008008 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008009 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008010 TPACPI_FAN_CMD_SPEED |
8011 TPACPI_FAN_CMD_LEVEL |
8012 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008013 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008014 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008015 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008016 TPACPI_FAN_CMD_LEVEL |
8017 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008018 }
8019 }
8020 }
8021
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008022 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8023 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008024 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8025 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8026 fan_status_access_mode, fan_control_access_mode);
8027
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008028 /* fan control master switch */
8029 if (!fan_control_allowed) {
8030 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8031 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008032 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008033 "fan control features disabled by parameter\n");
8034 }
8035
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008036 /* update fan_control_desired_level */
8037 if (fan_status_access_mode != TPACPI_FAN_NONE)
8038 fan_get_status_safe(NULL);
8039
8040 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8041 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008042 if (tp_features.second_fan) {
8043 /* attach second fan tachometer */
8044 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8045 &dev_attr_fan_fan2_input.attr;
8046 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008047 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008048 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008049 if (rc < 0)
8050 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008051
8052 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8053 &driver_attr_fan_watchdog);
8054 if (rc < 0) {
8055 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8056 &fan_attr_group);
8057 return rc;
8058 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008059 return 0;
8060 } else
8061 return 1;
8062}
8063
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008064static void fan_exit(void)
8065{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008066 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008067 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008068
8069 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008070 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008071 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8072 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008073
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008074 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008075 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008076}
8077
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008078static void fan_suspend(pm_message_t state)
8079{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008080 int rc;
8081
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008082 if (!fan_control_allowed)
8083 return;
8084
8085 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008086 fan_control_resume_level = 0;
8087 rc = fan_get_status_safe(&fan_control_resume_level);
8088 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008089 pr_notice("failed to read fan level for later "
8090 "restore during resume: %d\n", rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008091
8092 /* if it is undefined, don't attempt to restore it.
8093 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008094 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008095 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008096}
8097
8098static void fan_resume(void)
8099{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008100 u8 current_level = 7;
8101 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008102 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008103
8104 /* DSDT *always* updates status on resume */
8105 tp_features.fan_ctrl_status_undef = 0;
8106
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008107 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008108 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008109 (fan_get_status_safe(&current_level) < 0))
8110 return;
8111
8112 switch (fan_control_access_mode) {
8113 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008114 /* never decrease fan level */
8115 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008116 break;
8117 case TPACPI_FAN_WR_ACPI_FANS:
8118 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008119 /* never decrease fan level, scale is:
8120 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8121 *
8122 * We expect the firmware to set either 7 or AUTO, but we
8123 * handle FULLSPEED out of paranoia.
8124 *
8125 * So, we can safely only restore FULLSPEED or 7, anything
8126 * else could slow the fan. Restoring AUTO is useless, at
8127 * best that's exactly what the DSDT already set (it is the
8128 * slower it uses).
8129 *
8130 * Always keep in mind that the DSDT *will* have set the
8131 * fans to what the vendor supposes is the best level. We
8132 * muck with it only to speed the fan up.
8133 */
8134 if (fan_control_resume_level != 7 &&
8135 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8136 return;
8137 else
8138 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8139 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008140 break;
8141 default:
8142 return;
8143 }
8144 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008145 pr_notice("restoring fan level to 0x%02x\n",
8146 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008147 rc = fan_set_level_safe(fan_control_resume_level);
8148 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008149 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008150 }
8151}
8152
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008153static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008154{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008155 int rc;
8156 u8 status;
8157 unsigned int speed = 0;
8158
8159 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008160 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008161 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008162 rc = fan_get_status_safe(&status);
8163 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008164 return rc;
8165
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008166 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008167 "level:\t\t%d\n",
8168 (status != 0) ? "enabled" : "disabled", status);
8169 break;
8170
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008171 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008172 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008173 rc = fan_get_status_safe(&status);
8174 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008175 return rc;
8176
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008177 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008178 (status != 0) ? "enabled" : "disabled");
8179
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008180 rc = fan_get_speed(&speed);
8181 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008182 return rc;
8183
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008184 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008185
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008186 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008187 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008188 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008189 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008190 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008191 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008192 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008193 break;
8194
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008195 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008196 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008197 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008198 }
8199
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008200 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008201 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008202
8203 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008204 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008205 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008206 break;
8207
8208 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008209 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008210 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008211 break;
8212 }
8213 }
8214
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008215 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008216 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008217 "commands:\twatchdog <timeout> (<timeout> "
8218 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008219
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008220 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008221 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008222 " (<speed> is 0-65535)\n");
8223
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008224 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008225}
8226
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008227static int fan_write_cmd_level(const char *cmd, int *rc)
8228{
8229 int level;
8230
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008231 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008232 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008233 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008234 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008235 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008236 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008237 return 0;
8238
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008239 *rc = fan_set_level_safe(level);
8240 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008241 pr_err("level command accepted for unsupported access mode %d\n",
8242 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008243 else if (!*rc)
8244 tpacpi_disclose_usertask("procfs fan",
8245 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008246
8247 return 1;
8248}
8249
8250static int fan_write_cmd_enable(const char *cmd, int *rc)
8251{
8252 if (strlencmp(cmd, "enable") != 0)
8253 return 0;
8254
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008255 *rc = fan_set_enable();
8256 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008257 pr_err("enable command accepted for unsupported access mode %d\n",
8258 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008259 else if (!*rc)
8260 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008261
8262 return 1;
8263}
8264
8265static int fan_write_cmd_disable(const char *cmd, int *rc)
8266{
8267 if (strlencmp(cmd, "disable") != 0)
8268 return 0;
8269
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008270 *rc = fan_set_disable();
8271 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008272 pr_err("disable command accepted for unsupported access mode %d\n",
8273 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008274 else if (!*rc)
8275 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008276
8277 return 1;
8278}
8279
8280static int fan_write_cmd_speed(const char *cmd, int *rc)
8281{
8282 int speed;
8283
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008284 /* TODO:
8285 * Support speed <low> <medium> <high> ? */
8286
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008287 if (sscanf(cmd, "speed %d", &speed) != 1)
8288 return 0;
8289
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008290 *rc = fan_set_speed(speed);
8291 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008292 pr_err("speed command accepted for unsupported access mode %d\n",
8293 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008294 else if (!*rc)
8295 tpacpi_disclose_usertask("procfs fan",
8296 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008297
8298 return 1;
8299}
8300
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008301static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8302{
8303 int interval;
8304
8305 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8306 return 0;
8307
8308 if (interval < 0 || interval > 120)
8309 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008310 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008311 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008312 tpacpi_disclose_usertask("procfs fan",
8313 "set watchdog timer to %d\n",
8314 interval);
8315 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008316
8317 return 1;
8318}
8319
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008320static int fan_write(char *buf)
8321{
8322 char *cmd;
8323 int rc = 0;
8324
8325 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008326 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008327 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008328 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008329 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008330 fan_write_cmd_disable(cmd, &rc) ||
8331 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008332 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008333 fan_write_cmd_speed(cmd, &rc))
8334 )
8335 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008336 else if (!rc)
8337 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008338 }
8339
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008340 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008341}
8342
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008343static struct ibm_struct fan_driver_data = {
8344 .name = "fan",
8345 .read = fan_read,
8346 .write = fan_write,
8347 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008348 .suspend = fan_suspend,
8349 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008350};
8351
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008352/****************************************************************************
8353 ****************************************************************************
8354 *
8355 * Infrastructure
8356 *
8357 ****************************************************************************
8358 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008359
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008360/*
8361 * HKEY event callout for other subdrivers go here
8362 * (yes, it is ugly, but it is quick, safe, and gets the job done
8363 */
8364static void tpacpi_driver_event(const unsigned int hkey_event)
8365{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008366 if (ibm_backlight_device) {
8367 switch (hkey_event) {
8368 case TP_HKEY_EV_BRGHT_UP:
8369 case TP_HKEY_EV_BRGHT_DOWN:
8370 tpacpi_brightness_notify_change();
8371 }
8372 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008373 if (alsa_card) {
8374 switch (hkey_event) {
8375 case TP_HKEY_EV_VOL_UP:
8376 case TP_HKEY_EV_VOL_DOWN:
8377 case TP_HKEY_EV_VOL_MUTE:
8378 volume_alsa_notify_change();
8379 }
8380 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008381}
8382
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008383static void hotkey_driver_event(const unsigned int scancode)
8384{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008385 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008386}
8387
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008388/* sysfs name ---------------------------------------------------------- */
8389static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8390 struct device_attribute *attr,
8391 char *buf)
8392{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008393 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008394}
8395
8396static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8397 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8398
8399/* --------------------------------------------------------------------- */
8400
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008401/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03008402static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008403
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008404/*
8405 * Module and infrastructure proble, init and exit handling
8406 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008408static int force_load;
8409
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008410#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008411static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008412{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008413 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008414
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008415 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008416}
8417#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8418
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008419static void ibm_exit(struct ibm_struct *ibm)
8420{
8421 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8422
8423 list_del_init(&ibm->all_drivers);
8424
8425 if (ibm->flags.acpi_notify_installed) {
8426 dbg_printk(TPACPI_DBG_EXIT,
8427 "%s: acpi_remove_notify_handler\n", ibm->name);
8428 BUG_ON(!ibm->acpi);
8429 acpi_remove_notify_handler(*ibm->acpi->handle,
8430 ibm->acpi->type,
8431 dispatch_acpi_notify);
8432 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008433 }
8434
8435 if (ibm->flags.proc_created) {
8436 dbg_printk(TPACPI_DBG_EXIT,
8437 "%s: remove_proc_entry\n", ibm->name);
8438 remove_proc_entry(ibm->name, proc_dir);
8439 ibm->flags.proc_created = 0;
8440 }
8441
8442 if (ibm->flags.acpi_driver_registered) {
8443 dbg_printk(TPACPI_DBG_EXIT,
8444 "%s: acpi_bus_unregister_driver\n", ibm->name);
8445 BUG_ON(!ibm->acpi);
8446 acpi_bus_unregister_driver(ibm->acpi->driver);
8447 kfree(ibm->acpi->driver);
8448 ibm->acpi->driver = NULL;
8449 ibm->flags.acpi_driver_registered = 0;
8450 }
8451
8452 if (ibm->flags.init_called && ibm->exit) {
8453 ibm->exit();
8454 ibm->flags.init_called = 0;
8455 }
8456
8457 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8458}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008459
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008460static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008461{
8462 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008463 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008464 struct proc_dir_entry *entry;
8465
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008466 BUG_ON(ibm == NULL);
8467
8468 INIT_LIST_HEAD(&ibm->all_drivers);
8469
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008470 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471 return 0;
8472
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008473 dbg_printk(TPACPI_DBG_INIT,
8474 "probing for %s\n", ibm->name);
8475
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008476 if (iibm->init) {
8477 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008478 if (ret > 0)
8479 return 0; /* probe failed */
8480 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008482
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008483 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008484 }
8485
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008486 if (ibm->acpi) {
8487 if (ibm->acpi->hid) {
8488 ret = register_tpacpi_subdriver(ibm);
8489 if (ret)
8490 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008491 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008492
8493 if (ibm->acpi->notify) {
8494 ret = setup_acpi_notify(ibm);
8495 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07008496 pr_notice("disabling subdriver %s\n",
8497 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008498 ret = 0;
8499 goto err_out;
8500 }
8501 if (ret < 0)
8502 goto err_out;
8503 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008504 }
8505
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008506 dbg_printk(TPACPI_DBG_INIT,
8507 "%s installed\n", ibm->name);
8508
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008509 if (ibm->read) {
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008510 mode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008511
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008512 if (!mode)
8513 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008514 if (ibm->write)
8515 mode |= S_IWUSR;
8516 entry = proc_create_data(ibm->name, mode, proc_dir,
8517 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008518 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07008519 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008520 ret = -ENODEV;
8521 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008522 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008523 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008525
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008526 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8527
Linus Torvalds1da177e2005-04-16 15:20:36 -07008528 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008529
8530err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008531 dbg_printk(TPACPI_DBG_INIT,
8532 "%s: at error exit path with result %d\n",
8533 ibm->name, ret);
8534
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008535 ibm_exit(ibm);
8536 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537}
8538
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008539/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008541static bool __pure __init tpacpi_is_fw_digit(const char c)
8542{
8543 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8544}
8545
8546/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8547static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8548 const char t)
8549{
8550 return s && strlen(s) >= 8 &&
8551 tpacpi_is_fw_digit(s[0]) &&
8552 tpacpi_is_fw_digit(s[1]) &&
8553 s[2] == t && s[3] == 'T' &&
8554 tpacpi_is_fw_digit(s[4]) &&
Keith Packardb569ab32011-03-31 15:22:33 -07008555 tpacpi_is_fw_digit(s[5]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008556}
8557
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008558/* returns 0 - probe ok, or < 0 - probe error.
8559 * Probe ok doesn't mean thinkpad found.
8560 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8561static int __must_check __init get_thinkpad_model_data(
8562 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008563{
Jeff Garzik18552562007-10-03 15:15:40 -04008564 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008565 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008566 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008567
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008568 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008569 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008570
8571 memset(tp, 0, sizeof(*tp));
8572
8573 if (dmi_name_in_vendors("IBM"))
8574 tp->vendor = PCI_VENDOR_ID_IBM;
8575 else if (dmi_name_in_vendors("LENOVO"))
8576 tp->vendor = PCI_VENDOR_ID_LENOVO;
8577 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008578 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008579
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008580 s = dmi_get_system_info(DMI_BIOS_VERSION);
8581 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8582 if (s && !tp->bios_version_str)
8583 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008584
8585 /* Really ancient ThinkPad 240X will fail this, which is fine */
8586 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008587 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008588
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008589 tp->bios_model = tp->bios_version_str[0]
8590 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008591 tp->bios_release = (tp->bios_version_str[4] << 8)
8592 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008593
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008594 /*
8595 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8596 * X32 or newer, all Z series; Some models must have an
8597 * up-to-date BIOS or they will not be detected.
8598 *
8599 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8600 */
8601 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008602 if (sscanf(dev->name,
8603 "IBM ThinkPad Embedded Controller -[%17c",
8604 ec_fw_string) == 1) {
8605 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8606 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008607
8608 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008609 if (!tp->ec_version_str)
8610 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008611
8612 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8613 tp->ec_model = ec_fw_string[0]
8614 | (ec_fw_string[1] << 8);
8615 tp->ec_release = (ec_fw_string[4] << 8)
8616 | ec_fw_string[5];
8617 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008618 pr_notice("ThinkPad firmware release %s "
8619 "doesn't match the known patterns\n",
8620 ec_fw_string);
8621 pr_notice("please report this to %s\n",
8622 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008623 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008624 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008625 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008626 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008627
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008628 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8629 if (s && !strnicmp(s, "ThinkPad", 8)) {
8630 tp->model_str = kstrdup(s, GFP_KERNEL);
8631 if (!tp->model_str)
8632 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008633 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008634
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008635 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8636 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8637 if (s && !tp->nummodel_str)
8638 return -ENOMEM;
8639
8640 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641}
8642
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008643static int __init probe_for_thinkpad(void)
8644{
8645 int is_thinkpad;
8646
8647 if (acpi_disabled)
8648 return -ENODEV;
8649
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008650 /* It would be dangerous to run the driver in this case */
8651 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8652 return -ENODEV;
8653
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008654 /*
8655 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008656 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008657 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008658 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8659 (thinkpad_id.ec_model != 0) ||
8660 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008661
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03008662 /* The EC handler is required */
8663 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008664 if (!ec_handle) {
8665 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07008666 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008667 return -ENODEV;
8668 }
8669
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008670 if (!is_thinkpad && !force_load)
8671 return -ENODEV;
8672
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008673 return 0;
8674}
8675
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008676static void __init thinkpad_acpi_init_banner(void)
8677{
Joe Perches0978e012011-04-04 10:06:25 -07008678 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8679 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008680
Joe Perches0978e012011-04-04 10:06:25 -07008681 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008682 (thinkpad_id.bios_version_str) ?
8683 thinkpad_id.bios_version_str : "unknown",
8684 (thinkpad_id.ec_version_str) ?
8685 thinkpad_id.ec_version_str : "unknown");
8686
8687 BUG_ON(!thinkpad_id.vendor);
8688
8689 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07008690 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008691 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8692 "IBM" : ((thinkpad_id.vendor ==
8693 PCI_VENDOR_ID_LENOVO) ?
8694 "Lenovo" : "Unknown vendor"),
8695 thinkpad_id.model_str,
8696 (thinkpad_id.nummodel_str) ?
8697 thinkpad_id.nummodel_str : "unknown");
8698}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008699
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008700/* Module init, exit, parameters */
8701
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008702static struct ibm_init_struct ibms_init[] __initdata = {
8703 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008704 .data = &thinkpad_acpi_driver_data,
8705 },
8706 {
8707 .init = hotkey_init,
8708 .data = &hotkey_driver_data,
8709 },
8710 {
8711 .init = bluetooth_init,
8712 .data = &bluetooth_driver_data,
8713 },
8714 {
8715 .init = wan_init,
8716 .data = &wan_driver_data,
8717 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008718 {
8719 .init = uwb_init,
8720 .data = &uwb_driver_data,
8721 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008722#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008723 {
8724 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008725 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008726 .data = &video_driver_data,
8727 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008728#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008729 {
8730 .init = light_init,
8731 .data = &light_driver_data,
8732 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008733 {
8734 .init = cmos_init,
8735 .data = &cmos_driver_data,
8736 },
8737 {
8738 .init = led_init,
8739 .data = &led_driver_data,
8740 },
8741 {
8742 .init = beep_init,
8743 .data = &beep_driver_data,
8744 },
8745 {
8746 .init = thermal_init,
8747 .data = &thermal_driver_data,
8748 },
8749 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008750 .init = brightness_init,
8751 .data = &brightness_driver_data,
8752 },
8753 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008754 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008755 .data = &volume_driver_data,
8756 },
8757 {
8758 .init = fan_init,
8759 .data = &fan_driver_data,
8760 },
8761};
8762
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008763static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8764{
8765 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008766 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008767
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008768 if (!kp || !kp->name || !val)
8769 return -EINVAL;
8770
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008771 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8772 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008773 WARN_ON(ibm == NULL);
8774
8775 if (!ibm || !ibm->name)
8776 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008777
8778 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8779 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008780 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008781 strcpy(ibms_init[i].param, val);
8782 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008783 return 0;
8784 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008785 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008786
8787 return -EINVAL;
8788}
8789
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008790module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008791MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008792 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008793
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008794module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008795MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008796
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008797module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008798MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008799 "Attempts to load the driver even on a "
8800 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008801
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008802module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008803MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008804 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008805
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008806module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008807MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008808 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008809 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008810
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008811module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008812MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008813 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008814
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008815module_param(hotkey_report_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008816MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008817 "used for backwards compatibility with userspace, "
8818 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008819
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008820#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008821module_param_named(volume_mode, volume_mode, uint, 0444);
8822MODULE_PARM_DESC(volume_mode,
8823 "Selects volume control strategy: "
8824 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8825
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008826module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8827MODULE_PARM_DESC(volume_capabilities,
8828 "Selects the mixer capabilites: "
8829 "0=auto, 1=volume and mute, 2=mute only");
8830
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008831module_param_named(volume_control, volume_control_allowed, bool, 0444);
8832MODULE_PARM_DESC(volume_control,
8833 "Enables software override for the console audio "
8834 "control when true");
8835
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008836/* ALSA module API parameters */
8837module_param_named(index, alsa_index, int, 0444);
8838MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8839module_param_named(id, alsa_id, charp, 0444);
8840MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8841module_param_named(enable, alsa_enable, bool, 0444);
8842MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008843#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008844
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008845#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008846 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008847 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008848 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008849
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008850TPACPI_PARAM(hotkey);
8851TPACPI_PARAM(bluetooth);
8852TPACPI_PARAM(video);
8853TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008854TPACPI_PARAM(cmos);
8855TPACPI_PARAM(led);
8856TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008857TPACPI_PARAM(brightness);
8858TPACPI_PARAM(volume);
8859TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008860
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008861#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008862module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008863MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8864module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8865MODULE_PARM_DESC(wlsw_state,
8866 "Initial state of the emulated WLSW switch");
8867
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008868module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008869MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8870module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8871MODULE_PARM_DESC(bluetooth_state,
8872 "Initial state of the emulated bluetooth switch");
8873
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008874module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008875MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8876module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8877MODULE_PARM_DESC(wwan_state,
8878 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008879
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008880module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008881MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8882module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8883MODULE_PARM_DESC(uwb_state,
8884 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008885#endif
8886
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008887static void thinkpad_acpi_module_exit(void)
8888{
8889 struct ibm_struct *ibm, *itmp;
8890
8891 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8892
8893 list_for_each_entry_safe_reverse(ibm, itmp,
8894 &tpacpi_all_drivers,
8895 all_drivers) {
8896 ibm_exit(ibm);
8897 }
8898
8899 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8900
8901 if (tpacpi_inputdev) {
8902 if (tp_features.input_device_registered)
8903 input_unregister_device(tpacpi_inputdev);
8904 else
8905 input_free_device(tpacpi_inputdev);
8906 }
8907
8908 if (tpacpi_hwmon)
8909 hwmon_device_unregister(tpacpi_hwmon);
8910
8911 if (tp_features.sensors_pdev_attrs_registered)
8912 device_remove_file(&tpacpi_sensors_pdev->dev,
8913 &dev_attr_thinkpad_acpi_pdev_name);
8914 if (tpacpi_sensors_pdev)
8915 platform_device_unregister(tpacpi_sensors_pdev);
8916 if (tpacpi_pdev)
8917 platform_device_unregister(tpacpi_pdev);
8918
8919 if (tp_features.sensors_pdrv_attrs_registered)
8920 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8921 if (tp_features.platform_drv_attrs_registered)
8922 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8923
8924 if (tp_features.sensors_pdrv_registered)
8925 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8926
8927 if (tp_features.platform_drv_registered)
8928 platform_driver_unregister(&tpacpi_pdriver);
8929
8930 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008931 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008932
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008933 if (tpacpi_wq)
8934 destroy_workqueue(tpacpi_wq);
8935
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008936 kfree(thinkpad_id.bios_version_str);
8937 kfree(thinkpad_id.ec_version_str);
8938 kfree(thinkpad_id.model_str);
8939}
8940
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008941
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008942static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943{
8944 int ret, i;
8945
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008946 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8947
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008948 /* Parameter checking */
8949 if (hotkey_report_mode > 2)
8950 return -EINVAL;
8951
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008952 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008953
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008954 ret = get_thinkpad_model_data(&thinkpad_id);
8955 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07008956 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008957 thinkpad_acpi_module_exit();
8958 return ret;
8959 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008960 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008961 if (ret) {
8962 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008963 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008965
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008966 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008967
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008968 thinkpad_acpi_init_banner();
8969 tpacpi_check_outdated_fw();
8970
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008971 TPACPI_ACPIHANDLE_INIT(ecrd);
8972 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008973
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008974 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8975 if (!tpacpi_wq) {
8976 thinkpad_acpi_module_exit();
8977 return -ENOMEM;
8978 }
8979
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008980 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07008982 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008983 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008984 return -ENODEV;
8985 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008986
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008987 ret = platform_driver_register(&tpacpi_pdriver);
8988 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07008989 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008990 thinkpad_acpi_module_exit();
8991 return ret;
8992 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008993 tp_features.platform_drv_registered = 1;
8994
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008995 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8996 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07008997 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008998 thinkpad_acpi_module_exit();
8999 return ret;
9000 }
9001 tp_features.sensors_pdrv_registered = 1;
9002
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009003 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009004 if (!ret) {
9005 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009006 ret = tpacpi_create_driver_attributes(
9007 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009008 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009009 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009010 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009011 thinkpad_acpi_module_exit();
9012 return ret;
9013 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009014 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009015
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009016
9017 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009018 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009019 NULL, 0);
9020 if (IS_ERR(tpacpi_pdev)) {
9021 ret = PTR_ERR(tpacpi_pdev);
9022 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009023 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009024 thinkpad_acpi_module_exit();
9025 return ret;
9026 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009027 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009028 TPACPI_HWMON_DRVR_NAME,
9029 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009030 if (IS_ERR(tpacpi_sensors_pdev)) {
9031 ret = PTR_ERR(tpacpi_sensors_pdev);
9032 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009033 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009034 thinkpad_acpi_module_exit();
9035 return ret;
9036 }
9037 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9038 &dev_attr_thinkpad_acpi_pdev_name);
9039 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009040 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009041 thinkpad_acpi_module_exit();
9042 return ret;
9043 }
9044 tp_features.sensors_pdev_attrs_registered = 1;
9045 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009046 if (IS_ERR(tpacpi_hwmon)) {
9047 ret = PTR_ERR(tpacpi_hwmon);
9048 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009049 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009050 thinkpad_acpi_module_exit();
9051 return ret;
9052 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009053 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009054 tpacpi_inputdev = input_allocate_device();
9055 if (!tpacpi_inputdev) {
Joe Perches0978e012011-04-04 10:06:25 -07009056 pr_err("unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009057 thinkpad_acpi_module_exit();
9058 return -ENOMEM;
9059 } else {
9060 /* Prepare input device, but don't register */
9061 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009062 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009063 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009064 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009065 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9066 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009067 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009068 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009069
9070 /* Init subdriver dependencies */
9071 tpacpi_detect_brightness_capabilities();
9072
9073 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009074 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9075 ret = ibm_init(&ibms_init[i]);
9076 if (ret >= 0 && *ibms_init[i].param)
9077 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009078 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009079 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009080 return ret;
9081 }
9082 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009083
9084 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9085
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009086 ret = input_register_device(tpacpi_inputdev);
9087 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009088 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009089 thinkpad_acpi_module_exit();
9090 return ret;
9091 } else {
9092 tp_features.input_device_registered = 1;
9093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009094
9095 return 0;
9096}
9097
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009098MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9099
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009100/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009101 * This will autoload the driver in almost every ThinkPad
9102 * in widespread use.
9103 *
9104 * Only _VERY_ old models, like the 240, 240x and 570 lack
9105 * the HKEY event interface.
9106 */
9107MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9108
9109/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009110 * DMI matching for module autoloading
9111 *
9112 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9113 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9114 *
9115 * Only models listed in thinkwiki will be supported, so add yours
9116 * if it is not there yet.
9117 */
9118#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009119 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009120
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009121/* Ancient thinkpad BIOSes have to be identified by
9122 * BIOS type or model number, and there are far less
9123 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009124IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009125
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009126MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9127MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009128MODULE_DESCRIPTION(TPACPI_DESC);
9129MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009130MODULE_LICENSE("GPL");
9131
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009132module_init(thinkpad_acpi_module_init);
9133module_exit(thinkpad_acpi_module_exit);