blob: 024861db15871fe8cf70fbbe898a5e1b2c9cbfb8 [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 */
Xavier Naveira67ab6242015-02-10 08:45:18 +0100199 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300200
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300201 /* Thermal events */
202 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
203 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
204 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
205 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
206 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
207
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100208 /* AC-related events */
209 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300210
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300211 /* Misc */
212 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
213};
214
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200215/****************************************************************************
216 * Main driver
217 */
218
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200219#define TPACPI_NAME "thinkpad"
220#define TPACPI_DESC "ThinkPad ACPI Extras"
221#define TPACPI_FILE TPACPI_NAME "_acpi"
222#define TPACPI_URL "http://ibm-acpi.sf.net/"
223#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200224
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200225#define TPACPI_PROC_DIR "ibm"
226#define TPACPI_ACPI_EVENT_PREFIX "ibm"
227#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300228#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200229#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200230
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300231#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300232#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300233
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200234#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200235
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000236/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200237#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000238#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200239#define TPACPI_DBG_INIT 0x0001
240#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +0000241#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000242#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000243#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000244#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200245#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200246
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200247#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
248#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
249#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200250
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200251
252/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200253 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200254 */
255
256struct ibm_struct;
257
258struct tp_acpi_drv_struct {
259 const struct acpi_device_id *hid;
260 struct acpi_driver *driver;
261
262 void (*notify) (struct ibm_struct *, u32);
263 acpi_handle *handle;
264 u32 type;
265 struct acpi_device *device;
266};
267
268struct ibm_struct {
269 char *name;
270
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200271 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200272 int (*write) (char *);
273 void (*exit) (void);
274 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200275 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200276 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200277
278 struct list_head all_drivers;
279
280 struct tp_acpi_drv_struct *acpi;
281
282 struct {
283 u8 acpi_driver_registered:1;
284 u8 acpi_notify_installed:1;
285 u8 proc_created:1;
286 u8 init_called:1;
287 u8 experimental:1;
288 } flags;
289};
290
291struct ibm_init_struct {
292 char param[32];
293
294 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400295 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200296 struct ibm_struct *data;
297};
298
299static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200300 u32 bluetooth:1;
301 u32 hotkey:1;
302 u32 hotkey_mask:1;
303 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200304 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200305 u32 light:1;
306 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300307 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300308 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200309 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200310 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200311 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300312 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300313 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200314 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200315 u32 input_device_registered:1;
316 u32 platform_drv_registered:1;
317 u32 platform_drv_attrs_registered:1;
318 u32 sensors_pdrv_registered:1;
319 u32 sensors_pdrv_attrs_registered:1;
320 u32 sensors_pdev_attrs_registered:1;
321 u32 hotkey_poll_active:1;
Bastien Noceraf23a5bc2015-03-02 14:45:16 +0100322 u32 has_adaptive_kbd:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200323} tp_features;
324
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300325static struct {
326 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200327 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300328} tp_warned;
329
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200330struct thinkpad_id_data {
331 unsigned int vendor; /* ThinkPad vendor:
332 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
333
334 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
335 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
336
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300337 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200338 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300339 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
340 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200341
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300342 char *model_str; /* ThinkPad T43 */
343 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200344};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200345static struct thinkpad_id_data thinkpad_id;
346
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300347static enum {
348 TPACPI_LIFE_INIT = 0,
349 TPACPI_LIFE_RUNNING,
350 TPACPI_LIFE_EXITING,
351} tpacpi_lifecycle;
352
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200353static int experimental;
354static u32 dbg_level;
355
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300356static struct workqueue_struct *tpacpi_wq;
357
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000358enum led_status_t {
359 TPACPI_LED_OFF = 0,
360 TPACPI_LED_ON,
361 TPACPI_LED_BLINK,
362};
363
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300364/* Special LED class that can defer work */
365struct tpacpi_led_classdev {
366 struct led_classdev led_classdev;
367 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000368 enum led_status_t new_state;
Adam Leeedf2d772013-06-08 16:51:15 +0800369 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300370};
371
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300372/* brightness level capabilities */
373static unsigned int bright_maxlvl; /* 0 = unknown */
374
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200375#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
376static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030377static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200378static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030379static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200380static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030381static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200382static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030383static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200384#endif
385
386
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000387/*************************************************************************
388 * Debugging helpers
389 */
390
Joe Perches0978e012011-04-04 10:06:25 -0700391#define dbg_printk(a_dbg_level, format, arg...) \
392do { \
393 if (dbg_level & (a_dbg_level)) \
394 printk(KERN_DEBUG pr_fmt("%s: " format), \
395 __func__, ##arg); \
396} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000397
398#ifdef CONFIG_THINKPAD_ACPI_DEBUG
399#define vdbg_printk dbg_printk
400static const char *str_supported(int is_supported);
401#else
Joe Perches0978e012011-04-04 10:06:25 -0700402static inline const char *str_supported(int is_supported) { return ""; }
403#define vdbg_printk(a_dbg_level, format, arg...) \
404 no_printk(format, ##arg)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000405#endif
406
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000407static void tpacpi_log_usertask(const char * const what)
408{
Joe Perches0978e012011-04-04 10:06:25 -0700409 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
410 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000411}
412
Joe Perches0978e012011-04-04 10:06:25 -0700413#define tpacpi_disclose_usertask(what, format, arg...) \
414do { \
415 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
416 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
417 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
418 what, task_tgid_vnr(current), ## arg); \
419 } \
420} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000421
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300422/*
423 * Quirk handling helpers
424 *
425 * ThinkPad IDs and versions seen in the field so far
426 * are two-characters from the set [0-9A-Z], i.e. base 36.
427 *
428 * We use values well outside that range as specials.
429 */
430
431#define TPACPI_MATCH_ANY 0xffffU
432#define TPACPI_MATCH_UNKNOWN 0U
433
434/* TPID('1', 'Y') == 0x5931 */
435#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
436
437#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
438 { .vendor = PCI_VENDOR_ID_IBM, \
439 .bios = TPID(__id1, __id2), \
440 .ec = TPACPI_MATCH_ANY, \
441 .quirks = (__quirk) }
442
443#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
444 { .vendor = PCI_VENDOR_ID_LENOVO, \
445 .bios = TPID(__id1, __id2), \
446 .ec = TPACPI_MATCH_ANY, \
447 .quirks = (__quirk) }
448
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200449#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
450 { .vendor = PCI_VENDOR_ID_LENOVO, \
451 .bios = TPACPI_MATCH_ANY, \
452 .ec = TPID(__id1, __id2), \
453 .quirks = (__quirk) }
454
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300455struct tpacpi_quirk {
456 unsigned int vendor;
457 u16 bios;
458 u16 ec;
459 unsigned long quirks;
460};
461
462/**
463 * tpacpi_check_quirks() - search BIOS/EC version on a list
464 * @qlist: array of &struct tpacpi_quirk
465 * @qlist_size: number of elements in @qlist
466 *
467 * Iterates over a quirks list until one is found that matches the
468 * ThinkPad's vendor, BIOS and EC model.
469 *
470 * Returns 0 if nothing matches, otherwise returns the quirks field of
471 * the matching &struct tpacpi_quirk entry.
472 *
473 * The match criteria is: vendor, ec and bios much match.
474 */
475static unsigned long __init tpacpi_check_quirks(
476 const struct tpacpi_quirk *qlist,
477 unsigned int qlist_size)
478{
479 while (qlist_size) {
480 if ((qlist->vendor == thinkpad_id.vendor ||
481 qlist->vendor == TPACPI_MATCH_ANY) &&
482 (qlist->bios == thinkpad_id.bios_model ||
483 qlist->bios == TPACPI_MATCH_ANY) &&
484 (qlist->ec == thinkpad_id.ec_model ||
485 qlist->ec == TPACPI_MATCH_ANY))
486 return qlist->quirks;
487
488 qlist_size--;
489 qlist++;
490 }
491 return 0;
492}
493
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300494static inline bool __pure __init tpacpi_is_lenovo(void)
495{
496 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
497}
498
499static inline bool __pure __init tpacpi_is_ibm(void)
500{
501 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
502}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300503
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300504/****************************************************************************
505 ****************************************************************************
506 *
507 * ACPI Helpers and device model
508 *
509 ****************************************************************************
510 ****************************************************************************/
511
512/*************************************************************************
513 * ACPI basic handles
514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300516static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300517static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200519#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700521 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300522 &parent##_handle; \
523 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200525TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
526TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200528TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
529 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400530 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
531 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300532 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400533
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200534TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400535 "^HKEY", /* R30, R31 */
536 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300537 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400538
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300539/*************************************************************************
540 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200541 */
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700544 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400547 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200548 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400549 struct acpi_buffer result, *resultp;
550 union acpi_object out_obj;
551 acpi_status status;
552 va_list ap;
553 char res_type;
554 int success;
555 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700558 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return 0;
560 }
561
562 if (*fmt == 'q') {
563 quiet = 1;
564 fmt++;
565 } else
566 quiet = 0;
567
568 res_type = *(fmt++);
569
570 params.count = 0;
571 params.pointer = &in_objs[0];
572
573 va_start(ap, fmt);
574 while (*fmt) {
575 char c = *(fmt++);
576 switch (c) {
577 case 'd': /* int */
578 in_objs[params.count].integer.value = va_arg(ap, int);
579 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
580 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400581 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 default:
Joe Perches0978e012011-04-04 10:06:25 -0700583 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 "with invalid format character '%c'\n", c);
Jesper Juhl21365852010-12-24 19:56:28 +0100585 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return 0;
587 }
588 }
589 va_end(ap);
590
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400591 if (res_type != 'v') {
592 result.length = sizeof(out_obj);
593 result.pointer = &out_obj;
594 resultp = &result;
595 } else
596 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400598 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400601 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300602 success = (status == AE_OK &&
603 out_obj.type == ACPI_TYPE_INTEGER);
604 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700605 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400607 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 success = status == AE_OK;
609 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400610 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 default:
Joe Perches0978e012011-04-04 10:06:25 -0700612 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 "with invalid format character '%c'\n", res_type);
614 return 0;
615 }
616
617 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700618 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300619 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 return success;
622}
623
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200624static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300625{
626 int v;
627
628 if (ecrd_handle) {
629 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
630 return 0;
631 *p = v;
632 } else {
633 if (ec_read(i, p) < 0)
634 return 0;
635 }
636
637 return 1;
638}
639
640static int acpi_ec_write(int i, u8 v)
641{
642 if (ecwr_handle) {
643 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
644 return 0;
645 } else {
646 if (ec_write(i, v) < 0)
647 return 0;
648 }
649
650 return 1;
651}
652
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300653static int issue_thinkpad_cmos_command(int cmos_cmd)
654{
655 if (!cmos_handle)
656 return -ENXIO;
657
658 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
659 return -EIO;
660
661 return 0;
662}
663
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300664/*************************************************************************
665 * ACPI device model
666 */
667
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200668#define TPACPI_ACPIHANDLE_INIT(object) \
669 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300670 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200671
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300672static void __init drv_acpi_handle_init(const char *name,
673 acpi_handle *handle, const acpi_handle parent,
674 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300675{
676 int i;
677 acpi_status status;
678
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300679 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
680 name);
681
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300682 for (i = 0; i < num_paths; i++) {
683 status = acpi_get_handle(parent, paths[i], handle);
684 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300685 dbg_printk(TPACPI_DBG_INIT,
686 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300687 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300688 return;
689 }
690 }
691
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300692 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
693 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300694 *handle = NULL;
695}
696
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300697static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
698 u32 level, void *context, void **return_value)
699{
Aaron Lu46445b62013-10-11 21:27:46 +0800700 struct acpi_device *dev;
701 if (!strcmp(context, "video")) {
702 if (acpi_bus_get_device(handle, &dev))
703 return AE_OK;
704 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
705 return AE_OK;
706 }
707
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300708 *(acpi_handle *)return_value = handle;
709
710 return AE_CTRL_TERMINATE;
711}
712
713static void __init tpacpi_acpi_handle_locate(const char *name,
714 const char *hid,
715 acpi_handle *handle)
716{
717 acpi_status status;
718 acpi_handle device_found;
719
Aaron Lu46445b62013-10-11 21:27:46 +0800720 BUG_ON(!name || !handle);
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300721 vdbg_printk(TPACPI_DBG_INIT,
722 "trying to locate ACPI handle for %s, using HID %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +0800723 name, hid ? hid : "NULL");
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300724
725 memset(&device_found, 0, sizeof(device_found));
726 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
727 (void *)name, &device_found);
728
729 *handle = NULL;
730
731 if (ACPI_SUCCESS(status)) {
732 *handle = device_found;
733 dbg_printk(TPACPI_DBG_INIT,
734 "Found ACPI handle for %s\n", name);
735 } else {
736 vdbg_printk(TPACPI_DBG_INIT,
737 "Could not locate an ACPI handle for %s: %s\n",
738 name, acpi_format_exception(status));
739 }
740}
741
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300742static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300743{
744 struct ibm_struct *ibm = data;
745
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300746 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
747 return;
748
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300749 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300750 return;
751
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300752 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300753}
754
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300755static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300756{
757 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300758 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300759
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300760 BUG_ON(!ibm->acpi);
761
762 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300763 return 0;
764
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300765 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300766 "setting up ACPI notify for %s\n", ibm->name);
767
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300768 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
769 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700770 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300771 return -ENODEV;
772 }
773
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700774 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300775 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200776 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300777 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300778
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300779 status = acpi_install_notify_handler(*ibm->acpi->handle,
780 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300781 if (ACPI_FAILURE(status)) {
782 if (status == AE_ALREADY_EXISTS) {
Joe Perches0978e012011-04-04 10:06:25 -0700783 pr_notice("another device driver is already "
784 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300785 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700786 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300787 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300788 }
789 return -ENODEV;
790 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300791 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300792 return 0;
793}
794
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300795static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300796{
797 return 0;
798}
799
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300800static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300801{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300802 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300803
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300804 dbg_printk(TPACPI_DBG_INIT,
805 "registering %s as an ACPI driver\n", ibm->name);
806
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300807 BUG_ON(!ibm->acpi);
808
809 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
810 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700811 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300812 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300813 }
814
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200815 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300816 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200817
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300818 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300819
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300820 rc = acpi_bus_register_driver(ibm->acpi->driver);
821 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700822 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200823 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300824 kfree(ibm->acpi->driver);
825 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300826 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300827 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300828
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300829 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300830}
831
832
833/****************************************************************************
834 ****************************************************************************
835 *
836 * Procfs Helpers
837 *
838 ****************************************************************************
839 ****************************************************************************/
840
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200841static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300842{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200843 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300844
845 if (!ibm || !ibm->read)
846 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200847 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300848}
849
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200850static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300851{
Al Virod9dda782013-03-31 18:16:14 -0400852 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200853}
854
855static ssize_t dispatch_proc_write(struct file *file,
856 const char __user *userbuf,
857 size_t count, loff_t *pos)
858{
Al Virod9dda782013-03-31 18:16:14 -0400859 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300860 char *kernbuf;
861 int ret;
862
863 if (!ibm || !ibm->write)
864 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300865 if (count > PAGE_SIZE - 2)
866 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300867
868 kernbuf = kmalloc(count + 2, GFP_KERNEL);
869 if (!kernbuf)
870 return -ENOMEM;
871
872 if (copy_from_user(kernbuf, userbuf, count)) {
873 kfree(kernbuf);
874 return -EFAULT;
875 }
876
877 kernbuf[count] = 0;
878 strcat(kernbuf, ",");
879 ret = ibm->write(kernbuf);
880 if (ret == 0)
881 ret = count;
882
883 kfree(kernbuf);
884
885 return ret;
886}
887
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200888static const struct file_operations dispatch_proc_fops = {
889 .owner = THIS_MODULE,
890 .open = dispatch_proc_open,
891 .read = seq_read,
892 .llseek = seq_lseek,
893 .release = single_release,
894 .write = dispatch_proc_write,
895};
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897static char *next_cmd(char **cmds)
898{
899 char *start = *cmds;
900 char *end;
901
902 while ((end = strchr(start, ',')) && end == start)
903 start = end + 1;
904
905 if (!end)
906 return NULL;
907
908 *end = 0;
909 *cmds = end + 1;
910 return start;
911}
912
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300913
914/****************************************************************************
915 ****************************************************************************
916 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300917 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300918 *
919 ****************************************************************************
920 ****************************************************************************/
921
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300922static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300923static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700924static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300925static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300926static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200927static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300928
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200929#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200930static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200931{
932 struct ibm_struct *ibm, *itmp;
933
934 list_for_each_entry_safe(ibm, itmp,
935 &tpacpi_all_drivers,
936 all_drivers) {
937 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200938 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200939 }
940
941 return 0;
942}
943
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200944static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300945{
946 struct ibm_struct *ibm, *itmp;
947
948 list_for_each_entry_safe(ibm, itmp,
949 &tpacpi_all_drivers,
950 all_drivers) {
951 if (ibm->resume)
952 (ibm->resume)();
953 }
954
955 return 0;
956}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200957#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300958
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200959static SIMPLE_DEV_PM_OPS(tpacpi_pm,
960 tpacpi_suspend_handler, tpacpi_resume_handler);
961
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200962static void tpacpi_shutdown_handler(struct platform_device *pdev)
963{
964 struct ibm_struct *ibm, *itmp;
965
966 list_for_each_entry_safe(ibm, itmp,
967 &tpacpi_all_drivers,
968 all_drivers) {
969 if (ibm->shutdown)
970 (ibm->shutdown)();
971 }
972}
973
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300974static struct platform_driver tpacpi_pdriver = {
975 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200976 .name = TPACPI_DRVR_NAME,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200977 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300978 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200979 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300980};
981
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300982static struct platform_driver tpacpi_hwmon_pdriver = {
983 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200984 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300985 },
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 Holschuhd64c81c42008-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 Holschuhd64c81c42008-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 Jenkinsb3fa13292009-06-08 13:27:27 +01001275 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001276 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa13292009-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 Jenkinsb3fa13292009-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
Bastien Nocera6a68d852015-03-02 14:45:31 +01001915 TP_ACPI_HOTKEYSCAN_MUTE2,
1916 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1917 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1918 TP_ACPI_HOTKEYSCAN_CLOUD,
1919 TP_ACPI_HOTKEYSCAN_UNK9,
1920 TP_ACPI_HOTKEYSCAN_VOICE,
1921 TP_ACPI_HOTKEYSCAN_UNK10,
1922 TP_ACPI_HOTKEYSCAN_GESTURES,
1923 TP_ACPI_HOTKEYSCAN_UNK11,
1924 TP_ACPI_HOTKEYSCAN_UNK12,
1925 TP_ACPI_HOTKEYSCAN_UNK13,
1926 TP_ACPI_HOTKEYSCAN_CONFIG,
1927 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1928 TP_ACPI_HOTKEYSCAN_RELOAD,
1929 TP_ACPI_HOTKEYSCAN_BACK,
1930 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1931 TP_ACPI_HOTKEYSCAN_MIC_UP,
1932 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1933 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1934 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1935
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001936 /* Hotkey keymap size */
1937 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001938};
1939
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001940enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001941 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1942 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1943};
1944
1945enum { /* Positions of some of the keys in hotkey masks */
1946 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1947 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1948 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1949 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1950 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1951 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1952 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1953 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1954 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1955 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1956 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1957};
1958
1959enum { /* NVRAM to ACPI HKEY group map */
1960 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1961 TP_ACPI_HKEY_ZOOM_MASK |
1962 TP_ACPI_HKEY_DISPSWTCH_MASK |
1963 TP_ACPI_HKEY_HIBERNATE_MASK,
1964 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1965 TP_ACPI_HKEY_BRGHTDWN_MASK,
1966 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1967 TP_ACPI_HKEY_VOLDWN_MASK |
1968 TP_ACPI_HKEY_MUTE_MASK,
1969};
1970
1971#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1972struct tp_nvram_state {
1973 u16 thinkpad_toggle:1;
1974 u16 zoom_toggle:1;
1975 u16 display_toggle:1;
1976 u16 thinklight_toggle:1;
1977 u16 hibernate_toggle:1;
1978 u16 displayexp_toggle:1;
1979 u16 display_state:1;
1980 u16 brightness_toggle:1;
1981 u16 volume_toggle:1;
1982 u16 mute:1;
1983
1984 u8 brightness_level;
1985 u8 volume_level;
1986};
1987
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001988/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001989static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001990
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001991/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001992 * Acquire mutex to write poller control variables as an
1993 * atomic block.
1994 *
1995 * Increment hotkey_config_change when changing them if you
1996 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001997 *
1998 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1999 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002000static struct mutex hotkey_thread_data_mutex;
2001static unsigned int hotkey_config_change;
2002
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002003/*
2004 * hotkey poller control variables
2005 *
2006 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002007 *
2008 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2009 * should be used only when the changes need to be taken as
2010 * a block, OR when one needs to force the kthread to forget
2011 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002012 */
2013static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2014static unsigned int hotkey_poll_freq = 10; /* Hz */
2015
2016#define HOTKEY_CONFIG_CRITICAL_START \
2017 do { \
2018 mutex_lock(&hotkey_thread_data_mutex); \
2019 hotkey_config_change++; \
2020 } while (0);
2021#define HOTKEY_CONFIG_CRITICAL_END \
2022 mutex_unlock(&hotkey_thread_data_mutex);
2023
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002024#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2025
2026#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002027#define HOTKEY_CONFIG_CRITICAL_START
2028#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002029
2030#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2031
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002032static struct mutex hotkey_mutex;
2033
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002034static enum { /* Reasons for waking up */
2035 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2036 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2037 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2038} hotkey_wakeup_reason;
2039
2040static int hotkey_autosleep_ack;
2041
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002042static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2043static u32 hotkey_all_mask; /* all events supported in fw */
2044static u32 hotkey_reserved_mask; /* events better left disabled */
2045static u32 hotkey_driver_mask; /* events needed by the driver */
2046static u32 hotkey_user_mask; /* events visible to userspace */
2047static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002048
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002049static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002050
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002051static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002052
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002053static void tpacpi_driver_event(const unsigned int hkey_event);
2054static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002055static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002056
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002057/* HKEY.MHKG() return bits */
2058#define TP_HOTKEY_TABLET_MASK (1 << 3)
2059
Johannes Berg19d337d2009-06-02 13:01:37 +02002060static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002061{
Johannes Berg19d337d2009-06-02 13:01:37 +02002062 int status;
2063
2064 if (!tp_features.hotkey_wlsw)
2065 return -ENODEV;
2066
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002067#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002068 if (dbg_wlswemul)
2069 return (tpacpi_wlsw_emulstate) ?
2070 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002071#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002072
2073 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002074 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002075
2076 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002077}
2078
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002079static int hotkey_get_tablet_mode(int *status)
2080{
2081 int s;
2082
2083 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2084 return -EIO;
2085
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002086 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2087 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002088}
2089
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002090/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002091 * Reads current event mask from firmware, and updates
2092 * hotkey_acpi_mask accordingly. Also resets any bits
2093 * from hotkey_user_mask that are unavailable to be
2094 * delivered (shadow requirement of the userspace ABI).
2095 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002096 * Call with hotkey_mutex held
2097 */
2098static int hotkey_mask_get(void)
2099{
2100 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002101 u32 m = 0;
2102
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002103 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002104 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002105
2106 hotkey_acpi_mask = m;
2107 } else {
2108 /* no mask support doesn't mean no event support... */
2109 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002110 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002111
2112 /* sync userspace-visible mask */
2113 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002114
2115 return 0;
2116}
2117
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002118void static hotkey_mask_warn_incomplete_mask(void)
2119{
2120 /* log only what the user can fix... */
2121 const u32 wantedmask = hotkey_driver_mask &
2122 ~(hotkey_acpi_mask | hotkey_source_mask) &
2123 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2124
2125 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002126 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002127}
2128
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002129/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002130 * Set the firmware mask when supported
2131 *
2132 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2133 *
2134 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2135 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002136 * Call with hotkey_mutex held
2137 */
2138static int hotkey_mask_set(u32 mask)
2139{
2140 int i;
2141 int rc = 0;
2142
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002143 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002144
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002145 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002146 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002147 if (!acpi_evalf(hkey_handle,
2148 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002149 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002150 rc = -EIO;
2151 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002152 }
2153 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002154 }
2155
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002156 /*
2157 * We *must* make an inconditional call to hotkey_mask_get to
2158 * refresh hotkey_acpi_mask and update hotkey_user_mask
2159 *
2160 * Take the opportunity to also log when we cannot _enable_
2161 * a given event.
2162 */
2163 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Joe Perches0978e012011-04-04 10:06:25 -07002164 pr_notice("asked for hotkey mask 0x%08x, but "
2165 "firmware forced it to 0x%08x\n",
2166 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002167 }
2168
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002169 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2170 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002171
2172 return rc;
2173}
2174
2175/*
2176 * Sets hotkey_user_mask and tries to set the firmware mask
2177 *
2178 * Call with hotkey_mutex held
2179 */
2180static int hotkey_user_mask_set(const u32 mask)
2181{
2182 int rc;
2183
2184 /* Give people a chance to notice they are doing something that
2185 * is bound to go boom on their users sooner or later */
2186 if (!tp_warned.hotkey_mask_ff &&
2187 (mask == 0xffff || mask == 0xffffff ||
2188 mask == 0xffffffff)) {
2189 tp_warned.hotkey_mask_ff = 1;
Joe Perches0978e012011-04-04 10:06:25 -07002190 pr_notice("setting the hotkey mask to 0x%08x is likely "
2191 "not the best way to go about it\n", mask);
2192 pr_notice("please consider using the driver defaults, "
2193 "and refer to up-to-date thinkpad-acpi "
2194 "documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002195 }
2196
2197 /* Try to enable what the user asked for, plus whatever we need.
2198 * this syncs everything but won't enable bits in hotkey_user_mask */
2199 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2200
2201 /* Enable the available bits in hotkey_user_mask */
2202 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2203
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002204 return rc;
2205}
2206
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002207/*
2208 * Sets the driver hotkey mask.
2209 *
2210 * Can be called even if the hotkey subdriver is inactive
2211 */
2212static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2213{
2214 int rc;
2215
2216 /* Do the right thing if hotkey_init has not been called yet */
2217 if (!tp_features.hotkey) {
2218 hotkey_driver_mask = mask;
2219 return 0;
2220 }
2221
2222 mutex_lock(&hotkey_mutex);
2223
2224 HOTKEY_CONFIG_CRITICAL_START
2225 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002226#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002227 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002228#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002229 HOTKEY_CONFIG_CRITICAL_END
2230
2231 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2232 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002233 hotkey_poll_setup(true);
2234
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002235 mutex_unlock(&hotkey_mutex);
2236
2237 return rc;
2238}
2239
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002240static int hotkey_status_get(int *status)
2241{
2242 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2243 return -EIO;
2244
2245 return 0;
2246}
2247
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002248static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002249{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002250 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002251 return -EIO;
2252
2253 return 0;
2254}
2255
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002256static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002257{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002258 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002259
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002260 if (tp_features.hotkey_tablet &&
2261 !hotkey_get_tablet_mode(&state)) {
2262 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002263
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002264 input_report_switch(tpacpi_inputdev,
2265 SW_TABLET_MODE, !!state);
2266 input_sync(tpacpi_inputdev);
2267
2268 mutex_unlock(&tpacpi_inputdev_send_mutex);
2269 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002270}
2271
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002272/* Do NOT call without validating scancode first */
2273static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002274{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002275 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002276
2277 if (keycode != KEY_RESERVED) {
2278 mutex_lock(&tpacpi_inputdev_send_mutex);
2279
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002280 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002281 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002282 input_sync(tpacpi_inputdev);
2283
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002284 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002285 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002286 input_sync(tpacpi_inputdev);
2287
2288 mutex_unlock(&tpacpi_inputdev_send_mutex);
2289 }
2290}
2291
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002292/* Do NOT call without validating scancode first */
2293static void tpacpi_input_send_key_masked(const unsigned int scancode)
2294{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002295 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002296 if (hotkey_user_mask & (1 << scancode))
2297 tpacpi_input_send_key(scancode);
2298}
2299
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002300#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2301static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2302
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002303/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002304static void tpacpi_hotkey_send_key(unsigned int scancode)
2305{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002306 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002307}
2308
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002309static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002310{
2311 u8 d;
2312
2313 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2314 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2315 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2316 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2317 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2318 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2319 }
2320 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2321 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2322 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2323 }
2324 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2325 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2326 n->displayexp_toggle =
2327 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2328 }
2329 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2330 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2331 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2332 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2333 n->brightness_toggle =
2334 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2335 }
2336 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2337 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2338 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2339 >> TP_NVRAM_POS_LEVEL_VOLUME;
2340 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2341 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2342 }
2343}
2344
Behan Webstera4d44ba2014-02-12 21:58:46 +01002345#define TPACPI_COMPARE_KEY(__scancode, __member) \
2346do { \
2347 if ((event_mask & (1 << __scancode)) && \
2348 oldn->__member != newn->__member) \
2349 tpacpi_hotkey_send_key(__scancode); \
2350} while (0)
2351
2352#define TPACPI_MAY_SEND_KEY(__scancode) \
2353do { \
2354 if (event_mask & (1 << __scancode)) \
2355 tpacpi_hotkey_send_key(__scancode); \
2356} while (0)
2357
2358static void issue_volchange(const unsigned int oldvol,
2359 const unsigned int newvol,
2360 const u32 event_mask)
2361{
2362 unsigned int i = oldvol;
2363
2364 while (i > newvol) {
2365 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2366 i--;
2367 }
2368 while (i < newvol) {
2369 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2370 i++;
2371 }
2372}
2373
2374static void issue_brightnesschange(const unsigned int oldbrt,
2375 const unsigned int newbrt,
2376 const u32 event_mask)
2377{
2378 unsigned int i = oldbrt;
2379
2380 while (i > newbrt) {
2381 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2382 i--;
2383 }
2384 while (i < newbrt) {
2385 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2386 i++;
2387 }
2388}
2389
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002390static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2391 struct tp_nvram_state *newn,
2392 const u32 event_mask)
2393{
2394
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002395 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2396 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2397 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2398 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2399
2400 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2401
2402 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2403
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002404 /*
2405 * Handle volume
2406 *
2407 * This code is supposed to duplicate the IBM firmware behaviour:
2408 * - Pressing MUTE issues mute hotkey message, even when already mute
2409 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002410 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002411 * - The act of unmuting issues volume up/down notification,
2412 * depending which key was used to unmute
2413 *
2414 * We are constrained to what the NVRAM can tell us, which is not much
2415 * and certainly not enough if more than one volume hotkey was pressed
2416 * since the last poll cycle.
2417 *
2418 * Just to make our life interesting, some newer Lenovo ThinkPads have
2419 * bugs in the BIOS and may fail to update volume_toggle properly.
2420 */
2421 if (newn->mute) {
2422 /* muted */
2423 if (!oldn->mute ||
2424 oldn->volume_toggle != newn->volume_toggle ||
2425 oldn->volume_level != newn->volume_level) {
2426 /* recently muted, or repeated mute keypress, or
2427 * multiple presses ending in mute */
Behan Webstera4d44ba2014-02-12 21:58:46 +01002428 issue_volchange(oldn->volume_level, newn->volume_level,
2429 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002430 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2431 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002432 } else {
2433 /* unmute */
2434 if (oldn->mute) {
2435 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002437 }
2438 if (oldn->volume_level != newn->volume_level) {
Behan Webstera4d44ba2014-02-12 21:58:46 +01002439 issue_volchange(oldn->volume_level, newn->volume_level,
2440 event_mask);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002441 } else if (oldn->volume_toggle != newn->volume_toggle) {
2442 /* repeated vol up/down keypress at end of scale ? */
2443 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002444 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002445 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2446 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002447 }
2448 }
2449
2450 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002451 if (oldn->brightness_level != newn->brightness_level) {
2452 issue_brightnesschange(oldn->brightness_level,
Behan Webstera4d44ba2014-02-12 21:58:46 +01002453 newn->brightness_level, event_mask);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002454 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2455 /* repeated key presses that didn't change state */
2456 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002457 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002458 else if (newn->brightness_level >= bright_maxlvl
2459 && !tp_features.bright_unkfw)
2460 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002461 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002462
2463#undef TPACPI_COMPARE_KEY
2464#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002465}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002466
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002467/*
2468 * Polling driver
2469 *
2470 * We track all events in hotkey_source_mask all the time, since
2471 * most of them are edge-based. We only issue those requested by
2472 * hotkey_user_mask or hotkey_driver_mask, though.
2473 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002474static int hotkey_kthread(void *data)
2475{
2476 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002477 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002478 unsigned int si, so;
2479 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002480 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002481 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002482 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002483
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002484 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2485 goto exit;
2486
2487 set_freezable();
2488
2489 so = 0;
2490 si = 1;
2491 t = 0;
2492
2493 /* Initial state for compares */
2494 mutex_lock(&hotkey_thread_data_mutex);
2495 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002496 poll_mask = hotkey_source_mask;
2497 event_mask = hotkey_source_mask &
2498 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002499 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002500 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002501 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002502
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002503 while (!kthread_should_stop()) {
2504 if (t == 0) {
2505 if (likely(poll_freq))
2506 t = 1000/poll_freq;
2507 else
2508 t = 100; /* should never happen... */
2509 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002510 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002511 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002512 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002513
2514 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002515 continue;
2516
2517 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002518 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002519 /* forget old state on thaw or config change */
2520 si = so;
2521 t = 0;
2522 change_detector = hotkey_config_change;
2523 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002524 poll_mask = hotkey_source_mask;
2525 event_mask = hotkey_source_mask &
2526 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002527 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002528 mutex_unlock(&hotkey_thread_data_mutex);
2529
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002530 if (likely(poll_mask)) {
2531 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002532 if (likely(si != so)) {
2533 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002534 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002535 }
2536 }
2537
2538 so = si;
2539 si ^= 1;
2540 }
2541
2542exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002543 return 0;
2544}
2545
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002546/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002547static void hotkey_poll_stop_sync(void)
2548{
2549 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002550 kthread_stop(tpacpi_hotkey_task);
2551 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002552 }
2553}
2554
2555/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002556static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002557{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002558 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2559 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002560
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002561 if (hotkey_poll_freq > 0 &&
2562 (poll_driver_mask ||
2563 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002564 if (!tpacpi_hotkey_task) {
2565 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002566 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002567 if (IS_ERR(tpacpi_hotkey_task)) {
2568 tpacpi_hotkey_task = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07002569 pr_err("could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002570 "for hotkey polling\n");
2571 }
2572 }
2573 } else {
2574 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002575 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002576 hotkey_poll_freq == 0) {
Joe Perches0978e012011-04-04 10:06:25 -07002577 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2578 "require polling, which is currently "
2579 "disabled\n",
2580 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002581 }
2582 }
2583}
2584
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002585static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002586{
2587 mutex_lock(&hotkey_mutex);
2588 hotkey_poll_setup(may_warn);
2589 mutex_unlock(&hotkey_mutex);
2590}
2591
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002592/* call with hotkey_mutex held */
2593static void hotkey_poll_set_freq(unsigned int freq)
2594{
2595 if (!freq)
2596 hotkey_poll_stop_sync();
2597
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002598 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002599}
2600
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002601#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2602
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002603static void hotkey_poll_setup(const bool __unused)
2604{
2605}
2606
2607static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002608{
2609}
2610
2611#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2612
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002613static int hotkey_inputdev_open(struct input_dev *dev)
2614{
2615 switch (tpacpi_lifecycle) {
2616 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002617 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002618 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002619 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002620 case TPACPI_LIFE_EXITING:
2621 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002622 }
2623
2624 /* Should only happen if tpacpi_lifecycle is corrupt */
2625 BUG();
2626 return -EBUSY;
2627}
2628
2629static void hotkey_inputdev_close(struct input_dev *dev)
2630{
2631 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002632 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002633 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002634 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002635}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002636
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002637/* sysfs hotkey enable ------------------------------------------------- */
2638static ssize_t hotkey_enable_show(struct device *dev,
2639 struct device_attribute *attr,
2640 char *buf)
2641{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002642 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002643
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002644 printk_deprecated_attribute("hotkey_enable",
2645 "Hotkey reporting is always enabled");
2646
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002647 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002648 if (res)
2649 return res;
2650
2651 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2652}
2653
2654static ssize_t hotkey_enable_store(struct device *dev,
2655 struct device_attribute *attr,
2656 const char *buf, size_t count)
2657{
2658 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002659
2660 printk_deprecated_attribute("hotkey_enable",
2661 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002662
2663 if (parse_strtoul(buf, 1, &t))
2664 return -EINVAL;
2665
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002666 if (t == 0)
2667 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002668
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002669 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002670}
2671
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002672static DEVICE_ATTR_RW(hotkey_enable);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002673
2674/* sysfs hotkey mask --------------------------------------------------- */
2675static ssize_t hotkey_mask_show(struct device *dev,
2676 struct device_attribute *attr,
2677 char *buf)
2678{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002679 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002680}
2681
2682static ssize_t hotkey_mask_store(struct device *dev,
2683 struct device_attribute *attr,
2684 const char *buf, size_t count)
2685{
2686 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002687 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002688
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002689 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002690 return -EINVAL;
2691
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002692 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002693 return -ERESTARTSYS;
2694
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002695 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002696
2697#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002698 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002699#endif
2700
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002701 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002702
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002703 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2704
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002705 return (res) ? res : count;
2706}
2707
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002708static DEVICE_ATTR_RW(hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002709
2710/* sysfs hotkey bios_enabled ------------------------------------------- */
2711static ssize_t hotkey_bios_enabled_show(struct device *dev,
2712 struct device_attribute *attr,
2713 char *buf)
2714{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002715 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002716}
2717
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002718static DEVICE_ATTR_RO(hotkey_bios_enabled);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002719
2720/* sysfs hotkey bios_mask ---------------------------------------------- */
2721static ssize_t hotkey_bios_mask_show(struct device *dev,
2722 struct device_attribute *attr,
2723 char *buf)
2724{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002725 printk_deprecated_attribute("hotkey_bios_mask",
2726 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002727 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002728}
2729
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002730static DEVICE_ATTR_RO(hotkey_bios_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002731
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002732/* sysfs hotkey all_mask ----------------------------------------------- */
2733static ssize_t hotkey_all_mask_show(struct device *dev,
2734 struct device_attribute *attr,
2735 char *buf)
2736{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002737 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2738 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002739}
2740
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002741static DEVICE_ATTR_RO(hotkey_all_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002742
2743/* sysfs hotkey recommended_mask --------------------------------------- */
2744static ssize_t hotkey_recommended_mask_show(struct device *dev,
2745 struct device_attribute *attr,
2746 char *buf)
2747{
2748 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002749 (hotkey_all_mask | hotkey_source_mask)
2750 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002751}
2752
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002753static DEVICE_ATTR_RO(hotkey_recommended_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002754
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002755#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2756
2757/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2758static ssize_t hotkey_source_mask_show(struct device *dev,
2759 struct device_attribute *attr,
2760 char *buf)
2761{
2762 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2763}
2764
2765static ssize_t hotkey_source_mask_store(struct device *dev,
2766 struct device_attribute *attr,
2767 const char *buf, size_t count)
2768{
2769 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002770 u32 r_ev;
2771 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002772
2773 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2774 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2775 return -EINVAL;
2776
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002777 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002778 return -ERESTARTSYS;
2779
2780 HOTKEY_CONFIG_CRITICAL_START
2781 hotkey_source_mask = t;
2782 HOTKEY_CONFIG_CRITICAL_END
2783
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002784 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2785 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002786 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002787
2788 /* check if events needed by the driver got disabled */
2789 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2790 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002791
2792 mutex_unlock(&hotkey_mutex);
2793
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002794 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07002795 pr_err("hotkey_source_mask: "
2796 "failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002797
2798 if (r_ev)
Joe Perches0978e012011-04-04 10:06:25 -07002799 pr_notice("hotkey_source_mask: "
2800 "some important events were disabled: 0x%04x\n",
2801 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002802
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002803 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2804
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002805 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002806}
2807
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002808static DEVICE_ATTR_RW(hotkey_source_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002809
2810/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2811static ssize_t hotkey_poll_freq_show(struct device *dev,
2812 struct device_attribute *attr,
2813 char *buf)
2814{
2815 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2816}
2817
2818static ssize_t hotkey_poll_freq_store(struct device *dev,
2819 struct device_attribute *attr,
2820 const char *buf, size_t count)
2821{
2822 unsigned long t;
2823
2824 if (parse_strtoul(buf, 25, &t))
2825 return -EINVAL;
2826
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002827 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002828 return -ERESTARTSYS;
2829
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002830 hotkey_poll_set_freq(t);
2831 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002832
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002833 mutex_unlock(&hotkey_mutex);
2834
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002835 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2836
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002837 return count;
2838}
2839
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002840static DEVICE_ATTR_RW(hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002841
2842#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2843
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002844/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002845static ssize_t hotkey_radio_sw_show(struct device *dev,
2846 struct device_attribute *attr,
2847 char *buf)
2848{
Johannes Berg19d337d2009-06-02 13:01:37 +02002849 int res;
2850 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002851 if (res < 0)
2852 return res;
2853
Johannes Berg19d337d2009-06-02 13:01:37 +02002854 /* Opportunistic update */
2855 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2856
2857 return snprintf(buf, PAGE_SIZE, "%d\n",
2858 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002859}
2860
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002861static DEVICE_ATTR_RO(hotkey_radio_sw);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002862
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002863static void hotkey_radio_sw_notify_change(void)
2864{
2865 if (tp_features.hotkey_wlsw)
2866 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2867 "hotkey_radio_sw");
2868}
2869
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002870/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2871static ssize_t hotkey_tablet_mode_show(struct device *dev,
2872 struct device_attribute *attr,
2873 char *buf)
2874{
2875 int res, s;
2876 res = hotkey_get_tablet_mode(&s);
2877 if (res < 0)
2878 return res;
2879
2880 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2881}
2882
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002883static DEVICE_ATTR_RO(hotkey_tablet_mode);
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002884
2885static void hotkey_tablet_mode_notify_change(void)
2886{
2887 if (tp_features.hotkey_tablet)
2888 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2889 "hotkey_tablet_mode");
2890}
2891
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002892/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002893static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2894 struct device_attribute *attr,
2895 char *buf)
2896{
2897 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2898}
2899
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002900static DEVICE_ATTR_RO(hotkey_wakeup_reason);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002901
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002902static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002903{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002904 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2905 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002906}
2907
2908/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002909static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2910 struct device_attribute *attr,
2911 char *buf)
2912{
2913 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2914}
2915
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01002916static DEVICE_ATTR_RO(hotkey_wakeup_hotunplug_complete);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002917
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002918static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002919{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002920 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2921 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002922}
2923
Bastien Nocerab790cee2015-03-02 14:45:27 +01002924/* sysfs adaptive kbd mode --------------------------------------------- */
2925
2926static int adaptive_keyboard_get_mode(void);
2927static int adaptive_keyboard_set_mode(int new_mode);
2928
2929enum ADAPTIVE_KEY_MODE {
2930 HOME_MODE,
2931 WEB_BROWSER_MODE,
2932 WEB_CONFERENCE_MODE,
2933 FUNCTION_MODE,
2934 LAYFLAT_MODE
2935};
2936
2937static ssize_t adaptive_kbd_mode_show(struct device *dev,
2938 struct device_attribute *attr,
2939 char *buf)
2940{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03002941 int current_mode;
Bastien Nocerab790cee2015-03-02 14:45:27 +01002942
2943 current_mode = adaptive_keyboard_get_mode();
2944 if (current_mode < 0)
2945 return current_mode;
2946
2947 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
2948}
2949
2950static ssize_t adaptive_kbd_mode_store(struct device *dev,
2951 struct device_attribute *attr,
2952 const char *buf, size_t count)
2953{
2954 unsigned long t;
2955 int res;
2956
2957 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2958 return -EINVAL;
2959
2960 res = adaptive_keyboard_set_mode(t);
2961 return (res < 0) ? res : count;
2962}
2963
2964static DEVICE_ATTR_RW(adaptive_kbd_mode);
2965
2966static struct attribute *adaptive_kbd_attributes[] = {
2967 &dev_attr_adaptive_kbd_mode.attr,
2968 NULL
2969};
2970
2971static const struct attribute_group adaptive_kbd_attr_group = {
2972 .attrs = adaptive_kbd_attributes,
2973};
2974
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002975/* --------------------------------------------------------------------- */
2976
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002977static struct attribute *hotkey_attributes[] __initdata = {
2978 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002979 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002980 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002981 &dev_attr_hotkey_wakeup_reason.attr,
2982 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002983 &dev_attr_hotkey_mask.attr,
2984 &dev_attr_hotkey_all_mask.attr,
2985 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002986#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002987 &dev_attr_hotkey_source_mask.attr,
2988 &dev_attr_hotkey_poll_freq.attr,
2989#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002990};
2991
Johannes Berg19d337d2009-06-02 13:01:37 +02002992/*
2993 * Sync both the hw and sw blocking state of all switches
2994 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002995static void tpacpi_send_radiosw_update(void)
2996{
2997 int wlsw;
2998
Johannes Berg19d337d2009-06-02 13:01:37 +02002999 /*
3000 * We must sync all rfkill controllers *before* issuing any
3001 * rfkill input events, or we will race the rfkill core input
3002 * handler.
3003 *
Lucas De Marchic8440332011-03-17 17:18:22 -03003004 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02003005 * for the above.
3006 *
3007 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3008 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003009
Johannes Berg19d337d2009-06-02 13:01:37 +02003010 wlsw = hotkey_get_wlsw();
3011
3012 /* Sync hw blocking state first if it is hw-blocked */
3013 if (wlsw == TPACPI_RFK_RADIO_OFF)
3014 tpacpi_rfk_update_hwblock_state(true);
3015
3016 /* Sync sw blocking state */
3017 tpacpi_rfk_update_swstate_all();
3018
3019 /* Sync hw blocking state last if it is hw-unblocked */
3020 if (wlsw == TPACPI_RFK_RADIO_ON)
3021 tpacpi_rfk_update_hwblock_state(false);
3022
3023 /* Issue rfkill input event for WLSW switch */
3024 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003025 mutex_lock(&tpacpi_inputdev_send_mutex);
3026
3027 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003028 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003029 input_sync(tpacpi_inputdev);
3030
3031 mutex_unlock(&tpacpi_inputdev_send_mutex);
3032 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003033
3034 /*
3035 * this can be unconditional, as we will poll state again
3036 * if userspace uses the notify to read data
3037 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003038 hotkey_radio_sw_notify_change();
3039}
3040
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003041static void hotkey_exit(void)
3042{
3043#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003044 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003045 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003046 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003047#endif
3048
3049 if (hotkey_dev_attributes)
3050 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3051
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003052 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003053 "restoring original HKEY status and mask\n");
3054 /* yes, there is a bitwise or below, we want the
3055 * functions to be called even if one of them fail */
3056 if (((tp_features.hotkey_mask &&
3057 hotkey_mask_set(hotkey_orig_mask)) |
3058 hotkey_status_set(false)) != 0)
Joe Perches0978e012011-04-04 10:06:25 -07003059 pr_err("failed to restore hot key mask "
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003060 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003061}
3062
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003063static void __init hotkey_unmap(const unsigned int scancode)
3064{
3065 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3066 clear_bit(hotkey_keycode_map[scancode],
3067 tpacpi_inputdev->keybit);
3068 hotkey_keycode_map[scancode] = KEY_RESERVED;
3069 }
3070}
3071
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003072/*
3073 * HKEY quirks:
3074 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3075 */
3076
3077#define TPACPI_HK_Q_INIMASK 0x0001
3078
3079static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3080 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3081 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3082 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3083 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3084 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3085 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3086 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3087 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3088 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3089 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3090 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3091 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3092 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3093 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3094 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3095 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3096 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3097 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3098 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3099};
3100
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003101typedef u16 tpacpi_keymap_entry_t;
3102typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003103
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003104static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003105{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003106 /* Requirements for changing the default keymaps:
3107 *
3108 * 1. Many of the keys are mapped to KEY_RESERVED for very
3109 * good reasons. Do not change them unless you have deep
3110 * knowledge on the IBM and Lenovo ThinkPad firmware for
3111 * the various ThinkPad models. The driver behaves
3112 * differently for KEY_RESERVED: such keys have their
3113 * hot key mask *unset* in mask_recommended, and also
3114 * in the initial hot key mask programmed into the
3115 * firmware at driver load time, which means the firm-
3116 * ware may react very differently if you change them to
3117 * something else;
3118 *
3119 * 2. You must be subscribed to the linux-thinkpad and
3120 * ibm-acpi-devel mailing lists, and you should read the
3121 * list archives since 2007 if you want to change the
3122 * keymaps. This requirement exists so that you will
3123 * know the past history of problems with the thinkpad-
3124 * acpi driver keymaps, and also that you will be
3125 * listening to any bug reports;
3126 *
3127 * 3. Do not send thinkpad-acpi specific patches directly to
3128 * for merging, *ever*. Send them to the linux-acpi
3129 * mailinglist for comments. Merging is to be done only
3130 * through acpi-test and the ACPI maintainer.
3131 *
3132 * If the above is too much to ask, don't change the keymap.
3133 * Ask the thinkpad-acpi maintainer to do it, instead.
3134 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003135
3136 enum keymap_index {
3137 TPACPI_KEYMAP_IBM_GENERIC = 0,
3138 TPACPI_KEYMAP_LENOVO_GENERIC,
3139 };
3140
3141 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3142 /* Generic keymap for IBM ThinkPads */
3143 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003144 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003145 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003146 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3147 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003148
3149 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003150 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3151 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3152 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003153
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003154 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003155 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003156 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003157
3158 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003159 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003160
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003161 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3162 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003163
3164 /* Volume: firmware always react to it and reprograms
3165 * the built-in *extra* mixer. Never map it to control
3166 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003167 KEY_RESERVED, /* 0x14: VOLUME UP */
3168 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3169 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003170
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003171 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003172
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003173 /* (assignments unknown, please report if found) */
3174 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3175 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003176
3177 /* No assignments, only used for Adaptive keyboards. */
3178 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3179 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3180 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3181 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3182 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003183 },
3184
3185 /* Generic keymap for Lenovo ThinkPads */
3186 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003187 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3188 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003189 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003190 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003191
3192 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003193 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3194 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3195 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003196
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003197 /* These should be enabled --only-- when ACPI video
3198 * is disabled (i.e. in "vendor" mode), and are handled
3199 * in a special way by the init code */
3200 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3201 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003202
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003203 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003204
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003205 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3206 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003207
3208 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3209 * react to it and reprograms the built-in *extra* mixer.
3210 * Never map it to control another mixer by default.
3211 *
3212 * T60?, T61, R60?, R61: firmware and EC tries to send
3213 * these over the regular keyboard, so these are no-ops,
3214 * but there are still weird bugs re. MUTE, so do not
3215 * change unless you get test reports from all Lenovo
3216 * models. May cause the BIOS to interfere with the
3217 * HDA mixer.
3218 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003219 KEY_RESERVED, /* 0x14: VOLUME UP */
3220 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3221 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003222
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003223 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003224
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003225 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003226 KEY_UNKNOWN, KEY_UNKNOWN,
3227
3228 /*
3229 * The mic mute button only sends 0x1a. It does not
3230 * automatically mute the mic or change the mute light.
3231 */
3232 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3233
3234 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003235 KEY_UNKNOWN,
Hans de Goede8b9dd4f2014-04-09 11:46:46 +02003236
3237 /* Extra keys in use since the X240 / T440 / T540 */
Hans de Goede4beb81d2014-06-23 13:38:23 +02003238 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
Bastien Nocera6a68d852015-03-02 14:45:31 +01003239
3240 /*
3241 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3242 * The first item in this list is the Mute button which is
3243 * emitted with 0x103 through
3244 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3245 * symbol is held.
3246 * We'll need to offset those by 0x20.
3247 */
3248 KEY_RESERVED, /* Mute held, 0x103 */
3249 KEY_BRIGHTNESS_MIN, /* Backlight off */
3250 KEY_RESERVED, /* Clipping tool */
3251 KEY_RESERVED, /* Cloud */
3252 KEY_RESERVED,
3253 KEY_VOICECOMMAND, /* Voice */
3254 KEY_RESERVED,
3255 KEY_RESERVED, /* Gestures */
3256 KEY_RESERVED,
3257 KEY_RESERVED,
3258 KEY_RESERVED,
3259 KEY_CONFIG, /* Settings */
3260 KEY_RESERVED, /* New tab */
3261 KEY_REFRESH, /* Reload */
3262 KEY_BACK, /* Back */
3263 KEY_RESERVED, /* Microphone down */
3264 KEY_RESERVED, /* Microphone up */
3265 KEY_RESERVED, /* Microphone cancellation */
3266 KEY_RESERVED, /* Camera mode */
3267 KEY_RESERVED, /* Rotate display, 0x116 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003268 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003269 };
3270
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003271 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3272 /* Generic maps (fallback) */
3273 {
3274 .vendor = PCI_VENDOR_ID_IBM,
3275 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3276 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3277 },
3278 {
3279 .vendor = PCI_VENDOR_ID_LENOVO,
3280 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3281 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3282 },
3283 };
3284
3285#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003286#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003287
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003288 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003289 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003290 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003291 bool radiosw_state = false;
3292 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003293
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003294 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003295 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003296
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003297 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3298 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003299
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003300 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003301 BUG_ON(tpacpi_inputdev->open != NULL ||
3302 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003303
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003304 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003305 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003306
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003307#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003308 mutex_init(&hotkey_thread_data_mutex);
3309#endif
3310
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003311 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003312 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003313
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003314 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3315 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003316 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003317
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003318 if (!tp_features.hotkey)
3319 return 1;
3320
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01003321 /*
3322 * Check if we have an adaptive keyboard, like on the
3323 * Lenovo Carbon X1 2014 (2nd Gen).
3324 */
3325 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
Bastien Nocerab790cee2015-03-02 14:45:27 +01003326 if ((hkeyv >> 8) == 2) {
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01003327 tp_features.has_adaptive_kbd = true;
Bastien Nocerab790cee2015-03-02 14:45:27 +01003328 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3329 &adaptive_kbd_attr_group);
3330 if (res)
3331 goto err_exit;
3332 }
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01003333 }
3334
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003335 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3336 ARRAY_SIZE(tpacpi_hotkey_qtable));
3337
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003338 tpacpi_disable_brightness_delay();
3339
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003340 /* MUST have enough space for all attributes to be added to
3341 * hotkey_dev_attributes */
3342 hotkey_dev_attributes = create_attr_set(
3343 ARRAY_SIZE(hotkey_attributes) + 2,
3344 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003345 if (!hotkey_dev_attributes)
3346 return -ENOMEM;
3347 res = add_many_to_attr_set(hotkey_dev_attributes,
3348 hotkey_attributes,
3349 ARRAY_SIZE(hotkey_attributes));
3350 if (res)
3351 goto err_exit;
3352
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003353 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003354 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3355 for HKEY interface version 0x100 */
3356 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3357 if ((hkeyv >> 8) != 1) {
Joe Perches0978e012011-04-04 10:06:25 -07003358 pr_err("unknown version of the HKEY interface: 0x%x\n",
3359 hkeyv);
3360 pr_err("please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003361 } else {
3362 /*
3363 * MHKV 0x100 in A31, R40, R40e,
3364 * T4x, X31, and later
3365 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003366 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3367 "firmware HKEY interface version: 0x%x\n",
3368 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003369
3370 /* Paranoia check AND init hotkey_all_mask */
3371 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3372 "MHKA", "qd")) {
Joe Perches0978e012011-04-04 10:06:25 -07003373 pr_err("missing MHKA handler, "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003374 "please report this to %s\n",
3375 TPACPI_MAIL);
3376 /* Fallback: pre-init for FN+F3,F4,F12 */
3377 hotkey_all_mask = 0x080cU;
3378 } else {
3379 tp_features.hotkey_mask = 1;
3380 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003381 }
3382 }
3383
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003384 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3385 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003386 str_supported(tp_features.hotkey_mask));
3387
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003388 /* Init hotkey_all_mask if not initialized yet */
3389 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3390 (quirks & TPACPI_HK_Q_INIMASK))
3391 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003392
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003393 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003394 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003395 /* hotkey_source_mask *must* be zero for
3396 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003397 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003398 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003399 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003400
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003401 hotkey_orig_mask = hotkey_acpi_mask;
3402 } else {
3403 hotkey_orig_mask = hotkey_all_mask;
3404 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003405 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003406
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003407#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3408 if (dbg_wlswemul) {
3409 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003410 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003411 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003412 } else
3413#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003414 /* Not all thinkpads have a hardware radio switch */
3415 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3416 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003417 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003418 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003419 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003420 }
3421 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003422 res = add_to_attr_set(hotkey_dev_attributes,
3423 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003424
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003425 /* For X41t, X60t, X61t Tablets... */
3426 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3427 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003428 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Joe Perches0978e012011-04-04 10:06:25 -07003429 pr_info("possible tablet mode switch found; "
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003430 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003431 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003432 res = add_to_attr_set(hotkey_dev_attributes,
3433 &dev_attr_hotkey_tablet_mode.attr);
3434 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003435
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003436 if (!res)
3437 res = register_attr_set_with_sysfs(
3438 hotkey_dev_attributes,
3439 &tpacpi_pdev->dev.kobj);
3440 if (res)
3441 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003442
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003443 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003444 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3445 GFP_KERNEL);
3446 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003447 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003448 res = -ENOMEM;
3449 goto err_exit;
3450 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003451
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003452 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3453 ARRAY_SIZE(tpacpi_keymap_qtable));
3454 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3455 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3456 "using keymap number %lu\n", keymap_id);
3457
3458 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3459 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003460
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003461 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003462 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3463 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3464 tpacpi_inputdev->keycode = hotkey_keycode_map;
3465 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3466 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003467 input_set_capability(tpacpi_inputdev, EV_KEY,
3468 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003469 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003470 if (i < sizeof(hotkey_reserved_mask)*8)
3471 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003472 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003473 }
3474
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003475 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003476 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003477 input_report_switch(tpacpi_inputdev,
3478 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003479 }
3480 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003481 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003482 input_report_switch(tpacpi_inputdev,
3483 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003484 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003485
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003486 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003487 * userspace. tpacpi_detect_brightness_capabilities() must have
3488 * been called before this point */
Alex Hungff413192012-04-24 16:40:52 +08003489 if (acpi_video_backlight_support()) {
Joe Perches0978e012011-04-04 10:06:25 -07003490 pr_info("This ThinkPad has standard ACPI backlight "
3491 "brightness control, supported by the ACPI "
3492 "video driver\n");
3493 pr_notice("Disabling thinkpad-acpi brightness events "
3494 "by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003495
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003496 /* Disable brightness up/down on Lenovo thinkpads when
3497 * ACPI is handling them, otherwise it is plain impossible
3498 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003499 hotkey_reserved_mask |=
3500 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3501 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003502 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3503 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003504 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003505
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003506#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003507 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3508 & ~hotkey_all_mask
3509 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003510
3511 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3512 "hotkey source mask 0x%08x, polling freq %u\n",
3513 hotkey_source_mask, hotkey_poll_freq);
3514#endif
3515
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003516 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3517 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003518 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003519 if (res) {
3520 hotkey_exit();
3521 return res;
3522 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003523 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3524 | hotkey_driver_mask)
3525 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003526 if (res < 0 && res != -ENXIO) {
3527 hotkey_exit();
3528 return res;
3529 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003530 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3531 & ~hotkey_reserved_mask;
3532 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3533 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3534 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003535
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003536 tpacpi_inputdev->open = &hotkey_inputdev_open;
3537 tpacpi_inputdev->close = &hotkey_inputdev_close;
3538
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003539 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003540
3541 return 0;
3542
3543err_exit:
3544 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
Bastien Nocerab790cee2015-03-02 14:45:27 +01003545 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3546 &adaptive_kbd_attr_group);
3547
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003548 hotkey_dev_attributes = NULL;
3549
Jan van den Berg72a979f2014-09-17 00:01:08 +02003550 return (res < 0) ? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003551}
3552
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003553/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3554 * mode, Web conference mode, Function mode and Lay-flat mode.
3555 * We support Home mode and Function mode currently.
3556 *
3557 * Will consider support rest of modes in future.
3558 *
3559 */
Lad, Prabhakarb201a472015-02-05 14:45:38 +00003560static const int adaptive_keyboard_modes[] = {
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003561 HOME_MODE,
3562/* WEB_BROWSER_MODE = 2,
3563 WEB_CONFERENCE_MODE = 3, */
3564 FUNCTION_MODE
3565};
3566
3567#define DFR_CHANGE_ROW 0x101
3568#define DFR_SHOW_QUICKVIEW_ROW 0x102
Bastien Nocera6a68d852015-03-02 14:45:31 +01003569#define FIRST_ADAPTIVE_KEY 0x103
3570#define ADAPTIVE_KEY_OFFSET 0x020
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003571
3572/* press Fn key a while second, it will switch to Function Mode. Then
3573 * release Fn key, previous mode be restored.
3574 */
3575static bool adaptive_keyboard_mode_is_saved;
3576static int adaptive_keyboard_prev_mode;
3577
Bastien Noceraf74587f2015-03-02 14:45:22 +01003578static int adaptive_keyboard_get_mode(void)
3579{
3580 int mode = 0;
3581
3582 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3583 pr_err("Cannot read adaptive keyboard mode\n");
3584 return -EIO;
3585 }
3586
3587 return mode;
3588}
3589
3590static int adaptive_keyboard_set_mode(int new_mode)
3591{
3592 if (new_mode < 0 ||
3593 new_mode > LAYFLAT_MODE)
3594 return -EINVAL;
3595
3596 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3597 pr_err("Cannot set adaptive keyboard mode\n");
3598 return -EIO;
3599 }
3600
3601 return 0;
3602}
3603
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003604static int adaptive_keyboard_get_next_mode(int mode)
3605{
3606 size_t i;
3607 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3608
3609 for (i = 0; i <= max_mode; i++) {
3610 if (adaptive_keyboard_modes[i] == mode)
3611 break;
3612 }
3613
3614 if (i >= max_mode)
3615 i = 0;
3616 else
3617 i++;
3618
3619 return adaptive_keyboard_modes[i];
3620}
3621
3622static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3623{
Dan Carpenterabf9dc02015-03-11 12:34:50 +03003624 int current_mode = 0;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003625 int new_mode = 0;
Bastien Nocera6a68d852015-03-02 14:45:31 +01003626 int keycode;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003627
3628 switch (scancode) {
3629 case DFR_CHANGE_ROW:
3630 if (adaptive_keyboard_mode_is_saved) {
3631 new_mode = adaptive_keyboard_prev_mode;
3632 adaptive_keyboard_mode_is_saved = false;
3633 } else {
Bastien Noceraf74587f2015-03-02 14:45:22 +01003634 current_mode = adaptive_keyboard_get_mode();
3635 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003636 return false;
Bastien Noceraf74587f2015-03-02 14:45:22 +01003637 new_mode = adaptive_keyboard_get_next_mode(
3638 current_mode);
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003639 }
3640
Bastien Noceraf74587f2015-03-02 14:45:22 +01003641 if (adaptive_keyboard_set_mode(new_mode) < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003642 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003643
3644 return true;
3645
3646 case DFR_SHOW_QUICKVIEW_ROW:
Bastien Noceraf74587f2015-03-02 14:45:22 +01003647 current_mode = adaptive_keyboard_get_mode();
3648 if (current_mode < 0)
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003649 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003650
Bastien Noceraf74587f2015-03-02 14:45:22 +01003651 adaptive_keyboard_prev_mode = current_mode;
3652 adaptive_keyboard_mode_is_saved = true;
3653
3654 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3655 return false;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003656 return true;
3657
3658 default:
Bastien Nocera6a68d852015-03-02 14:45:31 +01003659 if (scancode < FIRST_ADAPTIVE_KEY || scancode > FIRST_ADAPTIVE_KEY +
3660 TPACPI_HOTKEY_MAP_LEN - ADAPTIVE_KEY_OFFSET) {
3661 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3662 scancode);
3663 return false;
3664 }
3665 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY + ADAPTIVE_KEY_OFFSET];
3666 if (keycode != KEY_RESERVED) {
3667 mutex_lock(&tpacpi_inputdev_send_mutex);
3668
3669 input_report_key(tpacpi_inputdev, keycode, 1);
3670 input_sync(tpacpi_inputdev);
3671
3672 input_report_key(tpacpi_inputdev, keycode, 0);
3673 input_sync(tpacpi_inputdev);
3674
3675 mutex_unlock(&tpacpi_inputdev_send_mutex);
3676 }
3677 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003678 }
3679}
3680
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003681static bool hotkey_notify_hotkey(const u32 hkey,
3682 bool *send_acpi_ev,
3683 bool *ignore_acpi_ev)
3684{
3685 /* 0x1000-0x1FFF: key presses */
3686 unsigned int scancode = hkey & 0xfff;
3687 *send_acpi_ev = true;
3688 *ignore_acpi_ev = false;
3689
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03003690 /* HKEY event 0x1001 is scancode 0x00 */
3691 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003692 scancode--;
3693 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003694 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003695 *send_acpi_ev = false;
3696 } else {
3697 *ignore_acpi_ev = true;
3698 }
3699 return true;
Shuduo Sang3a9d20b2014-03-06 18:20:46 +08003700 } else {
3701 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003702 }
3703 return false;
3704}
3705
3706static bool hotkey_notify_wakeup(const u32 hkey,
3707 bool *send_acpi_ev,
3708 bool *ignore_acpi_ev)
3709{
3710 /* 0x2000-0x2FFF: Wakeup reason */
3711 *send_acpi_ev = true;
3712 *ignore_acpi_ev = false;
3713
3714 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003715 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3716 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003717 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3718 *ignore_acpi_ev = true;
3719 break;
3720
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003721 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3722 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003723 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3724 *ignore_acpi_ev = true;
3725 break;
3726
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003727 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3728 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003729 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003730 /* how to auto-heal: */
3731 /* 2313: woke up from S3, go to S4/S5 */
3732 /* 2413: woke up from S4, go to S5 */
3733 break;
3734
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003735 default:
3736 return false;
3737 }
3738
3739 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003740 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003741 hotkey_wakeup_reason_notify_change();
3742 }
3743 return true;
3744}
3745
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003746static bool hotkey_notify_dockevent(const u32 hkey,
3747 bool *send_acpi_ev,
3748 bool *ignore_acpi_ev)
3749{
3750 /* 0x4000-0x4FFF: dock-related events */
3751 *send_acpi_ev = true;
3752 *ignore_acpi_ev = false;
3753
3754 switch (hkey) {
3755 case TP_HKEY_EV_UNDOCK_ACK:
3756 /* ACPI undock operation completed after wakeup */
3757 hotkey_autosleep_ack = 1;
3758 pr_info("undocked\n");
3759 hotkey_wakeup_hotunplug_complete_notify_change();
3760 return true;
3761
3762 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3763 pr_info("docked into hotplug port replicator\n");
3764 return true;
3765 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3766 pr_info("undocked from hotplug port replicator\n");
3767 return true;
3768
3769 default:
3770 return false;
3771 }
3772}
3773
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003774static bool hotkey_notify_usrevent(const u32 hkey,
3775 bool *send_acpi_ev,
3776 bool *ignore_acpi_ev)
3777{
3778 /* 0x5000-0x5FFF: human interface helpers */
3779 *send_acpi_ev = true;
3780 *ignore_acpi_ev = false;
3781
3782 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003783 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3784 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003785 return true;
3786
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003787 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3788 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003789 tpacpi_input_send_tabletsw();
3790 hotkey_tablet_mode_notify_change();
3791 *send_acpi_ev = false;
3792 return true;
3793
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003794 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3795 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3796 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003797 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003798 *ignore_acpi_ev = true;
3799 return true;
3800
3801 default:
3802 return false;
3803 }
3804}
3805
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003806static void thermal_dump_all_sensors(void);
3807
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003808static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003809 bool *send_acpi_ev,
3810 bool *ignore_acpi_ev)
3811{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003812 bool known = true;
3813
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003814 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003815 *send_acpi_ev = true;
3816 *ignore_acpi_ev = false;
3817
3818 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003819 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003820 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003821 /* recommended action: do nothing, we don't have
3822 * Lenovo ATM information */
3823 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003824 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003825 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003826 /* recommended action: warn user through gui */
3827 break;
3828 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003829 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003830 /* recommended action: immediate sleep/hibernate */
3831 break;
3832 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003833 pr_crit("THERMAL ALARM: "
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003834 "a sensor reports something is too hot!\n");
3835 /* recommended action: warn user through gui, that */
3836 /* some internal component is too hot */
3837 break;
3838 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003839 pr_alert("THERMAL EMERGENCY: "
3840 "a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003841 /* recommended action: immediate sleep/hibernate */
3842 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01003843 case TP_HKEY_EV_AC_CHANGED:
3844 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3845 * AC status changed; can be triggered by plugging or
3846 * unplugging AC adapter, docking or undocking. */
3847
3848 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003849
3850 case TP_HKEY_EV_KEY_NUMLOCK:
3851 case TP_HKEY_EV_KEY_FN:
Xavier Naveira67ab6242015-02-10 08:45:18 +01003852 case TP_HKEY_EV_KEY_FN_ESC:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003853 /* key press events, we just ignore them as long as the EC
3854 * is still reporting them in the normal keyboard stream */
3855 *send_acpi_ev = false;
3856 *ignore_acpi_ev = true;
3857 return true;
3858
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003859 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003860 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003861 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003862 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003863
3864 thermal_dump_all_sensors();
3865
3866 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003867}
3868
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003869static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3870{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003871 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003872 bool send_acpi_ev;
3873 bool ignore_acpi_ev;
3874 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003875
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003876 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07003877 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003878 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003879 acpi_bus_generate_netlink_event(
3880 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003881 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003882 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003883 return;
3884 }
3885
3886 while (1) {
3887 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07003888 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003889 return;
3890 }
3891
3892 if (hkey == 0) {
3893 /* queue empty */
3894 return;
3895 }
3896
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003897 send_acpi_ev = true;
3898 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003899
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003900 switch (hkey >> 12) {
3901 case 1:
3902 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003903 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3904 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003905 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003906 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003907 /* 0x2000-0x2FFF: Wakeup reason */
3908 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3909 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003910 break;
3911 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003912 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003913 switch (hkey) {
3914 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003915 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07003916 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003917 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003918 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003919 break;
3920 case TP_HKEY_EV_OPTDRV_EJ:
3921 /* FIXME: kick libata if SATA link offline */
3922 known_ev = true;
3923 break;
3924 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003925 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003926 }
3927 break;
3928 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003929 /* 0x4000-0x4FFF: dock-related events */
3930 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
3931 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003932 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003933 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003934 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003935 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3936 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003937 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003938 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003939 /* 0x6000-0x6FFF: thermal alarms/notices and
3940 * keyboard events */
3941 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003942 &ignore_acpi_ev);
3943 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003944 case 7:
3945 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003946 if (tp_features.hotkey_wlsw &&
3947 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003948 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003949 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003950 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003951 break;
3952 }
3953 /* fallthrough to default */
3954 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003955 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003956 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003957 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07003958 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3959 pr_notice("please report the conditions when this "
3960 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003961 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003962
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003963 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003964 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003965 acpi_bus_generate_netlink_event(
3966 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003967 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003968 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003969 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003970 }
3971}
3972
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02003973static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003974{
3975 /* Do these on suspend, we get the events on early resume! */
3976 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3977 hotkey_autosleep_ack = 0;
Shuduo Sang330947b2014-03-27 18:06:25 +08003978
3979 /* save previous mode of adaptive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01003980 if (tp_features.has_adaptive_kbd) {
3981 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
3982 "GTRW", "dd", 0)) {
3983 pr_err("Cannot read adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08003984 }
3985 }
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003986}
3987
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003988static void hotkey_resume(void)
3989{
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003990 tpacpi_disable_brightness_delay();
3991
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003992 if (hotkey_status_set(true) < 0 ||
3993 hotkey_mask_set(hotkey_acpi_mask) < 0)
Joe Perches0978e012011-04-04 10:06:25 -07003994 pr_err("error while attempting to reset the event "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003995 "firmware interface\n");
3996
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003997 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003998 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003999 hotkey_wakeup_reason_notify_change();
4000 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03004001 hotkey_poll_setup_safe(false);
Shuduo Sang330947b2014-03-27 18:06:25 +08004002
4003 /* restore previous mode of adapive keyboard of X1 Carbon */
Bastien Noceraf23a5bc2015-03-02 14:45:16 +01004004 if (tp_features.has_adaptive_kbd) {
4005 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4006 adaptive_keyboard_prev_mode)) {
4007 pr_err("Cannot set adaptive keyboard mode.\n");
Shuduo Sang330947b2014-03-27 18:06:25 +08004008 }
4009 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004010}
4011
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03004012/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004013static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004015 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004017 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004018 seq_printf(m, "status:\t\tnot supported\n");
4019 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004020 }
4021
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004022 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004023 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02004024 res = hotkey_status_get(&status);
4025 if (!res)
4026 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004027 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03004028 if (res)
4029 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004031 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004032 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004033 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4034 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004036 seq_printf(m, "mask:\t\tnot supported\n");
4037 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 }
4039
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041}
4042
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004043static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004044{
4045 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004046 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Joe Perches0978e012011-04-04 10:06:25 -07004047 pr_fmt("hotkey enable/disable functionality has been "
4048 "removed from the driver. "
4049 "Hotkeys are always enabled.\n")))
4050 pr_err("Please remove the hotkey=enable module "
4051 "parameter, it is deprecated. "
4052 "Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004053}
4054
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004055static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004057 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03004058 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004061 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 return -ENODEV;
4063
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02004064 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004065 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004066
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004067 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004068
4069 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 while ((cmd = next_cmd(&buf))) {
4071 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004072 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00004074 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00004075 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa462009-09-12 15:22:16 -03004077 mask = (hotkey_all_mask | hotkey_source_mask)
4078 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4080 /* mask set */
4081 } else if (sscanf(cmd, "%x", &mask) == 1) {
4082 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004083 } else {
4084 res = -EINVAL;
4085 goto errexit;
4086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004088
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004089 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00004090 tpacpi_disclose_usertask("procfs hotkey",
4091 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03004092 res = hotkey_user_mask_set(mask);
4093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004095errexit:
4096 mutex_unlock(&hotkey_mutex);
4097 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004098}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004100static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04004101 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4102 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004103 {"", 0},
4104};
4105
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004106static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004107 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004108 .notify = hotkey_notify,
4109 .handle = &hkey_handle,
4110 .type = ACPI_DEVICE_NOTIFY,
4111};
4112
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004113static struct ibm_struct hotkey_driver_data = {
4114 .name = "hotkey",
4115 .read = hotkey_read,
4116 .write = hotkey_write,
4117 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03004118 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02004119 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004120 .acpi = &ibm_hotkey_acpidriver,
4121};
4122
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004123/*************************************************************************
4124 * Bluetooth subdriver
4125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004127enum {
4128 /* ACPI GBDC/SBDC bits */
4129 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4130 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004131 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004132 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004133};
4134
4135enum {
4136 /* ACPI \BLTH commands */
4137 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4138 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4139 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4140 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4141 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004142};
4143
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004144#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4145
Johannes Berg19d337d2009-06-02 13:01:37 +02004146static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004147{
4148 int status;
4149
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004150#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4151 if (dbg_bluetoothemul)
4152 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004153 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004154#endif
4155
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004156 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4157 return -EIO;
4158
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004159 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004160 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004161}
4162
Johannes Berg19d337d2009-06-02 13:01:37 +02004163static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004164{
4165 int status;
4166
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004167 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004168 "will attempt to %s bluetooth\n",
4169 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004170
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004171#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4172 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004173 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004174 return 0;
4175 }
4176#endif
4177
Johannes Berg19d337d2009-06-02 13:01:37 +02004178 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004179 status = TP_ACPI_BLUETOOTH_RADIOSSW
4180 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4181 else
4182 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004183
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004184 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4185 return -EIO;
4186
4187 return 0;
4188}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004189
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004190/* sysfs bluetooth enable ---------------------------------------------- */
4191static ssize_t bluetooth_enable_show(struct device *dev,
4192 struct device_attribute *attr,
4193 char *buf)
4194{
Johannes Berg19d337d2009-06-02 13:01:37 +02004195 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4196 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004197}
4198
4199static ssize_t bluetooth_enable_store(struct device *dev,
4200 struct device_attribute *attr,
4201 const char *buf, size_t count)
4202{
Johannes Berg19d337d2009-06-02 13:01:37 +02004203 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4204 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004205}
4206
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004207static DEVICE_ATTR_RW(bluetooth_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004208
4209/* --------------------------------------------------------------------- */
4210
4211static struct attribute *bluetooth_attributes[] = {
4212 &dev_attr_bluetooth_enable.attr,
4213 NULL
4214};
4215
4216static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004217 .attrs = bluetooth_attributes,
4218};
4219
Johannes Berg19d337d2009-06-02 13:01:37 +02004220static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4221 .get_status = bluetooth_get_status,
4222 .set_status = bluetooth_set_status,
4223};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004224
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004225static void bluetooth_shutdown(void)
4226{
4227 /* Order firmware to save current state to NVRAM */
4228 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4229 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004230 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004231 else
4232 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004233 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004234}
4235
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004236static void bluetooth_exit(void)
4237{
4238 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4239 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004240
4241 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4242
4243 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004244}
4245
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004246static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004248 int res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004249 int status = 0;
4250
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004251 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4252 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004253
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004254 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004255
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004256 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4257 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004258 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004259 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004261 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4262 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004263 str_supported(tp_features.bluetooth),
4264 status);
4265
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004266#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4267 if (dbg_bluetoothemul) {
4268 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004269 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004270 } else
4271#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004272 if (tp_features.bluetooth &&
4273 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4274 /* no bluetooth hardware present in system */
4275 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004276 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004277 "bluetooth hardware not installed\n");
4278 }
4279
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004280 if (!tp_features.bluetooth)
4281 return 1;
4282
Johannes Berg19d337d2009-06-02 13:01:37 +02004283 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4284 &bluetooth_tprfk_ops,
4285 RFKILL_TYPE_BLUETOOTH,
4286 TPACPI_RFK_BLUETOOTH_SW_NAME,
4287 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004288 if (res)
4289 return res;
4290
Johannes Berg19d337d2009-06-02 13:01:37 +02004291 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4292 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004293 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004294 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004295 return res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004296 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004297
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299}
4300
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004301/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004302static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004304 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305}
4306
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004307static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308{
Johannes Berg19d337d2009-06-02 13:01:37 +02004309 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310}
4311
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004312static struct ibm_struct bluetooth_driver_data = {
4313 .name = "bluetooth",
4314 .read = bluetooth_read,
4315 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004316 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004317 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004318};
4319
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004320/*************************************************************************
4321 * Wan subdriver
4322 */
4323
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004324enum {
4325 /* ACPI GWAN/SWAN bits */
4326 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4327 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004328 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004329 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004330};
4331
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004332#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4333
Johannes Berg19d337d2009-06-02 13:01:37 +02004334static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004335{
4336 int status;
4337
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004338#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4339 if (dbg_wwanemul)
4340 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004341 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004342#endif
4343
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004344 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4345 return -EIO;
4346
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004347 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004348 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004349}
4350
Johannes Berg19d337d2009-06-02 13:01:37 +02004351static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004352{
4353 int status;
4354
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004355 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004356 "will attempt to %s wwan\n",
4357 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004358
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004359#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4360 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004361 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004362 return 0;
4363 }
4364#endif
4365
Johannes Berg19d337d2009-06-02 13:01:37 +02004366 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004367 status = TP_ACPI_WANCARD_RADIOSSW
4368 | TP_ACPI_WANCARD_RESUMECTRL;
4369 else
4370 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004371
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004372 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4373 return -EIO;
4374
4375 return 0;
4376}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004377
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004378/* sysfs wan enable ---------------------------------------------------- */
4379static ssize_t wan_enable_show(struct device *dev,
4380 struct device_attribute *attr,
4381 char *buf)
4382{
Johannes Berg19d337d2009-06-02 13:01:37 +02004383 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4384 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004385}
4386
4387static ssize_t wan_enable_store(struct device *dev,
4388 struct device_attribute *attr,
4389 const char *buf, size_t count)
4390{
Johannes Berg19d337d2009-06-02 13:01:37 +02004391 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4392 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004393}
4394
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01004395static DEVICE_ATTR_RW(wan_enable);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004396
4397/* --------------------------------------------------------------------- */
4398
4399static struct attribute *wan_attributes[] = {
4400 &dev_attr_wan_enable.attr,
4401 NULL
4402};
4403
4404static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004405 .attrs = wan_attributes,
4406};
4407
Johannes Berg19d337d2009-06-02 13:01:37 +02004408static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4409 .get_status = wan_get_status,
4410 .set_status = wan_set_status,
4411};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004412
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004413static void wan_shutdown(void)
4414{
4415 /* Order firmware to save current state to NVRAM */
4416 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4417 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004418 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004419 else
4420 vdbg_printk(TPACPI_DBG_RFKILL,
4421 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004422}
4423
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004424static void wan_exit(void)
4425{
4426 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4427 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004428
4429 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4430
4431 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004432}
4433
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004434static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004435{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004436 int res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004437 int status = 0;
4438
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004439 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4440 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004441
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004442 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004443
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004444 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004445 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004446
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004447 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4448 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004449 str_supported(tp_features.wan),
4450 status);
4451
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004452#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4453 if (dbg_wwanemul) {
4454 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004455 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004456 } else
4457#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004458 if (tp_features.wan &&
4459 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4460 /* no wan hardware present in system */
4461 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004462 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004463 "wan hardware not installed\n");
4464 }
4465
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004466 if (!tp_features.wan)
4467 return 1;
4468
Johannes Berg19d337d2009-06-02 13:01:37 +02004469 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4470 &wan_tprfk_ops,
4471 RFKILL_TYPE_WWAN,
4472 TPACPI_RFK_WWAN_SW_NAME,
4473 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004474 if (res)
4475 return res;
4476
Johannes Berg19d337d2009-06-02 13:01:37 +02004477 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4478 &wan_attr_group);
4479
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004480 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004481 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004482 return res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004483 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004484
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004485 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004486}
4487
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004488/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004489static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004490{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004491 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004492}
4493
4494static int wan_write(char *buf)
4495{
Johannes Berg19d337d2009-06-02 13:01:37 +02004496 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004497}
4498
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004499static struct ibm_struct wan_driver_data = {
4500 .name = "wan",
4501 .read = wan_read,
4502 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004503 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004504 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004505};
4506
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004507/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004508 * UWB subdriver
4509 */
4510
4511enum {
4512 /* ACPI GUWB/SUWB bits */
4513 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4514 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4515};
4516
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004517#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4518
Johannes Berg19d337d2009-06-02 13:01:37 +02004519static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004520{
4521 int status;
4522
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004523#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4524 if (dbg_uwbemul)
4525 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004526 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004527#endif
4528
4529 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4530 return -EIO;
4531
4532 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004533 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004534}
4535
Johannes Berg19d337d2009-06-02 13:01:37 +02004536static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004537{
4538 int status;
4539
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004540 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004541 "will attempt to %s UWB\n",
4542 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004543
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004544#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4545 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004546 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004547 return 0;
4548 }
4549#endif
4550
Johannes Berg19d337d2009-06-02 13:01:37 +02004551 if (state == TPACPI_RFK_RADIO_ON)
4552 status = TP_ACPI_UWB_RADIOSSW;
4553 else
4554 status = 0;
4555
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004556 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4557 return -EIO;
4558
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004559 return 0;
4560}
4561
4562/* --------------------------------------------------------------------- */
4563
Johannes Berg19d337d2009-06-02 13:01:37 +02004564static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4565 .get_status = uwb_get_status,
4566 .set_status = uwb_set_status,
4567};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004568
4569static void uwb_exit(void)
4570{
Johannes Berg19d337d2009-06-02 13:01:37 +02004571 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004572}
4573
4574static int __init uwb_init(struct ibm_init_struct *iibm)
4575{
4576 int res;
4577 int status = 0;
4578
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004579 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4580 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004581
4582 TPACPI_ACPIHANDLE_INIT(hkey);
4583
4584 tp_features.uwb = hkey_handle &&
4585 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4586
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004587 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4588 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004589 str_supported(tp_features.uwb),
4590 status);
4591
4592#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4593 if (dbg_uwbemul) {
4594 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004595 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004596 } else
4597#endif
4598 if (tp_features.uwb &&
4599 !(status & TP_ACPI_UWB_HWPRESENT)) {
4600 /* no uwb hardware present in system */
4601 tp_features.uwb = 0;
4602 dbg_printk(TPACPI_DBG_INIT,
4603 "uwb hardware not installed\n");
4604 }
4605
4606 if (!tp_features.uwb)
4607 return 1;
4608
4609 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004610 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004611 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004612 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004613 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004614 return res;
4615}
4616
4617static struct ibm_struct uwb_driver_data = {
4618 .name = "uwb",
4619 .exit = uwb_exit,
4620 .flags.experimental = 1,
4621};
4622
4623/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004624 * Video subdriver
4625 */
4626
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004627#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4628
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004629enum video_access_mode {
4630 TPACPI_VIDEO_NONE = 0,
4631 TPACPI_VIDEO_570, /* 570 */
4632 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4633 TPACPI_VIDEO_NEW, /* all others */
4634};
4635
4636enum { /* video status flags, based on VIDEO_570 */
4637 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4638 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4639 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4640};
4641
4642enum { /* TPACPI_VIDEO_570 constants */
4643 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4644 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4645 * video_status_flags */
4646 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4647 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4648};
4649
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004650static enum video_access_mode video_supported;
4651static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004652
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004653static int video_autosw_get(void);
4654static int video_autosw_set(int enable);
4655
Joe Perches112a6ee2011-04-04 10:06:24 -07004656TPACPI_HANDLE(vid, root,
4657 "\\_SB.PCI.AGP.VGA", /* 570 */
4658 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4659 "\\_SB.PCI0.VID0", /* 770e */
4660 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4661 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4662 "\\_SB.PCI0.AGP.VID", /* all others */
4663 ); /* R30, R31 */
4664
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004665TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004666
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004667static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004669 int ivga;
4670
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004671 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4672
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004673 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004674 if (tpacpi_is_ibm())
4675 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004676
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004677 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4678 /* G41, assume IVGA doesn't change */
4679 vid_handle = vid2_handle;
4680
4681 if (!vid_handle)
4682 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004683 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004684 else if (tpacpi_is_ibm() &&
4685 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004686 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004687 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004688 else if (tpacpi_is_ibm() &&
4689 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004690 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004691 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004692 else
4693 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004694 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004696 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4697 str_supported(video_supported != TPACPI_VIDEO_NONE),
4698 video_supported);
4699
Jan van den Berg72a979f2014-09-17 00:01:08 +02004700 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701}
4702
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004703static void video_exit(void)
4704{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004705 dbg_printk(TPACPI_DBG_EXIT,
4706 "restoring original video autoswitch mode\n");
4707 if (video_autosw_set(video_orig_autosw))
Joe Perches0978e012011-04-04 10:06:25 -07004708 pr_err("error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004709 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004710}
4711
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004712static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713{
4714 int status = 0;
4715 int i;
4716
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004717 switch (video_supported) {
4718 case TPACPI_VIDEO_570:
4719 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4720 TP_ACPI_VIDEO_570_PHSCMD))
4721 return -EIO;
4722 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4723 break;
4724 case TPACPI_VIDEO_770:
4725 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4726 return -EIO;
4727 if (i)
4728 status |= TP_ACPI_VIDEO_S_LCD;
4729 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4730 return -EIO;
4731 if (i)
4732 status |= TP_ACPI_VIDEO_S_CRT;
4733 break;
4734 case TPACPI_VIDEO_NEW:
4735 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4736 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4737 return -EIO;
4738 if (i)
4739 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004741 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4742 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4743 return -EIO;
4744 if (i)
4745 status |= TP_ACPI_VIDEO_S_LCD;
4746 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4747 return -EIO;
4748 if (i)
4749 status |= TP_ACPI_VIDEO_S_DVI;
4750 break;
4751 default:
4752 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
4755 return status;
4756}
4757
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004758static int video_outputsw_set(int status)
4759{
4760 int autosw;
4761 int res = 0;
4762
4763 switch (video_supported) {
4764 case TPACPI_VIDEO_570:
4765 res = acpi_evalf(NULL, NULL,
4766 "\\_SB.PHS2", "vdd",
4767 TP_ACPI_VIDEO_570_PHS2CMD,
4768 status | TP_ACPI_VIDEO_570_PHS2SET);
4769 break;
4770 case TPACPI_VIDEO_770:
4771 autosw = video_autosw_get();
4772 if (autosw < 0)
4773 return autosw;
4774
4775 res = video_autosw_set(1);
4776 if (res)
4777 return res;
4778 res = acpi_evalf(vid_handle, NULL,
4779 "ASWT", "vdd", status * 0x100, 0);
4780 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004781 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004782 return -EIO;
4783 }
4784 break;
4785 case TPACPI_VIDEO_NEW:
4786 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004787 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004788 break;
4789 default:
4790 return -ENOSYS;
4791 }
4792
Jan van den Berg72a979f2014-09-17 00:01:08 +02004793 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004794}
4795
4796static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004798 int autosw = 0;
4799
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004800 switch (video_supported) {
4801 case TPACPI_VIDEO_570:
4802 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4803 return -EIO;
4804 break;
4805 case TPACPI_VIDEO_770:
4806 case TPACPI_VIDEO_NEW:
4807 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4808 return -EIO;
4809 break;
4810 default:
4811 return -ENOSYS;
4812 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004813
4814 return autosw & 1;
4815}
4816
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004817static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004818{
Jan van den Berg72a979f2014-09-17 00:01:08 +02004819 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004820 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004821 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004822}
4823
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004824static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004825{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004826 int autosw = video_autosw_get();
4827 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004828
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004829 if (autosw < 0)
4830 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004831
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004832 switch (video_supported) {
4833 case TPACPI_VIDEO_570:
4834 res = video_autosw_set(1);
4835 if (res)
4836 return res;
4837 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4838 break;
4839 case TPACPI_VIDEO_770:
4840 case TPACPI_VIDEO_NEW:
4841 res = video_autosw_set(1);
4842 if (res)
4843 return res;
4844 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4845 break;
4846 default:
4847 return -ENOSYS;
4848 }
4849 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004850 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004851 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004852 }
4853
Jan van den Berg72a979f2014-09-17 00:01:08 +02004854 return (res) ? 0 : -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004855}
4856
4857static int video_expand_toggle(void)
4858{
4859 switch (video_supported) {
4860 case TPACPI_VIDEO_570:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004861 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004862 0 : -EIO;
4863 case TPACPI_VIDEO_770:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004864 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004865 0 : -EIO;
4866 case TPACPI_VIDEO_NEW:
Jan van den Berg72a979f2014-09-17 00:01:08 +02004867 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004868 0 : -EIO;
4869 default:
4870 return -ENOSYS;
4871 }
4872 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004873}
4874
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004875static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004876{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004877 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004878
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004879 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004880 seq_printf(m, "status:\t\tnot supported\n");
4881 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004882 }
4883
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004884 /* Even reads can crash X.org, so... */
4885 if (!capable(CAP_SYS_ADMIN))
4886 return -EPERM;
4887
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004888 status = video_outputsw_get();
4889 if (status < 0)
4890 return status;
4891
4892 autosw = video_autosw_get();
4893 if (autosw < 0)
4894 return autosw;
4895
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004896 seq_printf(m, "status:\t\tsupported\n");
4897 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4898 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004899 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004900 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4901 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4902 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4903 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004904 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004905 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4906 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4907 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004908
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004909 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004910}
4911
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004912static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913{
4914 char *cmd;
4915 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004916 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004918 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004919 return -ENODEV;
4920
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004921 /* Even reads can crash X.org, let alone writes... */
4922 if (!capable(CAP_SYS_ADMIN))
4923 return -EPERM;
4924
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004925 enable = 0;
4926 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927
4928 while ((cmd = next_cmd(&buf))) {
4929 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004930 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004932 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004934 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004936 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004937 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004938 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004939 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004940 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004941 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004942 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004944 res = video_autosw_set(1);
4945 if (res)
4946 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004948 res = video_autosw_set(0);
4949 if (res)
4950 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004952 res = video_outputsw_cycle();
4953 if (res)
4954 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004956 res = video_expand_toggle();
4957 if (res)
4958 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 } else
4960 return -EINVAL;
4961 }
4962
4963 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004964 status = video_outputsw_get();
4965 if (status < 0)
4966 return status;
4967 res = video_outputsw_set((status & ~disable) | enable);
4968 if (res)
4969 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 }
4971
4972 return 0;
4973}
4974
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004975static struct ibm_struct video_driver_data = {
4976 .name = "video",
4977 .read = video_read,
4978 .write = video_write,
4979 .exit = video_exit,
4980};
4981
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004982#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4983
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004984/*************************************************************************
4985 * Light (thinklight) subdriver
4986 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004988TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4989TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004990
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004991static int light_get_status(void)
4992{
4993 int status = 0;
4994
4995 if (tp_features.light_status) {
4996 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4997 return -EIO;
4998 return (!!status);
4999 }
5000
5001 return -ENXIO;
5002}
5003
5004static int light_set_status(int status)
5005{
5006 int rc;
5007
5008 if (tp_features.light) {
5009 if (cmos_handle) {
5010 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005011 (status) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005012 TP_CMOS_THINKLIGHT_ON :
5013 TP_CMOS_THINKLIGHT_OFF);
5014 } else {
5015 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
Jan van den Berg72a979f2014-09-17 00:01:08 +02005016 (status) ? 1 : 0);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005017 }
Jan van den Berg72a979f2014-09-17 00:01:08 +02005018 return (rc) ? 0 : -EIO;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005019 }
5020
5021 return -ENXIO;
5022}
5023
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005024static void light_set_status_worker(struct work_struct *work)
5025{
5026 struct tpacpi_led_classdev *data =
5027 container_of(work, struct tpacpi_led_classdev, work);
5028
5029 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005030 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005031}
5032
5033static void light_sysfs_set(struct led_classdev *led_cdev,
5034 enum led_brightness brightness)
5035{
5036 struct tpacpi_led_classdev *data =
5037 container_of(led_cdev,
5038 struct tpacpi_led_classdev,
5039 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005040 data->new_state = (brightness != LED_OFF) ?
5041 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005042 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005043}
5044
5045static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5046{
Jan van den Berg72a979f2014-09-17 00:01:08 +02005047 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005048}
5049
5050static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5051 .led_classdev = {
5052 .name = "tpacpi::thinklight",
5053 .brightness_set = &light_sysfs_set,
5054 .brightness_get = &light_sysfs_get,
5055 }
5056};
5057
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005058static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005060 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005061
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005062 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5063
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005064 if (tpacpi_is_ibm()) {
5065 TPACPI_ACPIHANDLE_INIT(ledb);
5066 TPACPI_ACPIHANDLE_INIT(lght);
5067 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005068 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005069 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005070
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005071 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005072 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005073
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005074 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005075 /* light status not supported on
5076 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005077 tp_features.light_status =
5078 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005080 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5081 str_supported(tp_features.light),
5082 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005083
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005084 if (!tp_features.light)
5085 return 1;
5086
5087 rc = led_classdev_register(&tpacpi_pdev->dev,
5088 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005089
5090 if (rc < 0) {
5091 tp_features.light = 0;
5092 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005093 } else {
5094 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005095 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005096
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005097 return rc;
5098}
5099
5100static void light_exit(void)
5101{
5102 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Tejun Heo6d394e12012-12-21 17:56:58 -08005103 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104}
5105
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005106static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005108 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005110 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005111 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005112 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005113 seq_printf(m, "status:\t\tunknown\n");
5114 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005115 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005116 status = light_get_status();
5117 if (status < 0)
5118 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005119 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5120 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124}
5125
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005126static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005129 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005130
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005131 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005132 return -ENODEV;
5133
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 while ((cmd = next_cmd(&buf))) {
5135 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005136 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005138 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 } else
5140 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 }
5142
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005143 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144}
5145
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005146static struct ibm_struct light_driver_data = {
5147 .name = "light",
5148 .read = light_read,
5149 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03005150 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005151};
5152
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005153/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005154 * CMOS subdriver
5155 */
5156
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005157/* sysfs cmos_command -------------------------------------------------- */
5158static ssize_t cmos_command_store(struct device *dev,
5159 struct device_attribute *attr,
5160 const char *buf, size_t count)
5161{
5162 unsigned long cmos_cmd;
5163 int res;
5164
5165 if (parse_strtoul(buf, 21, &cmos_cmd))
5166 return -EINVAL;
5167
5168 res = issue_thinkpad_cmos_command(cmos_cmd);
Jan van den Berg72a979f2014-09-17 00:01:08 +02005169 return (res) ? res : count;
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005170}
5171
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01005172static DEVICE_ATTR_WO(cmos_command);
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005173
5174/* --------------------------------------------------------------------- */
5175
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005176static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005177{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005178 int res;
5179
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005180 vdbg_printk(TPACPI_DBG_INIT,
5181 "initializing cmos commands subdriver\n");
5182
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005183 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005184
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005185 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5186 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005187
5188 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5189 if (res)
5190 return res;
5191
Jan van den Berg72a979f2014-09-17 00:01:08 +02005192 return (cmos_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005193}
5194
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005195static void cmos_exit(void)
5196{
5197 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5198}
5199
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005200static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005202 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5203 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005205 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005207 seq_printf(m, "status:\t\tsupported\n");
5208 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 }
5210
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212}
5213
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005214static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215{
5216 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005217 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218
5219 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005220 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5221 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 /* cmos_cmd set */
5223 } else
5224 return -EINVAL;
5225
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005226 res = issue_thinkpad_cmos_command(cmos_cmd);
5227 if (res)
5228 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 }
5230
5231 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005232}
5233
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005234static struct ibm_struct cmos_driver_data = {
5235 .name = "cmos",
5236 .read = cmos_read,
5237 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005238 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005239};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005240
5241/*************************************************************************
5242 * LED subdriver
5243 */
5244
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005245enum led_access_mode {
5246 TPACPI_LED_NONE = 0,
5247 TPACPI_LED_570, /* 570 */
5248 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5249 TPACPI_LED_NEW, /* all others */
5250};
5251
5252enum { /* For TPACPI_LED_OLD */
5253 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5254 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5255 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5256};
5257
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005258static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005259
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005260static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005261
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005262#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005263static struct tpacpi_led_classdev *tpacpi_leds;
5264static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005265static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005266 /* there's a limit of 19 chars + NULL before 2.6.26 */
5267 "tpacpi::power",
5268 "tpacpi:orange:batt",
5269 "tpacpi:green:batt",
5270 "tpacpi::dock_active",
5271 "tpacpi::bay_active",
5272 "tpacpi::dock_batt",
5273 "tpacpi::unknown_led",
5274 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005275 "tpacpi::dock_status1",
5276 "tpacpi::dock_status2",
5277 "tpacpi::unknown_led2",
5278 "tpacpi::unknown_led3",
5279 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005280};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005281#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005282
5283static inline bool tpacpi_is_led_restricted(const unsigned int led)
5284{
5285#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5286 return false;
5287#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005288 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005289#endif
5290}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005291
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005292static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005293{
5294 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005295 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005296
5297 switch (led_supported) {
5298 case TPACPI_LED_570:
5299 if (!acpi_evalf(ec_handle,
5300 &status, "GLED", "dd", 1 << led))
5301 return -EIO;
Jan van den Berg72a979f2014-09-17 00:01:08 +02005302 led_s = (status == 0) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005303 TPACPI_LED_OFF :
Jan van den Berg72a979f2014-09-17 00:01:08 +02005304 ((status == 1) ?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005305 TPACPI_LED_ON :
5306 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005307 tpacpi_led_state_cache[led] = led_s;
5308 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005309 default:
5310 return -ENXIO;
5311 }
5312
5313 /* not reached */
5314}
5315
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005316static int led_set_status(const unsigned int led,
5317 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005318{
5319 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005320 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5321 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005322
5323 int rc = 0;
5324
5325 switch (led_supported) {
5326 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005327 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005328 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005329 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005330 if (unlikely(tpacpi_is_led_restricted(led)))
5331 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005332 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5333 (1 << led), led_sled_arg1[ledstatus]))
5334 rc = -EIO;
5335 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005336 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005337 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005338 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005339 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005340 if (unlikely(tpacpi_is_led_restricted(led)))
5341 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005342 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5343 if (rc >= 0)
5344 rc = ec_write(TPACPI_LED_EC_HLBL,
5345 (ledstatus == TPACPI_LED_BLINK) << led);
5346 if (rc >= 0)
5347 rc = ec_write(TPACPI_LED_EC_HLCL,
5348 (ledstatus != TPACPI_LED_OFF) << led);
5349 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005350 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005351 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005352 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5353 return -EINVAL;
5354 if (unlikely(tpacpi_is_led_restricted(led)))
5355 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005356 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5357 led, led_led_arg1[ledstatus]))
5358 rc = -EIO;
5359 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005360 default:
5361 rc = -ENXIO;
5362 }
5363
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005364 if (!rc)
5365 tpacpi_led_state_cache[led] = ledstatus;
5366
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005367 return rc;
5368}
5369
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005370static void led_set_status_worker(struct work_struct *work)
5371{
5372 struct tpacpi_led_classdev *data =
5373 container_of(work, struct tpacpi_led_classdev, work);
5374
5375 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005376 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005377}
5378
5379static void led_sysfs_set(struct led_classdev *led_cdev,
5380 enum led_brightness brightness)
5381{
5382 struct tpacpi_led_classdev *data = container_of(led_cdev,
5383 struct tpacpi_led_classdev, led_classdev);
5384
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005385 if (brightness == LED_OFF)
5386 data->new_state = TPACPI_LED_OFF;
5387 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5388 data->new_state = TPACPI_LED_ON;
5389 else
5390 data->new_state = TPACPI_LED_BLINK;
5391
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005392 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005393}
5394
5395static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5396 unsigned long *delay_on, unsigned long *delay_off)
5397{
5398 struct tpacpi_led_classdev *data = container_of(led_cdev,
5399 struct tpacpi_led_classdev, led_classdev);
5400
5401 /* Can we choose the flash rate? */
5402 if (*delay_on == 0 && *delay_off == 0) {
5403 /* yes. set them to the hardware blink rate (1 Hz) */
5404 *delay_on = 500; /* ms */
5405 *delay_off = 500; /* ms */
5406 } else if ((*delay_on != 500) || (*delay_off != 500))
5407 return -EINVAL;
5408
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005409 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005410 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005411
5412 return 0;
5413}
5414
5415static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5416{
5417 int rc;
5418
5419 struct tpacpi_led_classdev *data = container_of(led_cdev,
5420 struct tpacpi_led_classdev, led_classdev);
5421
5422 rc = led_get_status(data->led);
5423
5424 if (rc == TPACPI_LED_OFF || rc < 0)
5425 rc = LED_OFF; /* no error handling in led class :( */
5426 else
5427 rc = LED_FULL;
5428
5429 return rc;
5430}
5431
5432static void led_exit(void)
5433{
5434 unsigned int i;
5435
5436 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5437 if (tpacpi_leds[i].led_classdev.name)
5438 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5439 }
5440
Li Dongyange03e3892012-07-25 10:45:07 +10005441 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005442 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005443}
5444
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005445static int __init tpacpi_init_led(unsigned int led)
5446{
5447 int rc;
5448
5449 tpacpi_leds[led].led = led;
5450
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005451 /* LEDs with no name don't get registered */
5452 if (!tpacpi_led_names[led])
5453 return 0;
5454
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005455 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5456 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5457 if (led_supported == TPACPI_LED_570)
5458 tpacpi_leds[led].led_classdev.brightness_get =
5459 &led_sysfs_get;
5460
5461 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5462
5463 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5464
5465 rc = led_classdev_register(&tpacpi_pdev->dev,
5466 &tpacpi_leds[led].led_classdev);
5467 if (rc < 0)
5468 tpacpi_leds[led].led_classdev.name = NULL;
5469
5470 return rc;
5471}
5472
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005473static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5474 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5475 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5476 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5477
5478 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5479 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5480 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5481 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5482 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5483 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5484 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5485 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5486
5487 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5488 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5489 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5490 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5491 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5492
5493 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5494 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5495 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5496 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5497
5498 /* (1) - may have excess leds enabled on MSB */
5499
5500 /* Defaults (order matters, keep last, don't reorder!) */
5501 { /* Lenovo */
5502 .vendor = PCI_VENDOR_ID_LENOVO,
5503 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5504 .quirks = 0x1fffU,
5505 },
5506 { /* IBM ThinkPads with no EC version string */
5507 .vendor = PCI_VENDOR_ID_IBM,
5508 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5509 .quirks = 0x00ffU,
5510 },
5511 { /* IBM ThinkPads with EC version string */
5512 .vendor = PCI_VENDOR_ID_IBM,
5513 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5514 .quirks = 0x00bfU,
5515 },
5516};
5517
5518#undef TPACPI_LEDQ_IBM
5519#undef TPACPI_LEDQ_LNV
5520
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005521static enum led_access_mode __init led_init_detect_mode(void)
5522{
5523 acpi_status status;
5524
5525 if (tpacpi_is_ibm()) {
5526 /* 570 */
5527 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5528 if (ACPI_SUCCESS(status))
5529 return TPACPI_LED_570;
5530
5531 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5532 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5533 if (ACPI_SUCCESS(status))
5534 return TPACPI_LED_OLD;
5535 }
5536
5537 /* most others */
5538 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5539 if (ACPI_SUCCESS(status))
5540 return TPACPI_LED_NEW;
5541
5542 /* R30, R31, and unknown firmwares */
5543 led_handle = NULL;
5544 return TPACPI_LED_NONE;
5545}
5546
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005547static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005548{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005549 unsigned int i;
5550 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005551 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005552
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005553 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5554
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005555 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005556
Adam Leefcb44e12013-06-07 16:20:08 +08005557 if (led_supported != TPACPI_LED_NONE) {
5558 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5559 ARRAY_SIZE(led_useful_qtable));
5560
5561 if (!useful_leds) {
5562 led_handle = NULL;
5563 led_supported = TPACPI_LED_NONE;
5564 }
5565 }
5566
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005567 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5568 str_supported(led_supported), led_supported);
5569
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005570 if (led_supported == TPACPI_LED_NONE)
5571 return 1;
5572
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005573 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5574 GFP_KERNEL);
5575 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005576 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005577 return -ENOMEM;
5578 }
5579
5580 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08005581 tpacpi_leds[i].led = -1;
5582
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005583 if (!tpacpi_is_led_restricted(i) &&
5584 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005585 rc = tpacpi_init_led(i);
5586 if (rc < 0) {
5587 led_exit();
5588 return rc;
5589 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005590 }
5591 }
5592
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005593#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Joe Perches0978e012011-04-04 10:06:25 -07005594 pr_notice("warning: userspace override of important "
5595 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005596#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005597 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005598}
5599
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005600#define str_led_status(s) \
5601 ((s) == TPACPI_LED_OFF ? "off" : \
5602 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005603
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005604static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005606 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005607 seq_printf(m, "status:\t\tnot supported\n");
5608 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005609 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005610 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005611
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005612 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005613 /* 570 */
5614 int i, status;
5615 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005616 status = led_get_status(i);
5617 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005618 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005619 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005620 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005621 }
5622 }
5623
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005624 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005625 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005627 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628}
5629
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005630static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631{
5632 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005633 int led, rc;
5634 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005635
5636 if (!led_supported)
5637 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638
5639 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08005640 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 return -EINVAL;
5642
Adam Leeedf2d772013-06-08 16:51:15 +08005643 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5644 tpacpi_leds[led].led < 0)
5645 return -ENODEV;
5646
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005647 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005648 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005650 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005651 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005652 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005653 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005654 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005655 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005656
5657 rc = led_set_status(led, s);
5658 if (rc < 0)
5659 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 }
5661
5662 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005663}
5664
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005665static struct ibm_struct led_driver_data = {
5666 .name = "led",
5667 .read = led_read,
5668 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005669 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005670};
5671
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005672/*************************************************************************
5673 * Beep subdriver
5674 */
5675
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005676TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005677
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005678#define TPACPI_BEEP_Q1 0x0001
5679
5680static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5681 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5682 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5683};
5684
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005685static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005686{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005687 unsigned long quirks;
5688
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005689 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5690
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005691 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005692
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005693 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5694 str_supported(beep_handle != NULL));
5695
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005696 quirks = tpacpi_check_quirks(beep_quirk_table,
5697 ARRAY_SIZE(beep_quirk_table));
5698
5699 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5700
Jan van den Berg72a979f2014-09-17 00:01:08 +02005701 return (beep_handle) ? 0 : 1;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005702}
5703
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005704static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005706 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005707 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005708 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005709 seq_printf(m, "status:\t\tsupported\n");
5710 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714}
5715
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005716static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717{
5718 char *cmd;
5719 int beep_cmd;
5720
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005721 if (!beep_handle)
5722 return -ENODEV;
5723
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005725 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5726 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 /* beep_cmd set */
5728 } else
5729 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005730 if (tp_features.beep_needs_two_args) {
5731 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5732 beep_cmd, 0))
5733 return -EIO;
5734 } else {
5735 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5736 beep_cmd))
5737 return -EIO;
5738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 }
5740
5741 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005742}
5743
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005744static struct ibm_struct beep_driver_data = {
5745 .name = "beep",
5746 .read = beep_read,
5747 .write = beep_write,
5748};
5749
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005750/*************************************************************************
5751 * Thermal subdriver
5752 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005753
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005754enum thermal_access_mode {
5755 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5756 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5757 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5758 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5759 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5760};
5761
5762enum { /* TPACPI_THERMAL_TPEC_* */
5763 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5764 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5765 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005766
5767 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005768};
5769
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005770
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005771#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5772struct ibm_thermal_sensors_struct {
5773 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5774};
5775
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005776static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005777
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005778/* idx is zero-based */
5779static int thermal_get_sensor(int idx, s32 *value)
5780{
5781 int t;
5782 s8 tmp;
5783 char tmpi[5];
5784
5785 t = TP_EC_THERMAL_TMP0;
5786
5787 switch (thermal_read_mode) {
5788#if TPACPI_MAX_THERMAL_SENSORS >= 16
5789 case TPACPI_THERMAL_TPEC_16:
5790 if (idx >= 8 && idx <= 15) {
5791 t = TP_EC_THERMAL_TMP8;
5792 idx -= 8;
5793 }
5794 /* fallthrough */
5795#endif
5796 case TPACPI_THERMAL_TPEC_8:
5797 if (idx <= 7) {
5798 if (!acpi_ec_read(t + idx, &tmp))
5799 return -EIO;
5800 *value = tmp * 1000;
5801 return 0;
5802 }
5803 break;
5804
5805 case TPACPI_THERMAL_ACPI_UPDT:
5806 if (idx <= 7) {
5807 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5808 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5809 return -EIO;
5810 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5811 return -EIO;
5812 *value = (t - 2732) * 100;
5813 return 0;
5814 }
5815 break;
5816
5817 case TPACPI_THERMAL_ACPI_TMP07:
5818 if (idx <= 7) {
5819 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5820 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5821 return -EIO;
5822 if (t > 127 || t < -127)
5823 t = TP_EC_THERMAL_TMP_NA;
5824 *value = t * 1000;
5825 return 0;
5826 }
5827 break;
5828
5829 case TPACPI_THERMAL_NONE:
5830 default:
5831 return -ENOSYS;
5832 }
5833
5834 return -EINVAL;
5835}
5836
5837static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5838{
5839 int res, i;
5840 int n;
5841
5842 n = 8;
5843 i = 0;
5844
5845 if (!s)
5846 return -EINVAL;
5847
5848 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5849 n = 16;
5850
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005851 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005852 res = thermal_get_sensor(i, &s->temp[i]);
5853 if (res)
5854 return res;
5855 }
5856
5857 return n;
5858}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005859
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005860static void thermal_dump_all_sensors(void)
5861{
5862 int n, i;
5863 struct ibm_thermal_sensors_struct t;
5864
5865 n = thermal_get_sensors(&t);
5866 if (n <= 0)
5867 return;
5868
Joe Perches0978e012011-04-04 10:06:25 -07005869 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005870
5871 for (i = 0; i < n; i++) {
5872 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07005873 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005874 else
Joe Perches0978e012011-04-04 10:06:25 -07005875 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005876 }
5877
Joe Perches0978e012011-04-04 10:06:25 -07005878 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005879}
5880
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005881/* sysfs temp##_input -------------------------------------------------- */
5882
5883static ssize_t thermal_temp_input_show(struct device *dev,
5884 struct device_attribute *attr,
5885 char *buf)
5886{
5887 struct sensor_device_attribute *sensor_attr =
5888 to_sensor_dev_attr(attr);
5889 int idx = sensor_attr->index;
5890 s32 value;
5891 int res;
5892
5893 res = thermal_get_sensor(idx, &value);
5894 if (res)
5895 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005896 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005897 return -ENXIO;
5898
5899 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5900}
5901
5902#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005903 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5904 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005905
5906static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5907 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5908 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5909 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5910 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5911 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5912 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5913 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5914 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5915 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5916 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5917 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5918 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5919 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5920 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5921 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5922 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5923};
5924
5925#define THERMAL_ATTRS(X) \
5926 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5927
5928static struct attribute *thermal_temp_input_attr[] = {
5929 THERMAL_ATTRS(8),
5930 THERMAL_ATTRS(9),
5931 THERMAL_ATTRS(10),
5932 THERMAL_ATTRS(11),
5933 THERMAL_ATTRS(12),
5934 THERMAL_ATTRS(13),
5935 THERMAL_ATTRS(14),
5936 THERMAL_ATTRS(15),
5937 THERMAL_ATTRS(0),
5938 THERMAL_ATTRS(1),
5939 THERMAL_ATTRS(2),
5940 THERMAL_ATTRS(3),
5941 THERMAL_ATTRS(4),
5942 THERMAL_ATTRS(5),
5943 THERMAL_ATTRS(6),
5944 THERMAL_ATTRS(7),
5945 NULL
5946};
5947
5948static const struct attribute_group thermal_temp_input16_group = {
5949 .attrs = thermal_temp_input_attr
5950};
5951
5952static const struct attribute_group thermal_temp_input8_group = {
5953 .attrs = &thermal_temp_input_attr[8]
5954};
5955
5956#undef THERMAL_SENSOR_ATTR_TEMP
5957#undef THERMAL_ATTRS
5958
5959/* --------------------------------------------------------------------- */
5960
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005961static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005962{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005963 u8 t, ta1, ta2;
5964 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005965 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005966 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005967
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005968 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5969
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005970 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005971
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005972 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005973 /*
5974 * Direct EC access mode: sensors at registers
5975 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5976 * non-implemented, thermal sensors return 0x80 when
5977 * not available
5978 */
5979
5980 ta1 = ta2 = 0;
5981 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005982 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005983 ta1 |= t;
5984 } else {
5985 ta1 = 0;
5986 break;
5987 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005988 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005989 ta2 |= t;
5990 } else {
5991 ta1 = 0;
5992 break;
5993 }
5994 }
5995 if (ta1 == 0) {
5996 /* This is sheer paranoia, but we handle it anyway */
5997 if (acpi_tmp7) {
Joe Perches0978e012011-04-04 10:06:25 -07005998 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005999 "falling back to ACPI TMPx access "
6000 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006001 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006002 } else {
Joe Perches0978e012011-04-04 10:06:25 -07006003 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006004 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006005 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006006 }
6007 } else {
6008 thermal_read_mode =
6009 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006010 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006011 }
6012 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006013 if (tpacpi_is_ibm() &&
6014 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006015 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006016 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006017 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006018 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006019 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006020 }
6021 } else {
6022 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006023 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006024 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006025
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006026 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6027 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6028 thermal_read_mode);
6029
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006030 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006031 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006032 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006033 &thermal_temp_input16_group);
6034 if (res)
6035 return res;
6036 break;
6037 case TPACPI_THERMAL_TPEC_8:
6038 case TPACPI_THERMAL_ACPI_TMP07:
6039 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006040 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006041 &thermal_temp_input8_group);
6042 if (res)
6043 return res;
6044 break;
6045 case TPACPI_THERMAL_NONE:
6046 default:
6047 return 1;
6048 }
6049
6050 return 0;
6051}
6052
6053static void thermal_exit(void)
6054{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006055 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006056 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006057 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006058 &thermal_temp_input16_group);
6059 break;
6060 case TPACPI_THERMAL_TPEC_8:
6061 case TPACPI_THERMAL_ACPI_TMP07:
6062 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006063 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08006064 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006065 break;
6066 case TPACPI_THERMAL_NONE:
6067 default:
6068 break;
6069 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006070}
6071
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006072static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006073{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006074 int n, i;
6075 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006076
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006077 n = thermal_get_sensors(&t);
6078 if (unlikely(n < 0))
6079 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006080
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006081 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006082
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006083 if (n > 0) {
6084 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006085 seq_printf(m, "%d ", t.temp[i] / 1000);
6086 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02006087 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006088 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006089
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006090 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006091}
6092
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006093static struct ibm_struct thermal_driver_data = {
6094 .name = "thermal",
6095 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03006096 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006097};
6098
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006099/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006100 * Backlight/brightness subdriver
6101 */
Holger Macht8acb0252006-10-20 14:30:28 -07006102
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006103#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6104
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006105/*
6106 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6107 * CMOS NVRAM byte 0x5E, bits 0-3.
6108 *
6109 * EC HBRV (0x31) has the following layout
6110 * Bit 7: unknown function
6111 * Bit 6: unknown function
6112 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6113 * Bit 4: must be set to zero to avoid problems
6114 * Bit 3-0: backlight brightness level
6115 *
6116 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006117 *
6118 * WARNING: The X61 has been verified to use HBRV for something else, so
6119 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6120 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006121 */
6122
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006123enum {
6124 TP_EC_BACKLIGHT = 0x31,
6125
6126 /* TP_EC_BACKLIGHT bitmasks */
6127 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6128 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6129 TP_EC_BACKLIGHT_MAPSW = 0x20,
6130};
6131
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006132enum tpacpi_brightness_access_mode {
6133 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6134 TPACPI_BRGHT_MODE_EC, /* EC control */
6135 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6136 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6137 TPACPI_BRGHT_MODE_MAX
6138};
6139
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006140static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006141
6142static enum tpacpi_brightness_access_mode brightness_mode =
6143 TPACPI_BRGHT_MODE_MAX;
6144
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006145static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6146
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006147static struct mutex brightness_mutex;
6148
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006149/* NVRAM brightness access,
6150 * call with brightness_mutex held! */
6151static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006152{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006153 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006154
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006155 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6156 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6157 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006158 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006159
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006160 return lnvram;
6161}
6162
6163static void tpacpi_brightness_checkpoint_nvram(void)
6164{
6165 u8 lec = 0;
6166 u8 b_nvram;
6167
6168 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6169 return;
6170
6171 vdbg_printk(TPACPI_DBG_BRGHT,
6172 "trying to checkpoint backlight level to NVRAM...\n");
6173
6174 if (mutex_lock_killable(&brightness_mutex) < 0)
6175 return;
6176
6177 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6178 goto unlock;
6179 lec &= TP_EC_BACKLIGHT_LVLMSK;
6180 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6181
6182 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6183 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6184 /* NVRAM needs update */
6185 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6186 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6187 b_nvram |= lec;
6188 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6189 dbg_printk(TPACPI_DBG_BRGHT,
6190 "updated NVRAM backlight level to %u (0x%02x)\n",
6191 (unsigned int) lec, (unsigned int) b_nvram);
6192 } else
6193 vdbg_printk(TPACPI_DBG_BRGHT,
6194 "NVRAM backlight level already is %u (0x%02x)\n",
6195 (unsigned int) lec, (unsigned int) b_nvram);
6196
6197unlock:
6198 mutex_unlock(&brightness_mutex);
6199}
6200
6201
6202/* call with brightness_mutex held! */
6203static int tpacpi_brightness_get_raw(int *status)
6204{
6205 u8 lec = 0;
6206
6207 switch (brightness_mode) {
6208 case TPACPI_BRGHT_MODE_UCMS_STEP:
6209 *status = tpacpi_brightness_nvram_get();
6210 return 0;
6211 case TPACPI_BRGHT_MODE_EC:
6212 case TPACPI_BRGHT_MODE_ECNVRAM:
6213 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006214 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006215 *status = lec;
6216 return 0;
6217 default:
6218 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006219 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006220}
6221
6222/* call with brightness_mutex held! */
6223/* do NOT call with illegal backlight level value */
6224static int tpacpi_brightness_set_ec(unsigned int value)
6225{
6226 u8 lec = 0;
6227
6228 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6229 return -EIO;
6230
6231 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6232 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6233 (value & TP_EC_BACKLIGHT_LVLMSK))))
6234 return -EIO;
6235
6236 return 0;
6237}
6238
6239/* call with brightness_mutex held! */
6240static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6241{
6242 int cmos_cmd, inc;
6243 unsigned int current_value, i;
6244
6245 current_value = tpacpi_brightness_nvram_get();
6246
6247 if (value == current_value)
6248 return 0;
6249
6250 cmos_cmd = (value > current_value) ?
6251 TP_CMOS_BRIGHTNESS_UP :
6252 TP_CMOS_BRIGHTNESS_DOWN;
6253 inc = (value > current_value) ? 1 : -1;
6254
6255 for (i = current_value; i != value; i += inc)
6256 if (issue_thinkpad_cmos_command(cmos_cmd))
6257 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006258
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006259 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006260}
6261
6262/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006263static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006264{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006265 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006266
Andrey Utkinbd3c7b9e2014-07-15 01:56:21 +03006267 if (value > bright_maxlvl)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006268 return -EINVAL;
6269
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006270 vdbg_printk(TPACPI_DBG_BRGHT,
6271 "set backlight level to %d\n", value);
6272
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006273 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006274 if (res < 0)
6275 return res;
6276
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006277 switch (brightness_mode) {
6278 case TPACPI_BRGHT_MODE_EC:
6279 case TPACPI_BRGHT_MODE_ECNVRAM:
6280 res = tpacpi_brightness_set_ec(value);
6281 break;
6282 case TPACPI_BRGHT_MODE_UCMS_STEP:
6283 res = tpacpi_brightness_set_ucmsstep(value);
6284 break;
6285 default:
6286 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006287 }
6288
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006289 mutex_unlock(&brightness_mutex);
6290 return res;
6291}
6292
6293/* sysfs backlight class ----------------------------------------------- */
6294
6295static int brightness_update_status(struct backlight_device *bd)
6296{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006297 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006298 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6299 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006300 bd->props.brightness : 0;
6301
6302 dbg_printk(TPACPI_DBG_BRGHT,
6303 "backlight: attempt to set level to %d\n",
6304 level);
6305
6306 /* it is the backlight class's job (caller) to handle
6307 * EINTR and other errors properly */
6308 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006309}
Holger Macht8acb0252006-10-20 14:30:28 -07006310
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006311static int brightness_get(struct backlight_device *bd)
6312{
6313 int status, res;
6314
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006315 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006316 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006317 return 0;
6318
6319 res = tpacpi_brightness_get_raw(&status);
6320
6321 mutex_unlock(&brightness_mutex);
6322
6323 if (res < 0)
6324 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006325
6326 return status & TP_EC_BACKLIGHT_LVLMSK;
6327}
6328
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006329static void tpacpi_brightness_notify_change(void)
6330{
6331 backlight_force_update(ibm_backlight_device,
6332 BACKLIGHT_UPDATE_HOTKEY);
6333}
6334
Lionel Debrouxacc24722010-11-16 14:14:02 +01006335static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006336 .get_brightness = brightness_get,
6337 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006338};
6339
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006340/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006341
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006342/*
6343 * Call _BCL method of video device. On some ThinkPads this will
6344 * switch the firmware to the ACPI brightness control mode.
6345 */
6346
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006347static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6348{
6349 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6350 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006351 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006352 int rc;
6353
Aaron Lu46445b62013-10-11 21:27:46 +08006354 if (acpi_bus_get_device(handle, &device))
6355 return 0;
6356
6357 rc = 0;
6358 list_for_each_entry(child, &device->children, node) {
6359 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6360 NULL, &buffer);
6361 if (ACPI_FAILURE(status))
6362 continue;
6363
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006364 obj = (union acpi_object *)buffer.pointer;
6365 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006366 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006367 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006368 rc = 0;
6369 } else {
6370 rc = obj->package.count;
6371 }
Aaron Lu46445b62013-10-11 21:27:46 +08006372 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006373 }
6374
6375 kfree(buffer.pointer);
6376 return rc;
6377}
6378
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006379
6380/*
6381 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6382 */
6383static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6384{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006385 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006386 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006387
Aaron Lu46445b62013-10-11 21:27:46 +08006388 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006389 if (video_device)
6390 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006391
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006392 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006393
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006394 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006395}
6396
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006397/*
6398 * These are only useful for models that have only one possibility
6399 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6400 * these quirks.
6401 */
6402#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6403#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6404#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6405
6406static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6407 /* Models with ATI GPUs known to require ECNVRAM mode */
6408 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6409
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006410 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006411 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006412 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006413 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006414 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6415
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006416 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006417 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006418 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6419 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006420
6421 /* Models with Intel GMA900 */
6422 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6423 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6424 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6425};
6426
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006427/*
6428 * Returns < 0 for error, otherwise sets tp_features.bright_*
6429 * and bright_maxlvl.
6430 */
6431static void __init tpacpi_detect_brightness_capabilities(void)
6432{
6433 unsigned int b;
6434
6435 vdbg_printk(TPACPI_DBG_INIT,
6436 "detecting firmware brightness interface capabilities\n");
6437
6438 /* we could run a quirks check here (same table used by
6439 * brightness_init) if needed */
6440
6441 /*
6442 * We always attempt to detect acpi support, so as to switch
6443 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6444 * going to publish a backlight interface
6445 */
6446 b = tpacpi_check_std_acpi_brightness_support();
6447 switch (b) {
6448 case 16:
6449 bright_maxlvl = 15;
Joe Perches0978e012011-04-04 10:06:25 -07006450 pr_info("detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006451 break;
6452 case 8:
6453 case 0:
6454 bright_maxlvl = 7;
Joe Perches0978e012011-04-04 10:06:25 -07006455 pr_info("detected a 8-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006456 break;
6457 default:
Joe Perches0978e012011-04-04 10:06:25 -07006458 pr_err("Unsupported brightness interface, "
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006459 "please contact %s\n", TPACPI_MAIL);
6460 tp_features.bright_unkfw = 1;
6461 bright_maxlvl = b - 1;
6462 }
6463}
6464
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006465static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006466{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006467 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006468 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006469 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006470
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006471 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6472
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006473 mutex_init(&brightness_mutex);
6474
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006475 quirks = tpacpi_check_quirks(brightness_quirk_table,
6476 ARRAY_SIZE(brightness_quirk_table));
6477
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006478 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006479
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006480 /* if it is unknown, we don't handle it: it wouldn't be safe */
6481 if (tp_features.bright_unkfw)
6482 return 1;
6483
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006484 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006485 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006486 "brightness support disabled by "
6487 "module parameter\n");
6488 return 1;
6489 }
6490
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006491 if (acpi_video_backlight_support()) {
6492 if (brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006493 pr_info("Standard ACPI backlight interface "
6494 "available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006495 return 1;
6496 } else if (brightness_enable == 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006497 pr_warn("Cannot enable backlight brightness support, "
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006498 "ACPI is already handling it. Refer to the "
Joe Perches0978e012011-04-04 10:06:25 -07006499 "acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006500 return 1;
6501 }
6502 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006503 pr_notice("Standard ACPI backlight interface not "
6504 "available, thinkpad_acpi native "
6505 "brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006506 }
6507
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006508 /*
6509 * Check for module parameter bogosity, note that we
6510 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6511 * able to detect "unspecified"
6512 */
6513 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006514 return -EINVAL;
6515
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006516 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6517 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6518 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006519 if (quirks & TPACPI_BRGHT_Q_EC)
6520 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6521 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006522 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6523
6524 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006525 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006526 brightness_mode);
6527 }
6528
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006529 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006530 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006531 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6532 brightness_mode == TPACPI_BRGHT_MODE_EC))
6533 return -EINVAL;
6534
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006535 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006536 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006537
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006538 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006539 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006540 props.max_brightness = bright_maxlvl;
6541 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006542 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6543 NULL, NULL,
6544 &ibm_backlight_data,
6545 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006546 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006547 int rc = PTR_ERR(ibm_backlight_device);
6548 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006549 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006550 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006551 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006552 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6553 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006554
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006555 if (quirks & TPACPI_BRGHT_Q_ASK) {
Joe Perches0978e012011-04-04 10:06:25 -07006556 pr_notice("brightness: will use unverified default: "
6557 "brightness_mode=%d\n", brightness_mode);
6558 pr_notice("brightness: please report to %s whether it works well "
6559 "or not on your ThinkPad\n", TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006560 }
6561
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006562 /* Added by mistake in early 2007. Probably useless, but it could
6563 * be working around some unknown firmware problem where the value
6564 * read at startup doesn't match the real hardware state... so leave
6565 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006566 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006567
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006568 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6569 "brightness: registering brightness hotkeys "
6570 "as change notification\n");
6571 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6572 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006573 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006574 return 0;
6575}
6576
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006577static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006578{
6579 tpacpi_brightness_checkpoint_nvram();
6580}
6581
6582static void brightness_shutdown(void)
6583{
6584 tpacpi_brightness_checkpoint_nvram();
6585}
6586
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006587static void brightness_exit(void)
6588{
6589 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006590 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006591 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006592 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006593 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006594
6595 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006596}
6597
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006598static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006599{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006600 int level;
6601
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006602 level = brightness_get(NULL);
6603 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006604 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006605 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006606 seq_printf(m, "level:\t\t%d\n", level);
6607 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006608 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6609 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006610 }
6611
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006612 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006613}
6614
6615static int brightness_write(char *buf)
6616{
6617 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006618 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006619 char *cmd;
6620
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006621 level = brightness_get(NULL);
6622 if (level < 0)
6623 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006624
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006625 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006626 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006627 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006628 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006629 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006630 if (level > 0)
6631 level--;
6632 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006633 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006634 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006635 } else
6636 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006637 }
6638
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006639 tpacpi_disclose_usertask("procfs brightness",
6640 "set level to %d\n", level);
6641
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006642 /*
6643 * Now we know what the final level should be, so we try to set it.
6644 * Doing it this way makes the syscall restartable in case of EINTR
6645 */
6646 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006647 if (!rc && ibm_backlight_device)
6648 backlight_force_update(ibm_backlight_device,
6649 BACKLIGHT_UPDATE_SYSFS);
Jan van den Berg72a979f2014-09-17 00:01:08 +02006650 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006651}
6652
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006653static struct ibm_struct brightness_driver_data = {
6654 .name = "brightness",
6655 .read = brightness_read,
6656 .write = brightness_write,
6657 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006658 .suspend = brightness_suspend,
6659 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006660};
6661
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006662/*************************************************************************
6663 * Volume subdriver
6664 */
6665
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006666/*
6667 * IBM ThinkPads have a simple volume controller with MUTE gating.
6668 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6669 *
6670 * Since the *61 series (and probably also the later *60 series), Lenovo
6671 * ThinkPads only implement the MUTE gate.
6672 *
6673 * EC register 0x30
6674 * Bit 6: MUTE (1 mutes sound)
6675 * Bit 3-0: Volume
6676 * Other bits should be zero as far as we know.
6677 *
6678 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6679 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6680 * such as bit 7 which is used to detect repeated presses of MUTE,
6681 * and we leave them unchanged.
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07006682 *
6683 * On newer Lenovo ThinkPads, the EC can automatically change the volume
6684 * in response to user input. Unfortunately, this rarely works well.
6685 * The laptop changes the state of its internal MUTE gate and, on some
6686 * models, sends KEY_MUTE, causing any user code that responds to the
6687 * mute button to get confused. The hardware MUTE gate is also
6688 * unnecessary, since user code can handle the mute button without
6689 * kernel or EC help.
6690 *
6691 * To avoid confusing userspace, we simply disable all EC-based mute
6692 * and volume controls when possible.
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006693 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006694
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006695#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6696
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006697#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6698#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6699#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6700
Takashi Iwaicab66612013-10-24 16:06:32 +02006701#if SNDRV_CARDS <= 32
6702#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
6703#else
6704#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
6705#endif
6706static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006707static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10306708static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006709
6710struct tpacpi_alsa_data {
6711 struct snd_card *card;
6712 struct snd_ctl_elem_id *ctl_mute_id;
6713 struct snd_ctl_elem_id *ctl_vol_id;
6714};
6715
6716static struct snd_card *alsa_card;
6717
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006718enum {
6719 TP_EC_AUDIO = 0x30,
6720
6721 /* TP_EC_AUDIO bits */
6722 TP_EC_AUDIO_MUTESW = 6,
6723
6724 /* TP_EC_AUDIO bitmasks */
6725 TP_EC_AUDIO_LVL_MSK = 0x0F,
6726 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6727
6728 /* Maximum volume */
6729 TP_EC_VOLUME_MAX = 14,
6730};
6731
6732enum tpacpi_volume_access_mode {
6733 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6734 TPACPI_VOL_MODE_EC, /* Pure EC control */
6735 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6736 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6737 TPACPI_VOL_MODE_MAX
6738};
6739
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006740enum tpacpi_volume_capabilities {
6741 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6742 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6743 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6744 TPACPI_VOL_CAP_MAX
6745};
6746
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07006747enum tpacpi_mute_btn_mode {
6748 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
6749 /* We don't know what mode 1 is. */
6750 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
6751 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
6752};
6753
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006754static enum tpacpi_volume_access_mode volume_mode =
6755 TPACPI_VOL_MODE_MAX;
6756
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006757static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10306758static bool volume_control_allowed;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07006759static bool software_mute_requested = true;
6760static bool software_mute_active;
6761static int software_mute_orig_mode;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006762
6763/*
6764 * Used to syncronize writers to TP_EC_AUDIO and
6765 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6766 */
6767static struct mutex volume_mutex;
6768
6769static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006770{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006771 u8 lec = 0;
6772 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006773 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006774
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006775 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6776 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006777 if (!volume_control_allowed)
6778 return;
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07006779 if (software_mute_active)
6780 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006781
6782 vdbg_printk(TPACPI_DBG_MIXER,
6783 "trying to checkpoint mixer state to NVRAM...\n");
6784
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006785 if (tp_features.mixer_no_level_control)
6786 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6787 else
6788 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6789
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006790 if (mutex_lock_killable(&volume_mutex) < 0)
6791 return;
6792
6793 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6794 goto unlock;
6795 lec &= ec_mask;
6796 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6797
6798 if (lec != (b_nvram & ec_mask)) {
6799 /* NVRAM needs update */
6800 b_nvram &= ~ec_mask;
6801 b_nvram |= lec;
6802 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6803 dbg_printk(TPACPI_DBG_MIXER,
6804 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6805 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006806 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006807 vdbg_printk(TPACPI_DBG_MIXER,
6808 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6809 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006810 }
6811
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006812unlock:
6813 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006814}
6815
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006816static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006817{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006818 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006819
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006820 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6821 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006822
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006823 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006824
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006825 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006826
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006827 return 0;
6828}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006829
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006830static int volume_get_status(u8 *status)
6831{
6832 return volume_get_status_ec(status);
6833}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006834
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006835static int volume_set_status_ec(const u8 status)
6836{
6837 if (!acpi_ec_write(TP_EC_AUDIO, status))
6838 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006839
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006840 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6841
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07006842 /*
6843 * On X200s, and possibly on others, it can take a while for
6844 * reads to become correct.
6845 */
6846 msleep(1);
6847
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006848 return 0;
6849}
6850
6851static int volume_set_status(const u8 status)
6852{
6853 return volume_set_status_ec(status);
6854}
6855
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006856/* returns < 0 on error, 0 on no change, 1 on change */
6857static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006858{
6859 int rc;
6860 u8 s, n;
6861
6862 if (mutex_lock_killable(&volume_mutex) < 0)
6863 return -EINTR;
6864
6865 rc = volume_get_status_ec(&s);
6866 if (rc)
6867 goto unlock;
6868
6869 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6870 s & ~TP_EC_AUDIO_MUTESW_MSK;
6871
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006872 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006873 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006874 if (!rc)
6875 rc = 1;
6876 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006877
6878unlock:
6879 mutex_unlock(&volume_mutex);
6880 return rc;
6881}
6882
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006883static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006884{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006885 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006886 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006887 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006888}
6889
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006890static int volume_set_mute(const bool mute)
6891{
6892 int rc;
6893
6894 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6895 (mute) ? "" : "un");
6896
6897 rc = __volume_set_mute_ec(mute);
6898 return (rc < 0) ? rc : 0;
6899}
6900
6901/* returns < 0 on error, 0 on no change, 1 on change */
6902static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006903{
6904 int rc;
6905 u8 s, n;
6906
6907 if (vol > TP_EC_VOLUME_MAX)
6908 return -EINVAL;
6909
6910 if (mutex_lock_killable(&volume_mutex) < 0)
6911 return -EINTR;
6912
6913 rc = volume_get_status_ec(&s);
6914 if (rc)
6915 goto unlock;
6916
6917 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6918
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006919 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006920 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006921 if (!rc)
6922 rc = 1;
6923 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006924
6925unlock:
6926 mutex_unlock(&volume_mutex);
6927 return rc;
6928}
6929
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07006930static int volume_set_software_mute(bool startup)
6931{
6932 int result;
6933
6934 if (!tpacpi_is_lenovo())
6935 return -ENODEV;
6936
6937 if (startup) {
6938 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
6939 "HAUM", "qd"))
6940 return -EIO;
6941
6942 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6943 "Initial HAUM setting was %d\n",
6944 software_mute_orig_mode);
6945 }
6946
6947 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
6948 (int)TP_EC_MUTE_BTN_NONE))
6949 return -EIO;
6950
6951 if (result != TP_EC_MUTE_BTN_NONE)
6952 pr_warn("Unexpected SAUM result %d\n",
6953 result);
6954
6955 /*
6956 * In software mute mode, the standard codec controls take
6957 * precendence, so we unmute the ThinkPad HW switch at
6958 * startup. Just on case there are SAUM-capable ThinkPads
6959 * with level controls, set max HW volume as well.
6960 */
6961 if (tp_features.mixer_no_level_control)
6962 result = volume_set_mute(false);
6963 else
6964 result = volume_set_status(TP_EC_VOLUME_MAX);
6965
6966 if (result != 0)
6967 pr_warn("Failed to unmute the HW mute switch\n");
6968
6969 return 0;
6970}
6971
6972static void volume_exit_software_mute(void)
6973{
6974 int r;
6975
6976 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
6977 || r != software_mute_orig_mode)
6978 pr_warn("Failed to restore mute mode\n");
6979}
6980
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006981static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006982{
6983 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006984 "ALSA: trying to set volume level to %hu\n", vol);
6985 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006986}
6987
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006988static void volume_alsa_notify_change(void)
6989{
6990 struct tpacpi_alsa_data *d;
6991
6992 if (alsa_card && alsa_card->private_data) {
6993 d = alsa_card->private_data;
6994 if (d->ctl_mute_id)
6995 snd_ctl_notify(alsa_card,
6996 SNDRV_CTL_EVENT_MASK_VALUE,
6997 d->ctl_mute_id);
6998 if (d->ctl_vol_id)
6999 snd_ctl_notify(alsa_card,
7000 SNDRV_CTL_EVENT_MASK_VALUE,
7001 d->ctl_vol_id);
7002 }
7003}
7004
7005static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7006 struct snd_ctl_elem_info *uinfo)
7007{
7008 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7009 uinfo->count = 1;
7010 uinfo->value.integer.min = 0;
7011 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7012 return 0;
7013}
7014
7015static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7016 struct snd_ctl_elem_value *ucontrol)
7017{
7018 u8 s;
7019 int rc;
7020
7021 rc = volume_get_status(&s);
7022 if (rc < 0)
7023 return rc;
7024
7025 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7026 return 0;
7027}
7028
7029static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7030 struct snd_ctl_elem_value *ucontrol)
7031{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007032 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7033 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007034 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007035}
7036
7037#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7038
7039static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7040 struct snd_ctl_elem_value *ucontrol)
7041{
7042 u8 s;
7043 int rc;
7044
7045 rc = volume_get_status(&s);
7046 if (rc < 0)
7047 return rc;
7048
7049 ucontrol->value.integer.value[0] =
7050 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7051 return 0;
7052}
7053
7054static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7055 struct snd_ctl_elem_value *ucontrol)
7056{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03007057 tpacpi_disclose_usertask("ALSA", "%smute\n",
7058 ucontrol->value.integer.value[0] ?
7059 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03007060 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007061}
7062
Mathias Krausec3aa4722014-07-16 19:43:17 +02007063static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007064 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7065 .name = "Console Playback Volume",
7066 .index = 0,
7067 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7068 .info = volume_alsa_vol_info,
7069 .get = volume_alsa_vol_get,
7070};
7071
Mathias Krausec3aa4722014-07-16 19:43:17 +02007072static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007073 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7074 .name = "Console Playback Switch",
7075 .index = 0,
7076 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7077 .info = volume_alsa_mute_info,
7078 .get = volume_alsa_mute_get,
7079};
7080
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02007081static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007082{
7083 tpacpi_volume_checkpoint_nvram();
7084}
7085
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007086static void volume_resume(void)
7087{
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007088 if (software_mute_active) {
7089 if (volume_set_software_mute(false) < 0)
7090 pr_warn("Failed to restore software mute\n");
7091 } else {
7092 volume_alsa_notify_change();
7093 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007094}
7095
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007096static void volume_shutdown(void)
7097{
7098 tpacpi_volume_checkpoint_nvram();
7099}
7100
7101static void volume_exit(void)
7102{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007103 if (alsa_card) {
7104 snd_card_free(alsa_card);
7105 alsa_card = NULL;
7106 }
7107
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007108 tpacpi_volume_checkpoint_nvram();
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007109
7110 if (software_mute_active)
7111 volume_exit_software_mute();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007112}
7113
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007114static int __init volume_create_alsa_mixer(void)
7115{
7116 struct snd_card *card;
7117 struct tpacpi_alsa_data *data;
7118 struct snd_kcontrol *ctl_vol;
7119 struct snd_kcontrol *ctl_mute;
7120 int rc;
7121
Takashi Iwai89235e52014-01-29 15:01:27 +01007122 rc = snd_card_new(&tpacpi_pdev->dev,
7123 alsa_index, alsa_id, THIS_MODULE,
7124 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007125 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07007126 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007127 return 1;
7128 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007129
7130 BUG_ON(!card->private_data);
7131 data = card->private_data;
7132 data->card = card;
7133
7134 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7135 sizeof(card->driver));
7136 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7137 sizeof(card->shortname));
7138 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7139 (thinkpad_id.ec_version_str) ?
7140 thinkpad_id.ec_version_str : "(unknown)");
7141 snprintf(card->longname, sizeof(card->longname),
7142 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7143 (thinkpad_id.ec_version_str) ?
7144 thinkpad_id.ec_version_str : "unknown");
7145
7146 if (volume_control_allowed) {
7147 volume_alsa_control_vol.put = volume_alsa_vol_put;
7148 volume_alsa_control_vol.access =
7149 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7150
7151 volume_alsa_control_mute.put = volume_alsa_mute_put;
7152 volume_alsa_control_mute.access =
7153 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7154 }
7155
7156 if (!tp_features.mixer_no_level_control) {
7157 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7158 rc = snd_ctl_add(card, ctl_vol);
7159 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007160 pr_err("Failed to create ALSA volume control: %d\n",
7161 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007162 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007163 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007164 data->ctl_vol_id = &ctl_vol->id;
7165 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007166
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007167 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7168 rc = snd_ctl_add(card, ctl_mute);
7169 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007170 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007171 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007172 }
7173 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007174
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007175 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007176 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007177 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007178 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007179 }
7180
7181 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02007182 return 0;
7183
7184err_exit:
7185 snd_card_free(card);
7186 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007187}
7188
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007189#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7190#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7191
7192static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7193 /* Whitelist volume level on all IBM by default */
7194 { .vendor = PCI_VENDOR_ID_IBM,
7195 .bios = TPACPI_MATCH_ANY,
7196 .ec = TPACPI_MATCH_ANY,
7197 .quirks = TPACPI_VOL_Q_LEVEL },
7198
7199 /* Lenovo models with volume control (needs confirmation) */
7200 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7201 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7202 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7203 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7204 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7205 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7206 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7207
7208 /* Whitelist mute-only on all Lenovo by default */
7209 { .vendor = PCI_VENDOR_ID_LENOVO,
7210 .bios = TPACPI_MATCH_ANY,
7211 .ec = TPACPI_MATCH_ANY,
7212 .quirks = TPACPI_VOL_Q_MUTEONLY }
7213};
7214
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007215static int __init volume_init(struct ibm_init_struct *iibm)
7216{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007217 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007218 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007219
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007220 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7221
7222 mutex_init(&volume_mutex);
7223
7224 /*
7225 * Check for module parameter bogosity, note that we
7226 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7227 * able to detect "unspecified"
7228 */
7229 if (volume_mode > TPACPI_VOL_MODE_MAX)
7230 return -EINVAL;
7231
7232 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Joe Perches0978e012011-04-04 10:06:25 -07007233 pr_err("UCMS step volume mode not implemented, "
7234 "please contact %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007235 return 1;
7236 }
7237
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007238 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7239 return -EINVAL;
7240
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007241 /*
7242 * The ALSA mixer is our primary interface.
7243 * When disabled, don't install the subdriver at all
7244 */
7245 if (!alsa_enable) {
7246 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7247 "ALSA mixer disabled by parameter, "
7248 "not loading volume subdriver...\n");
7249 return 1;
7250 }
7251
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007252 quirks = tpacpi_check_quirks(volume_quirk_table,
7253 ARRAY_SIZE(volume_quirk_table));
7254
7255 switch (volume_capabilities) {
7256 case TPACPI_VOL_CAP_AUTO:
7257 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7258 tp_features.mixer_no_level_control = 1;
7259 else if (quirks & TPACPI_VOL_Q_LEVEL)
7260 tp_features.mixer_no_level_control = 0;
7261 else
7262 return 1; /* no mixer */
7263 break;
7264 case TPACPI_VOL_CAP_VOLMUTE:
7265 tp_features.mixer_no_level_control = 0;
7266 break;
7267 case TPACPI_VOL_CAP_MUTEONLY:
7268 tp_features.mixer_no_level_control = 1;
7269 break;
7270 default:
7271 return 1;
7272 }
7273
7274 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7275 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7276 "using user-supplied volume_capabilities=%d\n",
7277 volume_capabilities);
7278
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007279 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7280 volume_mode == TPACPI_VOL_MODE_MAX) {
7281 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7282
7283 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7284 "driver auto-selected volume_mode=%d\n",
7285 volume_mode);
7286 } else {
7287 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7288 "using user-supplied volume_mode=%d\n",
7289 volume_mode);
7290 }
7291
7292 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007293 "mute is supported, volume control is %s\n",
7294 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007295
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007296 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7297 software_mute_active = true;
7298 } else {
7299 rc = volume_create_alsa_mixer();
7300 if (rc) {
7301 pr_err("Could not create the ALSA mixer interface\n");
7302 return rc;
7303 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007304
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07007305 pr_info("Console audio control enabled, mode: %s\n",
7306 (volume_control_allowed) ?
7307 "override (read/write)" :
7308 "monitor (read only)");
7309 }
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007310
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007311 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7312 "registering volume hotkeys as change notification\n");
7313 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7314 | TP_ACPI_HKEY_VOLUP_MASK
7315 | TP_ACPI_HKEY_VOLDWN_MASK
7316 | TP_ACPI_HKEY_MUTE_MASK);
7317
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007318 return 0;
7319}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007320
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007321static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007322{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007323 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007324
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007325 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007326 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007327 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007328 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007329 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007330 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007331 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007332 status & TP_EC_AUDIO_LVL_MSK);
7333
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007334 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007335 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007336
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007337 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007338 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007339 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007340 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007341 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007342 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007343 "commands:\tlevel <level>"
7344 " (<level> is 0-%d)\n",
7345 TP_EC_VOLUME_MAX);
7346 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007347 }
7348 }
7349
7350 return 0;
7351}
7352
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007353static int volume_write(char *buf)
7354{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007355 u8 s;
7356 u8 new_level, new_mute;
7357 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007358 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007359 int rc;
7360
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007361 /*
7362 * We do allow volume control at driver startup, so that the
7363 * user can set initial state through the volume=... parameter hack.
7364 */
7365 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7366 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7367 tp_warned.volume_ctrl_forbidden = 1;
Joe Perches0978e012011-04-04 10:06:25 -07007368 pr_notice("Console audio control in monitor mode, "
7369 "changes are not allowed\n");
7370 pr_notice("Use the volume_control=1 module parameter "
7371 "to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007372 }
7373 return -EPERM;
7374 }
7375
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007376 rc = volume_get_status(&s);
7377 if (rc < 0)
7378 return rc;
7379
7380 new_level = s & TP_EC_AUDIO_LVL_MSK;
7381 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007382
7383 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007384 if (!tp_features.mixer_no_level_control) {
7385 if (strlencmp(cmd, "up") == 0) {
7386 if (new_mute)
7387 new_mute = 0;
7388 else if (new_level < TP_EC_VOLUME_MAX)
7389 new_level++;
7390 continue;
7391 } else if (strlencmp(cmd, "down") == 0) {
7392 if (new_mute)
7393 new_mute = 0;
7394 else if (new_level > 0)
7395 new_level--;
7396 continue;
7397 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7398 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7399 new_level = l;
7400 continue;
7401 }
7402 }
7403 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007404 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007405 else if (strlencmp(cmd, "unmute") == 0)
7406 new_mute = 0;
7407 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007408 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007409 }
7410
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007411 if (tp_features.mixer_no_level_control) {
7412 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7413 new_mute ? "" : "un");
7414 rc = volume_set_mute(!!new_mute);
7415 } else {
7416 tpacpi_disclose_usertask("procfs volume",
7417 "%smute and set level to %d\n",
7418 new_mute ? "" : "un", new_level);
7419 rc = volume_set_status(new_mute | new_level);
7420 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007421 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007422
7423 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007424}
7425
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007426static struct ibm_struct volume_driver_data = {
7427 .name = "volume",
7428 .read = volume_read,
7429 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007430 .exit = volume_exit,
7431 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007432 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007433 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007434};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007435
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007436#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7437
7438#define alsa_card NULL
7439
7440static void inline volume_alsa_notify_change(void)
7441{
7442}
7443
7444static int __init volume_init(struct ibm_init_struct *iibm)
7445{
Joe Perches0978e012011-04-04 10:06:25 -07007446 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007447
7448 return 1;
7449}
7450
7451static struct ibm_struct volume_driver_data = {
7452 .name = "volume",
7453};
7454
7455#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7456
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007457/*************************************************************************
7458 * Fan subdriver
7459 */
7460
7461/*
7462 * FAN ACCESS MODES
7463 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007464 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007465 * ACPI GFAN method: returns fan level
7466 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007467 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007468 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007469 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007470 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007471 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7472 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007473 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7474 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007475 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007476 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007477 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7478 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007479 *
7480 * Fan speed changes of any sort (including those caused by the
7481 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007482 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007483 * limited.
7484 *
7485 * Reading is not available if GFAN exists.
7486 * Writing is not available if SFAN exists.
7487 *
7488 * Bits
7489 * 7 automatic mode engaged;
7490 * (default operation mode of the ThinkPad)
7491 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007492 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007493 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007494 * the tachometer while the fan controller ramps up
7495 * the speed (which can take up to a few *minutes*).
7496 * Speeds up fan to 100% duty-cycle, which is far above
7497 * the standard RPM levels. It is not impossible that
7498 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007499 * 5-3 unused in some models. Extra bits for fan level
7500 * in others, but still useless as all values above
7501 * 7 map to the same speed as level 7 in these models.
7502 * 2-0 fan level (0..7 usually)
7503 * 0x00 = stop
7504 * 0x07 = max (set when temperatures critical)
7505 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007506 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007507 *
7508 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007509 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007510 * does so, its initial value is meaningless (0x07).
7511 *
7512 * For firmware bugs, refer to:
7513 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7514 *
7515 * ----
7516 *
7517 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7518 * Main fan tachometer reading (in RPM)
7519 *
7520 * This register is present on all ThinkPads with a new-style EC, and
7521 * it is known not to be present on the A21m/e, and T22, as there is
7522 * something else in offset 0x84 according to the ACPI DSDT. Other
7523 * ThinkPads from this same time period (and earlier) probably lack the
7524 * tachometer as well.
7525 *
Nick Andrew877d0312009-01-26 11:06:57 +01007526 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007527 * was never fixed by IBM to report the EC firmware version string
7528 * probably support the tachometer (like the early X models), so
7529 * detecting it is quite hard. We need more data to know for sure.
7530 *
7531 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7532 * might result.
7533 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007534 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7535 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007536 *
7537 * For firmware bugs, refer to:
7538 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7539 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007540 * ----
7541 *
7542 * ThinkPad EC register 0x31 bit 0 (only on select models)
7543 *
7544 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7545 * show the speed of the main fan. When bit 0 of EC register 0x31
7546 * is one, the tachometer registers show the speed of the auxiliary
7547 * fan.
7548 *
7549 * Fan control seems to affect both fans, regardless of the state
7550 * of this bit.
7551 *
7552 * So far, only the firmware for the X60/X61 non-tablet versions
7553 * seem to support this (firmware TP-7M).
7554 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007555 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007556 * ThinkPad X31, X40, X41. Not available in the X60.
7557 *
7558 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7559 * high speed. ACPI DSDT seems to map these three speeds to levels
7560 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7561 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7562 *
7563 * The speeds are stored on handles
7564 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7565 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007566 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007567 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7568 *
7569 * ACPI DSDT switches which set is in use depending on various
7570 * factors.
7571 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007572 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007573 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7574 * but the ACPI tables just mention level 7.
7575 */
7576
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007577enum { /* Fan control constants */
7578 fan_status_offset = 0x2f, /* EC register 0x2f */
7579 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7580 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007581 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7582 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007583
7584 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7585 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7586
7587 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7588};
7589
7590enum fan_status_access_mode {
7591 TPACPI_FAN_NONE = 0, /* No fan status or control */
7592 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7593 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7594};
7595
7596enum fan_control_access_mode {
7597 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7598 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7599 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7600 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7601};
7602
7603enum fan_control_commands {
7604 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7605 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7606 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7607 * and also watchdog cmd */
7608};
7609
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307610static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007611
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007612static enum fan_status_access_mode fan_status_access_mode;
7613static enum fan_control_access_mode fan_control_access_mode;
7614static enum fan_control_commands fan_control_commands;
7615
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007616static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007617static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007618static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007619static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007620
7621static struct mutex fan_mutex;
7622
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007623static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007624static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007625
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007626TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7627TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007628 "\\FSPD", /* 600e/x, 770e, 770x */
7629 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007630TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007631 "JFNS", /* 770x-JL */
7632 ); /* all others */
7633
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007634/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007635 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7636 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7637 * be in auto mode (0x80).
7638 *
7639 * This is corrected by any write to HFSP either by the driver, or
7640 * by the firmware.
7641 *
7642 * We assume 0x07 really means auto mode while this quirk is active,
7643 * as this is far more likely than the ThinkPad being in level 7,
7644 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007645 *
7646 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7647 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007648 */
7649
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007650static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007651{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007652 if (fan_control_initial_status == 0x07) {
Joe Perches0978e012011-04-04 10:06:25 -07007653 pr_notice("fan_init: initial fan status is unknown, "
7654 "assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007655 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007656 }
7657}
7658
7659static void fan_quirk1_handle(u8 *fan_status)
7660{
7661 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7662 if (*fan_status != fan_control_initial_status) {
7663 /* something changed the HFSP regisnter since
7664 * driver init time, so it is not undefined
7665 * anymore */
7666 tp_features.fan_ctrl_status_undef = 0;
7667 } else {
7668 /* Return most likely status. In fact, it
7669 * might be the only possible status */
7670 *fan_status = TP_EC_FAN_AUTO;
7671 }
7672 }
7673}
7674
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007675/* Select main fan on X60/X61, NOOP on others */
7676static bool fan_select_fan1(void)
7677{
7678 if (tp_features.second_fan) {
7679 u8 val;
7680
7681 if (ec_read(fan_select_offset, &val) < 0)
7682 return false;
7683 val &= 0xFEU;
7684 if (ec_write(fan_select_offset, val) < 0)
7685 return false;
7686 }
7687 return true;
7688}
7689
7690/* Select secondary fan on X60/X61 */
7691static bool fan_select_fan2(void)
7692{
7693 u8 val;
7694
7695 if (!tp_features.second_fan)
7696 return false;
7697
7698 if (ec_read(fan_select_offset, &val) < 0)
7699 return false;
7700 val |= 0x01U;
7701 if (ec_write(fan_select_offset, val) < 0)
7702 return false;
7703
7704 return true;
7705}
7706
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007707/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007708 * Call with fan_mutex held
7709 */
7710static void fan_update_desired_level(u8 status)
7711{
7712 if ((status &
7713 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7714 if (status > 7)
7715 fan_control_desired_level = 7;
7716 else
7717 fan_control_desired_level = status;
7718 }
7719}
7720
7721static int fan_get_status(u8 *status)
7722{
7723 u8 s;
7724
7725 /* TODO:
7726 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7727
7728 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07007729 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007730 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07007731 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007732
Dan Carpentereceeb432012-09-01 12:54:07 -07007733 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007734 return -EIO;
7735
7736 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07007737 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007738
7739 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07007740 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007741 case TPACPI_FAN_RD_TPEC:
7742 /* all except 570, 600e/x, 770e, 770x */
7743 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7744 return -EIO;
7745
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007746 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007747 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007748 fan_quirk1_handle(status);
7749 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007750
7751 break;
7752
7753 default:
7754 return -ENXIO;
7755 }
7756
7757 return 0;
7758}
7759
7760static int fan_get_status_safe(u8 *status)
7761{
7762 int rc;
7763 u8 s;
7764
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007765 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007766 return -ERESTARTSYS;
7767 rc = fan_get_status(&s);
7768 if (!rc)
7769 fan_update_desired_level(s);
7770 mutex_unlock(&fan_mutex);
7771
7772 if (status)
7773 *status = s;
7774
7775 return rc;
7776}
7777
7778static int fan_get_speed(unsigned int *speed)
7779{
7780 u8 hi, lo;
7781
7782 switch (fan_status_access_mode) {
7783 case TPACPI_FAN_RD_TPEC:
7784 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007785 if (unlikely(!fan_select_fan1()))
7786 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007787 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7788 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7789 return -EIO;
7790
7791 if (likely(speed))
7792 *speed = (hi << 8) | lo;
7793
7794 break;
7795
7796 default:
7797 return -ENXIO;
7798 }
7799
7800 return 0;
7801}
7802
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007803static int fan2_get_speed(unsigned int *speed)
7804{
7805 u8 hi, lo;
7806 bool rc;
7807
7808 switch (fan_status_access_mode) {
7809 case TPACPI_FAN_RD_TPEC:
7810 /* all except 570, 600e/x, 770e, 770x */
7811 if (unlikely(!fan_select_fan2()))
7812 return -EIO;
7813 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7814 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7815 fan_select_fan1(); /* play it safe */
7816 if (rc)
7817 return -EIO;
7818
7819 if (likely(speed))
7820 *speed = (hi << 8) | lo;
7821
7822 break;
7823
7824 default:
7825 return -ENXIO;
7826 }
7827
7828 return 0;
7829}
7830
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007831static int fan_set_level(int level)
7832{
7833 if (!fan_control_allowed)
7834 return -EPERM;
7835
7836 switch (fan_control_access_mode) {
7837 case TPACPI_FAN_WR_ACPI_SFAN:
7838 if (level >= 0 && level <= 7) {
7839 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7840 return -EIO;
7841 } else
7842 return -EINVAL;
7843 break;
7844
7845 case TPACPI_FAN_WR_ACPI_FANS:
7846 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007847 if (!(level & TP_EC_FAN_AUTO) &&
7848 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007849 ((level < 0) || (level > 7)))
7850 return -EINVAL;
7851
7852 /* safety net should the EC not support AUTO
7853 * or FULLSPEED mode bits and just ignore them */
7854 if (level & TP_EC_FAN_FULLSPEED)
7855 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007856 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007857 level |= 4; /* safety min speed 4 */
7858
7859 if (!acpi_ec_write(fan_status_offset, level))
7860 return -EIO;
7861 else
7862 tp_features.fan_ctrl_status_undef = 0;
7863 break;
7864
7865 default:
7866 return -ENXIO;
7867 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007868
7869 vdbg_printk(TPACPI_DBG_FAN,
7870 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007871 return 0;
7872}
7873
7874static int fan_set_level_safe(int level)
7875{
7876 int rc;
7877
7878 if (!fan_control_allowed)
7879 return -EPERM;
7880
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007881 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007882 return -ERESTARTSYS;
7883
7884 if (level == TPACPI_FAN_LAST_LEVEL)
7885 level = fan_control_desired_level;
7886
7887 rc = fan_set_level(level);
7888 if (!rc)
7889 fan_update_desired_level(level);
7890
7891 mutex_unlock(&fan_mutex);
7892 return rc;
7893}
7894
7895static int fan_set_enable(void)
7896{
7897 u8 s;
7898 int rc;
7899
7900 if (!fan_control_allowed)
7901 return -EPERM;
7902
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007903 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007904 return -ERESTARTSYS;
7905
7906 switch (fan_control_access_mode) {
7907 case TPACPI_FAN_WR_ACPI_FANS:
7908 case TPACPI_FAN_WR_TPEC:
7909 rc = fan_get_status(&s);
7910 if (rc < 0)
7911 break;
7912
7913 /* Don't go out of emergency fan mode */
7914 if (s != 7) {
7915 s &= 0x07;
7916 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7917 }
7918
7919 if (!acpi_ec_write(fan_status_offset, s))
7920 rc = -EIO;
7921 else {
7922 tp_features.fan_ctrl_status_undef = 0;
7923 rc = 0;
7924 }
7925 break;
7926
7927 case TPACPI_FAN_WR_ACPI_SFAN:
7928 rc = fan_get_status(&s);
7929 if (rc < 0)
7930 break;
7931
7932 s &= 0x07;
7933
7934 /* Set fan to at least level 4 */
7935 s |= 4;
7936
7937 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007938 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007939 else
7940 rc = 0;
7941 break;
7942
7943 default:
7944 rc = -ENXIO;
7945 }
7946
7947 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007948
7949 if (!rc)
7950 vdbg_printk(TPACPI_DBG_FAN,
7951 "fan control: set fan control register to 0x%02x\n",
7952 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007953 return rc;
7954}
7955
7956static int fan_set_disable(void)
7957{
7958 int rc;
7959
7960 if (!fan_control_allowed)
7961 return -EPERM;
7962
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007963 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007964 return -ERESTARTSYS;
7965
7966 rc = 0;
7967 switch (fan_control_access_mode) {
7968 case TPACPI_FAN_WR_ACPI_FANS:
7969 case TPACPI_FAN_WR_TPEC:
7970 if (!acpi_ec_write(fan_status_offset, 0x00))
7971 rc = -EIO;
7972 else {
7973 fan_control_desired_level = 0;
7974 tp_features.fan_ctrl_status_undef = 0;
7975 }
7976 break;
7977
7978 case TPACPI_FAN_WR_ACPI_SFAN:
7979 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7980 rc = -EIO;
7981 else
7982 fan_control_desired_level = 0;
7983 break;
7984
7985 default:
7986 rc = -ENXIO;
7987 }
7988
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007989 if (!rc)
7990 vdbg_printk(TPACPI_DBG_FAN,
7991 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007992
7993 mutex_unlock(&fan_mutex);
7994 return rc;
7995}
7996
7997static int fan_set_speed(int speed)
7998{
7999 int rc;
8000
8001 if (!fan_control_allowed)
8002 return -EPERM;
8003
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008004 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008005 return -ERESTARTSYS;
8006
8007 rc = 0;
8008 switch (fan_control_access_mode) {
8009 case TPACPI_FAN_WR_ACPI_FANS:
8010 if (speed >= 0 && speed <= 65535) {
8011 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8012 speed, speed, speed))
8013 rc = -EIO;
8014 } else
8015 rc = -EINVAL;
8016 break;
8017
8018 default:
8019 rc = -ENXIO;
8020 }
8021
8022 mutex_unlock(&fan_mutex);
8023 return rc;
8024}
8025
8026static void fan_watchdog_reset(void)
8027{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008028 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8029 return;
8030
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008031 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07008032 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8033 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8034 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8035 else
8036 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008037}
8038
8039static void fan_watchdog_fire(struct work_struct *ignored)
8040{
8041 int rc;
8042
8043 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8044 return;
8045
Joe Perches0978e012011-04-04 10:06:25 -07008046 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008047 rc = fan_set_enable();
8048 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07008049 pr_err("fan watchdog: error %d while enabling fan, "
8050 "will try again later...\n", -rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008051 /* reschedule for later */
8052 fan_watchdog_reset();
8053 }
8054}
8055
8056/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008057 * SYSFS fan layout: hwmon compatible (device)
8058 *
8059 * pwm*_enable:
8060 * 0: "disengaged" mode
8061 * 1: manual mode
8062 * 2: native EC "auto" mode (recommended, hardware default)
8063 *
8064 * pwm*: set speed in manual mode, ignored otherwise.
8065 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8066 * interpolation.
8067 *
8068 * fan*_input: tachometer reading, RPM
8069 *
8070 *
8071 * SYSFS fan layout: extensions
8072 *
8073 * fan_watchdog (driver):
8074 * fan watchdog interval in seconds, 0 disables (default), max 120
8075 */
8076
8077/* sysfs fan pwm1_enable ----------------------------------------------- */
8078static ssize_t fan_pwm1_enable_show(struct device *dev,
8079 struct device_attribute *attr,
8080 char *buf)
8081{
8082 int res, mode;
8083 u8 status;
8084
8085 res = fan_get_status_safe(&status);
8086 if (res)
8087 return res;
8088
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008089 if (status & TP_EC_FAN_FULLSPEED) {
8090 mode = 0;
8091 } else if (status & TP_EC_FAN_AUTO) {
8092 mode = 2;
8093 } else
8094 mode = 1;
8095
8096 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8097}
8098
8099static ssize_t fan_pwm1_enable_store(struct device *dev,
8100 struct device_attribute *attr,
8101 const char *buf, size_t count)
8102{
8103 unsigned long t;
8104 int res, level;
8105
8106 if (parse_strtoul(buf, 2, &t))
8107 return -EINVAL;
8108
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008109 tpacpi_disclose_usertask("hwmon pwm1_enable",
8110 "set fan mode to %lu\n", t);
8111
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008112 switch (t) {
8113 case 0:
8114 level = TP_EC_FAN_FULLSPEED;
8115 break;
8116 case 1:
8117 level = TPACPI_FAN_LAST_LEVEL;
8118 break;
8119 case 2:
8120 level = TP_EC_FAN_AUTO;
8121 break;
8122 case 3:
8123 /* reserved for software-controlled auto mode */
8124 return -ENOSYS;
8125 default:
8126 return -EINVAL;
8127 }
8128
8129 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008130 if (res == -ENXIO)
8131 return -EINVAL;
8132 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008133 return res;
8134
8135 fan_watchdog_reset();
8136
8137 return count;
8138}
8139
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01008140static DEVICE_ATTR_RW(fan_pwm1_enable);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008141
8142/* sysfs fan pwm1 ------------------------------------------------------ */
8143static ssize_t fan_pwm1_show(struct device *dev,
8144 struct device_attribute *attr,
8145 char *buf)
8146{
8147 int res;
8148 u8 status;
8149
8150 res = fan_get_status_safe(&status);
8151 if (res)
8152 return res;
8153
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008154 if ((status &
8155 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8156 status = fan_control_desired_level;
8157
8158 if (status > 7)
8159 status = 7;
8160
8161 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8162}
8163
8164static ssize_t fan_pwm1_store(struct device *dev,
8165 struct device_attribute *attr,
8166 const char *buf, size_t count)
8167{
8168 unsigned long s;
8169 int rc;
8170 u8 status, newlevel;
8171
8172 if (parse_strtoul(buf, 255, &s))
8173 return -EINVAL;
8174
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008175 tpacpi_disclose_usertask("hwmon pwm1",
8176 "set fan speed to %lu\n", s);
8177
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008178 /* scale down from 0-255 to 0-7 */
8179 newlevel = (s >> 5) & 0x07;
8180
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02008181 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02008182 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008183
8184 rc = fan_get_status(&status);
8185 if (!rc && (status &
8186 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8187 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03008188 if (rc == -ENXIO)
8189 rc = -EINVAL;
8190 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008191 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03008192 fan_watchdog_reset();
8193 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008194 }
8195
8196 mutex_unlock(&fan_mutex);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008197 return (rc) ? rc : count;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008198}
8199
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01008200static DEVICE_ATTR_RW(fan_pwm1);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008201
8202/* sysfs fan fan1_input ------------------------------------------------ */
8203static ssize_t fan_fan1_input_show(struct device *dev,
8204 struct device_attribute *attr,
8205 char *buf)
8206{
8207 int res;
8208 unsigned int speed;
8209
8210 res = fan_get_speed(&speed);
8211 if (res < 0)
8212 return res;
8213
8214 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8215}
8216
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01008217static DEVICE_ATTR_RO(fan_fan1_input);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008218
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008219/* sysfs fan fan2_input ------------------------------------------------ */
8220static ssize_t fan_fan2_input_show(struct device *dev,
8221 struct device_attribute *attr,
8222 char *buf)
8223{
8224 int res;
8225 unsigned int speed;
8226
8227 res = fan2_get_speed(&speed);
8228 if (res < 0)
8229 return res;
8230
8231 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8232}
8233
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01008234static DEVICE_ATTR_RO(fan_fan2_input);
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008235
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008236/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008237static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
8238 char *buf)
8239{
8240 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8241}
8242
8243static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8244 const char *buf, size_t count)
8245{
8246 unsigned long t;
8247
8248 if (parse_strtoul(buf, 120, &t))
8249 return -EINVAL;
8250
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008251 if (!fan_control_allowed)
8252 return -EPERM;
8253
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008254 fan_watchdog_maxinterval = t;
8255 fan_watchdog_reset();
8256
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008257 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8258
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008259 return count;
8260}
8261
8262static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8263 fan_fan_watchdog_show, fan_fan_watchdog_store);
8264
8265/* --------------------------------------------------------------------- */
8266static struct attribute *fan_attributes[] = {
8267 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8268 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008269 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008270 NULL
8271};
8272
8273static const struct attribute_group fan_attr_group = {
8274 .attrs = fan_attributes,
8275};
8276
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008277#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8278#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008279
8280#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8281 { .vendor = PCI_VENDOR_ID_IBM, \
8282 .bios = TPACPI_MATCH_ANY, \
8283 .ec = TPID(__id1, __id2), \
8284 .quirks = __quirks }
8285
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008286#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8287 { .vendor = PCI_VENDOR_ID_LENOVO, \
8288 .bios = TPACPI_MATCH_ANY, \
8289 .ec = TPID(__id1, __id2), \
8290 .quirks = __quirks }
8291
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008292static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8293 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8294 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8295 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8296 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008297 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008298};
8299
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008300#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008301#undef TPACPI_FAN_QI
8302
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008303static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008304{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008305 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008306 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008307
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008308 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8309 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008310
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008311 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008312 fan_status_access_mode = TPACPI_FAN_NONE;
8313 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008314 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008315 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008316 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008317 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008318 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008319
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008320 if (tpacpi_is_ibm()) {
8321 TPACPI_ACPIHANDLE_INIT(fans);
8322 TPACPI_ACPIHANDLE_INIT(gfan);
8323 TPACPI_ACPIHANDLE_INIT(sfan);
8324 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008325
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008326 quirks = tpacpi_check_quirks(fan_quirk_table,
8327 ARRAY_SIZE(fan_quirk_table));
8328
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008329 if (gfan_handle) {
8330 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008331 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008332 } else {
8333 /* all other ThinkPads: note that even old-style
8334 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008335 if (likely(acpi_ec_read(fan_status_offset,
8336 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008337 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008338 if (quirks & TPACPI_FAN_Q1)
8339 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008340 if (quirks & TPACPI_FAN_2FAN) {
8341 tp_features.second_fan = 1;
8342 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8343 "secondary fan support enabled\n");
8344 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008345 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008346 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008347 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008348 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008349 }
8350 }
8351
8352 if (sfan_handle) {
8353 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008354 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008355 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008356 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008357 } else {
8358 if (!gfan_handle) {
8359 /* gfan without sfan means no fan control */
8360 /* all other models implement TP EC 0x2f control */
8361
8362 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008363 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008364 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008365 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008366 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008367 TPACPI_FAN_CMD_SPEED |
8368 TPACPI_FAN_CMD_LEVEL |
8369 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008370 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008371 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008372 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008373 TPACPI_FAN_CMD_LEVEL |
8374 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008375 }
8376 }
8377 }
8378
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008379 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8380 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008381 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8382 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8383 fan_status_access_mode, fan_control_access_mode);
8384
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008385 /* fan control master switch */
8386 if (!fan_control_allowed) {
8387 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8388 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008389 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008390 "fan control features disabled by parameter\n");
8391 }
8392
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008393 /* update fan_control_desired_level */
8394 if (fan_status_access_mode != TPACPI_FAN_NONE)
8395 fan_get_status_safe(NULL);
8396
8397 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8398 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008399 if (tp_features.second_fan) {
8400 /* attach second fan tachometer */
8401 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8402 &dev_attr_fan_fan2_input.attr;
8403 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008404 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008405 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008406 if (rc < 0)
8407 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008408
8409 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8410 &driver_attr_fan_watchdog);
8411 if (rc < 0) {
8412 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8413 &fan_attr_group);
8414 return rc;
8415 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008416 return 0;
8417 } else
8418 return 1;
8419}
8420
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008421static void fan_exit(void)
8422{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008423 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008424 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008425
8426 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008427 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008428 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8429 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008430
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008431 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008432 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008433}
8434
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008435static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008436{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008437 int rc;
8438
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008439 if (!fan_control_allowed)
8440 return;
8441
8442 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008443 fan_control_resume_level = 0;
8444 rc = fan_get_status_safe(&fan_control_resume_level);
8445 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008446 pr_notice("failed to read fan level for later "
8447 "restore during resume: %d\n", rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008448
8449 /* if it is undefined, don't attempt to restore it.
8450 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008451 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008452 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008453}
8454
8455static void fan_resume(void)
8456{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008457 u8 current_level = 7;
8458 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008459 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008460
8461 /* DSDT *always* updates status on resume */
8462 tp_features.fan_ctrl_status_undef = 0;
8463
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008464 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008465 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008466 (fan_get_status_safe(&current_level) < 0))
8467 return;
8468
8469 switch (fan_control_access_mode) {
8470 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008471 /* never decrease fan level */
8472 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008473 break;
8474 case TPACPI_FAN_WR_ACPI_FANS:
8475 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008476 /* never decrease fan level, scale is:
8477 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8478 *
8479 * We expect the firmware to set either 7 or AUTO, but we
8480 * handle FULLSPEED out of paranoia.
8481 *
8482 * So, we can safely only restore FULLSPEED or 7, anything
8483 * else could slow the fan. Restoring AUTO is useless, at
8484 * best that's exactly what the DSDT already set (it is the
8485 * slower it uses).
8486 *
8487 * Always keep in mind that the DSDT *will* have set the
8488 * fans to what the vendor supposes is the best level. We
8489 * muck with it only to speed the fan up.
8490 */
8491 if (fan_control_resume_level != 7 &&
8492 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8493 return;
8494 else
8495 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8496 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008497 break;
8498 default:
8499 return;
8500 }
8501 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008502 pr_notice("restoring fan level to 0x%02x\n",
8503 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008504 rc = fan_set_level_safe(fan_control_resume_level);
8505 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008506 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008507 }
8508}
8509
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008510static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008511{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008512 int rc;
8513 u8 status;
8514 unsigned int speed = 0;
8515
8516 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008517 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008518 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008519 rc = fan_get_status_safe(&status);
8520 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008521 return rc;
8522
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008523 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008524 "level:\t\t%d\n",
8525 (status != 0) ? "enabled" : "disabled", status);
8526 break;
8527
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008528 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008529 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008530 rc = fan_get_status_safe(&status);
8531 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008532 return rc;
8533
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008534 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008535 (status != 0) ? "enabled" : "disabled");
8536
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008537 rc = fan_get_speed(&speed);
8538 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008539 return rc;
8540
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008541 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008542
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008543 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008544 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008545 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008546 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008547 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008548 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008549 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008550 break;
8551
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008552 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008553 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008554 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008555 }
8556
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008557 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008558 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008559
8560 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008561 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008562 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008563 break;
8564
8565 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008566 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008567 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008568 break;
8569 }
8570 }
8571
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008572 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008573 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008574 "commands:\twatchdog <timeout> (<timeout> "
8575 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008576
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008577 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008578 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008579 " (<speed> is 0-65535)\n");
8580
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008581 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008582}
8583
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008584static int fan_write_cmd_level(const char *cmd, int *rc)
8585{
8586 int level;
8587
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008588 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008589 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008590 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008591 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008592 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008593 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008594 return 0;
8595
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008596 *rc = fan_set_level_safe(level);
8597 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008598 pr_err("level command accepted for unsupported access mode %d\n",
8599 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008600 else if (!*rc)
8601 tpacpi_disclose_usertask("procfs fan",
8602 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008603
8604 return 1;
8605}
8606
8607static int fan_write_cmd_enable(const char *cmd, int *rc)
8608{
8609 if (strlencmp(cmd, "enable") != 0)
8610 return 0;
8611
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008612 *rc = fan_set_enable();
8613 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008614 pr_err("enable command accepted for unsupported access mode %d\n",
8615 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008616 else if (!*rc)
8617 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008618
8619 return 1;
8620}
8621
8622static int fan_write_cmd_disable(const char *cmd, int *rc)
8623{
8624 if (strlencmp(cmd, "disable") != 0)
8625 return 0;
8626
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008627 *rc = fan_set_disable();
8628 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008629 pr_err("disable command accepted for unsupported access mode %d\n",
8630 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008631 else if (!*rc)
8632 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008633
8634 return 1;
8635}
8636
8637static int fan_write_cmd_speed(const char *cmd, int *rc)
8638{
8639 int speed;
8640
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008641 /* TODO:
8642 * Support speed <low> <medium> <high> ? */
8643
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008644 if (sscanf(cmd, "speed %d", &speed) != 1)
8645 return 0;
8646
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008647 *rc = fan_set_speed(speed);
8648 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008649 pr_err("speed command accepted for unsupported access mode %d\n",
8650 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008651 else if (!*rc)
8652 tpacpi_disclose_usertask("procfs fan",
8653 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008654
8655 return 1;
8656}
8657
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008658static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8659{
8660 int interval;
8661
8662 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8663 return 0;
8664
8665 if (interval < 0 || interval > 120)
8666 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008667 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008668 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008669 tpacpi_disclose_usertask("procfs fan",
8670 "set watchdog timer to %d\n",
8671 interval);
8672 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008673
8674 return 1;
8675}
8676
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008677static int fan_write(char *buf)
8678{
8679 char *cmd;
8680 int rc = 0;
8681
8682 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008683 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008684 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008685 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008686 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008687 fan_write_cmd_disable(cmd, &rc) ||
8688 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008689 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008690 fan_write_cmd_speed(cmd, &rc))
8691 )
8692 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008693 else if (!rc)
8694 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008695 }
8696
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008697 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008698}
8699
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008700static struct ibm_struct fan_driver_data = {
8701 .name = "fan",
8702 .read = fan_read,
8703 .write = fan_write,
8704 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008705 .suspend = fan_suspend,
8706 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008707};
8708
David Henningsson420f9732013-10-16 23:10:31 +02008709/*************************************************************************
8710 * Mute LED subdriver
8711 */
8712
8713
8714struct tp_led_table {
8715 acpi_string name;
8716 int on_value;
8717 int off_value;
8718 int state;
8719};
8720
8721static struct tp_led_table led_tables[] = {
8722 [TPACPI_LED_MUTE] = {
8723 .name = "SSMS",
8724 .on_value = 1,
8725 .off_value = 0,
8726 },
8727 [TPACPI_LED_MICMUTE] = {
8728 .name = "MMTS",
8729 .on_value = 2,
8730 .off_value = 0,
8731 },
8732};
8733
8734static int mute_led_on_off(struct tp_led_table *t, bool state)
8735{
8736 acpi_handle temp;
8737 int output;
8738
8739 if (!ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp))) {
8740 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
8741 return -EIO;
8742 }
8743
8744 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
8745 state ? t->on_value : t->off_value))
8746 return -EIO;
8747
8748 t->state = state;
8749 return state;
8750}
8751
8752int tpacpi_led_set(int whichled, bool on)
8753{
8754 struct tp_led_table *t;
8755
8756 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
8757 return -EINVAL;
8758
8759 t = &led_tables[whichled];
8760 if (t->state < 0 || t->state == on)
8761 return t->state;
8762 return mute_led_on_off(t, on);
8763}
8764EXPORT_SYMBOL_GPL(tpacpi_led_set);
8765
8766static int mute_led_init(struct ibm_init_struct *iibm)
8767{
8768 acpi_handle temp;
8769 int i;
8770
8771 for (i = 0; i < TPACPI_LED_MAX; i++) {
8772 struct tp_led_table *t = &led_tables[i];
8773 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
8774 mute_led_on_off(t, false);
8775 else
8776 t->state = -ENODEV;
8777 }
8778 return 0;
8779}
8780
8781static void mute_led_exit(void)
8782{
8783 int i;
8784
8785 for (i = 0; i < TPACPI_LED_MAX; i++)
8786 tpacpi_led_set(i, false);
8787}
8788
Takashi Iwai119f4492014-02-12 16:32:45 +01008789static void mute_led_resume(void)
8790{
8791 int i;
8792
8793 for (i = 0; i < TPACPI_LED_MAX; i++) {
8794 struct tp_led_table *t = &led_tables[i];
8795 if (t->state >= 0)
8796 mute_led_on_off(t, t->state);
8797 }
8798}
8799
David Henningsson420f9732013-10-16 23:10:31 +02008800static struct ibm_struct mute_led_driver_data = {
8801 .name = "mute_led",
8802 .exit = mute_led_exit,
Takashi Iwai119f4492014-02-12 16:32:45 +01008803 .resume = mute_led_resume,
David Henningsson420f9732013-10-16 23:10:31 +02008804};
8805
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008806/****************************************************************************
8807 ****************************************************************************
8808 *
8809 * Infrastructure
8810 *
8811 ****************************************************************************
8812 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008813
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008814/*
8815 * HKEY event callout for other subdrivers go here
8816 * (yes, it is ugly, but it is quick, safe, and gets the job done
8817 */
8818static void tpacpi_driver_event(const unsigned int hkey_event)
8819{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008820 if (ibm_backlight_device) {
8821 switch (hkey_event) {
8822 case TP_HKEY_EV_BRGHT_UP:
8823 case TP_HKEY_EV_BRGHT_DOWN:
8824 tpacpi_brightness_notify_change();
8825 }
8826 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008827 if (alsa_card) {
8828 switch (hkey_event) {
8829 case TP_HKEY_EV_VOL_UP:
8830 case TP_HKEY_EV_VOL_DOWN:
8831 case TP_HKEY_EV_VOL_MUTE:
8832 volume_alsa_notify_change();
8833 }
8834 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008835}
8836
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008837static void hotkey_driver_event(const unsigned int scancode)
8838{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008839 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008840}
8841
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008842/* sysfs name ---------------------------------------------------------- */
8843static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8844 struct device_attribute *attr,
8845 char *buf)
8846{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008847 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008848}
8849
Bastien Nocerab4dd04ac2015-03-02 18:40:50 +01008850static DEVICE_ATTR_RO(thinkpad_acpi_pdev_name);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008851
8852/* --------------------------------------------------------------------- */
8853
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008854/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03008855static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008856
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008857/*
8858 * Module and infrastructure proble, init and exit handling
8859 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860
Rusty Russell90ab5ee2012-01-13 09:32:20 +10308861static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008862
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008863#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008864static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008865{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008866 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008867
Jan van den Berg72a979f2014-09-17 00:01:08 +02008868 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008869}
8870#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8871
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008872static void ibm_exit(struct ibm_struct *ibm)
8873{
8874 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8875
8876 list_del_init(&ibm->all_drivers);
8877
8878 if (ibm->flags.acpi_notify_installed) {
8879 dbg_printk(TPACPI_DBG_EXIT,
8880 "%s: acpi_remove_notify_handler\n", ibm->name);
8881 BUG_ON(!ibm->acpi);
8882 acpi_remove_notify_handler(*ibm->acpi->handle,
8883 ibm->acpi->type,
8884 dispatch_acpi_notify);
8885 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008886 }
8887
8888 if (ibm->flags.proc_created) {
8889 dbg_printk(TPACPI_DBG_EXIT,
8890 "%s: remove_proc_entry\n", ibm->name);
8891 remove_proc_entry(ibm->name, proc_dir);
8892 ibm->flags.proc_created = 0;
8893 }
8894
8895 if (ibm->flags.acpi_driver_registered) {
8896 dbg_printk(TPACPI_DBG_EXIT,
8897 "%s: acpi_bus_unregister_driver\n", ibm->name);
8898 BUG_ON(!ibm->acpi);
8899 acpi_bus_unregister_driver(ibm->acpi->driver);
8900 kfree(ibm->acpi->driver);
8901 ibm->acpi->driver = NULL;
8902 ibm->flags.acpi_driver_registered = 0;
8903 }
8904
8905 if (ibm->flags.init_called && ibm->exit) {
8906 ibm->exit();
8907 ibm->flags.init_called = 0;
8908 }
8909
8910 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8911}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008912
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008913static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008914{
8915 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008916 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008917 struct proc_dir_entry *entry;
8918
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008919 BUG_ON(ibm == NULL);
8920
8921 INIT_LIST_HEAD(&ibm->all_drivers);
8922
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008923 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008924 return 0;
8925
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008926 dbg_printk(TPACPI_DBG_INIT,
8927 "probing for %s\n", ibm->name);
8928
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008929 if (iibm->init) {
8930 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008931 if (ret > 0)
8932 return 0; /* probe failed */
8933 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008935
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008936 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008937 }
8938
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008939 if (ibm->acpi) {
8940 if (ibm->acpi->hid) {
8941 ret = register_tpacpi_subdriver(ibm);
8942 if (ret)
8943 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008944 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008945
8946 if (ibm->acpi->notify) {
8947 ret = setup_acpi_notify(ibm);
8948 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07008949 pr_notice("disabling subdriver %s\n",
8950 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008951 ret = 0;
8952 goto err_out;
8953 }
8954 if (ret < 0)
8955 goto err_out;
8956 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008957 }
8958
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008959 dbg_printk(TPACPI_DBG_INIT,
8960 "%s installed\n", ibm->name);
8961
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008962 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04008963 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008964
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008965 if (!mode)
8966 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008967 if (ibm->write)
8968 mode |= S_IWUSR;
8969 entry = proc_create_data(ibm->name, mode, proc_dir,
8970 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008971 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07008972 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008973 ret = -ENODEV;
8974 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008975 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008976 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008978
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008979 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8980
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008982
8983err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008984 dbg_printk(TPACPI_DBG_INIT,
8985 "%s: at error exit path with result %d\n",
8986 ibm->name, ret);
8987
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008988 ibm_exit(ibm);
Jan van den Berg72a979f2014-09-17 00:01:08 +02008989 return (ret < 0) ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008990}
8991
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008992/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008993
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008994static bool __pure __init tpacpi_is_fw_digit(const char c)
8995{
8996 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8997}
8998
Jan van den Berg72a979f2014-09-17 00:01:08 +02008999static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009000 const char t)
9001{
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009002 /*
9003 * Most models: xxyTkkWW (#.##c)
9004 * Ancient 570/600 and -SL lacks (#.##c)
9005 */
9006 if (s && strlen(s) >= 8 &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009007 tpacpi_is_fw_digit(s[0]) &&
9008 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009009 s[2] == t &&
9010 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009011 tpacpi_is_fw_digit(s[4]) &&
Adam Lee1b0eb5b2015-02-11 13:43:10 +08009012 tpacpi_is_fw_digit(s[5]))
9013 return true;
9014
9015 /* New models: xxxyTkkW (#.##c); T550 and some others */
9016 return s && strlen(s) >= 8 &&
9017 tpacpi_is_fw_digit(s[0]) &&
9018 tpacpi_is_fw_digit(s[1]) &&
9019 tpacpi_is_fw_digit(s[2]) &&
9020 s[3] == t &&
9021 (s[4] == 'T' || s[4] == 'N') &&
9022 tpacpi_is_fw_digit(s[5]) &&
9023 tpacpi_is_fw_digit(s[6]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009024}
9025
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009026/* returns 0 - probe ok, or < 0 - probe error.
9027 * Probe ok doesn't mean thinkpad found.
9028 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9029static int __must_check __init get_thinkpad_model_data(
9030 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009031{
Jeff Garzik18552562007-10-03 15:15:40 -04009032 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009033 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009034 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009035
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009036 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009037 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009038
9039 memset(tp, 0, sizeof(*tp));
9040
9041 if (dmi_name_in_vendors("IBM"))
9042 tp->vendor = PCI_VENDOR_ID_IBM;
9043 else if (dmi_name_in_vendors("LENOVO"))
9044 tp->vendor = PCI_VENDOR_ID_LENOVO;
9045 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009046 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009047
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009048 s = dmi_get_system_info(DMI_BIOS_VERSION);
9049 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9050 if (s && !tp->bios_version_str)
9051 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009052
9053 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05009054 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9055 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009056 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009057
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009058 tp->bios_model = tp->bios_version_str[0]
9059 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009060 tp->bios_release = (tp->bios_version_str[4] << 8)
9061 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009062
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009063 /*
9064 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9065 * X32 or newer, all Z series; Some models must have an
9066 * up-to-date BIOS or they will not be detected.
9067 *
9068 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9069 */
9070 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009071 if (sscanf(dev->name,
9072 "IBM ThinkPad Embedded Controller -[%17c",
9073 ec_fw_string) == 1) {
9074 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9075 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009076
9077 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009078 if (!tp->ec_version_str)
9079 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009080
9081 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9082 tp->ec_model = ec_fw_string[0]
9083 | (ec_fw_string[1] << 8);
9084 tp->ec_release = (ec_fw_string[4] << 8)
9085 | ec_fw_string[5];
9086 } else {
Joe Perches0978e012011-04-04 10:06:25 -07009087 pr_notice("ThinkPad firmware release %s "
9088 "doesn't match the known patterns\n",
9089 ec_fw_string);
9090 pr_notice("please report this to %s\n",
9091 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03009092 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009093 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02009094 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02009095 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009096
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009097 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009098 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009099 tp->model_str = kstrdup(s, GFP_KERNEL);
9100 if (!tp->model_str)
9101 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009102 } else {
9103 s = dmi_get_system_info(DMI_BIOS_VENDOR);
Rasmus Villemoes40f5c772014-10-13 15:54:52 -07009104 if (s && !(strncasecmp(s, "Lenovo", 6))) {
Manoj Iyera4f46bb2012-08-06 18:15:37 -05009105 tp->model_str = kstrdup(s, GFP_KERNEL);
9106 if (!tp->model_str)
9107 return -ENOMEM;
9108 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009109 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03009110
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009111 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9112 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9113 if (s && !tp->nummodel_str)
9114 return -ENOMEM;
9115
9116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009117}
9118
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009119static int __init probe_for_thinkpad(void)
9120{
9121 int is_thinkpad;
9122
9123 if (acpi_disabled)
9124 return -ENODEV;
9125
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009126 /* It would be dangerous to run the driver in this case */
9127 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9128 return -ENODEV;
9129
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009130 /*
9131 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009132 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009133 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03009134 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9135 (thinkpad_id.ec_model != 0) ||
9136 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009137
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03009138 /* The EC handler is required */
9139 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009140 if (!ec_handle) {
9141 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07009142 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009143 return -ENODEV;
9144 }
9145
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009146 if (!is_thinkpad && !force_load)
9147 return -ENODEV;
9148
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009149 return 0;
9150}
9151
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009152static void __init thinkpad_acpi_init_banner(void)
9153{
Joe Perches0978e012011-04-04 10:06:25 -07009154 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9155 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009156
Joe Perches0978e012011-04-04 10:06:25 -07009157 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009158 (thinkpad_id.bios_version_str) ?
9159 thinkpad_id.bios_version_str : "unknown",
9160 (thinkpad_id.ec_version_str) ?
9161 thinkpad_id.ec_version_str : "unknown");
9162
9163 BUG_ON(!thinkpad_id.vendor);
9164
9165 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07009166 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009167 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9168 "IBM" : ((thinkpad_id.vendor ==
9169 PCI_VENDOR_ID_LENOVO) ?
9170 "Lenovo" : "Unknown vendor"),
9171 thinkpad_id.model_str,
9172 (thinkpad_id.nummodel_str) ?
9173 thinkpad_id.nummodel_str : "unknown");
9174}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009175
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009176/* Module init, exit, parameters */
9177
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009178static struct ibm_init_struct ibms_init[] __initdata = {
9179 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009180 .data = &thinkpad_acpi_driver_data,
9181 },
9182 {
9183 .init = hotkey_init,
9184 .data = &hotkey_driver_data,
9185 },
9186 {
9187 .init = bluetooth_init,
9188 .data = &bluetooth_driver_data,
9189 },
9190 {
9191 .init = wan_init,
9192 .data = &wan_driver_data,
9193 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009194 {
9195 .init = uwb_init,
9196 .data = &uwb_driver_data,
9197 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009198#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009199 {
9200 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03009201 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009202 .data = &video_driver_data,
9203 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02009204#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009205 {
9206 .init = light_init,
9207 .data = &light_driver_data,
9208 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009209 {
9210 .init = cmos_init,
9211 .data = &cmos_driver_data,
9212 },
9213 {
9214 .init = led_init,
9215 .data = &led_driver_data,
9216 },
9217 {
9218 .init = beep_init,
9219 .data = &beep_driver_data,
9220 },
9221 {
9222 .init = thermal_init,
9223 .data = &thermal_driver_data,
9224 },
9225 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009226 .init = brightness_init,
9227 .data = &brightness_driver_data,
9228 },
9229 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009230 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009231 .data = &volume_driver_data,
9232 },
9233 {
9234 .init = fan_init,
9235 .data = &fan_driver_data,
9236 },
David Henningsson420f9732013-10-16 23:10:31 +02009237 {
9238 .init = mute_led_init,
9239 .data = &mute_led_driver_data,
9240 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009241};
9242
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009243static int __init set_ibm_param(const char *val, struct kernel_param *kp)
9244{
9245 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009246 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009247
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009248 if (!kp || !kp->name || !val)
9249 return -EINVAL;
9250
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009251 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9252 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02009253 WARN_ON(ibm == NULL);
9254
9255 if (!ibm || !ibm->name)
9256 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009257
9258 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9259 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009260 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009261 strcpy(ibms_init[i].param, val);
9262 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009263 return 0;
9264 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009265 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009266
9267 return -EINVAL;
9268}
9269
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009270module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009271MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009272 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009273
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009274module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009275MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03009276
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009277module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009278MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009279 "Attempts to load the driver even on a "
9280 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03009281
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009282module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009283MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009284 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03009285
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009286module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009287MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009288 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00009289 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03009290
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009291module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009292MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009293 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02009294
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009295#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02009296module_param_named(volume_mode, volume_mode, uint, 0444);
9297MODULE_PARM_DESC(volume_mode,
9298 "Selects volume control strategy: "
9299 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9300
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02009301module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9302MODULE_PARM_DESC(volume_capabilities,
9303 "Selects the mixer capabilites: "
9304 "0=auto, 1=volume and mute, 2=mute only");
9305
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02009306module_param_named(volume_control, volume_control_allowed, bool, 0444);
9307MODULE_PARM_DESC(volume_control,
9308 "Enables software override for the console audio "
9309 "control when true");
9310
Andy Lutomirski9a417ec2014-10-17 17:04:29 -07009311module_param_named(software_mute, software_mute_requested, bool, 0444);
9312MODULE_PARM_DESC(software_mute,
9313 "Request full software mute control");
9314
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009315/* ALSA module API parameters */
9316module_param_named(index, alsa_index, int, 0444);
9317MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9318module_param_named(id, alsa_id, charp, 0444);
9319MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9320module_param_named(enable, alsa_enable, bool, 0444);
9321MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02009322#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02009323
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009324#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009325 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02009326 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009327 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009328
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009329TPACPI_PARAM(hotkey);
9330TPACPI_PARAM(bluetooth);
9331TPACPI_PARAM(video);
9332TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009333TPACPI_PARAM(cmos);
9334TPACPI_PARAM(led);
9335TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009336TPACPI_PARAM(brightness);
9337TPACPI_PARAM(volume);
9338TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03009339
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009340#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009341module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009342MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9343module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9344MODULE_PARM_DESC(wlsw_state,
9345 "Initial state of the emulated WLSW switch");
9346
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009347module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009348MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9349module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9350MODULE_PARM_DESC(bluetooth_state,
9351 "Initial state of the emulated bluetooth switch");
9352
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009353module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009354MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9355module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9356MODULE_PARM_DESC(wwan_state,
9357 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009358
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00009359module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02009360MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9361module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9362MODULE_PARM_DESC(uwb_state,
9363 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02009364#endif
9365
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009366static void thinkpad_acpi_module_exit(void)
9367{
9368 struct ibm_struct *ibm, *itmp;
9369
9370 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9371
9372 list_for_each_entry_safe_reverse(ibm, itmp,
9373 &tpacpi_all_drivers,
9374 all_drivers) {
9375 ibm_exit(ibm);
9376 }
9377
9378 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9379
9380 if (tpacpi_inputdev) {
9381 if (tp_features.input_device_registered)
9382 input_unregister_device(tpacpi_inputdev);
9383 else
9384 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +10009385 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009386 }
9387
9388 if (tpacpi_hwmon)
9389 hwmon_device_unregister(tpacpi_hwmon);
9390
9391 if (tp_features.sensors_pdev_attrs_registered)
9392 device_remove_file(&tpacpi_sensors_pdev->dev,
9393 &dev_attr_thinkpad_acpi_pdev_name);
9394 if (tpacpi_sensors_pdev)
9395 platform_device_unregister(tpacpi_sensors_pdev);
9396 if (tpacpi_pdev)
9397 platform_device_unregister(tpacpi_pdev);
9398
9399 if (tp_features.sensors_pdrv_attrs_registered)
9400 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9401 if (tp_features.platform_drv_attrs_registered)
9402 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9403
9404 if (tp_features.sensors_pdrv_registered)
9405 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9406
9407 if (tp_features.platform_drv_registered)
9408 platform_driver_unregister(&tpacpi_pdriver);
9409
9410 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009411 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009412
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009413 if (tpacpi_wq)
9414 destroy_workqueue(tpacpi_wq);
9415
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009416 kfree(thinkpad_id.bios_version_str);
9417 kfree(thinkpad_id.ec_version_str);
9418 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +10009419 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009420}
9421
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009422
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009423static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424{
9425 int ret, i;
9426
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009427 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9428
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009429 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009430
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009431 ret = get_thinkpad_model_data(&thinkpad_id);
9432 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009433 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009434 thinkpad_acpi_module_exit();
9435 return ret;
9436 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009437 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009438 if (ret) {
9439 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009440 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009442
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009443 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009444
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009445 thinkpad_acpi_init_banner();
9446 tpacpi_check_outdated_fw();
9447
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009448 TPACPI_ACPIHANDLE_INIT(ecrd);
9449 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009450
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009451 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9452 if (!tpacpi_wq) {
9453 thinkpad_acpi_module_exit();
9454 return -ENOMEM;
9455 }
9456
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009457 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009458 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07009459 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009460 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009461 return -ENODEV;
9462 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009463
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009464 ret = platform_driver_register(&tpacpi_pdriver);
9465 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009466 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009467 thinkpad_acpi_module_exit();
9468 return ret;
9469 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009470 tp_features.platform_drv_registered = 1;
9471
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009472 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9473 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009474 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009475 thinkpad_acpi_module_exit();
9476 return ret;
9477 }
9478 tp_features.sensors_pdrv_registered = 1;
9479
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009480 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009481 if (!ret) {
9482 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009483 ret = tpacpi_create_driver_attributes(
9484 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009485 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009486 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009487 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009488 thinkpad_acpi_module_exit();
9489 return ret;
9490 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009491 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009492
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009493
9494 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009495 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009496 NULL, 0);
9497 if (IS_ERR(tpacpi_pdev)) {
9498 ret = PTR_ERR(tpacpi_pdev);
9499 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009500 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009501 thinkpad_acpi_module_exit();
9502 return ret;
9503 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009504 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009505 TPACPI_HWMON_DRVR_NAME,
9506 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009507 if (IS_ERR(tpacpi_sensors_pdev)) {
9508 ret = PTR_ERR(tpacpi_sensors_pdev);
9509 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009510 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009511 thinkpad_acpi_module_exit();
9512 return ret;
9513 }
9514 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9515 &dev_attr_thinkpad_acpi_pdev_name);
9516 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009517 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009518 thinkpad_acpi_module_exit();
9519 return ret;
9520 }
9521 tp_features.sensors_pdev_attrs_registered = 1;
9522 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009523 if (IS_ERR(tpacpi_hwmon)) {
9524 ret = PTR_ERR(tpacpi_hwmon);
9525 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009526 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009527 thinkpad_acpi_module_exit();
9528 return ret;
9529 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009530 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009531 tpacpi_inputdev = input_allocate_device();
9532 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009533 thinkpad_acpi_module_exit();
9534 return -ENOMEM;
9535 } else {
9536 /* Prepare input device, but don't register */
9537 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009538 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009539 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009540 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009541 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9542 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009543 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009544 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009545
9546 /* Init subdriver dependencies */
9547 tpacpi_detect_brightness_capabilities();
9548
9549 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009550 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9551 ret = ibm_init(&ibms_init[i]);
9552 if (ret >= 0 && *ibms_init[i].param)
9553 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009554 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009555 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009556 return ret;
9557 }
9558 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009559
9560 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9561
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009562 ret = input_register_device(tpacpi_inputdev);
9563 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009564 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009565 thinkpad_acpi_module_exit();
9566 return ret;
9567 } else {
9568 tp_features.input_device_registered = 1;
9569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009570
9571 return 0;
9572}
9573
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009574MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9575
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009576/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009577 * This will autoload the driver in almost every ThinkPad
9578 * in widespread use.
9579 *
9580 * Only _VERY_ old models, like the 240, 240x and 570 lack
9581 * the HKEY event interface.
9582 */
9583MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9584
9585/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009586 * DMI matching for module autoloading
9587 *
9588 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9589 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9590 *
9591 * Only models listed in thinkwiki will be supported, so add yours
9592 * if it is not there yet.
9593 */
9594#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009595 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009596
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009597/* Ancient thinkpad BIOSes have to be identified by
9598 * BIOS type or model number, and there are far less
9599 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009600IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009601
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009602MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9603MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009604MODULE_DESCRIPTION(TPACPI_DESC);
9605MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009606MODULE_LICENSE("GPL");
9607
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009608module_init(thinkpad_acpi_module_init);
9609module_exit(thinkpad_acpi_module_exit);