blob: 18c9862dd932f8114fd0e735fa5de449a41b0f97 [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
David Henningsson420f9732013-10-16 23:10:31 +020026#define TPACPI_VERSION "0.25"
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#include <linux/nvram.h>
65#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020066#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020067#include <linux/sysfs.h>
68#include <linux/backlight.h>
69#include <linux/fb.h>
70#include <linux/platform_device.h>
71#include <linux/hwmon.h>
72#include <linux/hwmon-sysfs.h>
73#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030074#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030075#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020076#include <linux/dmi.h>
77#include <linux/jiffies.h>
78#include <linux/workqueue.h>
Lv Zheng8b484632013-12-03 08:49:16 +080079#include <linux/acpi.h>
80#include <linux/pci_ids.h>
81#include <linux/thinkpad_acpi.h>
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020082#include <sound/core.h>
83#include <sound/control.h>
84#include <sound/initval.h>
Lv Zheng8b484632013-12-03 08:49:16 +080085#include <asm/uaccess.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020086
87/* ThinkPad CMOS commands */
88#define TP_CMOS_VOLUME_DOWN 0
89#define TP_CMOS_VOLUME_UP 1
90#define TP_CMOS_VOLUME_MUTE 2
91#define TP_CMOS_BRIGHTNESS_UP 4
92#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030093#define TP_CMOS_THINKLIGHT_ON 12
94#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020095
96/* NVRAM Addresses */
97enum tp_nvram_addr {
98 TP_NVRAM_ADDR_HK2 = 0x57,
99 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
100 TP_NVRAM_ADDR_VIDEO = 0x59,
101 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
102 TP_NVRAM_ADDR_MIXER = 0x60,
103};
104
105/* NVRAM bit masks */
106enum {
107 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
108 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
109 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
110 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
111 TP_NVRAM_MASK_THINKLIGHT = 0x10,
112 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
113 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
114 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
115 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
116 TP_NVRAM_MASK_MUTE = 0x40,
117 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
118 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
119 TP_NVRAM_POS_LEVEL_VOLUME = 0,
120};
121
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300122/* Misc NVRAM-related */
123enum {
124 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
125};
126
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200127/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400128#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
129#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300130#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200131
132/* Input IDs */
133#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
134#define TPACPI_HKEY_INPUT_VERSION 0x4101
135
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200136/* ACPI \WGSV commands */
137enum {
138 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
139 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
140 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
141 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
142};
143
144/* TP_ACPI_WGSV_GET_STATE bits */
145enum {
146 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
147 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
148 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
149 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
150 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
151 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
152 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
153 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
154 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
155 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
156};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200157
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300158/* HKEY events */
159enum tpacpi_hkey_event_t {
160 /* Hotkey-related */
161 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
162 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
163 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
164 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
165 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
166 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
167
168 /* Reasons for waking up from S3/S4 */
169 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
170 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
171 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
172 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
173 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
174 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
175
176 /* Auto-sleep after eject request */
177 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
178 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
179
180 /* Misc bay events */
181 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -0300182 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
183 or port replicator */
184 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
185 dock or port replicator */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300186
187 /* User-interface events */
188 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
189 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
190 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
191 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
192 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
193 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
194 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
195
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300196 /* Key-related user-interface events */
197 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
198 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
199
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300200 /* Thermal events */
201 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
202 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
203 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
204 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
205 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
206
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100207 /* AC-related events */
208 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300209
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300210 /* Misc */
211 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
212};
213
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200214/****************************************************************************
215 * Main driver
216 */
217
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200218#define TPACPI_NAME "thinkpad"
219#define TPACPI_DESC "ThinkPad ACPI Extras"
220#define TPACPI_FILE TPACPI_NAME "_acpi"
221#define TPACPI_URL "http://ibm-acpi.sf.net/"
222#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200223
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200224#define TPACPI_PROC_DIR "ibm"
225#define TPACPI_ACPI_EVENT_PREFIX "ibm"
226#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300227#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200228#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200229
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300230#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300231#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300232
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200233#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200234
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000235/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200236#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000237#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200238#define TPACPI_DBG_INIT 0x0001
239#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000240#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000241#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000242#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000243#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200244#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200245
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200246#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
247#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
248#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200249
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200250
251/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200252 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200253 */
254
255struct ibm_struct;
256
257struct tp_acpi_drv_struct {
258 const struct acpi_device_id *hid;
259 struct acpi_driver *driver;
260
261 void (*notify) (struct ibm_struct *, u32);
262 acpi_handle *handle;
263 u32 type;
264 struct acpi_device *device;
265};
266
267struct ibm_struct {
268 char *name;
269
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200270 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200271 int (*write) (char *);
272 void (*exit) (void);
273 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200274 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200275 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200276
277 struct list_head all_drivers;
278
279 struct tp_acpi_drv_struct *acpi;
280
281 struct {
282 u8 acpi_driver_registered:1;
283 u8 acpi_notify_installed:1;
284 u8 proc_created:1;
285 u8 init_called:1;
286 u8 experimental:1;
287 } flags;
288};
289
290struct ibm_init_struct {
291 char param[32];
292
293 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400294 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200295 struct ibm_struct *data;
296};
297
298static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200299 u32 bluetooth:1;
300 u32 hotkey:1;
301 u32 hotkey_mask:1;
302 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200303 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200304 u32 light:1;
305 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300306 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300307 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200308 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200309 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200310 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300311 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300312 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200313 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200314 u32 input_device_registered:1;
315 u32 platform_drv_registered:1;
316 u32 platform_drv_attrs_registered:1;
317 u32 sensors_pdrv_registered:1;
318 u32 sensors_pdrv_attrs_registered:1;
319 u32 sensors_pdev_attrs_registered:1;
320 u32 hotkey_poll_active:1;
321} tp_features;
322
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300323static struct {
324 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200325 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300326} tp_warned;
327
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200328struct thinkpad_id_data {
329 unsigned int vendor; /* ThinkPad vendor:
330 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
331
332 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
333 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
334
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300335 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200336 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300337 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
338 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200339
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300340 char *model_str; /* ThinkPad T43 */
341 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200342};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200343static struct thinkpad_id_data thinkpad_id;
344
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300345static enum {
346 TPACPI_LIFE_INIT = 0,
347 TPACPI_LIFE_RUNNING,
348 TPACPI_LIFE_EXITING,
349} tpacpi_lifecycle;
350
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200351static int experimental;
352static u32 dbg_level;
353
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300354static struct workqueue_struct *tpacpi_wq;
355
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000356enum led_status_t {
357 TPACPI_LED_OFF = 0,
358 TPACPI_LED_ON,
359 TPACPI_LED_BLINK,
360};
361
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300362/* Special LED class that can defer work */
363struct tpacpi_led_classdev {
364 struct led_classdev led_classdev;
365 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000366 enum led_status_t new_state;
Adam Leeedf2d772013-06-08 16:51:15 +0800367 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300368};
369
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300370/* brightness level capabilities */
371static unsigned int bright_maxlvl; /* 0 = unknown */
372
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200373#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
374static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030375static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200376static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030377static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200378static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030379static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200380static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030381static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200382#endif
383
384
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000385/*************************************************************************
386 * Debugging helpers
387 */
388
Joe Perches0978e012011-04-04 10:06:25 -0700389#define dbg_printk(a_dbg_level, format, arg...) \
390do { \
391 if (dbg_level & (a_dbg_level)) \
392 printk(KERN_DEBUG pr_fmt("%s: " format), \
393 __func__, ##arg); \
394} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000395
396#ifdef CONFIG_THINKPAD_ACPI_DEBUG
397#define vdbg_printk dbg_printk
398static const char *str_supported(int is_supported);
399#else
Joe Perches0978e012011-04-04 10:06:25 -0700400static inline const char *str_supported(int is_supported) { return ""; }
401#define vdbg_printk(a_dbg_level, format, arg...) \
402 no_printk(format, ##arg)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000403#endif
404
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000405static void tpacpi_log_usertask(const char * const what)
406{
Joe Perches0978e012011-04-04 10:06:25 -0700407 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
408 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000409}
410
Joe Perches0978e012011-04-04 10:06:25 -0700411#define tpacpi_disclose_usertask(what, format, arg...) \
412do { \
413 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
414 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
415 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
416 what, task_tgid_vnr(current), ## arg); \
417 } \
418} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000419
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300420/*
421 * Quirk handling helpers
422 *
423 * ThinkPad IDs and versions seen in the field so far
424 * are two-characters from the set [0-9A-Z], i.e. base 36.
425 *
426 * We use values well outside that range as specials.
427 */
428
429#define TPACPI_MATCH_ANY 0xffffU
430#define TPACPI_MATCH_UNKNOWN 0U
431
432/* TPID('1', 'Y') == 0x5931 */
433#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
434
435#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
436 { .vendor = PCI_VENDOR_ID_IBM, \
437 .bios = TPID(__id1, __id2), \
438 .ec = TPACPI_MATCH_ANY, \
439 .quirks = (__quirk) }
440
441#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
442 { .vendor = PCI_VENDOR_ID_LENOVO, \
443 .bios = TPID(__id1, __id2), \
444 .ec = TPACPI_MATCH_ANY, \
445 .quirks = (__quirk) }
446
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200447#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_LENOVO, \
449 .bios = TPACPI_MATCH_ANY, \
450 .ec = TPID(__id1, __id2), \
451 .quirks = (__quirk) }
452
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300453struct tpacpi_quirk {
454 unsigned int vendor;
455 u16 bios;
456 u16 ec;
457 unsigned long quirks;
458};
459
460/**
461 * tpacpi_check_quirks() - search BIOS/EC version on a list
462 * @qlist: array of &struct tpacpi_quirk
463 * @qlist_size: number of elements in @qlist
464 *
465 * Iterates over a quirks list until one is found that matches the
466 * ThinkPad's vendor, BIOS and EC model.
467 *
468 * Returns 0 if nothing matches, otherwise returns the quirks field of
469 * the matching &struct tpacpi_quirk entry.
470 *
471 * The match criteria is: vendor, ec and bios much match.
472 */
473static unsigned long __init tpacpi_check_quirks(
474 const struct tpacpi_quirk *qlist,
475 unsigned int qlist_size)
476{
477 while (qlist_size) {
478 if ((qlist->vendor == thinkpad_id.vendor ||
479 qlist->vendor == TPACPI_MATCH_ANY) &&
480 (qlist->bios == thinkpad_id.bios_model ||
481 qlist->bios == TPACPI_MATCH_ANY) &&
482 (qlist->ec == thinkpad_id.ec_model ||
483 qlist->ec == TPACPI_MATCH_ANY))
484 return qlist->quirks;
485
486 qlist_size--;
487 qlist++;
488 }
489 return 0;
490}
491
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300492static inline bool __pure __init tpacpi_is_lenovo(void)
493{
494 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
495}
496
497static inline bool __pure __init tpacpi_is_ibm(void)
498{
499 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
500}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300501
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300502/****************************************************************************
503 ****************************************************************************
504 *
505 * ACPI Helpers and device model
506 *
507 ****************************************************************************
508 ****************************************************************************/
509
510/*************************************************************************
511 * ACPI basic handles
512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300514static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300515static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200517#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700519 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300520 &parent##_handle; \
521 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200523TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
524TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200526TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
527 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400528 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
529 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300530 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400531
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200532TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400533 "^HKEY", /* R30, R31 */
534 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300535 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400536
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300537/*************************************************************************
538 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200539 */
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700542 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400545 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200546 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400547 struct acpi_buffer result, *resultp;
548 union acpi_object out_obj;
549 acpi_status status;
550 va_list ap;
551 char res_type;
552 int success;
553 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700556 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return 0;
558 }
559
560 if (*fmt == 'q') {
561 quiet = 1;
562 fmt++;
563 } else
564 quiet = 0;
565
566 res_type = *(fmt++);
567
568 params.count = 0;
569 params.pointer = &in_objs[0];
570
571 va_start(ap, fmt);
572 while (*fmt) {
573 char c = *(fmt++);
574 switch (c) {
575 case 'd': /* int */
576 in_objs[params.count].integer.value = va_arg(ap, int);
577 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
578 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400579 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 default:
Joe Perches0978e012011-04-04 10:06:25 -0700581 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 "with invalid format character '%c'\n", c);
Jesper Juhl21365852010-12-24 19:56:28 +0100583 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return 0;
585 }
586 }
587 va_end(ap);
588
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400589 if (res_type != 'v') {
590 result.length = sizeof(out_obj);
591 result.pointer = &out_obj;
592 resultp = &result;
593 } else
594 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400596 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400599 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300600 success = (status == AE_OK &&
601 out_obj.type == ACPI_TYPE_INTEGER);
602 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700603 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400605 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 success = status == AE_OK;
607 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400608 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 default:
Joe Perches0978e012011-04-04 10:06:25 -0700610 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 "with invalid format character '%c'\n", res_type);
612 return 0;
613 }
614
615 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700616 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300617 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 return success;
620}
621
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200622static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300623{
624 int v;
625
626 if (ecrd_handle) {
627 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
628 return 0;
629 *p = v;
630 } else {
631 if (ec_read(i, p) < 0)
632 return 0;
633 }
634
635 return 1;
636}
637
638static int acpi_ec_write(int i, u8 v)
639{
640 if (ecwr_handle) {
641 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
642 return 0;
643 } else {
644 if (ec_write(i, v) < 0)
645 return 0;
646 }
647
648 return 1;
649}
650
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300651static int issue_thinkpad_cmos_command(int cmos_cmd)
652{
653 if (!cmos_handle)
654 return -ENXIO;
655
656 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
657 return -EIO;
658
659 return 0;
660}
661
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300662/*************************************************************************
663 * ACPI device model
664 */
665
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200666#define TPACPI_ACPIHANDLE_INIT(object) \
667 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300668 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200669
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300670static void __init drv_acpi_handle_init(const char *name,
671 acpi_handle *handle, const acpi_handle parent,
672 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300673{
674 int i;
675 acpi_status status;
676
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300677 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
678 name);
679
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300680 for (i = 0; i < num_paths; i++) {
681 status = acpi_get_handle(parent, paths[i], handle);
682 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300683 dbg_printk(TPACPI_DBG_INIT,
684 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300685 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300686 return;
687 }
688 }
689
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300690 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
691 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300692 *handle = NULL;
693}
694
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300695static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
696 u32 level, void *context, void **return_value)
697{
Aaron Lu46445b62013-10-11 21:27:46 +0800698 struct acpi_device *dev;
699 if (!strcmp(context, "video")) {
700 if (acpi_bus_get_device(handle, &dev))
701 return AE_OK;
702 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
703 return AE_OK;
704 }
705
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300706 *(acpi_handle *)return_value = handle;
707
708 return AE_CTRL_TERMINATE;
709}
710
711static void __init tpacpi_acpi_handle_locate(const char *name,
712 const char *hid,
713 acpi_handle *handle)
714{
715 acpi_status status;
716 acpi_handle device_found;
717
Aaron Lu46445b62013-10-11 21:27:46 +0800718 BUG_ON(!name || !handle);
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300719 vdbg_printk(TPACPI_DBG_INIT,
720 "trying to locate ACPI handle for %s, using HID %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +0800721 name, hid ? hid : "NULL");
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300722
723 memset(&device_found, 0, sizeof(device_found));
724 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
725 (void *)name, &device_found);
726
727 *handle = NULL;
728
729 if (ACPI_SUCCESS(status)) {
730 *handle = device_found;
731 dbg_printk(TPACPI_DBG_INIT,
732 "Found ACPI handle for %s\n", name);
733 } else {
734 vdbg_printk(TPACPI_DBG_INIT,
735 "Could not locate an ACPI handle for %s: %s\n",
736 name, acpi_format_exception(status));
737 }
738}
739
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300740static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300741{
742 struct ibm_struct *ibm = data;
743
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300744 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
745 return;
746
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300747 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300748 return;
749
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300750 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300751}
752
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300753static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300754{
755 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300756 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300757
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300758 BUG_ON(!ibm->acpi);
759
760 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300761 return 0;
762
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300763 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300764 "setting up ACPI notify for %s\n", ibm->name);
765
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300766 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
767 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700768 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300769 return -ENODEV;
770 }
771
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700772 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300773 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200774 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300775 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300776
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300777 status = acpi_install_notify_handler(*ibm->acpi->handle,
778 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300779 if (ACPI_FAILURE(status)) {
780 if (status == AE_ALREADY_EXISTS) {
Joe Perches0978e012011-04-04 10:06:25 -0700781 pr_notice("another device driver is already "
782 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300783 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700784 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300785 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300786 }
787 return -ENODEV;
788 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300789 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300790 return 0;
791}
792
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300793static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300794{
795 return 0;
796}
797
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300798static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300799{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300800 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300801
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300802 dbg_printk(TPACPI_DBG_INIT,
803 "registering %s as an ACPI driver\n", ibm->name);
804
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300805 BUG_ON(!ibm->acpi);
806
807 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
808 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700809 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300810 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300811 }
812
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200813 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300814 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200815
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300816 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300817
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300818 rc = acpi_bus_register_driver(ibm->acpi->driver);
819 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700820 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200821 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300822 kfree(ibm->acpi->driver);
823 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300824 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300825 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300826
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300827 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300828}
829
830
831/****************************************************************************
832 ****************************************************************************
833 *
834 * Procfs Helpers
835 *
836 ****************************************************************************
837 ****************************************************************************/
838
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200839static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300840{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200841 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300842
843 if (!ibm || !ibm->read)
844 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200845 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300846}
847
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200848static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300849{
Al Virod9dda782013-03-31 18:16:14 -0400850 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200851}
852
853static ssize_t dispatch_proc_write(struct file *file,
854 const char __user *userbuf,
855 size_t count, loff_t *pos)
856{
Al Virod9dda782013-03-31 18:16:14 -0400857 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300858 char *kernbuf;
859 int ret;
860
861 if (!ibm || !ibm->write)
862 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300863 if (count > PAGE_SIZE - 2)
864 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300865
866 kernbuf = kmalloc(count + 2, GFP_KERNEL);
867 if (!kernbuf)
868 return -ENOMEM;
869
870 if (copy_from_user(kernbuf, userbuf, count)) {
871 kfree(kernbuf);
872 return -EFAULT;
873 }
874
875 kernbuf[count] = 0;
876 strcat(kernbuf, ",");
877 ret = ibm->write(kernbuf);
878 if (ret == 0)
879 ret = count;
880
881 kfree(kernbuf);
882
883 return ret;
884}
885
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200886static const struct file_operations dispatch_proc_fops = {
887 .owner = THIS_MODULE,
888 .open = dispatch_proc_open,
889 .read = seq_read,
890 .llseek = seq_lseek,
891 .release = single_release,
892 .write = dispatch_proc_write,
893};
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895static char *next_cmd(char **cmds)
896{
897 char *start = *cmds;
898 char *end;
899
900 while ((end = strchr(start, ',')) && end == start)
901 start = end + 1;
902
903 if (!end)
904 return NULL;
905
906 *end = 0;
907 *cmds = end + 1;
908 return start;
909}
910
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300911
912/****************************************************************************
913 ****************************************************************************
914 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300915 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300916 *
917 ****************************************************************************
918 ****************************************************************************/
919
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300920static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300921static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700922static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300923static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300924static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200925static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300926
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +0200927#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200928static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200929{
930 struct ibm_struct *ibm, *itmp;
931
932 list_for_each_entry_safe(ibm, itmp,
933 &tpacpi_all_drivers,
934 all_drivers) {
935 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200936 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200937 }
938
939 return 0;
940}
941
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200942static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300943{
944 struct ibm_struct *ibm, *itmp;
945
946 list_for_each_entry_safe(ibm, itmp,
947 &tpacpi_all_drivers,
948 all_drivers) {
949 if (ibm->resume)
950 (ibm->resume)();
951 }
952
953 return 0;
954}
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +0200955#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300956
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200957static SIMPLE_DEV_PM_OPS(tpacpi_pm,
958 tpacpi_suspend_handler, tpacpi_resume_handler);
959
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200960static void tpacpi_shutdown_handler(struct platform_device *pdev)
961{
962 struct ibm_struct *ibm, *itmp;
963
964 list_for_each_entry_safe(ibm, itmp,
965 &tpacpi_all_drivers,
966 all_drivers) {
967 if (ibm->shutdown)
968 (ibm->shutdown)();
969 }
970}
971
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300972static struct platform_driver tpacpi_pdriver = {
973 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200974 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300975 .owner = THIS_MODULE,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200976 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300977 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200978 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300979};
980
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300981static struct platform_driver tpacpi_hwmon_pdriver = {
982 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200983 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300984 .owner = THIS_MODULE,
985 },
986};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300987
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300988/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300989 * sysfs support helpers
990 */
991
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200992struct attribute_set {
993 unsigned int members, max_members;
994 struct attribute_group group;
995};
996
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300997struct attribute_set_obj {
998 struct attribute_set s;
999 struct attribute *a;
1000} __attribute__((packed));
1001
1002static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001003 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001004{
1005 struct attribute_set_obj *sobj;
1006
1007 if (max_members == 0)
1008 return NULL;
1009
1010 /* Allocates space for implicit NULL at the end too */
1011 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1012 max_members * sizeof(struct attribute *),
1013 GFP_KERNEL);
1014 if (!sobj)
1015 return NULL;
1016 sobj->s.max_members = max_members;
1017 sobj->s.group.attrs = &sobj->a;
1018 sobj->s.group.name = name;
1019
1020 return &sobj->s;
1021}
1022
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001023#define destroy_attr_set(_set) \
1024 kfree(_set);
1025
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001026/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001027static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001028{
1029 if (!s || !attr)
1030 return -EINVAL;
1031
1032 if (s->members >= s->max_members)
1033 return -ENOMEM;
1034
1035 s->group.attrs[s->members] = attr;
1036 s->members++;
1037
1038 return 0;
1039}
1040
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001041static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001042 struct attribute **attr,
1043 unsigned int count)
1044{
1045 int i, res;
1046
1047 for (i = 0; i < count; i++) {
1048 res = add_to_attr_set(s, attr[i]);
1049 if (res)
1050 return res;
1051 }
1052
1053 return 0;
1054}
1055
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001056static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001057{
1058 sysfs_remove_group(kobj, &s->group);
1059 destroy_attr_set(s);
1060}
1061
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001062#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1063 sysfs_create_group(_kobj, &_attr_set->group)
1064
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001065static int parse_strtoul(const char *buf,
1066 unsigned long max, unsigned long *value)
1067{
1068 char *endp;
1069
André Goddard Rosae7d28602009-12-14 18:01:06 -08001070 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1071 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001072 if (*endp || *value > max)
1073 return -EINVAL;
1074
1075 return 0;
1076}
1077
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001078static void tpacpi_disable_brightness_delay(void)
1079{
1080 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001081 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001082}
1083
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001084static void printk_deprecated_attribute(const char * const what,
1085 const char * const details)
1086{
1087 tpacpi_log_usertask("deprecated sysfs attribute");
Joe Perches0978e012011-04-04 10:06:25 -07001088 pr_warn("WARNING: sysfs attribute %s is deprecated and "
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001089 "will be removed. %s\n",
1090 what, details);
1091}
1092
Johannes Berg19d337d2009-06-02 13:01:37 +02001093/*************************************************************************
1094 * rfkill and radio control support helpers
1095 */
1096
1097/*
1098 * ThinkPad-ACPI firmware handling model:
1099 *
1100 * WLSW (master wireless switch) is event-driven, and is common to all
1101 * firmware-controlled radios. It cannot be controlled, just monitored,
1102 * as expected. It overrides all radio state in firmware
1103 *
1104 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1105 * (TODO: verify how WLSW interacts with the returned radio state).
1106 *
1107 * The only time there are shadow radio state changes, is when
1108 * masked-off hotkeys are used.
1109 */
1110
1111/*
1112 * Internal driver API for radio state:
1113 *
1114 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1115 * bool: true means radio blocked (off)
1116 */
1117enum tpacpi_rfkill_state {
1118 TPACPI_RFK_RADIO_OFF = 0,
1119 TPACPI_RFK_RADIO_ON
1120};
1121
1122/* rfkill switches */
1123enum tpacpi_rfk_id {
1124 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1125 TPACPI_RFK_WWAN_SW_ID,
1126 TPACPI_RFK_UWB_SW_ID,
1127 TPACPI_RFK_SW_MAX
1128};
1129
1130static const char *tpacpi_rfkill_names[] = {
1131 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1132 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1133 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1134 [TPACPI_RFK_SW_MAX] = NULL
1135};
1136
1137/* ThinkPad-ACPI rfkill subdriver */
1138struct tpacpi_rfk {
1139 struct rfkill *rfkill;
1140 enum tpacpi_rfk_id id;
1141 const struct tpacpi_rfk_ops *ops;
1142};
1143
1144struct tpacpi_rfk_ops {
1145 /* firmware interface */
1146 int (*get_status)(void);
1147 int (*set_status)(const enum tpacpi_rfkill_state);
1148};
1149
1150static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1151
1152/* Query FW and update rfkill sw state for a given rfkill switch */
1153static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1154{
1155 int status;
1156
1157 if (!tp_rfk)
1158 return -ENODEV;
1159
1160 status = (tp_rfk->ops->get_status)();
1161 if (status < 0)
1162 return status;
1163
1164 rfkill_set_sw_state(tp_rfk->rfkill,
1165 (status == TPACPI_RFK_RADIO_OFF));
1166
1167 return status;
1168}
1169
1170/* Query FW and update rfkill sw state for all rfkill switches */
1171static void tpacpi_rfk_update_swstate_all(void)
1172{
1173 unsigned int i;
1174
1175 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1176 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1177}
1178
1179/*
1180 * Sync the HW-blocking state of all rfkill switches,
1181 * do notice it causes the rfkill core to schedule uevents
1182 */
1183static void tpacpi_rfk_update_hwblock_state(bool blocked)
1184{
1185 unsigned int i;
1186 struct tpacpi_rfk *tp_rfk;
1187
1188 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1189 tp_rfk = tpacpi_rfkill_switches[i];
1190 if (tp_rfk) {
1191 if (rfkill_set_hw_state(tp_rfk->rfkill,
1192 blocked)) {
1193 /* ignore -- we track sw block */
1194 }
1195 }
1196 }
1197}
1198
1199/* Call to get the WLSW state from the firmware */
1200static int hotkey_get_wlsw(void);
1201
1202/* Call to query WLSW state and update all rfkill switches */
1203static bool tpacpi_rfk_check_hwblock_state(void)
1204{
1205 int res = hotkey_get_wlsw();
1206 int hw_blocked;
1207
1208 /* When unknown or unsupported, we have to assume it is unblocked */
1209 if (res < 0)
1210 return false;
1211
1212 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1213 tpacpi_rfk_update_hwblock_state(hw_blocked);
1214
1215 return hw_blocked;
1216}
1217
1218static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1219{
1220 struct tpacpi_rfk *tp_rfk = data;
1221 int res;
1222
1223 dbg_printk(TPACPI_DBG_RFKILL,
1224 "request to change radio state to %s\n",
1225 blocked ? "blocked" : "unblocked");
1226
1227 /* try to set radio state */
1228 res = (tp_rfk->ops->set_status)(blocked ?
1229 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1230
1231 /* and update the rfkill core with whatever the FW really did */
1232 tpacpi_rfk_update_swstate(tp_rfk);
1233
1234 return (res < 0) ? res : 0;
1235}
1236
1237static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1238 .set_block = tpacpi_rfk_hook_set_block,
1239};
1240
1241static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1242 const struct tpacpi_rfk_ops *tp_rfkops,
1243 const enum rfkill_type rfktype,
1244 const char *name,
1245 const bool set_default)
1246{
1247 struct tpacpi_rfk *atp_rfk;
1248 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001249 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001250 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001251 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001252
1253 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1254
Johannes Berg19d337d2009-06-02 13:01:37 +02001255 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1256 if (atp_rfk)
1257 atp_rfk->rfkill = rfkill_alloc(name,
1258 &tpacpi_pdev->dev,
1259 rfktype,
1260 &tpacpi_rfk_rfkill_ops,
1261 atp_rfk);
1262 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001263 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001264 kfree(atp_rfk);
1265 return -ENOMEM;
1266 }
1267
1268 atp_rfk->id = id;
1269 atp_rfk->ops = tp_rfkops;
1270
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001271 sw_status = (tp_rfkops->get_status)();
1272 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001273 pr_err("failed to read initial state for %s, error %d\n",
1274 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001275 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001276 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001277 if (set_default) {
1278 /* try to keep the initial state, since we ask the
1279 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001280 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001281 }
1282 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001283 hw_state = tpacpi_rfk_check_hwblock_state();
1284 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001285
1286 res = rfkill_register(atp_rfk->rfkill);
1287 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001288 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001289 rfkill_destroy(atp_rfk->rfkill);
1290 kfree(atp_rfk);
1291 return res;
1292 }
1293
1294 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001295
Joe Perches0978e012011-04-04 10:06:25 -07001296 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001297 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001298 return 0;
1299}
1300
1301static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1302{
1303 struct tpacpi_rfk *tp_rfk;
1304
1305 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1306
1307 tp_rfk = tpacpi_rfkill_switches[id];
1308 if (tp_rfk) {
1309 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001310 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001311 tpacpi_rfkill_switches[id] = NULL;
1312 kfree(tp_rfk);
1313 }
1314}
1315
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001316static void printk_deprecated_rfkill_attribute(const char * const what)
1317{
1318 printk_deprecated_attribute(what,
1319 "Please switch to generic rfkill before year 2010");
1320}
1321
Johannes Berg19d337d2009-06-02 13:01:37 +02001322/* sysfs <radio> enable ------------------------------------------------ */
1323static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1324 struct device_attribute *attr,
1325 char *buf)
1326{
1327 int status;
1328
1329 printk_deprecated_rfkill_attribute(attr->attr.name);
1330
1331 /* This is in the ABI... */
1332 if (tpacpi_rfk_check_hwblock_state()) {
1333 status = TPACPI_RFK_RADIO_OFF;
1334 } else {
1335 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1336 if (status < 0)
1337 return status;
1338 }
1339
1340 return snprintf(buf, PAGE_SIZE, "%d\n",
1341 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1342}
1343
1344static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1345 struct device_attribute *attr,
1346 const char *buf, size_t count)
1347{
1348 unsigned long t;
1349 int res;
1350
1351 printk_deprecated_rfkill_attribute(attr->attr.name);
1352
1353 if (parse_strtoul(buf, 1, &t))
1354 return -EINVAL;
1355
1356 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1357
1358 /* This is in the ABI... */
1359 if (tpacpi_rfk_check_hwblock_state() && !!t)
1360 return -EPERM;
1361
1362 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1363 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1364 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1365
1366 return (res < 0) ? res : count;
1367}
1368
1369/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001370static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001371{
Johannes Berg19d337d2009-06-02 13:01:37 +02001372 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001373 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001374 else {
1375 int status;
1376
1377 /* This is in the ABI... */
1378 if (tpacpi_rfk_check_hwblock_state()) {
1379 status = TPACPI_RFK_RADIO_OFF;
1380 } else {
1381 status = tpacpi_rfk_update_swstate(
1382 tpacpi_rfkill_switches[id]);
1383 if (status < 0)
1384 return status;
1385 }
1386
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001387 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001388 (status == TPACPI_RFK_RADIO_ON) ?
1389 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001390 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001391 }
1392
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001393 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001394}
1395
1396static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1397{
1398 char *cmd;
1399 int status = -1;
1400 int res = 0;
1401
1402 if (id >= TPACPI_RFK_SW_MAX)
1403 return -ENODEV;
1404
1405 while ((cmd = next_cmd(&buf))) {
1406 if (strlencmp(cmd, "enable") == 0)
1407 status = TPACPI_RFK_RADIO_ON;
1408 else if (strlencmp(cmd, "disable") == 0)
1409 status = TPACPI_RFK_RADIO_OFF;
1410 else
1411 return -EINVAL;
1412 }
1413
1414 if (status != -1) {
1415 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1416 (status == TPACPI_RFK_RADIO_ON) ?
1417 "enable" : "disable",
1418 tpacpi_rfkill_names[id]);
1419 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1420 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1421 }
1422
1423 return res;
1424}
1425
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001426/*************************************************************************
1427 * thinkpad-acpi driver attributes
1428 */
1429
1430/* interface_version --------------------------------------------------- */
1431static ssize_t tpacpi_driver_interface_version_show(
1432 struct device_driver *drv,
1433 char *buf)
1434{
1435 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1436}
1437
1438static DRIVER_ATTR(interface_version, S_IRUGO,
1439 tpacpi_driver_interface_version_show, NULL);
1440
1441/* debug_level --------------------------------------------------------- */
1442static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1443 char *buf)
1444{
1445 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1446}
1447
1448static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1449 const char *buf, size_t count)
1450{
1451 unsigned long t;
1452
1453 if (parse_strtoul(buf, 0xffff, &t))
1454 return -EINVAL;
1455
1456 dbg_level = t;
1457
1458 return count;
1459}
1460
1461static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1462 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1463
1464/* version ------------------------------------------------------------- */
1465static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1466 char *buf)
1467{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001468 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1469 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001470}
1471
1472static DRIVER_ATTR(version, S_IRUGO,
1473 tpacpi_driver_version_show, NULL);
1474
1475/* --------------------------------------------------------------------- */
1476
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001477#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1478
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001479/* wlsw_emulstate ------------------------------------------------------ */
1480static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1481 char *buf)
1482{
1483 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1484}
1485
1486static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1487 const char *buf, size_t count)
1488{
1489 unsigned long t;
1490
1491 if (parse_strtoul(buf, 1, &t))
1492 return -EINVAL;
1493
Johannes Berg19d337d2009-06-02 13:01:37 +02001494 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001495 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001496 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1497 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001498
1499 return count;
1500}
1501
1502static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1503 tpacpi_driver_wlsw_emulstate_show,
1504 tpacpi_driver_wlsw_emulstate_store);
1505
1506/* bluetooth_emulstate ------------------------------------------------- */
1507static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1508 struct device_driver *drv,
1509 char *buf)
1510{
1511 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1512}
1513
1514static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1515 struct device_driver *drv,
1516 const char *buf, size_t count)
1517{
1518 unsigned long t;
1519
1520 if (parse_strtoul(buf, 1, &t))
1521 return -EINVAL;
1522
1523 tpacpi_bluetooth_emulstate = !!t;
1524
1525 return count;
1526}
1527
1528static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1529 tpacpi_driver_bluetooth_emulstate_show,
1530 tpacpi_driver_bluetooth_emulstate_store);
1531
1532/* wwan_emulstate ------------------------------------------------- */
1533static ssize_t tpacpi_driver_wwan_emulstate_show(
1534 struct device_driver *drv,
1535 char *buf)
1536{
1537 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1538}
1539
1540static ssize_t tpacpi_driver_wwan_emulstate_store(
1541 struct device_driver *drv,
1542 const char *buf, size_t count)
1543{
1544 unsigned long t;
1545
1546 if (parse_strtoul(buf, 1, &t))
1547 return -EINVAL;
1548
1549 tpacpi_wwan_emulstate = !!t;
1550
1551 return count;
1552}
1553
1554static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1555 tpacpi_driver_wwan_emulstate_show,
1556 tpacpi_driver_wwan_emulstate_store);
1557
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001558/* uwb_emulstate ------------------------------------------------- */
1559static ssize_t tpacpi_driver_uwb_emulstate_show(
1560 struct device_driver *drv,
1561 char *buf)
1562{
1563 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1564}
1565
1566static ssize_t tpacpi_driver_uwb_emulstate_store(
1567 struct device_driver *drv,
1568 const char *buf, size_t count)
1569{
1570 unsigned long t;
1571
1572 if (parse_strtoul(buf, 1, &t))
1573 return -EINVAL;
1574
1575 tpacpi_uwb_emulstate = !!t;
1576
1577 return count;
1578}
1579
1580static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1581 tpacpi_driver_uwb_emulstate_show,
1582 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001583#endif
1584
1585/* --------------------------------------------------------------------- */
1586
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001587static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001588 &driver_attr_debug_level, &driver_attr_version,
1589 &driver_attr_interface_version,
1590};
1591
1592static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1593{
1594 int i, res;
1595
1596 i = 0;
1597 res = 0;
1598 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1599 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1600 i++;
1601 }
1602
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001603#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1604 if (!res && dbg_wlswemul)
1605 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1606 if (!res && dbg_bluetoothemul)
1607 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1608 if (!res && dbg_wwanemul)
1609 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001610 if (!res && dbg_uwbemul)
1611 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001612#endif
1613
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001614 return res;
1615}
1616
1617static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1618{
1619 int i;
1620
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001621 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001622 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001623
1624#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1625 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1626 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1627 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001628 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001629#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001630}
1631
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001632/*************************************************************************
1633 * Firmware Data
1634 */
1635
1636/*
1637 * Table of recommended minimum BIOS versions
1638 *
1639 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001640 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001641 * bugs and bad ACPI behaviour on older versions
1642 *
1643 * 2. BIOS or EC fw with known bugs that trigger on Linux
1644 *
1645 * 3. BIOS with known reduced functionality in older versions
1646 *
1647 * We recommend the latest BIOS and EC version.
1648 * We only support the latest BIOS and EC fw version as a rule.
1649 *
1650 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1651 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001652 *
1653 * WARNING: we use this table also to detect that the machine is
1654 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001655 */
1656
1657#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1658 { .vendor = (__v), \
1659 .bios = TPID(__id1, __id2), \
1660 .ec = TPACPI_MATCH_ANY, \
1661 .quirks = TPACPI_MATCH_ANY << 16 \
1662 | (__bv1) << 8 | (__bv2) }
1663
1664#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001665 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001666 { .vendor = (__v), \
1667 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001668 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001669 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1670 | (__bv1) << 8 | (__bv2) }
1671
1672#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1673 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1674
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_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1677 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001678 __bv1, __bv2, TPID(__id1, __id2), \
1679 __ev1, __ev2), \
1680 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1681 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1682 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001683
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001684/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001685#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1686 __eid1, __eid2, __ev1, __ev2) \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001688 __bv1, __bv2, TPID(__eid1, __eid2), \
1689 __ev1, __ev2), \
1690 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1691 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1692 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001693
1694#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1695 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1696
1697#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1698 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001699 __bv1, __bv2, TPID(__id1, __id2), \
1700 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001701
1702#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1703 __eid1, __eid2, __ev1, __ev2) \
1704 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001705 __bv1, __bv2, TPID(__eid1, __eid2), \
1706 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001707
1708static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1709 /* Numeric models ------------------ */
1710 /* FW MODEL BIOS VERS */
1711 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1712 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1713 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1714 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1715 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1716 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1717 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1718 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1719 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1720
1721 /* A-series ------------------------- */
1722 /* FW MODEL BIOS VERS EC VERS */
1723 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1724 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1725 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1726 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1727 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1728 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1729 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1730 TPV_QI0('1', '3', '2', '0'), /* A22m */
1731 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1732 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1733 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1734
1735 /* G-series ------------------------- */
1736 /* FW MODEL BIOS VERS */
1737 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1738 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1739
1740 /* R-series, T-series --------------- */
1741 /* FW MODEL BIOS VERS EC VERS */
1742 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1743 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1744 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1745 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1746 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1747 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1748 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1749 T40/p, T41/p, T42/p (1) */
1750 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1751 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1752 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1753 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1754
1755 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1756 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1757 TPV_QI0('1', '6', '3', '2'), /* T22 */
1758 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1759 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1760 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1761
1762 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1763 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001764 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001765
1766 /* BIOS FW BIOS VERS EC FW EC VERS */
1767 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1768 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1769
1770 /* X-series ------------------------- */
1771 /* FW MODEL BIOS VERS EC VERS */
1772 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1773 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1774 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1775 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1776 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1777 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1778 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1779
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001780 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1781 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001782
1783 /* (0) - older versions lack DMI EC fw string and functionality */
1784 /* (1) - older versions known to lack functionality */
1785};
1786
1787#undef TPV_QL1
1788#undef TPV_QL0
1789#undef TPV_QI2
1790#undef TPV_QI1
1791#undef TPV_QI0
1792#undef TPV_Q_X
1793#undef TPV_Q
1794
1795static void __init tpacpi_check_outdated_fw(void)
1796{
1797 unsigned long fwvers;
1798 u16 ec_version, bios_version;
1799
1800 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1801 ARRAY_SIZE(tpacpi_bios_version_qtable));
1802
1803 if (!fwvers)
1804 return;
1805
1806 bios_version = fwvers & 0xffffU;
1807 ec_version = (fwvers >> 16) & 0xffffU;
1808
1809 /* note that unknown versions are set to 0x0000 and we use that */
1810 if ((bios_version > thinkpad_id.bios_release) ||
1811 (ec_version > thinkpad_id.ec_release &&
1812 ec_version != TPACPI_MATCH_ANY)) {
1813 /*
1814 * The changelogs would let us track down the exact
1815 * reason, but it is just too much of a pain to track
1816 * it. We only list BIOSes that are either really
1817 * broken, or really stable to begin with, so it is
1818 * best if the user upgrades the firmware anyway.
1819 */
Joe Perches0978e012011-04-04 10:06:25 -07001820 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1821 pr_warn("WARNING: This firmware may be missing critical bug "
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001822 "fixes and/or important features\n");
1823 }
1824}
1825
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001826static bool __init tpacpi_is_fw_known(void)
1827{
1828 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1829 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1830}
1831
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001832/****************************************************************************
1833 ****************************************************************************
1834 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001835 * Subdrivers
1836 *
1837 ****************************************************************************
1838 ****************************************************************************/
1839
1840/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001841 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001842 */
1843
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001844static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001846 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1847 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849}
1850
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001851static struct ibm_struct thinkpad_acpi_driver_data = {
1852 .name = "driver",
1853 .read = thinkpad_acpi_driver_read,
1854};
1855
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001856/*************************************************************************
1857 * Hotkey subdriver
1858 */
1859
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001860/*
1861 * ThinkPad firmware event model
1862 *
1863 * The ThinkPad firmware has two main event interfaces: normal ACPI
1864 * notifications (which follow the ACPI standard), and a private event
1865 * interface.
1866 *
1867 * The private event interface also issues events for the hotkeys. As
1868 * the driver gained features, the event handling code ended up being
1869 * built around the hotkey subdriver. This will need to be refactored
1870 * to a more formal event API eventually.
1871 *
1872 * Some "hotkeys" are actually supposed to be used as event reports,
1873 * such as "brightness has changed", "volume has changed", depending on
1874 * the ThinkPad model and how the firmware is operating.
1875 *
1876 * Unlike other classes, hotkey-class events have mask/unmask control on
1877 * non-ancient firmware. However, how it behaves changes a lot with the
1878 * firmware model and version.
1879 */
1880
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001881enum { /* hot key scan codes (derived from ACPI DSDT) */
1882 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1883 TP_ACPI_HOTKEYSCAN_FNF2,
1884 TP_ACPI_HOTKEYSCAN_FNF3,
1885 TP_ACPI_HOTKEYSCAN_FNF4,
1886 TP_ACPI_HOTKEYSCAN_FNF5,
1887 TP_ACPI_HOTKEYSCAN_FNF6,
1888 TP_ACPI_HOTKEYSCAN_FNF7,
1889 TP_ACPI_HOTKEYSCAN_FNF8,
1890 TP_ACPI_HOTKEYSCAN_FNF9,
1891 TP_ACPI_HOTKEYSCAN_FNF10,
1892 TP_ACPI_HOTKEYSCAN_FNF11,
1893 TP_ACPI_HOTKEYSCAN_FNF12,
1894 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1895 TP_ACPI_HOTKEYSCAN_FNINSERT,
1896 TP_ACPI_HOTKEYSCAN_FNDELETE,
1897 TP_ACPI_HOTKEYSCAN_FNHOME,
1898 TP_ACPI_HOTKEYSCAN_FNEND,
1899 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1900 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1901 TP_ACPI_HOTKEYSCAN_FNSPACE,
1902 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1903 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1904 TP_ACPI_HOTKEYSCAN_MUTE,
1905 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001906 TP_ACPI_HOTKEYSCAN_UNK1,
1907 TP_ACPI_HOTKEYSCAN_UNK2,
1908 TP_ACPI_HOTKEYSCAN_UNK3,
1909 TP_ACPI_HOTKEYSCAN_UNK4,
1910 TP_ACPI_HOTKEYSCAN_UNK5,
1911 TP_ACPI_HOTKEYSCAN_UNK6,
1912 TP_ACPI_HOTKEYSCAN_UNK7,
1913 TP_ACPI_HOTKEYSCAN_UNK8,
1914
1915 /* Hotkey keymap size */
1916 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001917};
1918
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001919enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001920 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1921 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1922};
1923
1924enum { /* Positions of some of the keys in hotkey masks */
1925 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1926 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1927 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1928 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1929 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1930 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1931 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1932 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1933 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1934 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1935 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1936};
1937
1938enum { /* NVRAM to ACPI HKEY group map */
1939 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1940 TP_ACPI_HKEY_ZOOM_MASK |
1941 TP_ACPI_HKEY_DISPSWTCH_MASK |
1942 TP_ACPI_HKEY_HIBERNATE_MASK,
1943 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1944 TP_ACPI_HKEY_BRGHTDWN_MASK,
1945 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1946 TP_ACPI_HKEY_VOLDWN_MASK |
1947 TP_ACPI_HKEY_MUTE_MASK,
1948};
1949
1950#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1951struct tp_nvram_state {
1952 u16 thinkpad_toggle:1;
1953 u16 zoom_toggle:1;
1954 u16 display_toggle:1;
1955 u16 thinklight_toggle:1;
1956 u16 hibernate_toggle:1;
1957 u16 displayexp_toggle:1;
1958 u16 display_state:1;
1959 u16 brightness_toggle:1;
1960 u16 volume_toggle:1;
1961 u16 mute:1;
1962
1963 u8 brightness_level;
1964 u8 volume_level;
1965};
1966
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001967/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001968static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001969
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001970/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001971 * Acquire mutex to write poller control variables as an
1972 * atomic block.
1973 *
1974 * Increment hotkey_config_change when changing them if you
1975 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001976 *
1977 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1978 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001979static struct mutex hotkey_thread_data_mutex;
1980static unsigned int hotkey_config_change;
1981
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001982/*
1983 * hotkey poller control variables
1984 *
1985 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001986 *
1987 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1988 * should be used only when the changes need to be taken as
1989 * a block, OR when one needs to force the kthread to forget
1990 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001991 */
1992static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1993static unsigned int hotkey_poll_freq = 10; /* Hz */
1994
1995#define HOTKEY_CONFIG_CRITICAL_START \
1996 do { \
1997 mutex_lock(&hotkey_thread_data_mutex); \
1998 hotkey_config_change++; \
1999 } while (0);
2000#define HOTKEY_CONFIG_CRITICAL_END \
2001 mutex_unlock(&hotkey_thread_data_mutex);
2002
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002003#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2004
2005#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002006#define HOTKEY_CONFIG_CRITICAL_START
2007#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002008
2009#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2010
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002011static struct mutex hotkey_mutex;
2012
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002013static enum { /* Reasons for waking up */
2014 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2015 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2016 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2017} hotkey_wakeup_reason;
2018
2019static int hotkey_autosleep_ack;
2020
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002021static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2022static u32 hotkey_all_mask; /* all events supported in fw */
2023static u32 hotkey_reserved_mask; /* events better left disabled */
2024static u32 hotkey_driver_mask; /* events needed by the driver */
2025static u32 hotkey_user_mask; /* events visible to userspace */
2026static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002027
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002028static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002029
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03002030static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002031
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002032static void tpacpi_driver_event(const unsigned int hkey_event);
2033static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002034static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002035
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002036/* HKEY.MHKG() return bits */
2037#define TP_HOTKEY_TABLET_MASK (1 << 3)
2038
Johannes Berg19d337d2009-06-02 13:01:37 +02002039static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002040{
Johannes Berg19d337d2009-06-02 13:01:37 +02002041 int status;
2042
2043 if (!tp_features.hotkey_wlsw)
2044 return -ENODEV;
2045
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002046#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002047 if (dbg_wlswemul)
2048 return (tpacpi_wlsw_emulstate) ?
2049 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002050#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002051
2052 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002053 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002054
2055 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002056}
2057
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002058static int hotkey_get_tablet_mode(int *status)
2059{
2060 int s;
2061
2062 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2063 return -EIO;
2064
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002065 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2066 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002067}
2068
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002069/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002070 * Reads current event mask from firmware, and updates
2071 * hotkey_acpi_mask accordingly. Also resets any bits
2072 * from hotkey_user_mask that are unavailable to be
2073 * delivered (shadow requirement of the userspace ABI).
2074 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002075 * Call with hotkey_mutex held
2076 */
2077static int hotkey_mask_get(void)
2078{
2079 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002080 u32 m = 0;
2081
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002082 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002083 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002084
2085 hotkey_acpi_mask = m;
2086 } else {
2087 /* no mask support doesn't mean no event support... */
2088 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002089 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002090
2091 /* sync userspace-visible mask */
2092 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002093
2094 return 0;
2095}
2096
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002097void static hotkey_mask_warn_incomplete_mask(void)
2098{
2099 /* log only what the user can fix... */
2100 const u32 wantedmask = hotkey_driver_mask &
2101 ~(hotkey_acpi_mask | hotkey_source_mask) &
2102 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2103
2104 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002105 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002106}
2107
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002108/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002109 * Set the firmware mask when supported
2110 *
2111 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2112 *
2113 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2114 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002115 * Call with hotkey_mutex held
2116 */
2117static int hotkey_mask_set(u32 mask)
2118{
2119 int i;
2120 int rc = 0;
2121
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002122 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002123
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002124 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002125 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002126 if (!acpi_evalf(hkey_handle,
2127 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002128 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002129 rc = -EIO;
2130 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002131 }
2132 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002133 }
2134
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002135 /*
2136 * We *must* make an inconditional call to hotkey_mask_get to
2137 * refresh hotkey_acpi_mask and update hotkey_user_mask
2138 *
2139 * Take the opportunity to also log when we cannot _enable_
2140 * a given event.
2141 */
2142 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Joe Perches0978e012011-04-04 10:06:25 -07002143 pr_notice("asked for hotkey mask 0x%08x, but "
2144 "firmware forced it to 0x%08x\n",
2145 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002146 }
2147
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002148 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2149 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002150
2151 return rc;
2152}
2153
2154/*
2155 * Sets hotkey_user_mask and tries to set the firmware mask
2156 *
2157 * Call with hotkey_mutex held
2158 */
2159static int hotkey_user_mask_set(const u32 mask)
2160{
2161 int rc;
2162
2163 /* Give people a chance to notice they are doing something that
2164 * is bound to go boom on their users sooner or later */
2165 if (!tp_warned.hotkey_mask_ff &&
2166 (mask == 0xffff || mask == 0xffffff ||
2167 mask == 0xffffffff)) {
2168 tp_warned.hotkey_mask_ff = 1;
Joe Perches0978e012011-04-04 10:06:25 -07002169 pr_notice("setting the hotkey mask to 0x%08x is likely "
2170 "not the best way to go about it\n", mask);
2171 pr_notice("please consider using the driver defaults, "
2172 "and refer to up-to-date thinkpad-acpi "
2173 "documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002174 }
2175
2176 /* Try to enable what the user asked for, plus whatever we need.
2177 * this syncs everything but won't enable bits in hotkey_user_mask */
2178 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2179
2180 /* Enable the available bits in hotkey_user_mask */
2181 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2182
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002183 return rc;
2184}
2185
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002186/*
2187 * Sets the driver hotkey mask.
2188 *
2189 * Can be called even if the hotkey subdriver is inactive
2190 */
2191static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2192{
2193 int rc;
2194
2195 /* Do the right thing if hotkey_init has not been called yet */
2196 if (!tp_features.hotkey) {
2197 hotkey_driver_mask = mask;
2198 return 0;
2199 }
2200
2201 mutex_lock(&hotkey_mutex);
2202
2203 HOTKEY_CONFIG_CRITICAL_START
2204 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002205#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002206 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002207#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002208 HOTKEY_CONFIG_CRITICAL_END
2209
2210 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2211 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002212 hotkey_poll_setup(true);
2213
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002214 mutex_unlock(&hotkey_mutex);
2215
2216 return rc;
2217}
2218
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002219static int hotkey_status_get(int *status)
2220{
2221 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2222 return -EIO;
2223
2224 return 0;
2225}
2226
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002227static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002228{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002229 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002230 return -EIO;
2231
2232 return 0;
2233}
2234
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002235static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002236{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002237 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002238
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002239 if (tp_features.hotkey_tablet &&
2240 !hotkey_get_tablet_mode(&state)) {
2241 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002242
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002243 input_report_switch(tpacpi_inputdev,
2244 SW_TABLET_MODE, !!state);
2245 input_sync(tpacpi_inputdev);
2246
2247 mutex_unlock(&tpacpi_inputdev_send_mutex);
2248 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002249}
2250
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002251/* Do NOT call without validating scancode first */
2252static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002253{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002254 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002255
2256 if (keycode != KEY_RESERVED) {
2257 mutex_lock(&tpacpi_inputdev_send_mutex);
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, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002261 input_sync(tpacpi_inputdev);
2262
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002263 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002264 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002265 input_sync(tpacpi_inputdev);
2266
2267 mutex_unlock(&tpacpi_inputdev_send_mutex);
2268 }
2269}
2270
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002271/* Do NOT call without validating scancode first */
2272static void tpacpi_input_send_key_masked(const unsigned int scancode)
2273{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002274 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002275 if (hotkey_user_mask & (1 << scancode))
2276 tpacpi_input_send_key(scancode);
2277}
2278
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002279#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2280static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2281
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002282/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002283static void tpacpi_hotkey_send_key(unsigned int scancode)
2284{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002285 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002286}
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;
Tejun Heo8a32c442011-11-21 12:32:23 -08002455 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002456 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002457 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002458
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002459 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2460 goto exit;
2461
2462 set_freezable();
2463
2464 so = 0;
2465 si = 1;
2466 t = 0;
2467
2468 /* Initial state for compares */
2469 mutex_lock(&hotkey_thread_data_mutex);
2470 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002471 poll_mask = hotkey_source_mask;
2472 event_mask = hotkey_source_mask &
2473 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002474 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002475 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002476 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002477
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002478 while (!kthread_should_stop()) {
2479 if (t == 0) {
2480 if (likely(poll_freq))
2481 t = 1000/poll_freq;
2482 else
2483 t = 100; /* should never happen... */
2484 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002485 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002486 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002487 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002488
2489 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002490 continue;
2491
2492 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002493 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002494 /* forget old state on thaw or config change */
2495 si = so;
2496 t = 0;
2497 change_detector = hotkey_config_change;
2498 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002499 poll_mask = hotkey_source_mask;
2500 event_mask = hotkey_source_mask &
2501 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002502 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002503 mutex_unlock(&hotkey_thread_data_mutex);
2504
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002505 if (likely(poll_mask)) {
2506 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002507 if (likely(si != so)) {
2508 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002509 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002510 }
2511 }
2512
2513 so = si;
2514 si ^= 1;
2515 }
2516
2517exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002518 return 0;
2519}
2520
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002521/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002522static void hotkey_poll_stop_sync(void)
2523{
2524 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002525 kthread_stop(tpacpi_hotkey_task);
2526 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002527 }
2528}
2529
2530/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002531static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002532{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002533 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2534 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002535
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002536 if (hotkey_poll_freq > 0 &&
2537 (poll_driver_mask ||
2538 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002539 if (!tpacpi_hotkey_task) {
2540 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002541 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002542 if (IS_ERR(tpacpi_hotkey_task)) {
2543 tpacpi_hotkey_task = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07002544 pr_err("could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002545 "for hotkey polling\n");
2546 }
2547 }
2548 } else {
2549 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002550 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002551 hotkey_poll_freq == 0) {
Joe Perches0978e012011-04-04 10:06:25 -07002552 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2553 "require polling, which is currently "
2554 "disabled\n",
2555 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002556 }
2557 }
2558}
2559
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002560static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002561{
2562 mutex_lock(&hotkey_mutex);
2563 hotkey_poll_setup(may_warn);
2564 mutex_unlock(&hotkey_mutex);
2565}
2566
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002567/* call with hotkey_mutex held */
2568static void hotkey_poll_set_freq(unsigned int freq)
2569{
2570 if (!freq)
2571 hotkey_poll_stop_sync();
2572
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002573 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002574}
2575
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002576#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2577
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002578static void hotkey_poll_setup(const bool __unused)
2579{
2580}
2581
2582static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002583{
2584}
2585
2586#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2587
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002588static int hotkey_inputdev_open(struct input_dev *dev)
2589{
2590 switch (tpacpi_lifecycle) {
2591 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002592 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002593 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002594 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002595 case TPACPI_LIFE_EXITING:
2596 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002597 }
2598
2599 /* Should only happen if tpacpi_lifecycle is corrupt */
2600 BUG();
2601 return -EBUSY;
2602}
2603
2604static void hotkey_inputdev_close(struct input_dev *dev)
2605{
2606 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002607 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002608 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002609 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002610}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002611
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002612/* sysfs hotkey enable ------------------------------------------------- */
2613static ssize_t hotkey_enable_show(struct device *dev,
2614 struct device_attribute *attr,
2615 char *buf)
2616{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002617 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002618
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002619 printk_deprecated_attribute("hotkey_enable",
2620 "Hotkey reporting is always enabled");
2621
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002622 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002623 if (res)
2624 return res;
2625
2626 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2627}
2628
2629static ssize_t hotkey_enable_store(struct device *dev,
2630 struct device_attribute *attr,
2631 const char *buf, size_t count)
2632{
2633 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002634
2635 printk_deprecated_attribute("hotkey_enable",
2636 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002637
2638 if (parse_strtoul(buf, 1, &t))
2639 return -EINVAL;
2640
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002641 if (t == 0)
2642 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002643
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002644 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002645}
2646
2647static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002648 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002649 hotkey_enable_show, hotkey_enable_store);
2650
2651/* sysfs hotkey mask --------------------------------------------------- */
2652static ssize_t hotkey_mask_show(struct device *dev,
2653 struct device_attribute *attr,
2654 char *buf)
2655{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002656 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002657}
2658
2659static ssize_t hotkey_mask_store(struct device *dev,
2660 struct device_attribute *attr,
2661 const char *buf, size_t count)
2662{
2663 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002664 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002665
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002666 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002667 return -EINVAL;
2668
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002669 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002670 return -ERESTARTSYS;
2671
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002672 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002673
2674#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002675 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002676#endif
2677
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002678 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002679
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002680 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2681
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002682 return (res) ? res : count;
2683}
2684
2685static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002686 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002687 hotkey_mask_show, hotkey_mask_store);
2688
2689/* sysfs hotkey bios_enabled ------------------------------------------- */
2690static ssize_t hotkey_bios_enabled_show(struct device *dev,
2691 struct device_attribute *attr,
2692 char *buf)
2693{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002694 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002695}
2696
2697static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002698 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002699
2700/* sysfs hotkey bios_mask ---------------------------------------------- */
2701static ssize_t hotkey_bios_mask_show(struct device *dev,
2702 struct device_attribute *attr,
2703 char *buf)
2704{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002705 printk_deprecated_attribute("hotkey_bios_mask",
2706 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002707 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002708}
2709
2710static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002711 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002712
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002713/* sysfs hotkey all_mask ----------------------------------------------- */
2714static ssize_t hotkey_all_mask_show(struct device *dev,
2715 struct device_attribute *attr,
2716 char *buf)
2717{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002718 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2719 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002720}
2721
2722static struct device_attribute dev_attr_hotkey_all_mask =
2723 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2724
2725/* sysfs hotkey recommended_mask --------------------------------------- */
2726static ssize_t hotkey_recommended_mask_show(struct device *dev,
2727 struct device_attribute *attr,
2728 char *buf)
2729{
2730 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002731 (hotkey_all_mask | hotkey_source_mask)
2732 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002733}
2734
2735static struct device_attribute dev_attr_hotkey_recommended_mask =
2736 __ATTR(hotkey_recommended_mask, S_IRUGO,
2737 hotkey_recommended_mask_show, NULL);
2738
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002739#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2740
2741/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2742static ssize_t hotkey_source_mask_show(struct device *dev,
2743 struct device_attribute *attr,
2744 char *buf)
2745{
2746 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2747}
2748
2749static ssize_t hotkey_source_mask_store(struct device *dev,
2750 struct device_attribute *attr,
2751 const char *buf, size_t count)
2752{
2753 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002754 u32 r_ev;
2755 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002756
2757 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2758 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2759 return -EINVAL;
2760
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002761 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002762 return -ERESTARTSYS;
2763
2764 HOTKEY_CONFIG_CRITICAL_START
2765 hotkey_source_mask = t;
2766 HOTKEY_CONFIG_CRITICAL_END
2767
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002768 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2769 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002770 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002771
2772 /* check if events needed by the driver got disabled */
2773 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2774 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002775
2776 mutex_unlock(&hotkey_mutex);
2777
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002778 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07002779 pr_err("hotkey_source_mask: "
2780 "failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002781
2782 if (r_ev)
Joe Perches0978e012011-04-04 10:06:25 -07002783 pr_notice("hotkey_source_mask: "
2784 "some important events were disabled: 0x%04x\n",
2785 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002786
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002787 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2788
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002789 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002790}
2791
2792static struct device_attribute dev_attr_hotkey_source_mask =
2793 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2794 hotkey_source_mask_show, hotkey_source_mask_store);
2795
2796/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2797static ssize_t hotkey_poll_freq_show(struct device *dev,
2798 struct device_attribute *attr,
2799 char *buf)
2800{
2801 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2802}
2803
2804static ssize_t hotkey_poll_freq_store(struct device *dev,
2805 struct device_attribute *attr,
2806 const char *buf, size_t count)
2807{
2808 unsigned long t;
2809
2810 if (parse_strtoul(buf, 25, &t))
2811 return -EINVAL;
2812
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002813 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002814 return -ERESTARTSYS;
2815
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002816 hotkey_poll_set_freq(t);
2817 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002818
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002819 mutex_unlock(&hotkey_mutex);
2820
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002821 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2822
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002823 return count;
2824}
2825
2826static struct device_attribute dev_attr_hotkey_poll_freq =
2827 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2828 hotkey_poll_freq_show, hotkey_poll_freq_store);
2829
2830#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2831
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002832/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002833static ssize_t hotkey_radio_sw_show(struct device *dev,
2834 struct device_attribute *attr,
2835 char *buf)
2836{
Johannes Berg19d337d2009-06-02 13:01:37 +02002837 int res;
2838 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002839 if (res < 0)
2840 return res;
2841
Johannes Berg19d337d2009-06-02 13:01:37 +02002842 /* Opportunistic update */
2843 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2844
2845 return snprintf(buf, PAGE_SIZE, "%d\n",
2846 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002847}
2848
2849static struct device_attribute dev_attr_hotkey_radio_sw =
2850 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2851
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002852static void hotkey_radio_sw_notify_change(void)
2853{
2854 if (tp_features.hotkey_wlsw)
2855 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2856 "hotkey_radio_sw");
2857}
2858
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002859/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2860static ssize_t hotkey_tablet_mode_show(struct device *dev,
2861 struct device_attribute *attr,
2862 char *buf)
2863{
2864 int res, s;
2865 res = hotkey_get_tablet_mode(&s);
2866 if (res < 0)
2867 return res;
2868
2869 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2870}
2871
2872static struct device_attribute dev_attr_hotkey_tablet_mode =
2873 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2874
2875static void hotkey_tablet_mode_notify_change(void)
2876{
2877 if (tp_features.hotkey_tablet)
2878 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2879 "hotkey_tablet_mode");
2880}
2881
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002882/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002883static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2884 struct device_attribute *attr,
2885 char *buf)
2886{
2887 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2888}
2889
2890static struct device_attribute dev_attr_hotkey_wakeup_reason =
2891 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2892
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002893static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002894{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002895 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2896 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002897}
2898
2899/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002900static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2901 struct device_attribute *attr,
2902 char *buf)
2903{
2904 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2905}
2906
2907static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2908 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2909 hotkey_wakeup_hotunplug_complete_show, NULL);
2910
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002911static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002912{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002913 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2914 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002915}
2916
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002917/* --------------------------------------------------------------------- */
2918
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002919static struct attribute *hotkey_attributes[] __initdata = {
2920 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002921 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002922 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002923 &dev_attr_hotkey_wakeup_reason.attr,
2924 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002925 &dev_attr_hotkey_mask.attr,
2926 &dev_attr_hotkey_all_mask.attr,
2927 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002928#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002929 &dev_attr_hotkey_source_mask.attr,
2930 &dev_attr_hotkey_poll_freq.attr,
2931#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002932};
2933
Johannes Berg19d337d2009-06-02 13:01:37 +02002934/*
2935 * Sync both the hw and sw blocking state of all switches
2936 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002937static void tpacpi_send_radiosw_update(void)
2938{
2939 int wlsw;
2940
Johannes Berg19d337d2009-06-02 13:01:37 +02002941 /*
2942 * We must sync all rfkill controllers *before* issuing any
2943 * rfkill input events, or we will race the rfkill core input
2944 * handler.
2945 *
Lucas De Marchic8440332011-03-17 17:18:22 -03002946 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02002947 * for the above.
2948 *
2949 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2950 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002951
Johannes Berg19d337d2009-06-02 13:01:37 +02002952 wlsw = hotkey_get_wlsw();
2953
2954 /* Sync hw blocking state first if it is hw-blocked */
2955 if (wlsw == TPACPI_RFK_RADIO_OFF)
2956 tpacpi_rfk_update_hwblock_state(true);
2957
2958 /* Sync sw blocking state */
2959 tpacpi_rfk_update_swstate_all();
2960
2961 /* Sync hw blocking state last if it is hw-unblocked */
2962 if (wlsw == TPACPI_RFK_RADIO_ON)
2963 tpacpi_rfk_update_hwblock_state(false);
2964
2965 /* Issue rfkill input event for WLSW switch */
2966 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002967 mutex_lock(&tpacpi_inputdev_send_mutex);
2968
2969 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002970 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002971 input_sync(tpacpi_inputdev);
2972
2973 mutex_unlock(&tpacpi_inputdev_send_mutex);
2974 }
Johannes Berg19d337d2009-06-02 13:01:37 +02002975
2976 /*
2977 * this can be unconditional, as we will poll state again
2978 * if userspace uses the notify to read data
2979 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002980 hotkey_radio_sw_notify_change();
2981}
2982
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002983static void hotkey_exit(void)
2984{
2985#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002986 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002987 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002988 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002989#endif
2990
2991 if (hotkey_dev_attributes)
2992 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2993
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03002994 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002995 "restoring original HKEY status and mask\n");
2996 /* yes, there is a bitwise or below, we want the
2997 * functions to be called even if one of them fail */
2998 if (((tp_features.hotkey_mask &&
2999 hotkey_mask_set(hotkey_orig_mask)) |
3000 hotkey_status_set(false)) != 0)
Joe Perches0978e012011-04-04 10:06:25 -07003001 pr_err("failed to restore hot key mask "
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003002 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003003}
3004
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003005static void __init hotkey_unmap(const unsigned int scancode)
3006{
3007 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3008 clear_bit(hotkey_keycode_map[scancode],
3009 tpacpi_inputdev->keybit);
3010 hotkey_keycode_map[scancode] = KEY_RESERVED;
3011 }
3012}
3013
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003014/*
3015 * HKEY quirks:
3016 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3017 */
3018
3019#define TPACPI_HK_Q_INIMASK 0x0001
3020
3021static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3022 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3023 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3024 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3025 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3026 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3027 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3028 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3029 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3030 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3031 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3032 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3033 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3034 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3035 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3036 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3037 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3038 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3039 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3040 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3041};
3042
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003043typedef u16 tpacpi_keymap_entry_t;
3044typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003045
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003046static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003047{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003048 /* Requirements for changing the default keymaps:
3049 *
3050 * 1. Many of the keys are mapped to KEY_RESERVED for very
3051 * good reasons. Do not change them unless you have deep
3052 * knowledge on the IBM and Lenovo ThinkPad firmware for
3053 * the various ThinkPad models. The driver behaves
3054 * differently for KEY_RESERVED: such keys have their
3055 * hot key mask *unset* in mask_recommended, and also
3056 * in the initial hot key mask programmed into the
3057 * firmware at driver load time, which means the firm-
3058 * ware may react very differently if you change them to
3059 * something else;
3060 *
3061 * 2. You must be subscribed to the linux-thinkpad and
3062 * ibm-acpi-devel mailing lists, and you should read the
3063 * list archives since 2007 if you want to change the
3064 * keymaps. This requirement exists so that you will
3065 * know the past history of problems with the thinkpad-
3066 * acpi driver keymaps, and also that you will be
3067 * listening to any bug reports;
3068 *
3069 * 3. Do not send thinkpad-acpi specific patches directly to
3070 * for merging, *ever*. Send them to the linux-acpi
3071 * mailinglist for comments. Merging is to be done only
3072 * through acpi-test and the ACPI maintainer.
3073 *
3074 * If the above is too much to ask, don't change the keymap.
3075 * Ask the thinkpad-acpi maintainer to do it, instead.
3076 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003077
3078 enum keymap_index {
3079 TPACPI_KEYMAP_IBM_GENERIC = 0,
3080 TPACPI_KEYMAP_LENOVO_GENERIC,
3081 };
3082
3083 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3084 /* Generic keymap for IBM ThinkPads */
3085 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003086 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003087 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003088 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3089 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003090
3091 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003092 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3093 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3094 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003095
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003096 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003097 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003098 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003099
3100 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003101 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003102
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003103 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3104 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003105
3106 /* Volume: firmware always react to it and reprograms
3107 * the built-in *extra* mixer. Never map it to control
3108 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003109 KEY_RESERVED, /* 0x14: VOLUME UP */
3110 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3111 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003112
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003113 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003114
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003115 /* (assignments unknown, please report if found) */
3116 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3117 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003118 },
3119
3120 /* Generic keymap for Lenovo ThinkPads */
3121 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003122 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3123 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003124 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003125 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003126
3127 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003128 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3129 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3130 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003131
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003132 /* These should be enabled --only-- when ACPI video
3133 * is disabled (i.e. in "vendor" mode), and are handled
3134 * in a special way by the init code */
3135 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3136 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003137
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003138 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003139
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003140 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3141 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003142
3143 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3144 * react to it and reprograms the built-in *extra* mixer.
3145 * Never map it to control another mixer by default.
3146 *
3147 * T60?, T61, R60?, R61: firmware and EC tries to send
3148 * these over the regular keyboard, so these are no-ops,
3149 * but there are still weird bugs re. MUTE, so do not
3150 * change unless you get test reports from all Lenovo
3151 * models. May cause the BIOS to interfere with the
3152 * HDA mixer.
3153 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003154 KEY_RESERVED, /* 0x14: VOLUME UP */
3155 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3156 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003157
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003158 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003159
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003160 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003161 KEY_UNKNOWN, KEY_UNKNOWN,
3162
3163 /*
3164 * The mic mute button only sends 0x1a. It does not
3165 * automatically mute the mic or change the mute light.
3166 */
3167 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3168
3169 /* (assignments unknown, please report if found) */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003170 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Andy Lutomirski33009552011-05-24 15:16:43 -04003171 KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003172 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003173 };
3174
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003175 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3176 /* Generic maps (fallback) */
3177 {
3178 .vendor = PCI_VENDOR_ID_IBM,
3179 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3180 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3181 },
3182 {
3183 .vendor = PCI_VENDOR_ID_LENOVO,
3184 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3185 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3186 },
3187 };
3188
3189#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003190#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003191
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003192 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003193 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003194 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003195 bool radiosw_state = false;
3196 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003197
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003198 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003199 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003200
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003201 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3202 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003203
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003204 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003205 BUG_ON(tpacpi_inputdev->open != NULL ||
3206 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003207
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003208 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003209 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003210
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003211#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003212 mutex_init(&hotkey_thread_data_mutex);
3213#endif
3214
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003215 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003216 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003217
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003218 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3219 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003220 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003221
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003222 if (!tp_features.hotkey)
3223 return 1;
3224
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003225 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3226 ARRAY_SIZE(tpacpi_hotkey_qtable));
3227
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003228 tpacpi_disable_brightness_delay();
3229
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003230 /* MUST have enough space for all attributes to be added to
3231 * hotkey_dev_attributes */
3232 hotkey_dev_attributes = create_attr_set(
3233 ARRAY_SIZE(hotkey_attributes) + 2,
3234 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003235 if (!hotkey_dev_attributes)
3236 return -ENOMEM;
3237 res = add_many_to_attr_set(hotkey_dev_attributes,
3238 hotkey_attributes,
3239 ARRAY_SIZE(hotkey_attributes));
3240 if (res)
3241 goto err_exit;
3242
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003243 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003244 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3245 for HKEY interface version 0x100 */
3246 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3247 if ((hkeyv >> 8) != 1) {
Joe Perches0978e012011-04-04 10:06:25 -07003248 pr_err("unknown version of the HKEY interface: 0x%x\n",
3249 hkeyv);
3250 pr_err("please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003251 } else {
3252 /*
3253 * MHKV 0x100 in A31, R40, R40e,
3254 * T4x, X31, and later
3255 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003256 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3257 "firmware HKEY interface version: 0x%x\n",
3258 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003259
3260 /* Paranoia check AND init hotkey_all_mask */
3261 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3262 "MHKA", "qd")) {
Joe Perches0978e012011-04-04 10:06:25 -07003263 pr_err("missing MHKA handler, "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003264 "please report this to %s\n",
3265 TPACPI_MAIL);
3266 /* Fallback: pre-init for FN+F3,F4,F12 */
3267 hotkey_all_mask = 0x080cU;
3268 } else {
3269 tp_features.hotkey_mask = 1;
3270 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003271 }
3272 }
3273
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003274 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3275 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003276 str_supported(tp_features.hotkey_mask));
3277
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003278 /* Init hotkey_all_mask if not initialized yet */
3279 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3280 (quirks & TPACPI_HK_Q_INIMASK))
3281 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003282
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003283 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003284 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003285 /* hotkey_source_mask *must* be zero for
3286 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003287 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003288 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003289 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003290
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003291 hotkey_orig_mask = hotkey_acpi_mask;
3292 } else {
3293 hotkey_orig_mask = hotkey_all_mask;
3294 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003295 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003296
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003297#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3298 if (dbg_wlswemul) {
3299 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003300 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003301 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003302 } else
3303#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003304 /* Not all thinkpads have a hardware radio switch */
3305 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3306 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003307 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003308 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003309 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003310 }
3311 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003312 res = add_to_attr_set(hotkey_dev_attributes,
3313 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003314
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003315 /* For X41t, X60t, X61t Tablets... */
3316 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3317 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003318 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Joe Perches0978e012011-04-04 10:06:25 -07003319 pr_info("possible tablet mode switch found; "
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003320 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003321 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003322 res = add_to_attr_set(hotkey_dev_attributes,
3323 &dev_attr_hotkey_tablet_mode.attr);
3324 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003325
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003326 if (!res)
3327 res = register_attr_set_with_sysfs(
3328 hotkey_dev_attributes,
3329 &tpacpi_pdev->dev.kobj);
3330 if (res)
3331 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003332
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003333 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003334 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3335 GFP_KERNEL);
3336 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003337 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003338 res = -ENOMEM;
3339 goto err_exit;
3340 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003341
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003342 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3343 ARRAY_SIZE(tpacpi_keymap_qtable));
3344 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3345 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3346 "using keymap number %lu\n", keymap_id);
3347
3348 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3349 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003350
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003351 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003352 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3353 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3354 tpacpi_inputdev->keycode = hotkey_keycode_map;
3355 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3356 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003357 input_set_capability(tpacpi_inputdev, EV_KEY,
3358 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003359 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003360 if (i < sizeof(hotkey_reserved_mask)*8)
3361 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003362 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003363 }
3364
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003365 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003366 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003367 input_report_switch(tpacpi_inputdev,
3368 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003369 }
3370 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003371 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003372 input_report_switch(tpacpi_inputdev,
3373 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003374 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003375
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003376 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003377 * userspace. tpacpi_detect_brightness_capabilities() must have
3378 * been called before this point */
Alex Hungff413192012-04-24 16:40:52 +08003379 if (acpi_video_backlight_support()) {
Joe Perches0978e012011-04-04 10:06:25 -07003380 pr_info("This ThinkPad has standard ACPI backlight "
3381 "brightness control, supported by the ACPI "
3382 "video driver\n");
3383 pr_notice("Disabling thinkpad-acpi brightness events "
3384 "by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003385
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003386 /* Disable brightness up/down on Lenovo thinkpads when
3387 * ACPI is handling them, otherwise it is plain impossible
3388 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003389 hotkey_reserved_mask |=
3390 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3391 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003392 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3393 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003394 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003395
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003396#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003397 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3398 & ~hotkey_all_mask
3399 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003400
3401 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3402 "hotkey source mask 0x%08x, polling freq %u\n",
3403 hotkey_source_mask, hotkey_poll_freq);
3404#endif
3405
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003406 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3407 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003408 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003409 if (res) {
3410 hotkey_exit();
3411 return res;
3412 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003413 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3414 | hotkey_driver_mask)
3415 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003416 if (res < 0 && res != -ENXIO) {
3417 hotkey_exit();
3418 return res;
3419 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003420 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3421 & ~hotkey_reserved_mask;
3422 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3423 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3424 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003425
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003426 tpacpi_inputdev->open = &hotkey_inputdev_open;
3427 tpacpi_inputdev->close = &hotkey_inputdev_close;
3428
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003429 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003430
3431 return 0;
3432
3433err_exit:
3434 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3435 hotkey_dev_attributes = NULL;
3436
3437 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003438}
3439
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003440/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3441 * mode, Web conference mode, Function mode and Lay-flat mode.
3442 * We support Home mode and Function mode currently.
3443 *
3444 * Will consider support rest of modes in future.
3445 *
3446 */
3447enum ADAPTIVE_KEY_MODE {
3448 HOME_MODE,
3449 WEB_BROWSER_MODE,
3450 WEB_CONFERENCE_MODE,
3451 FUNCTION_MODE,
3452 LAYFLAT_MODE
3453};
3454
3455const int adaptive_keyboard_modes[] = {
3456 HOME_MODE,
3457/* WEB_BROWSER_MODE = 2,
3458 WEB_CONFERENCE_MODE = 3, */
3459 FUNCTION_MODE
3460};
3461
3462#define DFR_CHANGE_ROW 0x101
3463#define DFR_SHOW_QUICKVIEW_ROW 0x102
3464
3465/* press Fn key a while second, it will switch to Function Mode. Then
3466 * release Fn key, previous mode be restored.
3467 */
3468static bool adaptive_keyboard_mode_is_saved;
3469static int adaptive_keyboard_prev_mode;
3470
3471static int adaptive_keyboard_get_next_mode(int mode)
3472{
3473 size_t i;
3474 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3475
3476 for (i = 0; i <= max_mode; i++) {
3477 if (adaptive_keyboard_modes[i] == mode)
3478 break;
3479 }
3480
3481 if (i >= max_mode)
3482 i = 0;
3483 else
3484 i++;
3485
3486 return adaptive_keyboard_modes[i];
3487}
3488
3489static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3490{
3491 u32 current_mode = 0;
3492 int new_mode = 0;
3493
3494 switch (scancode) {
3495 case DFR_CHANGE_ROW:
3496 if (adaptive_keyboard_mode_is_saved) {
3497 new_mode = adaptive_keyboard_prev_mode;
3498 adaptive_keyboard_mode_is_saved = false;
3499 } else {
3500 if (!acpi_evalf(
3501 hkey_handle, &current_mode,
3502 "GTRW", "dd", 0)) {
3503 pr_err("Cannot read adaptive keyboard mode\n");
3504 return false;
3505 } else {
3506 new_mode = adaptive_keyboard_get_next_mode(
3507 current_mode);
3508 }
3509 }
3510
3511 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3512 pr_err("Cannot set adaptive keyboard mode\n");
3513 return false;
3514 }
3515
3516 return true;
3517
3518 case DFR_SHOW_QUICKVIEW_ROW:
3519 if (!acpi_evalf(hkey_handle,
3520 &adaptive_keyboard_prev_mode,
3521 "GTRW", "dd", 0)) {
3522 pr_err("Cannot read adaptive keyboard mode\n");
3523 return false;
3524 } else {
3525 adaptive_keyboard_mode_is_saved = true;
3526
3527 if (!acpi_evalf(hkey_handle,
3528 NULL, "STRW", "vd", FUNCTION_MODE)) {
3529 pr_err("Cannot set adaptive keyboard mode\n");
3530 return false;
3531 }
3532 }
3533 return true;
3534
3535 default:
3536 return false;
3537 }
3538}
3539
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003540static bool hotkey_notify_hotkey(const u32 hkey,
3541 bool *send_acpi_ev,
3542 bool *ignore_acpi_ev)
3543{
3544 /* 0x1000-0x1FFF: key presses */
3545 unsigned int scancode = hkey & 0xfff;
3546 *send_acpi_ev = true;
3547 *ignore_acpi_ev = false;
3548
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03003549 /* HKEY event 0x1001 is scancode 0x00 */
3550 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003551 scancode--;
3552 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003553 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003554 *send_acpi_ev = false;
3555 } else {
3556 *ignore_acpi_ev = true;
3557 }
3558 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003559 } else {
3560 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003561 }
3562 return false;
3563}
3564
3565static bool hotkey_notify_wakeup(const u32 hkey,
3566 bool *send_acpi_ev,
3567 bool *ignore_acpi_ev)
3568{
3569 /* 0x2000-0x2FFF: Wakeup reason */
3570 *send_acpi_ev = true;
3571 *ignore_acpi_ev = false;
3572
3573 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003574 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3575 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003576 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3577 *ignore_acpi_ev = true;
3578 break;
3579
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003580 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3581 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003582 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3583 *ignore_acpi_ev = true;
3584 break;
3585
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003586 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3587 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003588 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003589 /* how to auto-heal: */
3590 /* 2313: woke up from S3, go to S4/S5 */
3591 /* 2413: woke up from S4, go to S5 */
3592 break;
3593
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003594 default:
3595 return false;
3596 }
3597
3598 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003599 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003600 hotkey_wakeup_reason_notify_change();
3601 }
3602 return true;
3603}
3604
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003605static bool hotkey_notify_dockevent(const u32 hkey,
3606 bool *send_acpi_ev,
3607 bool *ignore_acpi_ev)
3608{
3609 /* 0x4000-0x4FFF: dock-related events */
3610 *send_acpi_ev = true;
3611 *ignore_acpi_ev = false;
3612
3613 switch (hkey) {
3614 case TP_HKEY_EV_UNDOCK_ACK:
3615 /* ACPI undock operation completed after wakeup */
3616 hotkey_autosleep_ack = 1;
3617 pr_info("undocked\n");
3618 hotkey_wakeup_hotunplug_complete_notify_change();
3619 return true;
3620
3621 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3622 pr_info("docked into hotplug port replicator\n");
3623 return true;
3624 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3625 pr_info("undocked from hotplug port replicator\n");
3626 return true;
3627
3628 default:
3629 return false;
3630 }
3631}
3632
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003633static bool hotkey_notify_usrevent(const u32 hkey,
3634 bool *send_acpi_ev,
3635 bool *ignore_acpi_ev)
3636{
3637 /* 0x5000-0x5FFF: human interface helpers */
3638 *send_acpi_ev = true;
3639 *ignore_acpi_ev = false;
3640
3641 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003642 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3643 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003644 return true;
3645
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003646 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3647 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003648 tpacpi_input_send_tabletsw();
3649 hotkey_tablet_mode_notify_change();
3650 *send_acpi_ev = false;
3651 return true;
3652
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003653 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3654 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3655 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003656 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003657 *ignore_acpi_ev = true;
3658 return true;
3659
3660 default:
3661 return false;
3662 }
3663}
3664
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003665static void thermal_dump_all_sensors(void);
3666
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003667static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003668 bool *send_acpi_ev,
3669 bool *ignore_acpi_ev)
3670{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003671 bool known = true;
3672
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003673 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003674 *send_acpi_ev = true;
3675 *ignore_acpi_ev = false;
3676
3677 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003678 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003679 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003680 /* recommended action: do nothing, we don't have
3681 * Lenovo ATM information */
3682 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003683 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003684 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003685 /* recommended action: warn user through gui */
3686 break;
3687 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003688 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003689 /* recommended action: immediate sleep/hibernate */
3690 break;
3691 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003692 pr_crit("THERMAL ALARM: "
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003693 "a sensor reports something is too hot!\n");
3694 /* recommended action: warn user through gui, that */
3695 /* some internal component is too hot */
3696 break;
3697 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003698 pr_alert("THERMAL EMERGENCY: "
3699 "a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003700 /* recommended action: immediate sleep/hibernate */
3701 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01003702 case TP_HKEY_EV_AC_CHANGED:
3703 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3704 * AC status changed; can be triggered by plugging or
3705 * unplugging AC adapter, docking or undocking. */
3706
3707 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003708
3709 case TP_HKEY_EV_KEY_NUMLOCK:
3710 case TP_HKEY_EV_KEY_FN:
3711 /* key press events, we just ignore them as long as the EC
3712 * is still reporting them in the normal keyboard stream */
3713 *send_acpi_ev = false;
3714 *ignore_acpi_ev = true;
3715 return true;
3716
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003717 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003718 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003719 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003720 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003721
3722 thermal_dump_all_sensors();
3723
3724 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003725}
3726
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003727static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3728{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003729 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003730 bool send_acpi_ev;
3731 bool ignore_acpi_ev;
3732 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003733
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003734 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07003735 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003736 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003737 acpi_bus_generate_netlink_event(
3738 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003739 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003740 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003741 return;
3742 }
3743
3744 while (1) {
3745 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07003746 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003747 return;
3748 }
3749
3750 if (hkey == 0) {
3751 /* queue empty */
3752 return;
3753 }
3754
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003755 send_acpi_ev = true;
3756 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003757
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003758 switch (hkey >> 12) {
3759 case 1:
3760 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003761 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3762 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003763 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003764 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003765 /* 0x2000-0x2FFF: Wakeup reason */
3766 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3767 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003768 break;
3769 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003770 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003771 switch (hkey) {
3772 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003773 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07003774 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003775 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003776 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003777 break;
3778 case TP_HKEY_EV_OPTDRV_EJ:
3779 /* FIXME: kick libata if SATA link offline */
3780 known_ev = true;
3781 break;
3782 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003783 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003784 }
3785 break;
3786 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003787 /* 0x4000-0x4FFF: dock-related events */
3788 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
3789 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003790 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003791 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003792 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003793 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3794 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003795 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003796 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003797 /* 0x6000-0x6FFF: thermal alarms/notices and
3798 * keyboard events */
3799 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003800 &ignore_acpi_ev);
3801 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003802 case 7:
3803 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003804 if (tp_features.hotkey_wlsw &&
3805 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003806 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003807 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003808 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003809 break;
3810 }
3811 /* fallthrough to default */
3812 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003813 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003814 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003815 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07003816 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3817 pr_notice("please report the conditions when this "
3818 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003819 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003820
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003821 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003822 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003823 acpi_bus_generate_netlink_event(
3824 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003825 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003826 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003827 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003828 }
3829}
3830
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02003831static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003832{
Shuduo Sang330947b2014-03-27 18:06:25 +08003833 int hkeyv;
3834
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003835 /* Do these on suspend, we get the events on early resume! */
3836 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3837 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08003838
3839 /* save previous mode of adaptive keyboard of X1 Carbon */
3840 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3841 if ((hkeyv >> 8) == 2) {
3842 if (!acpi_evalf(hkey_handle,
3843 &adaptive_keyboard_prev_mode,
3844 "GTRW", "dd", 0)) {
3845 pr_err("Cannot read adaptive keyboard mode.\n");
3846 }
3847 }
3848 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003849}
3850
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003851static void hotkey_resume(void)
3852{
Shuduo Sang330947b2014-03-27 18:06:25 +08003853 int hkeyv;
3854
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003855 tpacpi_disable_brightness_delay();
3856
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003857 if (hotkey_status_set(true) < 0 ||
3858 hotkey_mask_set(hotkey_acpi_mask) < 0)
Joe Perches0978e012011-04-04 10:06:25 -07003859 pr_err("error while attempting to reset the event "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003860 "firmware interface\n");
3861
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003862 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003863 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003864 hotkey_wakeup_reason_notify_change();
3865 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003866 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08003867
3868 /* restore previous mode of adapive keyboard of X1 Carbon */
3869 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3870 if ((hkeyv >> 8) == 2) {
3871 if (!acpi_evalf(hkey_handle,
3872 NULL,
3873 "STRW", "vd",
3874 adaptive_keyboard_prev_mode)) {
3875 pr_err("Cannot set adaptive keyboard mode.\n");
3876 }
3877 }
3878 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003879}
3880
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003881/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003882static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003884 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003886 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003887 seq_printf(m, "status:\t\tnot supported\n");
3888 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003889 }
3890
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003891 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003892 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003893 res = hotkey_status_get(&status);
3894 if (!res)
3895 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003896 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003897 if (res)
3898 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003900 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003901 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003902 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3903 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003905 seq_printf(m, "mask:\t\tnot supported\n");
3906 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 }
3908
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003909 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910}
3911
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003912static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003913{
3914 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003915 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Joe Perches0978e012011-04-04 10:06:25 -07003916 pr_fmt("hotkey enable/disable functionality has been "
3917 "removed from the driver. "
3918 "Hotkeys are always enabled.\n")))
3919 pr_err("Please remove the hotkey=enable module "
3920 "parameter, it is deprecated. "
3921 "Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003922}
3923
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003924static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003926 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003927 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003930 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 return -ENODEV;
3932
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003933 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003934 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003935
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003936 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003937
3938 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 while ((cmd = next_cmd(&buf))) {
3940 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003941 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003943 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003944 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003946 mask = (hotkey_all_mask | hotkey_source_mask)
3947 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3949 /* mask set */
3950 } else if (sscanf(cmd, "%x", &mask) == 1) {
3951 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003952 } else {
3953 res = -EINVAL;
3954 goto errexit;
3955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003957
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003958 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003959 tpacpi_disclose_usertask("procfs hotkey",
3960 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003961 res = hotkey_user_mask_set(mask);
3962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003964errexit:
3965 mutex_unlock(&hotkey_mutex);
3966 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003967}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003969static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04003970 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3971 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003972 {"", 0},
3973};
3974
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003975static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003976 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003977 .notify = hotkey_notify,
3978 .handle = &hkey_handle,
3979 .type = ACPI_DEVICE_NOTIFY,
3980};
3981
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003982static struct ibm_struct hotkey_driver_data = {
3983 .name = "hotkey",
3984 .read = hotkey_read,
3985 .write = hotkey_write,
3986 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003987 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003988 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003989 .acpi = &ibm_hotkey_acpidriver,
3990};
3991
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003992/*************************************************************************
3993 * Bluetooth subdriver
3994 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003996enum {
3997 /* ACPI GBDC/SBDC bits */
3998 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3999 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004000 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004001 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004002};
4003
4004enum {
4005 /* ACPI \BLTH commands */
4006 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4007 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4008 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4009 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4010 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004011};
4012
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004013#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4014
Johannes Berg19d337d2009-06-02 13:01:37 +02004015static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004016{
4017 int status;
4018
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004019#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4020 if (dbg_bluetoothemul)
4021 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004022 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004023#endif
4024
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004025 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4026 return -EIO;
4027
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004028 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004029 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004030}
4031
Johannes Berg19d337d2009-06-02 13:01:37 +02004032static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004033{
4034 int status;
4035
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004036 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004037 "will attempt to %s bluetooth\n",
4038 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004039
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004040#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4041 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004042 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004043 return 0;
4044 }
4045#endif
4046
Johannes Berg19d337d2009-06-02 13:01:37 +02004047 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004048 status = TP_ACPI_BLUETOOTH_RADIOSSW
4049 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4050 else
4051 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004052
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004053 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4054 return -EIO;
4055
4056 return 0;
4057}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004058
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004059/* sysfs bluetooth enable ---------------------------------------------- */
4060static ssize_t bluetooth_enable_show(struct device *dev,
4061 struct device_attribute *attr,
4062 char *buf)
4063{
Johannes Berg19d337d2009-06-02 13:01:37 +02004064 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4065 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004066}
4067
4068static ssize_t bluetooth_enable_store(struct device *dev,
4069 struct device_attribute *attr,
4070 const char *buf, size_t count)
4071{
Johannes Berg19d337d2009-06-02 13:01:37 +02004072 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4073 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004074}
4075
4076static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004077 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004078 bluetooth_enable_show, bluetooth_enable_store);
4079
4080/* --------------------------------------------------------------------- */
4081
4082static struct attribute *bluetooth_attributes[] = {
4083 &dev_attr_bluetooth_enable.attr,
4084 NULL
4085};
4086
4087static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004088 .attrs = bluetooth_attributes,
4089};
4090
Johannes Berg19d337d2009-06-02 13:01:37 +02004091static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4092 .get_status = bluetooth_get_status,
4093 .set_status = bluetooth_set_status,
4094};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004095
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004096static void bluetooth_shutdown(void)
4097{
4098 /* Order firmware to save current state to NVRAM */
4099 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4100 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004101 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004102 else
4103 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004104 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004105}
4106
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004107static void bluetooth_exit(void)
4108{
4109 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4110 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004111
4112 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4113
4114 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004115}
4116
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004117static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004119 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004120 int status = 0;
4121
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004122 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4123 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004124
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004125 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004126
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004127 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4128 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004129 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004130 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004132 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4133 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004134 str_supported(tp_features.bluetooth),
4135 status);
4136
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004137#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4138 if (dbg_bluetoothemul) {
4139 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004140 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004141 } else
4142#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004143 if (tp_features.bluetooth &&
4144 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4145 /* no bluetooth hardware present in system */
4146 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004147 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004148 "bluetooth hardware not installed\n");
4149 }
4150
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004151 if (!tp_features.bluetooth)
4152 return 1;
4153
Johannes Berg19d337d2009-06-02 13:01:37 +02004154 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4155 &bluetooth_tprfk_ops,
4156 RFKILL_TYPE_BLUETOOTH,
4157 TPACPI_RFK_BLUETOOTH_SW_NAME,
4158 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004159 if (res)
4160 return res;
4161
Johannes Berg19d337d2009-06-02 13:01:37 +02004162 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4163 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004164 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004165 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004166 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004167 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004168
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170}
4171
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004172/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004173static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004175 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176}
4177
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004178static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179{
Johannes Berg19d337d2009-06-02 13:01:37 +02004180 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181}
4182
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004183static struct ibm_struct bluetooth_driver_data = {
4184 .name = "bluetooth",
4185 .read = bluetooth_read,
4186 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004187 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004188 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004189};
4190
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004191/*************************************************************************
4192 * Wan subdriver
4193 */
4194
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004195enum {
4196 /* ACPI GWAN/SWAN bits */
4197 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4198 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004199 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004200 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004201};
4202
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004203#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4204
Johannes Berg19d337d2009-06-02 13:01:37 +02004205static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004206{
4207 int status;
4208
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004209#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4210 if (dbg_wwanemul)
4211 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004212 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004213#endif
4214
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004215 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4216 return -EIO;
4217
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004218 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004219 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004220}
4221
Johannes Berg19d337d2009-06-02 13:01:37 +02004222static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004223{
4224 int status;
4225
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004226 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004227 "will attempt to %s wwan\n",
4228 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004229
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004230#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4231 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004232 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004233 return 0;
4234 }
4235#endif
4236
Johannes Berg19d337d2009-06-02 13:01:37 +02004237 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004238 status = TP_ACPI_WANCARD_RADIOSSW
4239 | TP_ACPI_WANCARD_RESUMECTRL;
4240 else
4241 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004242
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004243 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4244 return -EIO;
4245
4246 return 0;
4247}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004248
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004249/* sysfs wan enable ---------------------------------------------------- */
4250static ssize_t wan_enable_show(struct device *dev,
4251 struct device_attribute *attr,
4252 char *buf)
4253{
Johannes Berg19d337d2009-06-02 13:01:37 +02004254 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4255 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004256}
4257
4258static ssize_t wan_enable_store(struct device *dev,
4259 struct device_attribute *attr,
4260 const char *buf, size_t count)
4261{
Johannes Berg19d337d2009-06-02 13:01:37 +02004262 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4263 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004264}
4265
4266static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004267 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004268 wan_enable_show, wan_enable_store);
4269
4270/* --------------------------------------------------------------------- */
4271
4272static struct attribute *wan_attributes[] = {
4273 &dev_attr_wan_enable.attr,
4274 NULL
4275};
4276
4277static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004278 .attrs = wan_attributes,
4279};
4280
Johannes Berg19d337d2009-06-02 13:01:37 +02004281static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4282 .get_status = wan_get_status,
4283 .set_status = wan_set_status,
4284};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004285
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004286static void wan_shutdown(void)
4287{
4288 /* Order firmware to save current state to NVRAM */
4289 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4290 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004291 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004292 else
4293 vdbg_printk(TPACPI_DBG_RFKILL,
4294 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004295}
4296
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004297static void wan_exit(void)
4298{
4299 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4300 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004301
4302 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4303
4304 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004305}
4306
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004307static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004308{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004309 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004310 int status = 0;
4311
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004312 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4313 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004314
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004315 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004316
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004317 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004318 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004319
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004320 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4321 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004322 str_supported(tp_features.wan),
4323 status);
4324
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004325#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4326 if (dbg_wwanemul) {
4327 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004328 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004329 } else
4330#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004331 if (tp_features.wan &&
4332 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4333 /* no wan hardware present in system */
4334 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004335 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004336 "wan hardware not installed\n");
4337 }
4338
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004339 if (!tp_features.wan)
4340 return 1;
4341
Johannes Berg19d337d2009-06-02 13:01:37 +02004342 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4343 &wan_tprfk_ops,
4344 RFKILL_TYPE_WWAN,
4345 TPACPI_RFK_WWAN_SW_NAME,
4346 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004347 if (res)
4348 return res;
4349
Johannes Berg19d337d2009-06-02 13:01:37 +02004350 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4351 &wan_attr_group);
4352
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004353 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004354 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004355 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004356 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004357
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004358 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004359}
4360
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004361/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004362static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004363{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004364 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004365}
4366
4367static int wan_write(char *buf)
4368{
Johannes Berg19d337d2009-06-02 13:01:37 +02004369 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004370}
4371
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004372static struct ibm_struct wan_driver_data = {
4373 .name = "wan",
4374 .read = wan_read,
4375 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004376 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004377 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004378};
4379
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004380/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004381 * UWB subdriver
4382 */
4383
4384enum {
4385 /* ACPI GUWB/SUWB bits */
4386 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4387 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4388};
4389
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004390#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4391
Johannes Berg19d337d2009-06-02 13:01:37 +02004392static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004393{
4394 int status;
4395
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004396#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4397 if (dbg_uwbemul)
4398 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004399 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004400#endif
4401
4402 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4403 return -EIO;
4404
4405 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004406 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004407}
4408
Johannes Berg19d337d2009-06-02 13:01:37 +02004409static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004410{
4411 int status;
4412
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004413 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004414 "will attempt to %s UWB\n",
4415 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004416
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004417#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4418 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004419 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004420 return 0;
4421 }
4422#endif
4423
Johannes Berg19d337d2009-06-02 13:01:37 +02004424 if (state == TPACPI_RFK_RADIO_ON)
4425 status = TP_ACPI_UWB_RADIOSSW;
4426 else
4427 status = 0;
4428
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004429 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4430 return -EIO;
4431
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004432 return 0;
4433}
4434
4435/* --------------------------------------------------------------------- */
4436
Johannes Berg19d337d2009-06-02 13:01:37 +02004437static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4438 .get_status = uwb_get_status,
4439 .set_status = uwb_set_status,
4440};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004441
4442static void uwb_exit(void)
4443{
Johannes Berg19d337d2009-06-02 13:01:37 +02004444 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004445}
4446
4447static int __init uwb_init(struct ibm_init_struct *iibm)
4448{
4449 int res;
4450 int status = 0;
4451
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004452 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4453 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004454
4455 TPACPI_ACPIHANDLE_INIT(hkey);
4456
4457 tp_features.uwb = hkey_handle &&
4458 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4459
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004460 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4461 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004462 str_supported(tp_features.uwb),
4463 status);
4464
4465#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4466 if (dbg_uwbemul) {
4467 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004468 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004469 } else
4470#endif
4471 if (tp_features.uwb &&
4472 !(status & TP_ACPI_UWB_HWPRESENT)) {
4473 /* no uwb hardware present in system */
4474 tp_features.uwb = 0;
4475 dbg_printk(TPACPI_DBG_INIT,
4476 "uwb hardware not installed\n");
4477 }
4478
4479 if (!tp_features.uwb)
4480 return 1;
4481
4482 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004483 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004484 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004485 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004486 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004487 return res;
4488}
4489
4490static struct ibm_struct uwb_driver_data = {
4491 .name = "uwb",
4492 .exit = uwb_exit,
4493 .flags.experimental = 1,
4494};
4495
4496/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004497 * Video subdriver
4498 */
4499
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004500#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4501
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004502enum video_access_mode {
4503 TPACPI_VIDEO_NONE = 0,
4504 TPACPI_VIDEO_570, /* 570 */
4505 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4506 TPACPI_VIDEO_NEW, /* all others */
4507};
4508
4509enum { /* video status flags, based on VIDEO_570 */
4510 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4511 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4512 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4513};
4514
4515enum { /* TPACPI_VIDEO_570 constants */
4516 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4517 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4518 * video_status_flags */
4519 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4520 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4521};
4522
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004523static enum video_access_mode video_supported;
4524static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004525
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004526static int video_autosw_get(void);
4527static int video_autosw_set(int enable);
4528
Joe Perches112a6ee2011-04-04 10:06:24 -07004529TPACPI_HANDLE(vid, root,
4530 "\\_SB.PCI.AGP.VGA", /* 570 */
4531 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4532 "\\_SB.PCI0.VID0", /* 770e */
4533 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4534 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4535 "\\_SB.PCI0.AGP.VID", /* all others */
4536 ); /* R30, R31 */
4537
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004538TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004539
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004540static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004542 int ivga;
4543
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004544 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4545
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004546 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004547 if (tpacpi_is_ibm())
4548 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004549
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004550 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4551 /* G41, assume IVGA doesn't change */
4552 vid_handle = vid2_handle;
4553
4554 if (!vid_handle)
4555 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004556 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004557 else if (tpacpi_is_ibm() &&
4558 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004559 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004560 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004561 else if (tpacpi_is_ibm() &&
4562 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004563 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004564 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004565 else
4566 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004567 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004569 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4570 str_supported(video_supported != TPACPI_VIDEO_NONE),
4571 video_supported);
4572
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004573 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574}
4575
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004576static void video_exit(void)
4577{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004578 dbg_printk(TPACPI_DBG_EXIT,
4579 "restoring original video autoswitch mode\n");
4580 if (video_autosw_set(video_orig_autosw))
Joe Perches0978e012011-04-04 10:06:25 -07004581 pr_err("error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004582 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004583}
4584
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004585static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586{
4587 int status = 0;
4588 int i;
4589
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004590 switch (video_supported) {
4591 case TPACPI_VIDEO_570:
4592 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4593 TP_ACPI_VIDEO_570_PHSCMD))
4594 return -EIO;
4595 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4596 break;
4597 case TPACPI_VIDEO_770:
4598 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4599 return -EIO;
4600 if (i)
4601 status |= TP_ACPI_VIDEO_S_LCD;
4602 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4603 return -EIO;
4604 if (i)
4605 status |= TP_ACPI_VIDEO_S_CRT;
4606 break;
4607 case TPACPI_VIDEO_NEW:
4608 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4609 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4610 return -EIO;
4611 if (i)
4612 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004614 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4615 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4616 return -EIO;
4617 if (i)
4618 status |= TP_ACPI_VIDEO_S_LCD;
4619 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4620 return -EIO;
4621 if (i)
4622 status |= TP_ACPI_VIDEO_S_DVI;
4623 break;
4624 default:
4625 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
4628 return status;
4629}
4630
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004631static int video_outputsw_set(int status)
4632{
4633 int autosw;
4634 int res = 0;
4635
4636 switch (video_supported) {
4637 case TPACPI_VIDEO_570:
4638 res = acpi_evalf(NULL, NULL,
4639 "\\_SB.PHS2", "vdd",
4640 TP_ACPI_VIDEO_570_PHS2CMD,
4641 status | TP_ACPI_VIDEO_570_PHS2SET);
4642 break;
4643 case TPACPI_VIDEO_770:
4644 autosw = video_autosw_get();
4645 if (autosw < 0)
4646 return autosw;
4647
4648 res = video_autosw_set(1);
4649 if (res)
4650 return res;
4651 res = acpi_evalf(vid_handle, NULL,
4652 "ASWT", "vdd", status * 0x100, 0);
4653 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004654 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004655 return -EIO;
4656 }
4657 break;
4658 case TPACPI_VIDEO_NEW:
4659 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004660 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004661 break;
4662 default:
4663 return -ENOSYS;
4664 }
4665
4666 return (res)? 0 : -EIO;
4667}
4668
4669static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004671 int autosw = 0;
4672
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004673 switch (video_supported) {
4674 case TPACPI_VIDEO_570:
4675 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4676 return -EIO;
4677 break;
4678 case TPACPI_VIDEO_770:
4679 case TPACPI_VIDEO_NEW:
4680 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4681 return -EIO;
4682 break;
4683 default:
4684 return -ENOSYS;
4685 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004686
4687 return autosw & 1;
4688}
4689
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004690static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004691{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004692 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004693 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004694 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004695}
4696
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004697static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004698{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004699 int autosw = video_autosw_get();
4700 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004701
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004702 if (autosw < 0)
4703 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004704
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004705 switch (video_supported) {
4706 case TPACPI_VIDEO_570:
4707 res = video_autosw_set(1);
4708 if (res)
4709 return res;
4710 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4711 break;
4712 case TPACPI_VIDEO_770:
4713 case TPACPI_VIDEO_NEW:
4714 res = video_autosw_set(1);
4715 if (res)
4716 return res;
4717 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4718 break;
4719 default:
4720 return -ENOSYS;
4721 }
4722 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004723 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004724 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004725 }
4726
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004727 return (res)? 0 : -EIO;
4728}
4729
4730static int video_expand_toggle(void)
4731{
4732 switch (video_supported) {
4733 case TPACPI_VIDEO_570:
4734 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4735 0 : -EIO;
4736 case TPACPI_VIDEO_770:
4737 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4738 0 : -EIO;
4739 case TPACPI_VIDEO_NEW:
4740 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4741 0 : -EIO;
4742 default:
4743 return -ENOSYS;
4744 }
4745 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004746}
4747
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004748static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004749{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004750 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004751
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004752 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004753 seq_printf(m, "status:\t\tnot supported\n");
4754 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004755 }
4756
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004757 /* Even reads can crash X.org, so... */
4758 if (!capable(CAP_SYS_ADMIN))
4759 return -EPERM;
4760
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004761 status = video_outputsw_get();
4762 if (status < 0)
4763 return status;
4764
4765 autosw = video_autosw_get();
4766 if (autosw < 0)
4767 return autosw;
4768
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004769 seq_printf(m, "status:\t\tsupported\n");
4770 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4771 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004772 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004773 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4774 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4775 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4776 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004777 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004778 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4779 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4780 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004781
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004782 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004783}
4784
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004785static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786{
4787 char *cmd;
4788 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004789 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004791 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004792 return -ENODEV;
4793
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004794 /* Even reads can crash X.org, let alone writes... */
4795 if (!capable(CAP_SYS_ADMIN))
4796 return -EPERM;
4797
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004798 enable = 0;
4799 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
4801 while ((cmd = next_cmd(&buf))) {
4802 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004803 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004805 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004807 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004809 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004810 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004811 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004812 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004813 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004814 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004815 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004817 res = video_autosw_set(1);
4818 if (res)
4819 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004821 res = video_autosw_set(0);
4822 if (res)
4823 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004825 res = video_outputsw_cycle();
4826 if (res)
4827 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004829 res = video_expand_toggle();
4830 if (res)
4831 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 } else
4833 return -EINVAL;
4834 }
4835
4836 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004837 status = video_outputsw_get();
4838 if (status < 0)
4839 return status;
4840 res = video_outputsw_set((status & ~disable) | enable);
4841 if (res)
4842 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 }
4844
4845 return 0;
4846}
4847
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004848static struct ibm_struct video_driver_data = {
4849 .name = "video",
4850 .read = video_read,
4851 .write = video_write,
4852 .exit = video_exit,
4853};
4854
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004855#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4856
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004857/*************************************************************************
4858 * Light (thinklight) subdriver
4859 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004861TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4862TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004863
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004864static int light_get_status(void)
4865{
4866 int status = 0;
4867
4868 if (tp_features.light_status) {
4869 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4870 return -EIO;
4871 return (!!status);
4872 }
4873
4874 return -ENXIO;
4875}
4876
4877static int light_set_status(int status)
4878{
4879 int rc;
4880
4881 if (tp_features.light) {
4882 if (cmos_handle) {
4883 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4884 (status)?
4885 TP_CMOS_THINKLIGHT_ON :
4886 TP_CMOS_THINKLIGHT_OFF);
4887 } else {
4888 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4889 (status)? 1 : 0);
4890 }
4891 return (rc)? 0 : -EIO;
4892 }
4893
4894 return -ENXIO;
4895}
4896
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004897static void light_set_status_worker(struct work_struct *work)
4898{
4899 struct tpacpi_led_classdev *data =
4900 container_of(work, struct tpacpi_led_classdev, work);
4901
4902 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004903 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004904}
4905
4906static void light_sysfs_set(struct led_classdev *led_cdev,
4907 enum led_brightness brightness)
4908{
4909 struct tpacpi_led_classdev *data =
4910 container_of(led_cdev,
4911 struct tpacpi_led_classdev,
4912 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004913 data->new_state = (brightness != LED_OFF) ?
4914 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004915 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004916}
4917
4918static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4919{
4920 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4921}
4922
4923static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4924 .led_classdev = {
4925 .name = "tpacpi::thinklight",
4926 .brightness_set = &light_sysfs_set,
4927 .brightness_get = &light_sysfs_get,
4928 }
4929};
4930
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004931static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004933 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004934
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004935 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4936
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004937 if (tpacpi_is_ibm()) {
4938 TPACPI_ACPIHANDLE_INIT(ledb);
4939 TPACPI_ACPIHANDLE_INIT(lght);
4940 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004941 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004942 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004943
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004944 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004945 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004946
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004947 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004948 /* light status not supported on
4949 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004950 tp_features.light_status =
4951 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004953 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4954 str_supported(tp_features.light),
4955 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004956
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004957 if (!tp_features.light)
4958 return 1;
4959
4960 rc = led_classdev_register(&tpacpi_pdev->dev,
4961 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004962
4963 if (rc < 0) {
4964 tp_features.light = 0;
4965 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004966 } else {
4967 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004968 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004969
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004970 return rc;
4971}
4972
4973static void light_exit(void)
4974{
4975 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Tejun Heo6d394e12012-12-21 17:56:58 -08004976 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977}
4978
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004979static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004981 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004983 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004984 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004985 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004986 seq_printf(m, "status:\t\tunknown\n");
4987 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004988 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004989 status = light_get_status();
4990 if (status < 0)
4991 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004992 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4993 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997}
4998
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004999static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005002 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005003
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005004 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005005 return -ENODEV;
5006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 while ((cmd = next_cmd(&buf))) {
5008 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005009 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005011 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 } else
5013 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 }
5015
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005016 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017}
5018
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005019static struct ibm_struct light_driver_data = {
5020 .name = "light",
5021 .read = light_read,
5022 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005023 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005024};
5025
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005026/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005027 * CMOS subdriver
5028 */
5029
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005030/* sysfs cmos_command -------------------------------------------------- */
5031static ssize_t cmos_command_store(struct device *dev,
5032 struct device_attribute *attr,
5033 const char *buf, size_t count)
5034{
5035 unsigned long cmos_cmd;
5036 int res;
5037
5038 if (parse_strtoul(buf, 21, &cmos_cmd))
5039 return -EINVAL;
5040
5041 res = issue_thinkpad_cmos_command(cmos_cmd);
5042 return (res)? res : count;
5043}
5044
5045static struct device_attribute dev_attr_cmos_command =
5046 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
5047
5048/* --------------------------------------------------------------------- */
5049
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005050static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005051{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005052 int res;
5053
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005054 vdbg_printk(TPACPI_DBG_INIT,
5055 "initializing cmos commands subdriver\n");
5056
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005057 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005058
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005059 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5060 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005061
5062 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5063 if (res)
5064 return res;
5065
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005066 return (cmos_handle)? 0 : 1;
5067}
5068
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005069static void cmos_exit(void)
5070{
5071 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5072}
5073
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005074static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005076 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5077 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005079 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005081 seq_printf(m, "status:\t\tsupported\n");
5082 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 }
5084
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086}
5087
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005088static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089{
5090 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005091 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092
5093 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005094 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5095 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 /* cmos_cmd set */
5097 } else
5098 return -EINVAL;
5099
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005100 res = issue_thinkpad_cmos_command(cmos_cmd);
5101 if (res)
5102 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 }
5104
5105 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005106}
5107
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005108static struct ibm_struct cmos_driver_data = {
5109 .name = "cmos",
5110 .read = cmos_read,
5111 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005112 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005113};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005114
5115/*************************************************************************
5116 * LED subdriver
5117 */
5118
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005119enum led_access_mode {
5120 TPACPI_LED_NONE = 0,
5121 TPACPI_LED_570, /* 570 */
5122 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5123 TPACPI_LED_NEW, /* all others */
5124};
5125
5126enum { /* For TPACPI_LED_OLD */
5127 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5128 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5129 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5130};
5131
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005132static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005133
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005134static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005135
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005136#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005137static struct tpacpi_led_classdev *tpacpi_leds;
5138static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005139static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005140 /* there's a limit of 19 chars + NULL before 2.6.26 */
5141 "tpacpi::power",
5142 "tpacpi:orange:batt",
5143 "tpacpi:green:batt",
5144 "tpacpi::dock_active",
5145 "tpacpi::bay_active",
5146 "tpacpi::dock_batt",
5147 "tpacpi::unknown_led",
5148 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005149 "tpacpi::dock_status1",
5150 "tpacpi::dock_status2",
5151 "tpacpi::unknown_led2",
5152 "tpacpi::unknown_led3",
5153 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005154};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005155#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005156
5157static inline bool tpacpi_is_led_restricted(const unsigned int led)
5158{
5159#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5160 return false;
5161#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005162 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005163#endif
5164}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005165
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005166static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005167{
5168 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005169 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005170
5171 switch (led_supported) {
5172 case TPACPI_LED_570:
5173 if (!acpi_evalf(ec_handle,
5174 &status, "GLED", "dd", 1 << led))
5175 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005176 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005177 TPACPI_LED_OFF :
5178 ((status == 1)?
5179 TPACPI_LED_ON :
5180 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005181 tpacpi_led_state_cache[led] = led_s;
5182 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005183 default:
5184 return -ENXIO;
5185 }
5186
5187 /* not reached */
5188}
5189
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005190static int led_set_status(const unsigned int led,
5191 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005192{
5193 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005194 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5195 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005196
5197 int rc = 0;
5198
5199 switch (led_supported) {
5200 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005201 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005202 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005203 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005204 if (unlikely(tpacpi_is_led_restricted(led)))
5205 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005206 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5207 (1 << led), led_sled_arg1[ledstatus]))
5208 rc = -EIO;
5209 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005210 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005211 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005212 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005213 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005214 if (unlikely(tpacpi_is_led_restricted(led)))
5215 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005216 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5217 if (rc >= 0)
5218 rc = ec_write(TPACPI_LED_EC_HLBL,
5219 (ledstatus == TPACPI_LED_BLINK) << led);
5220 if (rc >= 0)
5221 rc = ec_write(TPACPI_LED_EC_HLCL,
5222 (ledstatus != TPACPI_LED_OFF) << led);
5223 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005224 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005225 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005226 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5227 return -EINVAL;
5228 if (unlikely(tpacpi_is_led_restricted(led)))
5229 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005230 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5231 led, led_led_arg1[ledstatus]))
5232 rc = -EIO;
5233 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005234 default:
5235 rc = -ENXIO;
5236 }
5237
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005238 if (!rc)
5239 tpacpi_led_state_cache[led] = ledstatus;
5240
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005241 return rc;
5242}
5243
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005244static void led_set_status_worker(struct work_struct *work)
5245{
5246 struct tpacpi_led_classdev *data =
5247 container_of(work, struct tpacpi_led_classdev, work);
5248
5249 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005250 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005251}
5252
5253static void led_sysfs_set(struct led_classdev *led_cdev,
5254 enum led_brightness brightness)
5255{
5256 struct tpacpi_led_classdev *data = container_of(led_cdev,
5257 struct tpacpi_led_classdev, led_classdev);
5258
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005259 if (brightness == LED_OFF)
5260 data->new_state = TPACPI_LED_OFF;
5261 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5262 data->new_state = TPACPI_LED_ON;
5263 else
5264 data->new_state = TPACPI_LED_BLINK;
5265
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005266 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005267}
5268
5269static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5270 unsigned long *delay_on, unsigned long *delay_off)
5271{
5272 struct tpacpi_led_classdev *data = container_of(led_cdev,
5273 struct tpacpi_led_classdev, led_classdev);
5274
5275 /* Can we choose the flash rate? */
5276 if (*delay_on == 0 && *delay_off == 0) {
5277 /* yes. set them to the hardware blink rate (1 Hz) */
5278 *delay_on = 500; /* ms */
5279 *delay_off = 500; /* ms */
5280 } else if ((*delay_on != 500) || (*delay_off != 500))
5281 return -EINVAL;
5282
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005283 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005284 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005285
5286 return 0;
5287}
5288
5289static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5290{
5291 int rc;
5292
5293 struct tpacpi_led_classdev *data = container_of(led_cdev,
5294 struct tpacpi_led_classdev, led_classdev);
5295
5296 rc = led_get_status(data->led);
5297
5298 if (rc == TPACPI_LED_OFF || rc < 0)
5299 rc = LED_OFF; /* no error handling in led class :( */
5300 else
5301 rc = LED_FULL;
5302
5303 return rc;
5304}
5305
5306static void led_exit(void)
5307{
5308 unsigned int i;
5309
5310 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5311 if (tpacpi_leds[i].led_classdev.name)
5312 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5313 }
5314
Li Dongyange03e3892012-07-25 10:45:07 +10005315 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005316 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005317}
5318
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005319static int __init tpacpi_init_led(unsigned int led)
5320{
5321 int rc;
5322
5323 tpacpi_leds[led].led = led;
5324
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005325 /* LEDs with no name don't get registered */
5326 if (!tpacpi_led_names[led])
5327 return 0;
5328
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005329 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5330 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5331 if (led_supported == TPACPI_LED_570)
5332 tpacpi_leds[led].led_classdev.brightness_get =
5333 &led_sysfs_get;
5334
5335 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5336
5337 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5338
5339 rc = led_classdev_register(&tpacpi_pdev->dev,
5340 &tpacpi_leds[led].led_classdev);
5341 if (rc < 0)
5342 tpacpi_leds[led].led_classdev.name = NULL;
5343
5344 return rc;
5345}
5346
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005347static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5348 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5349 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5350 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5351
5352 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5353 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5354 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5355 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5356 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5357 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5358 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5359 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5360
5361 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5362 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5363 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5364 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5365 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5366
5367 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5368 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5369 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5370 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5371
5372 /* (1) - may have excess leds enabled on MSB */
5373
5374 /* Defaults (order matters, keep last, don't reorder!) */
5375 { /* Lenovo */
5376 .vendor = PCI_VENDOR_ID_LENOVO,
5377 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5378 .quirks = 0x1fffU,
5379 },
5380 { /* IBM ThinkPads with no EC version string */
5381 .vendor = PCI_VENDOR_ID_IBM,
5382 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5383 .quirks = 0x00ffU,
5384 },
5385 { /* IBM ThinkPads with EC version string */
5386 .vendor = PCI_VENDOR_ID_IBM,
5387 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5388 .quirks = 0x00bfU,
5389 },
5390};
5391
5392#undef TPACPI_LEDQ_IBM
5393#undef TPACPI_LEDQ_LNV
5394
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005395static enum led_access_mode __init led_init_detect_mode(void)
5396{
5397 acpi_status status;
5398
5399 if (tpacpi_is_ibm()) {
5400 /* 570 */
5401 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5402 if (ACPI_SUCCESS(status))
5403 return TPACPI_LED_570;
5404
5405 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5406 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5407 if (ACPI_SUCCESS(status))
5408 return TPACPI_LED_OLD;
5409 }
5410
5411 /* most others */
5412 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5413 if (ACPI_SUCCESS(status))
5414 return TPACPI_LED_NEW;
5415
5416 /* R30, R31, and unknown firmwares */
5417 led_handle = NULL;
5418 return TPACPI_LED_NONE;
5419}
5420
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005421static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005422{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005423 unsigned int i;
5424 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005425 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005426
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005427 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5428
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005429 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005430
Adam Leefcb44e12013-06-07 16:20:08 +08005431 if (led_supported != TPACPI_LED_NONE) {
5432 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5433 ARRAY_SIZE(led_useful_qtable));
5434
5435 if (!useful_leds) {
5436 led_handle = NULL;
5437 led_supported = TPACPI_LED_NONE;
5438 }
5439 }
5440
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005441 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5442 str_supported(led_supported), led_supported);
5443
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005444 if (led_supported == TPACPI_LED_NONE)
5445 return 1;
5446
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005447 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5448 GFP_KERNEL);
5449 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005450 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005451 return -ENOMEM;
5452 }
5453
5454 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08005455 tpacpi_leds[i].led = -1;
5456
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005457 if (!tpacpi_is_led_restricted(i) &&
5458 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005459 rc = tpacpi_init_led(i);
5460 if (rc < 0) {
5461 led_exit();
5462 return rc;
5463 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005464 }
5465 }
5466
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005467#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Joe Perches0978e012011-04-04 10:06:25 -07005468 pr_notice("warning: userspace override of important "
5469 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005470#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005471 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005472}
5473
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005474#define str_led_status(s) \
5475 ((s) == TPACPI_LED_OFF ? "off" : \
5476 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005477
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005478static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005480 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005481 seq_printf(m, "status:\t\tnot supported\n");
5482 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005483 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005484 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005485
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005486 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005487 /* 570 */
5488 int i, status;
5489 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005490 status = led_get_status(i);
5491 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005492 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005493 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005494 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005495 }
5496 }
5497
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005498 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005499 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502}
5503
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005504static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505{
5506 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005507 int led, rc;
5508 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005509
5510 if (!led_supported)
5511 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
5513 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08005514 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 return -EINVAL;
5516
Adam Leeedf2d772013-06-08 16:51:15 +08005517 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5518 tpacpi_leds[led].led < 0)
5519 return -ENODEV;
5520
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005521 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005522 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005524 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005525 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005526 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005527 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005528 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005529 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005530
5531 rc = led_set_status(led, s);
5532 if (rc < 0)
5533 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 }
5535
5536 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005537}
5538
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005539static struct ibm_struct led_driver_data = {
5540 .name = "led",
5541 .read = led_read,
5542 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005543 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005544};
5545
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005546/*************************************************************************
5547 * Beep subdriver
5548 */
5549
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005550TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005551
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005552#define TPACPI_BEEP_Q1 0x0001
5553
5554static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5555 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5556 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5557};
5558
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005559static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005560{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005561 unsigned long quirks;
5562
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005563 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5564
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005565 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005566
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005567 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5568 str_supported(beep_handle != NULL));
5569
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005570 quirks = tpacpi_check_quirks(beep_quirk_table,
5571 ARRAY_SIZE(beep_quirk_table));
5572
5573 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5574
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005575 return (beep_handle)? 0 : 1;
5576}
5577
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005578static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005580 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005581 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005582 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005583 seq_printf(m, "status:\t\tsupported\n");
5584 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588}
5589
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005590static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591{
5592 char *cmd;
5593 int beep_cmd;
5594
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005595 if (!beep_handle)
5596 return -ENODEV;
5597
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005599 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5600 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 /* beep_cmd set */
5602 } else
5603 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005604 if (tp_features.beep_needs_two_args) {
5605 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5606 beep_cmd, 0))
5607 return -EIO;
5608 } else {
5609 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5610 beep_cmd))
5611 return -EIO;
5612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 }
5614
5615 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005616}
5617
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005618static struct ibm_struct beep_driver_data = {
5619 .name = "beep",
5620 .read = beep_read,
5621 .write = beep_write,
5622};
5623
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005624/*************************************************************************
5625 * Thermal subdriver
5626 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005627
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005628enum thermal_access_mode {
5629 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5630 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5631 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5632 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5633 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5634};
5635
5636enum { /* TPACPI_THERMAL_TPEC_* */
5637 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5638 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5639 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005640
5641 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005642};
5643
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005644
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005645#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5646struct ibm_thermal_sensors_struct {
5647 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5648};
5649
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005650static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005651
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005652/* idx is zero-based */
5653static int thermal_get_sensor(int idx, s32 *value)
5654{
5655 int t;
5656 s8 tmp;
5657 char tmpi[5];
5658
5659 t = TP_EC_THERMAL_TMP0;
5660
5661 switch (thermal_read_mode) {
5662#if TPACPI_MAX_THERMAL_SENSORS >= 16
5663 case TPACPI_THERMAL_TPEC_16:
5664 if (idx >= 8 && idx <= 15) {
5665 t = TP_EC_THERMAL_TMP8;
5666 idx -= 8;
5667 }
5668 /* fallthrough */
5669#endif
5670 case TPACPI_THERMAL_TPEC_8:
5671 if (idx <= 7) {
5672 if (!acpi_ec_read(t + idx, &tmp))
5673 return -EIO;
5674 *value = tmp * 1000;
5675 return 0;
5676 }
5677 break;
5678
5679 case TPACPI_THERMAL_ACPI_UPDT:
5680 if (idx <= 7) {
5681 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5682 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5683 return -EIO;
5684 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5685 return -EIO;
5686 *value = (t - 2732) * 100;
5687 return 0;
5688 }
5689 break;
5690
5691 case TPACPI_THERMAL_ACPI_TMP07:
5692 if (idx <= 7) {
5693 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5694 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5695 return -EIO;
5696 if (t > 127 || t < -127)
5697 t = TP_EC_THERMAL_TMP_NA;
5698 *value = t * 1000;
5699 return 0;
5700 }
5701 break;
5702
5703 case TPACPI_THERMAL_NONE:
5704 default:
5705 return -ENOSYS;
5706 }
5707
5708 return -EINVAL;
5709}
5710
5711static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5712{
5713 int res, i;
5714 int n;
5715
5716 n = 8;
5717 i = 0;
5718
5719 if (!s)
5720 return -EINVAL;
5721
5722 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5723 n = 16;
5724
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005725 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005726 res = thermal_get_sensor(i, &s->temp[i]);
5727 if (res)
5728 return res;
5729 }
5730
5731 return n;
5732}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005733
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005734static void thermal_dump_all_sensors(void)
5735{
5736 int n, i;
5737 struct ibm_thermal_sensors_struct t;
5738
5739 n = thermal_get_sensors(&t);
5740 if (n <= 0)
5741 return;
5742
Joe Perches0978e012011-04-04 10:06:25 -07005743 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005744
5745 for (i = 0; i < n; i++) {
5746 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07005747 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005748 else
Joe Perches0978e012011-04-04 10:06:25 -07005749 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005750 }
5751
Joe Perches0978e012011-04-04 10:06:25 -07005752 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005753}
5754
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005755/* sysfs temp##_input -------------------------------------------------- */
5756
5757static ssize_t thermal_temp_input_show(struct device *dev,
5758 struct device_attribute *attr,
5759 char *buf)
5760{
5761 struct sensor_device_attribute *sensor_attr =
5762 to_sensor_dev_attr(attr);
5763 int idx = sensor_attr->index;
5764 s32 value;
5765 int res;
5766
5767 res = thermal_get_sensor(idx, &value);
5768 if (res)
5769 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005770 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005771 return -ENXIO;
5772
5773 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5774}
5775
5776#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005777 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5778 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005779
5780static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5781 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5782 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5783 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5784 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5785 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5786 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5787 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5788 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5789 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5790 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5791 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5792 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5793 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5794 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5795 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5796 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5797};
5798
5799#define THERMAL_ATTRS(X) \
5800 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5801
5802static struct attribute *thermal_temp_input_attr[] = {
5803 THERMAL_ATTRS(8),
5804 THERMAL_ATTRS(9),
5805 THERMAL_ATTRS(10),
5806 THERMAL_ATTRS(11),
5807 THERMAL_ATTRS(12),
5808 THERMAL_ATTRS(13),
5809 THERMAL_ATTRS(14),
5810 THERMAL_ATTRS(15),
5811 THERMAL_ATTRS(0),
5812 THERMAL_ATTRS(1),
5813 THERMAL_ATTRS(2),
5814 THERMAL_ATTRS(3),
5815 THERMAL_ATTRS(4),
5816 THERMAL_ATTRS(5),
5817 THERMAL_ATTRS(6),
5818 THERMAL_ATTRS(7),
5819 NULL
5820};
5821
5822static const struct attribute_group thermal_temp_input16_group = {
5823 .attrs = thermal_temp_input_attr
5824};
5825
5826static const struct attribute_group thermal_temp_input8_group = {
5827 .attrs = &thermal_temp_input_attr[8]
5828};
5829
5830#undef THERMAL_SENSOR_ATTR_TEMP
5831#undef THERMAL_ATTRS
5832
5833/* --------------------------------------------------------------------- */
5834
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005835static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005836{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005837 u8 t, ta1, ta2;
5838 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005839 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005840 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005841
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005842 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5843
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005844 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005845
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005846 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005847 /*
5848 * Direct EC access mode: sensors at registers
5849 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5850 * non-implemented, thermal sensors return 0x80 when
5851 * not available
5852 */
5853
5854 ta1 = ta2 = 0;
5855 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005856 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005857 ta1 |= t;
5858 } else {
5859 ta1 = 0;
5860 break;
5861 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005862 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005863 ta2 |= t;
5864 } else {
5865 ta1 = 0;
5866 break;
5867 }
5868 }
5869 if (ta1 == 0) {
5870 /* This is sheer paranoia, but we handle it anyway */
5871 if (acpi_tmp7) {
Joe Perches0978e012011-04-04 10:06:25 -07005872 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005873 "falling back to ACPI TMPx access "
5874 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005875 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005876 } else {
Joe Perches0978e012011-04-04 10:06:25 -07005877 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005878 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005879 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005880 }
5881 } else {
5882 thermal_read_mode =
5883 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005884 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005885 }
5886 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005887 if (tpacpi_is_ibm() &&
5888 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005889 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005890 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005891 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005892 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005893 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005894 }
5895 } else {
5896 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005897 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005898 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005899
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005900 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5901 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5902 thermal_read_mode);
5903
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005904 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005905 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005906 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005907 &thermal_temp_input16_group);
5908 if (res)
5909 return res;
5910 break;
5911 case TPACPI_THERMAL_TPEC_8:
5912 case TPACPI_THERMAL_ACPI_TMP07:
5913 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005914 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005915 &thermal_temp_input8_group);
5916 if (res)
5917 return res;
5918 break;
5919 case TPACPI_THERMAL_NONE:
5920 default:
5921 return 1;
5922 }
5923
5924 return 0;
5925}
5926
5927static void thermal_exit(void)
5928{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005929 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005930 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005931 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005932 &thermal_temp_input16_group);
5933 break;
5934 case TPACPI_THERMAL_TPEC_8:
5935 case TPACPI_THERMAL_ACPI_TMP07:
5936 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005937 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08005938 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005939 break;
5940 case TPACPI_THERMAL_NONE:
5941 default:
5942 break;
5943 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005944}
5945
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005946static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005947{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005948 int n, i;
5949 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005950
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005951 n = thermal_get_sensors(&t);
5952 if (unlikely(n < 0))
5953 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005954
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005955 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005956
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005957 if (n > 0) {
5958 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005959 seq_printf(m, "%d ", t.temp[i] / 1000);
5960 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005961 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005962 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005963
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005964 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005965}
5966
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005967static struct ibm_struct thermal_driver_data = {
5968 .name = "thermal",
5969 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005970 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005971};
5972
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005973/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005974 * Backlight/brightness subdriver
5975 */
Holger Macht8acb0252006-10-20 14:30:28 -07005976
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005977#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5978
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005979/*
5980 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5981 * CMOS NVRAM byte 0x5E, bits 0-3.
5982 *
5983 * EC HBRV (0x31) has the following layout
5984 * Bit 7: unknown function
5985 * Bit 6: unknown function
5986 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5987 * Bit 4: must be set to zero to avoid problems
5988 * Bit 3-0: backlight brightness level
5989 *
5990 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005991 *
5992 * WARNING: The X61 has been verified to use HBRV for something else, so
5993 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5994 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005995 */
5996
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005997enum {
5998 TP_EC_BACKLIGHT = 0x31,
5999
6000 /* TP_EC_BACKLIGHT bitmasks */
6001 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6002 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6003 TP_EC_BACKLIGHT_MAPSW = 0x20,
6004};
6005
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006006enum tpacpi_brightness_access_mode {
6007 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6008 TPACPI_BRGHT_MODE_EC, /* EC control */
6009 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6010 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6011 TPACPI_BRGHT_MODE_MAX
6012};
6013
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03006014static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006015
6016static enum tpacpi_brightness_access_mode brightness_mode =
6017 TPACPI_BRGHT_MODE_MAX;
6018
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006019static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6020
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006021static struct mutex brightness_mutex;
6022
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006023/* NVRAM brightness access,
6024 * call with brightness_mutex held! */
6025static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006026{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006027 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006028
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006029 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6030 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6031 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006032 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006033
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006034 return lnvram;
6035}
6036
6037static void tpacpi_brightness_checkpoint_nvram(void)
6038{
6039 u8 lec = 0;
6040 u8 b_nvram;
6041
6042 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6043 return;
6044
6045 vdbg_printk(TPACPI_DBG_BRGHT,
6046 "trying to checkpoint backlight level to NVRAM...\n");
6047
6048 if (mutex_lock_killable(&brightness_mutex) < 0)
6049 return;
6050
6051 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6052 goto unlock;
6053 lec &= TP_EC_BACKLIGHT_LVLMSK;
6054 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6055
6056 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6057 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6058 /* NVRAM needs update */
6059 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6060 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6061 b_nvram |= lec;
6062 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6063 dbg_printk(TPACPI_DBG_BRGHT,
6064 "updated NVRAM backlight level to %u (0x%02x)\n",
6065 (unsigned int) lec, (unsigned int) b_nvram);
6066 } else
6067 vdbg_printk(TPACPI_DBG_BRGHT,
6068 "NVRAM backlight level already is %u (0x%02x)\n",
6069 (unsigned int) lec, (unsigned int) b_nvram);
6070
6071unlock:
6072 mutex_unlock(&brightness_mutex);
6073}
6074
6075
6076/* call with brightness_mutex held! */
6077static int tpacpi_brightness_get_raw(int *status)
6078{
6079 u8 lec = 0;
6080
6081 switch (brightness_mode) {
6082 case TPACPI_BRGHT_MODE_UCMS_STEP:
6083 *status = tpacpi_brightness_nvram_get();
6084 return 0;
6085 case TPACPI_BRGHT_MODE_EC:
6086 case TPACPI_BRGHT_MODE_ECNVRAM:
6087 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006088 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006089 *status = lec;
6090 return 0;
6091 default:
6092 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006093 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006094}
6095
6096/* call with brightness_mutex held! */
6097/* do NOT call with illegal backlight level value */
6098static int tpacpi_brightness_set_ec(unsigned int value)
6099{
6100 u8 lec = 0;
6101
6102 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6103 return -EIO;
6104
6105 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6106 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6107 (value & TP_EC_BACKLIGHT_LVLMSK))))
6108 return -EIO;
6109
6110 return 0;
6111}
6112
6113/* call with brightness_mutex held! */
6114static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6115{
6116 int cmos_cmd, inc;
6117 unsigned int current_value, i;
6118
6119 current_value = tpacpi_brightness_nvram_get();
6120
6121 if (value == current_value)
6122 return 0;
6123
6124 cmos_cmd = (value > current_value) ?
6125 TP_CMOS_BRIGHTNESS_UP :
6126 TP_CMOS_BRIGHTNESS_DOWN;
6127 inc = (value > current_value) ? 1 : -1;
6128
6129 for (i = current_value; i != value; i += inc)
6130 if (issue_thinkpad_cmos_command(cmos_cmd))
6131 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006132
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006133 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006134}
6135
6136/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006137static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006138{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006139 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006140
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006141 if (value > bright_maxlvl || value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006142 return -EINVAL;
6143
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006144 vdbg_printk(TPACPI_DBG_BRGHT,
6145 "set backlight level to %d\n", value);
6146
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006147 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006148 if (res < 0)
6149 return res;
6150
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006151 switch (brightness_mode) {
6152 case TPACPI_BRGHT_MODE_EC:
6153 case TPACPI_BRGHT_MODE_ECNVRAM:
6154 res = tpacpi_brightness_set_ec(value);
6155 break;
6156 case TPACPI_BRGHT_MODE_UCMS_STEP:
6157 res = tpacpi_brightness_set_ucmsstep(value);
6158 break;
6159 default:
6160 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006161 }
6162
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006163 mutex_unlock(&brightness_mutex);
6164 return res;
6165}
6166
6167/* sysfs backlight class ----------------------------------------------- */
6168
6169static int brightness_update_status(struct backlight_device *bd)
6170{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006171 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006172 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6173 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006174 bd->props.brightness : 0;
6175
6176 dbg_printk(TPACPI_DBG_BRGHT,
6177 "backlight: attempt to set level to %d\n",
6178 level);
6179
6180 /* it is the backlight class's job (caller) to handle
6181 * EINTR and other errors properly */
6182 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006183}
Holger Macht8acb0252006-10-20 14:30:28 -07006184
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006185static int brightness_get(struct backlight_device *bd)
6186{
6187 int status, res;
6188
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006189 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006190 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006191 return 0;
6192
6193 res = tpacpi_brightness_get_raw(&status);
6194
6195 mutex_unlock(&brightness_mutex);
6196
6197 if (res < 0)
6198 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006199
6200 return status & TP_EC_BACKLIGHT_LVLMSK;
6201}
6202
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006203static void tpacpi_brightness_notify_change(void)
6204{
6205 backlight_force_update(ibm_backlight_device,
6206 BACKLIGHT_UPDATE_HOTKEY);
6207}
6208
Lionel Debrouxacc24722010-11-16 14:14:02 +01006209static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006210 .get_brightness = brightness_get,
6211 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006212};
6213
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006214/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006215
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006216/*
6217 * Call _BCL method of video device. On some ThinkPads this will
6218 * switch the firmware to the ACPI brightness control mode.
6219 */
6220
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006221static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6222{
6223 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6224 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006225 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006226 int rc;
6227
Aaron Lu46445b62013-10-11 21:27:46 +08006228 if (acpi_bus_get_device(handle, &device))
6229 return 0;
6230
6231 rc = 0;
6232 list_for_each_entry(child, &device->children, node) {
6233 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6234 NULL, &buffer);
6235 if (ACPI_FAILURE(status))
6236 continue;
6237
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006238 obj = (union acpi_object *)buffer.pointer;
6239 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006240 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006241 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006242 rc = 0;
6243 } else {
6244 rc = obj->package.count;
6245 }
Aaron Lu46445b62013-10-11 21:27:46 +08006246 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006247 }
6248
6249 kfree(buffer.pointer);
6250 return rc;
6251}
6252
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006253
6254/*
6255 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6256 */
6257static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6258{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006259 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006260 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006261
Aaron Lu46445b62013-10-11 21:27:46 +08006262 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006263 if (video_device)
6264 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006265
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006266 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006267
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006268 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006269}
6270
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006271/*
6272 * These are only useful for models that have only one possibility
6273 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6274 * these quirks.
6275 */
6276#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6277#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6278#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6279
6280static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6281 /* Models with ATI GPUs known to require ECNVRAM mode */
6282 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6283
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006284 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006285 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006286 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006287 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006288 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6289
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006290 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006291 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006292 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6293 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006294
6295 /* Models with Intel GMA900 */
6296 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6297 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6298 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6299};
6300
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006301/*
6302 * Returns < 0 for error, otherwise sets tp_features.bright_*
6303 * and bright_maxlvl.
6304 */
6305static void __init tpacpi_detect_brightness_capabilities(void)
6306{
6307 unsigned int b;
6308
6309 vdbg_printk(TPACPI_DBG_INIT,
6310 "detecting firmware brightness interface capabilities\n");
6311
6312 /* we could run a quirks check here (same table used by
6313 * brightness_init) if needed */
6314
6315 /*
6316 * We always attempt to detect acpi support, so as to switch
6317 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6318 * going to publish a backlight interface
6319 */
6320 b = tpacpi_check_std_acpi_brightness_support();
6321 switch (b) {
6322 case 16:
6323 bright_maxlvl = 15;
Joe Perches0978e012011-04-04 10:06:25 -07006324 pr_info("detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006325 break;
6326 case 8:
6327 case 0:
6328 bright_maxlvl = 7;
Joe Perches0978e012011-04-04 10:06:25 -07006329 pr_info("detected a 8-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006330 break;
6331 default:
Joe Perches0978e012011-04-04 10:06:25 -07006332 pr_err("Unsupported brightness interface, "
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006333 "please contact %s\n", TPACPI_MAIL);
6334 tp_features.bright_unkfw = 1;
6335 bright_maxlvl = b - 1;
6336 }
6337}
6338
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006339static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006340{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006341 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006342 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006343 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006344
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006345 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6346
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006347 mutex_init(&brightness_mutex);
6348
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006349 quirks = tpacpi_check_quirks(brightness_quirk_table,
6350 ARRAY_SIZE(brightness_quirk_table));
6351
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006352 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006353
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006354 /* if it is unknown, we don't handle it: it wouldn't be safe */
6355 if (tp_features.bright_unkfw)
6356 return 1;
6357
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006358 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006359 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006360 "brightness support disabled by "
6361 "module parameter\n");
6362 return 1;
6363 }
6364
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006365 if (acpi_video_backlight_support()) {
6366 if (brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006367 pr_info("Standard ACPI backlight interface "
6368 "available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006369 return 1;
6370 } else if (brightness_enable == 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006371 pr_warn("Cannot enable backlight brightness support, "
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006372 "ACPI is already handling it. Refer to the "
Joe Perches0978e012011-04-04 10:06:25 -07006373 "acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006374 return 1;
6375 }
6376 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006377 pr_notice("Standard ACPI backlight interface not "
6378 "available, thinkpad_acpi native "
6379 "brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006380 }
6381
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006382 /*
6383 * Check for module parameter bogosity, note that we
6384 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6385 * able to detect "unspecified"
6386 */
6387 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006388 return -EINVAL;
6389
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006390 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6391 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6392 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006393 if (quirks & TPACPI_BRGHT_Q_EC)
6394 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6395 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006396 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6397
6398 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006399 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006400 brightness_mode);
6401 }
6402
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006403 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006404 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006405 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6406 brightness_mode == TPACPI_BRGHT_MODE_EC))
6407 return -EINVAL;
6408
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006409 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006410 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006411
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006412 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006413 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006414 props.max_brightness = bright_maxlvl;
6415 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006416 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6417 NULL, NULL,
6418 &ibm_backlight_data,
6419 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006420 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006421 int rc = PTR_ERR(ibm_backlight_device);
6422 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006423 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006424 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006425 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006426 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6427 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006428
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006429 if (quirks & TPACPI_BRGHT_Q_ASK) {
Joe Perches0978e012011-04-04 10:06:25 -07006430 pr_notice("brightness: will use unverified default: "
6431 "brightness_mode=%d\n", brightness_mode);
6432 pr_notice("brightness: please report to %s whether it works well "
6433 "or not on your ThinkPad\n", TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006434 }
6435
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006436 /* Added by mistake in early 2007. Probably useless, but it could
6437 * be working around some unknown firmware problem where the value
6438 * read at startup doesn't match the real hardware state... so leave
6439 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006440 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006441
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006442 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6443 "brightness: registering brightness hotkeys "
6444 "as change notification\n");
6445 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6446 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006447 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006448 return 0;
6449}
6450
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006451static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006452{
6453 tpacpi_brightness_checkpoint_nvram();
6454}
6455
6456static void brightness_shutdown(void)
6457{
6458 tpacpi_brightness_checkpoint_nvram();
6459}
6460
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006461static void brightness_exit(void)
6462{
6463 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006464 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006465 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006466 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006467 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006468
6469 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006470}
6471
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006472static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006473{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006474 int level;
6475
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006476 level = brightness_get(NULL);
6477 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006478 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006479 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006480 seq_printf(m, "level:\t\t%d\n", level);
6481 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006482 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6483 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006484 }
6485
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006486 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006487}
6488
6489static int brightness_write(char *buf)
6490{
6491 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006492 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006493 char *cmd;
6494
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006495 level = brightness_get(NULL);
6496 if (level < 0)
6497 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006498
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006499 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006500 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006501 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006502 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006503 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006504 if (level > 0)
6505 level--;
6506 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006507 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006508 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006509 } else
6510 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006511 }
6512
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006513 tpacpi_disclose_usertask("procfs brightness",
6514 "set level to %d\n", level);
6515
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006516 /*
6517 * Now we know what the final level should be, so we try to set it.
6518 * Doing it this way makes the syscall restartable in case of EINTR
6519 */
6520 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006521 if (!rc && ibm_backlight_device)
6522 backlight_force_update(ibm_backlight_device,
6523 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006524 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006525}
6526
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006527static struct ibm_struct brightness_driver_data = {
6528 .name = "brightness",
6529 .read = brightness_read,
6530 .write = brightness_write,
6531 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006532 .suspend = brightness_suspend,
6533 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006534};
6535
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006536/*************************************************************************
6537 * Volume subdriver
6538 */
6539
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006540/*
6541 * IBM ThinkPads have a simple volume controller with MUTE gating.
6542 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6543 *
6544 * Since the *61 series (and probably also the later *60 series), Lenovo
6545 * ThinkPads only implement the MUTE gate.
6546 *
6547 * EC register 0x30
6548 * Bit 6: MUTE (1 mutes sound)
6549 * Bit 3-0: Volume
6550 * Other bits should be zero as far as we know.
6551 *
6552 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6553 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6554 * such as bit 7 which is used to detect repeated presses of MUTE,
6555 * and we leave them unchanged.
6556 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006557
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006558#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6559
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006560#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6561#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6562#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6563
Takashi Iwaicab66612013-10-24 16:06:32 +02006564#if SNDRV_CARDS <= 32
6565#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
6566#else
6567#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
6568#endif
6569static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006570static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10306571static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006572
6573struct tpacpi_alsa_data {
6574 struct snd_card *card;
6575 struct snd_ctl_elem_id *ctl_mute_id;
6576 struct snd_ctl_elem_id *ctl_vol_id;
6577};
6578
6579static struct snd_card *alsa_card;
6580
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006581enum {
6582 TP_EC_AUDIO = 0x30,
6583
6584 /* TP_EC_AUDIO bits */
6585 TP_EC_AUDIO_MUTESW = 6,
6586
6587 /* TP_EC_AUDIO bitmasks */
6588 TP_EC_AUDIO_LVL_MSK = 0x0F,
6589 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6590
6591 /* Maximum volume */
6592 TP_EC_VOLUME_MAX = 14,
6593};
6594
6595enum tpacpi_volume_access_mode {
6596 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6597 TPACPI_VOL_MODE_EC, /* Pure EC control */
6598 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6599 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6600 TPACPI_VOL_MODE_MAX
6601};
6602
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006603enum tpacpi_volume_capabilities {
6604 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6605 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6606 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6607 TPACPI_VOL_CAP_MAX
6608};
6609
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006610static enum tpacpi_volume_access_mode volume_mode =
6611 TPACPI_VOL_MODE_MAX;
6612
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006613static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10306614static bool volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006615
6616/*
6617 * Used to syncronize writers to TP_EC_AUDIO and
6618 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6619 */
6620static struct mutex volume_mutex;
6621
6622static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006623{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006624 u8 lec = 0;
6625 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006626 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006627
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006628 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6629 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006630 if (!volume_control_allowed)
6631 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006632
6633 vdbg_printk(TPACPI_DBG_MIXER,
6634 "trying to checkpoint mixer state to NVRAM...\n");
6635
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006636 if (tp_features.mixer_no_level_control)
6637 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6638 else
6639 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6640
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006641 if (mutex_lock_killable(&volume_mutex) < 0)
6642 return;
6643
6644 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6645 goto unlock;
6646 lec &= ec_mask;
6647 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6648
6649 if (lec != (b_nvram & ec_mask)) {
6650 /* NVRAM needs update */
6651 b_nvram &= ~ec_mask;
6652 b_nvram |= lec;
6653 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6654 dbg_printk(TPACPI_DBG_MIXER,
6655 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6656 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006657 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006658 vdbg_printk(TPACPI_DBG_MIXER,
6659 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6660 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006661 }
6662
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006663unlock:
6664 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006665}
6666
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006667static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006668{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006669 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006670
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006671 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6672 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006673
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006674 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006675
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006676 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006677
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006678 return 0;
6679}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006680
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006681static int volume_get_status(u8 *status)
6682{
6683 return volume_get_status_ec(status);
6684}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006685
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006686static int volume_set_status_ec(const u8 status)
6687{
6688 if (!acpi_ec_write(TP_EC_AUDIO, status))
6689 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006690
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006691 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6692
6693 return 0;
6694}
6695
6696static int volume_set_status(const u8 status)
6697{
6698 return volume_set_status_ec(status);
6699}
6700
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006701/* returns < 0 on error, 0 on no change, 1 on change */
6702static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006703{
6704 int rc;
6705 u8 s, n;
6706
6707 if (mutex_lock_killable(&volume_mutex) < 0)
6708 return -EINTR;
6709
6710 rc = volume_get_status_ec(&s);
6711 if (rc)
6712 goto unlock;
6713
6714 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6715 s & ~TP_EC_AUDIO_MUTESW_MSK;
6716
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006717 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006718 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006719 if (!rc)
6720 rc = 1;
6721 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006722
6723unlock:
6724 mutex_unlock(&volume_mutex);
6725 return rc;
6726}
6727
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006728static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006729{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006730 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006731 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006732 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006733}
6734
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006735static int volume_set_mute(const bool mute)
6736{
6737 int rc;
6738
6739 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6740 (mute) ? "" : "un");
6741
6742 rc = __volume_set_mute_ec(mute);
6743 return (rc < 0) ? rc : 0;
6744}
6745
6746/* returns < 0 on error, 0 on no change, 1 on change */
6747static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006748{
6749 int rc;
6750 u8 s, n;
6751
6752 if (vol > TP_EC_VOLUME_MAX)
6753 return -EINVAL;
6754
6755 if (mutex_lock_killable(&volume_mutex) < 0)
6756 return -EINTR;
6757
6758 rc = volume_get_status_ec(&s);
6759 if (rc)
6760 goto unlock;
6761
6762 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6763
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006764 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006765 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006766 if (!rc)
6767 rc = 1;
6768 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006769
6770unlock:
6771 mutex_unlock(&volume_mutex);
6772 return rc;
6773}
6774
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006775static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006776{
6777 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006778 "ALSA: trying to set volume level to %hu\n", vol);
6779 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006780}
6781
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006782static void volume_alsa_notify_change(void)
6783{
6784 struct tpacpi_alsa_data *d;
6785
6786 if (alsa_card && alsa_card->private_data) {
6787 d = alsa_card->private_data;
6788 if (d->ctl_mute_id)
6789 snd_ctl_notify(alsa_card,
6790 SNDRV_CTL_EVENT_MASK_VALUE,
6791 d->ctl_mute_id);
6792 if (d->ctl_vol_id)
6793 snd_ctl_notify(alsa_card,
6794 SNDRV_CTL_EVENT_MASK_VALUE,
6795 d->ctl_vol_id);
6796 }
6797}
6798
6799static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6800 struct snd_ctl_elem_info *uinfo)
6801{
6802 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6803 uinfo->count = 1;
6804 uinfo->value.integer.min = 0;
6805 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6806 return 0;
6807}
6808
6809static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6810 struct snd_ctl_elem_value *ucontrol)
6811{
6812 u8 s;
6813 int rc;
6814
6815 rc = volume_get_status(&s);
6816 if (rc < 0)
6817 return rc;
6818
6819 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6820 return 0;
6821}
6822
6823static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6824 struct snd_ctl_elem_value *ucontrol)
6825{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006826 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6827 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006828 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006829}
6830
6831#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6832
6833static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6834 struct snd_ctl_elem_value *ucontrol)
6835{
6836 u8 s;
6837 int rc;
6838
6839 rc = volume_get_status(&s);
6840 if (rc < 0)
6841 return rc;
6842
6843 ucontrol->value.integer.value[0] =
6844 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6845 return 0;
6846}
6847
6848static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6849 struct snd_ctl_elem_value *ucontrol)
6850{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006851 tpacpi_disclose_usertask("ALSA", "%smute\n",
6852 ucontrol->value.integer.value[0] ?
6853 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006854 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006855}
6856
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08006857static struct snd_kcontrol_new volume_alsa_control_vol = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006858 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6859 .name = "Console Playback Volume",
6860 .index = 0,
6861 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6862 .info = volume_alsa_vol_info,
6863 .get = volume_alsa_vol_get,
6864};
6865
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08006866static struct snd_kcontrol_new volume_alsa_control_mute = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006867 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6868 .name = "Console Playback Switch",
6869 .index = 0,
6870 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6871 .info = volume_alsa_mute_info,
6872 .get = volume_alsa_mute_get,
6873};
6874
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006875static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006876{
6877 tpacpi_volume_checkpoint_nvram();
6878}
6879
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006880static void volume_resume(void)
6881{
6882 volume_alsa_notify_change();
6883}
6884
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006885static void volume_shutdown(void)
6886{
6887 tpacpi_volume_checkpoint_nvram();
6888}
6889
6890static void volume_exit(void)
6891{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006892 if (alsa_card) {
6893 snd_card_free(alsa_card);
6894 alsa_card = NULL;
6895 }
6896
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006897 tpacpi_volume_checkpoint_nvram();
6898}
6899
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006900static int __init volume_create_alsa_mixer(void)
6901{
6902 struct snd_card *card;
6903 struct tpacpi_alsa_data *data;
6904 struct snd_kcontrol *ctl_vol;
6905 struct snd_kcontrol *ctl_mute;
6906 int rc;
6907
Takashi Iwai89235e52014-01-29 15:01:27 +01006908 rc = snd_card_new(&tpacpi_pdev->dev,
6909 alsa_index, alsa_id, THIS_MODULE,
6910 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006911 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07006912 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006913 return 1;
6914 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006915
6916 BUG_ON(!card->private_data);
6917 data = card->private_data;
6918 data->card = card;
6919
6920 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6921 sizeof(card->driver));
6922 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6923 sizeof(card->shortname));
6924 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6925 (thinkpad_id.ec_version_str) ?
6926 thinkpad_id.ec_version_str : "(unknown)");
6927 snprintf(card->longname, sizeof(card->longname),
6928 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6929 (thinkpad_id.ec_version_str) ?
6930 thinkpad_id.ec_version_str : "unknown");
6931
6932 if (volume_control_allowed) {
6933 volume_alsa_control_vol.put = volume_alsa_vol_put;
6934 volume_alsa_control_vol.access =
6935 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6936
6937 volume_alsa_control_mute.put = volume_alsa_mute_put;
6938 volume_alsa_control_mute.access =
6939 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6940 }
6941
6942 if (!tp_features.mixer_no_level_control) {
6943 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6944 rc = snd_ctl_add(card, ctl_vol);
6945 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006946 pr_err("Failed to create ALSA volume control: %d\n",
6947 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006948 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006949 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006950 data->ctl_vol_id = &ctl_vol->id;
6951 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006952
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006953 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6954 rc = snd_ctl_add(card, ctl_mute);
6955 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006956 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006957 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006958 }
6959 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006960
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006961 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006962 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006963 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006964 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006965 }
6966
6967 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006968 return 0;
6969
6970err_exit:
6971 snd_card_free(card);
6972 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006973}
6974
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006975#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6976#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6977
6978static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6979 /* Whitelist volume level on all IBM by default */
6980 { .vendor = PCI_VENDOR_ID_IBM,
6981 .bios = TPACPI_MATCH_ANY,
6982 .ec = TPACPI_MATCH_ANY,
6983 .quirks = TPACPI_VOL_Q_LEVEL },
6984
6985 /* Lenovo models with volume control (needs confirmation) */
6986 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6987 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6988 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6989 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6990 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6991 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6992 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6993
6994 /* Whitelist mute-only on all Lenovo by default */
6995 { .vendor = PCI_VENDOR_ID_LENOVO,
6996 .bios = TPACPI_MATCH_ANY,
6997 .ec = TPACPI_MATCH_ANY,
6998 .quirks = TPACPI_VOL_Q_MUTEONLY }
6999};
7000
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007001static int __init volume_init(struct ibm_init_struct *iibm)
7002{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007003 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007004 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007005
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007006 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7007
7008 mutex_init(&volume_mutex);
7009
7010 /*
7011 * Check for module parameter bogosity, note that we
7012 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7013 * able to detect "unspecified"
7014 */
7015 if (volume_mode > TPACPI_VOL_MODE_MAX)
7016 return -EINVAL;
7017
7018 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Joe Perches0978e012011-04-04 10:06:25 -07007019 pr_err("UCMS step volume mode not implemented, "
7020 "please contact %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007021 return 1;
7022 }
7023
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007024 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7025 return -EINVAL;
7026
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007027 /*
7028 * The ALSA mixer is our primary interface.
7029 * When disabled, don't install the subdriver at all
7030 */
7031 if (!alsa_enable) {
7032 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7033 "ALSA mixer disabled by parameter, "
7034 "not loading volume subdriver...\n");
7035 return 1;
7036 }
7037
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007038 quirks = tpacpi_check_quirks(volume_quirk_table,
7039 ARRAY_SIZE(volume_quirk_table));
7040
7041 switch (volume_capabilities) {
7042 case TPACPI_VOL_CAP_AUTO:
7043 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7044 tp_features.mixer_no_level_control = 1;
7045 else if (quirks & TPACPI_VOL_Q_LEVEL)
7046 tp_features.mixer_no_level_control = 0;
7047 else
7048 return 1; /* no mixer */
7049 break;
7050 case TPACPI_VOL_CAP_VOLMUTE:
7051 tp_features.mixer_no_level_control = 0;
7052 break;
7053 case TPACPI_VOL_CAP_MUTEONLY:
7054 tp_features.mixer_no_level_control = 1;
7055 break;
7056 default:
7057 return 1;
7058 }
7059
7060 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7061 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7062 "using user-supplied volume_capabilities=%d\n",
7063 volume_capabilities);
7064
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007065 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7066 volume_mode == TPACPI_VOL_MODE_MAX) {
7067 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7068
7069 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7070 "driver auto-selected volume_mode=%d\n",
7071 volume_mode);
7072 } else {
7073 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7074 "using user-supplied volume_mode=%d\n",
7075 volume_mode);
7076 }
7077
7078 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007079 "mute is supported, volume control is %s\n",
7080 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007081
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007082 rc = volume_create_alsa_mixer();
7083 if (rc) {
Joe Perches0978e012011-04-04 10:06:25 -07007084 pr_err("Could not create the ALSA mixer interface\n");
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007085 return rc;
7086 }
7087
Joe Perches0978e012011-04-04 10:06:25 -07007088 pr_info("Console audio control enabled, mode: %s\n",
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007089 (volume_control_allowed) ?
7090 "override (read/write)" :
7091 "monitor (read only)");
7092
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007093 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7094 "registering volume hotkeys as change notification\n");
7095 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7096 | TP_ACPI_HKEY_VOLUP_MASK
7097 | TP_ACPI_HKEY_VOLDWN_MASK
7098 | TP_ACPI_HKEY_MUTE_MASK);
7099
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007100 return 0;
7101}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007102
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007103static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007104{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007105 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007106
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007107 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007108 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007109 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007110 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007111 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007112 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007113 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007114 status & TP_EC_AUDIO_LVL_MSK);
7115
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007116 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007117 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007118
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007119 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007120 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007121 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007122 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007123 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007124 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007125 "commands:\tlevel <level>"
7126 " (<level> is 0-%d)\n",
7127 TP_EC_VOLUME_MAX);
7128 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007129 }
7130 }
7131
7132 return 0;
7133}
7134
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007135static int volume_write(char *buf)
7136{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007137 u8 s;
7138 u8 new_level, new_mute;
7139 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007140 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007141 int rc;
7142
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007143 /*
7144 * We do allow volume control at driver startup, so that the
7145 * user can set initial state through the volume=... parameter hack.
7146 */
7147 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7148 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7149 tp_warned.volume_ctrl_forbidden = 1;
Joe Perches0978e012011-04-04 10:06:25 -07007150 pr_notice("Console audio control in monitor mode, "
7151 "changes are not allowed\n");
7152 pr_notice("Use the volume_control=1 module parameter "
7153 "to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007154 }
7155 return -EPERM;
7156 }
7157
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007158 rc = volume_get_status(&s);
7159 if (rc < 0)
7160 return rc;
7161
7162 new_level = s & TP_EC_AUDIO_LVL_MSK;
7163 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007164
7165 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007166 if (!tp_features.mixer_no_level_control) {
7167 if (strlencmp(cmd, "up") == 0) {
7168 if (new_mute)
7169 new_mute = 0;
7170 else if (new_level < TP_EC_VOLUME_MAX)
7171 new_level++;
7172 continue;
7173 } else if (strlencmp(cmd, "down") == 0) {
7174 if (new_mute)
7175 new_mute = 0;
7176 else if (new_level > 0)
7177 new_level--;
7178 continue;
7179 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7180 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7181 new_level = l;
7182 continue;
7183 }
7184 }
7185 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007186 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007187 else if (strlencmp(cmd, "unmute") == 0)
7188 new_mute = 0;
7189 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007190 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007191 }
7192
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007193 if (tp_features.mixer_no_level_control) {
7194 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7195 new_mute ? "" : "un");
7196 rc = volume_set_mute(!!new_mute);
7197 } else {
7198 tpacpi_disclose_usertask("procfs volume",
7199 "%smute and set level to %d\n",
7200 new_mute ? "" : "un", new_level);
7201 rc = volume_set_status(new_mute | new_level);
7202 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007203 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007204
7205 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007206}
7207
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007208static struct ibm_struct volume_driver_data = {
7209 .name = "volume",
7210 .read = volume_read,
7211 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007212 .exit = volume_exit,
7213 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007214 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007215 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007216};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007217
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007218#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7219
7220#define alsa_card NULL
7221
7222static void inline volume_alsa_notify_change(void)
7223{
7224}
7225
7226static int __init volume_init(struct ibm_init_struct *iibm)
7227{
Joe Perches0978e012011-04-04 10:06:25 -07007228 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007229
7230 return 1;
7231}
7232
7233static struct ibm_struct volume_driver_data = {
7234 .name = "volume",
7235};
7236
7237#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7238
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007239/*************************************************************************
7240 * Fan subdriver
7241 */
7242
7243/*
7244 * FAN ACCESS MODES
7245 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007246 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007247 * ACPI GFAN method: returns fan level
7248 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007249 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007250 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007251 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007252 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007253 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7254 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007255 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7256 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007257 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007258 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007259 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7260 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007261 *
7262 * Fan speed changes of any sort (including those caused by the
7263 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007264 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007265 * limited.
7266 *
7267 * Reading is not available if GFAN exists.
7268 * Writing is not available if SFAN exists.
7269 *
7270 * Bits
7271 * 7 automatic mode engaged;
7272 * (default operation mode of the ThinkPad)
7273 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007274 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007275 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007276 * the tachometer while the fan controller ramps up
7277 * the speed (which can take up to a few *minutes*).
7278 * Speeds up fan to 100% duty-cycle, which is far above
7279 * the standard RPM levels. It is not impossible that
7280 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007281 * 5-3 unused in some models. Extra bits for fan level
7282 * in others, but still useless as all values above
7283 * 7 map to the same speed as level 7 in these models.
7284 * 2-0 fan level (0..7 usually)
7285 * 0x00 = stop
7286 * 0x07 = max (set when temperatures critical)
7287 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007288 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007289 *
7290 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007291 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007292 * does so, its initial value is meaningless (0x07).
7293 *
7294 * For firmware bugs, refer to:
7295 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7296 *
7297 * ----
7298 *
7299 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7300 * Main fan tachometer reading (in RPM)
7301 *
7302 * This register is present on all ThinkPads with a new-style EC, and
7303 * it is known not to be present on the A21m/e, and T22, as there is
7304 * something else in offset 0x84 according to the ACPI DSDT. Other
7305 * ThinkPads from this same time period (and earlier) probably lack the
7306 * tachometer as well.
7307 *
Nick Andrew877d0312009-01-26 11:06:57 +01007308 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007309 * was never fixed by IBM to report the EC firmware version string
7310 * probably support the tachometer (like the early X models), so
7311 * detecting it is quite hard. We need more data to know for sure.
7312 *
7313 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7314 * might result.
7315 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007316 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7317 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007318 *
7319 * For firmware bugs, refer to:
7320 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7321 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007322 * ----
7323 *
7324 * ThinkPad EC register 0x31 bit 0 (only on select models)
7325 *
7326 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7327 * show the speed of the main fan. When bit 0 of EC register 0x31
7328 * is one, the tachometer registers show the speed of the auxiliary
7329 * fan.
7330 *
7331 * Fan control seems to affect both fans, regardless of the state
7332 * of this bit.
7333 *
7334 * So far, only the firmware for the X60/X61 non-tablet versions
7335 * seem to support this (firmware TP-7M).
7336 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007337 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007338 * ThinkPad X31, X40, X41. Not available in the X60.
7339 *
7340 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7341 * high speed. ACPI DSDT seems to map these three speeds to levels
7342 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7343 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7344 *
7345 * The speeds are stored on handles
7346 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7347 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007348 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007349 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7350 *
7351 * ACPI DSDT switches which set is in use depending on various
7352 * factors.
7353 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007354 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007355 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7356 * but the ACPI tables just mention level 7.
7357 */
7358
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007359enum { /* Fan control constants */
7360 fan_status_offset = 0x2f, /* EC register 0x2f */
7361 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7362 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007363 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7364 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007365
7366 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7367 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7368
7369 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7370};
7371
7372enum fan_status_access_mode {
7373 TPACPI_FAN_NONE = 0, /* No fan status or control */
7374 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7375 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7376};
7377
7378enum fan_control_access_mode {
7379 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7380 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7381 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7382 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7383};
7384
7385enum fan_control_commands {
7386 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7387 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7388 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7389 * and also watchdog cmd */
7390};
7391
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307392static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007393
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007394static enum fan_status_access_mode fan_status_access_mode;
7395static enum fan_control_access_mode fan_control_access_mode;
7396static enum fan_control_commands fan_control_commands;
7397
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007398static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007399static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007400static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007401static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007402
7403static struct mutex fan_mutex;
7404
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007405static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007406static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007407
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007408TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7409TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007410 "\\FSPD", /* 600e/x, 770e, 770x */
7411 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007412TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007413 "JFNS", /* 770x-JL */
7414 ); /* all others */
7415
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007416/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007417 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7418 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7419 * be in auto mode (0x80).
7420 *
7421 * This is corrected by any write to HFSP either by the driver, or
7422 * by the firmware.
7423 *
7424 * We assume 0x07 really means auto mode while this quirk is active,
7425 * as this is far more likely than the ThinkPad being in level 7,
7426 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007427 *
7428 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7429 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007430 */
7431
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007432static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007433{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007434 if (fan_control_initial_status == 0x07) {
Joe Perches0978e012011-04-04 10:06:25 -07007435 pr_notice("fan_init: initial fan status is unknown, "
7436 "assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007437 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007438 }
7439}
7440
7441static void fan_quirk1_handle(u8 *fan_status)
7442{
7443 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7444 if (*fan_status != fan_control_initial_status) {
7445 /* something changed the HFSP regisnter since
7446 * driver init time, so it is not undefined
7447 * anymore */
7448 tp_features.fan_ctrl_status_undef = 0;
7449 } else {
7450 /* Return most likely status. In fact, it
7451 * might be the only possible status */
7452 *fan_status = TP_EC_FAN_AUTO;
7453 }
7454 }
7455}
7456
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007457/* Select main fan on X60/X61, NOOP on others */
7458static bool fan_select_fan1(void)
7459{
7460 if (tp_features.second_fan) {
7461 u8 val;
7462
7463 if (ec_read(fan_select_offset, &val) < 0)
7464 return false;
7465 val &= 0xFEU;
7466 if (ec_write(fan_select_offset, val) < 0)
7467 return false;
7468 }
7469 return true;
7470}
7471
7472/* Select secondary fan on X60/X61 */
7473static bool fan_select_fan2(void)
7474{
7475 u8 val;
7476
7477 if (!tp_features.second_fan)
7478 return false;
7479
7480 if (ec_read(fan_select_offset, &val) < 0)
7481 return false;
7482 val |= 0x01U;
7483 if (ec_write(fan_select_offset, val) < 0)
7484 return false;
7485
7486 return true;
7487}
7488
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007489/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007490 * Call with fan_mutex held
7491 */
7492static void fan_update_desired_level(u8 status)
7493{
7494 if ((status &
7495 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7496 if (status > 7)
7497 fan_control_desired_level = 7;
7498 else
7499 fan_control_desired_level = status;
7500 }
7501}
7502
7503static int fan_get_status(u8 *status)
7504{
7505 u8 s;
7506
7507 /* TODO:
7508 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7509
7510 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07007511 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007512 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07007513 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007514
Dan Carpentereceeb432012-09-01 12:54:07 -07007515 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007516 return -EIO;
7517
7518 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07007519 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007520
7521 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07007522 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007523 case TPACPI_FAN_RD_TPEC:
7524 /* all except 570, 600e/x, 770e, 770x */
7525 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7526 return -EIO;
7527
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007528 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007529 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007530 fan_quirk1_handle(status);
7531 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007532
7533 break;
7534
7535 default:
7536 return -ENXIO;
7537 }
7538
7539 return 0;
7540}
7541
7542static int fan_get_status_safe(u8 *status)
7543{
7544 int rc;
7545 u8 s;
7546
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007547 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007548 return -ERESTARTSYS;
7549 rc = fan_get_status(&s);
7550 if (!rc)
7551 fan_update_desired_level(s);
7552 mutex_unlock(&fan_mutex);
7553
7554 if (status)
7555 *status = s;
7556
7557 return rc;
7558}
7559
7560static int fan_get_speed(unsigned int *speed)
7561{
7562 u8 hi, lo;
7563
7564 switch (fan_status_access_mode) {
7565 case TPACPI_FAN_RD_TPEC:
7566 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007567 if (unlikely(!fan_select_fan1()))
7568 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007569 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7570 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7571 return -EIO;
7572
7573 if (likely(speed))
7574 *speed = (hi << 8) | lo;
7575
7576 break;
7577
7578 default:
7579 return -ENXIO;
7580 }
7581
7582 return 0;
7583}
7584
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007585static int fan2_get_speed(unsigned int *speed)
7586{
7587 u8 hi, lo;
7588 bool rc;
7589
7590 switch (fan_status_access_mode) {
7591 case TPACPI_FAN_RD_TPEC:
7592 /* all except 570, 600e/x, 770e, 770x */
7593 if (unlikely(!fan_select_fan2()))
7594 return -EIO;
7595 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7596 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7597 fan_select_fan1(); /* play it safe */
7598 if (rc)
7599 return -EIO;
7600
7601 if (likely(speed))
7602 *speed = (hi << 8) | lo;
7603
7604 break;
7605
7606 default:
7607 return -ENXIO;
7608 }
7609
7610 return 0;
7611}
7612
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007613static int fan_set_level(int level)
7614{
7615 if (!fan_control_allowed)
7616 return -EPERM;
7617
7618 switch (fan_control_access_mode) {
7619 case TPACPI_FAN_WR_ACPI_SFAN:
7620 if (level >= 0 && level <= 7) {
7621 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7622 return -EIO;
7623 } else
7624 return -EINVAL;
7625 break;
7626
7627 case TPACPI_FAN_WR_ACPI_FANS:
7628 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007629 if (!(level & TP_EC_FAN_AUTO) &&
7630 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007631 ((level < 0) || (level > 7)))
7632 return -EINVAL;
7633
7634 /* safety net should the EC not support AUTO
7635 * or FULLSPEED mode bits and just ignore them */
7636 if (level & TP_EC_FAN_FULLSPEED)
7637 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007638 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007639 level |= 4; /* safety min speed 4 */
7640
7641 if (!acpi_ec_write(fan_status_offset, level))
7642 return -EIO;
7643 else
7644 tp_features.fan_ctrl_status_undef = 0;
7645 break;
7646
7647 default:
7648 return -ENXIO;
7649 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007650
7651 vdbg_printk(TPACPI_DBG_FAN,
7652 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007653 return 0;
7654}
7655
7656static int fan_set_level_safe(int level)
7657{
7658 int rc;
7659
7660 if (!fan_control_allowed)
7661 return -EPERM;
7662
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007663 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007664 return -ERESTARTSYS;
7665
7666 if (level == TPACPI_FAN_LAST_LEVEL)
7667 level = fan_control_desired_level;
7668
7669 rc = fan_set_level(level);
7670 if (!rc)
7671 fan_update_desired_level(level);
7672
7673 mutex_unlock(&fan_mutex);
7674 return rc;
7675}
7676
7677static int fan_set_enable(void)
7678{
7679 u8 s;
7680 int rc;
7681
7682 if (!fan_control_allowed)
7683 return -EPERM;
7684
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007685 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007686 return -ERESTARTSYS;
7687
7688 switch (fan_control_access_mode) {
7689 case TPACPI_FAN_WR_ACPI_FANS:
7690 case TPACPI_FAN_WR_TPEC:
7691 rc = fan_get_status(&s);
7692 if (rc < 0)
7693 break;
7694
7695 /* Don't go out of emergency fan mode */
7696 if (s != 7) {
7697 s &= 0x07;
7698 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7699 }
7700
7701 if (!acpi_ec_write(fan_status_offset, s))
7702 rc = -EIO;
7703 else {
7704 tp_features.fan_ctrl_status_undef = 0;
7705 rc = 0;
7706 }
7707 break;
7708
7709 case TPACPI_FAN_WR_ACPI_SFAN:
7710 rc = fan_get_status(&s);
7711 if (rc < 0)
7712 break;
7713
7714 s &= 0x07;
7715
7716 /* Set fan to at least level 4 */
7717 s |= 4;
7718
7719 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007720 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007721 else
7722 rc = 0;
7723 break;
7724
7725 default:
7726 rc = -ENXIO;
7727 }
7728
7729 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007730
7731 if (!rc)
7732 vdbg_printk(TPACPI_DBG_FAN,
7733 "fan control: set fan control register to 0x%02x\n",
7734 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007735 return rc;
7736}
7737
7738static int fan_set_disable(void)
7739{
7740 int rc;
7741
7742 if (!fan_control_allowed)
7743 return -EPERM;
7744
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007745 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007746 return -ERESTARTSYS;
7747
7748 rc = 0;
7749 switch (fan_control_access_mode) {
7750 case TPACPI_FAN_WR_ACPI_FANS:
7751 case TPACPI_FAN_WR_TPEC:
7752 if (!acpi_ec_write(fan_status_offset, 0x00))
7753 rc = -EIO;
7754 else {
7755 fan_control_desired_level = 0;
7756 tp_features.fan_ctrl_status_undef = 0;
7757 }
7758 break;
7759
7760 case TPACPI_FAN_WR_ACPI_SFAN:
7761 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7762 rc = -EIO;
7763 else
7764 fan_control_desired_level = 0;
7765 break;
7766
7767 default:
7768 rc = -ENXIO;
7769 }
7770
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007771 if (!rc)
7772 vdbg_printk(TPACPI_DBG_FAN,
7773 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007774
7775 mutex_unlock(&fan_mutex);
7776 return rc;
7777}
7778
7779static int fan_set_speed(int speed)
7780{
7781 int rc;
7782
7783 if (!fan_control_allowed)
7784 return -EPERM;
7785
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007786 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007787 return -ERESTARTSYS;
7788
7789 rc = 0;
7790 switch (fan_control_access_mode) {
7791 case TPACPI_FAN_WR_ACPI_FANS:
7792 if (speed >= 0 && speed <= 65535) {
7793 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7794 speed, speed, speed))
7795 rc = -EIO;
7796 } else
7797 rc = -EINVAL;
7798 break;
7799
7800 default:
7801 rc = -ENXIO;
7802 }
7803
7804 mutex_unlock(&fan_mutex);
7805 return rc;
7806}
7807
7808static void fan_watchdog_reset(void)
7809{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007810 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7811 return;
7812
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007813 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07007814 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
7815 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
7816 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
7817 else
7818 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007819}
7820
7821static void fan_watchdog_fire(struct work_struct *ignored)
7822{
7823 int rc;
7824
7825 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7826 return;
7827
Joe Perches0978e012011-04-04 10:06:25 -07007828 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007829 rc = fan_set_enable();
7830 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007831 pr_err("fan watchdog: error %d while enabling fan, "
7832 "will try again later...\n", -rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007833 /* reschedule for later */
7834 fan_watchdog_reset();
7835 }
7836}
7837
7838/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007839 * SYSFS fan layout: hwmon compatible (device)
7840 *
7841 * pwm*_enable:
7842 * 0: "disengaged" mode
7843 * 1: manual mode
7844 * 2: native EC "auto" mode (recommended, hardware default)
7845 *
7846 * pwm*: set speed in manual mode, ignored otherwise.
7847 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7848 * interpolation.
7849 *
7850 * fan*_input: tachometer reading, RPM
7851 *
7852 *
7853 * SYSFS fan layout: extensions
7854 *
7855 * fan_watchdog (driver):
7856 * fan watchdog interval in seconds, 0 disables (default), max 120
7857 */
7858
7859/* sysfs fan pwm1_enable ----------------------------------------------- */
7860static ssize_t fan_pwm1_enable_show(struct device *dev,
7861 struct device_attribute *attr,
7862 char *buf)
7863{
7864 int res, mode;
7865 u8 status;
7866
7867 res = fan_get_status_safe(&status);
7868 if (res)
7869 return res;
7870
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007871 if (status & TP_EC_FAN_FULLSPEED) {
7872 mode = 0;
7873 } else if (status & TP_EC_FAN_AUTO) {
7874 mode = 2;
7875 } else
7876 mode = 1;
7877
7878 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7879}
7880
7881static ssize_t fan_pwm1_enable_store(struct device *dev,
7882 struct device_attribute *attr,
7883 const char *buf, size_t count)
7884{
7885 unsigned long t;
7886 int res, level;
7887
7888 if (parse_strtoul(buf, 2, &t))
7889 return -EINVAL;
7890
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007891 tpacpi_disclose_usertask("hwmon pwm1_enable",
7892 "set fan mode to %lu\n", t);
7893
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007894 switch (t) {
7895 case 0:
7896 level = TP_EC_FAN_FULLSPEED;
7897 break;
7898 case 1:
7899 level = TPACPI_FAN_LAST_LEVEL;
7900 break;
7901 case 2:
7902 level = TP_EC_FAN_AUTO;
7903 break;
7904 case 3:
7905 /* reserved for software-controlled auto mode */
7906 return -ENOSYS;
7907 default:
7908 return -EINVAL;
7909 }
7910
7911 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007912 if (res == -ENXIO)
7913 return -EINVAL;
7914 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007915 return res;
7916
7917 fan_watchdog_reset();
7918
7919 return count;
7920}
7921
7922static struct device_attribute dev_attr_fan_pwm1_enable =
7923 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7924 fan_pwm1_enable_show, fan_pwm1_enable_store);
7925
7926/* sysfs fan pwm1 ------------------------------------------------------ */
7927static ssize_t fan_pwm1_show(struct device *dev,
7928 struct device_attribute *attr,
7929 char *buf)
7930{
7931 int res;
7932 u8 status;
7933
7934 res = fan_get_status_safe(&status);
7935 if (res)
7936 return res;
7937
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007938 if ((status &
7939 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7940 status = fan_control_desired_level;
7941
7942 if (status > 7)
7943 status = 7;
7944
7945 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7946}
7947
7948static ssize_t fan_pwm1_store(struct device *dev,
7949 struct device_attribute *attr,
7950 const char *buf, size_t count)
7951{
7952 unsigned long s;
7953 int rc;
7954 u8 status, newlevel;
7955
7956 if (parse_strtoul(buf, 255, &s))
7957 return -EINVAL;
7958
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007959 tpacpi_disclose_usertask("hwmon pwm1",
7960 "set fan speed to %lu\n", s);
7961
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007962 /* scale down from 0-255 to 0-7 */
7963 newlevel = (s >> 5) & 0x07;
7964
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007965 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007966 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007967
7968 rc = fan_get_status(&status);
7969 if (!rc && (status &
7970 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7971 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007972 if (rc == -ENXIO)
7973 rc = -EINVAL;
7974 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007975 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007976 fan_watchdog_reset();
7977 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007978 }
7979
7980 mutex_unlock(&fan_mutex);
7981 return (rc)? rc : count;
7982}
7983
7984static struct device_attribute dev_attr_fan_pwm1 =
7985 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7986 fan_pwm1_show, fan_pwm1_store);
7987
7988/* sysfs fan fan1_input ------------------------------------------------ */
7989static ssize_t fan_fan1_input_show(struct device *dev,
7990 struct device_attribute *attr,
7991 char *buf)
7992{
7993 int res;
7994 unsigned int speed;
7995
7996 res = fan_get_speed(&speed);
7997 if (res < 0)
7998 return res;
7999
8000 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8001}
8002
8003static struct device_attribute dev_attr_fan_fan1_input =
8004 __ATTR(fan1_input, S_IRUGO,
8005 fan_fan1_input_show, NULL);
8006
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008007/* sysfs fan fan2_input ------------------------------------------------ */
8008static ssize_t fan_fan2_input_show(struct device *dev,
8009 struct device_attribute *attr,
8010 char *buf)
8011{
8012 int res;
8013 unsigned int speed;
8014
8015 res = fan2_get_speed(&speed);
8016 if (res < 0)
8017 return res;
8018
8019 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8020}
8021
8022static struct device_attribute dev_attr_fan_fan2_input =
8023 __ATTR(fan2_input, S_IRUGO,
8024 fan_fan2_input_show, NULL);
8025
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008026/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008027static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
8028 char *buf)
8029{
8030 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8031}
8032
8033static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8034 const char *buf, size_t count)
8035{
8036 unsigned long t;
8037
8038 if (parse_strtoul(buf, 120, &t))
8039 return -EINVAL;
8040
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008041 if (!fan_control_allowed)
8042 return -EPERM;
8043
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008044 fan_watchdog_maxinterval = t;
8045 fan_watchdog_reset();
8046
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008047 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8048
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008049 return count;
8050}
8051
8052static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8053 fan_fan_watchdog_show, fan_fan_watchdog_store);
8054
8055/* --------------------------------------------------------------------- */
8056static struct attribute *fan_attributes[] = {
8057 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8058 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008059 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008060 NULL
8061};
8062
8063static const struct attribute_group fan_attr_group = {
8064 .attrs = fan_attributes,
8065};
8066
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008067#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8068#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008069
8070#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8071 { .vendor = PCI_VENDOR_ID_IBM, \
8072 .bios = TPACPI_MATCH_ANY, \
8073 .ec = TPID(__id1, __id2), \
8074 .quirks = __quirks }
8075
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008076#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8077 { .vendor = PCI_VENDOR_ID_LENOVO, \
8078 .bios = TPACPI_MATCH_ANY, \
8079 .ec = TPID(__id1, __id2), \
8080 .quirks = __quirks }
8081
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008082static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8083 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8084 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8085 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8086 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008087 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008088};
8089
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008090#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008091#undef TPACPI_FAN_QI
8092
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008093static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008094{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008095 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008096 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008097
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008098 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8099 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008100
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008101 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008102 fan_status_access_mode = TPACPI_FAN_NONE;
8103 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008104 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008105 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008106 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008107 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008108 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008109
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008110 if (tpacpi_is_ibm()) {
8111 TPACPI_ACPIHANDLE_INIT(fans);
8112 TPACPI_ACPIHANDLE_INIT(gfan);
8113 TPACPI_ACPIHANDLE_INIT(sfan);
8114 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008115
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008116 quirks = tpacpi_check_quirks(fan_quirk_table,
8117 ARRAY_SIZE(fan_quirk_table));
8118
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008119 if (gfan_handle) {
8120 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008121 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008122 } else {
8123 /* all other ThinkPads: note that even old-style
8124 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008125 if (likely(acpi_ec_read(fan_status_offset,
8126 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008127 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008128 if (quirks & TPACPI_FAN_Q1)
8129 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008130 if (quirks & TPACPI_FAN_2FAN) {
8131 tp_features.second_fan = 1;
8132 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8133 "secondary fan support enabled\n");
8134 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008135 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008136 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008137 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008138 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008139 }
8140 }
8141
8142 if (sfan_handle) {
8143 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008144 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008145 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008146 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008147 } else {
8148 if (!gfan_handle) {
8149 /* gfan without sfan means no fan control */
8150 /* all other models implement TP EC 0x2f control */
8151
8152 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008153 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008154 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008155 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008156 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008157 TPACPI_FAN_CMD_SPEED |
8158 TPACPI_FAN_CMD_LEVEL |
8159 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008160 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008161 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008162 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008163 TPACPI_FAN_CMD_LEVEL |
8164 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008165 }
8166 }
8167 }
8168
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008169 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8170 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008171 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8172 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8173 fan_status_access_mode, fan_control_access_mode);
8174
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008175 /* fan control master switch */
8176 if (!fan_control_allowed) {
8177 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8178 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008179 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008180 "fan control features disabled by parameter\n");
8181 }
8182
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008183 /* update fan_control_desired_level */
8184 if (fan_status_access_mode != TPACPI_FAN_NONE)
8185 fan_get_status_safe(NULL);
8186
8187 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8188 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008189 if (tp_features.second_fan) {
8190 /* attach second fan tachometer */
8191 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8192 &dev_attr_fan_fan2_input.attr;
8193 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008194 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008195 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008196 if (rc < 0)
8197 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008198
8199 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8200 &driver_attr_fan_watchdog);
8201 if (rc < 0) {
8202 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8203 &fan_attr_group);
8204 return rc;
8205 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008206 return 0;
8207 } else
8208 return 1;
8209}
8210
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008211static void fan_exit(void)
8212{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008213 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008214 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008215
8216 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008217 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008218 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8219 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008220
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008221 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008222 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008223}
8224
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008225static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008226{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008227 int rc;
8228
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008229 if (!fan_control_allowed)
8230 return;
8231
8232 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008233 fan_control_resume_level = 0;
8234 rc = fan_get_status_safe(&fan_control_resume_level);
8235 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008236 pr_notice("failed to read fan level for later "
8237 "restore during resume: %d\n", rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008238
8239 /* if it is undefined, don't attempt to restore it.
8240 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008241 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008242 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008243}
8244
8245static void fan_resume(void)
8246{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008247 u8 current_level = 7;
8248 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008249 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008250
8251 /* DSDT *always* updates status on resume */
8252 tp_features.fan_ctrl_status_undef = 0;
8253
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008254 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008255 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008256 (fan_get_status_safe(&current_level) < 0))
8257 return;
8258
8259 switch (fan_control_access_mode) {
8260 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008261 /* never decrease fan level */
8262 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008263 break;
8264 case TPACPI_FAN_WR_ACPI_FANS:
8265 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008266 /* never decrease fan level, scale is:
8267 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8268 *
8269 * We expect the firmware to set either 7 or AUTO, but we
8270 * handle FULLSPEED out of paranoia.
8271 *
8272 * So, we can safely only restore FULLSPEED or 7, anything
8273 * else could slow the fan. Restoring AUTO is useless, at
8274 * best that's exactly what the DSDT already set (it is the
8275 * slower it uses).
8276 *
8277 * Always keep in mind that the DSDT *will* have set the
8278 * fans to what the vendor supposes is the best level. We
8279 * muck with it only to speed the fan up.
8280 */
8281 if (fan_control_resume_level != 7 &&
8282 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8283 return;
8284 else
8285 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8286 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008287 break;
8288 default:
8289 return;
8290 }
8291 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008292 pr_notice("restoring fan level to 0x%02x\n",
8293 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008294 rc = fan_set_level_safe(fan_control_resume_level);
8295 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008296 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008297 }
8298}
8299
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008300static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008301{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008302 int rc;
8303 u8 status;
8304 unsigned int speed = 0;
8305
8306 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008307 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008308 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008309 rc = fan_get_status_safe(&status);
8310 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008311 return rc;
8312
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008313 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008314 "level:\t\t%d\n",
8315 (status != 0) ? "enabled" : "disabled", status);
8316 break;
8317
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008318 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008319 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008320 rc = fan_get_status_safe(&status);
8321 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008322 return rc;
8323
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008324 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008325 (status != 0) ? "enabled" : "disabled");
8326
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008327 rc = fan_get_speed(&speed);
8328 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008329 return rc;
8330
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008331 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008332
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008333 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008334 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008335 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008336 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008337 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008338 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008339 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008340 break;
8341
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008342 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008343 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008344 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008345 }
8346
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008347 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008348 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008349
8350 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008351 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008352 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008353 break;
8354
8355 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008356 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008357 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008358 break;
8359 }
8360 }
8361
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008362 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008363 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008364 "commands:\twatchdog <timeout> (<timeout> "
8365 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008366
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008367 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008368 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008369 " (<speed> is 0-65535)\n");
8370
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008371 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008372}
8373
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008374static int fan_write_cmd_level(const char *cmd, int *rc)
8375{
8376 int level;
8377
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008378 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008379 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008380 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008381 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008382 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008383 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008384 return 0;
8385
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008386 *rc = fan_set_level_safe(level);
8387 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008388 pr_err("level command accepted for unsupported access mode %d\n",
8389 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008390 else if (!*rc)
8391 tpacpi_disclose_usertask("procfs fan",
8392 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008393
8394 return 1;
8395}
8396
8397static int fan_write_cmd_enable(const char *cmd, int *rc)
8398{
8399 if (strlencmp(cmd, "enable") != 0)
8400 return 0;
8401
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008402 *rc = fan_set_enable();
8403 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008404 pr_err("enable command accepted for unsupported access mode %d\n",
8405 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008406 else if (!*rc)
8407 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008408
8409 return 1;
8410}
8411
8412static int fan_write_cmd_disable(const char *cmd, int *rc)
8413{
8414 if (strlencmp(cmd, "disable") != 0)
8415 return 0;
8416
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008417 *rc = fan_set_disable();
8418 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008419 pr_err("disable command accepted for unsupported access mode %d\n",
8420 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008421 else if (!*rc)
8422 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008423
8424 return 1;
8425}
8426
8427static int fan_write_cmd_speed(const char *cmd, int *rc)
8428{
8429 int speed;
8430
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008431 /* TODO:
8432 * Support speed <low> <medium> <high> ? */
8433
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008434 if (sscanf(cmd, "speed %d", &speed) != 1)
8435 return 0;
8436
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008437 *rc = fan_set_speed(speed);
8438 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008439 pr_err("speed command accepted for unsupported access mode %d\n",
8440 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008441 else if (!*rc)
8442 tpacpi_disclose_usertask("procfs fan",
8443 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008444
8445 return 1;
8446}
8447
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008448static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8449{
8450 int interval;
8451
8452 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8453 return 0;
8454
8455 if (interval < 0 || interval > 120)
8456 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008457 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008458 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008459 tpacpi_disclose_usertask("procfs fan",
8460 "set watchdog timer to %d\n",
8461 interval);
8462 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008463
8464 return 1;
8465}
8466
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008467static int fan_write(char *buf)
8468{
8469 char *cmd;
8470 int rc = 0;
8471
8472 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008473 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008474 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008475 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008476 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008477 fan_write_cmd_disable(cmd, &rc) ||
8478 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008479 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008480 fan_write_cmd_speed(cmd, &rc))
8481 )
8482 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008483 else if (!rc)
8484 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008485 }
8486
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008487 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008488}
8489
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008490static struct ibm_struct fan_driver_data = {
8491 .name = "fan",
8492 .read = fan_read,
8493 .write = fan_write,
8494 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008495 .suspend = fan_suspend,
8496 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008497};
8498
David Henningsson420f9732013-10-16 23:10:31 +02008499/*************************************************************************
8500 * Mute LED subdriver
8501 */
8502
8503
8504struct tp_led_table {
8505 acpi_string name;
8506 int on_value;
8507 int off_value;
8508 int state;
8509};
8510
8511static struct tp_led_table led_tables[] = {
8512 [TPACPI_LED_MUTE] = {
8513 .name = "SSMS",
8514 .on_value = 1,
8515 .off_value = 0,
8516 },
8517 [TPACPI_LED_MICMUTE] = {
8518 .name = "MMTS",
8519 .on_value = 2,
8520 .off_value = 0,
8521 },
8522};
8523
8524static int mute_led_on_off(struct tp_led_table *t, bool state)
8525{
8526 acpi_handle temp;
8527 int output;
8528
8529 if (!ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp))) {
8530 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
8531 return -EIO;
8532 }
8533
8534 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
8535 state ? t->on_value : t->off_value))
8536 return -EIO;
8537
8538 t->state = state;
8539 return state;
8540}
8541
8542int tpacpi_led_set(int whichled, bool on)
8543{
8544 struct tp_led_table *t;
8545
8546 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
8547 return -EINVAL;
8548
8549 t = &led_tables[whichled];
8550 if (t->state < 0 || t->state == on)
8551 return t->state;
8552 return mute_led_on_off(t, on);
8553}
8554EXPORT_SYMBOL_GPL(tpacpi_led_set);
8555
8556static int mute_led_init(struct ibm_init_struct *iibm)
8557{
8558 acpi_handle temp;
8559 int i;
8560
8561 for (i = 0; i < TPACPI_LED_MAX; i++) {
8562 struct tp_led_table *t = &led_tables[i];
8563 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
8564 mute_led_on_off(t, false);
8565 else
8566 t->state = -ENODEV;
8567 }
8568 return 0;
8569}
8570
8571static void mute_led_exit(void)
8572{
8573 int i;
8574
8575 for (i = 0; i < TPACPI_LED_MAX; i++)
8576 tpacpi_led_set(i, false);
8577}
8578
Takashi Iwai119f4492014-02-12 16:32:45 +01008579static void mute_led_resume(void)
8580{
8581 int i;
8582
8583 for (i = 0; i < TPACPI_LED_MAX; i++) {
8584 struct tp_led_table *t = &led_tables[i];
8585 if (t->state >= 0)
8586 mute_led_on_off(t, t->state);
8587 }
8588}
8589
David Henningsson420f9732013-10-16 23:10:31 +02008590static struct ibm_struct mute_led_driver_data = {
8591 .name = "mute_led",
8592 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01008593 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02008594};
8595
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008596/****************************************************************************
8597 ****************************************************************************
8598 *
8599 * Infrastructure
8600 *
8601 ****************************************************************************
8602 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008603
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008604/*
8605 * HKEY event callout for other subdrivers go here
8606 * (yes, it is ugly, but it is quick, safe, and gets the job done
8607 */
8608static void tpacpi_driver_event(const unsigned int hkey_event)
8609{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008610 if (ibm_backlight_device) {
8611 switch (hkey_event) {
8612 case TP_HKEY_EV_BRGHT_UP:
8613 case TP_HKEY_EV_BRGHT_DOWN:
8614 tpacpi_brightness_notify_change();
8615 }
8616 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008617 if (alsa_card) {
8618 switch (hkey_event) {
8619 case TP_HKEY_EV_VOL_UP:
8620 case TP_HKEY_EV_VOL_DOWN:
8621 case TP_HKEY_EV_VOL_MUTE:
8622 volume_alsa_notify_change();
8623 }
8624 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008625}
8626
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008627static void hotkey_driver_event(const unsigned int scancode)
8628{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008629 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008630}
8631
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008632/* sysfs name ---------------------------------------------------------- */
8633static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8634 struct device_attribute *attr,
8635 char *buf)
8636{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008637 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008638}
8639
8640static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8641 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8642
8643/* --------------------------------------------------------------------- */
8644
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008645/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03008646static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008647
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008648/*
8649 * Module and infrastructure proble, init and exit handling
8650 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008651
Rusty Russell90ab5ee2012-01-13 09:32:20 +10308652static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008653
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008654#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008655static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008656{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008657 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008658
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008659 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008660}
8661#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8662
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008663static void ibm_exit(struct ibm_struct *ibm)
8664{
8665 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8666
8667 list_del_init(&ibm->all_drivers);
8668
8669 if (ibm->flags.acpi_notify_installed) {
8670 dbg_printk(TPACPI_DBG_EXIT,
8671 "%s: acpi_remove_notify_handler\n", ibm->name);
8672 BUG_ON(!ibm->acpi);
8673 acpi_remove_notify_handler(*ibm->acpi->handle,
8674 ibm->acpi->type,
8675 dispatch_acpi_notify);
8676 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008677 }
8678
8679 if (ibm->flags.proc_created) {
8680 dbg_printk(TPACPI_DBG_EXIT,
8681 "%s: remove_proc_entry\n", ibm->name);
8682 remove_proc_entry(ibm->name, proc_dir);
8683 ibm->flags.proc_created = 0;
8684 }
8685
8686 if (ibm->flags.acpi_driver_registered) {
8687 dbg_printk(TPACPI_DBG_EXIT,
8688 "%s: acpi_bus_unregister_driver\n", ibm->name);
8689 BUG_ON(!ibm->acpi);
8690 acpi_bus_unregister_driver(ibm->acpi->driver);
8691 kfree(ibm->acpi->driver);
8692 ibm->acpi->driver = NULL;
8693 ibm->flags.acpi_driver_registered = 0;
8694 }
8695
8696 if (ibm->flags.init_called && ibm->exit) {
8697 ibm->exit();
8698 ibm->flags.init_called = 0;
8699 }
8700
8701 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8702}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008703
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008704static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008705{
8706 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008707 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008708 struct proc_dir_entry *entry;
8709
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008710 BUG_ON(ibm == NULL);
8711
8712 INIT_LIST_HEAD(&ibm->all_drivers);
8713
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008714 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008715 return 0;
8716
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008717 dbg_printk(TPACPI_DBG_INIT,
8718 "probing for %s\n", ibm->name);
8719
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008720 if (iibm->init) {
8721 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008722 if (ret > 0)
8723 return 0; /* probe failed */
8724 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008725 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008726
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008727 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008728 }
8729
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008730 if (ibm->acpi) {
8731 if (ibm->acpi->hid) {
8732 ret = register_tpacpi_subdriver(ibm);
8733 if (ret)
8734 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008735 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008736
8737 if (ibm->acpi->notify) {
8738 ret = setup_acpi_notify(ibm);
8739 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07008740 pr_notice("disabling subdriver %s\n",
8741 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008742 ret = 0;
8743 goto err_out;
8744 }
8745 if (ret < 0)
8746 goto err_out;
8747 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008748 }
8749
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008750 dbg_printk(TPACPI_DBG_INIT,
8751 "%s installed\n", ibm->name);
8752
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008753 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04008754 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008755
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008756 if (!mode)
8757 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008758 if (ibm->write)
8759 mode |= S_IWUSR;
8760 entry = proc_create_data(ibm->name, mode, proc_dir,
8761 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008762 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07008763 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008764 ret = -ENODEV;
8765 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008766 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008767 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008769
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008770 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8771
Linus Torvalds1da177e2005-04-16 15:20:36 -07008772 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008773
8774err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008775 dbg_printk(TPACPI_DBG_INIT,
8776 "%s: at error exit path with result %d\n",
8777 ibm->name, ret);
8778
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008779 ibm_exit(ibm);
8780 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008781}
8782
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008783/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008784
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008785static bool __pure __init tpacpi_is_fw_digit(const char c)
8786{
8787 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8788}
8789
8790/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8791static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8792 const char t)
8793{
8794 return s && strlen(s) >= 8 &&
8795 tpacpi_is_fw_digit(s[0]) &&
8796 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05008797 s[2] == t &&
8798 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008799 tpacpi_is_fw_digit(s[4]) &&
Keith Packardb569ab32011-03-31 15:22:33 -07008800 tpacpi_is_fw_digit(s[5]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008801}
8802
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008803/* returns 0 - probe ok, or < 0 - probe error.
8804 * Probe ok doesn't mean thinkpad found.
8805 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8806static int __must_check __init get_thinkpad_model_data(
8807 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008808{
Jeff Garzik18552562007-10-03 15:15:40 -04008809 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008810 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008811 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008812
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008813 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008814 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008815
8816 memset(tp, 0, sizeof(*tp));
8817
8818 if (dmi_name_in_vendors("IBM"))
8819 tp->vendor = PCI_VENDOR_ID_IBM;
8820 else if (dmi_name_in_vendors("LENOVO"))
8821 tp->vendor = PCI_VENDOR_ID_LENOVO;
8822 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008823 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008824
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008825 s = dmi_get_system_info(DMI_BIOS_VERSION);
8826 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8827 if (s && !tp->bios_version_str)
8828 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008829
8830 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05008831 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
8832 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008833 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008834
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008835 tp->bios_model = tp->bios_version_str[0]
8836 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008837 tp->bios_release = (tp->bios_version_str[4] << 8)
8838 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008839
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008840 /*
8841 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8842 * X32 or newer, all Z series; Some models must have an
8843 * up-to-date BIOS or they will not be detected.
8844 *
8845 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8846 */
8847 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008848 if (sscanf(dev->name,
8849 "IBM ThinkPad Embedded Controller -[%17c",
8850 ec_fw_string) == 1) {
8851 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8852 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008853
8854 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008855 if (!tp->ec_version_str)
8856 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008857
8858 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8859 tp->ec_model = ec_fw_string[0]
8860 | (ec_fw_string[1] << 8);
8861 tp->ec_release = (ec_fw_string[4] << 8)
8862 | ec_fw_string[5];
8863 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008864 pr_notice("ThinkPad firmware release %s "
8865 "doesn't match the known patterns\n",
8866 ec_fw_string);
8867 pr_notice("please report this to %s\n",
8868 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008869 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008870 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008871 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008872 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008873
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008874 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Manoj Iyer23b05312012-03-09 17:32:24 -06008875 if (s && !(strnicmp(s, "ThinkPad", 8) && strnicmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008876 tp->model_str = kstrdup(s, GFP_KERNEL);
8877 if (!tp->model_str)
8878 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05008879 } else {
8880 s = dmi_get_system_info(DMI_BIOS_VENDOR);
8881 if (s && !(strnicmp(s, "Lenovo", 6))) {
8882 tp->model_str = kstrdup(s, GFP_KERNEL);
8883 if (!tp->model_str)
8884 return -ENOMEM;
8885 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008886 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008887
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008888 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8889 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8890 if (s && !tp->nummodel_str)
8891 return -ENOMEM;
8892
8893 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894}
8895
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008896static int __init probe_for_thinkpad(void)
8897{
8898 int is_thinkpad;
8899
8900 if (acpi_disabled)
8901 return -ENODEV;
8902
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008903 /* It would be dangerous to run the driver in this case */
8904 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8905 return -ENODEV;
8906
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008907 /*
8908 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008909 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008910 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008911 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8912 (thinkpad_id.ec_model != 0) ||
8913 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008914
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03008915 /* The EC handler is required */
8916 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008917 if (!ec_handle) {
8918 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07008919 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008920 return -ENODEV;
8921 }
8922
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008923 if (!is_thinkpad && !force_load)
8924 return -ENODEV;
8925
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008926 return 0;
8927}
8928
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008929static void __init thinkpad_acpi_init_banner(void)
8930{
Joe Perches0978e012011-04-04 10:06:25 -07008931 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8932 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008933
Joe Perches0978e012011-04-04 10:06:25 -07008934 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008935 (thinkpad_id.bios_version_str) ?
8936 thinkpad_id.bios_version_str : "unknown",
8937 (thinkpad_id.ec_version_str) ?
8938 thinkpad_id.ec_version_str : "unknown");
8939
8940 BUG_ON(!thinkpad_id.vendor);
8941
8942 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07008943 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008944 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8945 "IBM" : ((thinkpad_id.vendor ==
8946 PCI_VENDOR_ID_LENOVO) ?
8947 "Lenovo" : "Unknown vendor"),
8948 thinkpad_id.model_str,
8949 (thinkpad_id.nummodel_str) ?
8950 thinkpad_id.nummodel_str : "unknown");
8951}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008952
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008953/* Module init, exit, parameters */
8954
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008955static struct ibm_init_struct ibms_init[] __initdata = {
8956 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008957 .data = &thinkpad_acpi_driver_data,
8958 },
8959 {
8960 .init = hotkey_init,
8961 .data = &hotkey_driver_data,
8962 },
8963 {
8964 .init = bluetooth_init,
8965 .data = &bluetooth_driver_data,
8966 },
8967 {
8968 .init = wan_init,
8969 .data = &wan_driver_data,
8970 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008971 {
8972 .init = uwb_init,
8973 .data = &uwb_driver_data,
8974 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008975#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008976 {
8977 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008978 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008979 .data = &video_driver_data,
8980 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008981#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008982 {
8983 .init = light_init,
8984 .data = &light_driver_data,
8985 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008986 {
8987 .init = cmos_init,
8988 .data = &cmos_driver_data,
8989 },
8990 {
8991 .init = led_init,
8992 .data = &led_driver_data,
8993 },
8994 {
8995 .init = beep_init,
8996 .data = &beep_driver_data,
8997 },
8998 {
8999 .init = thermal_init,
9000 .data = &thermal_driver_data,
9001 },
9002 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009003 .init = brightness_init,
9004 .data = &brightness_driver_data,
9005 },
9006 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009007 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009008 .data = &volume_driver_data,
9009 },
9010 {
9011 .init = fan_init,
9012 .data = &fan_driver_data,
9013 },
David Henningsson420f9732013-10-16 23:10:31 +02009014 {
9015 .init = mute_led_init,
9016 .data = &mute_led_driver_data,
9017 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009018};
9019
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009020static int __init set_ibm_param(const char *val, struct kernel_param *kp)
9021{
9022 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009023 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009024
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009025 if (!kp || !kp->name || !val)
9026 return -EINVAL;
9027
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009028 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9029 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009030 WARN_ON(ibm == NULL);
9031
9032 if (!ibm || !ibm->name)
9033 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009034
9035 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9036 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009037 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009038 strcpy(ibms_init[i].param, val);
9039 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009040 return 0;
9041 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009042 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009043
9044 return -EINVAL;
9045}
9046
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009047module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009048MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009049 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009050
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009051module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009052MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009053
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009054module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009055MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009056 "Attempts to load the driver even on a "
9057 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009058
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009059module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009060MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009061 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03009062
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009063module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009064MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009065 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00009066 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03009067
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009068module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009069MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009070 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02009071
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009072#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009073module_param_named(volume_mode, volume_mode, uint, 0444);
9074MODULE_PARM_DESC(volume_mode,
9075 "Selects volume control strategy: "
9076 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9077
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02009078module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9079MODULE_PARM_DESC(volume_capabilities,
9080 "Selects the mixer capabilites: "
9081 "0=auto, 1=volume and mute, 2=mute only");
9082
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02009083module_param_named(volume_control, volume_control_allowed, bool, 0444);
9084MODULE_PARM_DESC(volume_control,
9085 "Enables software override for the console audio "
9086 "control when true");
9087
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009088/* ALSA module API parameters */
9089module_param_named(index, alsa_index, int, 0444);
9090MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9091module_param_named(id, alsa_id, charp, 0444);
9092MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9093module_param_named(enable, alsa_enable, bool, 0444);
9094MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009095#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009096
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009097#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009098 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02009099 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009100 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009101
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009102TPACPI_PARAM(hotkey);
9103TPACPI_PARAM(bluetooth);
9104TPACPI_PARAM(video);
9105TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009106TPACPI_PARAM(cmos);
9107TPACPI_PARAM(led);
9108TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009109TPACPI_PARAM(brightness);
9110TPACPI_PARAM(volume);
9111TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009112
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009113#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009114module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009115MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9116module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9117MODULE_PARM_DESC(wlsw_state,
9118 "Initial state of the emulated WLSW switch");
9119
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009120module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009121MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9122module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9123MODULE_PARM_DESC(bluetooth_state,
9124 "Initial state of the emulated bluetooth switch");
9125
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009126module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009127MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9128module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9129MODULE_PARM_DESC(wwan_state,
9130 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009131
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00009132module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009133MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9134module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9135MODULE_PARM_DESC(uwb_state,
9136 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009137#endif
9138
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009139static void thinkpad_acpi_module_exit(void)
9140{
9141 struct ibm_struct *ibm, *itmp;
9142
9143 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9144
9145 list_for_each_entry_safe_reverse(ibm, itmp,
9146 &tpacpi_all_drivers,
9147 all_drivers) {
9148 ibm_exit(ibm);
9149 }
9150
9151 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9152
9153 if (tpacpi_inputdev) {
9154 if (tp_features.input_device_registered)
9155 input_unregister_device(tpacpi_inputdev);
9156 else
9157 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +10009158 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009159 }
9160
9161 if (tpacpi_hwmon)
9162 hwmon_device_unregister(tpacpi_hwmon);
9163
9164 if (tp_features.sensors_pdev_attrs_registered)
9165 device_remove_file(&tpacpi_sensors_pdev->dev,
9166 &dev_attr_thinkpad_acpi_pdev_name);
9167 if (tpacpi_sensors_pdev)
9168 platform_device_unregister(tpacpi_sensors_pdev);
9169 if (tpacpi_pdev)
9170 platform_device_unregister(tpacpi_pdev);
9171
9172 if (tp_features.sensors_pdrv_attrs_registered)
9173 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9174 if (tp_features.platform_drv_attrs_registered)
9175 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9176
9177 if (tp_features.sensors_pdrv_registered)
9178 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9179
9180 if (tp_features.platform_drv_registered)
9181 platform_driver_unregister(&tpacpi_pdriver);
9182
9183 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009184 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009185
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009186 if (tpacpi_wq)
9187 destroy_workqueue(tpacpi_wq);
9188
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009189 kfree(thinkpad_id.bios_version_str);
9190 kfree(thinkpad_id.ec_version_str);
9191 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +10009192 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009193}
9194
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009195
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009196static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009197{
9198 int ret, i;
9199
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009200 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9201
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009202 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009203
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009204 ret = get_thinkpad_model_data(&thinkpad_id);
9205 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009206 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009207 thinkpad_acpi_module_exit();
9208 return ret;
9209 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009210 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009211 if (ret) {
9212 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009213 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009215
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009216 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009217
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009218 thinkpad_acpi_init_banner();
9219 tpacpi_check_outdated_fw();
9220
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009221 TPACPI_ACPIHANDLE_INIT(ecrd);
9222 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009223
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009224 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9225 if (!tpacpi_wq) {
9226 thinkpad_acpi_module_exit();
9227 return -ENOMEM;
9228 }
9229
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009230 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07009232 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009233 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009234 return -ENODEV;
9235 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009236
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009237 ret = platform_driver_register(&tpacpi_pdriver);
9238 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009239 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009240 thinkpad_acpi_module_exit();
9241 return ret;
9242 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009243 tp_features.platform_drv_registered = 1;
9244
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009245 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9246 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009247 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009248 thinkpad_acpi_module_exit();
9249 return ret;
9250 }
9251 tp_features.sensors_pdrv_registered = 1;
9252
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009253 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009254 if (!ret) {
9255 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009256 ret = tpacpi_create_driver_attributes(
9257 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009258 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009259 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009260 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009261 thinkpad_acpi_module_exit();
9262 return ret;
9263 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009264 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009265
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009266
9267 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009268 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009269 NULL, 0);
9270 if (IS_ERR(tpacpi_pdev)) {
9271 ret = PTR_ERR(tpacpi_pdev);
9272 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009273 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009274 thinkpad_acpi_module_exit();
9275 return ret;
9276 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009277 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009278 TPACPI_HWMON_DRVR_NAME,
9279 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009280 if (IS_ERR(tpacpi_sensors_pdev)) {
9281 ret = PTR_ERR(tpacpi_sensors_pdev);
9282 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009283 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009284 thinkpad_acpi_module_exit();
9285 return ret;
9286 }
9287 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9288 &dev_attr_thinkpad_acpi_pdev_name);
9289 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009290 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009291 thinkpad_acpi_module_exit();
9292 return ret;
9293 }
9294 tp_features.sensors_pdev_attrs_registered = 1;
9295 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009296 if (IS_ERR(tpacpi_hwmon)) {
9297 ret = PTR_ERR(tpacpi_hwmon);
9298 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009299 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009300 thinkpad_acpi_module_exit();
9301 return ret;
9302 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009303 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009304 tpacpi_inputdev = input_allocate_device();
9305 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009306 thinkpad_acpi_module_exit();
9307 return -ENOMEM;
9308 } else {
9309 /* Prepare input device, but don't register */
9310 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009311 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009312 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009313 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009314 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9315 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009316 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009317 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009318
9319 /* Init subdriver dependencies */
9320 tpacpi_detect_brightness_capabilities();
9321
9322 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009323 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9324 ret = ibm_init(&ibms_init[i]);
9325 if (ret >= 0 && *ibms_init[i].param)
9326 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009327 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009328 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329 return ret;
9330 }
9331 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009332
9333 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9334
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009335 ret = input_register_device(tpacpi_inputdev);
9336 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009337 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009338 thinkpad_acpi_module_exit();
9339 return ret;
9340 } else {
9341 tp_features.input_device_registered = 1;
9342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009343
9344 return 0;
9345}
9346
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009347MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9348
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009349/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009350 * This will autoload the driver in almost every ThinkPad
9351 * in widespread use.
9352 *
9353 * Only _VERY_ old models, like the 240, 240x and 570 lack
9354 * the HKEY event interface.
9355 */
9356MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9357
9358/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009359 * DMI matching for module autoloading
9360 *
9361 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9362 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9363 *
9364 * Only models listed in thinkwiki will be supported, so add yours
9365 * if it is not there yet.
9366 */
9367#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009368 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009369
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009370/* Ancient thinkpad BIOSes have to be identified by
9371 * BIOS type or model number, and there are far less
9372 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009373IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009374
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009375MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9376MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009377MODULE_DESCRIPTION(TPACPI_DESC);
9378MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009379MODULE_LICENSE("GPL");
9380
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009381module_init(thinkpad_acpi_module_init);
9382module_exit(thinkpad_acpi_module_exit);