blob: 77f6e707a2a9a43d4c08777f1a97e42dd49a11a0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Joe Perches0978e012011-04-04 10:06:25 -070024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
Henrique de Moraes Holschuh5d2eb142009-12-15 21:51:13 -020026#define TPACPI_VERSION "0.24"
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020027#define TPACPI_SYSFS_VERSION 0x020700
Borislav Deianov78f81cc2005-08-17 00:00:00 -040028
29/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020031 * 2007-10-20 changelog trimmed down
32 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030033 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020035 *
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030039 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040040 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020042 *
43 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040044 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
51
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020052#include <linux/kernel.h>
53#include <linux/module.h>
54#include <linux/init.h>
55#include <linux/types.h>
56#include <linux/string.h>
57#include <linux/list.h>
58#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000059#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020060#include <linux/kthread.h>
61#include <linux/freezer.h>
62#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020064
65#include <linux/nvram.h>
66#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020067#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020068#include <linux/sysfs.h>
69#include <linux/backlight.h>
70#include <linux/fb.h>
71#include <linux/platform_device.h>
72#include <linux/hwmon.h>
73#include <linux/hwmon-sysfs.h>
74#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030075#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030076#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020077#include <asm/uaccess.h>
78
79#include <linux/dmi.h>
80#include <linux/jiffies.h>
81#include <linux/workqueue.h>
82
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020083#include <sound/core.h>
84#include <sound/control.h>
85#include <sound/initval.h>
86
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020087#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020088
89#include <linux/pci_ids.h>
90
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020091
92/* ThinkPad CMOS commands */
93#define TP_CMOS_VOLUME_DOWN 0
94#define TP_CMOS_VOLUME_UP 1
95#define TP_CMOS_VOLUME_MUTE 2
96#define TP_CMOS_BRIGHTNESS_UP 4
97#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030098#define TP_CMOS_THINKLIGHT_ON 12
99#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200100
101/* NVRAM Addresses */
102enum tp_nvram_addr {
103 TP_NVRAM_ADDR_HK2 = 0x57,
104 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
105 TP_NVRAM_ADDR_VIDEO = 0x59,
106 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
107 TP_NVRAM_ADDR_MIXER = 0x60,
108};
109
110/* NVRAM bit masks */
111enum {
112 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
113 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
114 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
115 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
116 TP_NVRAM_MASK_THINKLIGHT = 0x10,
117 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
118 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
119 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
120 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
121 TP_NVRAM_MASK_MUTE = 0x40,
122 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
123 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
124 TP_NVRAM_POS_LEVEL_VOLUME = 0,
125};
126
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300127/* Misc NVRAM-related */
128enum {
129 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
130};
131
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200132/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400133#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
134#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300135#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200136
137/* Input IDs */
138#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
139#define TPACPI_HKEY_INPUT_VERSION 0x4101
140
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200141/* ACPI \WGSV commands */
142enum {
143 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
144 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
145 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
146 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
147};
148
149/* TP_ACPI_WGSV_GET_STATE bits */
150enum {
151 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
152 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
153 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
154 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
155 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
156 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
157 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
158 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
159 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
160 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
161};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200162
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300163/* HKEY events */
164enum tpacpi_hkey_event_t {
165 /* Hotkey-related */
166 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
167 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
168 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
169 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
170 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
171 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
172
173 /* Reasons for waking up from S3/S4 */
174 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
175 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
176 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
177 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
178 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
179 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
180
181 /* Auto-sleep after eject request */
182 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
183 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
184
185 /* Misc bay events */
186 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
187
188 /* User-interface events */
189 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
190 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
191 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
192 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
193 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
194 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
195 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
196
197 /* Thermal events */
198 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
199 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
200 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
201 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
202 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
203
204 /* Misc */
205 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
206};
207
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200208/****************************************************************************
209 * Main driver
210 */
211
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200212#define TPACPI_NAME "thinkpad"
213#define TPACPI_DESC "ThinkPad ACPI Extras"
214#define TPACPI_FILE TPACPI_NAME "_acpi"
215#define TPACPI_URL "http://ibm-acpi.sf.net/"
216#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200217
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200218#define TPACPI_PROC_DIR "ibm"
219#define TPACPI_ACPI_EVENT_PREFIX "ibm"
220#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300221#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200222#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200223
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300224#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300225#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300226
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200227#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200228
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000229/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200230#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000231#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200232#define TPACPI_DBG_INIT 0x0001
233#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000234#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000235#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000236#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000237#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200238#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200239
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200240#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
241#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
242#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200243
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200244
245/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200246 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200247 */
248
249struct ibm_struct;
250
251struct tp_acpi_drv_struct {
252 const struct acpi_device_id *hid;
253 struct acpi_driver *driver;
254
255 void (*notify) (struct ibm_struct *, u32);
256 acpi_handle *handle;
257 u32 type;
258 struct acpi_device *device;
259};
260
261struct ibm_struct {
262 char *name;
263
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200264 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200265 int (*write) (char *);
266 void (*exit) (void);
267 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200268 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200269 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200270
271 struct list_head all_drivers;
272
273 struct tp_acpi_drv_struct *acpi;
274
275 struct {
276 u8 acpi_driver_registered:1;
277 u8 acpi_notify_installed:1;
278 u8 proc_created:1;
279 u8 init_called:1;
280 u8 experimental:1;
281 } flags;
282};
283
284struct ibm_init_struct {
285 char param[32];
286
287 int (*init) (struct ibm_init_struct *);
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -0300288 mode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200289 struct ibm_struct *data;
290};
291
292static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200293 u32 bluetooth:1;
294 u32 hotkey:1;
295 u32 hotkey_mask:1;
296 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200297 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200298 u32 light:1;
299 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300300 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300301 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200302 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200303 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200304 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300305 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300306 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200307 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200308 u32 input_device_registered:1;
309 u32 platform_drv_registered:1;
310 u32 platform_drv_attrs_registered:1;
311 u32 sensors_pdrv_registered:1;
312 u32 sensors_pdrv_attrs_registered:1;
313 u32 sensors_pdev_attrs_registered:1;
314 u32 hotkey_poll_active:1;
315} tp_features;
316
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300317static struct {
318 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200319 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300320} tp_warned;
321
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200322struct thinkpad_id_data {
323 unsigned int vendor; /* ThinkPad vendor:
324 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
325
326 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
327 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
328
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300329 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200330 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300331 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
332 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200333
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300334 char *model_str; /* ThinkPad T43 */
335 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200336};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200337static struct thinkpad_id_data thinkpad_id;
338
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300339static enum {
340 TPACPI_LIFE_INIT = 0,
341 TPACPI_LIFE_RUNNING,
342 TPACPI_LIFE_EXITING,
343} tpacpi_lifecycle;
344
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200345static int experimental;
346static u32 dbg_level;
347
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300348static struct workqueue_struct *tpacpi_wq;
349
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000350enum led_status_t {
351 TPACPI_LED_OFF = 0,
352 TPACPI_LED_ON,
353 TPACPI_LED_BLINK,
354};
355
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300356/* Special LED class that can defer work */
357struct tpacpi_led_classdev {
358 struct led_classdev led_classdev;
359 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000360 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300361 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300362};
363
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300364/* brightness level capabilities */
365static unsigned int bright_maxlvl; /* 0 = unknown */
366
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200367#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
368static int dbg_wlswemul;
369static int tpacpi_wlsw_emulstate;
370static int dbg_bluetoothemul;
371static int tpacpi_bluetooth_emulstate;
372static int dbg_wwanemul;
373static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200374static int dbg_uwbemul;
375static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200376#endif
377
378
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000379/*************************************************************************
380 * Debugging helpers
381 */
382
Joe Perches0978e012011-04-04 10:06:25 -0700383#define dbg_printk(a_dbg_level, format, arg...) \
384do { \
385 if (dbg_level & (a_dbg_level)) \
386 printk(KERN_DEBUG pr_fmt("%s: " format), \
387 __func__, ##arg); \
388} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000389
390#ifdef CONFIG_THINKPAD_ACPI_DEBUG
391#define vdbg_printk dbg_printk
392static const char *str_supported(int is_supported);
393#else
Joe Perches0978e012011-04-04 10:06:25 -0700394static inline const char *str_supported(int is_supported) { return ""; }
395#define vdbg_printk(a_dbg_level, format, arg...) \
396 no_printk(format, ##arg)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000397#endif
398
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000399static void tpacpi_log_usertask(const char * const what)
400{
Joe Perches0978e012011-04-04 10:06:25 -0700401 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
402 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000403}
404
Joe Perches0978e012011-04-04 10:06:25 -0700405#define tpacpi_disclose_usertask(what, format, arg...) \
406do { \
407 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
408 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
409 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
410 what, task_tgid_vnr(current), ## arg); \
411 } \
412} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000413
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300414/*
415 * Quirk handling helpers
416 *
417 * ThinkPad IDs and versions seen in the field so far
418 * are two-characters from the set [0-9A-Z], i.e. base 36.
419 *
420 * We use values well outside that range as specials.
421 */
422
423#define TPACPI_MATCH_ANY 0xffffU
424#define TPACPI_MATCH_UNKNOWN 0U
425
426/* TPID('1', 'Y') == 0x5931 */
427#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
428
429#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
430 { .vendor = PCI_VENDOR_ID_IBM, \
431 .bios = TPID(__id1, __id2), \
432 .ec = TPACPI_MATCH_ANY, \
433 .quirks = (__quirk) }
434
435#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
436 { .vendor = PCI_VENDOR_ID_LENOVO, \
437 .bios = TPID(__id1, __id2), \
438 .ec = TPACPI_MATCH_ANY, \
439 .quirks = (__quirk) }
440
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200441#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
442 { .vendor = PCI_VENDOR_ID_LENOVO, \
443 .bios = TPACPI_MATCH_ANY, \
444 .ec = TPID(__id1, __id2), \
445 .quirks = (__quirk) }
446
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300447struct tpacpi_quirk {
448 unsigned int vendor;
449 u16 bios;
450 u16 ec;
451 unsigned long quirks;
452};
453
454/**
455 * tpacpi_check_quirks() - search BIOS/EC version on a list
456 * @qlist: array of &struct tpacpi_quirk
457 * @qlist_size: number of elements in @qlist
458 *
459 * Iterates over a quirks list until one is found that matches the
460 * ThinkPad's vendor, BIOS and EC model.
461 *
462 * Returns 0 if nothing matches, otherwise returns the quirks field of
463 * the matching &struct tpacpi_quirk entry.
464 *
465 * The match criteria is: vendor, ec and bios much match.
466 */
467static unsigned long __init tpacpi_check_quirks(
468 const struct tpacpi_quirk *qlist,
469 unsigned int qlist_size)
470{
471 while (qlist_size) {
472 if ((qlist->vendor == thinkpad_id.vendor ||
473 qlist->vendor == TPACPI_MATCH_ANY) &&
474 (qlist->bios == thinkpad_id.bios_model ||
475 qlist->bios == TPACPI_MATCH_ANY) &&
476 (qlist->ec == thinkpad_id.ec_model ||
477 qlist->ec == TPACPI_MATCH_ANY))
478 return qlist->quirks;
479
480 qlist_size--;
481 qlist++;
482 }
483 return 0;
484}
485
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300486static inline bool __pure __init tpacpi_is_lenovo(void)
487{
488 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
489}
490
491static inline bool __pure __init tpacpi_is_ibm(void)
492{
493 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
494}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300495
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300496/****************************************************************************
497 ****************************************************************************
498 *
499 * ACPI Helpers and device model
500 *
501 ****************************************************************************
502 ****************************************************************************/
503
504/*************************************************************************
505 * ACPI basic handles
506 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300508static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300509static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200511#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 static acpi_handle object##_handle; \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300513 static const acpi_handle *object##_parent __initdata = \
514 &parent##_handle; \
515 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200517TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
518TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200520TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
521 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400522 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
523 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300524 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400525
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200526TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400527 "^HKEY", /* R30, R31 */
528 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300529 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400530
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300531/*************************************************************************
532 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200533 */
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535static int acpi_evalf(acpi_handle handle,
536 void *res, char *method, char *fmt, ...)
537{
538 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400539 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200540 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400541 struct acpi_buffer result, *resultp;
542 union acpi_object out_obj;
543 acpi_status status;
544 va_list ap;
545 char res_type;
546 int success;
547 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700550 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 0;
552 }
553
554 if (*fmt == 'q') {
555 quiet = 1;
556 fmt++;
557 } else
558 quiet = 0;
559
560 res_type = *(fmt++);
561
562 params.count = 0;
563 params.pointer = &in_objs[0];
564
565 va_start(ap, fmt);
566 while (*fmt) {
567 char c = *(fmt++);
568 switch (c) {
569 case 'd': /* int */
570 in_objs[params.count].integer.value = va_arg(ap, int);
571 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
572 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400573 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 default:
Joe Perches0978e012011-04-04 10:06:25 -0700575 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 "with invalid format character '%c'\n", c);
Jesper Juhl21365852010-12-24 19:56:28 +0100577 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return 0;
579 }
580 }
581 va_end(ap);
582
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400583 if (res_type != 'v') {
584 result.length = sizeof(out_obj);
585 result.pointer = &out_obj;
586 resultp = &result;
587 } else
588 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400590 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400593 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300594 success = (status == AE_OK &&
595 out_obj.type == ACPI_TYPE_INTEGER);
596 if (success && res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 *(int *)res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400599 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 success = status == AE_OK;
601 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400602 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 default:
Joe Perches0978e012011-04-04 10:06:25 -0700604 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 "with invalid format character '%c'\n", res_type);
606 return 0;
607 }
608
609 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700610 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300611 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 return success;
614}
615
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200616static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300617{
618 int v;
619
620 if (ecrd_handle) {
621 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
622 return 0;
623 *p = v;
624 } else {
625 if (ec_read(i, p) < 0)
626 return 0;
627 }
628
629 return 1;
630}
631
632static int acpi_ec_write(int i, u8 v)
633{
634 if (ecwr_handle) {
635 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
636 return 0;
637 } else {
638 if (ec_write(i, v) < 0)
639 return 0;
640 }
641
642 return 1;
643}
644
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300645static int issue_thinkpad_cmos_command(int cmos_cmd)
646{
647 if (!cmos_handle)
648 return -ENXIO;
649
650 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
651 return -EIO;
652
653 return 0;
654}
655
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300656/*************************************************************************
657 * ACPI device model
658 */
659
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200660#define TPACPI_ACPIHANDLE_INIT(object) \
661 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300662 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200663
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300664static void __init drv_acpi_handle_init(const char *name,
665 acpi_handle *handle, const acpi_handle parent,
666 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300667{
668 int i;
669 acpi_status status;
670
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300671 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
672 name);
673
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300674 for (i = 0; i < num_paths; i++) {
675 status = acpi_get_handle(parent, paths[i], handle);
676 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300677 dbg_printk(TPACPI_DBG_INIT,
678 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300679 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300680 return;
681 }
682 }
683
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300684 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
685 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300686 *handle = NULL;
687}
688
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300689static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
690 u32 level, void *context, void **return_value)
691{
692 *(acpi_handle *)return_value = handle;
693
694 return AE_CTRL_TERMINATE;
695}
696
697static void __init tpacpi_acpi_handle_locate(const char *name,
698 const char *hid,
699 acpi_handle *handle)
700{
701 acpi_status status;
702 acpi_handle device_found;
703
704 BUG_ON(!name || !hid || !handle);
705 vdbg_printk(TPACPI_DBG_INIT,
706 "trying to locate ACPI handle for %s, using HID %s\n",
707 name, hid);
708
709 memset(&device_found, 0, sizeof(device_found));
710 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
711 (void *)name, &device_found);
712
713 *handle = NULL;
714
715 if (ACPI_SUCCESS(status)) {
716 *handle = device_found;
717 dbg_printk(TPACPI_DBG_INIT,
718 "Found ACPI handle for %s\n", name);
719 } else {
720 vdbg_printk(TPACPI_DBG_INIT,
721 "Could not locate an ACPI handle for %s: %s\n",
722 name, acpi_format_exception(status));
723 }
724}
725
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300726static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300727{
728 struct ibm_struct *ibm = data;
729
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300730 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
731 return;
732
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300733 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300734 return;
735
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300736 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300737}
738
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300739static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300740{
741 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300742 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300743
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300744 BUG_ON(!ibm->acpi);
745
746 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300747 return 0;
748
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300749 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300750 "setting up ACPI notify for %s\n", ibm->name);
751
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300752 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
753 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700754 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300755 return -ENODEV;
756 }
757
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700758 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300759 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200760 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300761 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300762
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300763 status = acpi_install_notify_handler(*ibm->acpi->handle,
764 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300765 if (ACPI_FAILURE(status)) {
766 if (status == AE_ALREADY_EXISTS) {
Joe Perches0978e012011-04-04 10:06:25 -0700767 pr_notice("another device driver is already "
768 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300769 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700770 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300771 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300772 }
773 return -ENODEV;
774 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300775 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300776 return 0;
777}
778
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300779static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300780{
781 return 0;
782}
783
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300784static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300785{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300786 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300787
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300788 dbg_printk(TPACPI_DBG_INIT,
789 "registering %s as an ACPI driver\n", ibm->name);
790
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300791 BUG_ON(!ibm->acpi);
792
793 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
794 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700795 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300796 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300797 }
798
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200799 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300800 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200801
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300802 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300803
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300804 rc = acpi_bus_register_driver(ibm->acpi->driver);
805 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700806 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200807 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300808 kfree(ibm->acpi->driver);
809 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300810 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300811 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300812
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300813 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300814}
815
816
817/****************************************************************************
818 ****************************************************************************
819 *
820 * Procfs Helpers
821 *
822 ****************************************************************************
823 ****************************************************************************/
824
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200825static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300826{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200827 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300828
829 if (!ibm || !ibm->read)
830 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200831 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300832}
833
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200834static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300835{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200836 return single_open(file, dispatch_proc_show, PDE(inode)->data);
837}
838
839static ssize_t dispatch_proc_write(struct file *file,
840 const char __user *userbuf,
841 size_t count, loff_t *pos)
842{
843 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300844 char *kernbuf;
845 int ret;
846
847 if (!ibm || !ibm->write)
848 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300849 if (count > PAGE_SIZE - 2)
850 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300851
852 kernbuf = kmalloc(count + 2, GFP_KERNEL);
853 if (!kernbuf)
854 return -ENOMEM;
855
856 if (copy_from_user(kernbuf, userbuf, count)) {
857 kfree(kernbuf);
858 return -EFAULT;
859 }
860
861 kernbuf[count] = 0;
862 strcat(kernbuf, ",");
863 ret = ibm->write(kernbuf);
864 if (ret == 0)
865 ret = count;
866
867 kfree(kernbuf);
868
869 return ret;
870}
871
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200872static const struct file_operations dispatch_proc_fops = {
873 .owner = THIS_MODULE,
874 .open = dispatch_proc_open,
875 .read = seq_read,
876 .llseek = seq_lseek,
877 .release = single_release,
878 .write = dispatch_proc_write,
879};
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881static char *next_cmd(char **cmds)
882{
883 char *start = *cmds;
884 char *end;
885
886 while ((end = strchr(start, ',')) && end == start)
887 start = end + 1;
888
889 if (!end)
890 return NULL;
891
892 *end = 0;
893 *cmds = end + 1;
894 return start;
895}
896
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300897
898/****************************************************************************
899 ****************************************************************************
900 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300901 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300902 *
903 ****************************************************************************
904 ****************************************************************************/
905
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300906static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300907static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700908static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300909static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300910static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200911static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300912
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200913static int tpacpi_suspend_handler(struct platform_device *pdev,
914 pm_message_t state)
915{
916 struct ibm_struct *ibm, *itmp;
917
918 list_for_each_entry_safe(ibm, itmp,
919 &tpacpi_all_drivers,
920 all_drivers) {
921 if (ibm->suspend)
922 (ibm->suspend)(state);
923 }
924
925 return 0;
926}
927
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300928static int tpacpi_resume_handler(struct platform_device *pdev)
929{
930 struct ibm_struct *ibm, *itmp;
931
932 list_for_each_entry_safe(ibm, itmp,
933 &tpacpi_all_drivers,
934 all_drivers) {
935 if (ibm->resume)
936 (ibm->resume)();
937 }
938
939 return 0;
940}
941
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200942static void tpacpi_shutdown_handler(struct platform_device *pdev)
943{
944 struct ibm_struct *ibm, *itmp;
945
946 list_for_each_entry_safe(ibm, itmp,
947 &tpacpi_all_drivers,
948 all_drivers) {
949 if (ibm->shutdown)
950 (ibm->shutdown)();
951 }
952}
953
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300954static struct platform_driver tpacpi_pdriver = {
955 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200956 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300957 .owner = THIS_MODULE,
958 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200959 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300960 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200961 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300962};
963
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300964static struct platform_driver tpacpi_hwmon_pdriver = {
965 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200966 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300967 .owner = THIS_MODULE,
968 },
969};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300970
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300971/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300972 * sysfs support helpers
973 */
974
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200975struct attribute_set {
976 unsigned int members, max_members;
977 struct attribute_group group;
978};
979
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300980struct attribute_set_obj {
981 struct attribute_set s;
982 struct attribute *a;
983} __attribute__((packed));
984
985static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200986 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300987{
988 struct attribute_set_obj *sobj;
989
990 if (max_members == 0)
991 return NULL;
992
993 /* Allocates space for implicit NULL at the end too */
994 sobj = kzalloc(sizeof(struct attribute_set_obj) +
995 max_members * sizeof(struct attribute *),
996 GFP_KERNEL);
997 if (!sobj)
998 return NULL;
999 sobj->s.max_members = max_members;
1000 sobj->s.group.attrs = &sobj->a;
1001 sobj->s.group.name = name;
1002
1003 return &sobj->s;
1004}
1005
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001006#define destroy_attr_set(_set) \
1007 kfree(_set);
1008
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001009/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001010static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001011{
1012 if (!s || !attr)
1013 return -EINVAL;
1014
1015 if (s->members >= s->max_members)
1016 return -ENOMEM;
1017
1018 s->group.attrs[s->members] = attr;
1019 s->members++;
1020
1021 return 0;
1022}
1023
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001024static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001025 struct attribute **attr,
1026 unsigned int count)
1027{
1028 int i, res;
1029
1030 for (i = 0; i < count; i++) {
1031 res = add_to_attr_set(s, attr[i]);
1032 if (res)
1033 return res;
1034 }
1035
1036 return 0;
1037}
1038
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001039static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001040{
1041 sysfs_remove_group(kobj, &s->group);
1042 destroy_attr_set(s);
1043}
1044
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001045#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1046 sysfs_create_group(_kobj, &_attr_set->group)
1047
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001048static int parse_strtoul(const char *buf,
1049 unsigned long max, unsigned long *value)
1050{
1051 char *endp;
1052
André Goddard Rosae7d28602009-12-14 18:01:06 -08001053 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1054 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001055 if (*endp || *value > max)
1056 return -EINVAL;
1057
1058 return 0;
1059}
1060
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001061static void tpacpi_disable_brightness_delay(void)
1062{
1063 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001064 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001065}
1066
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001067static void printk_deprecated_attribute(const char * const what,
1068 const char * const details)
1069{
1070 tpacpi_log_usertask("deprecated sysfs attribute");
Joe Perches0978e012011-04-04 10:06:25 -07001071 pr_warn("WARNING: sysfs attribute %s is deprecated and "
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001072 "will be removed. %s\n",
1073 what, details);
1074}
1075
Johannes Berg19d337d2009-06-02 13:01:37 +02001076/*************************************************************************
1077 * rfkill and radio control support helpers
1078 */
1079
1080/*
1081 * ThinkPad-ACPI firmware handling model:
1082 *
1083 * WLSW (master wireless switch) is event-driven, and is common to all
1084 * firmware-controlled radios. It cannot be controlled, just monitored,
1085 * as expected. It overrides all radio state in firmware
1086 *
1087 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1088 * (TODO: verify how WLSW interacts with the returned radio state).
1089 *
1090 * The only time there are shadow radio state changes, is when
1091 * masked-off hotkeys are used.
1092 */
1093
1094/*
1095 * Internal driver API for radio state:
1096 *
1097 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1098 * bool: true means radio blocked (off)
1099 */
1100enum tpacpi_rfkill_state {
1101 TPACPI_RFK_RADIO_OFF = 0,
1102 TPACPI_RFK_RADIO_ON
1103};
1104
1105/* rfkill switches */
1106enum tpacpi_rfk_id {
1107 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1108 TPACPI_RFK_WWAN_SW_ID,
1109 TPACPI_RFK_UWB_SW_ID,
1110 TPACPI_RFK_SW_MAX
1111};
1112
1113static const char *tpacpi_rfkill_names[] = {
1114 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1115 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1116 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1117 [TPACPI_RFK_SW_MAX] = NULL
1118};
1119
1120/* ThinkPad-ACPI rfkill subdriver */
1121struct tpacpi_rfk {
1122 struct rfkill *rfkill;
1123 enum tpacpi_rfk_id id;
1124 const struct tpacpi_rfk_ops *ops;
1125};
1126
1127struct tpacpi_rfk_ops {
1128 /* firmware interface */
1129 int (*get_status)(void);
1130 int (*set_status)(const enum tpacpi_rfkill_state);
1131};
1132
1133static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1134
1135/* Query FW and update rfkill sw state for a given rfkill switch */
1136static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1137{
1138 int status;
1139
1140 if (!tp_rfk)
1141 return -ENODEV;
1142
1143 status = (tp_rfk->ops->get_status)();
1144 if (status < 0)
1145 return status;
1146
1147 rfkill_set_sw_state(tp_rfk->rfkill,
1148 (status == TPACPI_RFK_RADIO_OFF));
1149
1150 return status;
1151}
1152
1153/* Query FW and update rfkill sw state for all rfkill switches */
1154static void tpacpi_rfk_update_swstate_all(void)
1155{
1156 unsigned int i;
1157
1158 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1159 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1160}
1161
1162/*
1163 * Sync the HW-blocking state of all rfkill switches,
1164 * do notice it causes the rfkill core to schedule uevents
1165 */
1166static void tpacpi_rfk_update_hwblock_state(bool blocked)
1167{
1168 unsigned int i;
1169 struct tpacpi_rfk *tp_rfk;
1170
1171 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1172 tp_rfk = tpacpi_rfkill_switches[i];
1173 if (tp_rfk) {
1174 if (rfkill_set_hw_state(tp_rfk->rfkill,
1175 blocked)) {
1176 /* ignore -- we track sw block */
1177 }
1178 }
1179 }
1180}
1181
1182/* Call to get the WLSW state from the firmware */
1183static int hotkey_get_wlsw(void);
1184
1185/* Call to query WLSW state and update all rfkill switches */
1186static bool tpacpi_rfk_check_hwblock_state(void)
1187{
1188 int res = hotkey_get_wlsw();
1189 int hw_blocked;
1190
1191 /* When unknown or unsupported, we have to assume it is unblocked */
1192 if (res < 0)
1193 return false;
1194
1195 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1196 tpacpi_rfk_update_hwblock_state(hw_blocked);
1197
1198 return hw_blocked;
1199}
1200
1201static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1202{
1203 struct tpacpi_rfk *tp_rfk = data;
1204 int res;
1205
1206 dbg_printk(TPACPI_DBG_RFKILL,
1207 "request to change radio state to %s\n",
1208 blocked ? "blocked" : "unblocked");
1209
1210 /* try to set radio state */
1211 res = (tp_rfk->ops->set_status)(blocked ?
1212 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1213
1214 /* and update the rfkill core with whatever the FW really did */
1215 tpacpi_rfk_update_swstate(tp_rfk);
1216
1217 return (res < 0) ? res : 0;
1218}
1219
1220static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1221 .set_block = tpacpi_rfk_hook_set_block,
1222};
1223
1224static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1225 const struct tpacpi_rfk_ops *tp_rfkops,
1226 const enum rfkill_type rfktype,
1227 const char *name,
1228 const bool set_default)
1229{
1230 struct tpacpi_rfk *atp_rfk;
1231 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001232 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001233 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001234 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001235
1236 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1237
Johannes Berg19d337d2009-06-02 13:01:37 +02001238 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1239 if (atp_rfk)
1240 atp_rfk->rfkill = rfkill_alloc(name,
1241 &tpacpi_pdev->dev,
1242 rfktype,
1243 &tpacpi_rfk_rfkill_ops,
1244 atp_rfk);
1245 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001246 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001247 kfree(atp_rfk);
1248 return -ENOMEM;
1249 }
1250
1251 atp_rfk->id = id;
1252 atp_rfk->ops = tp_rfkops;
1253
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001254 sw_status = (tp_rfkops->get_status)();
1255 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001256 pr_err("failed to read initial state for %s, error %d\n",
1257 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001258 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001259 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001260 if (set_default) {
1261 /* try to keep the initial state, since we ask the
1262 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001263 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001264 }
1265 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001266 hw_state = tpacpi_rfk_check_hwblock_state();
1267 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001268
1269 res = rfkill_register(atp_rfk->rfkill);
1270 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001271 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001272 rfkill_destroy(atp_rfk->rfkill);
1273 kfree(atp_rfk);
1274 return res;
1275 }
1276
1277 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001278
Joe Perches0978e012011-04-04 10:06:25 -07001279 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001280 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001281 return 0;
1282}
1283
1284static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1285{
1286 struct tpacpi_rfk *tp_rfk;
1287
1288 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1289
1290 tp_rfk = tpacpi_rfkill_switches[id];
1291 if (tp_rfk) {
1292 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001293 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001294 tpacpi_rfkill_switches[id] = NULL;
1295 kfree(tp_rfk);
1296 }
1297}
1298
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001299static void printk_deprecated_rfkill_attribute(const char * const what)
1300{
1301 printk_deprecated_attribute(what,
1302 "Please switch to generic rfkill before year 2010");
1303}
1304
Johannes Berg19d337d2009-06-02 13:01:37 +02001305/* sysfs <radio> enable ------------------------------------------------ */
1306static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1307 struct device_attribute *attr,
1308 char *buf)
1309{
1310 int status;
1311
1312 printk_deprecated_rfkill_attribute(attr->attr.name);
1313
1314 /* This is in the ABI... */
1315 if (tpacpi_rfk_check_hwblock_state()) {
1316 status = TPACPI_RFK_RADIO_OFF;
1317 } else {
1318 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1319 if (status < 0)
1320 return status;
1321 }
1322
1323 return snprintf(buf, PAGE_SIZE, "%d\n",
1324 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1325}
1326
1327static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1328 struct device_attribute *attr,
1329 const char *buf, size_t count)
1330{
1331 unsigned long t;
1332 int res;
1333
1334 printk_deprecated_rfkill_attribute(attr->attr.name);
1335
1336 if (parse_strtoul(buf, 1, &t))
1337 return -EINVAL;
1338
1339 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1340
1341 /* This is in the ABI... */
1342 if (tpacpi_rfk_check_hwblock_state() && !!t)
1343 return -EPERM;
1344
1345 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1346 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1347 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1348
1349 return (res < 0) ? res : count;
1350}
1351
1352/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001353static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001354{
Johannes Berg19d337d2009-06-02 13:01:37 +02001355 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001356 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001357 else {
1358 int status;
1359
1360 /* This is in the ABI... */
1361 if (tpacpi_rfk_check_hwblock_state()) {
1362 status = TPACPI_RFK_RADIO_OFF;
1363 } else {
1364 status = tpacpi_rfk_update_swstate(
1365 tpacpi_rfkill_switches[id]);
1366 if (status < 0)
1367 return status;
1368 }
1369
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001370 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001371 (status == TPACPI_RFK_RADIO_ON) ?
1372 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001373 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001374 }
1375
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001376 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001377}
1378
1379static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1380{
1381 char *cmd;
1382 int status = -1;
1383 int res = 0;
1384
1385 if (id >= TPACPI_RFK_SW_MAX)
1386 return -ENODEV;
1387
1388 while ((cmd = next_cmd(&buf))) {
1389 if (strlencmp(cmd, "enable") == 0)
1390 status = TPACPI_RFK_RADIO_ON;
1391 else if (strlencmp(cmd, "disable") == 0)
1392 status = TPACPI_RFK_RADIO_OFF;
1393 else
1394 return -EINVAL;
1395 }
1396
1397 if (status != -1) {
1398 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1399 (status == TPACPI_RFK_RADIO_ON) ?
1400 "enable" : "disable",
1401 tpacpi_rfkill_names[id]);
1402 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1403 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1404 }
1405
1406 return res;
1407}
1408
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001409/*************************************************************************
1410 * thinkpad-acpi driver attributes
1411 */
1412
1413/* interface_version --------------------------------------------------- */
1414static ssize_t tpacpi_driver_interface_version_show(
1415 struct device_driver *drv,
1416 char *buf)
1417{
1418 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1419}
1420
1421static DRIVER_ATTR(interface_version, S_IRUGO,
1422 tpacpi_driver_interface_version_show, NULL);
1423
1424/* debug_level --------------------------------------------------------- */
1425static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1426 char *buf)
1427{
1428 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1429}
1430
1431static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1432 const char *buf, size_t count)
1433{
1434 unsigned long t;
1435
1436 if (parse_strtoul(buf, 0xffff, &t))
1437 return -EINVAL;
1438
1439 dbg_level = t;
1440
1441 return count;
1442}
1443
1444static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1445 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1446
1447/* version ------------------------------------------------------------- */
1448static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1449 char *buf)
1450{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001451 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1452 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001453}
1454
1455static DRIVER_ATTR(version, S_IRUGO,
1456 tpacpi_driver_version_show, NULL);
1457
1458/* --------------------------------------------------------------------- */
1459
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001460#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1461
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001462/* wlsw_emulstate ------------------------------------------------------ */
1463static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1464 char *buf)
1465{
1466 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1467}
1468
1469static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1470 const char *buf, size_t count)
1471{
1472 unsigned long t;
1473
1474 if (parse_strtoul(buf, 1, &t))
1475 return -EINVAL;
1476
Johannes Berg19d337d2009-06-02 13:01:37 +02001477 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001478 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001479 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1480 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001481
1482 return count;
1483}
1484
1485static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1486 tpacpi_driver_wlsw_emulstate_show,
1487 tpacpi_driver_wlsw_emulstate_store);
1488
1489/* bluetooth_emulstate ------------------------------------------------- */
1490static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1491 struct device_driver *drv,
1492 char *buf)
1493{
1494 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1495}
1496
1497static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1498 struct device_driver *drv,
1499 const char *buf, size_t count)
1500{
1501 unsigned long t;
1502
1503 if (parse_strtoul(buf, 1, &t))
1504 return -EINVAL;
1505
1506 tpacpi_bluetooth_emulstate = !!t;
1507
1508 return count;
1509}
1510
1511static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1512 tpacpi_driver_bluetooth_emulstate_show,
1513 tpacpi_driver_bluetooth_emulstate_store);
1514
1515/* wwan_emulstate ------------------------------------------------- */
1516static ssize_t tpacpi_driver_wwan_emulstate_show(
1517 struct device_driver *drv,
1518 char *buf)
1519{
1520 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1521}
1522
1523static ssize_t tpacpi_driver_wwan_emulstate_store(
1524 struct device_driver *drv,
1525 const char *buf, size_t count)
1526{
1527 unsigned long t;
1528
1529 if (parse_strtoul(buf, 1, &t))
1530 return -EINVAL;
1531
1532 tpacpi_wwan_emulstate = !!t;
1533
1534 return count;
1535}
1536
1537static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1538 tpacpi_driver_wwan_emulstate_show,
1539 tpacpi_driver_wwan_emulstate_store);
1540
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001541/* uwb_emulstate ------------------------------------------------- */
1542static ssize_t tpacpi_driver_uwb_emulstate_show(
1543 struct device_driver *drv,
1544 char *buf)
1545{
1546 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1547}
1548
1549static ssize_t tpacpi_driver_uwb_emulstate_store(
1550 struct device_driver *drv,
1551 const char *buf, size_t count)
1552{
1553 unsigned long t;
1554
1555 if (parse_strtoul(buf, 1, &t))
1556 return -EINVAL;
1557
1558 tpacpi_uwb_emulstate = !!t;
1559
1560 return count;
1561}
1562
1563static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1564 tpacpi_driver_uwb_emulstate_show,
1565 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001566#endif
1567
1568/* --------------------------------------------------------------------- */
1569
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001570static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001571 &driver_attr_debug_level, &driver_attr_version,
1572 &driver_attr_interface_version,
1573};
1574
1575static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1576{
1577 int i, res;
1578
1579 i = 0;
1580 res = 0;
1581 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1582 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1583 i++;
1584 }
1585
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001586#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1587 if (!res && dbg_wlswemul)
1588 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1589 if (!res && dbg_bluetoothemul)
1590 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1591 if (!res && dbg_wwanemul)
1592 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001593 if (!res && dbg_uwbemul)
1594 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001595#endif
1596
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001597 return res;
1598}
1599
1600static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1601{
1602 int i;
1603
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001604 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001605 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001606
1607#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1608 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1609 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1610 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001611 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001612#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001613}
1614
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001615/*************************************************************************
1616 * Firmware Data
1617 */
1618
1619/*
1620 * Table of recommended minimum BIOS versions
1621 *
1622 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001623 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001624 * bugs and bad ACPI behaviour on older versions
1625 *
1626 * 2. BIOS or EC fw with known bugs that trigger on Linux
1627 *
1628 * 3. BIOS with known reduced functionality in older versions
1629 *
1630 * We recommend the latest BIOS and EC version.
1631 * We only support the latest BIOS and EC fw version as a rule.
1632 *
1633 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1634 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001635 *
1636 * WARNING: we use this table also to detect that the machine is
1637 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001638 */
1639
1640#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1641 { .vendor = (__v), \
1642 .bios = TPID(__id1, __id2), \
1643 .ec = TPACPI_MATCH_ANY, \
1644 .quirks = TPACPI_MATCH_ANY << 16 \
1645 | (__bv1) << 8 | (__bv2) }
1646
1647#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001648 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001649 { .vendor = (__v), \
1650 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001651 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001652 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1653 | (__bv1) << 8 | (__bv2) }
1654
1655#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1656 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1657
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001658/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001659#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1660 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001661 __bv1, __bv2, TPID(__id1, __id2), \
1662 __ev1, __ev2), \
1663 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1664 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1665 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001666
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001667/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001668#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1669 __eid1, __eid2, __ev1, __ev2) \
1670 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001671 __bv1, __bv2, TPID(__eid1, __eid2), \
1672 __ev1, __ev2), \
1673 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1674 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1675 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001676
1677#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1678 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1679
1680#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1681 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001682 __bv1, __bv2, TPID(__id1, __id2), \
1683 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001684
1685#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1686 __eid1, __eid2, __ev1, __ev2) \
1687 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001688 __bv1, __bv2, TPID(__eid1, __eid2), \
1689 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001690
1691static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1692 /* Numeric models ------------------ */
1693 /* FW MODEL BIOS VERS */
1694 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1695 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1696 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1697 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1698 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1699 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1700 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1701 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1702 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1703
1704 /* A-series ------------------------- */
1705 /* FW MODEL BIOS VERS EC VERS */
1706 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1707 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1708 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1709 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1710 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1711 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1712 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1713 TPV_QI0('1', '3', '2', '0'), /* A22m */
1714 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1715 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1716 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1717
1718 /* G-series ------------------------- */
1719 /* FW MODEL BIOS VERS */
1720 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1721 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1722
1723 /* R-series, T-series --------------- */
1724 /* FW MODEL BIOS VERS EC VERS */
1725 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1726 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1727 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1728 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1729 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1730 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1731 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1732 T40/p, T41/p, T42/p (1) */
1733 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1734 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1735 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1736 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1737
1738 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1739 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1740 TPV_QI0('1', '6', '3', '2'), /* T22 */
1741 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1742 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1743 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1744
1745 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1746 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001747 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001748
1749 /* BIOS FW BIOS VERS EC FW EC VERS */
1750 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1751 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1752
1753 /* X-series ------------------------- */
1754 /* FW MODEL BIOS VERS EC VERS */
1755 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1756 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1757 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1758 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1759 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1760 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1761 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1762
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001763 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1764 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001765
1766 /* (0) - older versions lack DMI EC fw string and functionality */
1767 /* (1) - older versions known to lack functionality */
1768};
1769
1770#undef TPV_QL1
1771#undef TPV_QL0
1772#undef TPV_QI2
1773#undef TPV_QI1
1774#undef TPV_QI0
1775#undef TPV_Q_X
1776#undef TPV_Q
1777
1778static void __init tpacpi_check_outdated_fw(void)
1779{
1780 unsigned long fwvers;
1781 u16 ec_version, bios_version;
1782
1783 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1784 ARRAY_SIZE(tpacpi_bios_version_qtable));
1785
1786 if (!fwvers)
1787 return;
1788
1789 bios_version = fwvers & 0xffffU;
1790 ec_version = (fwvers >> 16) & 0xffffU;
1791
1792 /* note that unknown versions are set to 0x0000 and we use that */
1793 if ((bios_version > thinkpad_id.bios_release) ||
1794 (ec_version > thinkpad_id.ec_release &&
1795 ec_version != TPACPI_MATCH_ANY)) {
1796 /*
1797 * The changelogs would let us track down the exact
1798 * reason, but it is just too much of a pain to track
1799 * it. We only list BIOSes that are either really
1800 * broken, or really stable to begin with, so it is
1801 * best if the user upgrades the firmware anyway.
1802 */
Joe Perches0978e012011-04-04 10:06:25 -07001803 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1804 pr_warn("WARNING: This firmware may be missing critical bug "
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001805 "fixes and/or important features\n");
1806 }
1807}
1808
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001809static bool __init tpacpi_is_fw_known(void)
1810{
1811 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1812 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1813}
1814
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001815/****************************************************************************
1816 ****************************************************************************
1817 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001818 * Subdrivers
1819 *
1820 ****************************************************************************
1821 ****************************************************************************/
1822
1823/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001824 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001825 */
1826
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001827static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001829 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1830 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001834static struct ibm_struct thinkpad_acpi_driver_data = {
1835 .name = "driver",
1836 .read = thinkpad_acpi_driver_read,
1837};
1838
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001839/*************************************************************************
1840 * Hotkey subdriver
1841 */
1842
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001843/*
1844 * ThinkPad firmware event model
1845 *
1846 * The ThinkPad firmware has two main event interfaces: normal ACPI
1847 * notifications (which follow the ACPI standard), and a private event
1848 * interface.
1849 *
1850 * The private event interface also issues events for the hotkeys. As
1851 * the driver gained features, the event handling code ended up being
1852 * built around the hotkey subdriver. This will need to be refactored
1853 * to a more formal event API eventually.
1854 *
1855 * Some "hotkeys" are actually supposed to be used as event reports,
1856 * such as "brightness has changed", "volume has changed", depending on
1857 * the ThinkPad model and how the firmware is operating.
1858 *
1859 * Unlike other classes, hotkey-class events have mask/unmask control on
1860 * non-ancient firmware. However, how it behaves changes a lot with the
1861 * firmware model and version.
1862 */
1863
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001864enum { /* hot key scan codes (derived from ACPI DSDT) */
1865 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1866 TP_ACPI_HOTKEYSCAN_FNF2,
1867 TP_ACPI_HOTKEYSCAN_FNF3,
1868 TP_ACPI_HOTKEYSCAN_FNF4,
1869 TP_ACPI_HOTKEYSCAN_FNF5,
1870 TP_ACPI_HOTKEYSCAN_FNF6,
1871 TP_ACPI_HOTKEYSCAN_FNF7,
1872 TP_ACPI_HOTKEYSCAN_FNF8,
1873 TP_ACPI_HOTKEYSCAN_FNF9,
1874 TP_ACPI_HOTKEYSCAN_FNF10,
1875 TP_ACPI_HOTKEYSCAN_FNF11,
1876 TP_ACPI_HOTKEYSCAN_FNF12,
1877 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1878 TP_ACPI_HOTKEYSCAN_FNINSERT,
1879 TP_ACPI_HOTKEYSCAN_FNDELETE,
1880 TP_ACPI_HOTKEYSCAN_FNHOME,
1881 TP_ACPI_HOTKEYSCAN_FNEND,
1882 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1883 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1884 TP_ACPI_HOTKEYSCAN_FNSPACE,
1885 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1886 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1887 TP_ACPI_HOTKEYSCAN_MUTE,
1888 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001889 TP_ACPI_HOTKEYSCAN_UNK1,
1890 TP_ACPI_HOTKEYSCAN_UNK2,
1891 TP_ACPI_HOTKEYSCAN_UNK3,
1892 TP_ACPI_HOTKEYSCAN_UNK4,
1893 TP_ACPI_HOTKEYSCAN_UNK5,
1894 TP_ACPI_HOTKEYSCAN_UNK6,
1895 TP_ACPI_HOTKEYSCAN_UNK7,
1896 TP_ACPI_HOTKEYSCAN_UNK8,
1897
1898 /* Hotkey keymap size */
1899 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001900};
1901
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001902enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001903 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1904 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1905};
1906
1907enum { /* Positions of some of the keys in hotkey masks */
1908 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1909 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1910 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1911 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1912 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1913 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1914 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1915 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1916 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1917 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1918 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1919};
1920
1921enum { /* NVRAM to ACPI HKEY group map */
1922 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1923 TP_ACPI_HKEY_ZOOM_MASK |
1924 TP_ACPI_HKEY_DISPSWTCH_MASK |
1925 TP_ACPI_HKEY_HIBERNATE_MASK,
1926 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1927 TP_ACPI_HKEY_BRGHTDWN_MASK,
1928 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1929 TP_ACPI_HKEY_VOLDWN_MASK |
1930 TP_ACPI_HKEY_MUTE_MASK,
1931};
1932
1933#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1934struct tp_nvram_state {
1935 u16 thinkpad_toggle:1;
1936 u16 zoom_toggle:1;
1937 u16 display_toggle:1;
1938 u16 thinklight_toggle:1;
1939 u16 hibernate_toggle:1;
1940 u16 displayexp_toggle:1;
1941 u16 display_state:1;
1942 u16 brightness_toggle:1;
1943 u16 volume_toggle:1;
1944 u16 mute:1;
1945
1946 u8 brightness_level;
1947 u8 volume_level;
1948};
1949
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001950/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001951static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001952
1953/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001954static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001955
1956/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001957 * Acquire mutex to write poller control variables as an
1958 * atomic block.
1959 *
1960 * Increment hotkey_config_change when changing them if you
1961 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001962 *
1963 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1964 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001965static struct mutex hotkey_thread_data_mutex;
1966static unsigned int hotkey_config_change;
1967
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001968/*
1969 * hotkey poller control variables
1970 *
1971 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001972 *
1973 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1974 * should be used only when the changes need to be taken as
1975 * a block, OR when one needs to force the kthread to forget
1976 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001977 */
1978static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1979static unsigned int hotkey_poll_freq = 10; /* Hz */
1980
1981#define HOTKEY_CONFIG_CRITICAL_START \
1982 do { \
1983 mutex_lock(&hotkey_thread_data_mutex); \
1984 hotkey_config_change++; \
1985 } while (0);
1986#define HOTKEY_CONFIG_CRITICAL_END \
1987 mutex_unlock(&hotkey_thread_data_mutex);
1988
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001989#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1990
1991#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001992#define HOTKEY_CONFIG_CRITICAL_START
1993#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001994
1995#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1996
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001997static struct mutex hotkey_mutex;
1998
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001999static enum { /* Reasons for waking up */
2000 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2001 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2002 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2003} hotkey_wakeup_reason;
2004
2005static int hotkey_autosleep_ack;
2006
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002007static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2008static u32 hotkey_all_mask; /* all events supported in fw */
2009static u32 hotkey_reserved_mask; /* events better left disabled */
2010static u32 hotkey_driver_mask; /* events needed by the driver */
2011static u32 hotkey_user_mask; /* events visible to userspace */
2012static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002013
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02002014static unsigned int hotkey_report_mode;
2015
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002016static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002017
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002018static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002019
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002020static void tpacpi_driver_event(const unsigned int hkey_event);
2021static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002022static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002023
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002024/* HKEY.MHKG() return bits */
2025#define TP_HOTKEY_TABLET_MASK (1 << 3)
2026
Johannes Berg19d337d2009-06-02 13:01:37 +02002027static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002028{
Johannes Berg19d337d2009-06-02 13:01:37 +02002029 int status;
2030
2031 if (!tp_features.hotkey_wlsw)
2032 return -ENODEV;
2033
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002034#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002035 if (dbg_wlswemul)
2036 return (tpacpi_wlsw_emulstate) ?
2037 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002038#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002039
2040 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002041 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002042
2043 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002044}
2045
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002046static int hotkey_get_tablet_mode(int *status)
2047{
2048 int s;
2049
2050 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2051 return -EIO;
2052
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002053 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2054 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002055}
2056
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002057/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002058 * Reads current event mask from firmware, and updates
2059 * hotkey_acpi_mask accordingly. Also resets any bits
2060 * from hotkey_user_mask that are unavailable to be
2061 * delivered (shadow requirement of the userspace ABI).
2062 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002063 * Call with hotkey_mutex held
2064 */
2065static int hotkey_mask_get(void)
2066{
2067 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002068 u32 m = 0;
2069
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002070 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002071 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002072
2073 hotkey_acpi_mask = m;
2074 } else {
2075 /* no mask support doesn't mean no event support... */
2076 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002077 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002078
2079 /* sync userspace-visible mask */
2080 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002081
2082 return 0;
2083}
2084
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002085void static hotkey_mask_warn_incomplete_mask(void)
2086{
2087 /* log only what the user can fix... */
2088 const u32 wantedmask = hotkey_driver_mask &
2089 ~(hotkey_acpi_mask | hotkey_source_mask) &
2090 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2091
2092 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002093 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002094}
2095
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002096/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002097 * Set the firmware mask when supported
2098 *
2099 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2100 *
2101 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2102 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002103 * Call with hotkey_mutex held
2104 */
2105static int hotkey_mask_set(u32 mask)
2106{
2107 int i;
2108 int rc = 0;
2109
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002110 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002111
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002112 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002113 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002114 if (!acpi_evalf(hkey_handle,
2115 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002116 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002117 rc = -EIO;
2118 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002119 }
2120 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002121 }
2122
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002123 /*
2124 * We *must* make an inconditional call to hotkey_mask_get to
2125 * refresh hotkey_acpi_mask and update hotkey_user_mask
2126 *
2127 * Take the opportunity to also log when we cannot _enable_
2128 * a given event.
2129 */
2130 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Joe Perches0978e012011-04-04 10:06:25 -07002131 pr_notice("asked for hotkey mask 0x%08x, but "
2132 "firmware forced it to 0x%08x\n",
2133 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002134 }
2135
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002136 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2137 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002138
2139 return rc;
2140}
2141
2142/*
2143 * Sets hotkey_user_mask and tries to set the firmware mask
2144 *
2145 * Call with hotkey_mutex held
2146 */
2147static int hotkey_user_mask_set(const u32 mask)
2148{
2149 int rc;
2150
2151 /* Give people a chance to notice they are doing something that
2152 * is bound to go boom on their users sooner or later */
2153 if (!tp_warned.hotkey_mask_ff &&
2154 (mask == 0xffff || mask == 0xffffff ||
2155 mask == 0xffffffff)) {
2156 tp_warned.hotkey_mask_ff = 1;
Joe Perches0978e012011-04-04 10:06:25 -07002157 pr_notice("setting the hotkey mask to 0x%08x is likely "
2158 "not the best way to go about it\n", mask);
2159 pr_notice("please consider using the driver defaults, "
2160 "and refer to up-to-date thinkpad-acpi "
2161 "documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002162 }
2163
2164 /* Try to enable what the user asked for, plus whatever we need.
2165 * this syncs everything but won't enable bits in hotkey_user_mask */
2166 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2167
2168 /* Enable the available bits in hotkey_user_mask */
2169 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2170
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002171 return rc;
2172}
2173
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002174/*
2175 * Sets the driver hotkey mask.
2176 *
2177 * Can be called even if the hotkey subdriver is inactive
2178 */
2179static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2180{
2181 int rc;
2182
2183 /* Do the right thing if hotkey_init has not been called yet */
2184 if (!tp_features.hotkey) {
2185 hotkey_driver_mask = mask;
2186 return 0;
2187 }
2188
2189 mutex_lock(&hotkey_mutex);
2190
2191 HOTKEY_CONFIG_CRITICAL_START
2192 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002193#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002194 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002195#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002196 HOTKEY_CONFIG_CRITICAL_END
2197
2198 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2199 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002200 hotkey_poll_setup(true);
2201
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002202 mutex_unlock(&hotkey_mutex);
2203
2204 return rc;
2205}
2206
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002207static int hotkey_status_get(int *status)
2208{
2209 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2210 return -EIO;
2211
2212 return 0;
2213}
2214
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002215static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002216{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002217 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002218 return -EIO;
2219
2220 return 0;
2221}
2222
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002223static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002224{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002225 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002226
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002227 if (tp_features.hotkey_tablet &&
2228 !hotkey_get_tablet_mode(&state)) {
2229 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002230
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002231 input_report_switch(tpacpi_inputdev,
2232 SW_TABLET_MODE, !!state);
2233 input_sync(tpacpi_inputdev);
2234
2235 mutex_unlock(&tpacpi_inputdev_send_mutex);
2236 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002237}
2238
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002239/* Do NOT call without validating scancode first */
2240static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002241{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002242 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002243
2244 if (keycode != KEY_RESERVED) {
2245 mutex_lock(&tpacpi_inputdev_send_mutex);
2246
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002247 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002248 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002249 input_sync(tpacpi_inputdev);
2250
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002251 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002252 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002253 input_sync(tpacpi_inputdev);
2254
2255 mutex_unlock(&tpacpi_inputdev_send_mutex);
2256 }
2257}
2258
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002259/* Do NOT call without validating scancode first */
2260static void tpacpi_input_send_key_masked(const unsigned int scancode)
2261{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002262 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002263 if (hotkey_user_mask & (1 << scancode))
2264 tpacpi_input_send_key(scancode);
2265}
2266
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002267#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2268static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2269
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002270/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002271static void tpacpi_hotkey_send_key(unsigned int scancode)
2272{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002273 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002274 if (hotkey_report_mode < 2) {
2275 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03002276 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002277 }
2278}
2279
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002280static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002281{
2282 u8 d;
2283
2284 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2285 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2286 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2287 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2288 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2289 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2290 }
2291 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2292 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2293 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2294 }
2295 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2296 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2297 n->displayexp_toggle =
2298 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2299 }
2300 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2301 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2302 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2303 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2304 n->brightness_toggle =
2305 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2306 }
2307 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2308 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2309 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2310 >> TP_NVRAM_POS_LEVEL_VOLUME;
2311 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2312 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2313 }
2314}
2315
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002316static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2317 struct tp_nvram_state *newn,
2318 const u32 event_mask)
2319{
2320
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002321#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002322 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002323 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002324 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002325 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002326 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002327
2328#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002329 do { \
2330 if (event_mask & (1 << __scancode)) \
2331 tpacpi_hotkey_send_key(__scancode); \
2332 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002333
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002334 void issue_volchange(const unsigned int oldvol,
2335 const unsigned int newvol)
2336 {
2337 unsigned int i = oldvol;
2338
2339 while (i > newvol) {
2340 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2341 i--;
2342 }
2343 while (i < newvol) {
2344 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2345 i++;
2346 }
2347 }
2348
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002349 void issue_brightnesschange(const unsigned int oldbrt,
2350 const unsigned int newbrt)
2351 {
2352 unsigned int i = oldbrt;
2353
2354 while (i > newbrt) {
2355 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2356 i--;
2357 }
2358 while (i < newbrt) {
2359 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2360 i++;
2361 }
2362 }
2363
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002364 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2365 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2366 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2367 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2368
2369 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2370
2371 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2372
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002373 /*
2374 * Handle volume
2375 *
2376 * This code is supposed to duplicate the IBM firmware behaviour:
2377 * - Pressing MUTE issues mute hotkey message, even when already mute
2378 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002379 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002380 * - The act of unmuting issues volume up/down notification,
2381 * depending which key was used to unmute
2382 *
2383 * We are constrained to what the NVRAM can tell us, which is not much
2384 * and certainly not enough if more than one volume hotkey was pressed
2385 * since the last poll cycle.
2386 *
2387 * Just to make our life interesting, some newer Lenovo ThinkPads have
2388 * bugs in the BIOS and may fail to update volume_toggle properly.
2389 */
2390 if (newn->mute) {
2391 /* muted */
2392 if (!oldn->mute ||
2393 oldn->volume_toggle != newn->volume_toggle ||
2394 oldn->volume_level != newn->volume_level) {
2395 /* recently muted, or repeated mute keypress, or
2396 * multiple presses ending in mute */
2397 issue_volchange(oldn->volume_level, newn->volume_level);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002398 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2399 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002400 } else {
2401 /* unmute */
2402 if (oldn->mute) {
2403 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002404 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002405 }
2406 if (oldn->volume_level != newn->volume_level) {
2407 issue_volchange(oldn->volume_level, newn->volume_level);
2408 } else if (oldn->volume_toggle != newn->volume_toggle) {
2409 /* repeated vol up/down keypress at end of scale ? */
2410 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002411 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002412 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2413 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002414 }
2415 }
2416
2417 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002418 if (oldn->brightness_level != newn->brightness_level) {
2419 issue_brightnesschange(oldn->brightness_level,
2420 newn->brightness_level);
2421 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2422 /* repeated key presses that didn't change state */
2423 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002425 else if (newn->brightness_level >= bright_maxlvl
2426 && !tp_features.bright_unkfw)
2427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002428 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002429
2430#undef TPACPI_COMPARE_KEY
2431#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002432}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002433
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002434/*
2435 * Polling driver
2436 *
2437 * We track all events in hotkey_source_mask all the time, since
2438 * most of them are edge-based. We only issue those requested by
2439 * hotkey_user_mask or hotkey_driver_mask, though.
2440 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002441static int hotkey_kthread(void *data)
2442{
2443 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002444 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002445 unsigned int si, so;
2446 unsigned long t;
2447 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002448 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002449
2450 mutex_lock(&hotkey_thread_mutex);
2451
2452 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2453 goto exit;
2454
2455 set_freezable();
2456
2457 so = 0;
2458 si = 1;
2459 t = 0;
2460
2461 /* Initial state for compares */
2462 mutex_lock(&hotkey_thread_data_mutex);
2463 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002464 poll_mask = hotkey_source_mask;
2465 event_mask = hotkey_source_mask &
2466 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002467 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002468 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002469 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002470
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002471 while (!kthread_should_stop()) {
2472 if (t == 0) {
2473 if (likely(poll_freq))
2474 t = 1000/poll_freq;
2475 else
2476 t = 100; /* should never happen... */
2477 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002478 t = msleep_interruptible(t);
2479 if (unlikely(kthread_should_stop()))
2480 break;
2481 must_reset = try_to_freeze();
2482 if (t > 0 && !must_reset)
2483 continue;
2484
2485 mutex_lock(&hotkey_thread_data_mutex);
2486 if (must_reset || hotkey_config_change != change_detector) {
2487 /* forget old state on thaw or config change */
2488 si = so;
2489 t = 0;
2490 change_detector = hotkey_config_change;
2491 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002492 poll_mask = hotkey_source_mask;
2493 event_mask = hotkey_source_mask &
2494 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002495 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002496 mutex_unlock(&hotkey_thread_data_mutex);
2497
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002498 if (likely(poll_mask)) {
2499 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002500 if (likely(si != so)) {
2501 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002502 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002503 }
2504 }
2505
2506 so = si;
2507 si ^= 1;
2508 }
2509
2510exit:
2511 mutex_unlock(&hotkey_thread_mutex);
2512 return 0;
2513}
2514
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002515/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002516static void hotkey_poll_stop_sync(void)
2517{
2518 if (tpacpi_hotkey_task) {
2519 if (frozen(tpacpi_hotkey_task) ||
2520 freezing(tpacpi_hotkey_task))
2521 thaw_process(tpacpi_hotkey_task);
2522
2523 kthread_stop(tpacpi_hotkey_task);
2524 tpacpi_hotkey_task = NULL;
2525 mutex_lock(&hotkey_thread_mutex);
2526 /* at this point, the thread did exit */
2527 mutex_unlock(&hotkey_thread_mutex);
2528 }
2529}
2530
2531/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002532static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002533{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002534 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2535 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002536
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002537 if (hotkey_poll_freq > 0 &&
2538 (poll_driver_mask ||
2539 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002540 if (!tpacpi_hotkey_task) {
2541 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002542 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002543 if (IS_ERR(tpacpi_hotkey_task)) {
2544 tpacpi_hotkey_task = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07002545 pr_err("could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002546 "for hotkey polling\n");
2547 }
2548 }
2549 } else {
2550 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002551 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002552 hotkey_poll_freq == 0) {
Joe Perches0978e012011-04-04 10:06:25 -07002553 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2554 "require polling, which is currently "
2555 "disabled\n",
2556 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002557 }
2558 }
2559}
2560
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002561static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002562{
2563 mutex_lock(&hotkey_mutex);
2564 hotkey_poll_setup(may_warn);
2565 mutex_unlock(&hotkey_mutex);
2566}
2567
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002568/* call with hotkey_mutex held */
2569static void hotkey_poll_set_freq(unsigned int freq)
2570{
2571 if (!freq)
2572 hotkey_poll_stop_sync();
2573
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002574 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002575}
2576
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002577#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2578
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002579static void hotkey_poll_setup(const bool __unused)
2580{
2581}
2582
2583static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002584{
2585}
2586
2587#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2588
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002589static int hotkey_inputdev_open(struct input_dev *dev)
2590{
2591 switch (tpacpi_lifecycle) {
2592 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002593 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002594 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002595 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002596 case TPACPI_LIFE_EXITING:
2597 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002598 }
2599
2600 /* Should only happen if tpacpi_lifecycle is corrupt */
2601 BUG();
2602 return -EBUSY;
2603}
2604
2605static void hotkey_inputdev_close(struct input_dev *dev)
2606{
2607 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002608 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002609 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002610 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002611}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002612
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002613/* sysfs hotkey enable ------------------------------------------------- */
2614static ssize_t hotkey_enable_show(struct device *dev,
2615 struct device_attribute *attr,
2616 char *buf)
2617{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002618 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002619
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002620 printk_deprecated_attribute("hotkey_enable",
2621 "Hotkey reporting is always enabled");
2622
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002623 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002624 if (res)
2625 return res;
2626
2627 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2628}
2629
2630static ssize_t hotkey_enable_store(struct device *dev,
2631 struct device_attribute *attr,
2632 const char *buf, size_t count)
2633{
2634 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002635
2636 printk_deprecated_attribute("hotkey_enable",
2637 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002638
2639 if (parse_strtoul(buf, 1, &t))
2640 return -EINVAL;
2641
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002642 if (t == 0)
2643 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002644
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002645 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002646}
2647
2648static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002649 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002650 hotkey_enable_show, hotkey_enable_store);
2651
2652/* sysfs hotkey mask --------------------------------------------------- */
2653static ssize_t hotkey_mask_show(struct device *dev,
2654 struct device_attribute *attr,
2655 char *buf)
2656{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002657 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002658}
2659
2660static ssize_t hotkey_mask_store(struct device *dev,
2661 struct device_attribute *attr,
2662 const char *buf, size_t count)
2663{
2664 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002665 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002666
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002667 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002668 return -EINVAL;
2669
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002670 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002671 return -ERESTARTSYS;
2672
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002673 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002674
2675#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002676 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002677#endif
2678
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002679 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002680
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002681 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2682
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002683 return (res) ? res : count;
2684}
2685
2686static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002687 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002688 hotkey_mask_show, hotkey_mask_store);
2689
2690/* sysfs hotkey bios_enabled ------------------------------------------- */
2691static ssize_t hotkey_bios_enabled_show(struct device *dev,
2692 struct device_attribute *attr,
2693 char *buf)
2694{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002695 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002696}
2697
2698static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002699 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002700
2701/* sysfs hotkey bios_mask ---------------------------------------------- */
2702static ssize_t hotkey_bios_mask_show(struct device *dev,
2703 struct device_attribute *attr,
2704 char *buf)
2705{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002706 printk_deprecated_attribute("hotkey_bios_mask",
2707 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002708 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002709}
2710
2711static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002712 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002713
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002714/* sysfs hotkey all_mask ----------------------------------------------- */
2715static ssize_t hotkey_all_mask_show(struct device *dev,
2716 struct device_attribute *attr,
2717 char *buf)
2718{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002719 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2720 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002721}
2722
2723static struct device_attribute dev_attr_hotkey_all_mask =
2724 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2725
2726/* sysfs hotkey recommended_mask --------------------------------------- */
2727static ssize_t hotkey_recommended_mask_show(struct device *dev,
2728 struct device_attribute *attr,
2729 char *buf)
2730{
2731 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002732 (hotkey_all_mask | hotkey_source_mask)
2733 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002734}
2735
2736static struct device_attribute dev_attr_hotkey_recommended_mask =
2737 __ATTR(hotkey_recommended_mask, S_IRUGO,
2738 hotkey_recommended_mask_show, NULL);
2739
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002740#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2741
2742/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2743static ssize_t hotkey_source_mask_show(struct device *dev,
2744 struct device_attribute *attr,
2745 char *buf)
2746{
2747 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2748}
2749
2750static ssize_t hotkey_source_mask_store(struct device *dev,
2751 struct device_attribute *attr,
2752 const char *buf, size_t count)
2753{
2754 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002755 u32 r_ev;
2756 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002757
2758 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2759 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2760 return -EINVAL;
2761
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002762 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002763 return -ERESTARTSYS;
2764
2765 HOTKEY_CONFIG_CRITICAL_START
2766 hotkey_source_mask = t;
2767 HOTKEY_CONFIG_CRITICAL_END
2768
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002769 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2770 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002771 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002772
2773 /* check if events needed by the driver got disabled */
2774 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2775 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002776
2777 mutex_unlock(&hotkey_mutex);
2778
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002779 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07002780 pr_err("hotkey_source_mask: "
2781 "failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002782
2783 if (r_ev)
Joe Perches0978e012011-04-04 10:06:25 -07002784 pr_notice("hotkey_source_mask: "
2785 "some important events were disabled: 0x%04x\n",
2786 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002787
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002788 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2789
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002790 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002791}
2792
2793static struct device_attribute dev_attr_hotkey_source_mask =
2794 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2795 hotkey_source_mask_show, hotkey_source_mask_store);
2796
2797/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2798static ssize_t hotkey_poll_freq_show(struct device *dev,
2799 struct device_attribute *attr,
2800 char *buf)
2801{
2802 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2803}
2804
2805static ssize_t hotkey_poll_freq_store(struct device *dev,
2806 struct device_attribute *attr,
2807 const char *buf, size_t count)
2808{
2809 unsigned long t;
2810
2811 if (parse_strtoul(buf, 25, &t))
2812 return -EINVAL;
2813
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002814 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002815 return -ERESTARTSYS;
2816
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002817 hotkey_poll_set_freq(t);
2818 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002819
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002820 mutex_unlock(&hotkey_mutex);
2821
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002822 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2823
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002824 return count;
2825}
2826
2827static struct device_attribute dev_attr_hotkey_poll_freq =
2828 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2829 hotkey_poll_freq_show, hotkey_poll_freq_store);
2830
2831#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2832
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002833/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002834static ssize_t hotkey_radio_sw_show(struct device *dev,
2835 struct device_attribute *attr,
2836 char *buf)
2837{
Johannes Berg19d337d2009-06-02 13:01:37 +02002838 int res;
2839 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002840 if (res < 0)
2841 return res;
2842
Johannes Berg19d337d2009-06-02 13:01:37 +02002843 /* Opportunistic update */
2844 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2845
2846 return snprintf(buf, PAGE_SIZE, "%d\n",
2847 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002848}
2849
2850static struct device_attribute dev_attr_hotkey_radio_sw =
2851 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2852
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002853static void hotkey_radio_sw_notify_change(void)
2854{
2855 if (tp_features.hotkey_wlsw)
2856 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2857 "hotkey_radio_sw");
2858}
2859
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002860/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2861static ssize_t hotkey_tablet_mode_show(struct device *dev,
2862 struct device_attribute *attr,
2863 char *buf)
2864{
2865 int res, s;
2866 res = hotkey_get_tablet_mode(&s);
2867 if (res < 0)
2868 return res;
2869
2870 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2871}
2872
2873static struct device_attribute dev_attr_hotkey_tablet_mode =
2874 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2875
2876static void hotkey_tablet_mode_notify_change(void)
2877{
2878 if (tp_features.hotkey_tablet)
2879 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2880 "hotkey_tablet_mode");
2881}
2882
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002883/* sysfs hotkey report_mode -------------------------------------------- */
2884static ssize_t hotkey_report_mode_show(struct device *dev,
2885 struct device_attribute *attr,
2886 char *buf)
2887{
2888 return snprintf(buf, PAGE_SIZE, "%d\n",
2889 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2890}
2891
2892static struct device_attribute dev_attr_hotkey_report_mode =
2893 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2894
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002895/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002896static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2897 struct device_attribute *attr,
2898 char *buf)
2899{
2900 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2901}
2902
2903static struct device_attribute dev_attr_hotkey_wakeup_reason =
2904 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2905
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002906static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002907{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002908 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2909 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002910}
2911
2912/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002913static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2914 struct device_attribute *attr,
2915 char *buf)
2916{
2917 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2918}
2919
2920static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2921 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2922 hotkey_wakeup_hotunplug_complete_show, NULL);
2923
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002924static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002925{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002926 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2927 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002928}
2929
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002930/* --------------------------------------------------------------------- */
2931
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002932static struct attribute *hotkey_attributes[] __initdata = {
2933 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002934 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002935 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002936 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002937 &dev_attr_hotkey_wakeup_reason.attr,
2938 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002939 &dev_attr_hotkey_mask.attr,
2940 &dev_attr_hotkey_all_mask.attr,
2941 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002942#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002943 &dev_attr_hotkey_source_mask.attr,
2944 &dev_attr_hotkey_poll_freq.attr,
2945#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002946};
2947
Johannes Berg19d337d2009-06-02 13:01:37 +02002948/*
2949 * Sync both the hw and sw blocking state of all switches
2950 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002951static void tpacpi_send_radiosw_update(void)
2952{
2953 int wlsw;
2954
Johannes Berg19d337d2009-06-02 13:01:37 +02002955 /*
2956 * We must sync all rfkill controllers *before* issuing any
2957 * rfkill input events, or we will race the rfkill core input
2958 * handler.
2959 *
Lucas De Marchic8440332011-03-17 17:18:22 -03002960 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02002961 * for the above.
2962 *
2963 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2964 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002965
Johannes Berg19d337d2009-06-02 13:01:37 +02002966 wlsw = hotkey_get_wlsw();
2967
2968 /* Sync hw blocking state first if it is hw-blocked */
2969 if (wlsw == TPACPI_RFK_RADIO_OFF)
2970 tpacpi_rfk_update_hwblock_state(true);
2971
2972 /* Sync sw blocking state */
2973 tpacpi_rfk_update_swstate_all();
2974
2975 /* Sync hw blocking state last if it is hw-unblocked */
2976 if (wlsw == TPACPI_RFK_RADIO_ON)
2977 tpacpi_rfk_update_hwblock_state(false);
2978
2979 /* Issue rfkill input event for WLSW switch */
2980 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002981 mutex_lock(&tpacpi_inputdev_send_mutex);
2982
2983 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002984 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002985 input_sync(tpacpi_inputdev);
2986
2987 mutex_unlock(&tpacpi_inputdev_send_mutex);
2988 }
Johannes Berg19d337d2009-06-02 13:01:37 +02002989
2990 /*
2991 * this can be unconditional, as we will poll state again
2992 * if userspace uses the notify to read data
2993 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002994 hotkey_radio_sw_notify_change();
2995}
2996
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002997static void hotkey_exit(void)
2998{
2999#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003000 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003001 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003002 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003003#endif
3004
3005 if (hotkey_dev_attributes)
3006 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3007
3008 kfree(hotkey_keycode_map);
3009
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003010 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003011 "restoring original HKEY status and mask\n");
3012 /* yes, there is a bitwise or below, we want the
3013 * functions to be called even if one of them fail */
3014 if (((tp_features.hotkey_mask &&
3015 hotkey_mask_set(hotkey_orig_mask)) |
3016 hotkey_status_set(false)) != 0)
Joe Perches0978e012011-04-04 10:06:25 -07003017 pr_err("failed to restore hot key mask "
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003018 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003019}
3020
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003021static void __init hotkey_unmap(const unsigned int scancode)
3022{
3023 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3024 clear_bit(hotkey_keycode_map[scancode],
3025 tpacpi_inputdev->keybit);
3026 hotkey_keycode_map[scancode] = KEY_RESERVED;
3027 }
3028}
3029
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003030/*
3031 * HKEY quirks:
3032 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3033 */
3034
3035#define TPACPI_HK_Q_INIMASK 0x0001
3036
3037static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3038 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3039 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3040 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3041 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3042 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3043 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3044 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3045 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3046 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3047 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3048 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3049 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3050 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3051 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3052 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3053 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3054 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3055 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3056 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3057};
3058
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003059typedef u16 tpacpi_keymap_entry_t;
3060typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003061
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003062static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003063{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003064 /* Requirements for changing the default keymaps:
3065 *
3066 * 1. Many of the keys are mapped to KEY_RESERVED for very
3067 * good reasons. Do not change them unless you have deep
3068 * knowledge on the IBM and Lenovo ThinkPad firmware for
3069 * the various ThinkPad models. The driver behaves
3070 * differently for KEY_RESERVED: such keys have their
3071 * hot key mask *unset* in mask_recommended, and also
3072 * in the initial hot key mask programmed into the
3073 * firmware at driver load time, which means the firm-
3074 * ware may react very differently if you change them to
3075 * something else;
3076 *
3077 * 2. You must be subscribed to the linux-thinkpad and
3078 * ibm-acpi-devel mailing lists, and you should read the
3079 * list archives since 2007 if you want to change the
3080 * keymaps. This requirement exists so that you will
3081 * know the past history of problems with the thinkpad-
3082 * acpi driver keymaps, and also that you will be
3083 * listening to any bug reports;
3084 *
3085 * 3. Do not send thinkpad-acpi specific patches directly to
3086 * for merging, *ever*. Send them to the linux-acpi
3087 * mailinglist for comments. Merging is to be done only
3088 * through acpi-test and the ACPI maintainer.
3089 *
3090 * If the above is too much to ask, don't change the keymap.
3091 * Ask the thinkpad-acpi maintainer to do it, instead.
3092 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003093
3094 enum keymap_index {
3095 TPACPI_KEYMAP_IBM_GENERIC = 0,
3096 TPACPI_KEYMAP_LENOVO_GENERIC,
3097 };
3098
3099 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3100 /* Generic keymap for IBM ThinkPads */
3101 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003102 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003103 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003104 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3105 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003106
3107 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003108 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3109 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3110 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003111
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003112 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003113 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003114 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003115
3116 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003117 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003118
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003119 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3120 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003121
3122 /* Volume: firmware always react to it and reprograms
3123 * the built-in *extra* mixer. Never map it to control
3124 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003125 KEY_RESERVED, /* 0x14: VOLUME UP */
3126 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3127 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003128
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003129 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003130
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003131 /* (assignments unknown, please report if found) */
3132 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3133 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003134 },
3135
3136 /* Generic keymap for Lenovo ThinkPads */
3137 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003138 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3139 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003140 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003141 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003142
3143 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003144 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3145 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3146 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003147
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003148 /* These should be enabled --only-- when ACPI video
3149 * is disabled (i.e. in "vendor" mode), and are handled
3150 * in a special way by the init code */
3151 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3152 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003153
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003154 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003155
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003156 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3157 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003158
3159 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3160 * react to it and reprograms the built-in *extra* mixer.
3161 * Never map it to control another mixer by default.
3162 *
3163 * T60?, T61, R60?, R61: firmware and EC tries to send
3164 * these over the regular keyboard, so these are no-ops,
3165 * but there are still weird bugs re. MUTE, so do not
3166 * change unless you get test reports from all Lenovo
3167 * models. May cause the BIOS to interfere with the
3168 * HDA mixer.
3169 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003170 KEY_RESERVED, /* 0x14: VOLUME UP */
3171 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3172 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003173
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003174 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003175
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003176 /* (assignments unknown, please report if found) */
3177 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3178 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003179 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003180 };
3181
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003182 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3183 /* Generic maps (fallback) */
3184 {
3185 .vendor = PCI_VENDOR_ID_IBM,
3186 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3187 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3188 },
3189 {
3190 .vendor = PCI_VENDOR_ID_LENOVO,
3191 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3192 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3193 },
3194 };
3195
3196#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003197#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003198
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003199 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003200 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003201 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003202 bool radiosw_state = false;
3203 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003204
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003205 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003206 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003207
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003208 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3209 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003210
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003211 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003212 BUG_ON(tpacpi_inputdev->open != NULL ||
3213 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003214
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003215 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003216 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003217
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003218#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3219 mutex_init(&hotkey_thread_mutex);
3220 mutex_init(&hotkey_thread_data_mutex);
3221#endif
3222
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003223 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003224 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003225
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003226 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3227 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003228 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003229
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003230 if (!tp_features.hotkey)
3231 return 1;
3232
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003233 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3234 ARRAY_SIZE(tpacpi_hotkey_qtable));
3235
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003236 tpacpi_disable_brightness_delay();
3237
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003238 /* MUST have enough space for all attributes to be added to
3239 * hotkey_dev_attributes */
3240 hotkey_dev_attributes = create_attr_set(
3241 ARRAY_SIZE(hotkey_attributes) + 2,
3242 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003243 if (!hotkey_dev_attributes)
3244 return -ENOMEM;
3245 res = add_many_to_attr_set(hotkey_dev_attributes,
3246 hotkey_attributes,
3247 ARRAY_SIZE(hotkey_attributes));
3248 if (res)
3249 goto err_exit;
3250
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003251 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003252 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3253 for HKEY interface version 0x100 */
3254 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3255 if ((hkeyv >> 8) != 1) {
Joe Perches0978e012011-04-04 10:06:25 -07003256 pr_err("unknown version of the HKEY interface: 0x%x\n",
3257 hkeyv);
3258 pr_err("please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003259 } else {
3260 /*
3261 * MHKV 0x100 in A31, R40, R40e,
3262 * T4x, X31, and later
3263 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003264 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3265 "firmware HKEY interface version: 0x%x\n",
3266 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003267
3268 /* Paranoia check AND init hotkey_all_mask */
3269 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3270 "MHKA", "qd")) {
Joe Perches0978e012011-04-04 10:06:25 -07003271 pr_err("missing MHKA handler, "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003272 "please report this to %s\n",
3273 TPACPI_MAIL);
3274 /* Fallback: pre-init for FN+F3,F4,F12 */
3275 hotkey_all_mask = 0x080cU;
3276 } else {
3277 tp_features.hotkey_mask = 1;
3278 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003279 }
3280 }
3281
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003282 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3283 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003284 str_supported(tp_features.hotkey_mask));
3285
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003286 /* Init hotkey_all_mask if not initialized yet */
3287 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3288 (quirks & TPACPI_HK_Q_INIMASK))
3289 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003290
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003291 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003292 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003293 /* hotkey_source_mask *must* be zero for
3294 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003295 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003296 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003297 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003298
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003299 hotkey_orig_mask = hotkey_acpi_mask;
3300 } else {
3301 hotkey_orig_mask = hotkey_all_mask;
3302 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003303 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003304
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003305#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3306 if (dbg_wlswemul) {
3307 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003308 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003309 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003310 } else
3311#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003312 /* Not all thinkpads have a hardware radio switch */
3313 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3314 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003315 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003316 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003317 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003318 }
3319 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003320 res = add_to_attr_set(hotkey_dev_attributes,
3321 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003322
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003323 /* For X41t, X60t, X61t Tablets... */
3324 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3325 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003326 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Joe Perches0978e012011-04-04 10:06:25 -07003327 pr_info("possible tablet mode switch found; "
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003328 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003329 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003330 res = add_to_attr_set(hotkey_dev_attributes,
3331 &dev_attr_hotkey_tablet_mode.attr);
3332 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003333
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003334 if (!res)
3335 res = register_attr_set_with_sysfs(
3336 hotkey_dev_attributes,
3337 &tpacpi_pdev->dev.kobj);
3338 if (res)
3339 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003340
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003341 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003342 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3343 GFP_KERNEL);
3344 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003345 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003346 res = -ENOMEM;
3347 goto err_exit;
3348 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003349
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003350 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3351 ARRAY_SIZE(tpacpi_keymap_qtable));
3352 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3353 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3354 "using keymap number %lu\n", keymap_id);
3355
3356 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3357 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003358
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003359 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003360 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3361 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3362 tpacpi_inputdev->keycode = hotkey_keycode_map;
3363 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3364 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003365 input_set_capability(tpacpi_inputdev, EV_KEY,
3366 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003367 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003368 if (i < sizeof(hotkey_reserved_mask)*8)
3369 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003370 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003371 }
3372
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003373 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003374 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003375 input_report_switch(tpacpi_inputdev,
3376 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003377 }
3378 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003379 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003380 input_report_switch(tpacpi_inputdev,
3381 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003382 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003383
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003384 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003385 * userspace. tpacpi_detect_brightness_capabilities() must have
3386 * been called before this point */
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003387 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Joe Perches0978e012011-04-04 10:06:25 -07003388 pr_info("This ThinkPad has standard ACPI backlight "
3389 "brightness control, supported by the ACPI "
3390 "video driver\n");
3391 pr_notice("Disabling thinkpad-acpi brightness events "
3392 "by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003393
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003394 /* Disable brightness up/down on Lenovo thinkpads when
3395 * ACPI is handling them, otherwise it is plain impossible
3396 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003397 hotkey_reserved_mask |=
3398 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3399 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003400 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3401 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003402 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003403
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003404#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003405 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3406 & ~hotkey_all_mask
3407 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003408
3409 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3410 "hotkey source mask 0x%08x, polling freq %u\n",
3411 hotkey_source_mask, hotkey_poll_freq);
3412#endif
3413
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003414 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3415 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003416 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003417 if (res) {
3418 hotkey_exit();
3419 return res;
3420 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003421 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3422 | hotkey_driver_mask)
3423 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003424 if (res < 0 && res != -ENXIO) {
3425 hotkey_exit();
3426 return res;
3427 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003428 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3429 & ~hotkey_reserved_mask;
3430 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3431 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3432 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003433
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003434 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3435 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003436 (hotkey_report_mode < 2) ?
3437 "enabled" : "disabled");
3438
3439 tpacpi_inputdev->open = &hotkey_inputdev_open;
3440 tpacpi_inputdev->close = &hotkey_inputdev_close;
3441
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003442 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003443
3444 return 0;
3445
3446err_exit:
3447 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3448 hotkey_dev_attributes = NULL;
3449
3450 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003451}
3452
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003453static bool hotkey_notify_hotkey(const u32 hkey,
3454 bool *send_acpi_ev,
3455 bool *ignore_acpi_ev)
3456{
3457 /* 0x1000-0x1FFF: key presses */
3458 unsigned int scancode = hkey & 0xfff;
3459 *send_acpi_ev = true;
3460 *ignore_acpi_ev = false;
3461
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03003462 /* HKEY event 0x1001 is scancode 0x00 */
3463 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003464 scancode--;
3465 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003466 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003467 *send_acpi_ev = false;
3468 } else {
3469 *ignore_acpi_ev = true;
3470 }
3471 return true;
3472 }
3473 return false;
3474}
3475
3476static bool hotkey_notify_wakeup(const u32 hkey,
3477 bool *send_acpi_ev,
3478 bool *ignore_acpi_ev)
3479{
3480 /* 0x2000-0x2FFF: Wakeup reason */
3481 *send_acpi_ev = true;
3482 *ignore_acpi_ev = false;
3483
3484 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003485 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3486 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003487 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3488 *ignore_acpi_ev = true;
3489 break;
3490
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003491 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3492 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003493 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3494 *ignore_acpi_ev = true;
3495 break;
3496
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003497 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3498 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003499 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003500 /* how to auto-heal: */
3501 /* 2313: woke up from S3, go to S4/S5 */
3502 /* 2413: woke up from S4, go to S5 */
3503 break;
3504
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003505 default:
3506 return false;
3507 }
3508
3509 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003510 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003511 hotkey_wakeup_reason_notify_change();
3512 }
3513 return true;
3514}
3515
3516static bool hotkey_notify_usrevent(const u32 hkey,
3517 bool *send_acpi_ev,
3518 bool *ignore_acpi_ev)
3519{
3520 /* 0x5000-0x5FFF: human interface helpers */
3521 *send_acpi_ev = true;
3522 *ignore_acpi_ev = false;
3523
3524 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003525 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3526 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003527 return true;
3528
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003529 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3530 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003531 tpacpi_input_send_tabletsw();
3532 hotkey_tablet_mode_notify_change();
3533 *send_acpi_ev = false;
3534 return true;
3535
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003536 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3537 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3538 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003539 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003540 *ignore_acpi_ev = true;
3541 return true;
3542
3543 default:
3544 return false;
3545 }
3546}
3547
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003548static void thermal_dump_all_sensors(void);
3549
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003550static bool hotkey_notify_thermal(const u32 hkey,
3551 bool *send_acpi_ev,
3552 bool *ignore_acpi_ev)
3553{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003554 bool known = true;
3555
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003556 /* 0x6000-0x6FFF: thermal alarms */
3557 *send_acpi_ev = true;
3558 *ignore_acpi_ev = false;
3559
3560 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003561 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003562 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003563 /* recommended action: do nothing, we don't have
3564 * Lenovo ATM information */
3565 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003566 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003567 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003568 /* recommended action: warn user through gui */
3569 break;
3570 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003571 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003572 /* recommended action: immediate sleep/hibernate */
3573 break;
3574 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003575 pr_crit("THERMAL ALARM: "
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003576 "a sensor reports something is too hot!\n");
3577 /* recommended action: warn user through gui, that */
3578 /* some internal component is too hot */
3579 break;
3580 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003581 pr_alert("THERMAL EMERGENCY: "
3582 "a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003583 /* recommended action: immediate sleep/hibernate */
3584 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003585 default:
Joe Perches0978e012011-04-04 10:06:25 -07003586 pr_alert("THERMAL ALERT: unknown thermal alarm received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003587 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003588 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003589
3590 thermal_dump_all_sensors();
3591
3592 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003593}
3594
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003595static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3596{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003597 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003598 bool send_acpi_ev;
3599 bool ignore_acpi_ev;
3600 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003601
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003602 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07003603 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003604 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003605 acpi_bus_generate_netlink_event(
3606 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003607 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003608 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003609 return;
3610 }
3611
3612 while (1) {
3613 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07003614 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003615 return;
3616 }
3617
3618 if (hkey == 0) {
3619 /* queue empty */
3620 return;
3621 }
3622
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003623 send_acpi_ev = true;
3624 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003625
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003626 switch (hkey >> 12) {
3627 case 1:
3628 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003629 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3630 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003631 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003632 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003633 /* 0x2000-0x2FFF: Wakeup reason */
3634 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3635 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003636 break;
3637 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003638 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003639 switch (hkey) {
3640 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003641 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07003642 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003643 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003644 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003645 break;
3646 case TP_HKEY_EV_OPTDRV_EJ:
3647 /* FIXME: kick libata if SATA link offline */
3648 known_ev = true;
3649 break;
3650 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003651 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003652 }
3653 break;
3654 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003655 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003656 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003657 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07003658 pr_info("undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003659 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003660 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003661 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003662 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003663 }
3664 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003665 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003666 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003667 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3668 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003669 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003670 case 6:
3671 /* 0x6000-0x6FFF: thermal alarms */
3672 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3673 &ignore_acpi_ev);
3674 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003675 case 7:
3676 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003677 if (tp_features.hotkey_wlsw &&
3678 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003679 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003680 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003681 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003682 break;
3683 }
3684 /* fallthrough to default */
3685 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003686 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003687 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003688 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07003689 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3690 pr_notice("please report the conditions when this "
3691 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003692 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003693
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003694 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003695 if (!ignore_acpi_ev &&
3696 (send_acpi_ev || hotkey_report_mode < 2)) {
3697 acpi_bus_generate_proc_event(ibm->acpi->device,
3698 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003699 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003700
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003701 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003702 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003703 acpi_bus_generate_netlink_event(
3704 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003705 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003706 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003707 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003708 }
3709}
3710
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003711static void hotkey_suspend(pm_message_t state)
3712{
3713 /* Do these on suspend, we get the events on early resume! */
3714 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3715 hotkey_autosleep_ack = 0;
3716}
3717
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003718static void hotkey_resume(void)
3719{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003720 tpacpi_disable_brightness_delay();
3721
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003722 if (hotkey_status_set(true) < 0 ||
3723 hotkey_mask_set(hotkey_acpi_mask) < 0)
Joe Perches0978e012011-04-04 10:06:25 -07003724 pr_err("error while attempting to reset the event "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003725 "firmware interface\n");
3726
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003727 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003728 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003729 hotkey_wakeup_reason_notify_change();
3730 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003731 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003732}
3733
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003734/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003735static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003737 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003739 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003740 seq_printf(m, "status:\t\tnot supported\n");
3741 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003742 }
3743
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003744 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003745 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003746 res = hotkey_status_get(&status);
3747 if (!res)
3748 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003749 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003750 if (res)
3751 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003753 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003754 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003755 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3756 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003758 seq_printf(m, "mask:\t\tnot supported\n");
3759 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 }
3761
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763}
3764
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003765static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003766{
3767 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003768 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Joe Perches0978e012011-04-04 10:06:25 -07003769 pr_fmt("hotkey enable/disable functionality has been "
3770 "removed from the driver. "
3771 "Hotkeys are always enabled.\n")))
3772 pr_err("Please remove the hotkey=enable module "
3773 "parameter, it is deprecated. "
3774 "Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003775}
3776
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003777static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003779 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003780 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003783 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 return -ENODEV;
3785
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003786 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003787 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003788
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003789 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003790
3791 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 while ((cmd = next_cmd(&buf))) {
3793 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003794 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003796 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003797 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003799 mask = (hotkey_all_mask | hotkey_source_mask)
3800 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3802 /* mask set */
3803 } else if (sscanf(cmd, "%x", &mask) == 1) {
3804 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003805 } else {
3806 res = -EINVAL;
3807 goto errexit;
3808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003810
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003811 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003812 tpacpi_disclose_usertask("procfs hotkey",
3813 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003814 res = hotkey_user_mask_set(mask);
3815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003817errexit:
3818 mutex_unlock(&hotkey_mutex);
3819 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003820}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003822static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04003823 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3824 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003825 {"", 0},
3826};
3827
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003828static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003829 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003830 .notify = hotkey_notify,
3831 .handle = &hkey_handle,
3832 .type = ACPI_DEVICE_NOTIFY,
3833};
3834
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003835static struct ibm_struct hotkey_driver_data = {
3836 .name = "hotkey",
3837 .read = hotkey_read,
3838 .write = hotkey_write,
3839 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003840 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003841 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003842 .acpi = &ibm_hotkey_acpidriver,
3843};
3844
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003845/*************************************************************************
3846 * Bluetooth subdriver
3847 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003849enum {
3850 /* ACPI GBDC/SBDC bits */
3851 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3852 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003853 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003854 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003855};
3856
3857enum {
3858 /* ACPI \BLTH commands */
3859 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3860 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3861 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3862 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3863 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003864};
3865
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003866#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3867
Johannes Berg19d337d2009-06-02 13:01:37 +02003868static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003869{
3870 int status;
3871
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003872#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3873 if (dbg_bluetoothemul)
3874 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003875 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003876#endif
3877
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003878 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3879 return -EIO;
3880
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003881 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003882 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003883}
3884
Johannes Berg19d337d2009-06-02 13:01:37 +02003885static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003886{
3887 int status;
3888
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003889 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003890 "will attempt to %s bluetooth\n",
3891 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003892
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003893#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3894 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003895 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003896 return 0;
3897 }
3898#endif
3899
Johannes Berg19d337d2009-06-02 13:01:37 +02003900 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003901 status = TP_ACPI_BLUETOOTH_RADIOSSW
3902 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3903 else
3904 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003905
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003906 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3907 return -EIO;
3908
3909 return 0;
3910}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003911
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003912/* sysfs bluetooth enable ---------------------------------------------- */
3913static ssize_t bluetooth_enable_show(struct device *dev,
3914 struct device_attribute *attr,
3915 char *buf)
3916{
Johannes Berg19d337d2009-06-02 13:01:37 +02003917 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3918 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003919}
3920
3921static ssize_t bluetooth_enable_store(struct device *dev,
3922 struct device_attribute *attr,
3923 const char *buf, size_t count)
3924{
Johannes Berg19d337d2009-06-02 13:01:37 +02003925 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3926 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003927}
3928
3929static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003930 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003931 bluetooth_enable_show, bluetooth_enable_store);
3932
3933/* --------------------------------------------------------------------- */
3934
3935static struct attribute *bluetooth_attributes[] = {
3936 &dev_attr_bluetooth_enable.attr,
3937 NULL
3938};
3939
3940static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003941 .attrs = bluetooth_attributes,
3942};
3943
Johannes Berg19d337d2009-06-02 13:01:37 +02003944static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3945 .get_status = bluetooth_get_status,
3946 .set_status = bluetooth_set_status,
3947};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003948
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003949static void bluetooth_shutdown(void)
3950{
3951 /* Order firmware to save current state to NVRAM */
3952 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3953 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07003954 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003955 else
3956 vdbg_printk(TPACPI_DBG_RFKILL,
3957 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003958}
3959
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003960static void bluetooth_exit(void)
3961{
3962 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3963 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003964
3965 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3966
3967 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003968}
3969
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003970static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003972 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003973 int status = 0;
3974
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003975 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3976 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003977
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003978 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003979
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003980 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3981 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003982 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003983 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003985 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3986 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003987 str_supported(tp_features.bluetooth),
3988 status);
3989
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003990#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3991 if (dbg_bluetoothemul) {
3992 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07003993 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003994 } else
3995#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003996 if (tp_features.bluetooth &&
3997 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3998 /* no bluetooth hardware present in system */
3999 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004000 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004001 "bluetooth hardware not installed\n");
4002 }
4003
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004004 if (!tp_features.bluetooth)
4005 return 1;
4006
Johannes Berg19d337d2009-06-02 13:01:37 +02004007 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4008 &bluetooth_tprfk_ops,
4009 RFKILL_TYPE_BLUETOOTH,
4010 TPACPI_RFK_BLUETOOTH_SW_NAME,
4011 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004012 if (res)
4013 return res;
4014
Johannes Berg19d337d2009-06-02 13:01:37 +02004015 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4016 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004017 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004018 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004019 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004020 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004021
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004022 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023}
4024
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004025/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004026static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004028 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029}
4030
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004031static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032{
Johannes Berg19d337d2009-06-02 13:01:37 +02004033 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034}
4035
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004036static struct ibm_struct bluetooth_driver_data = {
4037 .name = "bluetooth",
4038 .read = bluetooth_read,
4039 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004040 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004041 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004042};
4043
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004044/*************************************************************************
4045 * Wan subdriver
4046 */
4047
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004048enum {
4049 /* ACPI GWAN/SWAN bits */
4050 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4051 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004052 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004053 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004054};
4055
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004056#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4057
Johannes Berg19d337d2009-06-02 13:01:37 +02004058static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004059{
4060 int status;
4061
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004062#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4063 if (dbg_wwanemul)
4064 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004065 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004066#endif
4067
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004068 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4069 return -EIO;
4070
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004071 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004072 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004073}
4074
Johannes Berg19d337d2009-06-02 13:01:37 +02004075static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004076{
4077 int status;
4078
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004079 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004080 "will attempt to %s wwan\n",
4081 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004082
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004083#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4084 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004085 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004086 return 0;
4087 }
4088#endif
4089
Johannes Berg19d337d2009-06-02 13:01:37 +02004090 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004091 status = TP_ACPI_WANCARD_RADIOSSW
4092 | TP_ACPI_WANCARD_RESUMECTRL;
4093 else
4094 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004095
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004096 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4097 return -EIO;
4098
4099 return 0;
4100}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004101
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004102/* sysfs wan enable ---------------------------------------------------- */
4103static ssize_t wan_enable_show(struct device *dev,
4104 struct device_attribute *attr,
4105 char *buf)
4106{
Johannes Berg19d337d2009-06-02 13:01:37 +02004107 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4108 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004109}
4110
4111static ssize_t wan_enable_store(struct device *dev,
4112 struct device_attribute *attr,
4113 const char *buf, size_t count)
4114{
Johannes Berg19d337d2009-06-02 13:01:37 +02004115 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4116 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004117}
4118
4119static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004120 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004121 wan_enable_show, wan_enable_store);
4122
4123/* --------------------------------------------------------------------- */
4124
4125static struct attribute *wan_attributes[] = {
4126 &dev_attr_wan_enable.attr,
4127 NULL
4128};
4129
4130static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004131 .attrs = wan_attributes,
4132};
4133
Johannes Berg19d337d2009-06-02 13:01:37 +02004134static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4135 .get_status = wan_get_status,
4136 .set_status = wan_set_status,
4137};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004138
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004139static void wan_shutdown(void)
4140{
4141 /* Order firmware to save current state to NVRAM */
4142 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4143 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004144 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004145 else
4146 vdbg_printk(TPACPI_DBG_RFKILL,
4147 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004148}
4149
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004150static void wan_exit(void)
4151{
4152 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4153 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004154
4155 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4156
4157 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004158}
4159
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004160static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004161{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004162 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004163 int status = 0;
4164
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004165 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4166 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004167
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004168 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004169
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004170 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004171 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004172
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004173 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4174 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004175 str_supported(tp_features.wan),
4176 status);
4177
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004178#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4179 if (dbg_wwanemul) {
4180 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004181 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004182 } else
4183#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004184 if (tp_features.wan &&
4185 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4186 /* no wan hardware present in system */
4187 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004188 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004189 "wan hardware not installed\n");
4190 }
4191
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004192 if (!tp_features.wan)
4193 return 1;
4194
Johannes Berg19d337d2009-06-02 13:01:37 +02004195 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4196 &wan_tprfk_ops,
4197 RFKILL_TYPE_WWAN,
4198 TPACPI_RFK_WWAN_SW_NAME,
4199 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004200 if (res)
4201 return res;
4202
Johannes Berg19d337d2009-06-02 13:01:37 +02004203 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4204 &wan_attr_group);
4205
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004206 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004207 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004208 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004209 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004210
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004211 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004212}
4213
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004214/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004215static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004216{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004217 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004218}
4219
4220static int wan_write(char *buf)
4221{
Johannes Berg19d337d2009-06-02 13:01:37 +02004222 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004223}
4224
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004225static struct ibm_struct wan_driver_data = {
4226 .name = "wan",
4227 .read = wan_read,
4228 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004229 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004230 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004231};
4232
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004233/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004234 * UWB subdriver
4235 */
4236
4237enum {
4238 /* ACPI GUWB/SUWB bits */
4239 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4240 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4241};
4242
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004243#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4244
Johannes Berg19d337d2009-06-02 13:01:37 +02004245static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004246{
4247 int status;
4248
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004249#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4250 if (dbg_uwbemul)
4251 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004252 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004253#endif
4254
4255 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4256 return -EIO;
4257
4258 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004259 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004260}
4261
Johannes Berg19d337d2009-06-02 13:01:37 +02004262static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004263{
4264 int status;
4265
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004266 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004267 "will attempt to %s UWB\n",
4268 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004269
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004270#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4271 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004272 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004273 return 0;
4274 }
4275#endif
4276
Johannes Berg19d337d2009-06-02 13:01:37 +02004277 if (state == TPACPI_RFK_RADIO_ON)
4278 status = TP_ACPI_UWB_RADIOSSW;
4279 else
4280 status = 0;
4281
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004282 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4283 return -EIO;
4284
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004285 return 0;
4286}
4287
4288/* --------------------------------------------------------------------- */
4289
Johannes Berg19d337d2009-06-02 13:01:37 +02004290static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4291 .get_status = uwb_get_status,
4292 .set_status = uwb_set_status,
4293};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004294
4295static void uwb_exit(void)
4296{
Johannes Berg19d337d2009-06-02 13:01:37 +02004297 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004298}
4299
4300static int __init uwb_init(struct ibm_init_struct *iibm)
4301{
4302 int res;
4303 int status = 0;
4304
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004305 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4306 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004307
4308 TPACPI_ACPIHANDLE_INIT(hkey);
4309
4310 tp_features.uwb = hkey_handle &&
4311 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4312
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004313 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4314 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004315 str_supported(tp_features.uwb),
4316 status);
4317
4318#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4319 if (dbg_uwbemul) {
4320 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004321 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004322 } else
4323#endif
4324 if (tp_features.uwb &&
4325 !(status & TP_ACPI_UWB_HWPRESENT)) {
4326 /* no uwb hardware present in system */
4327 tp_features.uwb = 0;
4328 dbg_printk(TPACPI_DBG_INIT,
4329 "uwb hardware not installed\n");
4330 }
4331
4332 if (!tp_features.uwb)
4333 return 1;
4334
4335 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004336 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004337 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004338 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004339 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004340 return res;
4341}
4342
4343static struct ibm_struct uwb_driver_data = {
4344 .name = "uwb",
4345 .exit = uwb_exit,
4346 .flags.experimental = 1,
4347};
4348
4349/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004350 * Video subdriver
4351 */
4352
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004353#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4354
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004355enum video_access_mode {
4356 TPACPI_VIDEO_NONE = 0,
4357 TPACPI_VIDEO_570, /* 570 */
4358 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4359 TPACPI_VIDEO_NEW, /* all others */
4360};
4361
4362enum { /* video status flags, based on VIDEO_570 */
4363 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4364 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4365 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4366};
4367
4368enum { /* TPACPI_VIDEO_570 constants */
4369 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4370 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4371 * video_status_flags */
4372 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4373 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4374};
4375
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004376static enum video_access_mode video_supported;
4377static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004378
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004379static int video_autosw_get(void);
4380static int video_autosw_set(int enable);
4381
Joe Perches112a6ee2011-04-04 10:06:24 -07004382TPACPI_HANDLE(vid, root,
4383 "\\_SB.PCI.AGP.VGA", /* 570 */
4384 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4385 "\\_SB.PCI0.VID0", /* 770e */
4386 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4387 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4388 "\\_SB.PCI0.AGP.VID", /* all others */
4389 ); /* R30, R31 */
4390
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004391TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004392
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004393static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004395 int ivga;
4396
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004397 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4398
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004399 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004400 if (tpacpi_is_ibm())
4401 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004402
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004403 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4404 /* G41, assume IVGA doesn't change */
4405 vid_handle = vid2_handle;
4406
4407 if (!vid_handle)
4408 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004409 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004410 else if (tpacpi_is_ibm() &&
4411 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004412 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004413 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004414 else if (tpacpi_is_ibm() &&
4415 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004416 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004417 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004418 else
4419 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004420 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004422 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4423 str_supported(video_supported != TPACPI_VIDEO_NONE),
4424 video_supported);
4425
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004426 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427}
4428
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004429static void video_exit(void)
4430{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004431 dbg_printk(TPACPI_DBG_EXIT,
4432 "restoring original video autoswitch mode\n");
4433 if (video_autosw_set(video_orig_autosw))
Joe Perches0978e012011-04-04 10:06:25 -07004434 pr_err("error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004435 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004436}
4437
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004438static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439{
4440 int status = 0;
4441 int i;
4442
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004443 switch (video_supported) {
4444 case TPACPI_VIDEO_570:
4445 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4446 TP_ACPI_VIDEO_570_PHSCMD))
4447 return -EIO;
4448 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4449 break;
4450 case TPACPI_VIDEO_770:
4451 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4452 return -EIO;
4453 if (i)
4454 status |= TP_ACPI_VIDEO_S_LCD;
4455 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4456 return -EIO;
4457 if (i)
4458 status |= TP_ACPI_VIDEO_S_CRT;
4459 break;
4460 case TPACPI_VIDEO_NEW:
4461 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4462 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4463 return -EIO;
4464 if (i)
4465 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004467 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4468 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4469 return -EIO;
4470 if (i)
4471 status |= TP_ACPI_VIDEO_S_LCD;
4472 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4473 return -EIO;
4474 if (i)
4475 status |= TP_ACPI_VIDEO_S_DVI;
4476 break;
4477 default:
4478 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
4481 return status;
4482}
4483
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004484static int video_outputsw_set(int status)
4485{
4486 int autosw;
4487 int res = 0;
4488
4489 switch (video_supported) {
4490 case TPACPI_VIDEO_570:
4491 res = acpi_evalf(NULL, NULL,
4492 "\\_SB.PHS2", "vdd",
4493 TP_ACPI_VIDEO_570_PHS2CMD,
4494 status | TP_ACPI_VIDEO_570_PHS2SET);
4495 break;
4496 case TPACPI_VIDEO_770:
4497 autosw = video_autosw_get();
4498 if (autosw < 0)
4499 return autosw;
4500
4501 res = video_autosw_set(1);
4502 if (res)
4503 return res;
4504 res = acpi_evalf(vid_handle, NULL,
4505 "ASWT", "vdd", status * 0x100, 0);
4506 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004507 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004508 return -EIO;
4509 }
4510 break;
4511 case TPACPI_VIDEO_NEW:
4512 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004513 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004514 break;
4515 default:
4516 return -ENOSYS;
4517 }
4518
4519 return (res)? 0 : -EIO;
4520}
4521
4522static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004524 int autosw = 0;
4525
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004526 switch (video_supported) {
4527 case TPACPI_VIDEO_570:
4528 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4529 return -EIO;
4530 break;
4531 case TPACPI_VIDEO_770:
4532 case TPACPI_VIDEO_NEW:
4533 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4534 return -EIO;
4535 break;
4536 default:
4537 return -ENOSYS;
4538 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004539
4540 return autosw & 1;
4541}
4542
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004543static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004544{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004545 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004546 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004547 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004548}
4549
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004550static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004551{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004552 int autosw = video_autosw_get();
4553 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004554
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004555 if (autosw < 0)
4556 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004557
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004558 switch (video_supported) {
4559 case TPACPI_VIDEO_570:
4560 res = video_autosw_set(1);
4561 if (res)
4562 return res;
4563 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4564 break;
4565 case TPACPI_VIDEO_770:
4566 case TPACPI_VIDEO_NEW:
4567 res = video_autosw_set(1);
4568 if (res)
4569 return res;
4570 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4571 break;
4572 default:
4573 return -ENOSYS;
4574 }
4575 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004576 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004577 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004578 }
4579
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004580 return (res)? 0 : -EIO;
4581}
4582
4583static int video_expand_toggle(void)
4584{
4585 switch (video_supported) {
4586 case TPACPI_VIDEO_570:
4587 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4588 0 : -EIO;
4589 case TPACPI_VIDEO_770:
4590 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4591 0 : -EIO;
4592 case TPACPI_VIDEO_NEW:
4593 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4594 0 : -EIO;
4595 default:
4596 return -ENOSYS;
4597 }
4598 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004599}
4600
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004601static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004602{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004603 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004604
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004605 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004606 seq_printf(m, "status:\t\tnot supported\n");
4607 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004608 }
4609
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004610 /* Even reads can crash X.org, so... */
4611 if (!capable(CAP_SYS_ADMIN))
4612 return -EPERM;
4613
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004614 status = video_outputsw_get();
4615 if (status < 0)
4616 return status;
4617
4618 autosw = video_autosw_get();
4619 if (autosw < 0)
4620 return autosw;
4621
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004622 seq_printf(m, "status:\t\tsupported\n");
4623 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4624 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004625 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004626 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4627 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4628 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4629 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004630 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004631 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4632 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4633 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004634
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004635 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004636}
4637
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004638static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639{
4640 char *cmd;
4641 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004642 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004644 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004645 return -ENODEV;
4646
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004647 /* Even reads can crash X.org, let alone writes... */
4648 if (!capable(CAP_SYS_ADMIN))
4649 return -EPERM;
4650
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004651 enable = 0;
4652 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
4654 while ((cmd = next_cmd(&buf))) {
4655 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004656 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004658 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004660 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004662 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004663 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004664 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004665 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004666 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004667 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004668 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004670 res = video_autosw_set(1);
4671 if (res)
4672 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004674 res = video_autosw_set(0);
4675 if (res)
4676 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004678 res = video_outputsw_cycle();
4679 if (res)
4680 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004682 res = video_expand_toggle();
4683 if (res)
4684 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 } else
4686 return -EINVAL;
4687 }
4688
4689 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004690 status = video_outputsw_get();
4691 if (status < 0)
4692 return status;
4693 res = video_outputsw_set((status & ~disable) | enable);
4694 if (res)
4695 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 }
4697
4698 return 0;
4699}
4700
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004701static struct ibm_struct video_driver_data = {
4702 .name = "video",
4703 .read = video_read,
4704 .write = video_write,
4705 .exit = video_exit,
4706};
4707
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004708#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4709
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004710/*************************************************************************
4711 * Light (thinklight) subdriver
4712 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004714TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4715TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004716
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004717static int light_get_status(void)
4718{
4719 int status = 0;
4720
4721 if (tp_features.light_status) {
4722 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4723 return -EIO;
4724 return (!!status);
4725 }
4726
4727 return -ENXIO;
4728}
4729
4730static int light_set_status(int status)
4731{
4732 int rc;
4733
4734 if (tp_features.light) {
4735 if (cmos_handle) {
4736 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4737 (status)?
4738 TP_CMOS_THINKLIGHT_ON :
4739 TP_CMOS_THINKLIGHT_OFF);
4740 } else {
4741 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4742 (status)? 1 : 0);
4743 }
4744 return (rc)? 0 : -EIO;
4745 }
4746
4747 return -ENXIO;
4748}
4749
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004750static void light_set_status_worker(struct work_struct *work)
4751{
4752 struct tpacpi_led_classdev *data =
4753 container_of(work, struct tpacpi_led_classdev, work);
4754
4755 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004756 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004757}
4758
4759static void light_sysfs_set(struct led_classdev *led_cdev,
4760 enum led_brightness brightness)
4761{
4762 struct tpacpi_led_classdev *data =
4763 container_of(led_cdev,
4764 struct tpacpi_led_classdev,
4765 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004766 data->new_state = (brightness != LED_OFF) ?
4767 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004768 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004769}
4770
4771static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4772{
4773 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4774}
4775
4776static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4777 .led_classdev = {
4778 .name = "tpacpi::thinklight",
4779 .brightness_set = &light_sysfs_set,
4780 .brightness_get = &light_sysfs_get,
4781 }
4782};
4783
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004784static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004786 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004787
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004788 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4789
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004790 if (tpacpi_is_ibm()) {
4791 TPACPI_ACPIHANDLE_INIT(ledb);
4792 TPACPI_ACPIHANDLE_INIT(lght);
4793 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004794 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004795 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004796
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004797 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004798 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004799
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004800 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004801 /* light status not supported on
4802 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004803 tp_features.light_status =
4804 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004806 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4807 str_supported(tp_features.light),
4808 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004809
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004810 if (!tp_features.light)
4811 return 1;
4812
4813 rc = led_classdev_register(&tpacpi_pdev->dev,
4814 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004815
4816 if (rc < 0) {
4817 tp_features.light = 0;
4818 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004819 } else {
4820 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004821 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004822
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004823 return rc;
4824}
4825
4826static void light_exit(void)
4827{
4828 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4829 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004830 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831}
4832
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004833static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004835 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004837 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004838 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004839 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004840 seq_printf(m, "status:\t\tunknown\n");
4841 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004842 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004843 status = light_get_status();
4844 if (status < 0)
4845 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004846 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4847 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004850 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851}
4852
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004853static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004856 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004857
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004858 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004859 return -ENODEV;
4860
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 while ((cmd = next_cmd(&buf))) {
4862 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004863 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004865 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 } else
4867 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 }
4869
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004870 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871}
4872
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004873static struct ibm_struct light_driver_data = {
4874 .name = "light",
4875 .read = light_read,
4876 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004877 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004878};
4879
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004880/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004881 * CMOS subdriver
4882 */
4883
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004884/* sysfs cmos_command -------------------------------------------------- */
4885static ssize_t cmos_command_store(struct device *dev,
4886 struct device_attribute *attr,
4887 const char *buf, size_t count)
4888{
4889 unsigned long cmos_cmd;
4890 int res;
4891
4892 if (parse_strtoul(buf, 21, &cmos_cmd))
4893 return -EINVAL;
4894
4895 res = issue_thinkpad_cmos_command(cmos_cmd);
4896 return (res)? res : count;
4897}
4898
4899static struct device_attribute dev_attr_cmos_command =
4900 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4901
4902/* --------------------------------------------------------------------- */
4903
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004904static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004905{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004906 int res;
4907
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004908 vdbg_printk(TPACPI_DBG_INIT,
4909 "initializing cmos commands subdriver\n");
4910
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004911 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004912
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004913 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4914 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004915
4916 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4917 if (res)
4918 return res;
4919
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004920 return (cmos_handle)? 0 : 1;
4921}
4922
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004923static void cmos_exit(void)
4924{
4925 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4926}
4927
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004928static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004930 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4931 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004933 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004935 seq_printf(m, "status:\t\tsupported\n");
4936 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 }
4938
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004939 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940}
4941
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004942static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943{
4944 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004945 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946
4947 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004948 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4949 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 /* cmos_cmd set */
4951 } else
4952 return -EINVAL;
4953
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004954 res = issue_thinkpad_cmos_command(cmos_cmd);
4955 if (res)
4956 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 }
4958
4959 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004960}
4961
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004962static struct ibm_struct cmos_driver_data = {
4963 .name = "cmos",
4964 .read = cmos_read,
4965 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004966 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004967};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004968
4969/*************************************************************************
4970 * LED subdriver
4971 */
4972
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004973enum led_access_mode {
4974 TPACPI_LED_NONE = 0,
4975 TPACPI_LED_570, /* 570 */
4976 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4977 TPACPI_LED_NEW, /* all others */
4978};
4979
4980enum { /* For TPACPI_LED_OLD */
4981 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4982 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4983 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4984};
4985
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004986static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004987
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03004988static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004989
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004990#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004991static struct tpacpi_led_classdev *tpacpi_leds;
4992static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004993static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004994 /* there's a limit of 19 chars + NULL before 2.6.26 */
4995 "tpacpi::power",
4996 "tpacpi:orange:batt",
4997 "tpacpi:green:batt",
4998 "tpacpi::dock_active",
4999 "tpacpi::bay_active",
5000 "tpacpi::dock_batt",
5001 "tpacpi::unknown_led",
5002 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005003 "tpacpi::dock_status1",
5004 "tpacpi::dock_status2",
5005 "tpacpi::unknown_led2",
5006 "tpacpi::unknown_led3",
5007 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005008};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005009#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005010
5011static inline bool tpacpi_is_led_restricted(const unsigned int led)
5012{
5013#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5014 return false;
5015#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005016 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005017#endif
5018}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005019
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005020static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005021{
5022 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005023 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005024
5025 switch (led_supported) {
5026 case TPACPI_LED_570:
5027 if (!acpi_evalf(ec_handle,
5028 &status, "GLED", "dd", 1 << led))
5029 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005030 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005031 TPACPI_LED_OFF :
5032 ((status == 1)?
5033 TPACPI_LED_ON :
5034 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005035 tpacpi_led_state_cache[led] = led_s;
5036 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005037 default:
5038 return -ENXIO;
5039 }
5040
5041 /* not reached */
5042}
5043
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005044static int led_set_status(const unsigned int led,
5045 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005046{
5047 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005048 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5049 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005050
5051 int rc = 0;
5052
5053 switch (led_supported) {
5054 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005055 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005056 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005057 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005058 if (unlikely(tpacpi_is_led_restricted(led)))
5059 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005060 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5061 (1 << led), led_sled_arg1[ledstatus]))
5062 rc = -EIO;
5063 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005064 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005065 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005066 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005067 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005068 if (unlikely(tpacpi_is_led_restricted(led)))
5069 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005070 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5071 if (rc >= 0)
5072 rc = ec_write(TPACPI_LED_EC_HLBL,
5073 (ledstatus == TPACPI_LED_BLINK) << led);
5074 if (rc >= 0)
5075 rc = ec_write(TPACPI_LED_EC_HLCL,
5076 (ledstatus != TPACPI_LED_OFF) << led);
5077 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005078 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005079 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005080 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5081 return -EINVAL;
5082 if (unlikely(tpacpi_is_led_restricted(led)))
5083 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005084 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5085 led, led_led_arg1[ledstatus]))
5086 rc = -EIO;
5087 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005088 default:
5089 rc = -ENXIO;
5090 }
5091
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005092 if (!rc)
5093 tpacpi_led_state_cache[led] = ledstatus;
5094
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005095 return rc;
5096}
5097
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005098static void led_set_status_worker(struct work_struct *work)
5099{
5100 struct tpacpi_led_classdev *data =
5101 container_of(work, struct tpacpi_led_classdev, work);
5102
5103 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005104 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005105}
5106
5107static void led_sysfs_set(struct led_classdev *led_cdev,
5108 enum led_brightness brightness)
5109{
5110 struct tpacpi_led_classdev *data = container_of(led_cdev,
5111 struct tpacpi_led_classdev, led_classdev);
5112
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005113 if (brightness == LED_OFF)
5114 data->new_state = TPACPI_LED_OFF;
5115 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5116 data->new_state = TPACPI_LED_ON;
5117 else
5118 data->new_state = TPACPI_LED_BLINK;
5119
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005120 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005121}
5122
5123static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5124 unsigned long *delay_on, unsigned long *delay_off)
5125{
5126 struct tpacpi_led_classdev *data = container_of(led_cdev,
5127 struct tpacpi_led_classdev, led_classdev);
5128
5129 /* Can we choose the flash rate? */
5130 if (*delay_on == 0 && *delay_off == 0) {
5131 /* yes. set them to the hardware blink rate (1 Hz) */
5132 *delay_on = 500; /* ms */
5133 *delay_off = 500; /* ms */
5134 } else if ((*delay_on != 500) || (*delay_off != 500))
5135 return -EINVAL;
5136
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005137 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005138 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005139
5140 return 0;
5141}
5142
5143static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5144{
5145 int rc;
5146
5147 struct tpacpi_led_classdev *data = container_of(led_cdev,
5148 struct tpacpi_led_classdev, led_classdev);
5149
5150 rc = led_get_status(data->led);
5151
5152 if (rc == TPACPI_LED_OFF || rc < 0)
5153 rc = LED_OFF; /* no error handling in led class :( */
5154 else
5155 rc = LED_FULL;
5156
5157 return rc;
5158}
5159
5160static void led_exit(void)
5161{
5162 unsigned int i;
5163
5164 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5165 if (tpacpi_leds[i].led_classdev.name)
5166 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5167 }
5168
5169 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005170}
5171
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005172static int __init tpacpi_init_led(unsigned int led)
5173{
5174 int rc;
5175
5176 tpacpi_leds[led].led = led;
5177
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005178 /* LEDs with no name don't get registered */
5179 if (!tpacpi_led_names[led])
5180 return 0;
5181
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005182 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5183 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5184 if (led_supported == TPACPI_LED_570)
5185 tpacpi_leds[led].led_classdev.brightness_get =
5186 &led_sysfs_get;
5187
5188 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5189
5190 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5191
5192 rc = led_classdev_register(&tpacpi_pdev->dev,
5193 &tpacpi_leds[led].led_classdev);
5194 if (rc < 0)
5195 tpacpi_leds[led].led_classdev.name = NULL;
5196
5197 return rc;
5198}
5199
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005200static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5201 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5202 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5203 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5204
5205 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5206 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5207 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5208 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5209 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5210 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5211 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5212 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5213
5214 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5215 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5216 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5217 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5218 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5219
5220 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5221 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5222 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5223 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5224
5225 /* (1) - may have excess leds enabled on MSB */
5226
5227 /* Defaults (order matters, keep last, don't reorder!) */
5228 { /* Lenovo */
5229 .vendor = PCI_VENDOR_ID_LENOVO,
5230 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5231 .quirks = 0x1fffU,
5232 },
5233 { /* IBM ThinkPads with no EC version string */
5234 .vendor = PCI_VENDOR_ID_IBM,
5235 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5236 .quirks = 0x00ffU,
5237 },
5238 { /* IBM ThinkPads with EC version string */
5239 .vendor = PCI_VENDOR_ID_IBM,
5240 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5241 .quirks = 0x00bfU,
5242 },
5243};
5244
5245#undef TPACPI_LEDQ_IBM
5246#undef TPACPI_LEDQ_LNV
5247
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005248static enum led_access_mode __init led_init_detect_mode(void)
5249{
5250 acpi_status status;
5251
5252 if (tpacpi_is_ibm()) {
5253 /* 570 */
5254 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5255 if (ACPI_SUCCESS(status))
5256 return TPACPI_LED_570;
5257
5258 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5259 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5260 if (ACPI_SUCCESS(status))
5261 return TPACPI_LED_OLD;
5262 }
5263
5264 /* most others */
5265 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5266 if (ACPI_SUCCESS(status))
5267 return TPACPI_LED_NEW;
5268
5269 /* R30, R31, and unknown firmwares */
5270 led_handle = NULL;
5271 return TPACPI_LED_NONE;
5272}
5273
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005274static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005275{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005276 unsigned int i;
5277 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005278 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005279
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005280 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5281
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005282 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005283
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005284 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5285 str_supported(led_supported), led_supported);
5286
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005287 if (led_supported == TPACPI_LED_NONE)
5288 return 1;
5289
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005290 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5291 GFP_KERNEL);
5292 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005293 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005294 return -ENOMEM;
5295 }
5296
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005297 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5298 ARRAY_SIZE(led_useful_qtable));
5299
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005300 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005301 if (!tpacpi_is_led_restricted(i) &&
5302 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005303 rc = tpacpi_init_led(i);
5304 if (rc < 0) {
5305 led_exit();
5306 return rc;
5307 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005308 }
5309 }
5310
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005311#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Joe Perches0978e012011-04-04 10:06:25 -07005312 pr_notice("warning: userspace override of important "
5313 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005314#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005315 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005316}
5317
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005318#define str_led_status(s) \
5319 ((s) == TPACPI_LED_OFF ? "off" : \
5320 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005321
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005322static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005324 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005325 seq_printf(m, "status:\t\tnot supported\n");
5326 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005327 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005328 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005329
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005330 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005331 /* 570 */
5332 int i, status;
5333 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005334 status = led_get_status(i);
5335 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005336 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005337 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005338 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005339 }
5340 }
5341
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005342 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005343 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346}
5347
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005348static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349{
5350 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005351 int led, rc;
5352 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005353
5354 if (!led_supported)
5355 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356
5357 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005358 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 return -EINVAL;
5360
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005361 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005362 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005364 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005365 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005366 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005367 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005368 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005369 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005370
5371 rc = led_set_status(led, s);
5372 if (rc < 0)
5373 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 }
5375
5376 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005377}
5378
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005379static struct ibm_struct led_driver_data = {
5380 .name = "led",
5381 .read = led_read,
5382 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005383 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005384};
5385
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005386/*************************************************************************
5387 * Beep subdriver
5388 */
5389
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005390TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005391
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005392#define TPACPI_BEEP_Q1 0x0001
5393
5394static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5395 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5396 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5397};
5398
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005399static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005400{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005401 unsigned long quirks;
5402
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005403 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5404
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005405 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005406
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005407 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5408 str_supported(beep_handle != NULL));
5409
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005410 quirks = tpacpi_check_quirks(beep_quirk_table,
5411 ARRAY_SIZE(beep_quirk_table));
5412
5413 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5414
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005415 return (beep_handle)? 0 : 1;
5416}
5417
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005418static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005420 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005421 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005422 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005423 seq_printf(m, "status:\t\tsupported\n");
5424 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428}
5429
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005430static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431{
5432 char *cmd;
5433 int beep_cmd;
5434
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005435 if (!beep_handle)
5436 return -ENODEV;
5437
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005439 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5440 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 /* beep_cmd set */
5442 } else
5443 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005444 if (tp_features.beep_needs_two_args) {
5445 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5446 beep_cmd, 0))
5447 return -EIO;
5448 } else {
5449 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5450 beep_cmd))
5451 return -EIO;
5452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 }
5454
5455 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005456}
5457
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005458static struct ibm_struct beep_driver_data = {
5459 .name = "beep",
5460 .read = beep_read,
5461 .write = beep_write,
5462};
5463
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005464/*************************************************************************
5465 * Thermal subdriver
5466 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005467
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005468enum thermal_access_mode {
5469 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5470 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5471 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5472 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5473 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5474};
5475
5476enum { /* TPACPI_THERMAL_TPEC_* */
5477 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5478 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5479 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005480
5481 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005482};
5483
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005484
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005485#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5486struct ibm_thermal_sensors_struct {
5487 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5488};
5489
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005490static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005491
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005492/* idx is zero-based */
5493static int thermal_get_sensor(int idx, s32 *value)
5494{
5495 int t;
5496 s8 tmp;
5497 char tmpi[5];
5498
5499 t = TP_EC_THERMAL_TMP0;
5500
5501 switch (thermal_read_mode) {
5502#if TPACPI_MAX_THERMAL_SENSORS >= 16
5503 case TPACPI_THERMAL_TPEC_16:
5504 if (idx >= 8 && idx <= 15) {
5505 t = TP_EC_THERMAL_TMP8;
5506 idx -= 8;
5507 }
5508 /* fallthrough */
5509#endif
5510 case TPACPI_THERMAL_TPEC_8:
5511 if (idx <= 7) {
5512 if (!acpi_ec_read(t + idx, &tmp))
5513 return -EIO;
5514 *value = tmp * 1000;
5515 return 0;
5516 }
5517 break;
5518
5519 case TPACPI_THERMAL_ACPI_UPDT:
5520 if (idx <= 7) {
5521 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5522 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5523 return -EIO;
5524 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5525 return -EIO;
5526 *value = (t - 2732) * 100;
5527 return 0;
5528 }
5529 break;
5530
5531 case TPACPI_THERMAL_ACPI_TMP07:
5532 if (idx <= 7) {
5533 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5534 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5535 return -EIO;
5536 if (t > 127 || t < -127)
5537 t = TP_EC_THERMAL_TMP_NA;
5538 *value = t * 1000;
5539 return 0;
5540 }
5541 break;
5542
5543 case TPACPI_THERMAL_NONE:
5544 default:
5545 return -ENOSYS;
5546 }
5547
5548 return -EINVAL;
5549}
5550
5551static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5552{
5553 int res, i;
5554 int n;
5555
5556 n = 8;
5557 i = 0;
5558
5559 if (!s)
5560 return -EINVAL;
5561
5562 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5563 n = 16;
5564
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005565 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005566 res = thermal_get_sensor(i, &s->temp[i]);
5567 if (res)
5568 return res;
5569 }
5570
5571 return n;
5572}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005573
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005574static void thermal_dump_all_sensors(void)
5575{
5576 int n, i;
5577 struct ibm_thermal_sensors_struct t;
5578
5579 n = thermal_get_sensors(&t);
5580 if (n <= 0)
5581 return;
5582
Joe Perches0978e012011-04-04 10:06:25 -07005583 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005584
5585 for (i = 0; i < n; i++) {
5586 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07005587 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005588 else
Joe Perches0978e012011-04-04 10:06:25 -07005589 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005590 }
5591
Joe Perches0978e012011-04-04 10:06:25 -07005592 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005593}
5594
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005595/* sysfs temp##_input -------------------------------------------------- */
5596
5597static ssize_t thermal_temp_input_show(struct device *dev,
5598 struct device_attribute *attr,
5599 char *buf)
5600{
5601 struct sensor_device_attribute *sensor_attr =
5602 to_sensor_dev_attr(attr);
5603 int idx = sensor_attr->index;
5604 s32 value;
5605 int res;
5606
5607 res = thermal_get_sensor(idx, &value);
5608 if (res)
5609 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005610 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005611 return -ENXIO;
5612
5613 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5614}
5615
5616#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005617 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5618 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005619
5620static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5621 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5622 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5623 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5624 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5625 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5626 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5627 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5628 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5629 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5630 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5631 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5632 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5633 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5634 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5635 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5636 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5637};
5638
5639#define THERMAL_ATTRS(X) \
5640 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5641
5642static struct attribute *thermal_temp_input_attr[] = {
5643 THERMAL_ATTRS(8),
5644 THERMAL_ATTRS(9),
5645 THERMAL_ATTRS(10),
5646 THERMAL_ATTRS(11),
5647 THERMAL_ATTRS(12),
5648 THERMAL_ATTRS(13),
5649 THERMAL_ATTRS(14),
5650 THERMAL_ATTRS(15),
5651 THERMAL_ATTRS(0),
5652 THERMAL_ATTRS(1),
5653 THERMAL_ATTRS(2),
5654 THERMAL_ATTRS(3),
5655 THERMAL_ATTRS(4),
5656 THERMAL_ATTRS(5),
5657 THERMAL_ATTRS(6),
5658 THERMAL_ATTRS(7),
5659 NULL
5660};
5661
5662static const struct attribute_group thermal_temp_input16_group = {
5663 .attrs = thermal_temp_input_attr
5664};
5665
5666static const struct attribute_group thermal_temp_input8_group = {
5667 .attrs = &thermal_temp_input_attr[8]
5668};
5669
5670#undef THERMAL_SENSOR_ATTR_TEMP
5671#undef THERMAL_ATTRS
5672
5673/* --------------------------------------------------------------------- */
5674
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005675static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005676{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005677 u8 t, ta1, ta2;
5678 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005679 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005680 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005681
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005682 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5683
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005684 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005685
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005686 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005687 /*
5688 * Direct EC access mode: sensors at registers
5689 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5690 * non-implemented, thermal sensors return 0x80 when
5691 * not available
5692 */
5693
5694 ta1 = ta2 = 0;
5695 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005696 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005697 ta1 |= t;
5698 } else {
5699 ta1 = 0;
5700 break;
5701 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005702 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005703 ta2 |= t;
5704 } else {
5705 ta1 = 0;
5706 break;
5707 }
5708 }
5709 if (ta1 == 0) {
5710 /* This is sheer paranoia, but we handle it anyway */
5711 if (acpi_tmp7) {
Joe Perches0978e012011-04-04 10:06:25 -07005712 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005713 "falling back to ACPI TMPx access "
5714 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005715 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005716 } else {
Joe Perches0978e012011-04-04 10:06:25 -07005717 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005718 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005719 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005720 }
5721 } else {
5722 thermal_read_mode =
5723 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005724 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005725 }
5726 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005727 if (tpacpi_is_ibm() &&
5728 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005729 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005730 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005731 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005732 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005733 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005734 }
5735 } else {
5736 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005737 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005738 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005739
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005740 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5741 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5742 thermal_read_mode);
5743
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005744 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005745 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005746 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005747 &thermal_temp_input16_group);
5748 if (res)
5749 return res;
5750 break;
5751 case TPACPI_THERMAL_TPEC_8:
5752 case TPACPI_THERMAL_ACPI_TMP07:
5753 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005754 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005755 &thermal_temp_input8_group);
5756 if (res)
5757 return res;
5758 break;
5759 case TPACPI_THERMAL_NONE:
5760 default:
5761 return 1;
5762 }
5763
5764 return 0;
5765}
5766
5767static void thermal_exit(void)
5768{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005769 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005770 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005771 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005772 &thermal_temp_input16_group);
5773 break;
5774 case TPACPI_THERMAL_TPEC_8:
5775 case TPACPI_THERMAL_ACPI_TMP07:
5776 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005777 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08005778 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005779 break;
5780 case TPACPI_THERMAL_NONE:
5781 default:
5782 break;
5783 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005784}
5785
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005786static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005787{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005788 int n, i;
5789 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005790
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005791 n = thermal_get_sensors(&t);
5792 if (unlikely(n < 0))
5793 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005794
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005795 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005796
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005797 if (n > 0) {
5798 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005799 seq_printf(m, "%d ", t.temp[i] / 1000);
5800 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005801 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005802 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005803
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005804 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005805}
5806
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005807static struct ibm_struct thermal_driver_data = {
5808 .name = "thermal",
5809 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005810 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005811};
5812
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005813/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005814 * Backlight/brightness subdriver
5815 */
Holger Macht8acb0252006-10-20 14:30:28 -07005816
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005817#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5818
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005819/*
5820 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5821 * CMOS NVRAM byte 0x5E, bits 0-3.
5822 *
5823 * EC HBRV (0x31) has the following layout
5824 * Bit 7: unknown function
5825 * Bit 6: unknown function
5826 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5827 * Bit 4: must be set to zero to avoid problems
5828 * Bit 3-0: backlight brightness level
5829 *
5830 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005831 *
5832 * WARNING: The X61 has been verified to use HBRV for something else, so
5833 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5834 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005835 */
5836
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005837enum {
5838 TP_EC_BACKLIGHT = 0x31,
5839
5840 /* TP_EC_BACKLIGHT bitmasks */
5841 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5842 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5843 TP_EC_BACKLIGHT_MAPSW = 0x20,
5844};
5845
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005846enum tpacpi_brightness_access_mode {
5847 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5848 TPACPI_BRGHT_MODE_EC, /* EC control */
5849 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5850 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5851 TPACPI_BRGHT_MODE_MAX
5852};
5853
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005854static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005855
5856static enum tpacpi_brightness_access_mode brightness_mode =
5857 TPACPI_BRGHT_MODE_MAX;
5858
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005859static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5860
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005861static struct mutex brightness_mutex;
5862
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005863/* NVRAM brightness access,
5864 * call with brightness_mutex held! */
5865static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005866{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005867 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005868
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005869 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5870 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5871 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03005872 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005873
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005874 return lnvram;
5875}
5876
5877static void tpacpi_brightness_checkpoint_nvram(void)
5878{
5879 u8 lec = 0;
5880 u8 b_nvram;
5881
5882 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5883 return;
5884
5885 vdbg_printk(TPACPI_DBG_BRGHT,
5886 "trying to checkpoint backlight level to NVRAM...\n");
5887
5888 if (mutex_lock_killable(&brightness_mutex) < 0)
5889 return;
5890
5891 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5892 goto unlock;
5893 lec &= TP_EC_BACKLIGHT_LVLMSK;
5894 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5895
5896 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5897 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5898 /* NVRAM needs update */
5899 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5900 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5901 b_nvram |= lec;
5902 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5903 dbg_printk(TPACPI_DBG_BRGHT,
5904 "updated NVRAM backlight level to %u (0x%02x)\n",
5905 (unsigned int) lec, (unsigned int) b_nvram);
5906 } else
5907 vdbg_printk(TPACPI_DBG_BRGHT,
5908 "NVRAM backlight level already is %u (0x%02x)\n",
5909 (unsigned int) lec, (unsigned int) b_nvram);
5910
5911unlock:
5912 mutex_unlock(&brightness_mutex);
5913}
5914
5915
5916/* call with brightness_mutex held! */
5917static int tpacpi_brightness_get_raw(int *status)
5918{
5919 u8 lec = 0;
5920
5921 switch (brightness_mode) {
5922 case TPACPI_BRGHT_MODE_UCMS_STEP:
5923 *status = tpacpi_brightness_nvram_get();
5924 return 0;
5925 case TPACPI_BRGHT_MODE_EC:
5926 case TPACPI_BRGHT_MODE_ECNVRAM:
5927 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005928 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005929 *status = lec;
5930 return 0;
5931 default:
5932 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005933 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005934}
5935
5936/* call with brightness_mutex held! */
5937/* do NOT call with illegal backlight level value */
5938static int tpacpi_brightness_set_ec(unsigned int value)
5939{
5940 u8 lec = 0;
5941
5942 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5943 return -EIO;
5944
5945 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5946 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5947 (value & TP_EC_BACKLIGHT_LVLMSK))))
5948 return -EIO;
5949
5950 return 0;
5951}
5952
5953/* call with brightness_mutex held! */
5954static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5955{
5956 int cmos_cmd, inc;
5957 unsigned int current_value, i;
5958
5959 current_value = tpacpi_brightness_nvram_get();
5960
5961 if (value == current_value)
5962 return 0;
5963
5964 cmos_cmd = (value > current_value) ?
5965 TP_CMOS_BRIGHTNESS_UP :
5966 TP_CMOS_BRIGHTNESS_DOWN;
5967 inc = (value > current_value) ? 1 : -1;
5968
5969 for (i = current_value; i != value; i += inc)
5970 if (issue_thinkpad_cmos_command(cmos_cmd))
5971 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005972
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005973 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005974}
5975
5976/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005977static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005978{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005979 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005980
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03005981 if (value > bright_maxlvl || value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005982 return -EINVAL;
5983
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005984 vdbg_printk(TPACPI_DBG_BRGHT,
5985 "set backlight level to %d\n", value);
5986
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02005987 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005988 if (res < 0)
5989 return res;
5990
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005991 switch (brightness_mode) {
5992 case TPACPI_BRGHT_MODE_EC:
5993 case TPACPI_BRGHT_MODE_ECNVRAM:
5994 res = tpacpi_brightness_set_ec(value);
5995 break;
5996 case TPACPI_BRGHT_MODE_UCMS_STEP:
5997 res = tpacpi_brightness_set_ucmsstep(value);
5998 break;
5999 default:
6000 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006001 }
6002
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006003 mutex_unlock(&brightness_mutex);
6004 return res;
6005}
6006
6007/* sysfs backlight class ----------------------------------------------- */
6008
6009static int brightness_update_status(struct backlight_device *bd)
6010{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006011 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006012 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6013 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006014 bd->props.brightness : 0;
6015
6016 dbg_printk(TPACPI_DBG_BRGHT,
6017 "backlight: attempt to set level to %d\n",
6018 level);
6019
6020 /* it is the backlight class's job (caller) to handle
6021 * EINTR and other errors properly */
6022 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006023}
Holger Macht8acb0252006-10-20 14:30:28 -07006024
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006025static int brightness_get(struct backlight_device *bd)
6026{
6027 int status, res;
6028
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006029 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006030 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006031 return 0;
6032
6033 res = tpacpi_brightness_get_raw(&status);
6034
6035 mutex_unlock(&brightness_mutex);
6036
6037 if (res < 0)
6038 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006039
6040 return status & TP_EC_BACKLIGHT_LVLMSK;
6041}
6042
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006043static void tpacpi_brightness_notify_change(void)
6044{
6045 backlight_force_update(ibm_backlight_device,
6046 BACKLIGHT_UPDATE_HOTKEY);
6047}
6048
Lionel Debrouxacc24722010-11-16 14:14:02 +01006049static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006050 .get_brightness = brightness_get,
6051 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006052};
6053
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006054/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006055
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006056/*
6057 * Call _BCL method of video device. On some ThinkPads this will
6058 * switch the firmware to the ACPI brightness control mode.
6059 */
6060
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006061static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6062{
6063 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6064 union acpi_object *obj;
6065 int rc;
6066
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006067 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006068 obj = (union acpi_object *)buffer.pointer;
6069 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006070 pr_err("Unknown _BCL data, please report this to %s\n",
6071 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006072 rc = 0;
6073 } else {
6074 rc = obj->package.count;
6075 }
6076 } else {
6077 return 0;
6078 }
6079
6080 kfree(buffer.pointer);
6081 return rc;
6082}
6083
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006084
6085/*
6086 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6087 */
6088static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6089{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006090 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006091 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006092
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006093 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6094 if (video_device)
6095 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006096
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006097 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006098
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006099 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006100}
6101
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006102/*
6103 * These are only useful for models that have only one possibility
6104 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6105 * these quirks.
6106 */
6107#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6108#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6109#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6110
6111static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6112 /* Models with ATI GPUs known to require ECNVRAM mode */
6113 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6114
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006115 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006116 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006117 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006118 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006119 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6120
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006121 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006122 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006123 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6124 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006125
6126 /* Models with Intel GMA900 */
6127 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6128 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6129 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6130};
6131
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006132/*
6133 * Returns < 0 for error, otherwise sets tp_features.bright_*
6134 * and bright_maxlvl.
6135 */
6136static void __init tpacpi_detect_brightness_capabilities(void)
6137{
6138 unsigned int b;
6139
6140 vdbg_printk(TPACPI_DBG_INIT,
6141 "detecting firmware brightness interface capabilities\n");
6142
6143 /* we could run a quirks check here (same table used by
6144 * brightness_init) if needed */
6145
6146 /*
6147 * We always attempt to detect acpi support, so as to switch
6148 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6149 * going to publish a backlight interface
6150 */
6151 b = tpacpi_check_std_acpi_brightness_support();
6152 switch (b) {
6153 case 16:
6154 bright_maxlvl = 15;
Joe Perches0978e012011-04-04 10:06:25 -07006155 pr_info("detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006156 break;
6157 case 8:
6158 case 0:
6159 bright_maxlvl = 7;
Joe Perches0978e012011-04-04 10:06:25 -07006160 pr_info("detected a 8-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006161 break;
6162 default:
Joe Perches0978e012011-04-04 10:06:25 -07006163 pr_err("Unsupported brightness interface, "
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006164 "please contact %s\n", TPACPI_MAIL);
6165 tp_features.bright_unkfw = 1;
6166 bright_maxlvl = b - 1;
6167 }
6168}
6169
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006170static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006171{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006172 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006173 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006174 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006175
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006176 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6177
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006178 mutex_init(&brightness_mutex);
6179
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006180 quirks = tpacpi_check_quirks(brightness_quirk_table,
6181 ARRAY_SIZE(brightness_quirk_table));
6182
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006183 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006184
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006185 /* if it is unknown, we don't handle it: it wouldn't be safe */
6186 if (tp_features.bright_unkfw)
6187 return 1;
6188
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006189 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006190 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006191 "brightness support disabled by "
6192 "module parameter\n");
6193 return 1;
6194 }
6195
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006196 if (acpi_video_backlight_support()) {
6197 if (brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006198 pr_info("Standard ACPI backlight interface "
6199 "available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006200 return 1;
6201 } else if (brightness_enable == 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006202 pr_warn("Cannot enable backlight brightness support, "
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006203 "ACPI is already handling it. Refer to the "
Joe Perches0978e012011-04-04 10:06:25 -07006204 "acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006205 return 1;
6206 }
6207 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006208 pr_notice("Standard ACPI backlight interface not "
6209 "available, thinkpad_acpi native "
6210 "brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006211 }
6212
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006213 /*
6214 * Check for module parameter bogosity, note that we
6215 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6216 * able to detect "unspecified"
6217 */
6218 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006219 return -EINVAL;
6220
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006221 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6222 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6223 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006224 if (quirks & TPACPI_BRGHT_Q_EC)
6225 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6226 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006227 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6228
6229 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006230 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006231 brightness_mode);
6232 }
6233
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006234 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006235 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006236 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6237 brightness_mode == TPACPI_BRGHT_MODE_EC))
6238 return -EINVAL;
6239
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006240 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006241 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006242
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006243 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006244 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006245 props.max_brightness = bright_maxlvl;
6246 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006247 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6248 NULL, NULL,
6249 &ibm_backlight_data,
6250 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006251 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006252 int rc = PTR_ERR(ibm_backlight_device);
6253 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006254 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006255 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006256 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006257 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6258 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006259
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006260 if (quirks & TPACPI_BRGHT_Q_ASK) {
Joe Perches0978e012011-04-04 10:06:25 -07006261 pr_notice("brightness: will use unverified default: "
6262 "brightness_mode=%d\n", brightness_mode);
6263 pr_notice("brightness: please report to %s whether it works well "
6264 "or not on your ThinkPad\n", TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006265 }
6266
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006267 /* Added by mistake in early 2007. Probably useless, but it could
6268 * be working around some unknown firmware problem where the value
6269 * read at startup doesn't match the real hardware state... so leave
6270 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006271 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006272
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006273 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6274 "brightness: registering brightness hotkeys "
6275 "as change notification\n");
6276 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6277 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006278 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006279 return 0;
6280}
6281
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006282static void brightness_suspend(pm_message_t state)
6283{
6284 tpacpi_brightness_checkpoint_nvram();
6285}
6286
6287static void brightness_shutdown(void)
6288{
6289 tpacpi_brightness_checkpoint_nvram();
6290}
6291
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006292static void brightness_exit(void)
6293{
6294 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006295 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006296 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006297 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006298 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006299
6300 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006301}
6302
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006303static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006304{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006305 int level;
6306
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006307 level = brightness_get(NULL);
6308 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006309 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006310 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006311 seq_printf(m, "level:\t\t%d\n", level);
6312 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006313 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6314 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006315 }
6316
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006317 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006318}
6319
6320static int brightness_write(char *buf)
6321{
6322 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006323 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006324 char *cmd;
6325
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006326 level = brightness_get(NULL);
6327 if (level < 0)
6328 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006329
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006330 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006331 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006332 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006333 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006334 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006335 if (level > 0)
6336 level--;
6337 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006338 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006339 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006340 } else
6341 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006342 }
6343
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006344 tpacpi_disclose_usertask("procfs brightness",
6345 "set level to %d\n", level);
6346
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006347 /*
6348 * Now we know what the final level should be, so we try to set it.
6349 * Doing it this way makes the syscall restartable in case of EINTR
6350 */
6351 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006352 if (!rc && ibm_backlight_device)
6353 backlight_force_update(ibm_backlight_device,
6354 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006355 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006356}
6357
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006358static struct ibm_struct brightness_driver_data = {
6359 .name = "brightness",
6360 .read = brightness_read,
6361 .write = brightness_write,
6362 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006363 .suspend = brightness_suspend,
6364 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006365};
6366
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006367/*************************************************************************
6368 * Volume subdriver
6369 */
6370
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006371/*
6372 * IBM ThinkPads have a simple volume controller with MUTE gating.
6373 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6374 *
6375 * Since the *61 series (and probably also the later *60 series), Lenovo
6376 * ThinkPads only implement the MUTE gate.
6377 *
6378 * EC register 0x30
6379 * Bit 6: MUTE (1 mutes sound)
6380 * Bit 3-0: Volume
6381 * Other bits should be zero as far as we know.
6382 *
6383 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6384 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6385 * such as bit 7 which is used to detect repeated presses of MUTE,
6386 * and we leave them unchanged.
6387 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006388
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006389#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6390
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006391#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6392#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6393#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6394
Henrique de Moraes Holschuhead510c2009-12-26 22:52:13 -02006395static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006396static char *alsa_id = "ThinkPadEC";
6397static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6398
6399struct tpacpi_alsa_data {
6400 struct snd_card *card;
6401 struct snd_ctl_elem_id *ctl_mute_id;
6402 struct snd_ctl_elem_id *ctl_vol_id;
6403};
6404
6405static struct snd_card *alsa_card;
6406
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006407enum {
6408 TP_EC_AUDIO = 0x30,
6409
6410 /* TP_EC_AUDIO bits */
6411 TP_EC_AUDIO_MUTESW = 6,
6412
6413 /* TP_EC_AUDIO bitmasks */
6414 TP_EC_AUDIO_LVL_MSK = 0x0F,
6415 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6416
6417 /* Maximum volume */
6418 TP_EC_VOLUME_MAX = 14,
6419};
6420
6421enum tpacpi_volume_access_mode {
6422 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6423 TPACPI_VOL_MODE_EC, /* Pure EC control */
6424 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6425 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6426 TPACPI_VOL_MODE_MAX
6427};
6428
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006429enum tpacpi_volume_capabilities {
6430 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6431 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6432 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6433 TPACPI_VOL_CAP_MAX
6434};
6435
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006436static enum tpacpi_volume_access_mode volume_mode =
6437 TPACPI_VOL_MODE_MAX;
6438
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006439static enum tpacpi_volume_capabilities volume_capabilities;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006440static int volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006441
6442/*
6443 * Used to syncronize writers to TP_EC_AUDIO and
6444 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6445 */
6446static struct mutex volume_mutex;
6447
6448static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006449{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006450 u8 lec = 0;
6451 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006452 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006453
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006454 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6455 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006456 if (!volume_control_allowed)
6457 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006458
6459 vdbg_printk(TPACPI_DBG_MIXER,
6460 "trying to checkpoint mixer state to NVRAM...\n");
6461
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006462 if (tp_features.mixer_no_level_control)
6463 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6464 else
6465 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6466
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006467 if (mutex_lock_killable(&volume_mutex) < 0)
6468 return;
6469
6470 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6471 goto unlock;
6472 lec &= ec_mask;
6473 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6474
6475 if (lec != (b_nvram & ec_mask)) {
6476 /* NVRAM needs update */
6477 b_nvram &= ~ec_mask;
6478 b_nvram |= lec;
6479 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6480 dbg_printk(TPACPI_DBG_MIXER,
6481 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6482 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006483 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006484 vdbg_printk(TPACPI_DBG_MIXER,
6485 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6486 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006487 }
6488
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006489unlock:
6490 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006491}
6492
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006493static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006494{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006495 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006496
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006497 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6498 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006499
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006500 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006501
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006502 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006503
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006504 return 0;
6505}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006506
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006507static int volume_get_status(u8 *status)
6508{
6509 return volume_get_status_ec(status);
6510}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006511
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006512static int volume_set_status_ec(const u8 status)
6513{
6514 if (!acpi_ec_write(TP_EC_AUDIO, status))
6515 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006516
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006517 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6518
6519 return 0;
6520}
6521
6522static int volume_set_status(const u8 status)
6523{
6524 return volume_set_status_ec(status);
6525}
6526
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006527/* returns < 0 on error, 0 on no change, 1 on change */
6528static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006529{
6530 int rc;
6531 u8 s, n;
6532
6533 if (mutex_lock_killable(&volume_mutex) < 0)
6534 return -EINTR;
6535
6536 rc = volume_get_status_ec(&s);
6537 if (rc)
6538 goto unlock;
6539
6540 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6541 s & ~TP_EC_AUDIO_MUTESW_MSK;
6542
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006543 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006544 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006545 if (!rc)
6546 rc = 1;
6547 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006548
6549unlock:
6550 mutex_unlock(&volume_mutex);
6551 return rc;
6552}
6553
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006554static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006555{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006556 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006557 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006558 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006559}
6560
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006561static int volume_set_mute(const bool mute)
6562{
6563 int rc;
6564
6565 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6566 (mute) ? "" : "un");
6567
6568 rc = __volume_set_mute_ec(mute);
6569 return (rc < 0) ? rc : 0;
6570}
6571
6572/* returns < 0 on error, 0 on no change, 1 on change */
6573static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006574{
6575 int rc;
6576 u8 s, n;
6577
6578 if (vol > TP_EC_VOLUME_MAX)
6579 return -EINVAL;
6580
6581 if (mutex_lock_killable(&volume_mutex) < 0)
6582 return -EINTR;
6583
6584 rc = volume_get_status_ec(&s);
6585 if (rc)
6586 goto unlock;
6587
6588 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6589
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006590 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006591 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006592 if (!rc)
6593 rc = 1;
6594 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006595
6596unlock:
6597 mutex_unlock(&volume_mutex);
6598 return rc;
6599}
6600
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006601static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006602{
6603 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006604 "ALSA: trying to set volume level to %hu\n", vol);
6605 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006606}
6607
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006608static void volume_alsa_notify_change(void)
6609{
6610 struct tpacpi_alsa_data *d;
6611
6612 if (alsa_card && alsa_card->private_data) {
6613 d = alsa_card->private_data;
6614 if (d->ctl_mute_id)
6615 snd_ctl_notify(alsa_card,
6616 SNDRV_CTL_EVENT_MASK_VALUE,
6617 d->ctl_mute_id);
6618 if (d->ctl_vol_id)
6619 snd_ctl_notify(alsa_card,
6620 SNDRV_CTL_EVENT_MASK_VALUE,
6621 d->ctl_vol_id);
6622 }
6623}
6624
6625static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6626 struct snd_ctl_elem_info *uinfo)
6627{
6628 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6629 uinfo->count = 1;
6630 uinfo->value.integer.min = 0;
6631 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6632 return 0;
6633}
6634
6635static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6636 struct snd_ctl_elem_value *ucontrol)
6637{
6638 u8 s;
6639 int rc;
6640
6641 rc = volume_get_status(&s);
6642 if (rc < 0)
6643 return rc;
6644
6645 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6646 return 0;
6647}
6648
6649static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6650 struct snd_ctl_elem_value *ucontrol)
6651{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006652 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6653 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006654 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006655}
6656
6657#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6658
6659static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6660 struct snd_ctl_elem_value *ucontrol)
6661{
6662 u8 s;
6663 int rc;
6664
6665 rc = volume_get_status(&s);
6666 if (rc < 0)
6667 return rc;
6668
6669 ucontrol->value.integer.value[0] =
6670 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6671 return 0;
6672}
6673
6674static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6675 struct snd_ctl_elem_value *ucontrol)
6676{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006677 tpacpi_disclose_usertask("ALSA", "%smute\n",
6678 ucontrol->value.integer.value[0] ?
6679 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006680 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006681}
6682
6683static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6684 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6685 .name = "Console Playback Volume",
6686 .index = 0,
6687 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6688 .info = volume_alsa_vol_info,
6689 .get = volume_alsa_vol_get,
6690};
6691
6692static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6693 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6694 .name = "Console Playback Switch",
6695 .index = 0,
6696 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6697 .info = volume_alsa_mute_info,
6698 .get = volume_alsa_mute_get,
6699};
6700
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006701static void volume_suspend(pm_message_t state)
6702{
6703 tpacpi_volume_checkpoint_nvram();
6704}
6705
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006706static void volume_resume(void)
6707{
6708 volume_alsa_notify_change();
6709}
6710
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006711static void volume_shutdown(void)
6712{
6713 tpacpi_volume_checkpoint_nvram();
6714}
6715
6716static void volume_exit(void)
6717{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006718 if (alsa_card) {
6719 snd_card_free(alsa_card);
6720 alsa_card = NULL;
6721 }
6722
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006723 tpacpi_volume_checkpoint_nvram();
6724}
6725
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006726static int __init volume_create_alsa_mixer(void)
6727{
6728 struct snd_card *card;
6729 struct tpacpi_alsa_data *data;
6730 struct snd_kcontrol *ctl_vol;
6731 struct snd_kcontrol *ctl_mute;
6732 int rc;
6733
6734 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6735 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006736 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07006737 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006738 return 1;
6739 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006740
6741 BUG_ON(!card->private_data);
6742 data = card->private_data;
6743 data->card = card;
6744
6745 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6746 sizeof(card->driver));
6747 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6748 sizeof(card->shortname));
6749 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6750 (thinkpad_id.ec_version_str) ?
6751 thinkpad_id.ec_version_str : "(unknown)");
6752 snprintf(card->longname, sizeof(card->longname),
6753 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6754 (thinkpad_id.ec_version_str) ?
6755 thinkpad_id.ec_version_str : "unknown");
6756
6757 if (volume_control_allowed) {
6758 volume_alsa_control_vol.put = volume_alsa_vol_put;
6759 volume_alsa_control_vol.access =
6760 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6761
6762 volume_alsa_control_mute.put = volume_alsa_mute_put;
6763 volume_alsa_control_mute.access =
6764 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6765 }
6766
6767 if (!tp_features.mixer_no_level_control) {
6768 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6769 rc = snd_ctl_add(card, ctl_vol);
6770 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006771 pr_err("Failed to create ALSA volume control: %d\n",
6772 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006773 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006774 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006775 data->ctl_vol_id = &ctl_vol->id;
6776 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006777
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006778 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6779 rc = snd_ctl_add(card, ctl_mute);
6780 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006781 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006782 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006783 }
6784 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006785
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006786 snd_card_set_dev(card, &tpacpi_pdev->dev);
6787 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006788 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006789 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006790 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006791 }
6792
6793 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006794 return 0;
6795
6796err_exit:
6797 snd_card_free(card);
6798 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006799}
6800
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006801#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6802#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6803
6804static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6805 /* Whitelist volume level on all IBM by default */
6806 { .vendor = PCI_VENDOR_ID_IBM,
6807 .bios = TPACPI_MATCH_ANY,
6808 .ec = TPACPI_MATCH_ANY,
6809 .quirks = TPACPI_VOL_Q_LEVEL },
6810
6811 /* Lenovo models with volume control (needs confirmation) */
6812 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6813 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6814 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6815 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6816 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6817 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6818 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6819
6820 /* Whitelist mute-only on all Lenovo by default */
6821 { .vendor = PCI_VENDOR_ID_LENOVO,
6822 .bios = TPACPI_MATCH_ANY,
6823 .ec = TPACPI_MATCH_ANY,
6824 .quirks = TPACPI_VOL_Q_MUTEONLY }
6825};
6826
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006827static int __init volume_init(struct ibm_init_struct *iibm)
6828{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006829 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006830 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006831
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006832 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6833
6834 mutex_init(&volume_mutex);
6835
6836 /*
6837 * Check for module parameter bogosity, note that we
6838 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6839 * able to detect "unspecified"
6840 */
6841 if (volume_mode > TPACPI_VOL_MODE_MAX)
6842 return -EINVAL;
6843
6844 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Joe Perches0978e012011-04-04 10:06:25 -07006845 pr_err("UCMS step volume mode not implemented, "
6846 "please contact %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006847 return 1;
6848 }
6849
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006850 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6851 return -EINVAL;
6852
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006853 /*
6854 * The ALSA mixer is our primary interface.
6855 * When disabled, don't install the subdriver at all
6856 */
6857 if (!alsa_enable) {
6858 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6859 "ALSA mixer disabled by parameter, "
6860 "not loading volume subdriver...\n");
6861 return 1;
6862 }
6863
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006864 quirks = tpacpi_check_quirks(volume_quirk_table,
6865 ARRAY_SIZE(volume_quirk_table));
6866
6867 switch (volume_capabilities) {
6868 case TPACPI_VOL_CAP_AUTO:
6869 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6870 tp_features.mixer_no_level_control = 1;
6871 else if (quirks & TPACPI_VOL_Q_LEVEL)
6872 tp_features.mixer_no_level_control = 0;
6873 else
6874 return 1; /* no mixer */
6875 break;
6876 case TPACPI_VOL_CAP_VOLMUTE:
6877 tp_features.mixer_no_level_control = 0;
6878 break;
6879 case TPACPI_VOL_CAP_MUTEONLY:
6880 tp_features.mixer_no_level_control = 1;
6881 break;
6882 default:
6883 return 1;
6884 }
6885
6886 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6887 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6888 "using user-supplied volume_capabilities=%d\n",
6889 volume_capabilities);
6890
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006891 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6892 volume_mode == TPACPI_VOL_MODE_MAX) {
6893 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6894
6895 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6896 "driver auto-selected volume_mode=%d\n",
6897 volume_mode);
6898 } else {
6899 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6900 "using user-supplied volume_mode=%d\n",
6901 volume_mode);
6902 }
6903
6904 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006905 "mute is supported, volume control is %s\n",
6906 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006907
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006908 rc = volume_create_alsa_mixer();
6909 if (rc) {
Joe Perches0978e012011-04-04 10:06:25 -07006910 pr_err("Could not create the ALSA mixer interface\n");
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006911 return rc;
6912 }
6913
Joe Perches0978e012011-04-04 10:06:25 -07006914 pr_info("Console audio control enabled, mode: %s\n",
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006915 (volume_control_allowed) ?
6916 "override (read/write)" :
6917 "monitor (read only)");
6918
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006919 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6920 "registering volume hotkeys as change notification\n");
6921 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6922 | TP_ACPI_HKEY_VOLUP_MASK
6923 | TP_ACPI_HKEY_VOLDWN_MASK
6924 | TP_ACPI_HKEY_MUTE_MASK);
6925
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006926 return 0;
6927}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006928
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006929static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006930{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006931 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006932
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006933 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006934 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006935 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006936 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006937 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006938 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006939 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006940 status & TP_EC_AUDIO_LVL_MSK);
6941
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006942 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006943 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006944
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006945 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006946 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006947 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006948 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006949 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006950 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006951 "commands:\tlevel <level>"
6952 " (<level> is 0-%d)\n",
6953 TP_EC_VOLUME_MAX);
6954 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006955 }
6956 }
6957
6958 return 0;
6959}
6960
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006961static int volume_write(char *buf)
6962{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006963 u8 s;
6964 u8 new_level, new_mute;
6965 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006966 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006967 int rc;
6968
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006969 /*
6970 * We do allow volume control at driver startup, so that the
6971 * user can set initial state through the volume=... parameter hack.
6972 */
6973 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6974 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6975 tp_warned.volume_ctrl_forbidden = 1;
Joe Perches0978e012011-04-04 10:06:25 -07006976 pr_notice("Console audio control in monitor mode, "
6977 "changes are not allowed\n");
6978 pr_notice("Use the volume_control=1 module parameter "
6979 "to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006980 }
6981 return -EPERM;
6982 }
6983
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006984 rc = volume_get_status(&s);
6985 if (rc < 0)
6986 return rc;
6987
6988 new_level = s & TP_EC_AUDIO_LVL_MSK;
6989 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006990
6991 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006992 if (!tp_features.mixer_no_level_control) {
6993 if (strlencmp(cmd, "up") == 0) {
6994 if (new_mute)
6995 new_mute = 0;
6996 else if (new_level < TP_EC_VOLUME_MAX)
6997 new_level++;
6998 continue;
6999 } else if (strlencmp(cmd, "down") == 0) {
7000 if (new_mute)
7001 new_mute = 0;
7002 else if (new_level > 0)
7003 new_level--;
7004 continue;
7005 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7006 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7007 new_level = l;
7008 continue;
7009 }
7010 }
7011 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007012 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007013 else if (strlencmp(cmd, "unmute") == 0)
7014 new_mute = 0;
7015 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007016 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007017 }
7018
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007019 if (tp_features.mixer_no_level_control) {
7020 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7021 new_mute ? "" : "un");
7022 rc = volume_set_mute(!!new_mute);
7023 } else {
7024 tpacpi_disclose_usertask("procfs volume",
7025 "%smute and set level to %d\n",
7026 new_mute ? "" : "un", new_level);
7027 rc = volume_set_status(new_mute | new_level);
7028 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007029 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007030
7031 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007032}
7033
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007034static struct ibm_struct volume_driver_data = {
7035 .name = "volume",
7036 .read = volume_read,
7037 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007038 .exit = volume_exit,
7039 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007040 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007041 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007042};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007043
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007044#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7045
7046#define alsa_card NULL
7047
7048static void inline volume_alsa_notify_change(void)
7049{
7050}
7051
7052static int __init volume_init(struct ibm_init_struct *iibm)
7053{
Joe Perches0978e012011-04-04 10:06:25 -07007054 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007055
7056 return 1;
7057}
7058
7059static struct ibm_struct volume_driver_data = {
7060 .name = "volume",
7061};
7062
7063#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7064
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007065/*************************************************************************
7066 * Fan subdriver
7067 */
7068
7069/*
7070 * FAN ACCESS MODES
7071 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007072 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007073 * ACPI GFAN method: returns fan level
7074 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007075 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007076 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007077 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007078 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007079 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7080 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007081 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7082 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007083 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007084 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007085 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7086 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007087 *
7088 * Fan speed changes of any sort (including those caused by the
7089 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007090 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007091 * limited.
7092 *
7093 * Reading is not available if GFAN exists.
7094 * Writing is not available if SFAN exists.
7095 *
7096 * Bits
7097 * 7 automatic mode engaged;
7098 * (default operation mode of the ThinkPad)
7099 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007100 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007101 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007102 * the tachometer while the fan controller ramps up
7103 * the speed (which can take up to a few *minutes*).
7104 * Speeds up fan to 100% duty-cycle, which is far above
7105 * the standard RPM levels. It is not impossible that
7106 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007107 * 5-3 unused in some models. Extra bits for fan level
7108 * in others, but still useless as all values above
7109 * 7 map to the same speed as level 7 in these models.
7110 * 2-0 fan level (0..7 usually)
7111 * 0x00 = stop
7112 * 0x07 = max (set when temperatures critical)
7113 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007114 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007115 *
7116 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007117 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007118 * does so, its initial value is meaningless (0x07).
7119 *
7120 * For firmware bugs, refer to:
7121 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7122 *
7123 * ----
7124 *
7125 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7126 * Main fan tachometer reading (in RPM)
7127 *
7128 * This register is present on all ThinkPads with a new-style EC, and
7129 * it is known not to be present on the A21m/e, and T22, as there is
7130 * something else in offset 0x84 according to the ACPI DSDT. Other
7131 * ThinkPads from this same time period (and earlier) probably lack the
7132 * tachometer as well.
7133 *
Nick Andrew877d0312009-01-26 11:06:57 +01007134 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007135 * was never fixed by IBM to report the EC firmware version string
7136 * probably support the tachometer (like the early X models), so
7137 * detecting it is quite hard. We need more data to know for sure.
7138 *
7139 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7140 * might result.
7141 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007142 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7143 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007144 *
7145 * For firmware bugs, refer to:
7146 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7147 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007148 * ----
7149 *
7150 * ThinkPad EC register 0x31 bit 0 (only on select models)
7151 *
7152 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7153 * show the speed of the main fan. When bit 0 of EC register 0x31
7154 * is one, the tachometer registers show the speed of the auxiliary
7155 * fan.
7156 *
7157 * Fan control seems to affect both fans, regardless of the state
7158 * of this bit.
7159 *
7160 * So far, only the firmware for the X60/X61 non-tablet versions
7161 * seem to support this (firmware TP-7M).
7162 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007163 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007164 * ThinkPad X31, X40, X41. Not available in the X60.
7165 *
7166 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7167 * high speed. ACPI DSDT seems to map these three speeds to levels
7168 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7169 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7170 *
7171 * The speeds are stored on handles
7172 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7173 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007174 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007175 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7176 *
7177 * ACPI DSDT switches which set is in use depending on various
7178 * factors.
7179 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007180 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007181 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7182 * but the ACPI tables just mention level 7.
7183 */
7184
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007185enum { /* Fan control constants */
7186 fan_status_offset = 0x2f, /* EC register 0x2f */
7187 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7188 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007189 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7190 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007191
7192 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7193 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7194
7195 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7196};
7197
7198enum fan_status_access_mode {
7199 TPACPI_FAN_NONE = 0, /* No fan status or control */
7200 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7201 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7202};
7203
7204enum fan_control_access_mode {
7205 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7206 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7207 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7208 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7209};
7210
7211enum fan_control_commands {
7212 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7213 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7214 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7215 * and also watchdog cmd */
7216};
7217
7218static int fan_control_allowed;
7219
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007220static enum fan_status_access_mode fan_status_access_mode;
7221static enum fan_control_access_mode fan_control_access_mode;
7222static enum fan_control_commands fan_control_commands;
7223
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007224static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007225static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007226static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007227static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007228
7229static struct mutex fan_mutex;
7230
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007231static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007232static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007233
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007234TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7235TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007236 "\\FSPD", /* 600e/x, 770e, 770x */
7237 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007238TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007239 "JFNS", /* 770x-JL */
7240 ); /* all others */
7241
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007242/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007243 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7244 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7245 * be in auto mode (0x80).
7246 *
7247 * This is corrected by any write to HFSP either by the driver, or
7248 * by the firmware.
7249 *
7250 * We assume 0x07 really means auto mode while this quirk is active,
7251 * as this is far more likely than the ThinkPad being in level 7,
7252 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007253 *
7254 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7255 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007256 */
7257
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007258static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007259{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007260 if (fan_control_initial_status == 0x07) {
Joe Perches0978e012011-04-04 10:06:25 -07007261 pr_notice("fan_init: initial fan status is unknown, "
7262 "assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007263 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007264 }
7265}
7266
7267static void fan_quirk1_handle(u8 *fan_status)
7268{
7269 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7270 if (*fan_status != fan_control_initial_status) {
7271 /* something changed the HFSP regisnter since
7272 * driver init time, so it is not undefined
7273 * anymore */
7274 tp_features.fan_ctrl_status_undef = 0;
7275 } else {
7276 /* Return most likely status. In fact, it
7277 * might be the only possible status */
7278 *fan_status = TP_EC_FAN_AUTO;
7279 }
7280 }
7281}
7282
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007283/* Select main fan on X60/X61, NOOP on others */
7284static bool fan_select_fan1(void)
7285{
7286 if (tp_features.second_fan) {
7287 u8 val;
7288
7289 if (ec_read(fan_select_offset, &val) < 0)
7290 return false;
7291 val &= 0xFEU;
7292 if (ec_write(fan_select_offset, val) < 0)
7293 return false;
7294 }
7295 return true;
7296}
7297
7298/* Select secondary fan on X60/X61 */
7299static bool fan_select_fan2(void)
7300{
7301 u8 val;
7302
7303 if (!tp_features.second_fan)
7304 return false;
7305
7306 if (ec_read(fan_select_offset, &val) < 0)
7307 return false;
7308 val |= 0x01U;
7309 if (ec_write(fan_select_offset, val) < 0)
7310 return false;
7311
7312 return true;
7313}
7314
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007315/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007316 * Call with fan_mutex held
7317 */
7318static void fan_update_desired_level(u8 status)
7319{
7320 if ((status &
7321 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7322 if (status > 7)
7323 fan_control_desired_level = 7;
7324 else
7325 fan_control_desired_level = status;
7326 }
7327}
7328
7329static int fan_get_status(u8 *status)
7330{
7331 u8 s;
7332
7333 /* TODO:
7334 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7335
7336 switch (fan_status_access_mode) {
7337 case TPACPI_FAN_RD_ACPI_GFAN:
7338 /* 570, 600e/x, 770e, 770x */
7339
7340 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7341 return -EIO;
7342
7343 if (likely(status))
7344 *status = s & 0x07;
7345
7346 break;
7347
7348 case TPACPI_FAN_RD_TPEC:
7349 /* all except 570, 600e/x, 770e, 770x */
7350 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7351 return -EIO;
7352
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007353 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007354 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007355 fan_quirk1_handle(status);
7356 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007357
7358 break;
7359
7360 default:
7361 return -ENXIO;
7362 }
7363
7364 return 0;
7365}
7366
7367static int fan_get_status_safe(u8 *status)
7368{
7369 int rc;
7370 u8 s;
7371
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007372 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007373 return -ERESTARTSYS;
7374 rc = fan_get_status(&s);
7375 if (!rc)
7376 fan_update_desired_level(s);
7377 mutex_unlock(&fan_mutex);
7378
7379 if (status)
7380 *status = s;
7381
7382 return rc;
7383}
7384
7385static int fan_get_speed(unsigned int *speed)
7386{
7387 u8 hi, lo;
7388
7389 switch (fan_status_access_mode) {
7390 case TPACPI_FAN_RD_TPEC:
7391 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007392 if (unlikely(!fan_select_fan1()))
7393 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007394 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7395 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7396 return -EIO;
7397
7398 if (likely(speed))
7399 *speed = (hi << 8) | lo;
7400
7401 break;
7402
7403 default:
7404 return -ENXIO;
7405 }
7406
7407 return 0;
7408}
7409
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007410static int fan2_get_speed(unsigned int *speed)
7411{
7412 u8 hi, lo;
7413 bool rc;
7414
7415 switch (fan_status_access_mode) {
7416 case TPACPI_FAN_RD_TPEC:
7417 /* all except 570, 600e/x, 770e, 770x */
7418 if (unlikely(!fan_select_fan2()))
7419 return -EIO;
7420 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7421 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7422 fan_select_fan1(); /* play it safe */
7423 if (rc)
7424 return -EIO;
7425
7426 if (likely(speed))
7427 *speed = (hi << 8) | lo;
7428
7429 break;
7430
7431 default:
7432 return -ENXIO;
7433 }
7434
7435 return 0;
7436}
7437
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007438static int fan_set_level(int level)
7439{
7440 if (!fan_control_allowed)
7441 return -EPERM;
7442
7443 switch (fan_control_access_mode) {
7444 case TPACPI_FAN_WR_ACPI_SFAN:
7445 if (level >= 0 && level <= 7) {
7446 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7447 return -EIO;
7448 } else
7449 return -EINVAL;
7450 break;
7451
7452 case TPACPI_FAN_WR_ACPI_FANS:
7453 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007454 if (!(level & TP_EC_FAN_AUTO) &&
7455 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007456 ((level < 0) || (level > 7)))
7457 return -EINVAL;
7458
7459 /* safety net should the EC not support AUTO
7460 * or FULLSPEED mode bits and just ignore them */
7461 if (level & TP_EC_FAN_FULLSPEED)
7462 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007463 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007464 level |= 4; /* safety min speed 4 */
7465
7466 if (!acpi_ec_write(fan_status_offset, level))
7467 return -EIO;
7468 else
7469 tp_features.fan_ctrl_status_undef = 0;
7470 break;
7471
7472 default:
7473 return -ENXIO;
7474 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007475
7476 vdbg_printk(TPACPI_DBG_FAN,
7477 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007478 return 0;
7479}
7480
7481static int fan_set_level_safe(int level)
7482{
7483 int rc;
7484
7485 if (!fan_control_allowed)
7486 return -EPERM;
7487
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007488 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007489 return -ERESTARTSYS;
7490
7491 if (level == TPACPI_FAN_LAST_LEVEL)
7492 level = fan_control_desired_level;
7493
7494 rc = fan_set_level(level);
7495 if (!rc)
7496 fan_update_desired_level(level);
7497
7498 mutex_unlock(&fan_mutex);
7499 return rc;
7500}
7501
7502static int fan_set_enable(void)
7503{
7504 u8 s;
7505 int rc;
7506
7507 if (!fan_control_allowed)
7508 return -EPERM;
7509
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007510 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007511 return -ERESTARTSYS;
7512
7513 switch (fan_control_access_mode) {
7514 case TPACPI_FAN_WR_ACPI_FANS:
7515 case TPACPI_FAN_WR_TPEC:
7516 rc = fan_get_status(&s);
7517 if (rc < 0)
7518 break;
7519
7520 /* Don't go out of emergency fan mode */
7521 if (s != 7) {
7522 s &= 0x07;
7523 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7524 }
7525
7526 if (!acpi_ec_write(fan_status_offset, s))
7527 rc = -EIO;
7528 else {
7529 tp_features.fan_ctrl_status_undef = 0;
7530 rc = 0;
7531 }
7532 break;
7533
7534 case TPACPI_FAN_WR_ACPI_SFAN:
7535 rc = fan_get_status(&s);
7536 if (rc < 0)
7537 break;
7538
7539 s &= 0x07;
7540
7541 /* Set fan to at least level 4 */
7542 s |= 4;
7543
7544 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007545 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007546 else
7547 rc = 0;
7548 break;
7549
7550 default:
7551 rc = -ENXIO;
7552 }
7553
7554 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007555
7556 if (!rc)
7557 vdbg_printk(TPACPI_DBG_FAN,
7558 "fan control: set fan control register to 0x%02x\n",
7559 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007560 return rc;
7561}
7562
7563static int fan_set_disable(void)
7564{
7565 int rc;
7566
7567 if (!fan_control_allowed)
7568 return -EPERM;
7569
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007570 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007571 return -ERESTARTSYS;
7572
7573 rc = 0;
7574 switch (fan_control_access_mode) {
7575 case TPACPI_FAN_WR_ACPI_FANS:
7576 case TPACPI_FAN_WR_TPEC:
7577 if (!acpi_ec_write(fan_status_offset, 0x00))
7578 rc = -EIO;
7579 else {
7580 fan_control_desired_level = 0;
7581 tp_features.fan_ctrl_status_undef = 0;
7582 }
7583 break;
7584
7585 case TPACPI_FAN_WR_ACPI_SFAN:
7586 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7587 rc = -EIO;
7588 else
7589 fan_control_desired_level = 0;
7590 break;
7591
7592 default:
7593 rc = -ENXIO;
7594 }
7595
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007596 if (!rc)
7597 vdbg_printk(TPACPI_DBG_FAN,
7598 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007599
7600 mutex_unlock(&fan_mutex);
7601 return rc;
7602}
7603
7604static int fan_set_speed(int speed)
7605{
7606 int rc;
7607
7608 if (!fan_control_allowed)
7609 return -EPERM;
7610
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007611 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007612 return -ERESTARTSYS;
7613
7614 rc = 0;
7615 switch (fan_control_access_mode) {
7616 case TPACPI_FAN_WR_ACPI_FANS:
7617 if (speed >= 0 && speed <= 65535) {
7618 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7619 speed, speed, speed))
7620 rc = -EIO;
7621 } else
7622 rc = -EINVAL;
7623 break;
7624
7625 default:
7626 rc = -ENXIO;
7627 }
7628
7629 mutex_unlock(&fan_mutex);
7630 return rc;
7631}
7632
7633static void fan_watchdog_reset(void)
7634{
7635 static int fan_watchdog_active;
7636
7637 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7638 return;
7639
7640 if (fan_watchdog_active)
7641 cancel_delayed_work(&fan_watchdog_task);
7642
7643 if (fan_watchdog_maxinterval > 0 &&
7644 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7645 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007646 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007647 msecs_to_jiffies(fan_watchdog_maxinterval
7648 * 1000))) {
Joe Perches0978e012011-04-04 10:06:25 -07007649 pr_err("failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007650 "watchdog will not trigger\n");
7651 }
7652 } else
7653 fan_watchdog_active = 0;
7654}
7655
7656static void fan_watchdog_fire(struct work_struct *ignored)
7657{
7658 int rc;
7659
7660 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7661 return;
7662
Joe Perches0978e012011-04-04 10:06:25 -07007663 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007664 rc = fan_set_enable();
7665 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007666 pr_err("fan watchdog: error %d while enabling fan, "
7667 "will try again later...\n", -rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007668 /* reschedule for later */
7669 fan_watchdog_reset();
7670 }
7671}
7672
7673/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007674 * SYSFS fan layout: hwmon compatible (device)
7675 *
7676 * pwm*_enable:
7677 * 0: "disengaged" mode
7678 * 1: manual mode
7679 * 2: native EC "auto" mode (recommended, hardware default)
7680 *
7681 * pwm*: set speed in manual mode, ignored otherwise.
7682 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7683 * interpolation.
7684 *
7685 * fan*_input: tachometer reading, RPM
7686 *
7687 *
7688 * SYSFS fan layout: extensions
7689 *
7690 * fan_watchdog (driver):
7691 * fan watchdog interval in seconds, 0 disables (default), max 120
7692 */
7693
7694/* sysfs fan pwm1_enable ----------------------------------------------- */
7695static ssize_t fan_pwm1_enable_show(struct device *dev,
7696 struct device_attribute *attr,
7697 char *buf)
7698{
7699 int res, mode;
7700 u8 status;
7701
7702 res = fan_get_status_safe(&status);
7703 if (res)
7704 return res;
7705
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007706 if (status & TP_EC_FAN_FULLSPEED) {
7707 mode = 0;
7708 } else if (status & TP_EC_FAN_AUTO) {
7709 mode = 2;
7710 } else
7711 mode = 1;
7712
7713 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7714}
7715
7716static ssize_t fan_pwm1_enable_store(struct device *dev,
7717 struct device_attribute *attr,
7718 const char *buf, size_t count)
7719{
7720 unsigned long t;
7721 int res, level;
7722
7723 if (parse_strtoul(buf, 2, &t))
7724 return -EINVAL;
7725
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007726 tpacpi_disclose_usertask("hwmon pwm1_enable",
7727 "set fan mode to %lu\n", t);
7728
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007729 switch (t) {
7730 case 0:
7731 level = TP_EC_FAN_FULLSPEED;
7732 break;
7733 case 1:
7734 level = TPACPI_FAN_LAST_LEVEL;
7735 break;
7736 case 2:
7737 level = TP_EC_FAN_AUTO;
7738 break;
7739 case 3:
7740 /* reserved for software-controlled auto mode */
7741 return -ENOSYS;
7742 default:
7743 return -EINVAL;
7744 }
7745
7746 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007747 if (res == -ENXIO)
7748 return -EINVAL;
7749 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007750 return res;
7751
7752 fan_watchdog_reset();
7753
7754 return count;
7755}
7756
7757static struct device_attribute dev_attr_fan_pwm1_enable =
7758 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7759 fan_pwm1_enable_show, fan_pwm1_enable_store);
7760
7761/* sysfs fan pwm1 ------------------------------------------------------ */
7762static ssize_t fan_pwm1_show(struct device *dev,
7763 struct device_attribute *attr,
7764 char *buf)
7765{
7766 int res;
7767 u8 status;
7768
7769 res = fan_get_status_safe(&status);
7770 if (res)
7771 return res;
7772
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007773 if ((status &
7774 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7775 status = fan_control_desired_level;
7776
7777 if (status > 7)
7778 status = 7;
7779
7780 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7781}
7782
7783static ssize_t fan_pwm1_store(struct device *dev,
7784 struct device_attribute *attr,
7785 const char *buf, size_t count)
7786{
7787 unsigned long s;
7788 int rc;
7789 u8 status, newlevel;
7790
7791 if (parse_strtoul(buf, 255, &s))
7792 return -EINVAL;
7793
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007794 tpacpi_disclose_usertask("hwmon pwm1",
7795 "set fan speed to %lu\n", s);
7796
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007797 /* scale down from 0-255 to 0-7 */
7798 newlevel = (s >> 5) & 0x07;
7799
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007800 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007801 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007802
7803 rc = fan_get_status(&status);
7804 if (!rc && (status &
7805 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7806 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007807 if (rc == -ENXIO)
7808 rc = -EINVAL;
7809 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007810 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007811 fan_watchdog_reset();
7812 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007813 }
7814
7815 mutex_unlock(&fan_mutex);
7816 return (rc)? rc : count;
7817}
7818
7819static struct device_attribute dev_attr_fan_pwm1 =
7820 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7821 fan_pwm1_show, fan_pwm1_store);
7822
7823/* sysfs fan fan1_input ------------------------------------------------ */
7824static ssize_t fan_fan1_input_show(struct device *dev,
7825 struct device_attribute *attr,
7826 char *buf)
7827{
7828 int res;
7829 unsigned int speed;
7830
7831 res = fan_get_speed(&speed);
7832 if (res < 0)
7833 return res;
7834
7835 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7836}
7837
7838static struct device_attribute dev_attr_fan_fan1_input =
7839 __ATTR(fan1_input, S_IRUGO,
7840 fan_fan1_input_show, NULL);
7841
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007842/* sysfs fan fan2_input ------------------------------------------------ */
7843static ssize_t fan_fan2_input_show(struct device *dev,
7844 struct device_attribute *attr,
7845 char *buf)
7846{
7847 int res;
7848 unsigned int speed;
7849
7850 res = fan2_get_speed(&speed);
7851 if (res < 0)
7852 return res;
7853
7854 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7855}
7856
7857static struct device_attribute dev_attr_fan_fan2_input =
7858 __ATTR(fan2_input, S_IRUGO,
7859 fan_fan2_input_show, NULL);
7860
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007861/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007862static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7863 char *buf)
7864{
7865 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7866}
7867
7868static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7869 const char *buf, size_t count)
7870{
7871 unsigned long t;
7872
7873 if (parse_strtoul(buf, 120, &t))
7874 return -EINVAL;
7875
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007876 if (!fan_control_allowed)
7877 return -EPERM;
7878
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007879 fan_watchdog_maxinterval = t;
7880 fan_watchdog_reset();
7881
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007882 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7883
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007884 return count;
7885}
7886
7887static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7888 fan_fan_watchdog_show, fan_fan_watchdog_store);
7889
7890/* --------------------------------------------------------------------- */
7891static struct attribute *fan_attributes[] = {
7892 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7893 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007894 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007895 NULL
7896};
7897
7898static const struct attribute_group fan_attr_group = {
7899 .attrs = fan_attributes,
7900};
7901
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007902#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7903#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007904
7905#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7906 { .vendor = PCI_VENDOR_ID_IBM, \
7907 .bios = TPACPI_MATCH_ANY, \
7908 .ec = TPID(__id1, __id2), \
7909 .quirks = __quirks }
7910
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007911#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7912 { .vendor = PCI_VENDOR_ID_LENOVO, \
7913 .bios = TPACPI_MATCH_ANY, \
7914 .ec = TPID(__id1, __id2), \
7915 .quirks = __quirks }
7916
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007917static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7918 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7919 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7920 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7921 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007922 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007923};
7924
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007925#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007926#undef TPACPI_FAN_QI
7927
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007928static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007929{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007930 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007931 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007932
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007933 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7934 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007935
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007936 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007937 fan_status_access_mode = TPACPI_FAN_NONE;
7938 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007939 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007940 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007941 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007942 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007943 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007944
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03007945 if (tpacpi_is_ibm()) {
7946 TPACPI_ACPIHANDLE_INIT(fans);
7947 TPACPI_ACPIHANDLE_INIT(gfan);
7948 TPACPI_ACPIHANDLE_INIT(sfan);
7949 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007950
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007951 quirks = tpacpi_check_quirks(fan_quirk_table,
7952 ARRAY_SIZE(fan_quirk_table));
7953
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007954 if (gfan_handle) {
7955 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007956 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007957 } else {
7958 /* all other ThinkPads: note that even old-style
7959 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007960 if (likely(acpi_ec_read(fan_status_offset,
7961 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007962 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007963 if (quirks & TPACPI_FAN_Q1)
7964 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007965 if (quirks & TPACPI_FAN_2FAN) {
7966 tp_features.second_fan = 1;
7967 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7968 "secondary fan support enabled\n");
7969 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007970 } else {
Joe Perches0978e012011-04-04 10:06:25 -07007971 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007972 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007973 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007974 }
7975 }
7976
7977 if (sfan_handle) {
7978 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007979 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007980 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007981 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007982 } else {
7983 if (!gfan_handle) {
7984 /* gfan without sfan means no fan control */
7985 /* all other models implement TP EC 0x2f control */
7986
7987 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007988 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007989 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007990 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007991 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007992 TPACPI_FAN_CMD_SPEED |
7993 TPACPI_FAN_CMD_LEVEL |
7994 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007995 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007996 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007997 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007998 TPACPI_FAN_CMD_LEVEL |
7999 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008000 }
8001 }
8002 }
8003
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008004 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8005 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008006 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8007 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8008 fan_status_access_mode, fan_control_access_mode);
8009
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008010 /* fan control master switch */
8011 if (!fan_control_allowed) {
8012 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8013 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008014 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008015 "fan control features disabled by parameter\n");
8016 }
8017
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008018 /* update fan_control_desired_level */
8019 if (fan_status_access_mode != TPACPI_FAN_NONE)
8020 fan_get_status_safe(NULL);
8021
8022 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8023 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008024 if (tp_features.second_fan) {
8025 /* attach second fan tachometer */
8026 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8027 &dev_attr_fan_fan2_input.attr;
8028 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008029 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008030 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008031 if (rc < 0)
8032 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008033
8034 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8035 &driver_attr_fan_watchdog);
8036 if (rc < 0) {
8037 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8038 &fan_attr_group);
8039 return rc;
8040 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008041 return 0;
8042 } else
8043 return 1;
8044}
8045
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008046static void fan_exit(void)
8047{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008048 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008049 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008050
8051 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008052 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008053 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8054 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008055
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008056 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008057 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008058}
8059
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008060static void fan_suspend(pm_message_t state)
8061{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008062 int rc;
8063
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008064 if (!fan_control_allowed)
8065 return;
8066
8067 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008068 fan_control_resume_level = 0;
8069 rc = fan_get_status_safe(&fan_control_resume_level);
8070 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008071 pr_notice("failed to read fan level for later "
8072 "restore during resume: %d\n", rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008073
8074 /* if it is undefined, don't attempt to restore it.
8075 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008076 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008077 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008078}
8079
8080static void fan_resume(void)
8081{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008082 u8 current_level = 7;
8083 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008084 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008085
8086 /* DSDT *always* updates status on resume */
8087 tp_features.fan_ctrl_status_undef = 0;
8088
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008089 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008090 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008091 (fan_get_status_safe(&current_level) < 0))
8092 return;
8093
8094 switch (fan_control_access_mode) {
8095 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008096 /* never decrease fan level */
8097 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008098 break;
8099 case TPACPI_FAN_WR_ACPI_FANS:
8100 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008101 /* never decrease fan level, scale is:
8102 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8103 *
8104 * We expect the firmware to set either 7 or AUTO, but we
8105 * handle FULLSPEED out of paranoia.
8106 *
8107 * So, we can safely only restore FULLSPEED or 7, anything
8108 * else could slow the fan. Restoring AUTO is useless, at
8109 * best that's exactly what the DSDT already set (it is the
8110 * slower it uses).
8111 *
8112 * Always keep in mind that the DSDT *will* have set the
8113 * fans to what the vendor supposes is the best level. We
8114 * muck with it only to speed the fan up.
8115 */
8116 if (fan_control_resume_level != 7 &&
8117 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8118 return;
8119 else
8120 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8121 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008122 break;
8123 default:
8124 return;
8125 }
8126 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008127 pr_notice("restoring fan level to 0x%02x\n",
8128 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008129 rc = fan_set_level_safe(fan_control_resume_level);
8130 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008131 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008132 }
8133}
8134
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008135static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008136{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008137 int rc;
8138 u8 status;
8139 unsigned int speed = 0;
8140
8141 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008142 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008143 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008144 rc = fan_get_status_safe(&status);
8145 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008146 return rc;
8147
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008148 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008149 "level:\t\t%d\n",
8150 (status != 0) ? "enabled" : "disabled", status);
8151 break;
8152
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008153 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008154 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008155 rc = fan_get_status_safe(&status);
8156 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008157 return rc;
8158
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008159 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008160 (status != 0) ? "enabled" : "disabled");
8161
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008162 rc = fan_get_speed(&speed);
8163 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008164 return rc;
8165
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008166 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008167
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008168 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008169 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008170 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008171 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008172 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008173 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008174 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008175 break;
8176
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008177 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008178 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008179 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008180 }
8181
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008182 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008183 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008184
8185 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008186 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008187 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008188 break;
8189
8190 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008191 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008192 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008193 break;
8194 }
8195 }
8196
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008197 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008198 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008199 "commands:\twatchdog <timeout> (<timeout> "
8200 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008201
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008202 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008203 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008204 " (<speed> is 0-65535)\n");
8205
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008206 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008207}
8208
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008209static int fan_write_cmd_level(const char *cmd, int *rc)
8210{
8211 int level;
8212
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008213 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008214 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008215 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008216 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008217 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008218 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008219 return 0;
8220
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008221 *rc = fan_set_level_safe(level);
8222 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008223 pr_err("level command accepted for unsupported access mode %d\n",
8224 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008225 else if (!*rc)
8226 tpacpi_disclose_usertask("procfs fan",
8227 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008228
8229 return 1;
8230}
8231
8232static int fan_write_cmd_enable(const char *cmd, int *rc)
8233{
8234 if (strlencmp(cmd, "enable") != 0)
8235 return 0;
8236
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008237 *rc = fan_set_enable();
8238 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008239 pr_err("enable command accepted for unsupported access mode %d\n",
8240 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008241 else if (!*rc)
8242 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008243
8244 return 1;
8245}
8246
8247static int fan_write_cmd_disable(const char *cmd, int *rc)
8248{
8249 if (strlencmp(cmd, "disable") != 0)
8250 return 0;
8251
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008252 *rc = fan_set_disable();
8253 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008254 pr_err("disable command accepted for unsupported access mode %d\n",
8255 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008256 else if (!*rc)
8257 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008258
8259 return 1;
8260}
8261
8262static int fan_write_cmd_speed(const char *cmd, int *rc)
8263{
8264 int speed;
8265
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008266 /* TODO:
8267 * Support speed <low> <medium> <high> ? */
8268
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008269 if (sscanf(cmd, "speed %d", &speed) != 1)
8270 return 0;
8271
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008272 *rc = fan_set_speed(speed);
8273 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008274 pr_err("speed command accepted for unsupported access mode %d\n",
8275 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008276 else if (!*rc)
8277 tpacpi_disclose_usertask("procfs fan",
8278 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008279
8280 return 1;
8281}
8282
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008283static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8284{
8285 int interval;
8286
8287 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8288 return 0;
8289
8290 if (interval < 0 || interval > 120)
8291 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008292 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008293 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008294 tpacpi_disclose_usertask("procfs fan",
8295 "set watchdog timer to %d\n",
8296 interval);
8297 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008298
8299 return 1;
8300}
8301
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008302static int fan_write(char *buf)
8303{
8304 char *cmd;
8305 int rc = 0;
8306
8307 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008308 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008309 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008310 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008311 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008312 fan_write_cmd_disable(cmd, &rc) ||
8313 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008314 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008315 fan_write_cmd_speed(cmd, &rc))
8316 )
8317 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008318 else if (!rc)
8319 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008320 }
8321
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008322 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008323}
8324
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008325static struct ibm_struct fan_driver_data = {
8326 .name = "fan",
8327 .read = fan_read,
8328 .write = fan_write,
8329 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008330 .suspend = fan_suspend,
8331 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008332};
8333
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008334/****************************************************************************
8335 ****************************************************************************
8336 *
8337 * Infrastructure
8338 *
8339 ****************************************************************************
8340 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008342/*
8343 * HKEY event callout for other subdrivers go here
8344 * (yes, it is ugly, but it is quick, safe, and gets the job done
8345 */
8346static void tpacpi_driver_event(const unsigned int hkey_event)
8347{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008348 if (ibm_backlight_device) {
8349 switch (hkey_event) {
8350 case TP_HKEY_EV_BRGHT_UP:
8351 case TP_HKEY_EV_BRGHT_DOWN:
8352 tpacpi_brightness_notify_change();
8353 }
8354 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008355 if (alsa_card) {
8356 switch (hkey_event) {
8357 case TP_HKEY_EV_VOL_UP:
8358 case TP_HKEY_EV_VOL_DOWN:
8359 case TP_HKEY_EV_VOL_MUTE:
8360 volume_alsa_notify_change();
8361 }
8362 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008363}
8364
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008365static void hotkey_driver_event(const unsigned int scancode)
8366{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008367 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008368}
8369
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008370/* sysfs name ---------------------------------------------------------- */
8371static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8372 struct device_attribute *attr,
8373 char *buf)
8374{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008375 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008376}
8377
8378static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8379 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8380
8381/* --------------------------------------------------------------------- */
8382
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008383/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03008384static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008385
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008386/*
8387 * Module and infrastructure proble, init and exit handling
8388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008389
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008390static int force_load;
8391
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008392#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008393static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008394{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008395 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008396
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008397 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008398}
8399#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8400
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008401static void ibm_exit(struct ibm_struct *ibm)
8402{
8403 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8404
8405 list_del_init(&ibm->all_drivers);
8406
8407 if (ibm->flags.acpi_notify_installed) {
8408 dbg_printk(TPACPI_DBG_EXIT,
8409 "%s: acpi_remove_notify_handler\n", ibm->name);
8410 BUG_ON(!ibm->acpi);
8411 acpi_remove_notify_handler(*ibm->acpi->handle,
8412 ibm->acpi->type,
8413 dispatch_acpi_notify);
8414 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008415 }
8416
8417 if (ibm->flags.proc_created) {
8418 dbg_printk(TPACPI_DBG_EXIT,
8419 "%s: remove_proc_entry\n", ibm->name);
8420 remove_proc_entry(ibm->name, proc_dir);
8421 ibm->flags.proc_created = 0;
8422 }
8423
8424 if (ibm->flags.acpi_driver_registered) {
8425 dbg_printk(TPACPI_DBG_EXIT,
8426 "%s: acpi_bus_unregister_driver\n", ibm->name);
8427 BUG_ON(!ibm->acpi);
8428 acpi_bus_unregister_driver(ibm->acpi->driver);
8429 kfree(ibm->acpi->driver);
8430 ibm->acpi->driver = NULL;
8431 ibm->flags.acpi_driver_registered = 0;
8432 }
8433
8434 if (ibm->flags.init_called && ibm->exit) {
8435 ibm->exit();
8436 ibm->flags.init_called = 0;
8437 }
8438
8439 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8440}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008441
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008442static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443{
8444 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008445 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446 struct proc_dir_entry *entry;
8447
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008448 BUG_ON(ibm == NULL);
8449
8450 INIT_LIST_HEAD(&ibm->all_drivers);
8451
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008452 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008453 return 0;
8454
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008455 dbg_printk(TPACPI_DBG_INIT,
8456 "probing for %s\n", ibm->name);
8457
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008458 if (iibm->init) {
8459 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008460 if (ret > 0)
8461 return 0; /* probe failed */
8462 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008464
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008465 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466 }
8467
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008468 if (ibm->acpi) {
8469 if (ibm->acpi->hid) {
8470 ret = register_tpacpi_subdriver(ibm);
8471 if (ret)
8472 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008473 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008474
8475 if (ibm->acpi->notify) {
8476 ret = setup_acpi_notify(ibm);
8477 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07008478 pr_notice("disabling subdriver %s\n",
8479 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008480 ret = 0;
8481 goto err_out;
8482 }
8483 if (ret < 0)
8484 goto err_out;
8485 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008486 }
8487
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008488 dbg_printk(TPACPI_DBG_INIT,
8489 "%s installed\n", ibm->name);
8490
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008491 if (ibm->read) {
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008492 mode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008493
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008494 if (!mode)
8495 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008496 if (ibm->write)
8497 mode |= S_IWUSR;
8498 entry = proc_create_data(ibm->name, mode, proc_dir,
8499 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008500 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07008501 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008502 ret = -ENODEV;
8503 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008504 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008505 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008508 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8509
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008511
8512err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008513 dbg_printk(TPACPI_DBG_INIT,
8514 "%s: at error exit path with result %d\n",
8515 ibm->name, ret);
8516
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008517 ibm_exit(ibm);
8518 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008519}
8520
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008521/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008522
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008523static bool __pure __init tpacpi_is_fw_digit(const char c)
8524{
8525 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8526}
8527
8528/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8529static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8530 const char t)
8531{
8532 return s && strlen(s) >= 8 &&
8533 tpacpi_is_fw_digit(s[0]) &&
8534 tpacpi_is_fw_digit(s[1]) &&
8535 s[2] == t && s[3] == 'T' &&
8536 tpacpi_is_fw_digit(s[4]) &&
Keith Packardb569ab32011-03-31 15:22:33 -07008537 tpacpi_is_fw_digit(s[5]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008538}
8539
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008540/* returns 0 - probe ok, or < 0 - probe error.
8541 * Probe ok doesn't mean thinkpad found.
8542 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8543static int __must_check __init get_thinkpad_model_data(
8544 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008545{
Jeff Garzik18552562007-10-03 15:15:40 -04008546 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008547 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008548 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008549
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008550 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008551 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008552
8553 memset(tp, 0, sizeof(*tp));
8554
8555 if (dmi_name_in_vendors("IBM"))
8556 tp->vendor = PCI_VENDOR_ID_IBM;
8557 else if (dmi_name_in_vendors("LENOVO"))
8558 tp->vendor = PCI_VENDOR_ID_LENOVO;
8559 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008560 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008561
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008562 s = dmi_get_system_info(DMI_BIOS_VERSION);
8563 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8564 if (s && !tp->bios_version_str)
8565 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008566
8567 /* Really ancient ThinkPad 240X will fail this, which is fine */
8568 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008569 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008570
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008571 tp->bios_model = tp->bios_version_str[0]
8572 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008573 tp->bios_release = (tp->bios_version_str[4] << 8)
8574 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008575
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008576 /*
8577 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8578 * X32 or newer, all Z series; Some models must have an
8579 * up-to-date BIOS or they will not be detected.
8580 *
8581 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8582 */
8583 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008584 if (sscanf(dev->name,
8585 "IBM ThinkPad Embedded Controller -[%17c",
8586 ec_fw_string) == 1) {
8587 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8588 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008589
8590 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008591 if (!tp->ec_version_str)
8592 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008593
8594 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8595 tp->ec_model = ec_fw_string[0]
8596 | (ec_fw_string[1] << 8);
8597 tp->ec_release = (ec_fw_string[4] << 8)
8598 | ec_fw_string[5];
8599 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008600 pr_notice("ThinkPad firmware release %s "
8601 "doesn't match the known patterns\n",
8602 ec_fw_string);
8603 pr_notice("please report this to %s\n",
8604 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008605 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008606 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008607 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008608 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008609
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008610 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8611 if (s && !strnicmp(s, "ThinkPad", 8)) {
8612 tp->model_str = kstrdup(s, GFP_KERNEL);
8613 if (!tp->model_str)
8614 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008615 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008616
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008617 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8618 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8619 if (s && !tp->nummodel_str)
8620 return -ENOMEM;
8621
8622 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008623}
8624
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008625static int __init probe_for_thinkpad(void)
8626{
8627 int is_thinkpad;
8628
8629 if (acpi_disabled)
8630 return -ENODEV;
8631
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008632 /* It would be dangerous to run the driver in this case */
8633 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8634 return -ENODEV;
8635
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008636 /*
8637 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008638 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008639 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008640 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8641 (thinkpad_id.ec_model != 0) ||
8642 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008643
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03008644 /* The EC handler is required */
8645 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008646 if (!ec_handle) {
8647 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07008648 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008649 return -ENODEV;
8650 }
8651
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008652 if (!is_thinkpad && !force_load)
8653 return -ENODEV;
8654
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008655 return 0;
8656}
8657
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008658static void __init thinkpad_acpi_init_banner(void)
8659{
Joe Perches0978e012011-04-04 10:06:25 -07008660 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8661 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008662
Joe Perches0978e012011-04-04 10:06:25 -07008663 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008664 (thinkpad_id.bios_version_str) ?
8665 thinkpad_id.bios_version_str : "unknown",
8666 (thinkpad_id.ec_version_str) ?
8667 thinkpad_id.ec_version_str : "unknown");
8668
8669 BUG_ON(!thinkpad_id.vendor);
8670
8671 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07008672 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008673 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8674 "IBM" : ((thinkpad_id.vendor ==
8675 PCI_VENDOR_ID_LENOVO) ?
8676 "Lenovo" : "Unknown vendor"),
8677 thinkpad_id.model_str,
8678 (thinkpad_id.nummodel_str) ?
8679 thinkpad_id.nummodel_str : "unknown");
8680}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008681
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008682/* Module init, exit, parameters */
8683
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008684static struct ibm_init_struct ibms_init[] __initdata = {
8685 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008686 .data = &thinkpad_acpi_driver_data,
8687 },
8688 {
8689 .init = hotkey_init,
8690 .data = &hotkey_driver_data,
8691 },
8692 {
8693 .init = bluetooth_init,
8694 .data = &bluetooth_driver_data,
8695 },
8696 {
8697 .init = wan_init,
8698 .data = &wan_driver_data,
8699 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008700 {
8701 .init = uwb_init,
8702 .data = &uwb_driver_data,
8703 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008704#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008705 {
8706 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008707 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008708 .data = &video_driver_data,
8709 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008710#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008711 {
8712 .init = light_init,
8713 .data = &light_driver_data,
8714 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008715 {
8716 .init = cmos_init,
8717 .data = &cmos_driver_data,
8718 },
8719 {
8720 .init = led_init,
8721 .data = &led_driver_data,
8722 },
8723 {
8724 .init = beep_init,
8725 .data = &beep_driver_data,
8726 },
8727 {
8728 .init = thermal_init,
8729 .data = &thermal_driver_data,
8730 },
8731 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008732 .init = brightness_init,
8733 .data = &brightness_driver_data,
8734 },
8735 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008736 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008737 .data = &volume_driver_data,
8738 },
8739 {
8740 .init = fan_init,
8741 .data = &fan_driver_data,
8742 },
8743};
8744
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008745static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8746{
8747 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008748 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008749
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008750 if (!kp || !kp->name || !val)
8751 return -EINVAL;
8752
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008753 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8754 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008755 WARN_ON(ibm == NULL);
8756
8757 if (!ibm || !ibm->name)
8758 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008759
8760 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8761 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008762 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008763 strcpy(ibms_init[i].param, val);
8764 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008765 return 0;
8766 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008767 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008768
8769 return -EINVAL;
8770}
8771
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008772module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008773MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008774 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008775
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008776module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008777MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008778
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008779module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008780MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008781 "Attempts to load the driver even on a "
8782 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008783
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008784module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008785MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008786 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008787
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008788module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008789MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008790 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008791 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008792
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008793module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008794MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008795 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008796
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008797module_param(hotkey_report_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008798MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008799 "used for backwards compatibility with userspace, "
8800 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008801
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008802#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008803module_param_named(volume_mode, volume_mode, uint, 0444);
8804MODULE_PARM_DESC(volume_mode,
8805 "Selects volume control strategy: "
8806 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8807
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008808module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8809MODULE_PARM_DESC(volume_capabilities,
8810 "Selects the mixer capabilites: "
8811 "0=auto, 1=volume and mute, 2=mute only");
8812
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008813module_param_named(volume_control, volume_control_allowed, bool, 0444);
8814MODULE_PARM_DESC(volume_control,
8815 "Enables software override for the console audio "
8816 "control when true");
8817
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008818/* ALSA module API parameters */
8819module_param_named(index, alsa_index, int, 0444);
8820MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8821module_param_named(id, alsa_id, charp, 0444);
8822MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8823module_param_named(enable, alsa_enable, bool, 0444);
8824MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008825#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008826
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008827#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008828 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008829 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008830 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008831
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008832TPACPI_PARAM(hotkey);
8833TPACPI_PARAM(bluetooth);
8834TPACPI_PARAM(video);
8835TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008836TPACPI_PARAM(cmos);
8837TPACPI_PARAM(led);
8838TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008839TPACPI_PARAM(brightness);
8840TPACPI_PARAM(volume);
8841TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008842
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008843#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008844module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008845MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8846module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8847MODULE_PARM_DESC(wlsw_state,
8848 "Initial state of the emulated WLSW switch");
8849
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008850module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008851MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8852module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8853MODULE_PARM_DESC(bluetooth_state,
8854 "Initial state of the emulated bluetooth switch");
8855
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008856module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008857MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8858module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8859MODULE_PARM_DESC(wwan_state,
8860 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008861
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008862module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008863MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8864module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8865MODULE_PARM_DESC(uwb_state,
8866 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008867#endif
8868
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008869static void thinkpad_acpi_module_exit(void)
8870{
8871 struct ibm_struct *ibm, *itmp;
8872
8873 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8874
8875 list_for_each_entry_safe_reverse(ibm, itmp,
8876 &tpacpi_all_drivers,
8877 all_drivers) {
8878 ibm_exit(ibm);
8879 }
8880
8881 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8882
8883 if (tpacpi_inputdev) {
8884 if (tp_features.input_device_registered)
8885 input_unregister_device(tpacpi_inputdev);
8886 else
8887 input_free_device(tpacpi_inputdev);
8888 }
8889
8890 if (tpacpi_hwmon)
8891 hwmon_device_unregister(tpacpi_hwmon);
8892
8893 if (tp_features.sensors_pdev_attrs_registered)
8894 device_remove_file(&tpacpi_sensors_pdev->dev,
8895 &dev_attr_thinkpad_acpi_pdev_name);
8896 if (tpacpi_sensors_pdev)
8897 platform_device_unregister(tpacpi_sensors_pdev);
8898 if (tpacpi_pdev)
8899 platform_device_unregister(tpacpi_pdev);
8900
8901 if (tp_features.sensors_pdrv_attrs_registered)
8902 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8903 if (tp_features.platform_drv_attrs_registered)
8904 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8905
8906 if (tp_features.sensors_pdrv_registered)
8907 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8908
8909 if (tp_features.platform_drv_registered)
8910 platform_driver_unregister(&tpacpi_pdriver);
8911
8912 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008913 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008914
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008915 if (tpacpi_wq)
8916 destroy_workqueue(tpacpi_wq);
8917
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008918 kfree(thinkpad_id.bios_version_str);
8919 kfree(thinkpad_id.ec_version_str);
8920 kfree(thinkpad_id.model_str);
8921}
8922
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008923
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008924static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925{
8926 int ret, i;
8927
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008928 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8929
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008930 /* Parameter checking */
8931 if (hotkey_report_mode > 2)
8932 return -EINVAL;
8933
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008934 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008935
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008936 ret = get_thinkpad_model_data(&thinkpad_id);
8937 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07008938 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008939 thinkpad_acpi_module_exit();
8940 return ret;
8941 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008942 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008943 if (ret) {
8944 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008945 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008947
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008948 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008949
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008950 thinkpad_acpi_init_banner();
8951 tpacpi_check_outdated_fw();
8952
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008953 TPACPI_ACPIHANDLE_INIT(ecrd);
8954 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008956 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8957 if (!tpacpi_wq) {
8958 thinkpad_acpi_module_exit();
8959 return -ENOMEM;
8960 }
8961
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008962 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008963 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07008964 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008965 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008966 return -ENODEV;
8967 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008968
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008969 ret = platform_driver_register(&tpacpi_pdriver);
8970 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07008971 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008972 thinkpad_acpi_module_exit();
8973 return ret;
8974 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008975 tp_features.platform_drv_registered = 1;
8976
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008977 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8978 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07008979 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008980 thinkpad_acpi_module_exit();
8981 return ret;
8982 }
8983 tp_features.sensors_pdrv_registered = 1;
8984
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008985 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008986 if (!ret) {
8987 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008988 ret = tpacpi_create_driver_attributes(
8989 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008990 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008991 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07008992 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008993 thinkpad_acpi_module_exit();
8994 return ret;
8995 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008996 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008997
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008998
8999 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009000 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009001 NULL, 0);
9002 if (IS_ERR(tpacpi_pdev)) {
9003 ret = PTR_ERR(tpacpi_pdev);
9004 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009005 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009006 thinkpad_acpi_module_exit();
9007 return ret;
9008 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009009 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009010 TPACPI_HWMON_DRVR_NAME,
9011 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009012 if (IS_ERR(tpacpi_sensors_pdev)) {
9013 ret = PTR_ERR(tpacpi_sensors_pdev);
9014 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009015 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009016 thinkpad_acpi_module_exit();
9017 return ret;
9018 }
9019 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9020 &dev_attr_thinkpad_acpi_pdev_name);
9021 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009022 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009023 thinkpad_acpi_module_exit();
9024 return ret;
9025 }
9026 tp_features.sensors_pdev_attrs_registered = 1;
9027 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009028 if (IS_ERR(tpacpi_hwmon)) {
9029 ret = PTR_ERR(tpacpi_hwmon);
9030 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009031 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009032 thinkpad_acpi_module_exit();
9033 return ret;
9034 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009035 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009036 tpacpi_inputdev = input_allocate_device();
9037 if (!tpacpi_inputdev) {
Joe Perches0978e012011-04-04 10:06:25 -07009038 pr_err("unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009039 thinkpad_acpi_module_exit();
9040 return -ENOMEM;
9041 } else {
9042 /* Prepare input device, but don't register */
9043 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009044 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009045 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009046 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009047 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9048 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009049 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009050 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009051
9052 /* Init subdriver dependencies */
9053 tpacpi_detect_brightness_capabilities();
9054
9055 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009056 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9057 ret = ibm_init(&ibms_init[i]);
9058 if (ret >= 0 && *ibms_init[i].param)
9059 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009060 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009061 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009062 return ret;
9063 }
9064 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009065
9066 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9067
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009068 ret = input_register_device(tpacpi_inputdev);
9069 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009070 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009071 thinkpad_acpi_module_exit();
9072 return ret;
9073 } else {
9074 tp_features.input_device_registered = 1;
9075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009076
9077 return 0;
9078}
9079
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009080MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9081
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009082/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009083 * This will autoload the driver in almost every ThinkPad
9084 * in widespread use.
9085 *
9086 * Only _VERY_ old models, like the 240, 240x and 570 lack
9087 * the HKEY event interface.
9088 */
9089MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9090
9091/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009092 * DMI matching for module autoloading
9093 *
9094 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9095 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9096 *
9097 * Only models listed in thinkwiki will be supported, so add yours
9098 * if it is not there yet.
9099 */
9100#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009101 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009102
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009103/* Ancient thinkpad BIOSes have to be identified by
9104 * BIOS type or model number, and there are far less
9105 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009106IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009107
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009108MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9109MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009110MODULE_DESCRIPTION(TPACPI_DESC);
9111MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009112MODULE_LICENSE("GPL");
9113
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009114module_init(thinkpad_acpi_module_init);
9115module_exit(thinkpad_acpi_module_exit);